-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
action.yml
332 lines (292 loc) · 14.3 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# Tutorial to use with: https://github.com/andry81-devops/github-accum-stats
#
name: accum-gh-stats
description: 'GitHub composite action to request and accumulate a repository clones/views statistic'
branding:
icon: download
color: red
inputs:
deps_repo_owner:
description: 'Owner of a repository used to checkout dependencies'
required: true
deps_repo_branch:
description: 'Repository branch used to checkout dependencies'
default: master
deps_repo_read_token:
description: 'PAT token to a repository used to checkout dependencies'
default: ${{ github.token }}
stat_repo_owner:
description: 'Owner of a repository used to request the statistic'
required: true
stat_repo:
description: 'Repository name used to request the statistic'
required: true
stat_entity:
description: 'Statistic entity to request'
required: true
# variants:
# - clones
# - views
commit_msg_entity:
description: 'Commit message entity. The `stat_entity` does use if not defined.'
# examples:
# - cl
# - vi
stat_repo_read_token:
description: 'PAT token to a repository used to request the statistic with statistic read access rights'
required: true
curl_flags:
description: 'additional curl flags'
# example:
# `-H 'Cache-Control: no-cache'`
output_repo_owner:
description: 'Owner of a repository used to output the statistic'
required: true
output_repo:
description: 'Repository name used to output the statistic'
required: true
output_repo_branch:
description: 'Repository branch used to output the statistic'
default: master
output_repo_dir:
description: 'Output repository relative directory path to store requested and accumulated statistic'
required: true
# examples:
# - traffic/clones
# - traffic/views
output_repo_write_token:
description: 'PAT token to the output repository with write/push access rights'
required: true
# NOTE:
# The `flags` converts into `GH_WORKFLOW_FLAGS` environment variable as a single line string.
# You must evaluate it to a shell array to be able to use it in the bash shell: `eval __flags__=($GH_WORKFLOW_FLAGS)`
#
flags:
description: >
String of flag values list in format of `NAME[=[EXPR]]` ready to be evaluated as a shell array: `(<0> <1> ... <N>)`.
If `EXPR` is not empty and begins by '[' then, use `eval EXPR` instead of `(( EXPR ))` expression.
Otherwise if a flag expression is `NAME=`, then false (unflagged).
Otherwise if a flag expression is `NAME`, then true (flagged).
# See dependent repositories and scripts for details.
# Flag expression examples:
# # A is true, B is true, C is false, D is true, F is false, T is true.
# # GH_WORKFLOW_FLAGS=`A=1 B="1 + 1" C="1 - 1" D='[[ "1" -eq "1" ]]' F= T`
# flags: >-
# A=1
# B="1 + 1"
# C="1 - 1"
# D='[[ "1" -eq "1" ]]'
# F=
# T
# builtin flag names:
# ENABLE_PRINT_INITIAL_ENV_INTO_STDOUT=1
env:
description: environment variables
# See dependent repositories and scripts for details.
# examples:
# ENABLE_GENERATE_CHANGELOG_FILE=1
# CHANGELOG_FILE=changelog.txt
# ENABLE_COMMIT_MESSAGE_DATE_WITH_TIME=1 # insert the time string in format `HH:MMZ` additionally after the date in each commit message
# ENABLE_COMMIT_MESSAGE_DATE_TIME_WITH_LAST_CHANGED_DATE_OFFSET=1 # insert datetime suffix as offset to the last changed date in format `-DDT` to note the closest changed date
# ENABLE_COMMIT_MESSAGE_WITH_WORKFLOW_RUN_NUMBER=1 # insert the workflow run number after date/time prefix in each commit message
# ENABLE_GITHUB_ACTIONS_RUN_URL_PRINT_TO_CHANGELOG=1
# ENABLE_REPO_STATS_COMMITS_URL_PRINT_TO_CHANGELOG=1
# CONTINUE_ON_INVALID_INPUT=1
# CONTINUE_ON_EMPTY_CHANGES=1
# CONTINUE_ON_RESIDUAL_CHANGES=1
runs:
using: "composite"
steps:
- name: '[accum-gh-stats][01] load `flags` parameter into `GH_WORKFLOW_FLAGS` environment variable'
shell: bash
run: |
### [accum-gh-stats][01] load `flags` parameter into `GH_WORKFLOW_FLAGS` environment variable
# suppress not zero exit code
{ IFS=$' \t' read -r -d '' GH_WORKFLOW_FLAGS || (( 1 )); } <<::EOF::
${{ inputs.flags }}
::EOF::
GH_WORKFLOW_FLAGS="${GH_WORKFLOW_FLAGS//[$'\n\r']/}" # remove all line returns
echo "GH_WORKFLOW_FLAGS=$GH_WORKFLOW_FLAGS" >> $GITHUB_ENV
- name: '[accum-gh-stats][02] print initial environment variables'
shell: bash
run: |
### [accum-gh-stats][02] print initial environment variables
eval __flags__=($GH_WORKFLOW_FLAGS)
for arg in "${__flags__[@]}"; do
# suppress not zero exit code
IFS=$'=\n' read -r -d '' name value <<< "$arg" || (( 1 ))
if [[ "$name" == 'ENABLE_PRINT_INITIAL_ENV_INTO_STDOUT' ]]; then
# If `value` is not empty and begins by '[' then, use `eval "$value"` instead of `(( value ))` expression.
# Otherwise if `value` is empty and `arg` with assign character, then false (unflagged).
# Otherwise if `value` is empty and `arg` without assign character, then true (flagged).
if [[ -z "$value" && "${arg/=/}" == "$arg" ]] || ( [[ "${value:0:1}" != "[" ]] && (( value )) ) || ( [[ "${value:0:1}" == "[" ]] && eval "$value" ) ; then
env | sort
break
fi
fi
done
# step is required to workaround variable expansion in variables
- name: '[accum-gh-stats][03] declare dependent variables'
shell: bash
run: |
### [accum-gh-stats][03] declare dependent variables
echo "GH_WORKFLOW_ROOT=$GITHUB_WORKSPACE/gh-workflow" >> $GITHUB_ENV
- name: '[accum-gh-stats][04] checkout gh-workflow@${{ inputs.deps_repo_branch }}'
uses: andry81-devops/gh-action--git-checkout@master
with:
repository: ${{ inputs.deps_repo_owner }}/gh-workflow
ref: ${{ inputs.deps_repo_branch }}
path: gh-workflow
token: ${{ inputs.deps_repo_read_token }}
mkdir-p: >-
${{ env.GH_WORKFLOW_ROOT }}
- name: '[accum-gh-stats][05] update permissions'
shell: bash
run: |
### [accum-gh-stats][05] update permissions
chmod ug+x $GH_WORKFLOW_ROOT/bash/_common/update-permissions.sh
$GH_WORKFLOW_ROOT/bash/_common/update-permissions.sh ug+x "$GH_WORKFLOW_ROOT/bash" -iname '*.sh'
# step is required to pass environment variables externally
- name: '[accum-gh-stats][06] declare variables from lists'
shell: bash
run: |
### [accum-gh-stats][06] declare variables from lists
# suppress not zero exit code
{ IFS=$' \t' read -r -d '' INPUT_ENV || (( 1 )); } <<::EOF::
${{ inputs.env }}
::EOF::
INPUT_ENV="${INPUT_ENV//[$'\n\r']/}" # remove all line returns
$GH_WORKFLOW_ROOT/bash/github/set-env-from-args.sh \
${INPUT_ENV} \
"GHWF_GITHUB_ACTIONS_RUN_URL=https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" \
"GHWF_REPO_STATS_COMMITS_URL=https://github.com/${{ inputs.output_repo_owner }}/${{ inputs.output_repo }}/commits?branch=${{ inputs.output_repo_branch }}"
- name: '[accum-gh-stats][07] variables validation'
shell: bash
run: |
### [accum-gh-stats][07] variables validation
$GH_WORKFLOW_ROOT/bash/github/set-env-from-args.sh \
"CHANGELOG_FILE=${CHANGELOG_FILE:-"changelog.txt"}"
- name: '[accum-gh-stats][08] declare local variables'
shell: bash
run: |
### [accum-gh-stats][08] declare local variables
$GH_WORKFLOW_ROOT/bash/github/set-env-from-args.sh \
"changelog_file=$CHANGELOG_FILE" \
"CHANGELOG_FILE=$GITHUB_WORKSPACE/gh-stats/${{ inputs.output_repo_dir }}/$CHANGELOG_FILE" \
"stat_entity=${{ inputs.stat_entity }}" \
"commit_msg_entity=${{ inputs.commit_msg_entity }}" \
"stats_dir=$GITHUB_WORKSPACE/gh-stats/${{ inputs.output_repo_dir }}" \
"stats_json=$GITHUB_WORKSPACE/gh-stats/${{ inputs.output_repo_dir }}/latest.json"
- name: '[accum-gh-stats][09] variables validation'
shell: bash
run: |
### [accum-gh-stats][09] variables validation
case "$stat_entity" in
"views") $GH_WORKFLOW_ROOT/bash/github/set-env-from-args.sh "stat_entity_path=traffic/views";;
"clones") $GH_WORKFLOW_ROOT/bash/github/set-env-from-args.sh "stat_entity_path=traffic/clones";;
*) $GH_WORKFLOW_ROOT/bash/github/print-error.sh "\`stat_entity\` input parameter is invalid: \`$stat_entity\`"; exit 255;;
esac
- name: '[accum-gh-stats][10] declare local variables'
shell: bash
run: |
### [accum-gh-stats][10] declare local variables
$GH_WORKFLOW_ROOT/bash/github/set-env-from-args.sh \
"changelog_dir=${CHANGELOG_FILE%/*}"
- name: '[accum-gh-stats][11] allocate directories'
shell: bash
run: |
### [accum-gh-stats][11] allocate directories
# for details: https://docs.github.com/en/actions/learn-github-actions/contexts#runner-context
$GH_WORKFLOW_ROOT/bash/github/set-env-from-args.sh \
"TEMP_DIR=${{ runner.temp }}/accum-stats"
$GH_WORKFLOW_ROOT/bash/github/eval-from-args.sh -Ep \
'mkdir -p "$TEMP_DIR"'
- name: '[accum-gh-stats][12] head annotations'
shell: bash
run: |
### [accum-gh-stats][12] head annotations
$GH_WORKFLOW_ROOT/bash/github/print-notice.sh \
"stat repo: https://github.com/${{ inputs.stat_repo_owner }}/${{ inputs.stat_repo }}" \
"output repo dir: https://github.com/${{ inputs.output_repo_owner }}/${{ inputs.output_repo }}/tree/${{ inputs.output_repo_branch }}/${{ inputs.output_repo_dir }}"
$GH_WORKFLOW_ROOT/bash/github/print-notice-ln.sh \
"changelog file: https://github.com/${{ inputs.output_repo_owner }}/${{ inputs.output_repo }}/tree/${{ inputs.output_repo_branch }}/${{ inputs.output_repo_dir }}/$changelog_file" \
"env:"$'\n'" GITHUB_RUN_ID=\`$GITHUB_RUN_ID\`"$'\n'" GITHUB_RUN_NUMBER=\`$GITHUB_RUN_NUMBER\`"
- name: '[accum-gh-stats][13] checkout gh-stats@${{ inputs.output_repo_branch }}'
uses: andry81-devops/gh-action--git-checkout@master
with:
repository: ${{ inputs.output_repo_owner }}/${{ inputs.output_repo }}
ref: ${{ inputs.output_repo_branch }}
path: gh-stats
token: ${{ inputs.output_repo_write_token }}
mkdir-p: >-
${{ inputs.output_repo_dir }}
${{ env.changelog_dir }}
- name: '[accum-gh-stats][14] request `${{ inputs.stat_entity }}` json'
shell: bash
run: |
### [accum-gh-stats][14] request `${{ inputs.stat_entity }}` json
# CAUTION:
# The `sed` has to be used to ignore blank lines by replacing `CR` by `LF`.
# This is required for uniform parse the curl output in both verbose or non verbose mode.
#
curl \
--user "${{ inputs.stat_repo_owner }}:${{ inputs.stat_repo_read_token }}" \
${{ inputs.curl_flags }} \
-H "Accept: application/vnd.github.v3+json" \
-o "$stats_json" \
"https://api.github.com/repos/${{ inputs.stat_repo_owner }}/${{ inputs.stat_repo }}/$stat_entity_path" \
2>&1 | tee "$TEMP_DIR/curl-stderr.txt" | sed -E 's/\r([^\n])/\n\1/g' | grep -P '^(?: [% ] |(?: | \d|\d\d)\d |[<>] )'
last_error=$?
echo $'---\n'"$(<"$stats_json")"$'\n---'
if (( last_error )); then
if [[ -s "$TEMP_DIR/curl-stderr.txt" ]]; then
echo "$(<"$TEMP_DIR/curl-stderr.txt")"$'\n---'
fi
else
if [[ ! -s "$stats_json" && -s "$TEMP_DIR/curl-stderr.txt" ]]; then
echo "$(<"$TEMP_DIR/curl-stderr.txt")"$'\n---'
fi
fi
- name: '[accum-gh-stats][15] accumulate `${{ inputs.stat_entity }}`'
id: accum-stats
shell: bash
run: |
### [accum-gh-stats][15] accumulate `${{ inputs.stat_entity }}`
$GH_WORKFLOW_ROOT/bash/github/accum-stats.sh
# caution: no operation after that line if non zero exit code happens above
- name: '[accum-gh-stats][16] commit gh-stats'
shell: bash
run: |
### [accum-gh-stats][16] commit gh-stats
cd $GITHUB_WORKSPACE/gh-stats
git add .
git config --global user.name "GitHub Action"
git config --global user.email "[email protected]"
COMMIT_MESSAGE="A/U: $COMMIT_MESSAGE_DATE_TIME_PREFIX:"
if (( ENABLE_COMMIT_MESSAGE_WITH_WORKFLOW_RUN_NUMBER )) && [[ -n "$GITHUB_RUN_NUMBER" ]]; then
COMMIT_MESSAGE="$COMMIT_MESSAGE #$GITHUB_RUN_NUMBER:"
fi
COMMIT_MESSAGE="$COMMIT_MESSAGE ${COMMIT_MESSAGE_PREFIX}${COMMIT_MESSAGE_SUFFIX:+" | "}$COMMIT_MESSAGE_SUFFIX"
git status
git commit -m "$COMMIT_MESSAGE"
$GH_WORKFLOW_ROOT/bash/github/set-env-from-args.sh \
"HEAD_COMMIT_HASH=$(git rev-parse HEAD)"
- name: '[accum-gh-stats][17] push gh-stats'
uses: ad-m/github-push-action@master
with:
repository: ${{ inputs.output_repo_owner }}/${{ inputs.output_repo }}
branch: ${{ inputs.output_repo_branch }}
directory: gh-stats
github_token: ${{ inputs.output_repo_write_token }}
- name: '[accum-gh-stats][18] print commit parameters into pipeline log only'
shell: bash
run: |
### [accum-gh-stats][18] print commit parameters into pipeline log only
$GH_WORKFLOW_ROOT/bash/github/print-notice.sh \
"commit reference url: https://github.com/${{ inputs.output_repo_owner }}/${{ inputs.output_repo }}/commit/$HEAD_COMMIT_HASH"
- name: '[accum-gh-stats][19] flush print annotations'
if: always()
shell: bash
run: |
### [accum-gh-stats][19] flush print annotations
$GH_WORKFLOW_ROOT/bash/github/flush-print-annotations.sh