Skip to content

Commit 75d3854

Browse files
committed
feat: Add function to retrieve possible CodeQL directories and improve error handling for CodeQL directory retrieval
1 parent f6a190a commit 75d3854

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

src/action.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,47 @@ impl Action {
177177
languages
178178
}
179179

180+
/// Gets the possible directories for CodeQL operations.
181+
///
182+
/// This function identifies potential locations for CodeQL operation directories in the following order:
183+
/// 1. The `.codeql` directory in the GitHub workspace (if running in GitHub Actions)
184+
/// 2. The `.codeql` directory in the current working directory
185+
/// 3. The `.codeql` directory in the GitHub Actions runner's temp directory (if available)
186+
/// 4. The `.codeql` directory in the system's temporary directory
187+
///
188+
/// Each path is checked for existence and created if necessary by the caller.
189+
///
190+
/// # Returns
191+
/// - `Result<Vec<PathBuf>>`: A vector of possible directory paths for CodeQL operations
192+
///
193+
/// # Errors
194+
/// - If `working_directory()` fails
195+
/// - If path canonicalization fails
196+
fn get_codeql_directories(&self) -> Result<Vec<PathBuf>> {
197+
let mut paths = Vec::new();
198+
199+
// GITHUB_WORKSPACE
200+
if let Ok(github_workspace) = std::env::var("GITHUB_WORKSPACE") {
201+
paths.push(PathBuf::from(github_workspace).join(".codeql"));
202+
}
203+
204+
// Local CodeQL directory in the working directory
205+
if let Ok(local_codeql) = self.working_directory()?.join(".codeql").canonicalize() {
206+
paths.push(local_codeql);
207+
}
208+
209+
// Runner temp directory
210+
if let Ok(runner_temp) = std::env::var("RUNNER_TEMP") {
211+
paths.push(PathBuf::from(runner_temp).join(".codeql").canonicalize()?);
212+
}
213+
// temp_dir
214+
if let Ok(temp_dir) = std::env::temp_dir().canonicalize() {
215+
paths.push(temp_dir.join(".codeql"));
216+
}
217+
218+
Ok(paths)
219+
}
220+
180221
/// Returns the directory to use for CodeQL operations.
181222
///
182223
/// Gets the CodeQL directory to use for the action. It will first check if a local
@@ -187,17 +228,8 @@ impl Action {
187228
/// It uses the parent of the working directory to to stop issues where the
188229
/// database/sarif files gets indexed by CodeQL.
189230
pub fn get_codeql_dir(&self) -> Result<PathBuf> {
190-
let paths = vec![
191-
// Local CodeQL directory in the working directory parent
192-
self.working_directory()?
193-
.join("..")
194-
.join(".codeql")
195-
.canonicalize()?,
196-
// Runner temp directory
197-
PathBuf::from(std::env::var("RUNNER_TEMP").unwrap_or_else(|_| "/tmp".to_string()))
198-
.join(".codeql")
199-
.canonicalize()?,
200-
];
231+
let paths = self.get_codeql_directories()?;
232+
log::debug!("Possible CodeQL directories: {:?}", paths);
201233

202234
for path in paths {
203235
if !path.exists() {

src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ async fn main() -> Result<()> {
4141
let cwd = action
4242
.working_directory()
4343
.context("Failed to get working directory")?;
44-
let codeql_dir = action.get_codeql_dir()?;
44+
let codeql_dir = action
45+
.get_codeql_dir()
46+
.context("Failed to get CodeQL directory")?;
4547

4648
let databases = codeql_dir.join("databases");
4749
let sarif_output = codeql_dir.join("results");

0 commit comments

Comments
 (0)