Skip to content

Commit b64303c

Browse files
committed
Use actions/github-script to get data for notifications
1 parent 6fc7afb commit b64303c

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

.github/workflows/notifications.yml

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,26 @@ jobs:
1616
runs-on: ubuntu-20.04
1717
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
1818
steps:
19-
- name: Output Variables
19+
- name: Data
20+
uses: actions/github-script@v5
2021
continue-on-error: true
21-
id: vars
22-
run: |
23-
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')
24-
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')
25-
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')
26-
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')
27-
message_sanitized=$(awk '{gsub("&","&amp;");gsub("<","\\&lt;");gsub(">","\\&gt;");print}' <<<'${{ github.event.workflow_run.head_commit.message }}')
22+
id: data
23+
with:
24+
script: |
25+
const message = context.payload.workflow_run.head_commit.message
26+
message_sanitized = message.split('\n')[0]
27+
28+
const check_data = (await github.rest.checks.listForRef({
29+
owner: context.payload.repository.owner.login,
30+
repo: context.payload.repository.name,
31+
ref: context.payload.workflow_run.head_commit.id,
32+
})).data.check_runs.filter(check_run => check_run.conclusion === 'failure')[0]
2833
29-
echo "::set-output name=name::$name"
30-
echo "::set-output name=url::$url"
31-
echo "::set-output name=event::$event"
32-
echo "::set-output name=message::$message_sanitized"
34+
return {
35+
job_name: check_data.name,
36+
job_url: check_data.html_url,
37+
commit_message: message_sanitized,
38+
}
3339
3440
- name: Send Notification
3541
uses: 8398a7/action-slack@v3
@@ -41,16 +47,16 @@ jobs:
4147
icon_emoji: ':github:',
4248
mention: 'channel',
4349
attachments: [{
44-
title: '[${{ github.event.repository.full_name }}] ${{ github.event.workflow.name }} pipeline has failed (${{ steps.vars.outputs.event }})',
50+
title: '[${{ github.event.repository.full_name }}] ${{ github.event.workflow.name }} pipeline has failed (${{ github.event.workflow_run.event }})',
4551
color: 'danger',
4652
fields: [{
4753
title: 'Commit',
48-
value: `<https://github.com/${{ github.repository }}/commit/${{ github.event.workflow_run.head_commit.id }}|${{ steps.vars.outputs.message }}>`,
54+
value: `<https://github.com/${{ github.repository }}/commit/${{ github.event.workflow_run.head_commit.id }}|${{ fromJSON(steps.data.outputs.result).commit_message }}>`,
4955
short: true
5056
},
5157
{
5258
title: 'Failed Job',
53-
value: `<${{ steps.vars.outputs.url }}|${{ steps.vars.outputs.name }}>`,
59+
value: `<${{ fromJSON(steps.data.outputs.result).job_url }}|${{ fromJSON(steps.data.outputs.result).job_name }}>`,
5460
short: true
5561
},
5662
{

0 commit comments

Comments
 (0)