Skip to content

Commit b51bca1

Browse files
committed
feat: Update Main workflow + Action
1 parent ff016f4 commit b51bca1

File tree

4 files changed

+62
-20
lines changed

4 files changed

+62
-20
lines changed

action.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ inputs:
1717
required: true
1818
packs:
1919
description: Query Packs to use
20-
codeql_version:
20+
codeql-version:
2121
description: CodeQL Version
2222
default: latest
23+
working-directory:
24+
description: Working Directory
2325
attestation:
2426
description: Attestation
2527
default: 'false'

src/action.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![allow(dead_code)]
2+
use std::path::PathBuf;
3+
24
use anyhow::{Context, Result};
35
use ghactions::prelude::*;
46
use ghactions_core::repository::reference::RepositoryReference as Repository;
@@ -41,22 +43,49 @@ pub struct Action {
4143
packs: Vec<String>,
4244

4345
/// CodeQL Version
44-
#[input(description = "CodeQL Version", default = "latest")]
46+
#[input(
47+
description = "CodeQL Version",
48+
rename = "codeql-version",
49+
default = "latest"
50+
)]
4551
codeql_version: String,
4652

53+
/// Working Directory (defualt: `./`)
54+
#[input(
55+
description = "Working Directory",
56+
rename = "working-directory",
57+
default = "./"
58+
)]
59+
working_directory: String,
60+
4761
/// Attestation
4862
#[input(description = "Attestation", default = "false")]
4963
attestation: bool,
5064

5165
/// Version of the extractor to use
5266
#[output(description = "Version of the extractor to use")]
5367
version: String,
68+
5469
/// Path to the extractor
5570
#[output(description = "Path to the extractor")]
5671
extractor_path: String,
5772
}
5873

5974
impl Action {
75+
pub fn working_directory(&self) -> Result<PathBuf> {
76+
if self.working_directory.is_empty() {
77+
log::debug!("No working directory provided, using the current directory");
78+
return std::env::current_dir().context("Failed to get current directory");
79+
}
80+
log::debug!("Using the provided working directory");
81+
Ok(std::path::PathBuf::from(&self.working_directory)
82+
.canonicalize()
83+
.context(format!(
84+
"Failed to get working directory `{}`",
85+
self.working_directory
86+
))?)
87+
}
88+
6089
/// Gets the repository to use for the extractor. If the repository is not provided,
6190
/// it will use the repository that the action is running in.
6291
pub fn extractor_repository(&self) -> Result<Repository> {

src/extractors.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,15 @@ pub async fn fetch_extractor(
7979
log::info!("No attestation requested");
8080
}
8181

82-
if extractor_pack.exists() {
83-
log::info!("Removing existing asset {:?}", extractor_pack);
84-
std::fs::remove_dir_all(&extractor_pack)?;
85-
}
86-
87-
log::info!("Extracting asset to {:?}", extractor_path);
88-
toolcache
89-
.extract_archive(&extractor_tarball, &output)
90-
.await?;
82+
if !extractor_pack.exists() {
83+
log::info!("Extracting asset to {:?}", extractor_path);
84+
toolcache
85+
.extract_archive(&extractor_tarball, &output)
86+
.await?;
9187

92-
if !extractor_path.exists() {
93-
return Err(anyhow::anyhow!("Extractor not found"));
88+
if !extractor_path.exists() {
89+
return Err(anyhow::anyhow!("Extractor not found"));
90+
}
9491
}
9592

9693
Ok(extractor_pack.canonicalize()?)

src/main.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,32 @@ async fn main() -> Result<()> {
8787

8888
groupend!();
8989

90-
let databases = PathBuf::from("./.codeql");
90+
let cwd = action
91+
.working_directory()
92+
.context("Failed to get working directory")?;
93+
let databases = cwd.join(".codeql");
9194
let sarif_output = databases.join("results");
9295

9396
std::fs::create_dir_all(&sarif_output).context("Failed to create results directory")?;
9497

9598
for language in action.languages() {
96-
let group = format!("Running {} extractor", language.language());
97-
group!(group);
99+
group!(format!("Running {} extractor", language.language()));
98100

99101
log::info!("Running extractor for language :: {}", language);
100102

101103
let database_path = databases.join(format!("db-{}", language));
104+
log::info!("Database Path :: {:?}", database_path);
105+
if database_path.exists() {
106+
std::fs::remove_dir_all(&database_path).with_context(|| {
107+
format!("Failed to remove database directory {:?}", database_path)
108+
})?;
109+
}
110+
102111
let sarif_path = sarif_output.join(format!("{}-results.sarif", language.language()));
103112

104-
let database = CodeQLDatabase::init()
113+
let mut database = CodeQLDatabase::init()
105114
.name(action.get_repository_name()?)
106-
.source(".".to_string())
115+
.source(cwd.display().to_string())
107116
.path(database_path.display().to_string())
108117
.language(language.language())
109118
.build()
@@ -116,7 +125,7 @@ async fn main() -> Result<()> {
116125
.create()
117126
.await
118127
.context("Failed to create database")?;
119-
log::info!("Created database :: {:?}", database);
128+
log::debug!("Created database :: {:?}", database);
120129

121130
// TODO: Queries
122131
let queries = CodeQLQueries::from(format!(
@@ -126,7 +135,8 @@ async fn main() -> Result<()> {
126135
));
127136
log::info!("Queries :: {:?}", queries);
128137

129-
log::info!("Running analysis...");
138+
groupend!();
139+
group!(format!("Running {} analysis", language.language()));
130140
match codeql
131141
.database(&database)
132142
.queries(queries)
@@ -145,6 +155,10 @@ async fn main() -> Result<()> {
145155
}
146156
}
147157

158+
// Reload the database to get analysis info
159+
database.reload()?;
160+
log::info!("CodeQL Database LoC :: {}", database.lines_of_code());
161+
148162
log::info!("Analysis complete :: {:?}", database);
149163
groupend!();
150164
}

0 commit comments

Comments
 (0)