55//! installation process fails.
66
77use anyhow:: { Context , Result } ;
8+ use ghactions:: ActionTrait ;
89use ghastoolkit:: CodeQL ;
910
11+ use crate :: action:: Action ;
12+
13+ /// Download and install the CodeQL CLI, with fallback to GitHub CLI if necessary
14+ pub async fn codeql_download ( action : & Action ) -> Result < CodeQL > {
15+ let token = action. get_token ( ) ;
16+
17+ let mut codeql = CodeQL :: init ( )
18+ . build ( )
19+ . await
20+ . context ( "Failed to create CodeQL instance" ) ?;
21+ log:: debug!( "CodeQL :: {codeql:?}" ) ;
22+
23+ if !codeql. is_installed ( ) . await {
24+ let codeql_version = action. codeql_version ( ) ;
25+ log:: info!( "CodeQL not installed, installing `{codeql_version}`..." ) ;
26+
27+ // Try to install with authentication first (if token is available)
28+ if !token. is_empty ( ) {
29+ let octocrab_auth = action. octocrab_with_token ( token) ?;
30+ if let Ok ( _) = codeql. install ( & octocrab_auth, codeql_version) . await {
31+ log:: info!( "CodeQL installed using authentication" ) ;
32+ return Ok ( codeql) ;
33+ } else {
34+ log:: warn!(
35+ "Failed to install CodeQL with authentication, trying without authentication..."
36+ ) ;
37+ }
38+ }
39+
40+ // Try to install without authentication
41+ let octocrab = action. octocrab_without_token ( ) ?;
42+ if let Ok ( _) = codeql. install ( & octocrab, codeql_version) . await {
43+ log:: info!( "CodeQL installed without authentication" ) ;
44+ return Ok ( codeql) ;
45+ } else {
46+ log:: warn!( "Failed to install CodeQL without authentication" ) ;
47+ log:: info!( "Attempting to install CodeQL using GitHub CLI..." ) ;
48+ }
49+
50+ let location = gh_codeql_download ( codeql_version)
51+ . await
52+ . context ( "Failed to download CodeQL using GitHub CLI" ) ?;
53+ // Reinitialize CodeQL with the new path
54+ codeql = CodeQL :: init ( )
55+ . path ( location)
56+ . build ( )
57+ . await
58+ . context ( "Failed to create CodeQL instance after GitHub CLI installation" ) ?;
59+
60+ log:: info!( "CodeQL installed" ) ;
61+ } else {
62+ log:: info!( "CodeQL already installed" ) ;
63+ }
64+
65+ Ok ( codeql)
66+ }
67+
1068/// Download and install the CodeQL CLI using the GitHub CLI
1169///
1270/// This function serves as a fallback installation method when the standard CodeQL
@@ -20,7 +78,7 @@ use ghastoolkit::CodeQL;
2078///
2179/// # Returns
2280/// * `Result<String>` - Path to the installed CodeQL binary or an error
23- pub async fn gh_codeql_download ( codeql_version : & str ) -> Result < String > {
81+ async fn gh_codeql_download ( codeql_version : & str ) -> Result < String > {
2482 log:: info!( "Downloading CodeQL Extension for GitHub CLI..." ) ;
2583 log:: debug!( "Running command: gh extensions install github/gh-codeql" ) ;
2684 let status = tokio:: process:: Command :: new ( "gh" )
0 commit comments