@@ -4,6 +4,7 @@ import { ActionState } from "./action-common";
44import type { GitHubApiDetails } from "./api-client" ;
55import { CodeQLBundle , getCodeQLBundleName } from "./codeql-bundle" ;
66import { CODEQL_VERSION_ZSTD_BUNDLE } from "./feature-flags" ;
7+ import { Logger } from "./logging" ;
78import {
89 getPerLanguageBundleLanguage ,
910 logMissingPerLanguageBundle ,
@@ -26,6 +27,8 @@ export interface CodeQLRelease {
2627 url : string ;
2728 /** Returns the download URL for an asset, or `undefined` if the release doesn't have it. */
2829 getAssetURL ( name : string ) : string | undefined ;
30+ /** Names of the release's assets, if we looked up the release. */
31+ assetNames ?: string [ ] ;
2932}
3033
3134/** A release requested by URL, on this GitHub instance or on GitHub.com. */
@@ -86,6 +89,51 @@ export function parseCodeQLReleaseUrl(
8689 } ;
8790}
8891
92+ function parseCliVersion ( value : string ) : string | undefined {
93+ const parsed = semver . parse ( value ) ;
94+ if ( parsed === null ) {
95+ return undefined ;
96+ }
97+ // semver's normalized version omits build metadata, which distinguishes nightly builds.
98+ return (
99+ parsed . version + ( parsed . build . length ? `+${ parsed . build . join ( "." ) } ` : "" )
100+ ) ;
101+ }
102+
103+ /**
104+ * Returns the CLI version from the release's `cli-version-<version>.txt` asset if it has one, and
105+ * otherwise the version in the tag. Returns `undefined` if the release has conflicting markers.
106+ */
107+ export function getReleaseCliVersion (
108+ tagName : string ,
109+ assetNames : string [ ] ,
110+ logger : Logger ,
111+ ) : string | undefined {
112+ const versions = new Set < string > ( ) ;
113+ for ( const name of assetNames ) {
114+ const match = name . match ( / ^ c l i - v e r s i o n - ( .+ ) \. t x t $ / ) ;
115+ if ( match === null ) {
116+ continue ;
117+ }
118+ const version = parseCliVersion ( match [ 1 ] ) ;
119+ if ( version !== undefined ) {
120+ versions . add ( version ) ;
121+ } else {
122+ logger . debug ( `Ignoring invalid CLI version marker ${ name } .` ) ;
123+ }
124+ }
125+ if ( versions . size > 1 ) {
126+ logger . warning (
127+ `Release ${ tagName } has conflicting CLI version markers. Using a combined CodeQL bundle.` ,
128+ ) ;
129+ return undefined ;
130+ }
131+ if ( versions . size === 1 ) {
132+ return versions . values ( ) . next ( ) . value ;
133+ }
134+ return parseCliVersion ( tagName . replace ( / ^ c o d e q l - b u n d l e - / , "" ) ) ;
135+ }
136+
89137/**
90138 * Encodes a tag for use in a URL path. Slashes stay as path separators, as in GitHub's release URLs
91139 * for tags like `build/123`.
@@ -121,6 +169,7 @@ export async function getRelease(
121169 url : getReleasePageURL ( reference ) ,
122170 getAssetURL : ( name ) =>
123171 release . assets . find ( ( asset ) => asset . name === name ) ?. url ,
172+ assetNames : release . assets . map ( ( asset ) => asset . name ) ,
124173 } ;
125174}
126175
0 commit comments