forked from EricCrosson/install-github-release-binary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.ts
More file actions
266 lines (234 loc) · 7.61 KB
/
fetch.ts
File metadata and controls
266 lines (234 loc) · 7.61 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import type { Octokit } from "./octokit";
import { isEqual, isSome, none, Option, some } from "./option";
import { stripTargetTriple } from "./platform";
import type { TargetDuple } from "./types";
import {
isExactSemanticVersion,
ExactSemanticVersion,
RepositorySlug,
SemanticVersion,
Sha1Hash,
TargetTriple,
BinaryName,
} from "./types";
type Commit = {
sha: Sha1Hash;
};
// This type is only exported for testing.
export type Tag = {
name: SemanticVersion;
commit: Commit;
};
export type TagsResponse = ReadonlyArray<Tag>;
function containsExactTag(
tags: readonly SemanticVersion[] | undefined,
): ExactSemanticVersion | undefined {
if (tags === undefined) {
return undefined;
}
return tags.find(isExactSemanticVersion);
}
// This function is only exported for testing.
export function semanticVersionTagReducer(
givenTag: SemanticVersion,
): (tag: Tag) => Option<ExactSemanticVersion> {
const versionsBySha: Record<Sha1Hash, SemanticVersion[]> = {};
let givenTagSha: Option<Sha1Hash> = none();
// Conditions for an exact match are -- we know both the:
//
// - sha that the given tag points to
// - exact version tag matching that sha
//
// These can be found in either order.
return function reducer(tag: Tag): Option<ExactSemanticVersion> {
const sha = tag.commit.sha;
const version = tag.name;
// If we found the sha the given tag points to
if (version === givenTag) {
givenTagSha = some(sha);
// check if we already knew the exact version tag matching that sha
const maybeExactTag = containsExactTag(versionsBySha[sha]);
if (maybeExactTag !== undefined) {
return some(maybeExactTag);
}
}
// If we're not looking at the given tag, and we're not looking
// at an exact version, this data is of no use to us.
if (!isExactSemanticVersion(version)) {
return none();
}
// It is possible that we know the sha for the given tag,
// we're just looking for exact version tag matching that sha.
if (isEqual(givenTagSha, sha)) {
return some(version);
}
// Otherwise, record this map of sha -> exact version tag
// so we can find it when we know the sha of the given tag.
const associatedVersions = versionsBySha[sha];
if (associatedVersions === undefined) {
versionsBySha[sha] = [version];
} else {
associatedVersions.push(version);
}
return none();
};
}
// Find the exact semantic version tag that this tag maps to.
//
// We need an exact tag because that's the only accepted input
// to GitHub's getReleaseByTag endpoint.
export async function findExactSemanticVersionTag(
octokit: Octokit,
slug: RepositorySlug,
target: SemanticVersion,
): Promise<ExactSemanticVersion> {
if (isExactSemanticVersion(target)) {
return target;
}
const reducer = semanticVersionTagReducer(target);
for await (const response of octokit.paginate.iterator(
octokit.rest.repos.listTags,
{
owner: slug.owner,
repo: slug.repository,
per_page: 100,
},
)) {
// NOTE: we are not parsing here, so this is an unlawful type cast
for (const tag of response.data as unknown as TagsResponse) {
const maybeExactTag = reducer(tag);
if (isSome(maybeExactTag)) {
return maybeExactTag.value;
}
}
}
throw new Error(
`Expected to find an exact semantic version tag matching ${target} for ${slug.owner}/${slug.repository}`,
);
}
type ReleaseAssetMetadata = {
binaryName: Option<string>;
url: string;
};
/**
* Type for GitHub release metadata required by our function
*/
export type ReleaseMetadataResponse = {
data: {
assets: Array<{
label?: string | null;
name?: string;
url: string;
// Other fields may exist but we don't use them
}>;
};
};
/**
* Extract matching asset metadata from release assets based on target platform format
*/
export function findMatchingReleaseAssetMetadata(
releaseMetadata: ReleaseMetadataResponse,
slug: RepositorySlug,
binaryName: Option<BinaryName>,
tag: ExactSemanticVersion,
targetTriple: TargetTriple,
targetDuple: TargetDuple,
): ReleaseAssetMetadata {
// When the binary name is provided, look for matching binary with target triple or target duple
if (isSome(binaryName)) {
const targetLabelTraditional = `${binaryName.value}-${targetTriple}`;
const targetLabelDuple = `${binaryName.value}-${targetDuple}`;
const asset = releaseMetadata.data.assets.find((asset) => {
// Check for label match
if (typeof asset.label === "string") {
if (asset.label === targetLabelTraditional || asset.label === targetLabelDuple) {
return true;
}
}
// Check for name match
if (typeof asset.name === "string") {
if (asset.name === targetLabelTraditional || asset.name === targetLabelDuple) {
return true;
}
}
return false;
});
if (asset === undefined) {
throw new Error(
`Expected to find asset in release ${slug.owner}/${slug.repository}@${tag} with label or name ${targetLabelTraditional} or ${targetLabelDuple}`,
);
}
return {
binaryName: binaryName,
url: asset.url,
};
}
// When the binary name is not provided, support these use cases:
// 1. There is only one binary uploaded to this release, a named binary.
// 2. There is an asset label matching the target triple or target duple.
// In both cases, we assume that's the binary the user meant.
// If there is ambiguity, exit with an error.
const matchingAssets = releaseMetadata.data.assets.filter((asset) => {
// Check label match
if (typeof asset.label === "string") {
if (asset.label.endsWith(targetTriple) || asset.label.endsWith(targetDuple)) {
return true;
}
}
// Check name match
if (typeof asset.name === "string") {
if (asset.name.endsWith(targetTriple) || asset.name.endsWith(targetDuple)) {
return true;
}
}
return false;
});
if (matchingAssets.length === 0) {
throw new Error(
`Expected to find asset in release ${slug.owner}/${slug.repository}@${tag} with label or name ending in ${targetTriple} or ${targetDuple}`,
);
}
if (matchingAssets.length > 1) {
throw new Error(
`Ambiguous targets: expected to find a single asset in release ${slug.owner}/${slug.repository}@${tag} matching target triple ${targetTriple} or target duple ${targetDuple}, but found ${matchingAssets.length}.
To resolve, specify the desired binary with the target format ${slug.owner}/${slug.repository}/<binary-name>@${tag}`,
);
}
const asset = matchingAssets.shift()!;
// Determine which field matched to use for stripping the target triple
let matchField: string;
if (typeof asset.label === "string" &&
(asset.label.endsWith(targetTriple) || asset.label.endsWith(targetDuple))) {
matchField = asset.label;
} else {
matchField = asset.name!;
}
const targetName = stripTargetTriple(matchField);
return {
binaryName: targetName,
url: asset.url,
};
}
export async function fetchReleaseAssetMetadataFromTag(
octokit: Octokit,
slug: RepositorySlug,
binaryName: Option<BinaryName>,
tag: ExactSemanticVersion,
targetTriple: TargetTriple,
targetDuple: TargetDuple,
): Promise<ReleaseAssetMetadata> {
// Maintainer's note: this impure function call makes this function difficult to test.
const releaseMetadata = await octokit.rest.repos.getReleaseByTag({
owner: slug.owner,
repo: slug.repository,
tag,
});
return findMatchingReleaseAssetMetadata(
releaseMetadata,
slug,
binaryName,
tag,
targetTriple,
targetDuple,
);
}