-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathchangelog.js
executable file
·184 lines (153 loc) · 5.39 KB
/
changelog.js
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
#!/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 RSVP = require('rsvp');
const GitHubApi = require('github');
const execSync = require('child_process').execSync;
const github = new GitHubApi({ version: '3.0.0' });
if (process.env.GITHUB_TOKEN) {
github.authenticate({
type: 'token',
token: process.env.GITHUB_TOKEN,
});
}
const compareCommits = RSVP.denodeify(github.repos.compareCommits);
const getPullRequest = RSVP.denodeify(github.pullRequests.get);
const currentVersion = process.env.PRIOR_VERSION;
const head = process.env.HEAD || execSync('git rev-parse HEAD', { encoding: 'UTF-8' });
generateChangelog()
.then(console.log)
.catch((err) => console.error(err));
async function fetchAllChanges() {
let result = await compareCommits({
user: 'emberjs',
repo: 'ember.js',
base: currentVersion,
head,
});
let data = result.commits;
while (github.hasNextPage(result)) {
result = await github.getNextPage(result);
data.concat(result.commits);
}
return data;
}
async function generateChangelog() {
let commits = await fetchAllChanges();
let contributions = commits.filter(excludeDependabot).filter(isMergeOrCherryPick);
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];
}
return result;
})
);
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) {
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' }
);
} catch {
// 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]) {
let pullRequest = await getPullRequest({
user: 'emberjs',
repo: 'ember.js',
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;
}