-
Notifications
You must be signed in to change notification settings - Fork 1
264 lines (222 loc) · 9.76 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
---
name: Github PR to Gerrit Change request
# yamllint disable-line rule:truthy
on:
pull_request:
types: [opened, synchronize, edited, reopened]
branches:
- master
- main
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
gh2gerrit-push:
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: Set env with Gerrit Github repo format
# if: ${{ github.repository != '' && contains(//) }}
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"
# TODO: Read repo info from .gitreview file
- 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 }}"
# 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 -n10
- 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 PR number
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
# cat ${{ env.PR_NUMBER }}.patch
# TODO: Get number of commits in the patch file and set var in env PR_COMMIT_NUM
- 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 }}"
if [[ ${{ env.ACT }} ]]; then
git --no-pager log -n2 -p
fi
# Apply the patch on the HEAD
# git am --show-current-patch=diff "$GITHUB_WORKSPACE/${{ env.PR_NUMBER }}.patch"
if [ $? -ne 0 ]; then
git --no-pager log -n2 -p
git --no-pager diff
git status
fi
# Create a Change-id from all the PR
# Capture the last commit HEAD
git rev-parse HEAD > last-head-commit-id.txt
# Capture and check for signed-of-by and Change-Id
git reset --soft HEAD~2
# Check if change-id is present in pull request and process this conditionally
git log --format=%B --reverse HEAD..HEAD@{1} | grep -E "^(Change-Id)" > Change-ID.txt || true
git log --format=%B --reverse HEAD..HEAD@{1} | grep -E "^(Signed-off-by)" > signed-off-by.txt || true
git log --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}}@${{ vars.GERRIT_SERVER}}"}
echo "git review .... inprogress"
git review -n --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
query_result=$(ssh -v -p 29418 "${{ vars.GERRIT_SSH_USER_G2G }}@${{ vars.GERRIT_SERVER }}" \
gerrit query limit:1 is:open \
--current-patch-set --format=JSON "${{ env.GERRIT_CHANGE_ID }}" | jq -r '.url' | grep https)
echo "GERRIT_CHANGE_REQUEST_URL=${query_result}" >> "$GITHUB_ENV"
# gerrit query limit:1 owner:self is:open project:"${{ env.PROJECT_REPO_GITHUB }}" \
- 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: "PR-${{ env.PR_NUMBER }} pushed Gerrit for review ${{ env.GERRIT_CHANGE_REQUEST_URL }}!"
})
- name: Clean up resources and close the PR
run: |
echo "closing the up PR"
# - uses: actions/stale@v9
# with: