Skip to content

Commit 28cc532

Browse files
committed
feat: Implement GitHub Token retrieval and enhance CodeQL installation process
1 parent fab46d8 commit 28cc532

File tree

3 files changed

+75
-30
lines changed

3 files changed

+75
-30
lines changed

src/action.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ pub struct Action {
103103
}
104104

105105
impl Action {
106+
/// Returns the GitHub Token for the action
107+
pub fn get_token(&self) -> String {
108+
if self.token.is_empty() {
109+
std::env::var("GITHUB_TOKEN").unwrap_or_default()
110+
} else {
111+
self.token.clone()
112+
}
113+
}
114+
106115
/// Returns the working directory for the action
107116
///
108117
/// If no working directory is provided, the current directory is used.

src/codeql.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,66 @@
55
//! installation process fails.
66
77
use anyhow::{Context, Result};
8+
use ghactions::ActionTrait;
89
use 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")

src/main.rs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ mod action;
1313
mod codeql;
1414
mod extractors;
1515

16+
use crate::codeql::codeql_download;
1617
use action::{AUTHORS, Action, BANNER, VERSION};
1718

18-
use crate::codeql::gh_codeql_download;
19-
2019
/// Main function that drives the CodeQL Extractor Action workflow
2120
///
2221
/// This function:
@@ -50,35 +49,14 @@ async fn main() -> Result<()> {
5049

5150
group!("Setting up CodeQL");
5251

53-
let mut codeql = CodeQL::init()
54-
.build()
52+
let mut codeql = codeql_download(&action)
5553
.await
56-
.context("Failed to create CodeQL instance")?;
57-
log::debug!("CodeQL :: {codeql:?}");
58-
59-
if !codeql.is_installed().await {
60-
let codeql_version = action.codeql_version();
61-
log::info!("CodeQL not installed, installing `{codeql_version}`...");
62-
63-
if let Err(error) = codeql.install(&octocrab, codeql_version).await {
64-
log::warn!("Failed to install CodeQL: {error:?}");
65-
log::info!("Attempting to install CodeQL using GitHub CLI...");
66-
67-
let location = gh_codeql_download(codeql_version)
68-
.await
69-
.context("Failed to download CodeQL using GitHub CLI")?;
70-
71-
codeql = CodeQL::init()
72-
.path(location)
73-
.build()
74-
.await
75-
.context("Failed to create CodeQL instance after GitHub CLI installation")?;
76-
}
54+
.context("Failed to set up CodeQL")?;
55+
log::info!(
56+
"CodeQL CLI Version :: {}",
57+
codeql.version().unwrap_or_default()
58+
);
7759

78-
log::info!("CodeQL installed");
79-
} else {
80-
log::info!("CodeQL already installed");
81-
}
8260
// Packs installation
8361
action.install_packs(&codeql).await?;
8462

0 commit comments

Comments
 (0)