forked from majkrzak/create-release
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.js
104 lines (94 loc) · 2.36 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
require("child_process").execSync(
"npm install @actions/[email protected] @actions/[email protected]",
{
cwd: __dirname,
}
);
const fs = require("fs");
const core = require("@actions/core");
const github = require("@actions/github");
const api = new github.GitHub(core.getInput("token"));
const main = async () => {
const name = core.getInput("name");
const code = core.getInput("code");
const body = core.getInput("body");
const prerelease = core.getInput("prerelease") == "true";
const recreate = core.getInput("recreate") == "true";
const assets = core
.getInput("assets")
.split(" ")
.map((asset) => asset.split(":"));
if (recreate) {
await deleteReleaseIfExists(code);
}
const release = await api.repos.createRelease({
...github.context.repo,
tag_name: code,
target_commitish: github.context.sha,
name,
body,
draft: false,
prerelease: prerelease,
});
for (const [source, target, type] of assets) {
const data = fs.readFileSync(source);
api.repos.uploadReleaseAsset({
url: release.data.upload_url,
headers: {
["content-type"]: type,
["content-length"]: data.length,
},
name: target,
file: data,
});
}
};
async function deleteReleaseIfExists(code) {
let release;
try {
release = await api.repos.getReleaseByTag({
...github.context.repo,
tag: code,
});
} catch (err) {
console.log(err);
console.log("Release not found.. moving to creation");
}
if (!release) {
return;
}
const deleteRelease = async () =>
api.repos.deleteRelease({
...github.context.repo,
release_id: release.data.id,
});
const deleteTagRef = async () =>
api.git.deleteRef({
...github.context.repo,
ref: `tags/${code}`,
});
await retryOnFail(deleteRelease, 3);
await retryOnFail(deleteTagRef, 3);
}
async function retryOnFail(asyncFunction, maxTries = 3) {
if (maxTries < 1) {
throw `Retried ${maxTries}.. failed always. aborting`;
}
try {
await delay(1000);
return await asyncFunction();
} catch (err) {
console.log(err);
console.log(`Retrying now...`);
retryOnFail(asyncFunction, maxTries - 1);
}
}
async function delay(ms) {
await new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
main().catch((error) => {
console.error(error);
core.setFailed(error.message);
});