4
4
import requests
5
5
import subprocess
6
6
import 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
+ """
7
17
8
18
# The branch being merged from.
9
19
# 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):
49
59
commits_without_pull_requests = sorted (commits_without_pull_requests , key = lambda c : c .commit .author .date )
50
60
51
61
# 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 )
53
64
54
65
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 )
56
68
57
69
# List all PRs merged
58
70
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:' )
60
73
for pr in pull_requests :
61
74
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 + ')' )
65
76
66
77
# List all commits not part of a PR
67
78
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:' )
69
81
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.' )
73
91
74
92
title = 'Merge ' + MAIN_BRANCH + ' into ' + LATEST_RELEASE_BRANCH
75
93
76
94
# 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 )
78
96
print ('Created PR #' + str (pr .number ))
79
97
80
98
# Assign the conductor
@@ -95,7 +113,7 @@ def get_conductor(repo, pull_requests, other_commits):
95
113
# This will not include any commits that exist on the release branch
96
114
# that aren't on main.
97
115
def 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 ' )
99
117
100
118
# Convert to full-fledged commit objects
101
119
commits = [repo .get_commit (c ) for c in commits ]
@@ -135,17 +153,40 @@ def get_pr_for_commit(repo, commit):
135
153
def get_merger_of_pr (repo , pr ):
136
154
return repo .get_commit (pr .merge_commit_sha ).author .login
137
155
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
+
138
178
def main ():
139
179
if len (sys .argv ) != 3 :
140
180
raise Exception ('Usage: update-release.branch.py <github token> <repository nwo>' )
141
181
github_token = sys .argv [1 ]
142
182
repository_nwo = sys .argv [2 ]
143
183
144
184
repo = Github (github_token ).get_repo (repository_nwo )
185
+ version = get_current_version ()
145
186
146
187
# Print what we intend to go
147
188
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 ()
149
190
print ('Current head of ' + MAIN_BRANCH + ' is ' + short_main_sha )
150
191
151
192
# See if there are any commits to merge in
@@ -157,7 +198,7 @@ def main():
157
198
# The branch name is based off of the name of branch being merged into
158
199
# and the SHA of the branch being merged from. Thus if the branch already
159
200
# 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
161
202
print ('Branch name is ' + new_branch_name )
162
203
163
204
# Check if the branch already exists. If so we can abort as this script
@@ -168,7 +209,15 @@ def main():
168
209
169
210
# Create the new branch and push it to the remote
170
211
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
+
172
221
run_git ('push' , ORIGIN , new_branch_name )
173
222
174
223
# Open a PR to update the branch
0 commit comments