Skip to content

Commit 4cea925

Browse files
henrymercerCopilot
andcommitted
Look up releases requested by URL
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent e8f5d72 commit 4cea925

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

‎src/codeql-release.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getPublicRelease,
77
getRelease,
88
getReleaseCliVersion,
9+
getRequestedRelease,
910
parseCodeQLReleaseUrl,
1011
selectBundle,
1112
} from "./codeql-release";
@@ -174,6 +175,37 @@ test("getRelease propagates API errors", async (t) => {
174175
t.deepEqual(fixture.requests, [fixture.releaseAPIURL]);
175176
});
176177

178+
test("getRequestedRelease names a requested release that can't be found", async (t) => {
179+
const requested = { ...REFERENCE, isCurrentInstance: true };
180+
await t.throwsAsync(
181+
getRequestedRelease(releaseFixture({ status: 404 }).state, requested),
182+
{
183+
instanceOf: ConfigurationError,
184+
message: `Could not find the CodeQL release ${RELEASE_PAGE}. Check that it exists and that the token has access to it.`,
185+
},
186+
);
187+
t.like(
188+
await t.throwsAsync(
189+
getRequestedRelease(releaseFixture({ status: 500 }).state, requested),
190+
),
191+
{ status: 500 },
192+
);
193+
});
194+
195+
test("getRequestedRelease refers to a release on another instance by URL", async (t) => {
196+
const fixture = releaseFixture();
197+
const release = await getRequestedRelease(fixture.state, {
198+
...REFERENCE,
199+
isCurrentInstance: false,
200+
});
201+
t.is(
202+
release.getAssetURL(COMBINED),
203+
`https://github.com/octo/tools/releases/download/${TAG}/${COMBINED}`,
204+
);
205+
t.is(release.assetNames, undefined);
206+
t.deepEqual(fixture.requests, []);
207+
});
208+
177209
test("getPublicRelease constructs download URLs without looking up the release", async (t) => {
178210
const fixture = releaseFixture();
179211
const release = getPublicRelease({ ...REFERENCE, tagName: "nightly/v1+2" });

‎src/codeql-release.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import {
1111
} from "./per-language-bundles";
1212
import { BundlePlatform } from "./platform";
1313
import type { CompressionMethod } from "./tar";
14-
import { ConfigurationError, GITHUB_DOTCOM_URL, GitHubVariant } from "./util";
14+
import {
15+
asHTTPError,
16+
ConfigurationError,
17+
GITHUB_DOTCOM_URL,
18+
GitHubVariant,
19+
} from "./util";
1520

1621
/** Identifies a release on a GitHub instance. */
1722
export interface CodeQLReleaseReference {
@@ -188,6 +193,32 @@ export function getPublicRelease(
188193
};
189194
}
190195

196+
/** Gets a release requested by URL, looking it up if it's on the current GitHub instance. */
197+
export async function getRequestedRelease(
198+
action: ActionState<["Api"]>,
199+
requested: RequestedRelease,
200+
): Promise<CodeQLRelease> {
201+
if (!requested.isCurrentInstance) {
202+
// We can't send the token to another instance, so only public releases are usable there, and we
203+
// download them by URL rather than querying that instance's API. On the current instance we use
204+
// the API instead, because release download URLs don't accept tokens and so would fail for
205+
// private and internal repositories. Without the release's asset list, we assume the preferred
206+
// compression is available and take the CLI version from the tag.
207+
return getPublicRelease(requested);
208+
}
209+
try {
210+
return await getRelease(action, requested);
211+
} catch (e) {
212+
if (asHTTPError(e)?.status === 404) {
213+
throw new ConfigurationError(
214+
`Could not find the CodeQL release ${getReleasePageURL(requested)}. Check that it ` +
215+
"exists and that the token has access to it.",
216+
);
217+
}
218+
throw e;
219+
}
220+
}
221+
191222
/** Describes the job and runner that we are selecting a bundle for. */
192223
export interface BundleSelectionOptions {
193224
/** Explicit `languages` input, which determines whether a per-language bundle is eligible. */

0 commit comments

Comments
 (0)