Skip to content

Commit cf44a58

Browse files
committed
feat: support finding binary using file path
TICKET: VL-4112
1 parent debedf8 commit cf44a58

File tree

3 files changed

+173
-25
lines changed

3 files changed

+173
-25
lines changed

dist/index.js

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

src/fetch.ts

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export type ReleaseMetadataResponse = {
135135
data: {
136136
assets: Array<{
137137
label?: string | null;
138+
name?: string;
138139
url: string;
139140
// Other fields may exist but we don't use them
140141
}>;
@@ -157,15 +158,27 @@ export function findMatchingReleaseAssetMetadata(
157158
const targetLabelTraditional = `${binaryName.value}-${targetTriple}`;
158159
const targetLabelDuple = `${binaryName.value}-${targetDuple}`;
159160

160-
const asset = releaseMetadata.data.assets.find(
161-
(asset) =>
162-
typeof asset.label === "string" &&
163-
(asset.label === targetLabelTraditional || asset.label === targetLabelDuple),
164-
);
161+
const asset = releaseMetadata.data.assets.find((asset) => {
162+
// Check for label match
163+
if (typeof asset.label === "string") {
164+
if (asset.label === targetLabelTraditional || asset.label === targetLabelDuple) {
165+
return true;
166+
}
167+
}
168+
169+
// Check for name match
170+
if (typeof asset.name === "string") {
171+
if (asset.name === targetLabelTraditional || asset.name === targetLabelDuple) {
172+
return true;
173+
}
174+
}
175+
176+
return false;
177+
});
165178

166179
if (asset === undefined) {
167180
throw new Error(
168-
`Expected to find asset in release ${slug.owner}/${slug.repository}@${tag} with label ${targetLabelTraditional} or ${targetLabelDuple}`,
181+
`Expected to find asset in release ${slug.owner}/${slug.repository}@${tag} with label or name ${targetLabelTraditional} or ${targetLabelDuple}`,
169182
);
170183
}
171184

@@ -180,14 +193,26 @@ export function findMatchingReleaseAssetMetadata(
180193
// 2. There is an asset label matching the target triple or target duple.
181194
// In both cases, we assume that's the binary the user meant.
182195
// If there is ambiguity, exit with an error.
183-
const matchingAssets = releaseMetadata.data.assets.filter(
184-
(asset) =>
185-
typeof asset.label === "string" &&
186-
(asset.label.endsWith(targetTriple) || asset.label.endsWith(targetDuple)),
187-
);
196+
const matchingAssets = releaseMetadata.data.assets.filter((asset) => {
197+
// Check label match
198+
if (typeof asset.label === "string") {
199+
if (asset.label.endsWith(targetTriple) || asset.label.endsWith(targetDuple)) {
200+
return true;
201+
}
202+
}
203+
204+
// Check name match
205+
if (typeof asset.name === "string") {
206+
if (asset.name.endsWith(targetTriple) || asset.name.endsWith(targetDuple)) {
207+
return true;
208+
}
209+
}
210+
211+
return false;
212+
});
188213
if (matchingAssets.length === 0) {
189214
throw new Error(
190-
`Expected to find asset in release ${slug.owner}/${slug.repository}@${tag} with label ending in ${targetTriple} or ${targetDuple}`,
215+
`Expected to find asset in release ${slug.owner}/${slug.repository}@${tag} with label or name ending in ${targetTriple} or ${targetDuple}`,
191216
);
192217
}
193218
if (matchingAssets.length > 1) {
@@ -198,7 +223,17 @@ To resolve, specify the desired binary with the target format ${slug.owner}/${sl
198223
);
199224
}
200225
const asset = matchingAssets.shift()!;
201-
const targetName = stripTargetTriple(asset.label!);
226+
227+
// Determine which field matched to use for stripping the target triple
228+
let matchField: string;
229+
if (typeof asset.label === "string" &&
230+
(asset.label.endsWith(targetTriple) || asset.label.endsWith(targetDuple))) {
231+
matchField = asset.label;
232+
} else {
233+
matchField = asset.name!;
234+
}
235+
236+
const targetName = stripTargetTriple(matchField);
202237
return {
203238
binaryName: targetName,
204239
url: asset.url,

test/test-find-matching-release-asset.ts

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { findMatchingReleaseAssetMetadata } from "../src/fetch";
66
import type { ExactSemanticVersion, RepositorySlug, BinaryName, TargetTriple, TargetDuple } from "../src/types";
77

88
// Mocked releaseMetadata for tests
9-
function mockReleaseMetadata(assets: Array<{ label?: string; url: string }>) {
9+
function mockReleaseMetadata(assets: Array<{ label?: string; name?: string; url: string }>) {
1010
return {
1111
data: { assets }
1212
};
@@ -89,7 +89,7 @@ test("should throw error when binary name provided but no matching asset found",
8989
targetTriple,
9090
targetDuple
9191
);
92-
}, new Error(`Expected to find asset in release testowner/testrepo@v1.0.0 with label testbin-aarch64-apple-darwin or testbin-darwin-arm64`));
92+
}, new Error(`Expected to find asset in release testowner/testrepo@v1.0.0 with label or name testbin-aarch64-apple-darwin or testbin-darwin-arm64`));
9393
});
9494

9595
// Test without binary name
@@ -147,7 +147,7 @@ test("should throw error when no binary name provided and no matching asset foun
147147
targetTriple,
148148
targetDuple
149149
);
150-
}, new Error(`Expected to find asset in release testowner/testrepo@v1.0.0 with label ending in aarch64-apple-darwin or darwin-arm64`));
150+
}, new Error(`Expected to find asset in release testowner/testrepo@v1.0.0 with label or name ending in aarch64-apple-darwin or darwin-arm64`));
151151
});
152152

