-
Notifications
You must be signed in to change notification settings - Fork 66
/
changelog.js
84 lines (74 loc) · 2.2 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
const { spawn } = require('child_process');
var packageJson = require('./package.json');
var fs = require('fs');
const workspace = process.env.GITHUB_WORKSPACE;
const date = new Date();
const file = './CHANGELOG.md';
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
const [latestPr] = data
.split('/pull/')
.map((el) => parseInt(el.split(')\n')[0]))
.filter((el) => el)
.sort()
.reverse();
const replacement = `## [Unreleased]
## [[${packageJson.version}](https://github.com/multiversx/mx-sdk-dapp/pull/${
latestPr + 1
})] - ${date.toISOString().split('T')[0]}
`;
var result = data.replace('## [Unreleased]', replacement);
fs.writeFileSync(file, result, 'utf8', function (err) {
if (err) {
return console.log(err);
}
});
pushChanges();
});
const pushChanges = async () => {
await runInWorkspace('git', [
'config',
'user.name',
`"${process.env.GITHUB_USER || 'Automated Changelog'}"`
]);
await runInWorkspace('git', [
'config',
'user.email',
`"${
process.env.GITHUB_EMAIL ||
}"`
]);
const remoteRepo = `https://${process.env.GITHUB_ACTOR}:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git`;
await runInWorkspace('git', ['commit', '-a', '-m', 'update changelog']);
await runInWorkspace('git', ['push', remoteRepo, '--follow-tags']);
await runInWorkspace('git', ['push', remoteRepo, '--tags']);
};
function runInWorkspace(command, args) {
return new Promise((resolve, reject) => {
console.log('runInWorkspace | command:', command, 'args:', args);
const child = spawn(command, args, { cwd: workspace });
let isDone = false;
const errorMessages = [];
child.on('error', (error) => {
if (!isDone) {
isDone = true;
reject(error);
}
});
child.stderr.on('data', (chunk) => errorMessages.push(chunk));
child.on('exit', (code) => {
if (!isDone) {
if (code === 0) {
resolve();
} else {
reject(
`${errorMessages.join('')}${EOL}${command} exited with code ${code}`
);
}
}
});
});
}