From 8dbb8eca42028ebe09f4e3b627db95a181809b6a Mon Sep 17 00:00:00 2001 From: Luca Comellini Date: Wed, 26 Jan 2022 09:55:05 -0800 Subject: [PATCH] Use actions/github-script to get data for notifications (#2385) --- .github/workflows/notifications.yml | 36 +++++++++++++++++------------ 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/.github/workflows/notifications.yml b/.github/workflows/notifications.yml index cb0e7c7cbd..1edfd0e1a9 100644 --- a/.github/workflows/notifications.yml +++ b/.github/workflows/notifications.yml @@ -17,20 +17,26 @@ jobs: runs-on: ubuntu-20.04 if: ${{ github.event.workflow_run.conclusion == 'failure' }} steps: - - name: Output Variables + - name: Data + uses: actions/github-script@v5 continue-on-error: true - id: vars - run: | - suite_id=$(curl -sS -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }} | jq '.check_suite_id') - event=$(curl -sS -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }} | jq -r '.event') - name=$(curl -sS -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/check-suites/$suite_id/check-runs | jq -r '[.check_runs[] | select(.conclusion=="failure")][0].name') - url=$(curl -sS -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/check-suites/$suite_id/check-runs | jq -r '[.check_runs[] | select(.conclusion=="failure")][0].html_url') - message_sanitized=$(awk '{gsub("&","&");gsub("<","\\<");gsub(">","\\>");print}' <<<'${{ github.event.workflow_run.head_commit.message }}') + id: data + with: + script: | + const message = context.payload.workflow_run.head_commit.message + message_sanitized = message.replace(/&/g, '&').replace(//g, '>') + + const check_data = (await github.rest.checks.listForRef({ + owner: context.payload.repository.owner.login, + repo: context.payload.repository.name, + ref: context.payload.workflow_run.head_commit.id, + })).data.check_runs.filter(check_run => check_run.conclusion === 'failure')[0] - echo "::set-output name=name::$name" - echo "::set-output name=url::$url" - echo "::set-output name=event::$event" - echo "::set-output name=message::$message_sanitized" + return { + job_name: check_data.name, + job_url: check_data.html_url, + commit_message: message_sanitized, + } - name: Send Notification uses: 8398a7/action-slack@v3 @@ -42,16 +48,16 @@ jobs: icon_emoji: ':github:', mention: 'channel', attachments: [{ - title: '[${{ github.event.repository.full_name }}] ${{ github.event.workflow.name }} pipeline has failed (${{ steps.vars.outputs.event }})', + title: '[${{ github.event.repository.full_name }}] ${{ github.event.workflow.name }} pipeline has failed (${{ github.event.workflow_run.event }})', color: 'danger', fields: [{ title: 'Commit', - value: ``, + value: ``, short: true }, { title: 'Failed Job', - value: `<${{ steps.vars.outputs.url }}|${{ steps.vars.outputs.name }}>`, + value: `<${{ fromJSON(steps.data.outputs.result).job_url }}|${{ fromJSON(steps.data.outputs.result).job_name }}>`, short: true }, {