-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
157 lines (140 loc) · 4.18 KB
/
index.ts
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
import { createHash } from "node:crypto";
import { arch, platform } from "node:os";
import * as fs from "node:fs";
import * as path from "node:path";
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import { getErrors, unwrap } from "./either";
import { getOctokit, Octokit } from "./octokit";
import {
parseEnvironmentVariable,
parseTargetReleases,
parseToken,
} from "./parse";
import { getTargetTriple } from "./platform";
import {
fetchReleaseAssetMetadataFromTag,
findExactSemanticVersionTag,
} from "./fetch";
import type {
ExactSemanticVersion,
RepositorySlug,
TargetRelease,
} from "./types";
import { isSome, unwrapOrDefault } from "./option";
function getDestinationDirectory(
storageDirectory: string,
slug: RepositorySlug,
tag: ExactSemanticVersion,
platform: NodeJS.Platform,
architecture: string,
): string {
return path.join(
storageDirectory,
slug.owner.toLowerCase(),
slug.repository.toLowerCase(),
tag,
`${platform}-${architecture}`,
);
}
async function installGitHubReleaseBinary(
octokit: Octokit,
targetRelease: TargetRelease,
storageDirectory: string,
token: string,
): Promise<void> {
const targetTriple = getTargetTriple(arch(), platform());
const releaseTag = await findExactSemanticVersionTag(
octokit,
targetRelease.slug,
targetRelease.tag,
);
const destinationDirectory = getDestinationDirectory(
storageDirectory,
targetRelease.slug,
releaseTag,
platform(),
arch(),
);
const releaseAsset = await fetchReleaseAssetMetadataFromTag(
octokit,
targetRelease.slug,
targetRelease.binaryName,
releaseTag,
targetTriple,
);
const destinationBasename = unwrapOrDefault(
releaseAsset.binaryName,
targetRelease.slug.repository,
);
const destinationFilename = path.join(
destinationDirectory,
destinationBasename,
);
fs.mkdirSync(destinationDirectory, { recursive: true });
await tc.downloadTool(
releaseAsset.url,
destinationFilename,
`token ${token}`,
{ accept: "application/octet-stream" },
);
// Ensure the binary matches the expected checksum
if (isSome(targetRelease.checksum)) {
const fileBuffer = fs.readFileSync(destinationFilename);
const hash = createHash("sha256");
hash.update(fileBuffer);
const calculatedChecksum = hash.digest("hex");
const expectedChecksum = targetRelease.checksum.value;
if (calculatedChecksum !== expectedChecksum) {
const target = `${targetRelease.slug}@${targetRelease.tag}:sha256-${expectedChecksum}`;
core.error(
`Expected checksum ${expectedChecksum}, but got ${calculatedChecksum}`,
);
throw new Error(`Unexpected checksum for ${target}`);
} else {
core.debug(
`Calculated checksum ${calculatedChecksum} matches expected checksum ${expectedChecksum}`,
);
}
}
// Permissions are an attribute of the filesystem, not the file.
// Set the executable permission on the binary no matter where it came from.
fs.chmodSync(destinationFilename, "755");
core.addPath(destinationDirectory);
}
async function main(): Promise<void> {
const maybeToken = parseToken(
process.env["GITHUB_TOKEN"] || core.getInput("token"),
);
const maybeTargetReleases = parseTargetReleases(core.getInput("targets"));
const maybeHomeDirectory = parseEnvironmentVariable("HOME");
const errors = [maybeToken, maybeTargetReleases, maybeHomeDirectory].flatMap(
getErrors,
);
if (errors.length > 0) {
errors.forEach((error) => core.error(error));
throw new Error("Invalid inputs");
}
const token = unwrap(maybeToken);
const targetReleases = unwrap(maybeTargetReleases);
const homeDirectory = unwrap(maybeHomeDirectory);
const storageDirectory = path.join(
homeDirectory,
".install-github-release-binary",
"bin",
);
const octokit = getOctokit(token);
// REFACTOR(OPTIMIZE): if two targets can be pulled from the same
// release, we can make that happen with fewer API calls
await Promise.all(
targetReleases.map((targetRelease) =>
installGitHubReleaseBinary(
octokit,
targetRelease,
storageDirectory,
token,
),
),
);
}
main();