44import requests
55import subprocess
66import sys
7+ import json
8+ import datetime
9+ import os
10+
11+ EMPTY_CHANGELOG = """# CodeQL Action and CodeQL Runner Changelog
12+
13+ ## [UNRELEASED]
14+
15+ """
716
817# The branch being merged from.
918# This is the one that contains day-to-day development work.
@@ -49,32 +58,40 @@ def open_pr(repo, all_commits, short_main_sha, branch_name):
4958 commits_without_pull_requests = sorted (commits_without_pull_requests , key = lambda c : c .commit .author .date )
5059
5160 # Start constructing the body text
52- body = 'Merging ' + short_main_sha + ' into ' + LATEST_RELEASE_BRANCH
61+ body = []
62+ body .append ('Merging ' + short_main_sha + ' into ' + LATEST_RELEASE_BRANCH )
5363
5464 conductor = get_conductor (repo , pull_requests , commits_without_pull_requests )
55- body += '\n \n Conductor for this PR is @' + conductor
65+ body .append ('' )
66+ body .append ('Conductor for this PR is @' + conductor )
5667
5768 # List all PRs merged
5869 if len (pull_requests ) > 0 :
59- body += '\n \n Contains the following pull requests:'
70+ body .append ('' )
71+ body .append ('Contains the following pull requests:' )
6072 for pr in pull_requests :
6173 merger = get_merger_of_pr (repo , pr )
62- body += '\n - #' + str (pr .number )
63- body += ' - ' + pr .title
64- body += ' (@' + merger + ')'
74+ body .append ('- #' + str (pr .number ) + ' - ' + pr .title + ' (@' + merger + ')' )
6575
6676 # List all commits not part of a PR
6777 if len (commits_without_pull_requests ) > 0 :
68- body += '\n \n Contains the following commits not from a pull request:'
78+ body .append ('' )
79+ body .append ('Contains the following commits not from a pull request:' )
6980 for commit in commits_without_pull_requests :
70- body += '\n - ' + commit .sha
71- body += ' - ' + get_truncated_commit_message (commit )
72- body += ' (@' + commit .author .login + ')'
81+ body .append ('- ' + commit .sha + ' - ' + get_truncated_commit_message (commit ) + ' (@' + commit .author .login + ')' )
82+
83+ body .append ('' )
84+ body .append ('Please review the following:' )
85+ body .append (' - [ ] The CHANGELOG displays the correct version and date.' )
86+ body .append (' - [ ] The CHANGELOG includes all relevant, user-facing changes since the last release.' )
87+ body .append (' - [ ] There are no unexpected commits being merged into the ' + LATEST_RELEASE_BRANCH + ' branch.' )
88+ body .append (' - [ ] The docs team is aware of any documentation changes that need to be released.' )
89+ body .append (' - [ ] The mergeback PR is merged back into ' + MAIN_BRANCH + ' after this PR is merged.' )
7390
7491 title = 'Merge ' + MAIN_BRANCH + ' into ' + LATEST_RELEASE_BRANCH
7592
7693 # Create the pull request
77- pr = repo .create_pull (title = title , body = body , head = branch_name , base = LATEST_RELEASE_BRANCH )
94+ pr = repo .create_pull (title = title , body = ' \n ' . join ( body ) , head = branch_name , base = LATEST_RELEASE_BRANCH )
7895 print ('Created PR #' + str (pr .number ))
7996
8097 # Assign the conductor
@@ -95,7 +112,7 @@ def get_conductor(repo, pull_requests, other_commits):
95112# This will not include any commits that exist on the release branch
96113# that aren't on main.
97114def get_commit_difference (repo ):
98- commits = run_git ('log' , '--pretty=format:%H' , ORIGIN + '/' + LATEST_RELEASE_BRANCH + '..' + MAIN_BRANCH ).strip ().split ('\n ' )
115+ commits = run_git ('log' , '--pretty=format:%H' , ORIGIN + '/' + LATEST_RELEASE_BRANCH + '..' + ORIGIN + '/' + MAIN_BRANCH ).strip ().split ('\n ' )
99116
100117 # Convert to full-fledged commit objects
101118 commits = [repo .get_commit (c ) for c in commits ]
@@ -135,17 +152,40 @@ def get_pr_for_commit(repo, commit):
135152def get_merger_of_pr (repo , pr ):
136153 return repo .get_commit (pr .merge_commit_sha ).author .login
137154
155+ def get_current_version ():
156+ with open ('package.json' , 'r' ) as f :
157+ return json .load (f )['version' ]
158+
159+ def get_today_string ():
160+ today = datetime .datetime .today ()
161+ return '{:%d %b %Y}' .format (today )
162+
163+ def update_changelog (version ):
164+ if (os .path .exists ('CHANGELOG.md' )):
165+ content = ''
166+ with open ('CHANGELOG.md' , 'r' ) as f :
167+ content = f .read ()
168+ else :
169+ content = EMPTY_CHANGELOG
170+
171+ newContent = content .replace ('[UNRELEASED]' , version + ' - ' + get_today_string (), 1 )
172+
173+ with open ('CHANGELOG.md' , 'w' ) as f :
174+ f .write (newContent )
175+
176+
138177def main ():
139178 if len (sys .argv ) != 3 :
140179 raise Exception ('Usage: update-release.branch.py <github token> <repository nwo>' )
141180 github_token = sys .argv [1 ]
142181 repository_nwo = sys .argv [2 ]
143182
144183 repo = Github (github_token ).get_repo (repository_nwo )
184+ version = get_current_version ()
145185
146186 # Print what we intend to go
147187 print ('Considering difference between ' + MAIN_BRANCH + ' and ' + LATEST_RELEASE_BRANCH )
148- short_main_sha = run_git ('rev-parse' , '--short' , MAIN_BRANCH ).strip ()
188+ short_main_sha = run_git ('rev-parse' , '--short' , ORIGIN + '/' + MAIN_BRANCH ).strip ()
149189 print ('Current head of ' + MAIN_BRANCH + ' is ' + short_main_sha )
150190
151191 # See if there are any commits to merge in
@@ -157,7 +197,7 @@ def main():
157197 # The branch name is based off of the name of branch being merged into
158198 # and the SHA of the branch being merged from. Thus if the branch already
159199 # exists we can assume we don't need to recreate it.
160- new_branch_name = 'update-' + LATEST_RELEASE_BRANCH + '-' + short_main_sha
200+ new_branch_name = 'update-v ' + version + '-' + short_main_sha
161201 print ('Branch name is ' + new_branch_name )
162202
163203 # Check if the branch already exists. If so we can abort as this script
@@ -168,7 +208,15 @@ def main():
168208
169209 # Create the new branch and push it to the remote
170210 print ('Creating branch ' + new_branch_name )
171- run_git ('checkout' , '-b' , new_branch_name , MAIN_BRANCH )
211+ run_git ('checkout' , '-b' , new_branch_name , ORIGIN + '/' + MAIN_BRANCH )
212+
213+ print ('Updating changelog' )
214+ update_changelog (version )
215+
216+ # Create a commit that updates the CHANGELOG
217+ run_git ('add' , 'CHANGELOG.md' )
218+ run_git ('commit' , '-m' , version )
219+
172220 run_git ('push' , ORIGIN , new_branch_name )
173221
174222 # Open a PR to update the branch
0 commit comments