From d457ac0c36230f83779997f4928fb99cd6439ef7 Mon Sep 17 00:00:00 2001 From: Aleksandr Shumilov Date: Mon, 9 Sep 2024 10:38:26 +0100 Subject: [PATCH] [FX-NULL] Add ability to snooze notification (#4538) --- ...y-about-pending-release-pull-requests.yaml | 126 +++++++++++------- 1 file changed, 77 insertions(+), 49 deletions(-) diff --git a/.github/workflows/notify-about-pending-release-pull-requests.yaml b/.github/workflows/notify-about-pending-release-pull-requests.yaml index 7db03c7524..29f0ada8c7 100644 --- a/.github/workflows/notify-about-pending-release-pull-requests.yaml +++ b/.github/workflows/notify-about-pending-release-pull-requests.yaml @@ -1,3 +1,9 @@ +# This workflow sends a Slack notification to the #frontend-exp-team-notifications channel +# if there is a pending pull request with the title "Version Packages" that has been open for more than 3 hours. +# +# Use STALE_VERSION_PACKAGES_NOTIFICATION_SNOOZE_UNITL repository variable (https://github.com/toptal/picasso/settings/variables/actions) +# to snooze notification unitl specific moment of time. The variable value should be in the date ISO format. + name: Notify about pending release pull requests on: @@ -6,8 +12,30 @@ on: workflow_dispatch: jobs: + check-if-notification-is-snoozed: + runs-on: ubuntu-latest + outputs: + snooze-notification: ${{ steps.snooze-notification.outputs.result }} + steps: + - name: Check if the notification is snoozed + uses: actions/github-script@v7 + id: snooze-notification + env: + SNOOZE_UNTIL: ${{ vars.STALE_VERSION_PACKAGES_NOTIFICATION_SNOOZE_UNITL }} + with: + script: | + const snoozeUntil = new Date(process.env.SNOOZE_UNTIL) + const now = new Date() + + console.log("Snooze until: ", snoozeUntil.toISOString()) + console.log("Current time: ", now.toISOString()) + + return (now <= snoozeUntil) + check-pending-release-pull-requests: runs-on: ubuntu-latest + needs: check-if-notification-is-snoozed + if: ${{ needs.check-if-notification-is-snoozed.outputs.snooze-notification == 'false' }} permissions: contents: write id-token: write @@ -15,59 +43,59 @@ jobs: issues: write pull-requests: write steps: - - name: Check out the repository - uses: actions/checkout@v4 + - name: Check out the repository + uses: actions/checkout@v4 - - name: GSM Secrets - id: secrets_manager - uses: toptal/davinci-github-actions/gsm-secrets@v15.3.1 - with: - workload_identity_provider: ${{ secrets.IDENTITY_POOL }} - service_account: ${{ secrets.SA_IDENTITY_POOL }} - secrets_name: |- - SLACK_BOT_TOKEN:toptal-ci/SLACK_BOT_TOKEN - TOPTAL_DEVBOT_TOKEN:toptal-ci/TOPTAL_DEVBOT_TOKEN + - name: GSM Secrets + id: secrets_manager + uses: toptal/davinci-github-actions/gsm-secrets@v15.3.1 + with: + workload_identity_provider: ${{ secrets.IDENTITY_POOL }} + service_account: ${{ secrets.SA_IDENTITY_POOL }} + secrets_name: |- + SLACK_BOT_TOKEN:toptal-ci/SLACK_BOT_TOKEN + TOPTAL_DEVBOT_TOKEN:toptal-ci/TOPTAL_DEVBOT_TOKEN - - name: Parse secrets - id: parse_secrets - uses: toptal/davinci-github-actions/expose-json-outputs@v15.3.1 - with: - json: ${{ steps.secrets_manager.outputs.secrets }} + - name: Parse secrets + id: parse_secrets + uses: toptal/davinci-github-actions/expose-json-outputs@v15.3.1 + with: + json: ${{ steps.secrets_manager.outputs.secrets }} - - name: Set ENV Variables - shell: bash - run: |- - echo "SLACK_BOT_TOKEN=${{ steps.parse_secrets.outputs.SLACK_BOT_TOKEN }}" >> $GITHUB_ENV - echo "GITHUB_TOKEN=${{ steps.parse_secrets.outputs.TOPTAL_DEVBOT_TOKEN }}" >> $GITHUB_ENV + - name: Set ENV Variables + shell: bash + run: |- + echo "SLACK_BOT_TOKEN=${{ steps.parse_secrets.outputs.SLACK_BOT_TOKEN }}" >> $GITHUB_ENV + echo "GITHUB_TOKEN=${{ steps.parse_secrets.outputs.TOPTAL_DEVBOT_TOKEN }}" >> $GITHUB_ENV - - name: Find version packages pull requests - uses: actions/github-script@v7 - id: find-pull-requests - with: - github-token: ${{ env.GITHUB_TOKEN }} - script: | - const { data: prs } = await github.rest.pulls.list({ - owner: context.repo.owner, - repo: context.repo.repo, - state: 'open', - }) + - name: Find version packages pull requests + uses: actions/github-script@v7 + id: find-pull-requests + with: + github-token: ${{ env.GITHUB_TOKEN }} + script: | + const { data: prs } = await github.rest.pulls.list({ + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open', + }) - const threeHoursInMilliseconds = 3 * 60 * 60 * 1000 + const threeHoursInMilliseconds = 3 * 60 * 60 * 1000 - // Find a PR with the title "Version Packages" that has been open long enough - const now = new Date() - const targetPR = prs.find(pr => pr.title.includes('Version Packages') && (now - new Date(pr.created_at)) > threeHoursInMilliseconds) + // Find a PR with the title "Version Packages" that has been open long enough + const now = new Date() + const targetPR = prs.find(pr => pr.title.includes('Version Packages') && (now - new Date(pr.created_at)) > threeHoursInMilliseconds) - if (targetPR) { - // If there is at least one stale pull request, require notification - core.setOutput('notification_is_needed', 'true') - } - - - name: Send a Slack notification - if: ${{ steps.find-pull-requests.outputs.notification_is_needed == 'true' }} - uses: slackapi/slack-github-action@v1.27.0 - env: - SLACK_BOT_TOKEN: ${{ env.SLACK_BOT_TOKEN }} - with: - channel-id: "-frontend-exp-team-notifications" - slack-message: ":x: There is stale Version Packages pull request in , review and merge it." + if (targetPR) { + // If there is at least one stale pull request, require notification + core.setOutput('notification_is_needed', 'true') + } + + - name: Send a Slack notification + if: ${{ steps.find-pull-requests.outputs.notification_is_needed == 'true' }} + uses: slackapi/slack-github-action@v1.27.0 + env: + SLACK_BOT_TOKEN: ${{ env.SLACK_BOT_TOKEN }} + with: + channel-id: "-frontend-exp-team-notifications" + slack-message: ":x: There is stale Version Packages pull request in , review and merge it. To snooze notifications, refer to the documentaion in the workflow code."