Skip to content

Commit

Permalink
Merge pull request #2570 from github/henrymercer/more-robust-tag-name
Browse files Browse the repository at this point in the history
 Improve robustness of extracting the bundle tag name
  • Loading branch information
dbartol authored Oct 28, 2024
2 parents 3aa7135 + 1f4b0cb commit 6a38de6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 7 deletions.
14 changes: 12 additions & 2 deletions lib/setup-codeql.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/setup-codeql.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lib/setup-codeql.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/setup-codeql.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/setup-codeql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,13 @@ test("setupCodeQLBundle logs the CodeQL CLI version being used when asked to dow
);
});
});

test('tryGetTagNameFromUrl extracts the right tag name for a repo name containing "codeql-bundle"', (t) => {
t.is(
setupCodeql.tryGetTagNameFromUrl(
"https://github.com/org/codeql-bundle-testing/releases/download/codeql-bundle-v2.19.0/codeql-bundle-linux64.tar.zst",
getRunnerLogger(true),
),
"codeql-bundle-v2.19.0",
);
});
24 changes: 21 additions & 3 deletions src/setup-codeql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,30 @@ function tryGetBundleVersionFromTagName(
return match[1];
}

function tryGetTagNameFromUrl(url: string, logger: Logger): string | undefined {
const match = url.match(/\/(codeql-bundle-.*)\//);
if (match === null || match.length < 2) {
export function tryGetTagNameFromUrl(
url: string,
logger: Logger,
): string | undefined {
const matches = [...url.matchAll(/\/(codeql-bundle-[^/]*)\//g)];
if (!matches.length) {
logger.debug(`Could not determine tag name for URL ${url}.`);
return undefined;
}
// Example: https://github.com/org/codeql-bundle-testing/releases/download/codeql-bundle-v2.19.0/codeql-bundle-linux64.tar.zst
// We require a trailing forward slash to be part of the match, so the last match gives us the tag
// name. An alternative approach would be to also match against `/releases/`, but this approach
// assumes less about the structure of the URL.
const match = matches[matches.length - 1];

if (match === null || match.length !== 2) {
logger.debug(
`Could not determine tag name for URL ${url}. Matched ${JSON.stringify(
match,
)}.`,
);
return undefined;
}

return match[1];
}

Expand Down

0 comments on commit 6a38de6

Please sign in to comment.