From b6394c2e9fcdea317ff350d59e985a40e02edd41 Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Fri, 8 Apr 2022 13:57:44 +0000 Subject: [PATCH] =?UTF-8?q?Build/Test=20Tools:=20Improve=20the=20accuracy?= =?UTF-8?q?=20of=20=E2=80=9Cfixed=E2=80=9D=20Slack=20notifications.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adjusts the logic of the Slack Notifications workflow to make the “fixed” notifications more reliable. Currently, the workflow looks at the immediately preceding workflow run for the current branch. This fails to indicate that a workflow is fixed when other unrelated commits are made, and when rerunning the workflow after a false failure (timeout, etc.). The workflow will now use the following logic to determine if something has been fixed: - When a workflow is rerun, the conclusion for the immediately preceding run attempt will now be used to determine if the current attempt has “fixed” the workflow. - When on the first run attempt for a workflow run, the workflow conclusion for the immediately preceding commit will be used. - When on the first run attempt for a workflow run and no preceding commits for the current branch are present (this is a fresh tag or branch), always consider it “fixed”. Props davidbaumwald. See #54742. git-svn-id: https://develop.svn.wordpress.org/trunk@53108 602fd350-edb4-49c9-b593-d223f7449a82 --- .github/workflows/slack-notifications.yml | 74 ++++++++++++++--------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/.github/workflows/slack-notifications.yml b/.github/workflows/slack-notifications.yml index 1e500dd394..4d646ac156 100644 --- a/.github/workflows/slack-notifications.yml +++ b/.github/workflows/slack-notifications.yml @@ -52,9 +52,8 @@ jobs: # submit data to Slack webhook URLs configured to post messages. # # Performs the following steps: - # - Retrieves the workflow ID (if necessary). - # - Retrieves the workflow URL (if necessary). - # - Retrieves the previous workflow run and stores its conclusion. + # - Retrieves the current workflow run. + # - Determine the conclusion of the previous workflow run or run attempt. # - Sets the previous conclusion as an output. # - Prepares the commit message. # - Constructs and stores a message payload as an output. @@ -68,10 +67,10 @@ jobs: payload: ${{ steps.create-payload.outputs.payload }} steps: - - name: Get the workflow ID - id: current-workflow-id + - name: Determine the status of the previous attempt + id: previous-attempt-result if: ${{ github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }} - uses: actions/github-script@441359b1a30438de65712c2fbca0abe4816fa667 # v5.0.0 + uses: actions/github-script@9ac08808f993958e9de277fe43a64532a609130e # v6.0.0 with: script: | const workflow_run = await github.rest.actions.getWorkflowRun({ @@ -79,36 +78,57 @@ jobs: repo: context.repo.repo, run_id: ${{ github.run_id }}, }); - return workflow_run.data.workflow_id; - - name: Get details about the previous workflow run - id: previous-result - uses: actions/github-script@441359b1a30438de65712c2fbca0abe4816fa667 # v5.0.0 - with: - script: | + // When a workflow has been restarted to fix a failure, check the previous run attempt. + if ( workflow_run.data.run_attempt > 1 ) { + const previous_run = await github.rest.actions.getWorkflowRunAttempt({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: ${{ github.run_id }}, + attempt_number: workflow_run.data.run_attempt - 1 + }); + + return previous_run.data.conclusion; + } + + let workflow_id = ''; + if ( ${{ github.event_name == 'workflow_run' }} ) { + workflow_id = '${{ github.event.workflow_run.workflow_id }}'; + } else { + workflow_id = workflow_run.data.workflow_id; + } + + // Otherwise, check the previous workflow run. const previous_runs = await github.rest.actions.listWorkflowRuns({ owner: context.repo.owner, repo: context.repo.repo, - workflow_id: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.workflow_id || steps.current-workflow-id.outputs.result }}, + workflow_id: workflow_id, branch: '${{ env.CURRENT_BRANCH }}', - per_page: 1, - page: 2, + exclude_pull_requests: true, }); - if ( previous_runs.data.total_count > 0 ) { - return previous_runs.data.workflow_runs[0].conclusion; - } else { - // Use failure so all first time runs for a branch or tag are reported to Slack. - return 'failure'; + // This is the first workflow run for this branch or tag. + if ( previous_runs.data.workflow_runs.length == 0 ) { + return 'none'; } + // Find the workflow run for the commit that immediately preceded this one. + for ( let i = 0; i < previous_runs.data.workflow_runs.length; i++ ) { + if ( previous_runs.data.workflow_runs[ i ].run_number == workflow_run.data.run_number ) { + return previous_runs.data.workflow_runs[ i + 1 ].conclusion; + } + } + + // Can't determine previous workflow conclusion. + return 'unknown'; + - name: Store previous conclusion as an output id: previous-conclusion - run: echo "::set-output name=previous_conclusion::${{ steps.previous-result.outputs.result }}" + run: echo "::set-output name=previous_conclusion::${{ steps.previous-attempt-result.outputs.result }}" - name: Get the commit message id: current-commit-message - uses: actions/github-script@441359b1a30438de65712c2fbca0abe4816fa667 # v5.0.0 + uses: actions/github-script@9ac08808f993958e9de277fe43a64532a609130e # v6.0.0 if: ${{ github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' }} with: script: | @@ -142,7 +162,7 @@ jobs: steps: - name: Post failure notifications to Slack - uses: slackapi/slack-github-action@410ae57cff5c6b682b106440be0e6c7eb8c98c9d # v1.16.0 + uses: slackapi/slack-github-action@16b6c78ee73689a627b65332b34e5d409c7299da # v1.18.0 with: payload: ${{ needs.prepare.outputs.payload }} env: @@ -154,11 +174,11 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 5 needs: [ prepare ] - if: ${{ needs.prepare.outputs.previous_conclusion == 'failure' && ( github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success' || inputs.calling_status == 'success' ) && success() }} + if: ${{ contains( fromJson( '["failure", "cancelled", "none"]' ), needs.prepare.outputs.previous_conclusion ) && ( github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success' || inputs.calling_status == 'success' ) && success() }} steps: - name: Post failure notifications to Slack - uses: slackapi/slack-github-action@410ae57cff5c6b682b106440be0e6c7eb8c98c9d # v1.16.0 + uses: slackapi/slack-github-action@16b6c78ee73689a627b65332b34e5d409c7299da # v1.18.0 with: payload: ${{ needs.prepare.outputs.payload }} env: @@ -174,7 +194,7 @@ jobs: steps: - name: Post success notifications to Slack - uses: slackapi/slack-github-action@410ae57cff5c6b682b106440be0e6c7eb8c98c9d # v1.16.0 + uses: slackapi/slack-github-action@16b6c78ee73689a627b65332b34e5d409c7299da # v1.18.0 with: payload: ${{ needs.prepare.outputs.payload }} env: @@ -190,7 +210,7 @@ jobs: steps: - name: Post cancelled notifications to Slack - uses: slackapi/slack-github-action@410ae57cff5c6b682b106440be0e6c7eb8c98c9d # v1.16.0 + uses: slackapi/slack-github-action@16b6c78ee73689a627b65332b34e5d409c7299da # v1.18.0 with: payload: ${{ needs.prepare.outputs.payload }} env: