-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
CIGitLogicTest.sh
executable file
·403 lines (318 loc) · 11 KB
/
CIGitLogicTest.sh
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/bin/bash
# Fail immediately if there is an error thrown
set -e
TEST_DIR=$(dirname "$(dirname "$(cd "$(dirname "$0")" || exit 1; pwd)/$(basename "$0")")")
declare -r SCRIPTS_DIR="$TEST_DIR/../scripts"
declare -r DUMMY_DIR="$HOME/DumDumRepo"
declare -r GIT_REMOTE="$HOME/dummyGitRemotes/DumDumRepo"
declare -r SEMVER_LEVEL_BUILD='BUILD'
declare -r SEMVER_LEVEL_PATCH='PATCH'
declare -r bumpVersion="$TEST_DIR/utils/bumpVersion.mjs"
declare -r getPreviousVersion="$TEST_DIR/utils/getPreviousVersion.mjs"
declare -r getPullRequestsMergedBetween="$TEST_DIR/utils/getPullRequestsMergedBetween.mjs"
source "$SCRIPTS_DIR/shellUtils.sh"
function setup_git_as_human {
info "Switching to human git user"
git config --local user.name test
git config --local user.email test@test.com
}
function setup_git_as_osbotify {
info "Switching to OSBotify git user"
git config --local user.name OSBotify
git config --local user.email infra+osbotify@expensify.com
}
function print_version {
< package.json jq -r .version
}
function init_git_server {
info "Initializing git server..."
cd "$HOME" || exit 1
rm -rf "$GIT_REMOTE" || exit 1
mkdir -p "$GIT_REMOTE"
cd "$GIT_REMOTE" || exit 1
git init -b main
setup_git_as_human
npm init -y
npm version --no-git-tag-version 1.0.0-0
npm install underscore
echo "node_modules/" >> .gitignore
git add -A
git commit -m "Initial commit"
git switch -c staging
git tag "$(print_version)"
git branch production
git config --local receive.denyCurrentBranch ignore
success "Initialized git server in $GIT_REMOTE"
}
# Note that instead of doing a git clone, we checkout the repo following the same steps used by actions/checkout
function checkout_repo {
info "Checking out repo at $DUMMY_DIR"
if [ -d "$DUMMY_DIR" ]; then
info "Found existing directory at $DUMMY_DIR, deleting it to simulate a fresh checkout..."
cd "$HOME" || exit 1
rm -rf "$DUMMY_DIR"
fi
mkdir "$DUMMY_DIR"
cd "$DUMMY_DIR" || exit 1
git init
git remote add origin "$GIT_REMOTE"
git fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin +refs/heads/main:refs/remotes/origin/main
git checkout --progress --force -B main refs/remotes/origin/main
success "Checked out repo at $DUMMY_DIR!"
}
function bump_version {
info "Bumping version..."
setup_git_as_osbotify
git switch main
npm --no-git-tag-version version "$(node "$bumpVersion" "$(print_version)" "$1")"
git add package.json package-lock.json
git commit -m "Update version to $(print_version)"
git push origin main
success "Version bumped to $(print_version) on main"
}
function update_staging_from_main {
info "Recreating staging from main..."
git switch main
if git rev-parse --verify staging 2>/dev/null; then
git branch -D staging
fi
git switch -c staging
git push --force origin staging
success "Recreated staging from main!"
}
function update_production_from_staging {
info "Recreating production from staging..."
if ! git rev-parse --verify staging 2>/dev/null; then
git fetch origin staging --depth=1
fi
git switch staging
if git rev-parse --verify production 2>/dev/null; then
git branch -D production
fi
git switch -c production
git push --force origin production
success "Recreated production from staging!"
}
function merge_pr {
info "Merging PR #$1 to main"
git switch main
git merge "pr-$1" --no-ff -m "Merge pull request #$1 from Expensify/pr-$1"
git push origin main
git branch -d "pr-$1"
success "Merged PR #$1 to main"
}
function cherry_pick_pr {
info "Cherry-picking PR $1 to staging..."
merge_pr "$1"
PR_MERGE_COMMIT="$(git rev-parse HEAD)"
bump_version "$SEMVER_LEVEL_BUILD"
VERSION_BUMP_COMMIT="$(git rev-parse HEAD)"
checkout_repo
setup_git_as_osbotify
PREVIOUS_PATCH_VERSION="$(node "$getPreviousVersion" "$(print_version)" "$SEMVER_LEVEL_PATCH")"
git fetch origin main staging --no-tags --shallow-exclude="$PREVIOUS_PATCH_VERSION"
git switch staging
git switch -c cherry-pick-staging
git cherry-pick -x --mainline 1 "$VERSION_BUMP_COMMIT"
setup_git_as_human
git cherry-pick -x --mainline 1 --strategy=recursive -Xtheirs "$PR_MERGE_COMMIT"
setup_git_as_osbotify
git switch staging
git merge cherry-pick-staging --no-ff -m "Merge pull request #$(($1 + 1)) from Expensify/cherry-pick-staging"
git branch -d cherry-pick-staging
git push origin staging
info "Merged PR #$(($1 + 1)) into staging"
success "Successfully cherry-picked PR #$1 to staging!"
}
function tag_staging {
info "Tagging new version from the staging branch..."
setup_git_as_osbotify
if ! git rev-parse --verify staging 2>/dev/null; then
git fetch origin staging --depth=1
fi
git switch staging
git tag "$(print_version)"
git push --tags
success "Created new tag $(print_version)"
}
function assert_prs_merged_between {
checkout_repo
output=$(node "$getPullRequestsMergedBetween" "$1" "$2")
info "Checking output of getPullRequestsMergedBetween $1 $2"
assert_equal "$output" "$3"
}
### Phase 0: Verify necessary tools are installed (all tools should be pre-installed on all GitHub Actions runners)
if ! command -v jq &> /dev/null; then
error "command jq could not be found, install it with \`brew install jq\` (macOS) or \`apt-get install jq\` (Linux) and re-run this script"
exit 1
fi
if ! command -v npm &> /dev/null; then
error "command npm could not be found, install it and re-run this script"
exit 1
fi
### Setup
title "Starting setup"
init_git_server
checkout_repo
success "Setup complete!"
title "Scenario #1: Merge a pull request while the checklist is unlocked"
info "Creating PR #1..."
setup_git_as_human
git switch -c pr-1
echo "Changes from PR #1" >> PR1.txt
git add PR1.txt
git commit -m "Changes from PR #1"
success "Created PR #1 in branch pr-1"
merge_pr 1
bump_version "$SEMVER_LEVEL_BUILD"
update_staging_from_main
# Tag staging
tag_staging
git switch main
# Verify output for checklist and deploy comment
assert_prs_merged_between '1.0.0-0' '1.0.0-1' "[ 1 ]"
success "Scenario #1 completed successfully!"
title "Scenario #2: Merge a pull request with the checklist locked, but don't CP it"
info "Creating PR #2..."
setup_git_as_human
git switch -c pr-2
echo "Changes from PR #2" >> PR2.txt
git add PR2.txt
git commit -m "Changes from PR #2"
merge_pr 2
success "Scenario #2 completed successfully!"
title "Scenario #3: Merge a pull request with the checklist locked and CP it to staging"
info "Creating PR #3 and merging it into main..."
git switch -c pr-3
echo "Changes from PR #3" >> PR3.txt
git add PR3.txt
git commit -m "Changes from PR #3"
cherry_pick_pr 3
tag_staging
# Verify output for checklist
assert_prs_merged_between '1.0.0-0' '1.0.0-2' "[ 3, 1 ]"
# Verify output for deploy comment
assert_prs_merged_between '1.0.0-1' '1.0.0-2' "[ 3 ]"
success "Scenario #3 completed successfully!"
title "Scenario #4: Close the checklist"
title "Scenario #4A: Run the production deploy"
update_production_from_staging
# Verify output for release body and production deploy comments
assert_prs_merged_between '1.0.0-0' '1.0.0-2' "[ 3, 1 ]"
success "Scenario #4A completed successfully!"
title "Scenario #4B: Run the staging deploy and create a new checklist"
bump_version "$SEMVER_LEVEL_PATCH"
update_staging_from_main
tag_staging
# Verify output for new checklist and staging deploy comments
# We also include the cherry picked PR 3 which is issue we will fix later
assert_prs_merged_between '1.0.0-2' '1.0.1-0' "[ 3, 2 ]"
success "Scenario #4B completed successfully!"
title "Scenario #5: Merging another pull request when the checklist is unlocked"
info "Creating PR #5..."
setup_git_as_human
git switch main
git switch -c pr-5
echo "Changes from PR #5" >> PR5.txt
git add PR5.txt
git commit -m "Changes from PR #5"
merge_pr 5
bump_version "$SEMVER_LEVEL_BUILD"
update_staging_from_main
tag_staging
# Verify output for checklist
assert_prs_merged_between '1.0.0-2' '1.0.1-1' "[ 5, 3, 2 ]"
# Verify output for deploy comment
assert_prs_merged_between '1.0.1-0' '1.0.1-1' "[ 5 ]"
success "Scenario #5 completed successfully!"
title "Scenario #6: Deploying a PR, then CPing a revert, then adding the same code back again before the next production deploy results in the correct code on staging and production."
info "Creating myFile.txt in PR #6"
setup_git_as_human
git switch main
git switch -c pr-6
echo "Changes from PR #6" >> myFile.txt
git add myFile.txt
git commit -m "Add myFile.txt in PR #6"
merge_pr 6
bump_version "$SEMVER_LEVEL_BUILD"
update_staging_from_main
tag_staging
# Verify output for checklist
assert_prs_merged_between '1.0.0-2' '1.0.1-2' "[ 6, 5, 3, 2 ]"
# Verify output for deploy comment
assert_prs_merged_between '1.0.1-1' '1.0.1-2' "[ 6 ]"
info "Appending and prepending content to myFile.txt in PR #7"
setup_git_as_human
git switch main
git switch -c pr-7
printf "[DEBUG] Before:\n\n%s" "$(cat myFile.txt)"
printf "Prepended content\n%s" "$(cat myFile.txt)" > myFile.txt
printf "\nAppended content\n" >> myFile.txt
printf "\n\n[DEBUG] After:\n\n%s\n" "$(cat myFile.txt)"
git add myFile.txt
git commit -m "Append and prepend content in myFile.txt"
merge_pr 7
bump_version "$SEMVER_LEVEL_BUILD"
update_staging_from_main
tag_staging
# Verify output for checklist
assert_prs_merged_between '1.0.0-2' '1.0.1-3' "[ 7, 6, 5, 3, 2 ]"
# Verify output for deploy comment
assert_prs_merged_between '1.0.1-2' '1.0.1-3' "[ 7 ]"
info "Making an unrelated change in PR #8"
setup_git_as_human
git switch main
git switch -c pr-8
echo "some content" >> anotherFile.txt
git add anotherFile.txt
git commit -m "Create another file"
merge_pr 8
info "Reverting the append + prepend on main in PR #9"
setup_git_as_human
git switch main
git switch -c pr-9
printf "Before:\n\n%s\n" "$(cat myFile.txt)"
echo "some content" > myFile.txt
printf "\nAfter:\n\n%s\n" "$(cat myFile.txt)"
git add myFile.txt
git commit -m "Revert append and prepend"
cherry_pick_pr 9
tag_staging
info "Verifying that the revert is present on staging, but the unrelated change is not"
if [[ "$(cat myFile.txt)" != "some content" ]]; then
error "Revert did not make it to staging"
exit 1
else
success "Revert made it to staging"
fi
if [[ -f anotherFile.txt ]]; then
error "Unrelated change made it to staging"
exit 1
else
success "Unrelated change not on staging yet"
fi
info "Repeating previously reverted append + prepend on main in PR #10"
setup_git_as_human
git switch main
git switch -c pr-10
printf "[DEBUG] Before:\n\n%s" "$(cat myFile.txt)"
printf "Prepended content\n%s" "$(cat myFile.txt)" > myFile.txt
printf "\nAppended content\n" >> myFile.txt
printf "\n\n[DEBUG] After:\n\n%s\n" "$(cat myFile.txt)"
git add myFile.txt
git commit -m "Append and prepend content in myFile.txt"
merge_pr 10
update_production_from_staging
bump_version "$SEMVER_LEVEL_PATCH"
update_staging_from_main
tag_staging
# Verify production release list
assert_prs_merged_between '1.0.0-2' '1.0.1-4' "[ 9, 7, 6, 5, 3, 2 ]"
# Verify PR list for the new checklist
assert_prs_merged_between '1.0.1-4' '1.0.2-0' "[ 10, 9, 8 ]"
### Cleanup
title "Cleaning up..."
cd "$TEST_DIR" || exit 1
rm -rf "$DUMMY_DIR"
rm -rf "$GIT_REMOTE"
success "All tests passed! Hooray!"