Skip to content

Commit 18e73d3

Browse files
Copilotfelickz
andauthored
Include available version range in latest-N/semver-range error messages
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 parent eccc888 commit 18e73d3

3 files changed

Lines changed: 97 additions & 6 deletions

File tree

‎lib/entry-points.js‎

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

‎src/setup-codeql.test.ts‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ test.serial(
463463

464464
await withTmpDir(async (tmpDir) => {
465465
setupActionsVars(tmpDir, tmpDir);
466-
await t.throwsAsync(
466+
const error = await t.throwsAsync(
467467
async () =>
468468
await setupCodeql.getCodeQLSource(
469469
"latest-99",
@@ -478,6 +478,13 @@ test.serial(
478478
),
479479
{ instanceOf: ConfigurationError },
480480
);
481+
// The error should mention the oldest and newest available releases, to help distinguish
482+
// a request that is out of range from other configuration mistakes.
483+
t.true(
484+
error.message.includes(
485+
"Available stable CodeQL CLI releases range from 2.24.0 to 2.25.3.",
486+
),
487+
);
481488
});
482489
},
483490
);
@@ -530,7 +537,7 @@ test.serial(
530537

531538
await withTmpDir(async (tmpDir) => {
532539
setupActionsVars(tmpDir, tmpDir);
533-
await t.throwsAsync(
540+
const error = await t.throwsAsync(
534541
async () =>
535542
await setupCodeql.getCodeQLSource(
536543
"9.x",
@@ -545,6 +552,48 @@ test.serial(
545552
),
546553
{ instanceOf: ConfigurationError },
547554
);
555+
t.true(
556+
error.message.includes(
557+
"Available stable CodeQL CLI releases range from 2.24.0 to 2.25.3.",
558+
),
559+
);
560+
});
561+
},
562+
);
563+
564+
test.serial(
565+
"getCodeQLSource throws a helpful error when a version range is older than any available release",
566+
async (t) => {
567+
const features = createFeatures([]);
568+
mockListStableCodeQLBundleReleases();
569+
570+
await withTmpDir(async (tmpDir) => {
571+
setupActionsVars(tmpDir, tmpDir);
572+
// The oldest release in STABLE_BUNDLE_RELEASES_TEST_SET is 2.24.0, so a range entirely
573+
// below that, such as this one modeled on a real user request for CodeQL 1.28.x, can never
574+
// be satisfied. The error should clearly state the oldest available release, since older
575+
// CodeQL bundles are tagged with a date, e.g. `codeql-bundle-20211208`, rather than a
576+
// semantic version, and so are invisible to SemVer-based `tools` inputs.
577+
const error = await t.throwsAsync(
578+
async () =>
579+
await setupCodeql.getCodeQLSource(
580+
"1.28.0 - 1.28.9",
581+
SAMPLE_DEFAULT_CLI_VERSION,
582+
undefined, // rawLanguages
583+
false, // useOverlayAwareDefaultCliVersion
584+
SAMPLE_DOTCOM_API_DETAILS,
585+
GitHubVariant.DOTCOM,
586+
false,
587+
features,
588+
getRunnerLogger(true),
589+
),
590+
{ instanceOf: ConfigurationError },
591+
);
592+
t.true(
593+
error.message.includes(
594+
"Available stable CodeQL CLI releases range from 2.24.0 to 2.25.3.",
595+
),
596+
);
548597
});
549598
},
550599
);

‎src/setup-codeql.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,32 @@ async function getSortedStableCliVersions(logger: Logger): Promise<string[]> {
331331
return [...versions].sort(semver.rcompare);
332332
}
333333

334+
/**
335+
* Returns a human-readable clause describing the oldest and newest versions in
336+
* `sortedVersions` (which must be sorted in descending version order, as returned by
337+
* `getSortedStableCliVersions`), suitable for appending to an error message when a requested
338+
* `latest-<N>` offset or version range cannot be satisfied.
339+
*
340+
* Older CodeQL bundle releases are not tagged with a semantic version (for example
341+
* `codeql-bundle-20211208`), so `sortedVersions` will never extend further back than the oldest
342+
* release with a semantic version tag. Mentioning that oldest version here helps explain
343+
* failures caused by requesting a version or range older than any semantically versioned bundle,
344+
* without needing to special-case that situation separately from any other unsatisfiable
345+
* request, such as one for a version newer than any release.
346+
*
347+
* Returns an empty string if `sortedVersions` is empty.
348+
*/
349+
function describeAvailableCliVersionRange(sortedVersions: string[]): string {
350+
if (sortedVersions.length === 0) {
351+
return "";
352+
}
353+
const newest = sortedVersions[0];
354+
const oldest = sortedVersions[sortedVersions.length - 1];
355+
return newest === oldest
356+
? ` The only available stable CodeQL CLI release is ${newest}.`
357+
: ` Available stable CodeQL CLI releases range from ${oldest} to ${newest}.`;
358+
}
359+
334360
export type CodeQLToolsSource =
335361
| {
336362
codeqlTarPath: string;
@@ -748,7 +774,9 @@ export async function getCodeQLSource(
748774
if (offset >= sortedVersions.length) {
749775
throw new util.ConfigurationError(
750776
`'tools: ${toolsInput}' was requested, but only ${sortedVersions.length} stable CodeQL ` +
751-
"CLI release(s) could be found.",
777+
`CLI release(s) could be found.${describeAvailableCliVersionRange(
778+
sortedVersions,
779+
)}`,
752780
);
753781
}
754782

@@ -775,7 +803,9 @@ export async function getCodeQLSource(
775803
if (!resolvedVersion) {
776804
throw new util.ConfigurationError(
777805
`'tools: ${toolsInput}' was requested, but no stable CodeQL CLI release satisfying that ` +
778-
"version range could be found.",
806+
`version range could be found.${describeAvailableCliVersionRange(
807+
sortedVersions,
808+
)}`,
779809
);
780810
}
781811

0 commit comments

Comments
 (0)