From 16557791a7191c40856ff6c5949f9c971ea7d245 Mon Sep 17 00:00:00 2001 From: Rares Munteanu Date: Tue, 3 Dec 2024 14:30:09 +0100 Subject: [PATCH] [MWPW-163539] Update RCP workflow --- .github/workflows/helpers.js | 11 +++++++++-- .github/workflows/merge-to-main.js | 3 ++- .github/workflows/merge-to-main.yaml | 1 + .github/workflows/merge-to-stage.js | 6 +++--- .github/workflows/rcp-notifier.js | 12 ++++++++---- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/.github/workflows/helpers.js b/.github/workflows/helpers.js index dcc7b0025d..b8b733eb0c 100644 --- a/.github/workflows/helpers.js +++ b/.github/workflows/helpers.js @@ -43,7 +43,11 @@ const RCPDates = [ }, ]; -const isWithinRCP = (offset = 0) => { +const isShortRCP = (start, end) => { + return ((end - start) / (1000 * 60 * 60)) < 24; +}; + +const isWithinRCP = ({ offset = 0, excludeShortRCP = false } = {}) => { const now = new Date(); if (now.getFullYear() !== CURRENT_YEAR) { console.log(`ADD NEW RCPs for ${CURRENT_YEAR + 1}`); @@ -53,7 +57,9 @@ const isWithinRCP = (offset = 0) => { if (RCPDates.some(({ start, end }) => { const adjustedStart = new Date(start); adjustedStart.setDate(adjustedStart.getDate() - offset); - return start <= now && now <= end + const match = adjustedStart <= now && now <= end; + if (!match || (excludeShortRCP && isShortRCP(start, end))) return false; + return true; })) { console.log( 'Current date is within a RCP (2 days earlier for stage, to keep stage clean & make CSO contributions during an RCP easier). Stopping execution.' @@ -148,6 +154,7 @@ module.exports = { getLocalConfigs, slackNotification, pulls: { addLabels, addFiles, getChecks, getReviews }, + isShortRCP, isWithinRCP, RCPDates, }; diff --git a/.github/workflows/merge-to-main.js b/.github/workflows/merge-to-main.js index efbc9e55bc..286ddaff19 100644 --- a/.github/workflows/merge-to-main.js +++ b/.github/workflows/merge-to-main.js @@ -9,6 +9,7 @@ const { const PR_TITLE = '[Release] Stage to Main'; const STAGE = 'stage'; const PROD = 'main'; +const MIN_SOT_APPROVALS = process.env.MIN_SOT_APPROVALS ? Number(process.env.MIN_SOT_APPROVALS) : 4; let github, owner, repo; @@ -40,7 +41,7 @@ const main = async (params) => { const stageToMainPR = await getStageToMainPR(); const signOffs = stageToMainPR?.labels.filter((l) => l.includes('SOT')); console.log(`${signOffs.length} SOT labels on PR ${stageToMainPR.number}`); - if (signOffs.length >= 4) { + if (signOffs.length >= MIN_SOT_APPROVALS) { console.log('Stage to Main PR has all required labels. Merging...'); await github.rest.pulls.merge({ owner, diff --git a/.github/workflows/merge-to-main.yaml b/.github/workflows/merge-to-main.yaml index ada833d19f..9800b0435a 100644 --- a/.github/workflows/merge-to-main.yaml +++ b/.github/workflows/merge-to-main.yaml @@ -9,6 +9,7 @@ on: env: MILO_RELEASE_SLACK_WH: ${{ secrets.MILO_RELEASE_SLACK_WH }} + MIN_SOT_APPROVALS: ${{ secrets.MIN_SOT_APPROVALS }} jobs: merge-to-main: diff --git a/.github/workflows/merge-to-stage.js b/.github/workflows/merge-to-stage.js index 71f290ace4..3923d72edd 100644 --- a/.github/workflows/merge-to-stage.js +++ b/.github/workflows/merge-to-stage.js @@ -31,8 +31,8 @@ const SLACK = { openedSyncPr: ({ html_url, number }) => `:fast_forward: Created <${html_url}|Stage to Main PR ${number}>`, }; -let github; -let owner; +let github; +let owner; let repo; let body = ` @@ -230,7 +230,7 @@ const main = async (params) => { github = params.github; owner = params.context.repo.owner; repo = params.context.repo.repo; - if (isWithinRCP(process.env.STAGE_RCP_OFFSET_DAYS || 2)) return console.log('Stopped, within RCP period.'); + if (isWithinRCP({ offset: process.env.STAGE_RCP_OFFSET_DAYS || 2, excludeShortRCP: true })) return console.log('Stopped, within RCP period.'); try { const stageToMainPR = await getStageToMainPR(); diff --git a/.github/workflows/rcp-notifier.js b/.github/workflows/rcp-notifier.js index ad5bdd2cde..11793238a6 100644 --- a/.github/workflows/rcp-notifier.js +++ b/.github/workflows/rcp-notifier.js @@ -2,10 +2,13 @@ const { slackNotification, getLocalConfigs, RCPDates, + isShortRCP, } = require('./helpers.js'); -const isWithin24Hours = (targetDate) => - Math.abs(new Date() - targetDate) <= 24 * 60 * 60 * 1000; +const isWithin24Hours = (targetDate) => { + const now = new Date(); + return now < targetDate && new Date(now.getTime() + 24 * 60 * 60 * 1000) > targetDate; +}; const calculateDateOffset = (date, offset) => { const newDate = new Date(date); @@ -19,17 +22,18 @@ const main = async () => { for (const rcp of RCPDates) { const start = new Date(rcp.start); const end = new Date(rcp.end); + const isShort = isShortRCP(start, end); const tenDaysBefore = calculateDateOffset(start, 10); const fourDaysBefore = calculateDateOffset(start, 4); const stageOffset = Number(process.env.STAGE_RCP_OFFSET_DAYS) || 2; const slackText = (days) => `Reminder RCP starts in ${days} days: from ${start.toUTCString()} to ${end.toUTCString()}. Merges to stage will be disabled beginning ${calculateDateOffset(start, stageOffset).toUTCString()}.`; - if (isWithin24Hours(tenDaysBefore)) { + if (isWithin24Hours(tenDaysBefore) && !isShort) { console.log('Is within 24 hours of 10 days before RCP'); await slackNotification(slackText(10), process.env.MILO_DEV_HOOK); } - if (isWithin24Hours(fourDaysBefore)) { + if (isWithin24Hours(fourDaysBefore) && !isShort) { console.log('Is within 24 hours of 4 days before RCP'); await slackNotification(slackText(4), process.env.MILO_DEV_HOOK); }