-
Notifications
You must be signed in to change notification settings - Fork 1
307 lines (257 loc) · 12.1 KB
/
gh2gerrit.yaml
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
---
name: Github PR to Gerrit Change request
# yamllint disable-line rule:truthy
on:
pull_request_target:
types: [opened, synchronize, edited, reopened]
branches:
- master
- main
# Permission added at job level or workflow level
# required to for updating comments on the PR
permissions: write-all
concurrency:
group: ${{ github.run_id }}
cancel-in-progress: true
jobs:
actionlint:
runs-on: ubuntu-latest
steps:
- name: Clone Github repo with the PR
uses: actions/checkout@v4
with:
ssh-user: ${{ vars.GERRIT_SSH_USER_G2G }}
# ref: ${{ github.event.pull_request.head.sha }}
# repository: "${{ github.repository }}"
- name: Download actionlint
id: get_actionlint
# yamllint disable-line rule:line-length
run: bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash)
shell: bash
- name: Check workflow files
run: ${{ steps.get_actionlint.outputs.executable }} -color
shell: bash
github2gerrit:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python@v5
id: setup-python
with:
python-version: "3.11"
- name: "Install required dependencies: git-review,jq"
run: |
python -m pip install --upgrade pip
pip install "git-review==2.3.1" jq
git review --version
jq --version
# - name: Read .gitreview and populate information
# shell: bash
# run: |
# set -x
#
# cd "$GITHUB_WORKSPACE/${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}"
# project_repo_gerrit_git=$(grep -E "project" .gitreview | cut -d "=" -f2)
# # # change any '-' to '/' and remove owner name
# project_repo_gerrit="${project_repo_gerrit_git//.*}"
# project_repo_github="${project_repo_gerrit////-}"
#
# echo "PROJECT_REPO_GERRIT=${project_repo_gerrit}" >> "$GITHUB_ENV"
# echo "PROJECT_REPO_GITHUB=${project_repo_github}" >> "$GITHUB_ENV"
- name: Set env with Gerrit Github repo format
shell: bash
run: |
set -x
project_repo_github="${{ github.repository }}"
# change any '-' to '/' and remove owner name
project_repo_github="${project_repo_github#*/}"
project_repo_gerrit="${project_repo_github//-//}"
echo "PROJECT_REPO_GERRIT=${project_repo_gerrit}" >> "$GITHUB_ENV"
echo "PROJECT_REPO_GITHUB=${project_repo_github}" >> "$GITHUB_ENV"
- name: Set env GITHUB branch in env
shell: bash
run: |
gerrit_branch="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
echo "GERRIT_BRANCH=${gerrit_branch}" >> "$GITHUB_ENV"
- name: Clone Github repo where the PR is submitted
uses: actions/checkout@v4
with:
ssh-user: ${{ vars.GERRIT_SSH_USER_G2G }}
path: "${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}"
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}
# repository: "${{ github.repository }}"
- name: Print last X commits in the git log
if: env.ACT == 'true'
run: |
set -x
cd "$GITHUB_WORKSPACE/${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}"
git --no-pager log --graph --all --decorate --pretty=oneline -n5
- name: Install SSH Key
uses: shimataro/ssh-key-action@d4fffb50872869abe2d9a9098a6d9c5aa7d16be4 # v2.7.0
with:
key: ${{ secrets.GERRIT_SSH_PRIVKEY_G2G }}
name: "id_rsa"
known_hosts: ${{ vars.GERRIT_KNOWN_HOSTS }}
config: |
Host ${{ vars.GERRIT_SERVER }}
User ${{ vars.GERRIT_SSH_USER_G2G }}
Port 29418
PubkeyAcceptedKeyTypes +ssh-rsa
IdentityFile ~/.ssh/id_rsa
- name: Setup Gerrit remote
if: env.PROJECT_REPO_GERRIT != ''
shell: bash
run: |
set -x
# Query for a pre-existing gerrit review
cd "$GITHUB_WORKSPACE/${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}"
# Setup global git config required by git-review
git config --global gitreview.username ${{ vars.GERRIT_SSH_USER_G2G }}
git config --global user.name ${{ vars.GERRIT_SSH_USER_G2G }}
git config --global user.email ${{ vars.GERRIT_SSH_USER_G2G_EMAIL }}
# Add Gerrit remote and print remote settings
git remote add gerrit ssh://${{ vars.GERRIT_SSH_USER_G2G }}@${{ vars.GERRIT_SERVER }}:29418/${{ env.PROJECT_REPO_GERRIT }}.git
git remote -v
# Workaround for git-review failing to copy the commit-msg hook to submodules
git config core.hooksPath "$(git rev-parse --show-toplevel)/.git/hooks"
# Init gerrit repo
git review -s -v
- name: Print the pull-request
run: |
set -x
export PR_NUMBER="${{ github.event.pull_request.number || github.event.issue.number }}"
echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_ENV"
# Set a temp PR to test with ACT
if [[ $PR_NUMBER == '' ]]; then
echo "PR_NUMBER=${{ vars.PR_NUM }}" >> "$GITHUB_ENV"
fi
- name: Download the PR .patch and .diff locally
if: env.PR_NUMBER != ''
run: |
set -x
patch_path="${{ vars.ORGANIZATION }}/${{ env.PROJECT_REPO_GITHUB }}/pull/${{ env.PR_NUMBER }}.patch"
wget -q "https://github.com/${patch_path}"
if [[ -f "${{ env.PR_NUMBER }}.patch" ]]; then
echo "PR-${{ env.PR_NUMBER }} download successfully"
fi
- name: Get the number of commits in the pull-request
if: ${{ github.event_name == 'pull_request_target' }}
run: |
set -x
cd "$GITHUB_WORKSPACE/${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}"
num_commits="$(gh pr view ${{ env.PR_NUMBER }} --json commits | jq '.[] | length')"
echo "PR_COMMITS=$num_commits" >> "$GITHUB_ENV"
env:
GH_TOKEN: ${{ github.token }}
- name: Print diff sha and refs
shell: bash
run: |
set -x
echo "github.sha: ${{ github.sha }}"
echo "github.ref: ${{ github.ref }}"
echo "github.ref_name: ${{ github.ref_name }}"
echo "github.event.pull_request.base.ref: ${{ github.event.pull_request.base.ref }}"
echo "github.event.pull_request.base.sha: ${{ github.event.pull_request.base.sha }}"
echo "github.event.pull_request.head.ref: ${{ github.event.pull_request.head.ref }}"
echo "github.event.pull_request.head.sha: ${{ github.event.pull_request.head.sha }}"
echo "github.merge_commit_sha: ${{ github.sha }}"
echo "github.merge_commit_sha: ${{ github.merge_commit_sha }}"
- name: Apply patch on local repo
if: hashFiles('${{ env.PR_NUMBER }}.patch') != ''
shell: bash
run: |
set -x
cd "$GITHUB_WORKSPACE/${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}"
# Apply the patch on the HEAD
# git am --show-current-patch=diff "$GITHUB_WORKSPACE/${{ env.PR_NUMBER }}.patch"
git --no-pager log --graph --all --decorate --pretty=oneline -n"${{ env.PR_COMMITS }}"
git status
# Create a Change-id from all the PR
# Capture the last commit HEAD
# git rev-parse HEAD > last-head-commit-id.txt
# Squash all commits in pull-request into a single commit
git reset --soft "${{ github.sha }}"
# Check if change-id is present in each commit and use the first one
# Capture and check for signed-of-by and Change-Id
git log -v --format=%B --reverse HEAD..HEAD@{1} | grep -E "^(Change-Id)" > change-ID.txt || true
git log -v --format=%B --reverse HEAD..HEAD@{1} | grep -E "^(Signed-off-by)" > signed-off-by.txt || true
git log -v --format=%B --reverse HEAD..HEAD@{1} | grep -Ev "^(Signed-off-by|Change-Id)" > commit-msg.txt
if [[ -f commit-msg.txt ]]; then
cat commit-msg.txt
commit_message="${commit_message:-commit-msg.txt}"
fi
if [[ -f change-ID.txt ]]; then
cat change-ID.txt
commit_message+=' '
commit_message+="change-ID.txt"
fi
if [[ -f signed-off-by.txt ]]; then
cat signed-off-by.txt | sort | uniq -d > signed-off-by-final.txt
cat signed-off-by-final.txt
commit_message+=' '
commit_message+="signed-off-by-final.txt"
fi
git commit -s -v --amend --no-edit -m "$(cat ${commit_message})"
git log -n2
# TODO: Get rid of any merge commit in the patch
# TODO: process each commint one at a time in a loop: foreach or iterate
- name: Submit the change to Gerrit repository
id: submit
if: env.PR_NUMBER != ''
run: |
set -x
cd "$GITHUB_WORKSPACE/${{ env.PROJECT_REPO_GITHUB }}-${{ env.GERRIT_BRANCH }}"
# If the reviewers email is unset/empty then use a default
reviewers_email=${REVIEWERS_EMAIL:-"${{ vars.GERRIT_SSH_USER_G2G_EMAIL }}"}
echo "git review .... inprogress"
git review --yes -t "GH-PR-${{ env.PR_NUMBER }}" --reviewers "$reviewers_email"
# retrive change-id from the submitted PR
gerrit_change_id=$(git show HEAD --format=%B -s | grep Change-Id: | cut -d " " -f2;)
# Set a temp PR to test with ACT
if [[ "$gerrit_change_id" == '' ]]; then
echo "FAIL: Could not find the change-Id"
else
echo "GERRIT_CHANGE_ID=${gerrit_change_id}" >> "$GITHUB_ENV"
fi
# echo "GERRIT_CHANGE_ID=Iafa61d4b861941624c4c44b0fde21f534a7344ad" >> "$GITHUB_ENV"
- name: Set fake change-Id to with nektos-act
if: ( steps.submit.outputs.gerrit_change_id != '' && env.ACT == 'true' )
run: |
set -x
echo "GERRIT_CHANGE_ID=Iafa61d4b861941624c4c44b0fde21f534a7344ad" >> "$GITHUB_ENV"
- name: Retrive the Gerrit change number from Change-ID
if: env.GERRIT_CHANGE_ID != ''
id: change_num
shell: bash
run: |
set -x
# Query for a pre-existing gerrit review to retrive Change-Id
ssh -v -p 29418 "${{ vars.GERRIT_SSH_USER_G2G }}@${{ vars.GERRIT_SERVER }}" \
gerrit query limit:1 owner:self is:open project:"${{ env.PROJECT_REPO_GERRIT }}" \
--current-patch-set --format=JSON "${{ env.GERRIT_CHANGE_ID }}" > query_result.txt
query_result_url=$(jq -r '.url | select( . != null )' query_result.txt)
query_result_number=$(jq -r '.number | select( . != null )' query_result.txt)
echo "GERRIT_CHANGE_REQUEST_URL=${query_result_url}" >> "$GITHUB_ENV"
echo "GERRIT_CHANGE_REQUEST_NUMBER=${query_result_number}" >> "$GITHUB_ENV"
- name: PR Comment update CR number
if: env.GERRIT_CHANGE_REQUEST_URL != ''
uses: actions/github-script@v7
with:
result-encoding: string
retries: 3
retry-exempt-status-codes: 400,401
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `The pull-request PR-${{ env.PR_NUMBER }} is submitted to Gerrit [${{ env.ORGANIZATION }}](https://${{ vars.GERRIT_SERVER }})!
To follow up on the change visit: [${{ env.GERRIT_CHANGE_REQUEST_NUMBER }}](${{ env.GERRIT_CHANGE_REQUEST_URL }})
NOTE: The pull-request PR-${{ env.PR_NUMBER }} will be closed, re-opening the pull-request will not update the same commit and may result in duplicate changes on Gerrit.`
- name: Close pull-request
run: gh pr close --comment "Auto-closing pull request" --delete-branch "${{ env.PR_NUMBER }}"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Clean up resources and close the PR
run: |
echo "closing the up PR"