44import requests
55import subprocess
66import sys
7+ import json
8+ import datetime
9+ import os
10+
11+ EMPTY_CHANGELOG = """
12+ # CodeQL Action and CodeQL Runner Changelog
13+
14+ ## [UNRELEASED]
15+
16+ """
717
818# The branch being merged from.
919# This is the one that contains day-to-day development work.
@@ -49,32 +59,40 @@ def open_pr(repo, all_commits, short_main_sha, branch_name):
4959 commits_without_pull_requests = sorted (commits_without_pull_requests , key = lambda c : c .commit .author .date )
5060
5161 # Start constructing the body text
52- body = 'Merging ' + short_main_sha + ' into ' + LATEST_RELEASE_BRANCH
62+ body = []
63+ body .append ('Merging ' + short_main_sha + ' into ' + LATEST_RELEASE_BRANCH )
5364
5465 conductor = get_conductor (repo , pull_requests , commits_without_pull_requests )
55- body += '\n \n Conductor for this PR is @' + conductor
66+ body .append ('' )
67+ body .append ('Conductor for this PR is @' + conductor )
5668
5769 # List all PRs merged
5870 if len (pull_requests ) > 0 :
59- body += '\n \n Contains the following pull requests:'
71+ body .append ('' )
72+ body .append ('Contains the following pull requests:' )
6073 for pr in pull_requests :
6174 merger = get_merger_of_pr (repo , pr )
62- body += '\n - #' + str (pr .number )
63- body += ' - ' + pr .title
64- body += ' (@' + merger + ')'
75+ body .append ('- #' + str (pr .number ) + ' - ' + pr .title + ' (@' + merger + ')' )
6576
6677 # List all commits not part of a PR
6778 if len (commits_without_pull_requests ) > 0 :
68- body += '\n \n Contains the following commits not from a pull request:'
79+ body .append ('' )
80+ body .append ('Contains the following commits not from a pull request:' )
6981 for commit in commits_without_pull_requests :
70- body += '\n - ' + commit .sha
71- body += ' - ' + get_truncated_commit_message (commit )
72- body += ' (@' + commit .author .login + ')'
82+ body .append ('- ' + commit .sha + ' - ' + get_truncated_commit_message (commit ) + ' (@' + commit .author .login + ')' )
83+
84+ body .append ('' )
85+ body .append ('Please review the following:' )
86+ body .append (' - [ ] The CHANGELOG displays the correct version and date.' )
87+ body .append (' - [ ] The CHANGELOG includes all relevant, user-facing changes since the last release.' )
88+ body .append (' - [ ] There are no unexpected commits being merged into the ' + LATEST_RELEASE_BRANCH + ' branch.' )
89+ body .append (' - [ ] The docs team is aware of any documentation changes that need to be released.' )
90+ body .append (' - [ ] The mergeback PR is merged back into ' + MAIN_BRANCH + ' after this PR is merged.' )
7391
7492 title = 'Merge ' + MAIN_BRANCH + ' into ' + LATEST_RELEASE_BRANCH
7593
7694 # Create the pull request
77- pr = repo .create_pull (title = title , body = body , head = branch_name , base = LATEST_RELEASE_BRANCH )
95+ pr = repo .create_pull (title = title , body = ' \n ' . join ( body ) , head = branch_name , base = LATEST_RELEASE_BRANCH )
7896 print ('Created PR #' + str (pr .number ))
7997
8098 # Assign the conductor
@@ -95,7 +113,7 @@ def get_conductor(repo, pull_requests, other_commits):
95113# This will not include any commits that exist on the release branch
96114# that aren't on main.
97115def get_commit_difference (repo ):
98- commits = run_git ('log' , '--pretty=format:%H' , ORIGIN + '/' + LATEST_RELEASE_BRANCH + '..' + MAIN_BRANCH ).strip ().split ('\n ' )
116+ commits = run_git ('log' , '--pretty=format:%H' , ORIGIN + '/' + LATEST_RELEASE_BRANCH + '..' + ORIGIN + '/' + MAIN_BRANCH ).strip ().split ('\n ' )
99117
100118 # Convert to full-fledged commit objects
101119 commits = [repo .get_commit (c ) for c in commits ]
@@ -135,17 +153,40 @@ def get_pr_for_commit(repo, commit):
135153def get_merger_of_pr (repo , pr ):
136154 return repo .get_commit (pr .merge_commit_sha ).author .login
137155
156+ def get_current_version ():
157+ with open ('package.json' , 'r' ) as f :
158+ return json .load (f )['version' ]
159+
160+ def get_today_string ():
161+ today = datetime .datetime .today ()
162+ return '{:%d %b %Y}' .format (today )
163+
164+ def update_changelog (version ):
165+ if (os .path .exists ('CHANGELOG.md' )):
166+ content = ''
167+ with open ('CHANGELOG.md' , 'r' ) as f :
168+ content = f .read ()
169+ else :
170+ content = EMPTY_CHANGELOG
171+
172+ newContent = content .replace ('[UNRELEASED]' , version + ' - ' + get_today_string (), 1 )
173+
174+ with open ('CHANGELOG.md' , 'w' ) as f :
175+ f .write (newContent )
176+
177+
138178def main ():
139179 if len (sys .argv ) != 3 :
140180 raise Exception ('Usage: update-release.branch.py <github token> <repository nwo>' )
141181 github_token = sys .argv [1 ]
142182 repository_nwo = sys .argv [2 ]
143183
144184 repo = Github (github_token ).get_repo (repository_nwo )
185+ version = get_current_version ()
145186
146187 # Print what we intend to go
147188 print ('Considering difference between ' + MAIN_BRANCH + ' and ' + LATEST_RELEASE_BRANCH )
148- short_main_sha = run_git ('rev-parse' , '--short' , MAIN_BRANCH ).strip ()
189+ short_main_sha = run_git ('rev-parse' , '--short' , ORIGIN + '/' + MAIN_BRANCH ).strip ()
149190 print ('Current head of ' + MAIN_BRANCH + ' is ' + short_main_sha )
150191
151192 # See if there are any commits to merge in
@@ -157,7 +198,7 @@ def main():
157198 # The branch name is based off of the name of branch being merged into
158199 # and the SHA of the branch being merged from. Thus if the branch already
159200 # exists we can assume we don't need to recreate it.
160- new_branch_name = 'update-' + LATEST_RELEASE_BRANCH + '-' + short_main_sha
201+ new_branch_name = 'update-v ' + version + '-' + short_main_sha
161202 print ('Branch name is ' + new_branch_name )
162203
163204 # Check if the branch already exists. If so we can abort as this script
@@ -168,7 +209,15 @@ def main():
168209
169210 # Create the new branch and push it to the remote
170211 print ('Creating branch ' + new_branch_name )
171- run_git ('checkout' , '-b' , new_branch_name , MAIN_BRANCH )
212+ run_git ('checkout' , '-b' , new_branch_name , ORIGIN + '/' + MAIN_BRANCH )
213+
214+ print ('Updating changelog' )
215+ update_changelog (version )
216+
217+ # Create a commit that updates the CHANGELOG
218+ run_git ('add' , 'CHANGELOG.md' )
219+ run_git ('commit' , '-m' , version )
220+
172221 run_git ('push' , ORIGIN , new_branch_name )
173222
174223 # Open a PR to update the branch
0 commit comments