Skip to content

Commit

Permalink
feat: silence slack messages during quiet period (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzhao authored Dec 5, 2023
1 parent b9c2f64 commit 1ccd5db
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Probot } from 'probot';
import { EventPayloads } from '@octokit/webhooks';
import { WebClient } from '@slack/web-api';
import { isQuietPeriod } from './util';

const API_REVIEW_REQUESTED_LABEL_ID = 1603621692; // api-review/requested
const { SLACK_BOT_TOKEN, NODE_ENV } = process.env;
Expand All @@ -21,11 +22,12 @@ async function postToSlack(pr: EventPayloads.WebhookPayloadPullRequestPullReques
/[&<>]/g,
(x) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;' })[x]!,
);
const name = isQuietPeriod() ? 'API WG' : '<!subteam^SNSJW1BA9>';
await slack.chat.postMessage({
channel: '#wg-api',
unfurl_links: false,
text:
`Hey <!subteam^SNSJW1BA9>! Just letting you know that the following PR needs API review:\n` +
`Hey ${name}! Just letting you know that the following PR needs API review:\n` +
`*<${pr._links.html.href}|${escapedTitle} (#${pr.number})>*`,
});
}
Expand Down
5 changes: 4 additions & 1 deletion src/remind.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ProbotOctokit } from 'probot';
import { WebClient } from '@slack/web-api';
import { isQuietPeriod } from './util';
const { SLACK_BOT_TOKEN, NODE_ENV } = process.env;

if (!SLACK_BOT_TOKEN && NODE_ENV !== 'test') {
Expand All @@ -18,7 +19,9 @@ async function main() {
q: `repo:electron/electron ${q}`,
sort: 'created',
});
if (items.length) {

// silence during quiet period
if (items.length && !isQuietPeriod()) {
const text =
`:blob-wave: *Reminder:* the <${searchUrl}|following PRs> are awaiting API review.\n` +
items
Expand Down
6 changes: 6 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* If the current date on runtime matches Electron's quiet period
*/
export function isQuietPeriod() {
return new Date().getMonth() === 11;
}
40 changes: 31 additions & 9 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,39 @@ describe('New PR Slack Notifications', () => {
probot.load(zoilist);
});

it('posts to slack when the api-review/requested label is added', async () => {
const payload = require('./fixtures/pull_request.labeled.semver_minor.json');
afterEach(() => {
jest.useRealTimers();
});

await probot.receive({ name: 'pull_request', payload, id: 'abc123' });
describe('when the `api-review/requested` label is added', () => {
it('posts to slack', async () => {
jest.useFakeTimers('modern').setSystemTime(new Date('2023-11-11'));
const payload = require('./fixtures/pull_request.labeled.semver_minor.json');

await probot.receive({ name: 'pull_request', payload, id: 'abc123' });

expect(client.chat.postMessage).toHaveBeenCalledWith({
channel: '#wg-api',
unfurl_links: false,
text:
`Hey <!subteam^SNSJW1BA9>! Just letting you know that the following PR needs API review:\n` +
'*<https://github.com/electron/electron/pull/38982|feat: add `BrowserWindow.isOccluded()` (#38982)>*',
});
});

it('does not @mention anyone in the month of December', async () => {
jest.useFakeTimers('modern').setSystemTime(new Date('2023-12-25'));
const payload = require('./fixtures/pull_request.labeled.semver_minor.json');

await probot.receive({ name: 'pull_request', payload, id: 'abc123' });

expect(client.chat.postMessage).toHaveBeenCalledWith({
channel: '#wg-api',
unfurl_links: false,
text:
`Hey <!subteam^SNSJW1BA9>! Just letting you know that the following PR needs API review:\n` +
'*<https://github.com/electron/electron/pull/38982|feat: add `BrowserWindow.isOccluded()` (#38982)>*',
expect(client.chat.postMessage).toHaveBeenCalledWith({
channel: '#wg-api',
unfurl_links: false,
text:
`Hey API WG! Just letting you know that the following PR needs API review:\n` +
'*<https://github.com/electron/electron/pull/38982|feat: add `BrowserWindow.isOccluded()` (#38982)>*',
});
});
});

Expand Down

0 comments on commit 1ccd5db

Please sign in to comment.