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
198 lines (174 loc) · 5.94 KB
/
Copy pathfetch.ts
File metadata and controls
198 lines (174 loc) · 5.94 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
import type { Octokit } from "./octokit";
import { isEqual, isSome, none, Option, some } from "./option";
import { stripTargetTriple } from "./platform";
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;
};
export async function fetchReleaseAssetMetadataFromTag(
octokit: Octokit,
slug: RepositorySlug,
binaryName: Option<BinaryName>,
tag: ExactSemanticVersion,
targetTriple: TargetTriple,
): 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,
});
// When the binary name is provided, look for matching binary and target triple.
if (isSome(binaryName)) {
const targetLabel = `${binaryName.value}-${targetTriple}`;
// First try to find asset by label (original behavior)
let asset = releaseMetadata.data.assets.find(
(asset) => asset.label === targetLabel,
);
// If not found by label, try to find asset by exact name match
if (asset === undefined) {
asset = releaseMetadata.data.assets.find(
(asset) => asset.name === binaryName.value,
);
}
if (asset === undefined) {
throw new Error(
`Expected to find asset in release ${slug.owner}/${slug.repository}@${tag} with label ${targetLabel} or name ${binaryName.value}`,
);
}
return {
binaryName: binaryName,
url: asset.url,
};
}
// When the binary name is not provided, support two use cases:
// 1. There is only one binary uploaded to this release, a named binary.
// 2. There is an asset label or name matching the target triple (with no binary name).
// In both cases, we assume that's the binary the user meant.
// If there is ambiguity, exit with an error.
const matchingTargetTriples = releaseMetadata.data.assets.filter(
(asset) =>
typeof asset.label === "string" && asset.label.endsWith(targetTriple),
);
if (matchingTargetTriples.length === 0) {
throw new Error(
`Expected to find asset in release ${slug.owner}/${slug.repository}@${tag} with label ending in ${targetTriple}`,
);
}
if (matchingTargetTriples.length > 1) {
throw new Error(
`Ambiguous targets: expected to find a single asset in release ${slug.owner}/${slug.repository}@${tag} matching target triple ${targetTriple}, but found ${matchingTargetTriples.length}.
To resolve, specify the desired binary with the target format ${slug.owner}/${slug.repository}/<binary-name>@${tag}`,
);
}
const asset = matchingTargetTriples.shift()!;
const targetName = stripTargetTriple(asset.label!);
return {
binaryName: targetName,
url: asset.url,
};
}