-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathchangelog.cjs
More file actions
executable file
·212 lines (176 loc) · 6.64 KB
/
Copy pathchangelog.cjs
File metadata and controls
executable file
·212 lines (176 loc) · 6.64 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
#!/usr/bin/env node
/* eslint-disable no-console */
'use strict';
/*
* This script generates the template a changelog by comparing a current version
* with main. Run this, copy what's logged into the `CHANGELOG.md` and update
* the top section based on the changes listed in "Community Contributions"
*
* Usage:
*
* bin/changelog.js
*/
const { Octokit } = require('@octokit/rest');
const execSync = require('child_process').execSync;
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});
const DEBUG = process.env.DEBUG === 'true';
const debug = (...args) => DEBUG && console.error('[DEBUG]', ...args);
const currentVersion = process.env.PRIOR_VERSION;
const head = (process.env.HEAD || execSync('git rev-parse HEAD', { encoding: 'UTF-8' })).trim();
debug('Current version:', currentVersion);
debug('Head:', head);
generateChangelog()
.then(console.log)
.catch((err) => console.error(err));
async function fetchAllChanges() {
debug('Fetching comparison:', `${currentVersion}...${head}`);
const comparison = await octokit.repos.compareCommitsWithBasehead({
owner: 'emberjs',
repo: 'ember.js',
basehead: `${currentVersion}...${head}`,
per_page: 100,
});
let commits = comparison.data.commits;
const totalCommits = comparison.data.total_commits;
debug('Total commits:', totalCommits, 'Fetched:', commits.length);
// If there are more commits than returned, fetch them page by page
if (totalCommits > commits.length) {
let page = 2;
while (commits.length < totalCommits) {
debug('Fetching page:', page);
const nextPage = await octokit.repos.compareCommitsWithBasehead({
owner: 'emberjs',
repo: 'ember.js',
basehead: `${currentVersion}...${head}`,
per_page: 100,
page: page,
});
commits = commits.concat(nextPage.data.commits);
debug('Now have', commits.length, 'commits');
page++;
// Safety check to prevent infinite loops
if (nextPage.data.commits.length === 0) break;
}
}
return commits;
}
async function generateChangelog() {
let commits = await fetchAllChanges();
debug('Processing', commits.length, 'commits');
let contributions = commits.filter(excludeDependabot).filter(isMergeOrCherryPick);
debug('Found', contributions.length, 'contributions after filtering');
let changes = await Promise.all(
contributions.map(async function (commitInfo) {
let message = await getCommitMessage(commitInfo);
let mergeFromBranchRegex = /#(\d+) from (.+)\//;
let mergePullRequestRegex = /Merge pull request #(\d+)/;
let mergeWithPrReferenceRegex = /\(#(\d+)\)$/m;
let result = {
sha: commitInfo.sha,
};
if (mergeFromBranchRegex.test(message)) {
let match = message.match(mergeFromBranchRegex);
result.number = match[1];
result.title = message.split('\n\n')[1];
} else if (mergePullRequestRegex.test(message)) {
let match = message.match(mergePullRequestRegex);
result.number = match[1];
result.title = message.split('\n\n')[1];
} else if (mergeWithPrReferenceRegex.test(message)) {
let match = message.match(mergeWithPrReferenceRegex);
result.number = match[1];
result.title = message.split('\n')[0];
} else {
result.title = message.split('\n\n')[0];
}
debug('Processed commit:', result.number || result.sha.slice(0, 8), '-', result.title);
return result;
})
);
debug('Sorting and deduplicating changes');
return changes
.sort(comparePrNumber)
.filter(uniqueByPrNumber)
.map((pr) => {
let title = pr.title;
let link;
if (pr.number) {
link =
'[#' + pr.number + ']' + '(https://github.com/emberjs/ember.js/pull/' + pr.number + ')';
} else {
link =
'[' + pr.sha.slice(0, 8) + '](https://github.com/emberjs/ember.js/commit/' + pr.sha + ')';
}
return '- ' + link + ' ' + title;
})
.join('\n');
}
async function getCommitMessage(commitInfo) {
let message = commitInfo.commit.message;
let matches;
if (message.indexOf('cherry picked from commit') > -1) {
debug('Processing cherry-pick commit:', commitInfo.sha.slice(0, 8));
let cherryPickRegex = /cherry picked from commit ([a-z0-9]+)/;
let originalCommit = cherryPickRegex.exec(message)[1];
try {
// command from http://stackoverflow.com/questions/8475448/find-merge-commit-which-include-a-specific-commit
message = execSync(
'commit=$((git rev-list ' +
originalCommit +
'..origin/main --ancestry-path | cat -n; git rev-list ' +
originalCommit +
'..origin/main --first-parent | cat -n) | sort -k2 | uniq -f1 -d | sort -n | tail -1 | cut -f2) && git show --format="%s\n\n%b" $commit',
{ encoding: 'utf8' }
);
debug('Found original merge commit for cherry-pick');
} catch {
debug('Could not find original merge commit for cherry-pick');
// ignored
}
}
if ((matches = message.match(/^Merge pull request #(\d+)/))) {
// if the commit was a merge from a PR and there's no additional content in
// the commit message (which is normally the title of the merged PR) then
// hit the Github API for the PR to get the title.
let prNumber = matches[1];
let lines = message.split(/\n\n/);
if (!lines[1]) {
debug('Fetching PR title for #' + prNumber);
let { data: pullRequest } = await octokit.pulls.get({
owner: 'emberjs',
repo: 'ember.js',
pull_number: prNumber,
});
return `Merge pull request #${prNumber}\n\n${pullRequest.title}`;
}
}
return message;
}
function excludeDependabot(commitInfo) {
let author = commitInfo.author && commitInfo.author.login;
return author !== 'dependabot-preview[bot]' && author !== 'dependabot[bot]';
}
function isMergeOrCherryPick(commitInfo) {
if (commitInfo.parents.length == 2) return true;
let message = commitInfo.commit.message;
return message.indexOf('Merge pull request #') > -1 || message.indexOf('cherry picked from') > -1;
}
function comparePrNumber(a, b) {
if (a.number && !b.number) return -1;
if (!a.number && b.number) return 1;
if (!a.number && !b.number) {
if (a.sha < b.sha) return -1;
if (a.sha > b.sha) return 1;
return 0;
}
if (a.number < b.number) return -1;
if (a.number > b.number) return 1;
return 0;
}
function uniqueByPrNumber(commitInfo, index, commits) {
const foundIndex = commits.findIndex(({ number }) => number === commitInfo.number);
// keep this item if it has a `number` that isn't elsewhere in the array, or doesn't have `number`
return foundIndex === index || foundIndex === -1;
}