forked from civ-clone/electron-renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-changelog.mjs
216 lines (174 loc) · 6.81 KB
/
generate-changelog.mjs
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
import { exec } from 'child_process';
import { promisify } from 'util';
const [, , targetCommit] = process.argv,
execPromise = promisify(exec),
getShortHash = async () => {
const { stdout } = await execPromise(`git log --format=%h -n 1 ${targetCommit ?? ''}`);
return stdout.trim();
},
getLocalChanges = async () => {
const { stdout } = await execPromise(`git log --format=%B -n 1 ${targetCommit ?? ''}`);
return stdout.trim().split(/\n/).map((line) => line.trim()).filter((line) => line !== null);
},
getCommitDate = async () => {
const { stdout } = await execPromise(`git log --format=%aI -n 1 ${targetCommit ?? ''}`);
return new Date(stdout.trim());
},
getExternalChanges = async () => {
const { stdout } = await execPromise(`git show ${targetCommit ?? ''} yarn.lock`);
return stdout.trim().split(/\n/)
.reduce((modules, line) => {
if (!/^[-+]/.test(line) || !/resolved|resolution/.test(line) || !/civ-clone/.test(line)) {
return modules;
}
const [, status] = line.match(/^([-+])/),
[, module] = line.match(/civ-clone\/([a-z\d-]+)/i),
[, hash] = line.match(/\b([0-9a-f]{40})\b/i) || [],
[, version] = line.match(/\b(\d+\.\d+\.\d+)\b/) || [];
if (!module) {
console.warn('no module??: ' + line);
return modules;
}
if (!(module in modules)) {
modules[module] = {};
}
if (status === '-') {
if (version) {
modules[module].fromVersion = version;
}
else {
modules[module].fromHash = hash;
}
return modules;
}
if (version) {
modules[module].toVersion = version;
}
else {
modules[module].toHash = hash;
}
return modules;
}, {})
},
githubAuthenticatedRequest = (path) => {
const githubToken = process.env.GITHUB_TOKEN;
if (!githubToken) {
console.error('GITHUB_TOKEN not defined in environment');
process.exit(1);
}
return fetch(
`https://api.github.com${path}`,
{
headers: {
'Authorization': `Bearer ${githubToken}`
}
}
);
},
getExternalLog = async (repo, to, from) => {
console.warn(`Getting logs for civ-clone/${repo}${from ? ` from '${from}'` : ''} to '${to}'...`);
const response = await githubAuthenticatedRequest(
`/repos/civ-clone/${repo}/commits?per_page=100`
);
if (!response.ok) {
console.warn(response.url);
console.warn(response);
return [];
}
const data = await response.json(),
logs = [];
let shouldCapture = false;
while (data.length) {
const current = data.pop();
if (from === null) {
shouldCapture = true;
}
if (shouldCapture) {
logs.push(...current.commit.message.trim().split(/\n/));
}
if (current.sha === from) {
shouldCapture = true;
}
if (current.sha === to) {
shouldCapture = false;
}
}
return logs.filter((entry) => entry.trim() !== '');
},
getTagHash = async (repo, tag) => {
console.warn(`Getting tag ${tag} for civ-clone/${repo}...`);
const baseTagResponse = await githubAuthenticatedRequest(
`/repos/civ-clone/${repo}/git/refs/tags/${tag}`
);
if (!baseTagResponse.ok) {
throw new Error(`Unable to get tag '${tag}' for civ-clone/${repo}: ${baseTagResponse.url} (${await baseTagResponse.text()})`);
}
const { object: { sha: tagHash, type }} = await baseTagResponse.json();
if (type === 'commit') {
return tagHash;
}
const tagResponse = await githubAuthenticatedRequest(
`/repos/civ-clone/${repo}/git/tags/${tagHash}`
);
if (!tagResponse.ok) {
throw new Error(`Unable to get tag '${tag}' for civ-clone/${repo}: ${tagResponse.url} (${await tagResponse.text()})`);
}
const { object: { sha: commitHash }} = await tagResponse.json();
return commitHash;
},
changeLogEntry = {
// TODO: determine the version number from package.json, once this changes
version: '0.1.0@' + await getShortHash(),
date: (await getCommitDate()).toISOString(),
localChanges: await getLocalChanges(),
externalChanges: await Object.entries(await getExternalChanges())
.reduce((modules, [module, { fromHash = null, toHash, fromVersion = null, toVersion }]) =>
console.warn([fromVersion, toVersion]) ||
modules.then(async (modules) => {
if (fromVersion && toVersion && fromVersion === toVersion) {
return modules;
}
if (fromVersion) {
fromHash = await getTagHash(module, fromVersion);
}
if (toVersion) {
toHash = await getTagHash(module, toVersion);
}
if (fromHash === toHash) {
return modules;
}
if (!toHash) {
modules[module] = {
status: 'removed',
};
return modules;
}
modules[module] = {
status: (fromHash === null ? 'added' : 'updated'),
log: await getExternalLog(module, toHash, fromHash ?? null)
};
return modules;
})
, Promise.resolve({}))
};
console.log(JSON.stringify(changeLogEntry));
// console.log(
// `
// # civ-clone/web-renderer ${changeLogEntry.release}
//
// ${changeLogEntry.localChanges.map((line) => `- ${line}`).join('\n')}
//
// ## External project changes
//
// ${Object.entries(changeLogEntry.externalChanges)
// .map(([module, logs]) =>
// `<details><summary> ${module}</summary>
//
// ${logs.map((line) => `- ${line}`).join('\n')}
// </details>
// `
// ).join('\n\n')}
//
// <p><small>Generated ${new Date().toISOString()}</small></p>
// `
// );