Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e0b20ee

Browse files
committed
Migrate release script to template strings
1 parent 218d40d commit e0b20ee

1 file changed

Lines changed: 21 additions & 21 deletions

File tree

.github/update-release-branch.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def run_git(*args, allow_non_zero_exit_code=False):
2626
cmd = ['git', *args]
2727
p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2828
if not allow_non_zero_exit_code and p.returncode != 0:
29-
raise Exception('Call to ' + ' '.join(cmd) + ' exited with code ' + str(p.returncode) + ' stderr:' + p.stderr.decode('ascii'))
29+
raise Exception(f'Call to {" ".join(cmd)} exited with code {p.returncode} stderr: {p.stderr.decode("ascii")}.')
3030
return p.stdout.decode('ascii')
3131

3232
# Returns true if the given branch exists on the origin remote
@@ -40,23 +40,23 @@ def open_pr(repo, all_commits, source_branch_short_sha, new_branch_name, conduct
4040
pull_requests = []
4141
commits_without_pull_requests = []
4242
for commit in all_commits:
43-
pr = get_pr_for_commit(repo, commit)
43+
pr = get_pr_for_commit(commit)
4444

4545
if pr is None:
4646
commits_without_pull_requests.append(commit)
4747
elif not any(p for p in pull_requests if p.number == pr.number):
4848
pull_requests.append(pr)
4949

50-
print('Found ' + str(len(pull_requests)) + ' pull requests')
51-
print('Found ' + str(len(commits_without_pull_requests)) + ' commits not in a pull request')
50+
print(f'Found {len(pull_requests)} pull requests.')
51+
print(f'Found {len(commits_without_pull_requests)} commits not in a pull request.')
5252

5353
# Sort PRs and commits by age
5454
pull_requests = sorted(pull_requests, key=lambda pr: pr.number)
5555
commits_without_pull_requests = sorted(commits_without_pull_requests, key=lambda c: c.commit.author.date)
5656

5757
# Start constructing the body text
5858
body = []
59-
body.append('Merging ' + source_branch_short_sha + ' into ' + TARGET_BRANCH)
59+
body.append(f'Merging {source_branch_short_sha} into {TARGET_BRANCH}.')
6060

6161
body.append('')
6262
body.append(f'Conductor for this PR is @{conductor}.')
@@ -81,22 +81,22 @@ def open_pr(repo, all_commits, source_branch_short_sha, new_branch_name, conduct
8181
body.append('Please do the following:')
8282
body.append(' - [ ] Ensure the CHANGELOG displays the correct version and date.')
8383
body.append(' - [ ] Ensure the CHANGELOG includes all relevant, user-facing changes since the last release.')
84-
body.append(' - [ ] Check that there are not any unexpected commits being merged into the ' + TARGET_BRANCH + ' branch.')
84+
body.append(f' - [ ] Check that there are not any unexpected commits being merged into the {TARGET_BRANCH} branch.')
8585
body.append(' - [ ] Ensure the docs team is aware of any documentation changes that need to be released.')
8686
body.append(' - [ ] Approve and merge this PR. Make sure `Create a merge commit` is selected rather than `Squash and merge` or `Rebase and merge`.')
8787
body.append(' - [ ] Merge the mergeback PR that will automatically be created once this PR is merged.')
8888

89-
title = 'Merge ' + SOURCE_BRANCH + ' into ' + TARGET_BRANCH
89+
title = f'Merge {SOURCE_BRANCH} into {TARGET_BRANCH}'
9090

9191
# Create the pull request
9292
# PR checks won't be triggered on PRs created by Actions. Therefore mark the PR as draft so that
9393
# a maintainer can take the PR out of draft, thereby triggering the PR checks.
9494
pr = repo.create_pull(title=title, body='\n'.join(body), head=new_branch_name, base=TARGET_BRANCH, draft=True)
95-
print('Created PR #' + str(pr.number))
95+
print(f'Created PR #{pr.number}')
9696

9797
# Assign the conductor
9898
pr.add_to_assignees(conductor)
99-
print('Assigned PR to ' + conductor)
99+
print(f'Assigned PR to {conductor}')
100100

101101
# Gets a list of the SHAs of all commits that have happened on the source branch
102102
# since the last release to the target branch.
@@ -105,7 +105,7 @@ def open_pr(repo, all_commits, source_branch_short_sha, new_branch_name, conduct
105105
def get_commit_difference(repo):
106106
# Passing split nothing means that the empty string splits to nothing: compare `''.split() == []`
107107
# to `''.split('\n') == ['']`.
108-
commits = run_git('log', '--pretty=format:%H', ORIGIN + '/' + TARGET_BRANCH + '..' + ORIGIN + '/' + SOURCE_BRANCH).strip().split()
108+
commits = run_git('log', '--pretty=format:%H', f'{ORIGIN}/{TARGET_BRANCH}..{ORIGIN}/{SOURCE_BRANCH}').strip().split()
109109

110110
# Convert to full-fledged commit objects
111111
commits = [repo.get_commit(c) for c in commits]
@@ -121,13 +121,13 @@ def is_pr_merge_commit(commit):
121121
def get_truncated_commit_message(commit):
122122
message = commit.commit.message.split('\n')[0]
123123
if len(message) > 60:
124-
return message[:57] + '...'
124+
return f'{message[:57]}...'
125125
else:
126126
return message
127127

128128
# Converts a commit into the PR that introduced it to the source branch.
129129
# Returns the PR object, or None if no PR could be found.
130-
def get_pr_for_commit(repo, commit):
130+
def get_pr_for_commit(commit):
131131
prs = commit.get_pulls()
132132

133133
if prs.totalCount > 0:
@@ -161,7 +161,7 @@ def update_changelog(version):
161161
else:
162162
content = EMPTY_CHANGELOG
163163

164-
newContent = content.replace('[UNRELEASED]', version + ' - ' + get_today_string(), 1)
164+
newContent = content.replace('[UNRELEASED]', f'${version} - {get_today_string()}', 1)
165165

166166
with open('CHANGELOG.md', 'w') as f:
167167
f.write(newContent)
@@ -195,30 +195,30 @@ def main():
195195
version = get_current_version()
196196

197197
# Print what we intend to go
198-
print('Considering difference between ' + SOURCE_BRANCH + ' and ' + TARGET_BRANCH)
199-
source_branch_short_sha = run_git('rev-parse', '--short', ORIGIN + '/' + SOURCE_BRANCH).strip()
200-
print('Current head of ' + SOURCE_BRANCH + ' is ' + source_branch_short_sha)
198+
print(f'Considering difference between {SOURCE_BRANCH} and {TARGET_BRANCH}...')
199+
source_branch_short_sha = run_git('rev-parse', '--short', f'{ORIGIN}/{SOURCE_BRANCH}').strip()
200+
print(f'Current head of {SOURCE_BRANCH} is {source_branch_short_sha}.')
201201

202202
# See if there are any commits to merge in
203203
commits = get_commit_difference(repo=repo)
204204
if len(commits) == 0:
205-
print('No commits to merge from ' + SOURCE_BRANCH + ' to ' + TARGET_BRANCH)
205+
print(f'No commits to merge from {SOURCE_BRANCH} to {TARGET_BRANCH}.')
206206
return
207207

208208
# The branch name is based off of the name of branch being merged into
209209
# and the SHA of the branch being merged from. Thus if the branch already
210210
# exists we can assume we don't need to recreate it.
211-
new_branch_name = 'update-v' + version + '-' + source_branch_short_sha
212-
print('Branch name is ' + new_branch_name)
211+
new_branch_name = f'update-v{version}-{source_branch_short_sha}'
212+
print(f'Branch name is {new_branch_name}.')
213213

214214
# Check if the branch already exists. If so we can abort as this script
215215
# has already run on this combination of branches.
216216
if branch_exists_on_remote(new_branch_name):
217-
print('Branch ' + new_branch_name + ' already exists. Nothing to do.')
217+
print(f'Branch {new_branch_name} already exists. Nothing to do.')
218218
return
219219

220220
# Create the new branch and push it to the remote
221-
print('Creating branch ' + new_branch_name)
221+
print(f'Creating branch {new_branch_name}.')
222222

223223
# If we're performing a standard release, there won't be any new commits on the target branch,
224224
# as these will have already been merged back into the source branch. Therefore we can just

0 commit comments

Comments
 (0)