forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease_notes.ts
More file actions
executable file
·243 lines (212 loc) · 8.49 KB
/
release_notes.ts
File metadata and controls
executable file
·243 lines (212 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env node
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/**
* Generates a draft release notes markdown file for a release. This script
* takes a start version of the union package, and optionally an end version.
* It then finds the matching versions for all the dependency packages, and
* finds all commit messages between those versions.
*
* The release notes are grouped by repository, and then bucketed by a set of
* tags which committers can use to organize commits into sections. See
* DEVELOPMENT.md for more details on the available tags.
*
* This script will ask for your github token which is used to make requests
* to the github API for usernames for commits as this is not stored in git
* logs. You can generate a token for your account here;
* https://github.com/settings/tokens
*
* Usage:
* # Release notes for all commits after tfjs union version 0.9.0.
* yarn release-notes --startVersion 0.9.0 --out ./draft_notes.md
*
* # Release notes for all commits after version 0.9.0 up to and including
* # version 0.10.0.
* yarn release-notes --startVersion 0.9.0 --endVersion 0.10.3 \
* --out ./draft_notes.md
*/
import * as commander from 'commander';
import * as mkdirp from 'mkdirp';
import * as readline from 'readline';
import * as fs from 'fs';
import * as util from './util';
import {$, Commit, Repo, RepoCommits} from './util';
// tslint:disable-next-line:no-require-imports
const octokit = require('@octokit/rest')();
const OUT_FILE = 'release-notes.md';
const TMP_DIR = '/tmp/tfjs-release-notes';
const UNION_DEPENDENCIES: Repo[] = [
{name: 'Core', identifier: 'tfjs-core'},
{name: 'Data', identifier: 'tfjs-data'},
{name: 'Layers', identifier: 'tfjs-layers'},
{name: 'Converter', identifier: 'tfjs-converter'}
];
const NODE_REPO: Repo = {
name: 'Node',
identifier: 'tfjs-node'
};
async function askUserForVersions(validVersions: string[], packageName: string):
Promise<{startVersion: string, endVersion: string}> {
const YELLOW_TERMINAL_COLOR = '\x1b[33m%s\x1b[0m';
const RED_TERMINAL_COLOR = '\x1b[31m%s\x1b[0m';
console.log(YELLOW_TERMINAL_COLOR, packageName + ' versions');
console.log(validVersions.join(', '));
const startVersion = await util.question(`Enter the union start version: `);
if (validVersions.indexOf(startVersion) === -1) {
console.log(RED_TERMINAL_COLOR, `Unknown start version: ${startVersion}`);
process.exit(1);
}
const defaultVersion = validVersions[validVersions.length - 1];
let endVersion = await util.question(
`Enter the union end version (leave empty for ${defaultVersion}): `);
if (endVersion === '') {
endVersion = defaultVersion;
}
if (validVersions.indexOf(endVersion) === -1) {
console.log(RED_TERMINAL_COLOR, `Unknown end version: ${endVersion}`);
process.exit(1);
}
return {startVersion, endVersion};
}
function getTaggedVersions(packageName: string) {
const versions =
$(`git tag`)
.split('\n')
.filter(x => new RegExp('^' + packageName + '-v([0-9])').test(x))
.map(x => x.substring((packageName + '-v').length));
return versions;
}
function getTagName(packageName: string, version: string) {
return packageName + '-v' + version;
}
async function main() {
mkdirp(TMP_DIR, (err) => {
if (err) {
console.log('Error creating temp dir', TMP_DIR);
process.exit(1);
}
});
// Remove anything that exists already in the tmp dir.
$(`rm -f -r ${TMP_DIR}/*`);
// Get union start version and end version.
const versions = getTaggedVersions('tfjs');
const {startVersion, endVersion} = await askUserForVersions(versions, 'tfjs');
// Clone the Node.js repo eagerly so we can query the tags.
const validNodeVersions = getTaggedVersions('tfjs-node');
const nodeVersions =
await askUserForVersions(validNodeVersions, NODE_REPO.identifier);
NODE_REPO.startVersion = nodeVersions.startVersion;
NODE_REPO.endVersion = nodeVersions.endVersion;
NODE_REPO.startCommit = $(`git rev-list -n 1 ${
getTagName(NODE_REPO.identifier, NODE_REPO.startVersion)}`);
// Get all the commits of the union package between the versions.
const unionCommits =
$(`git log --pretty=format:"%H" ` +
`${getTagName('tfjs', startVersion)}..` +
`${getTagName('tfjs', endVersion)}`);
const commitLines = unionCommits.trim().split('\n');
// Read the union package.json from the earliest commit so we can find the
// dependencies.
const earliestCommit = commitLines[commitLines.length - 1];
const earliestUnionPackageJson =
JSON.parse($(`git show ${earliestCommit}:tfjs/package.json`));
const latestCommit = commitLines[0];
const latestUnionPackageJson =
JSON.parse($(`git show ${latestCommit}:tfjs/package.json`));
// Populate start and end for each of the union dependencies.
UNION_DEPENDENCIES.forEach(repo => {
// Find the version of the dependency from the package.json from the
// earliest union tag.
const npm = '@tensorflow/' + repo.identifier;
const repoStartVersion = earliestUnionPackageJson.dependencies[npm];
const repoEndVersion = latestUnionPackageJson.dependencies[npm];
const dir = `${repo.name}`;
repo.startCommit =
$(repoStartVersion != null ?
`git rev-list -n 1 ` +
getTagName(repo.identifier, repoStartVersion) :
// Get the first commit if there are no tags yet.
`git rev-list --max-parents=0 HEAD`);
repo.startVersion = repoStartVersion != null ? repoStartVersion : null;
repo.endVersion = repoEndVersion;
});
const repoCommits: RepoCommits[] = [];
// Clone all of the dependencies into the tmp directory.
[...UNION_DEPENDENCIES, NODE_REPO].forEach(repo => {
console.log(
`${repo.name}: ${repo.startVersion}` +
` =====> ${repo.endVersion}`);
console.log('Querying commits...');
// Get subjects, bodies, emails, etc from commit metadata.
const commitFieldQueries = ['%s', '%b', '%aE', '%H'];
const commitFields = commitFieldQueries.map(query => {
// Use a unique delimiter so we can split the log.
const uniqueDelimiter = '--^^&&';
const versionQuery = repo.startVersion != null ?
`${getTagName(repo.identifier, repo.startVersion)}..` +
`${getTagName(repo.identifier, repo.endVersion)}` :
`#${repo.startCommit}..${
getTagName(repo.identifier, repo.endVersion)}`;
return $(`git log --pretty=format:"${query}${uniqueDelimiter}" ` +
`${versionQuery}`)
.trim()
.split(uniqueDelimiter)
.slice(0, -1)
.map(str => str.trim());
});
const commits: Commit[] = [];
for (let i = 0; i < commitFields[0].length; i++) {
// Make sure the files touched contain the repo directory.
const filesTouched =
$(`git show --pretty="format:" --name-only ${commitFields[3][i]}`)
.split('\n');
let touchedDir = false;
for (let j = 0; j < filesTouched.length; j++) {
if (filesTouched[j].startsWith(repo.identifier)) {
touchedDir = true;
break;
}
}
if (!touchedDir) {
continue;
}
commits.push({
subject: commitFields[0][i],
body: commitFields[1][i],
authorEmail: commitFields[2][i],
sha: commitFields[3][i]
});
}
repoCommits.push({
repo,
startVersion: repo.startVersion,
endVersion: repo.endVersion,
startCommit: repo.startCommit,
commits
});
});
// Ask for github token.
const token = await util.question(
'Enter GitHub token (https://github.com/settings/tokens): ');
octokit.authenticate({type: 'token', token});
const notes = await util.getReleaseNotesDraft(octokit, repoCommits);
fs.writeFileSync(OUT_FILE, notes);
console.log('Done writing notes to', OUT_FILE);
// So the script doesn't just hang.
process.exit(0);
}
main();