Skip to content

Commit debedf8

Browse files
Merge pull request #20 from BitGo/VL-4112-support-go-platform-duple
feat: support Go standard for target platform
2 parents c038e23 + 9008290 commit debedf8

File tree

7 files changed

+456
-54
lines changed

7 files changed

+456
-54
lines changed

dist/index.js

Lines changed: 68 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fetch.ts

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Octokit } from "./octokit";
22
import { isEqual, isSome, none, Option, some } from "./option";
33
import { stripTargetTriple } from "./platform";
4+
import type { TargetDuple } from "./types";
45

56
import {
67
isExactSemanticVersion,
@@ -127,62 +128,104 @@ type ReleaseAssetMetadata = {
127128
url: string;
128129
};
129130

130-
export async function fetchReleaseAssetMetadataFromTag(
131-
octokit: Octokit,
131+
/**
132+
* Type for GitHub release metadata required by our function
133+
*/
134+
export type ReleaseMetadataResponse = {
135+
data: {
136+
assets: Array<{
137+
label?: string | null;
138+
url: string;
139+
// Other fields may exist but we don't use them
140+
}>;
141+
};
142+
};
143+
144+
/**
145+
* Extract matching asset metadata from release assets based on target platform format
146+
*/
147+
export function findMatchingReleaseAssetMetadata(
148+
releaseMetadata: ReleaseMetadataResponse,
132149
slug: RepositorySlug,
133150
binaryName: Option<BinaryName>,
134151
tag: ExactSemanticVersion,
135152
targetTriple: TargetTriple,
136-
): Promise<ReleaseAssetMetadata> {
137-
// Maintainer's note: this impure function call makes this function difficult to test.
138-
const releaseMetadata = await octokit.rest.repos.getReleaseByTag({
139-
owner: slug.owner,
140-
repo: slug.repository,
141-
tag,
142-
});
143-
144-
// When the binary name is provided, look for matching binary and target triple.
153+
targetDuple: TargetDuple,
154+
): ReleaseAssetMetadata {
155+
// When the binary name is provided, look for matching binary with target triple or target duple
145156
if (isSome(binaryName)) {
146-
const targetLabel = `${binaryName.value}-${targetTriple}`;
157+
const targetLabelTraditional = `${binaryName.value}-${targetTriple}`;
158+
const targetLabelDuple = `${binaryName.value}-${targetDuple}`;
159+
147160
const asset = releaseMetadata.data.assets.find(
148-
(asset) => asset.label === targetLabel,
161+
(asset) =>
162+
typeof asset.label === "string" &&
163+
(asset.label === targetLabelTraditional || asset.label === targetLabelDuple),
149164
);
165+
150166
if (asset === undefined) {
151167
throw new Error(
152-
`Expected to find asset in release ${slug.owner}/${slug.repository}@${tag} with label ${targetLabel}`,
168+
`Expected to find asset in release ${slug.owner}/${slug.repository}@${tag} with label ${targetLabelTraditional} or ${targetLabelDuple}`,
153169
);
154170
}
171+
155172
return {
156173
binaryName: binaryName,
157174
url: asset.url,
158175
};
159176
}
160177

161-
// When the binary name is not provided, support two use cases:
178+
// When the binary name is not provided, support these use cases:
162179
// 1. There is only one binary uploaded to this release, a named binary.
163-
// 2. There is an asset label matching the target triple (with no binary name).
180+
// 2. There is an asset label matching the target triple or target duple.
164181
// In both cases, we assume that's the binary the user meant.
165182
// If there is ambiguity, exit with an error.
166-
const matchingTargetTriples = releaseMetadata.data.assets.filter(
183+
const matchingAssets = releaseMetadata.data.assets.filter(
167184
(asset) =>
168-
typeof asset.label === "string" && asset.label.endsWith(targetTriple),
185+
typeof asset.label === "string" &&
186+
(asset.label.endsWith(targetTriple) || asset.label.endsWith(targetDuple)),
169187
);
170-
if (matchingTargetTriples.length === 0) {
188+
if (matchingAssets.length === 0) {
171189
throw new Error(
172-
`Expected to find asset in release ${slug.owner}/${slug.repository}@${tag} with label ending in ${targetTriple}`,
190+
`Expected to find asset in release ${slug.owner}/${slug.repository}@${tag} with label ending in ${targetTriple} or ${targetDuple}`,
173191
);
174192
}
175-
if (matchingTargetTriples.length > 1) {
193+
if (matchingAssets.length > 1) {
176194
throw new Error(
177-
`Ambiguous targets: expected to find a single asset in release ${slug.owner}/${slug.repository}@${tag} matching target triple ${targetTriple}, but found ${matchingTargetTriples.length}.
195+
`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}.
178196
179197
To resolve, specify the desired binary with the target format ${slug.owner}/${slug.repository}/<binary-name>@${tag}`,
180198
);
181199
}
182-
const asset = matchingTargetTriples.shift()!;
200+
const asset = matchingAssets.shift()!;
183201
const targetName = stripTargetTriple(asset.label!);
184202
return {
185203
binaryName: targetName,
186204
url: asset.url,
187205
};
188206
}
207+
208+
export async function fetchReleaseAssetMetadataFromTag(
209+
octokit: Octokit,
210+
slug: RepositorySlug,
211+
binaryName: Option<BinaryName>,
212+
tag: ExactSemanticVersion,
213+
targetTriple: TargetTriple,
214+
targetDuple: TargetDuple,
215+
): Promise<ReleaseAssetMetadata> {
216+
// Maintainer's note: this impure function call makes this function difficult to test.
217+
const releaseMetadata = await octokit.rest.repos.getReleaseByTag({
218+
owner: slug.owner,
219+
repo: slug.repository,
220+
tag,
221+
});
222+
223+
return findMatchingReleaseAssetMetadata(
224+
releaseMetadata,
225+
slug,
226+
binaryName,
227+
tag,
228+
targetTriple,
229+
targetDuple,
230+
);
231+
}

src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
parseTargetReleases,
1414
parseToken,
1515
} from "./parse";
16-
import { getTargetTriple } from "./platform";
16+
import { getTargetTriple, getTargetDuple } from "./platform";
1717
import {
1818
fetchReleaseAssetMetadataFromTag,
1919
findExactSemanticVersionTag,
@@ -48,7 +48,10 @@ async function installGitHubReleaseBinary(
4848
token: string,
4949
ignoreExisting: boolean,
5050
): Promise<void> {
51-
const targetTriple = getTargetTriple(arch(), platform());
51+
const currentArch = arch();
52+
const currentPlatform = platform();
53+
const targetTriple = getTargetTriple(currentArch, currentPlatform);
54+
const targetDuple = getTargetDuple(currentArch, currentPlatform);
5255

5356
const releaseTag = await findExactSemanticVersionTag(
5457
octokit,
@@ -60,8 +63,8 @@ async function installGitHubReleaseBinary(
6063
storageDirectory,
6164
targetRelease.slug,
6265
releaseTag,
63-
platform(),
64-
arch(),
66+
currentPlatform,
67+
currentArch,
6568
);
6669

6770
const releaseAsset = await fetchReleaseAssetMetadataFromTag(
@@ -70,6 +73,7 @@ async function installGitHubReleaseBinary(
7073
targetRelease.binaryName,
7174
releaseTag,
7275
targetTriple,
76+
targetDuple,
7377
);
7478

7579
const destinationBasename = unwrapOrDefault(

0 commit comments

Comments
 (0)