Skip to content

Commit

Permalink
mwpw-152624: improve stage PR communication (#2474)
Browse files Browse the repository at this point in the history
mwpw-152624: improve stage communication
  • Loading branch information
mokimo authored Jun 17, 2024
1 parent 4e8e74c commit 82b5a83
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions .github/workflows/merge-to-stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ const hasFailingChecks = (checks) =>
name !== 'merge-to-stage' && conclusion === 'failure'
);

const commentOnPR = async (comment, prNumber) => {
console.log(comment); // Logs for debugging the action.
const { data: comments } = await github.rest.issues.listComments({
owner,
repo,
issue_number: prNumber,
});

const dayAgo = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
const hasRecentComment = comments
.filter(({ created_at }) => new Date(created_at) > dayAgo)
.some(({ body }) => body === comment);
if (hasRecentComment) return console.log('Comment exists for', prNumber);

await github.rest.issues.createComment({
owner,
repo,
issue_number: prNumber,
body: comment,
});
};

const getPRs = async () => {
let prs = await github.rest.pulls
.list({ owner, repo, state: 'open', per_page: 100, base: STAGE })
Expand All @@ -69,13 +91,19 @@ const getPRs = async () => {

prs = prs.filter(({ checks, reviews, number, title }) => {
if (hasFailingChecks(checks)) {
console.log(`Skipping ${number}: ${title} due to failing checks`);
commentOnPR(
`Skipped merging ${number}: ${title} due to failing checks`,
number
);
return false;
}

const approvals = reviews.filter(({ state }) => state === 'APPROVED');
if (approvals.length < REQUIRED_APPROVALS) {
console.log(`Skipping ${number}: ${title} due to insufficient approvals`);
commentOnPR(
`Skipped merging ${number}: ${title} due to insufficient approvals. Required: ${REQUIRED_APPROVALS} approvals`,
number
);
return false;
}

Expand Down Expand Up @@ -103,7 +131,10 @@ const merge = async ({ prs, type }) => {
for await (const { number, files, html_url, title } of prs) {
try {
if (files.some((file) => SEEN[file])) {
console.log(`Skipping ${number}: ${title} due to overlap in files.`);
commentOnPR(
`Skipped ${number}: ${title} due to file overlap. Merging will be attempted in the next batch`,
number
);
continue;
}
if (type !== LABELS.zeroImpact) {
Expand All @@ -130,7 +161,7 @@ const merge = async ({ prs, type }) => {
);
await new Promise((resolve) => setTimeout(resolve, 5000));
} catch (error) {
console.log(`Error merging ${number}: ${title}`, error.message);
commentOnPR(`Error merging ${number}: ${title} ` + error.message, number);
}
}
};
Expand Down Expand Up @@ -187,6 +218,10 @@ const openStageToMainPR = async () => {
});

await slackNotification(SLACK.openedSyncPr({ html_url, number }));
await slackNotification(
SLACK.openedSyncPr({ html_url, number }),
process.env.MILO_STAGE_SLACK_WH
);
} catch (error) {
if (error.message.includes('No commits between main and stage'))
return console.log('No new commits, no stage->main PR opened');
Expand Down

0 comments on commit 82b5a83

Please sign in to comment.