-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
216 lines (190 loc) · 6.56 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
name: Notify Slack
author: Freckle
description: |
Minimal inputs action to notify Slack of Job status
inputs:
slack-webhook-url:
description: |
Slack webhook url, typically a repository secret.
required: true
slack-channel:
description: |
Explicit channel for this notification. If omitted (the default), the
channel configured in the webhook is used.
required: false
default: ""
event-name:
description: |
A name for the event being notified about. If not given, the
workflow and job id is used.
required: false
default: ""
message:
description: |
Additional content to add to the message. Details about the commit that
triggered the Job in which this step is run are always included.
required: false
default: ""
commit-sha:
description: |
Commit SHA to fetch details for. Default is the PR head sha or github.sha
if not in the context of a PR.
required: false
default: "${{ github.event.pull_request.head.sha || github.sha }}"
slack-users:
description: |
A JSON object (as a string in the Yaml) mapping GitHub usernames to Slack
User Ids (e.g. UXXXXXX). If present, the commit author is looked up in the
map and the Slack user, if found, is at-mentioned in the notification
details. If a Slack user is not found, an error is generated as a build
annotation.
required: false
default: ""
slack-users-file:
description: |
Relative path within the repository to read the slack-users JSON from a
file. The file is read from the default branch via the API.
required: false
default: ""
job-name:
description: |
An explicit job-name, in cases where github.job (the job.id) won't work
such as matrix jobs.
required: false
default: ""
job-status:
description: |
An explicit job-status, in cases where the status of the current job is not
the desired status to notify about.
required: false
default: ""
github-token:
description: ""
required: false
default: "${{ github.token }}"
dry-run:
description: "Don't actually notify (useful for testing)"
required: false
default: false
runs:
using: composite
steps:
- shell: bash
run: |
# Set global ENV
cat >>"$GITHUB_ENV" <<'EOM'
COMMIT_JSON=/tmp/commit.json
SLACK_USERS_JSON=/tmp/slack-users.json
EOM
- shell: bash
run: |
# Fetch commit.json
gh api "/repos/${{ github.repository }}/commits/$COMMIT_SHA" >"$COMMIT_JSON"
env:
GH_TOKEN: ${{ inputs.github-token }}
COMMIT_SHA: ${{ inputs.commit-sha }}
- if: ${{ inputs.slack-users }}
shell: bash
name: Write slack-users.json from input
run: echo "$USERS" >"$SLACK_USERS_JSON"
env:
USERS: ${{ inputs.slack-users }}
- if: ${{ inputs.slack-users-file }}
shell: bash
run: |
# Write slack-users.json from repository file
gh api "/repos/${{ github.repository }}/contents/$USERS_FILE" --jq '.content' |
base64 -d >"$SLACK_USERS_JSON"
env:
GH_TOKEN: ${{ inputs.github-token }}
USERS_FILE: ${{ inputs.slack-users-file }}
- shell: bash
run: |
# Set SLACK_TITLE
event_name=$EVENT_NAME
event_name=${event_name:-${{ github.workflow }} ${{ github.job }}}
{
case "${{ inputs.job-status || job.status }}" in
success)
echo "SLACK_TITLE=$event_name succeeded"
echo "SLACK_COLOR=success"
;;
failure)
echo "SLACK_TITLE=$event_name failed"
echo "SLACK_COLOR=danger"
;;
cancelled)
echo "SLACK_TITLE=$event_name was cancelled"
echo "SLACK_COLOR=grey"
;;
esac
} >> "$GITHUB_ENV"
env:
EVENT_NAME: ${{ inputs.event-name }}
- shell: bash
run: |
# Set SLACK_MESSAGE
# https://unix.stackexchange.com/a/451250
relative_date() {
awk -v date="$(date +%s -d "$1")" -v now="$(date +%s)" '
BEGIN { diff = now - date;
if (diff > (24*60*60)) printf "%.0f days ago", diff/(24*60*60);
else if (diff > (60*60)) printf "%.0f hours ago", diff/(60*60);
else if (diff > 60) printf "%.0f minutes ago", diff/60;
else printf "%s seconds ago", diff;
}'
}
commit_author=$(jq -r '.author.login' "$COMMIT_JSON")
commit_timestamp=$(jq -r '.commit.committer.date' "$COMMIT_JSON")
commit_timestamp_r=$(relative_date "$commit_timestamp")
commit_sha_short=$(jq -r '.sha' "$COMMIT_JSON" | head -c 7)
commit_url=$(jq -r '.html_url' "$COMMIT_JSON")
if [[ -f "$SLACK_USERS_JSON" ]]; then
author=$(jq -r ".\"$commit_author\" // \"unknown\" | \"<@\" + . + \">\"" "$SLACK_USERS_JSON")
else
author="$commit_author"
fi
if [[ "$author" == '<@unknown>' ]]; then
cat "$GITHUB_ACTION_PATH/unknown.txt" >&2
author="$author ($commit_author)"
fi
{
echo "SLACK_MESSAGE<<EOM"
printf '%s committed <%s|%s> %s (%s)\n' \
"$author" \
"$commit_url" \
"$commit_sha_short" \
"$commit_timestamp_r" \
"$commit_timestamp"
echo "$MESSAGE"
echo "EOM"
} >>"$GITHUB_ENV"
env:
MESSAGE: ${{ inputs.message }}
- shell: bash
run: |
# Set SLACK_FOOTER
url=$(
gh --repo '${{ github.repository }}' run view '${{ github.run_id }}' \
--json jobs \
--jq ".jobs[] | select(.name == \"$JOB_NAME\") | .url"
)
if [[ -z "$url" ]]; then
echo "No Job with name $JOB_NAME found in Workflow Runs:" >&2
gh --repo '${{ github.repository }}' run view '${{ github.run_id }}' \
--json jobs --jq '.jobs[] | .name' >&2
exit 1
fi
echo "SLACK_FOOTER=$url" >>"$GITHUB_ENV"
env:
GH_TOKEN: ${{ inputs.github-token }}
JOB_NAME: ${{ inputs.job-name || github.job }}
- if: ${{ inputs.dry-run != 'true' }}
name: Notify
uses: rtCamp/action-slack-notify@v2
env:
MSG_MINIMAL: "true" # SLACK_MESSAGE will include all details
SLACK_ICON: https://github.com/freckle-automation.png?size=48
SLACK_USERNAME: GitHub Actions
SLACK_WEBHOOK: ${{ inputs.slack-webhook-url }}
SLACK_CHANNEL: ${{ inputs.slack-channel }}