Skip to content

Commit d0bd808

Browse files
committed
Expose a more restrictive interface to the release script
Give the release script modes rather than source and target branches
1 parent bed132d commit d0bd808

2 files changed

Lines changed: 38 additions & 35 deletions

File tree

.github/update-release-branch.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
1414
"""
1515

16+
# Value of the mode flag for a v1 release
17+
V1_MODE = 'v1-release'
18+
19+
# Value of the mode flag for a v2 release
20+
V2_MODE = 'v2-release'
21+
1622
# Name of the remote
1723
ORIGIN = 'origin'
1824

@@ -30,7 +36,7 @@ def branch_exists_on_remote(branch_name):
3036
return run_git('ls-remote', '--heads', ORIGIN, branch_name).strip() != ''
3137

3238
# Opens a PR from the given branch to the target branch
33-
def open_pr(repo, all_commits, source_branch_short_sha, new_branch_name, source_branch, target_branch, conductor, is_v2_to_v1_backport, labels):
39+
def open_pr(repo, all_commits, source_branch_short_sha, new_branch_name, source_branch, target_branch, conductor, is_v2_release, labels):
3440
# Sort the commits into the pull requests that introduced them,
3541
# and any commits that don't have a pull request
3642
pull_requests = []
@@ -79,7 +85,7 @@ def open_pr(repo, all_commits, source_branch_short_sha, new_branch_name, source_
7985
body.append(' - [ ] The CHANGELOG includes all relevant, user-facing changes since the last release.')
8086
body.append(' - [ ] There are no unexpected commits being merged into the ' + target_branch + ' branch.')
8187
body.append(' - [ ] The docs team is aware of any documentation changes that need to be released.')
82-
if not is_v2_to_v1_backport:
88+
if is_v2_release:
8389
body.append(' - [ ] The mergeback PR is merged back into ' + source_branch + ' after this PR is merged.')
8490
body.append(' - [ ] The v1 release PR is merged after this PR is merged.')
8591

@@ -181,48 +187,48 @@ def main():
181187
help='The nwo of the repository, for example github/codeql-action.'
182188
)
183189
parser.add_argument(
184-
'--source-branch',
185-
type=str,
186-
required=True,
187-
help='The branch being merged from, typically "main" for a v2 release or "v2" for a v1 release.'
188-
)
189-
parser.add_argument(
190-
'--target-branch',
190+
'--mode',
191191
type=str,
192192
required=True,
193-
help='The branch being merged into, typically "v2" for a v2 release or "v1" for a v1 release.'
193+
choices=[V2_MODE, V1_MODE],
194+
help=f"Which release to perform. '{V2_MODE}' uses main as the source branch and v2 as the target branch. " +
195+
f"'{V1_MODE}' uses v2 as the source branch and v1 as the target branch."
194196
)
195197
parser.add_argument(
196198
'--conductor',
197199
type=str,
198200
required=True,
199201
help='The GitHub handle of the person who is conducting the release process.'
200202
)
201-
parser.add_argument(
202-
'--perform-v2-to-v1-backport',
203-
action='store_true',
204-
help='Pass this flag if this release is a backport from v2 to v1.'
205-
)
206203

207204
args = parser.parse_args()
208205

206+
if args.mode == V2_MODE:
207+
source_branch = 'main'
208+
target_branch = 'v2'
209+
elif args.mode == V1_MODE:
210+
source_branch = 'v2'
211+
target_branch = 'v1'
212+
else:
213+
raise ValueError(f"Unexpected value for release mode: '{args.mode}'")
214+
209215
repo = Github(args.github_token).get_repo(args.repository_nwo)
210216
version = get_current_version()
211217

212-
if args.perform_v2_to_v1_backport:
218+
if args.mode == V1_MODE:
213219
# Change the version number to a v1 equivalent
214220
version = get_current_version()
215221
version = f'1{version[1:]}'
216222

217223
# Print what we intend to go
218-
print('Considering difference between ' + args.source_branch + ' and ' + args.target_branch)
219-
source_branch_short_sha = run_git('rev-parse', '--short', ORIGIN + '/' + args.source_branch).strip()
220-
print('Current head of ' + args.source_branch + ' is ' + source_branch_short_sha)
224+
print('Considering difference between ' + source_branch + ' and ' + target_branch)
225+
source_branch_short_sha = run_git('rev-parse', '--short', ORIGIN + '/' + source_branch).strip()
226+
print('Current head of ' + source_branch + ' is ' + source_branch_short_sha)
221227

222228
# See if there are any commits to merge in
223-
commits = get_commit_difference(repo=repo, source_branch=args.source_branch, target_branch=args.target_branch)
229+
commits = get_commit_difference(repo=repo, source_branch=source_branch, target_branch=target_branch)
224230
if len(commits) == 0:
225-
print('No commits to merge from ' + args.source_branch + ' to ' + args.target_branch)
231+
print('No commits to merge from ' + source_branch + ' to ' + target_branch)
226232
return
227233

228234
# The branch name is based off of the name of branch being merged into
@@ -240,7 +246,7 @@ def main():
240246
# Create the new branch and push it to the remote
241247
print('Creating branch ' + new_branch_name)
242248

243-
if args.perform_v2_to_v1_backport:
249+
if args.mode == V1_MODE:
244250
# If we're performing a backport, start from the v1 branch
245251
print(f'Creating {new_branch_name} from the {ORIGIN}/v1 branch')
246252
run_git('checkout', '-b', new_branch_name, f'{ORIGIN}/v1')
@@ -267,8 +273,8 @@ def main():
267273
else:
268274
print(' Nothing to revert.')
269275

270-
print(f'Merging {ORIGIN}/{args.source_branch} into the release prep branch')
271-
run_git('merge', f'{ORIGIN}/{args.source_branch}', '--no-edit')
276+
print(f'Merging {ORIGIN}/{source_branch} into the release prep branch')
277+
run_git('merge', f'{ORIGIN}/{source_branch}', '--no-edit')
272278

273279
# Migrate the package version number from a v2 version number to a v1 version number
274280
print(f'Setting version number to {version}')
@@ -286,7 +292,7 @@ def main():
286292
# If we're performing a standard release, there won't be any new commits on the target branch,
287293
# as these will have already been merged back into the source branch. Therefore we can just
288294
# start from the source branch.
289-
run_git('checkout', '-b', new_branch_name, f'{ORIGIN}/{args.source_branch}')
295+
run_git('checkout', '-b', new_branch_name, f'{ORIGIN}/{source_branch}')
290296

291297
print('Updating changelog')
292298
update_changelog(version)
@@ -303,11 +309,11 @@ def main():
303309
commits,
304310
source_branch_short_sha,
305311
new_branch_name,
306-
source_branch=args.source_branch,
307-
target_branch=args.target_branch,
312+
source_branch=source_branch,
313+
target_branch=target_branch,
308314
conductor=args.conductor,
309-
is_v2_to_v1_backport=args.perform_v2_to_v1_backport,
310-
labels=['Update dependencies'] if args.perform_v2_to_v1_backport else [],
315+
is_v2_release=args.mode == V2_MODE,
316+
labels=['Update dependencies'] if args.mode == V1_MODE else [],
311317
)
312318

313319
if __name__ == '__main__':

.github/workflows/update-release-branch.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ jobs:
4949
python .github/update-release-branch.py \
5050
--github-token ${{ secrets.GITHUB_TOKEN }} \
5151
--repository-nwo ${{ github.repository }} \
52-
--source-branch main \
53-
--target-branch v2 \
52+
--mode release-v2 \
5453
--conductor ${GITHUB_ACTOR}
5554
5655
- name: Update v1 release branch
@@ -59,7 +58,5 @@ jobs:
5958
python .github/update-release-branch.py \
6059
--github-token ${{ secrets.GITHUB_TOKEN }} \
6160
--repository-nwo ${{ github.repository }} \
62-
--source-branch v2 \
63-
--target-branch v1 \
64-
--conductor ${GITHUB_ACTOR} \
65-
--perform-v2-to-v1-backport
61+
--mode release-v1 \
62+
--conductor ${GITHUB_ACTOR}

0 commit comments

Comments
 (0)