Skip to content

Commit

Permalink
Improve result message
Browse files Browse the repository at this point in the history
Signed-off-by: Taiki Ono <taiki@finatext.com>
  • Loading branch information
taiki45 committed Mar 28, 2024
1 parent c968555 commit 2c6b793
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-annotation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
- name: Test apply
env:
REPORT_PATH: tmp/report.json
GUIDE: "see more detail at https://example.com"
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
shell: bash
# Test data: secret_key = deadbeef

Check warning on line 40 in .github/workflows/test-annotation.yml

View workflow job for this annotation

GitHub Actions / Test GitHub annotation

[gls] reported by reviewdog 🐶 `test` rule finds possible secret: deadbeef see more detail at https://example.com Raw Output: {"locations":[{"physicalLocation":{"artifactLocation":{"uri":".github/workflows/test-annotation.yml","uriBaseId":""},"region":{"startLine":40,"startColumn":23,"endLine":40,"endColumn":43}}}],"message":{"text":"`test` rule finds possible secret: deadbeef\nsee more detail at https://example.com"},"ruleId":"test"}
Expand Down
5 changes: 4 additions & 1 deletion src/cli/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ pub fn apply(args: ApplyArgs) -> Result {
writeln!(out, "{}", serde_json::to_string_pretty(&confirmed)?).with_context(msg_f)?;
}
Format::Sarif => {
let guide = args
.guide
.map_or_else(String::new, |guide| format!("\n{guide}"));
// SARIF doesn't contain `line` field, so pass original Finding-s.
let s = to_sarif(result.confirmed)?;
let s = to_sarif(result.confirmed, &guide)?;
writeln!(out, "{s}").with_context(msg_f)?;
}
Format::Github => {
Expand Down
59 changes: 30 additions & 29 deletions src/sarif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,36 +115,37 @@ struct Properties {
tags: Vec<String>,
}

impl From<Finding> for SarifResult {
fn from(finding: Finding) -> Self {
Self {
message: Message {
text: finding.description,
},
rule_id: finding.rule_id,
locations: vec![Location {
physical_location: PhysicalLocation {
artifact_location: ArtifactLocation { uri: finding.file },
region: Region {
start_line: finding.start_line,
start_column: finding.start_column,
end_line: finding.end_line,
end_column: finding.end_column,
snippet: Snippet {
text: finding.secret,
},
fn to_result(finding: Finding, guide: &str) -> SarifResult {
SarifResult {
message: Message {
text: format!(
"`{}` rule finds possible secret: {}{guide}",
finding.rule_id, finding.secret
),
},
rule_id: finding.rule_id,
locations: vec![Location {
physical_location: PhysicalLocation {
artifact_location: ArtifactLocation { uri: finding.file },
region: Region {
start_line: finding.start_line,
start_column: finding.start_column,
end_line: finding.end_line,
end_column: finding.end_column,
snippet: Snippet {
text: finding.secret,
},
},
}],
partial_fingerprints: PartialFingerprints {
commit_sha: finding.commit,
commit_message: finding.message,
email: finding.email,
author: finding.author,
date: finding.date,
},
properties: Properties { tags: finding.tags },
}
}],
partial_fingerprints: PartialFingerprints {
commit_sha: finding.commit,
commit_message: finding.message,
email: finding.email,
author: finding.author,
date: finding.date,
},
properties: Properties { tags: finding.tags },
}
}

Expand All @@ -154,7 +155,7 @@ const DRIVER_NAME: &str = "gls";
const DRIVER_SEMANTIC_VERSION: &str = "v0.0.0"; // TODO: embed version
const DRIVER_INFORMATION_URI: &str = "https://github.com/Finatext/gls";

pub fn to_sarif(findings: Vec<Finding>) -> anyhow::Result<String> {
pub fn to_sarif(findings: Vec<Finding>, guide: &str) -> anyhow::Result<String> {
let rules: HashSet<&str> = findings.iter().fold(HashSet::new(), |mut acc, finding| {
acc.insert(&finding.rule_id);
acc
Expand Down Expand Up @@ -183,7 +184,7 @@ pub fn to_sarif(findings: Vec<Finding>) -> anyhow::Result<String> {
rules,
},
},
results: findings.into_iter().map(Into::into).collect(),
results: findings.into_iter().map(|f| to_result(f, guide)).collect(),
}],
};

Expand Down

0 comments on commit 2c6b793

Please sign in to comment.