-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (131 loc) · 5.23 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
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
const fetch = require('node-fetch').default;
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs');
const { Octokit } = require("@octokit/rest");
const path = require('path');
async function parse_options(file_path = process.env.GITHUB_WORKSPACE || "./") {
let changelog_format = core.getInput('changelog_format') || 'keepachangelog';
let toggle = (changelog_format === 'keepachangelog');
const options = {
changelog: core.getInput('changelog') || 'CHANGELOG.md',
filtered_changelog: core.getInput('filtered_changelog') || 'FILTERED_CHANGELOG.md',
changelog_format: core.getInput('changelog_format') || 'keepachangelog',
start_token: core.getInput('start_token') || (toggle ? '## [' : '#### [v'),
end_token: core.getInput('end_token') || (toggle ? '## [' : '#### [v'),
specific_tag: core.getInput('specific_tag') || '',
upcoming_release: core.getInput('upcoming_release') || 'false',
create_release: core.getInput('create_release') || 'true',
update_release: core.getInput('update_release') || 'true',
skip_n_lines: parseInt(core.getInput('skip_n_lines')) || (toggle ? 0 : 2)
};
options.changelog_path = path.join(file_path, options.changelog);
options.filtered_changelog_path = path.join(file_path, options.filtered_changelog);
console.log(options);
return options;
}
async function parse_changelog(options) {
// Change start_token accordingly
if (options.upcoming_release === 'true') {
if (options.start_token === '#### [v') {
options.start_token = '#### ['
}
else if (options.changelog_format === 'keepachangelog') {
options.start_token = '## [Unreleased]'
}
}
// Parse the changelog
let filtered_lines = [];
let started_processing = false;
let index = 0;
//let extracted_version_tag;
const lines = fs.readFileSync(options.changelog_path, 'utf-8').split('\n');
for(let line of lines) {
index += 1;
if(line.startsWith(options.end_token) && started_processing) {
break;
} else if (line.startsWith(options.start_token)) {
//this part extracts the version tag (optional)
//const version_tag_line = line.match(/v[\d\.]+/);
//extracted_version_tag = version_tag_line[0];
started_processing = true;
index = 0;
continue;
}
if(started_processing && index > options.skip_n_lines) {
filtered_lines.push(line);
}
}
let release_notes = filtered_lines.join('\n');
return release_notes;
}
async function write_filtered_changelog(release_notes, options) {
fs.writeFileSync(options.filtered_changelog_path, release_notes);
}
async function read_filtered_changelog(options) {
const releaseBody = fs.readFileSync(options.filtered_changelog_path, 'utf-8');
//console.log(releaseBody);
return releaseBody
}
async function create_release(release_notes, options) {
const token = process.env.GITHUB_TOKEN;
//const octokit = new Octokit({ auth: token });
const octokit = new Octokit({ auth: token, request: {fetch: fetch, } });
// Create a new release
const { owner, repo } = github.context.repo;
// get the release tag from the refs
const ref = github.context.ref;
const refParts = ref.split('/');
let version_tag = options.specific_tag || refParts[refParts.length - 1];
//console.log(version_tag);
//console.log(github.context.ref);
// Fetch all releases
const releases = await octokit.rest.repos.listReleases({ owner, repo });
// Check if your release exists
let release = releases.data.find(r => r.tag_name === version_tag);
// If release exists, update it
if (release && options.update_release === 'true') {
await octokit.rest.repos.updateRelease({
owner,
repo,
release_id: release.id,
body: release_notes
});
// If release does not exist, create it
} else if (!release && options.create_release === 'true') {
await octokit.rest.repos.createRelease({
owner,
repo,
tag_name: version_tag,
name: `Release ${version_tag}`,
body: release_notes,
draft: false,
prerelease: options.upcoming_release === 'true'
});
}
return true;
}
async function main() {
try {
let options = await parse_options();
let release_notes = await parse_changelog(options);
console.log(release_notes);
write_filtered_changelog(release_notes, options);
//release_notes = await read_filtered_changelog(options);
await create_release(release_notes, options);
core.setOutput("releasenotes", release_notes);
} catch (error) {
core.setFailed(error.message);
}
}
// run the program
main();
// module exports for jest tests
module.exports = {
parse_options,
parse_changelog,
write_filtered_changelog,
read_filtered_changelog,
create_release,
main
};