Skip to content

Commit 96703d3

Browse files
committed
fix: Update SARIF patching
1 parent ea24236 commit 96703d3

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

src/extractors.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,39 @@ pub async fn fetch_extractor(
149149
Ok(extractor_pack)
150150
}
151151

152+
/// Update the SARIF file with the extractor information (CodeQL ${language})
153+
///
154+
/// Update only the `runs.0.tool.driver` section of the SARIF file
155+
pub fn update_sarif(path: &PathBuf, extractor: String) -> Result<()> {
156+
let sarif_content =
157+
std::fs::read_to_string(path).context(format!("Failed to read SARIF file: {:?}", path))?;
158+
let mut sarif_json: serde_json::Value = serde_json::from_str(&sarif_content)
159+
.context(format!("Failed to parse SARIF file: {:?}", path))?;
160+
161+
log::debug!("SARIF JSON :: {sarif_json:#?}");
162+
if let Some(tool) = sarif_json
163+
.get_mut("runs")
164+
.and_then(|runs| runs.get_mut(0))
165+
.and_then(|run| run.get_mut("tool"))
166+
{
167+
if let Some(driver) = tool.get_mut("driver") {
168+
driver["name"] = serde_json::Value::String(format!("CodeQL - {}", extractor));
169+
log::info!("Updated SARIF file with extractor: {extractor}");
170+
} else {
171+
log::warn!("No 'driver' field found in SARIF file");
172+
}
173+
} else {
174+
log::warn!("No 'runs' or 'tool' field found in SARIF file");
175+
}
176+
177+
let data = serde_json::to_string(&sarif_json)
178+
.context(format!("Failed to serialize SARIF JSON: {:?}", path))?;
179+
// Write the updated SARIF back to the file
180+
std::fs::write(path, data)
181+
.context(format!("Failed to write SARIF file: {:?}", path))?;
182+
Ok(())
183+
}
184+
152185
/// Update the permissions for tool scripts (*.sh) and the extractor (extractor)
153186
fn update_tools_permisisons(path: &PathBuf) -> Result<()> {
154187
let tools_path = path.join("tools");

src/main.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::{Context, Result};
22
use ghactions::{ActionTrait, group, groupend};
33
use ghactions_core::RepositoryReference;
44
use ghastoolkit::prelude::*;
5-
use ghastoolkit::{Sarif, codeql::database::queries::CodeQLQueries};
5+
use ghastoolkit::{codeql::database::queries::CodeQLQueries};
66
use log::{debug, info};
77

88
mod action;
@@ -189,24 +189,8 @@ async fn main() -> Result<()> {
189189

190190
log::info!("Post-processing SARIF results");
191191

192-
match Sarif::try_from(sarif_path.clone()) {
193-
Ok(mut sarif) => {
194-
log::info!("Updating SARIF tool name for language: {language}");
195-
sarif.runs.iter_mut().for_each(|run| {
196-
run.tool.driver.name = format!("CodeQL - {language}");
197-
});
198-
199-
log::debug!("Writing SARIF file to {sarif_path:?}");
200-
if let Err(e) = std::fs::write(&sarif_path, serde_json::to_string(&sarif)?) {
201-
log::error!("Failed to write SARIF file: {e}");
202-
} else {
203-
log::info!("SARIF file written successfully: {sarif_path:?}");
204-
}
205-
}
206-
Err(e) => {
207-
log::error!("Failed to read and parse SARIF file: {e}");
208-
}
209-
}
192+
extractors::update_sarif(&sarif_path, extractor.display_name.clone())
193+
.context("Failed to update SARIF file with extractor information")?;
210194

211195
// Reload the database to get analysis info
212196
database.reload()?;

0 commit comments

Comments
 (0)