-
Notifications
You must be signed in to change notification settings - Fork 0
292 lines (255 loc) · 14 KB
/
topic_pool.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
# Use GitHub workflow/action/repo to implement gerrit-style topic function
#
# Copyright 2024 General Motors LLC
#
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program; if not, see <https://www.gnu.org/licenses>.
#
# Changelog:
# 20240724: Design by dw
name: topic_pool
on:
workflow_call:
secrets:
PR_TOPIC_TOKEN:
required: true
PR_TOPIC_ID:
required: true
pull_request:
types: [opened, reopened, synchronize, ready_for_review, converted_to_draft]
pull_request_target:
types: [closed]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_TOPIC_ID: ${{ secrets.PR_TOPIC_ID }}
PR_TOPIC_TOKEN: ${{ secrets.PR_TOPIC_TOKEN }}
JOB_CLONE_TOKEN: ${{ secrets.PR_TOPIC_ID }}:${{ secrets.PR_TOPIC_TOKEN }}@
WORKSPACE: ${{ github.workspace }}
BUILD_NUMBER: ${{ github.run_number }}
BUILD_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
PR_BASE_BRANCH: ${{ github.event.pull_request.base.ref }}
PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
PR_BEFORE_SHA: ${{ github.event.before }}
PR_HEAD_BRANCH: ${{ github.event.pull_request.head.ref }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
PR_URL: ${{ github.event.pull_request.html_url }}
PR_ISSUE_URL: ${{ github.event.pull_request.issue_url }}
PR_NODE_ID: ${{ github.event.pull_request.node_id }}
PR_NUM: ${{ github.event.pull_request.number }}
GH_ORG_REPO: ${{ github.repository }}
GH_EVENT_ISSUE_NUM: ${{ github.event.issue.number }}
GH_EVENT_SENDER: ${{ github.event.sender.login }}
TP_PRX_BRANCH: ${{ github.repository }}/pr${{ github.event.pull_request.number }}
jobs:
topic_pool:
runs-on: ubuntu-latest
timeout-minutes: 5
concurrency:
group: pr_topic_pool-${{ github.repository }}
cancel-in-progress: false
steps:
- name: set build description
shell: bash
run: |
env TZ=America/Detroit date >>${GITHUB_STEP_SUMMARY}
echo "[${GITHUB_WORKFLOW}#${GITHUB_RUN_NUMBER}](${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}) in ${RUNNER_NAME}" >>${GITHUB_STEP_SUMMARY}
- name: pr change only
if: ${{ github.event_name != 'pull_request_target' && github.event_name != 'pull_request' }}
run: |
exit 0
- name: setup git config
shell: bash
run: |
git config --global user.name "Ding Wei"
git config --global user.email "[email protected]"
git config --global core.autocrlf false
- name: get pr commit message
shell: bash
if: ${{ github.event.pull_request.merged != true }}
run: |
rm -fr ${WORKSPACE}/*.tmp || true
export API_URL="https://api.github.com/repos/${{ github.repository }}/branches/${PR_HEAD_BRANCH}"
export RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" ${API_URL})
if [[ ${RESPONSE} != 200 ]] ; then
echo BRANCH_LOST=true >>${GITHUB_ENV}
exit 0
fi
curl -s -L -k \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/commits/${{ github.event.pull_request.head.sha }} \
| jq -r '.commit.message' | sed 's/^"//g' | sed 's/"$//g' >${WORKSPACE}/row_commit_msg.tmp || true
if [[ $(egrep '\\n\\n|\\r' ${WORKSPACE}/row_commit_msg.tmp) ]] ; then
echo -e $(cat ${WORKSPACE}/row_commit_msg.tmp) >${WORKSPACE}/commit_msg.tmp
else
cp ${WORKSPACE}/row_commit_msg.tmp ${WORKSPACE}/commit_msg.tmp
cat ${WORKSPACE}/commit_msg.tmp || true
fi
cd ${WORKSPACE}
if [[ -z ${PR_NUM} ]] ; then
export PR_NUM=${GH_EVENT_ISSUE_NUM}
export TP_PRX_BRANCH=${{ github.repository }}/pr${GH_EVENT_ISSUE_NUM}
echo PR_NUM=${PR_NUM} >>${GITHUB_ENV}
echo TP_PRX_BRANCH=${TP_PRX_BRANCH} >>${GITHUB_ENV}
fi
if [[ -z ${PR_URL} ]] ; then
export PR_URL=${{ github.event.pull_request.issue_url }}
echo PR_URL=${PR_URL} >>${GITHUB_ENV}
fi
export TOPIC_NAME=$(grep '\[TOPIC=' ${WORKSPACE}/commit_msg.tmp | awk -F'TOPIC=' {'print $2'} | awk -F']' {'print $1'} | head -n1)
export JiraID=$(grep 'JiraID:' ${WORKSPACE}/commit_msg.tmp | sed 's/ //g' | awk -F'JiraID:' {'print $2'} | tr -d '\r' | head -n1)
export JiraCloudID=$(grep 'JiraCloudID:' ${WORKSPACE}/commit_msg.tmp | sed 's/ //g' | awk -F'JiraCloudID:' {'print $2'} | tr -d '\r' | head -n1)
[[ ! -z ${JiraID} ]] && export TOPIC_JIRA_NAME=70P1C-JID-${JiraID} || export TOPIC_JIRA_NAME=''
[[ ! -z ${JiraCloudID} ]] && export TOPIC_JIRA_NAME=70P1C-JCID-${JiraCloudID} || true
[[ -z ${TOPIC_NAME} && ! -z ${TOPIC_JIRA_NAME} ]] && export TOPIC_NAME=${TOPIC_JIRA_NAME} || true
[[ -z ${TOPIC_NAME} && -z ${TOPIC_JIRA_NAME} ]] && export TOPIC_NAME=70P1C-$(echo ${PR_URL} | md5sum | cut -c1-7) || true
[[ ! -z ${TOPIC_NAME} ]] && echo ${TOPIC_NAME} >${WORKSPACE}/TOPIC || true
echo PR_NUM=${PR_NUM} >>${GITHUB_ENV}
echo JiraID=${JiraID} >>${GITHUB_ENV}
echo JiraCloudID=${JiraCloudID} >>${GITHUB_ENV}
echo TOPIC_NAME=${TOPIC_NAME} >>${GITHUB_ENV}
echo GH_ORG=$(dirname ${{ github.repository }}) >>${GITHUB_ENV}
echo GH_REPO=$(basename ${{ github.repository }}) >>${GITHUB_ENV}
echo TOPIC_BRANCH=$(dirname ${{ github.repository }})/topic-${TOPIC_NAME} >>${GITHUB_ENV}
cat ${GITHUB_ENV}
- name: check topic name
shell: bash
if: ${{ github.event.pull_request.merged != true }}
run: |
[[ ! -z ${BRANCH_LOST} ]] && exit 0
[[ ${TOPIC_NAME} =~ 70P1C ]] && exit 0
if [[ $(echo "${TOPIC_NAME}" | sed 's/ /:/g' | sed 's/[a-zA-Z0-9]//g' | sed 's/[-_]//g' | wc -m) != 1 || $(echo "${TOPIC_NAME}" | wc -m) -gt 30 ]] ; then
export COMMENT_BODY="### Failed: topic name against the topic naming convention<br><br>1. Optional git commit message: [TOPIC=Abc-123]<br>2. Character Limitation: a-z, A-Z, 0-9, - and _<br>3. Maximum 25 characters in length<br>4. No hidden characters (just type, do not copy and paste)<br><br>Bad topic name: ${TOPIC_NAME}"
curl -s -k -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUM}/comments \
-d "{\"body\":\"${COMMENT_BODY}\"}" || true
echo BAD_TOPIC_NAME=BAD_TOPIC_NAME >>${GITHUB_ENV}
fi
- name: checkout topic branches
shell: bash
if: ${{ github.event.pull_request.merged != true }}
run: |
[[ ! -z ${BRANCH_LOST} ]] && exit 0
export TOPIC_POOL=$(dirname ${{ github.repository }})/${{ github.workflow }}
echo TOPIC_POOL=${TOPIC_POOL} >>${GITHUB_ENV}
export API_URL="https://api.github.com/repos/${TOPIC_POOL}/branches/${TP_PRX_BRANCH}"
export RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${PR_TOPIC_TOKEN}" ${API_URL})
rm -fr ${WORKSPACE}/topic_pool_prX.tmp || true
if [[ ${RESPONSE} -eq 200 ]] ; then
git clone -q -b ${TP_PRX_BRANCH} --single-branch \
https://${JOB_CLONE_TOKEN}github.com/${TOPIC_POOL} ${WORKSPACE}/topic_pool_prX.tmp
else
git clone -q -b main --single-branch \
https://${JOB_CLONE_TOKEN}github.com/${TOPIC_POOL} ${WORKSPACE}/topic_pool_prX.tmp
cd ${WORKSPACE}/topic_pool_prX.tmp
git checkout --orphan ${TP_PRX_BRANCH}
rm -fr * .github .gitignore || true
git clean --force -d
git add * || true
fi
export API_URL="https://api.github.com/repos/${TOPIC_POOL}/branches/${TOPIC_BRANCH}"
export RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token ${PR_TOPIC_TOKEN}" ${API_URL})
rm -fr ${WORKSPACE}/topic_pool.tmp || true
if [[ ${RESPONSE} -eq 200 ]] ; then
git clone -q -b ${TOPIC_BRANCH} --single-branch \
https://${JOB_CLONE_TOKEN}github.com/${TOPIC_POOL} ${WORKSPACE}/topic_pool.tmp
else
git clone -q -b main --single-branch \
https://${JOB_CLONE_TOKEN}github.com/${TOPIC_POOL} ${WORKSPACE}/topic_pool.tmp
cd ${WORKSPACE}/topic_pool.tmp
git checkout --orphan ${TOPIC_BRANCH}
rm -fr * .github .gitignore || true
git clean --force -d
git add * || true
fi
ls -la ${WORKSPACE}/topic_pool.tmp ${WORKSPACE}/topic_pool_prX.tmp
- name: setup topic info
shell: bash
if: ${{ github.event.pull_request.merged != true && github.event.pull_request.draft != true && github.event.action != 'closed' }}
run: |
[[ ! -z ${BRANCH_LOST} ]] && exit 0
cp ${WORKSPACE}/TOPIC ${WORKSPACE}/topic_pool_prX.tmp
cat << _EOF_ >${WORKSPACE}/${GH_REPO}
TP_GH_REPO=${{ github.repository }}
TP_PR_HEAD_BRANCH=${{ github.event.pull_request.head.ref }}
TP_PR_HEAD_SHA=${{ github.event.pull_request.head.sha }}
TP_PR_URL=${{ github.event.pull_request.html_url }}
TP_PR_HEAD_GIT_LOG=$(head -n1 ${WORKSPACE}/commit_msg.tmp | sed 's/"//g' | sed "s/'//g" | sed 's/ /_/g' | sed 's/\.\.\./_/g' | sed 's/\./_/g' | sed 's/:/_/g' | sed 's/#/_/g' | sed 's/\//_/g' | sed 's/!/_/g' | sed 's/\$/_/g' | sed 's/&/_/g' | sed 's/|/_/g' | sed 's/*/_/g' | sed 's/(/_/g' | sed 's/)/_/g' | sed 's/__/_/g' | cut -c1-44)
TP_PR_BASE_BRANCH=${{ github.event.pull_request.base.ref }}
TP_PR_NODE_ID=${{ github.event.pull_request.node_id }}
TP_JIRA_ID=${JiraID}
TP_JIRA_CLOUD_ID=${JiraCloudID}
_EOF_
sort -u ${WORKSPACE}/${GH_REPO} >${WORKSPACE}/topic_pool.tmp/${GH_REPO}
cat ${WORKSPACE}/topic_pool.tmp/${GH_REPO}
- name: remove pr form changed topic
shell: bash
if: ${{ github.event.pull_request.merged != true && github.event.pull_request.draft != true && github.event.action != 'closed' }}
run: |
[[ ! -z ${BRANCH_LOST} ]] && exit 0
cd ${WORKSPACE}/topic_pool_prX.tmp
export OLD_TOPIC=$(git diff | egrep -v '\-\-\-' | grep '^-' | sed 's/^-//g')
echo OLD_TOPIC=${OLD_TOPIC}
if [[ ! -z ${OLD_TOPIC} ]] ; then
export OLD_TOPIC_BRANCH=${GH_ORG}/topic-${OLD_TOPIC}
git clone -q -b ${OLD_TOPIC_BRANCH} --single-branch \
https://${JOB_CLONE_TOKEN}github.com/${TOPIC_POOL} ${WORKSPACE}/retired_topic.tmp
cd ${WORKSPACE}/retired_topic.tmp
git rm ${GH_REPO} || true
git commit -am "Remove ${GH_REPO} because topic changed" || true
git push --force origin ${OLD_TOPIC_BRANCH}:${OLD_TOPIC_BRANCH} || true
fi
- name: remove pr from topic when pr draft or close
shell: bash
if: ${{ github.event.pull_request.draft == true || github.event.action == 'closed' }}
run: |
[[ ! -z ${BRANCH_LOST} ]] && exit 0
[[ ! -e ${WORKSPACE}/topic_pool_prX.tmp ]] && exit 0
cd ${WORKSPACE}/topic_pool_prX.tmp
echo >TOPIC
git add TOPIC || true
git commit -am "Remove ${GH_REPO} because pr draft of close" || true
git push --force origin ${TP_PRX_BRANCH}:${TP_PRX_BRANCH} || true
cd ${WORKSPACE}/topic_pool.tmp
git rm ${GH_REPO} || true
git commit -am "Remove ${GH_REPO} because pr draft or close" || true
git push --force origin ${TOPIC_BRANCH}:${TOPIC_BRANCH} || true
ls -la ${WORKSPACE}/topic_pool.tmp ${WORKSPACE}/topic_pool_prX.tmp
- name: one topic have one PR in the same repository
shell: bash
if: ${{ github.event.pull_request.merged != true && github.event.pull_request.draft != true && github.event.action != 'closed' }}
run: |
[[ ! -z ${BRANCH_LOST} ]] && exit 0
cd ${WORKSPACE}/topic_pool.tmp
if [[ $(git diff ${GH_REPO} | grep 'TP_PR_URL=' | grep '^-') ]] ; then
export CONFLICT_PR_NUM=$(basename $(git diff ${GH_REPO} | grep 'TP_PR_URL=' | grep '^-' | awk -F'TP_PR_URL=' {'print $2'}))
export CONFLICT_PR_ISSUE_URL="https://api.github.com/repos/${{ github.repository }}/issues/${CONFLICT_PR_NUM}"
export COMMENT_BODY="## Note:\nThe topic can only have one PR in the same repository\nThe PR ${CONFLICT_PR_URL} replaced by ${{ github.event.pull_request.html_url }} in [TOPIC=${TOPIC_NAME}]\n\n[remove_topic@$(date +%y%m%d-%H%M%S)](${BUILD_URL})"
curl -s -k -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
${CONFLICT_PR_ISSUE_URL}/comments \
-d "{\"body\":\"${COMMENT_BODY}\"}"
fi
- name: commit topic info
shell: bash
if: ${{ github.event.pull_request.merged != true && github.event.pull_request.draft != true && github.event.action != 'closed' }}
run: |
[[ ! -z ${BRANCH_LOST} ]] && exit 0
cd ${WORKSPACE}/topic_pool_prX.tmp
git add * || true
git commit -am "Update topic ${TOPIC_BRANCH}" || true
git push --force origin ${TP_PRX_BRANCH}:${TP_PRX_BRANCH} || true
cd ${WORKSPACE}/topic_pool.tmp
git add * || true
git commit -am "Update topic ${TOPIC_BRANCH}" || true
[[ -z ${BAD_TOPIC_NAME} ]] && git push --force origin ${TOPIC_BRANCH}:${TOPIC_BRANCH} || true
ls -la ${WORKSPACE}/topic_pool.tmp ${WORKSPACE}/topic_pool_prX.tmp