153153
test("should throw error when multiple assets match without binary name", () => {
@@ -227,4 +227,91 @@ test("should find x86_64 asset with binary name using target duple", () => {
227227
binaryName,
228228
url: "https://example.com/testbin-x86-duple"
229229
});
230+
});
231+
232+
// Tests for the new feature: finding assets by name property
233+
test("should find asset with binary name using name property with target triple", () => {
234+
const binaryName = some("testbin" as unknown as BinaryName);
235+
const mockAssets = [
236+
// No label property, only name property
237+
{ name: "testbin-aarch64-apple-darwin", url: "https://example.com/testbin-triple" },
238+
];
239+
240+
const result = findMatchingReleaseAssetMetadata(
241+
mockReleaseMetadata(mockAssets),
242+
mockSlug,
243+
binaryName,
244+
mockTag,
245+
targetTriple,
246+
targetDuple
247+
);
248+
249+
assert.deepEqual(result, {
250+
binaryName,
251+
url: "https://example.com/testbin-triple"
252+
});
253+
});
254+
255+
test("should find asset with binary name using name property with target duple", () => {
256+
const binaryName = some("testbin" as unknown as BinaryName);
257+
const mockAssets = [
258+
// No label property, only name property
259+
{ name: "testbin-darwin-arm64", url: "https://example.com/testbin-duple" },
260+
];
261+
262+
const result = findMatchingReleaseAssetMetadata(
263+
mockReleaseMetadata(mockAssets),
264+
mockSlug,
265+
binaryName,
266+
mockTag,
267+
targetTriple,
268+
targetDuple
269+
);
270+
271+
assert.deepEqual(result, {
272+
binaryName,
273+
url: "https://example.com/testbin-duple"
274+
});
275+
});
276+
277+
test("should find asset without binary name using name property with target triple", () => {
278+
const mockAssets = [
279+
// No label property, only name property
280+
{ name: "somebin-aarch64-apple-darwin", url: "https://example.com/somebin-triple" },
281+
];
282+
283+
const result = findMatchingReleaseAssetMetadata(
284+
mockReleaseMetadata(mockAssets),
285+
mockSlug,
286+
none(),
287+
mockTag,
288+
targetTriple,
289+
targetDuple
290+
);
291+
292+
assert.deepEqual(result, {
293+
binaryName: some("somebin"),
294+
url: "https://example.com/somebin-triple"
295+
});
296+
});
297+
298+
test("should find asset without binary name using name property with target duple", () => {
299+
const mockAssets = [
300+
// No label property, only name property
301+
{ name: "somebin-darwin-arm64", url: "https://example.com/somebin-duple" },
302+
];
303+
304+
const result = findMatchingReleaseAssetMetadata(
305+
mockReleaseMetadata(mockAssets),
306+
mockSlug,
307+
none(),
308+
mockTag,
309+
targetTriple,
310+
targetDuple
311+
);
312+
313+
assert.deepEqual(result, {
314+
binaryName: some("somebin"),
315+
url: "https://example.com/somebin-duple"
316+
});
230317
});

0 commit comments

Comments
 (0)