Skip to content

Commit 374cd9a

Browse files
committed
Improve send-notification
1 parent 716018e commit 374cd9a

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed
Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
name: Send Notification
22
description: Sends a Google Chat message as a notification of the job's outcome
3+
34
inputs:
45
webhook-url:
56
description: 'Google Chat Webhook URL'
67
required: true
8+
79
runs:
810
using: composite
911
steps:
@@ -12,57 +14,51 @@ runs:
1214
env:
1315
GH_TOKEN: ${{ github.token }}
1416
run: |
15-
# Gather vars only available from github context
16-
commitUrl="${{ github.event.head_commit.url }}"
17-
author="${{ github.event.sender.login }}"
18-
authorUrl="${{ github.event.sender.html_url }}"
1917
# Pull additional details from GitHub API
2018
json=$(gh run view ${{ github.run_id }} -R ${{ github.repository }} --json name,number,displayTitle,url,headSha,jobs)
21-
# Determine overall run status (based on all jobs preceding this one)
22-
runStatus=$(echo $json | jq -r '.jobs | .[0:length - 1] | if all(.conclusion == "success" or .conclusion == "skipped") then "succeeded" elif any(.conclusion == "cancelled") then "cancelled" elif any(.conclusion == "failure") then "failed" else "unsuccessful" end')
23-
# Build job info messages, joined in single line (multiline breaks GITHUB_ENV)
24-
jobInfo=$(echo $json | jq -r '.jobs | .[0:length - 1] | map("<" + .url + "|" + .name + "> was " + .conclusion) | join(", ")')
25-
# Get sanitized display title (usually a commit message) from GitHub API
26-
displayTitle=$(echo $json | jq -r .displayTitle)
19+
# Get sanitized display title (usually a commit message or PR title) from GitHub API
20+
displayTitle=$(echo $json | jq -r .displayTitle | sed 's/"/\\"/g')
2721
# Parse workflow yaml file name from full ref name
28-
workflow=$(echo ${{ github.workflow_ref }} | awk -F'/|@' '{print $5}')
22+
workflow=$(echo ${{ github.workflow_ref }} | awk -F'/|@' '{ print $5 }')
2923
# Build workflow URL with branch name
3024
workflowUrl="${{ github.server_url }}/${{ github.repository }}/actions/workflows/${workflow}?query=branch%3A${{ github.ref_name }}"
3125
# Get workflow name from GitHub API (also available as "github.workflow")
32-
workflowName=$(echo $json | jq -r .name)
33-
# Parse 7-digit commit id from head sha
34-
shaId=$(echo $json | jq -r .headSha | awk '{ print substr($0, 0, 7) }')
26+
workflowName=$(echo $json | jq -r .name | sed 's/"/\\"/g')
3527
# Get workflow run URL from GitHub API
3628
runUrl=$(echo $json | jq -r .url)
3729
# Get workflow run number from GitHub API (also available as "github.run_number")
3830
runNumber=$(echo $json | jq -r .number)
31+
# Determine overall run status (based on all jobs preceding this one)
32+
runStatus=$(echo $json | jq -r '.jobs | map(select(.conclusion != "")) | if all(.conclusion == "success" or .conclusion == "skipped") then "succeeded" elif any(.conclusion == "failure") then "failed" elif any(.conclusion == "cancelled") then "cancelled" else "unsuccessful" end')
33+
# Gather author info
34+
author="${{ github.event.sender.login }}"
35+
authorUrl="${{ github.event.sender.html_url }}"
3936
# **** Templates ****
4037
# Workflow status with link to workflow run
4138
workflowStatus="<${runUrl}|${displayTitle}> ${runStatus}"
4239
# Workflow info with link to all workflow runs for this branch
4340
workflowInfo="<${workflowUrl}|${workflowName}> #${runNumber}"
44-
# Determine run info, with either commit info or manual run info
45-
if [ -z "${{ github.event.head_commit.url }}" ] ; then
46-
runInfo="Manually run by <${authorUrl}|${author}>"
47-
else
41+
# Determine run info, with either pull request, commit or manual run info
42+
if [ ! -z "${{ github.event.pull_request.html_url }}" ] ; then
43+
prNumber="${{ github.event.pull_request.number }}"
44+
prUrl="${{ github.event.pull_request.html_url }}"
45+
runInfo="Pull request #<${prUrl}|${prNumber}> opened by <${authorUrl}|${author}>"
46+
elif [ ! -z "${{ github.event.head_commit.url }}" ] ; then
47+
commitUrl="${{ github.event.head_commit.url }}"
48+
shaId=$(echo $json | jq -r .headSha | awk '{ print substr($0, 0, 7) }')
4849
runInfo="Commit <${commitUrl}|${shaId}> pushed by <${authorUrl}|${author}>"
50+
else
51+
runInfo="Manually run by <${authorUrl}|${author}>"
4952
fi
53+
# Build job info messages, joined in single line (multiline breaks GITHUB_ENV)
54+
jobInfo=$(echo $json | jq -r '.jobs | map(select(.conclusion != "")) | map("* <" + .url + "|" + .name + "> was " + .conclusion) | join("\\n")')
5055
# Set results as env vars
51-
echo "DISPLAY_TITLE=$displayTitle" >> $GITHUB_ENV
52-
echo "WORKFLOW_URL=$workflowUrl" >> $GITHUB_ENV
53-
echo "WORKFLOW_NAME=$workflowName" >> $GITHUB_ENV
5456
echo "WORKFLOW_STATUS=$workflowStatus" >> $GITHUB_ENV
5557
echo "WORKFLOW_INFO=$workflowInfo" >> $GITHUB_ENV
56-
echo "RUN_URL=$runUrl" >> $GITHUB_ENV
57-
echo "RUN_NUMBER=$runNumber" >> $GITHUB_ENV
5858
echo "RUN_INFO=$runInfo" >> $GITHUB_ENV
5959
echo "JOB_INFO=$jobInfo" >> $GITHUB_ENV
60-
echo "AUTHOR=$author" >> $GITHUB_ENV
61-
echo "AUTHOR_URL=$authorUrl" >> $GITHUB_ENV
62-
echo "SHA_ID=$shaId" >> $GITHUB_ENV
63-
echo "COMMIT_URL=$commitUrl" >> $GITHUB_ENV
6460
# Output status
6561
echo "result=$runStatus" >> $GITHUB_OUTPUT
6662
- shell: bash
6763
run: |
68-
curl -X POST '${{ inputs.webhook-url }}' -H 'Content-Type: application/json' -d '{ "text": "${{ env.WORKFLOW_STATUS }}\n\n${{ env.WORKFLOW_INFO }}: ${{ env.RUN_INFO }}\n${{ env.JOB_INFO }}" }' || true
64+
curl -X POST '${{ inputs.webhook-url }}' -H 'Content-Type: application/json' -d '{ "text": "${{ env.WORKFLOW_STATUS }}\n${{ env.WORKFLOW_INFO }}: ${{ env.RUN_INFO }}\n\n${{ env.JOB_INFO }}" }' || true

0 commit comments

Comments
 (0)