-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
88 lines (79 loc) · 2.07 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
const core = require("@actions/core");
const github = require("@actions/github");
const crypto = require("crypto");
async function run() {
const token = core.getInput("token");
const octokit = github.getOctokit(token);
let release = null;
let tag = github.context.ref.replace(/^refs\/tags\//, "");
const { owner, repo } = github.context.repo;
try {
release = await octokit.rest.repos.getReleaseByTag({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
tag: tag,
});
} catch (err) {
if (err.status === 404) {
let newRelease = {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
tag_name: tag,
};
release = await octokit.rest.repos.createRelease(newRelease);
}
}
const release_id = release.data.id;
const assets = await octokit.rest.repos.listReleaseAssets({
owner,
repo,
release_id,
});
const hashes = await Promise.all(
assets.data
.filter((d) => d.name !== "checksums.txt")
.map(async (asset) => {
const file = await octokit.rest.repos.getReleaseAsset({
owner,
repo,
asset_id: asset.id,
headers: { accept: "application/octet-stream" },
});
const hash = crypto
.createHash("sha256")
.update(Buffer.from(file.data))
.digest("hex");
return `${hash} ${asset.name}`;
})
);
const data = hashes.join("\n");
try {
await octokit.rest.repos.uploadReleaseAsset({
owner,
repo,
release_id,
name: "checksums.txt",
data: data,
});
} catch (err) {
const assets = await octokit.rest.repos.listReleaseAssets({
owner,
repo,
release_id,
});
const asset = assets.data.filter((a) => a.name === "checksums.txt")[0];
await octokit.rest.repos.deleteReleaseAsset({
owner,
repo,
asset_id: asset.id,
});
await octokit.rest.repos.uploadReleaseAsset({
owner,
repo,
release_id,
name: "checksums.txt",
data: data,
});
}
}
run();