Skip to content

Commit

Permalink
mwpw-151992: allow to always merge zero impact PRs (#2436)
Browse files Browse the repository at this point in the history
* mwpw-151992: allow to always merge zero impact PRs

* Label zero impact PRs in slack & PR description
  • Loading branch information
mokimo authored Jun 10, 2024
1 parent 537eec4 commit e5cb173
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions .github/workflows/merge-to-stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const LABELS = {
highPriority: 'high priority',
readyForStage: 'Ready for Stage',
SOTPrefix: 'SOT',
zeroImpact: 'zero-impact',
};
const TEAM_MENTIONS = [
'@adobecom/miq-sot',
Expand All @@ -24,8 +25,8 @@ const TEAM_MENTIONS = [
'@adobecom/document-cloud-sot',
];
const SLACK = {
merge: ({ html_url, number, title }) =>
`:merged: PR merged to stage: <${html_url}|${number}: ${title}>.`,
merge: ({ html_url, number, title, prefix = '' }) =>
`:merged: PR merged to stage: ${prefix} <${html_url}|${number}: ${title}>.`,
openedSyncPr: ({ html_url, number }) =>
`:fast_forward: Created <${html_url}|Stage to Main PR ${number}>`,
};
Expand All @@ -46,6 +47,7 @@ let body = `
`;

const isHighPrio = (labels) => labels.includes(LABELS.highPriority);
const isZeroImpact = (labels) => labels.includes(LABELS.zeroImpact);

const hasFailingChecks = (checks) =>
checks.some(
Expand Down Expand Up @@ -80,19 +82,34 @@ const getPRs = async () => {
return true;
});

return prs.reverse(); // OLD PRs first
return prs.reverse().reduce(
(categorizedPRs, pr) => {
if (isZeroImpact(pr.labels)) {
categorizedPRs.zeroImpactPRs.push(pr);
} else if (isHighPrio(pr.labels)) {
categorizedPRs.highImpactPRs.push(pr);
} else {
categorizedPRs.normalPRs.push(pr);
}
return categorizedPRs;
},
{ zeroImpactPRs: [], highImpactPRs: [], normalPRs: [] }
);
};

const merge = async ({ prs }) => {
console.log(`Merging ${prs.length || 0} PRs that are ready... `);
const merge = async ({ prs, type }) => {
console.log(`Merging ${prs.length || 0} ${type} PRs that are ready... `);

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.`);
continue;
}
files.forEach((file) => (SEEN[file] = true));
if (type !== LABELS.zeroImpact) {
files.forEach((file) => (SEEN[file] = true));
}

if (!process.env.LOCAL_RUN) {
await github.rest.pulls.merge({
owner,
Expand All @@ -101,12 +118,14 @@ const merge = async ({ prs }) => {
merge_method: 'squash',
});
}
body = `- ${html_url}\n${body}`;
const prefix = type === LABELS.zeroImpact ? ' [ZERO IMPACT]' : '';
body = `-${prefix} ${html_url}\n${body}`;
await slackNotification(
SLACK.merge({
html_url,
number,
title,
prefix,
})
);
await new Promise((resolve) => setTimeout(resolve, 5000));
Expand Down Expand Up @@ -185,11 +204,12 @@ const main = async (params) => {
const stageToMainPR = await getStageToMainPR();
console.log('has Stage to Main PR:', !!stageToMainPR);
if (stageToMainPR) body = stageToMainPR.body;
const { zeroImpactPRs, highImpactPRs, normalPRs } = await getPRs();
await merge({ prs: zeroImpactPRs, type: LABELS.zeroImpact });
if (stageToMainPR?.labels.some((label) => label.includes(LABELS.SOTPrefix)))
return console.log('PR exists & testing started. Stopping execution.');
const prs = await getPRs();
await merge({ prs: prs.filter(({ labels }) => isHighPrio(labels)) });
await merge({ prs: prs.filter(({ labels }) => !isHighPrio(labels)) });
await merge({ prs: highImpactPRs, type: LABELS.highPriority });
await merge({ prs: normalPRs, type: 'normal' });
if (!stageToMainPR) await openStageToMainPR();
if (stageToMainPR && body !== stageToMainPR.body) {
console.log("Updating PR's body...");
Expand Down

0 comments on commit e5cb173

Please sign in to comment.