-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
137 lines (114 loc) · 3 KB
/
index.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
const core = require('@actions/core');
const fs = require('fs');
const { GitHub } = require('@actions/github');
const NEWLINE = ' \n';
async function run() {
const path = core.getInput('changelog'),
token = core.getInput('token'),
usePr = core.getInput('use_pullrequest'),
defaultBranch = core.getInput('default_branch'),
ownerRepo = process.env.GITHUB_REPOSITORY,
eventPath = process.env.GITHUB_EVENT_PATH;
const [owner, repo] = ownerRepo.split('/');
if (!token) {
core.error('Must provide valid `token` parameter.');
}
const octokit = new GitHub(token);
let sha;
let currentContents = '';
let changelogExists = true;
try {
let { data } = await octokit.repos.getContents({
owner,
repo,
path
});
if (data.type !== 'file') {
core.error(`Your specified changelog ${path}, is not a file`);
}
if (data.sha) {
sha = data.sha;
}
let contentBuffer = Buffer.from(data.content, 'base64');
currentContents = `${contentBuffer.toString('utf8')}`;
} catch (e) {
changelogExists = false;
}
let { url, tag, name, body, releaseBranch, repoDefaultBranch } = getReleaseData(
eventPath
);
let branch = defaultBranch || releaseBranch;
let newContents = `### [${name}](${url}):${NEWLINE} ${body} ${NEWLINE}${currentContents}`;
let buff = new Buffer.from(newContents);
let content = buff.toString('base64');
let options = {
owner,
repo,
path,
branch,
message: `Updated ${path} via Next Release action.`,
content
};
if (changelogExists) {
options.sha = sha;
}
let prBranch = core.getInput('branch_name') || `changelog-${tag}`;
if (!prBranch) {
prBranch = repoDefaultBranch;
}
if (usePr) {
let refSha;
try {
let { data } = await octokit.git.getRef({
owner,
repo,
ref: `heads/${branch}`
});
refSha = data.object.sha;
} catch (e) {
core.error(`Error creating changelog PR ${e}`);
return;
}
try {
await octokit.git.createRef({
owner,
repo,
sha: refSha,
ref: `refs/heads/${prBranch}`
});
options.branch = prBranch;
} catch (e) {
core.error(`Failed creating changelog PR: ${e}`);
return;
}
}
try {
await octokit.repos.createOrUpdateFile(options);
} catch (e) {
core.error(`Failed updating Changelog: ${e}`);
}
if (usePr) {
await octokit.pulls.create({
owner,
repo,
head: prBranch,
base: branch,
title: `Update ${path}`,
maintainer_can_modify: true,
body: 'Automatically generated by Next Release Changelog Action'
});
}
}
function getReleaseData(eventPath) {
const event = JSON.parse(fs.readFileSync(eventPath, 'utf8'));
let {
html_url: url,
tag_name: tag,
name,
body,
target_commitish: releaseBranch
} = event.release;
let { default_branch: repoDefaultBranch } = event.repository;
return { url, tag, name, body, releaseBranch, repoDefaultBranch };
}
run();