-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
action.js
222 lines (182 loc) · 7.23 KB
/
action.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
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
217
218
219
220
221
222
require('child_process')
.execSync(
'npm install @actions/core @actions/github conventional-changelog-cli mime@3.0.0',
{ cwd: __dirname }
);
const fs = require('fs');
const path = require('path');
const mime = require('mime');
const core = require('@actions/core');
const github = require('@actions/github');
(async () => {
try {
const api = github.getOctokit(core.getInput('token'));
const tag = core.getInput('tag');
const name = core.getInput('name');
const commit = core.getInput('commit');
const body = core.getInput('body');
const verbose = core.getInput('verbose') == 'true'; // input is always string, not boolean.
const draft = core.getInput('draft') == 'true';
const prerelease = core.getInput('prerelease') == 'true';
let files = null;
if (core.getInput('folder'))
{
const folder = core.getInput('folder');
log('Reading files in folder:' + folder);
files = fs.readdirSync(folder, {withFileTypes: true})
.filter(item => !item.isDirectory())
.map(item => path.join(folder, item.name))
log('Found files: ', files);
}
else
{
files = core.getInput('files').split(';');
}
let release = null;
let created = false; // Indicate if the release was created, or merely updated.
function info(text, ...params) {
log(text, true, ...params);
}
function debug(text, ...params) {
log(text, verbose, ...params);
}
function log(text, enabled, ...params) {
if (enabled) {
console.log(text, ...params);
}
}
function getFile(filePath) {
log('getFile: ' + filePath);
return {
name: path.basename(filePath),
mime: mime.getType(filePath) || 'application/octet-stream',
size: fs.lstatSync(filePath).size,
file: fs.readFileSync(filePath)
}
}
info(`🎄 <- That is when I wrote this code.`);
// First let us try to get the release.
try {
result = await api.repos.getReleaseByTag({
...github.context.repo,
tag: tag
});
debug(`Release already exists. Do the 🐹 dance.`, result);
// If this has been published, we'll create a new release.
if (draft && !result.data.draft) {
release = null;
debug(`The existing release was not draft. We can create a brand ✨ new release.`);
}
else {
// We cannot update assets on existing releases, so until a future update, we'll ignore updating releases that are published.
info(`Draft parameter is set to false and there is an existing release. Skipping any updates to release 🛑.`);
return;
}
}
catch (error) {
if (error.name != 'HttpError' || error.status != 404) {
throw error;
}
}
// Get releases if the first release get was not satisfactory.
if (!release) {
try {
var releases = await api.repos.listReleases({
...github.context.repo
});
debug('Releases', releases);
for (var i = 0; i < releases.data.length; ++i) {
var r = releases.data[i];
if (r.tag_name == tag && r.draft == draft && r.prerelease == prerelease) {
release = r;
debug('Found existing release based on searching.');
break;
}
}
}
catch (error) {
if (error.name != 'HttpError' || error.status != 404) {
throw error;
}
}
}
// Define the options, these are almost same when creating new and updating existing.
var releaseOptions = {
...github.context.repo,
tag_name: tag,
target_commitish: commit,
name,
body,
prerelease: prerelease,
draft: draft
};
// Create a release if it doesn't already exists.
if (!release) {
debug('Release Options (Create)', releaseOptions);
info(`🌻 Creating GitHub release for tag "${tag}".`);
const result = await api.repos.createRelease(releaseOptions);
release = result.data;
created = true;
}
else {
releaseOptions.release_id = release.id; // Must be part of the parameters.
debug('Release Options (Update)', releaseOptions);
info(`Found The 🦞. Updating GitHub release for tag "${tag}".`);
const result = await api.repos.updateRelease(releaseOptions);
release = result.data;
}
async function upload() {
var file = files.pop();
if (!file) {
return;
}
// var fileInfo = getFile(file);
const fileName = path.basename(file);
// If not a new release, we must delete the existing one.
if (!created && release.assets) {
const asset = release.assets.find(a => a.name === fileName);
// If the asset already exists, make sure we delete it first.
if (asset) {
var assetOptions = {
...github.context.repo,
asset_id: asset.id
};
info(`Asset "${fileName}" already exists, it must be put in a 🕳️.`);
debug('Asset Options (for delete operation)', assetOptions);
try {
const result = await api.repos.deleteReleaseAsset(assetOptions);
debug('Result from delete', result);
}
catch (err) {
console.error(`⚠️ Failed to delete file "${fileName}"`, err);
}
}
}
info(`🚧 Uploading ${fileName}.`);
try {
const result = await api.repos.uploadReleaseAsset({
url: release.upload_url,
// headers: {
// ['content-type']: fileInfo.mime,
// ['content-length']: fileInfo.size
// },
// name: fileInfo.name,
name: fileName,
data: fs.readFileSync(file)
});
debug('Result from upload', result);
}
catch (error) {
console.error(`⚠️ Failed to upload file`, error);
}
// Recursive go through all files to upload as release assets.
await upload();
}
// Start uploading all specified files.
await upload();
info('All is norminal 🚀. Execution has ended.')
} catch (error) {
console.error(error);
core.setFailed(error.message);
}
})();