Skip to content

Commit

Permalink
fix: zoilist should notify when API PR is ready for review (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere authored Aug 8, 2023
1 parent a0bfe4e commit 9a7f466
Show file tree
Hide file tree
Showing 3 changed files with 619 additions and 15 deletions.
48 changes: 33 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Probot } from 'probot';
import { EventPayloads } from '@octokit/webhooks';
import { WebClient } from '@slack/web-api';

const API_REVIEW_REQUESTED_LABEL_ID = 1603621692; // api-review/requested
Expand All @@ -11,22 +12,39 @@ if (!SLACK_BOT_TOKEN && NODE_ENV !== 'test') {

const slack = new WebClient(SLACK_BOT_TOKEN);

/**
* Posts a message to Slack notifying the API WG that a PR needs review.
* @param pr The PR to post to Slack
*/
async function postToSlack(pr: EventPayloads.WebhookPayloadPullRequestPullRequest) {
const escapedTitle = pr.title.replace(
/[&<>]/g,
(x) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;' })[x]!,
);
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` +
`*<${pr._links.html.href}|${escapedTitle} (#${pr.number})>*`,
});
}

// We want to notify the API WG if a PR is labeled with the API review label and is ready for review.
// If a PR is already labeled with the API review label, it won't trigger a notification, so we want to
// notify the API WG when it becomes ready for review.
export = (app: Probot) => {
app.on('pull_request.labeled', async ({ payload }) => {
const { pull_request: pr, label } = payload;

if (label?.id === API_REVIEW_REQUESTED_LABEL_ID && !pr.draft) {
const escapedTitle = pr.title.replace(
/[&<>]/g,
(x) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;' })[x]!,
);
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` +
`*<${pr._links.html.href}|${escapedTitle} (#${pr.number})>*`,
});
app.on(['pull_request.labeled', 'pull_request.ready_for_review'], async ({ payload }) => {
const { pull_request: pr, label, action } = payload;

const hasAPILabel = (id?: number) => id === API_REVIEW_REQUESTED_LABEL_ID;

const shouldReviewNewAPIPR = action === 'labeled' && !pr.draft && hasAPILabel(label?.id);
const shouldReviewReadyAPIPR =
action === 'ready_for_review' && pr.labels.some(({ id }) => hasAPILabel(id));

if (shouldReviewReadyAPIPR || shouldReviewNewAPIPR) {
await postToSlack(pr);
}
});
};
Loading

0 comments on commit 9a7f466

Please sign in to comment.