Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/squawk/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const FILE_NAME: &str = ".squawk.toml";
pub struct UploadToGitHubConfig {
#[serde(default)]
pub fail_on_violations: Option<bool>,
#[serde(default)]
pub only_comment_on_violations: Option<bool>,
}

#[derive(Debug, Default, Deserialize)]
Expand Down Expand Up @@ -258,6 +260,16 @@ fail_on_violations = true
assert_debug_snapshot!(ConfigFile::parse(Some(squawk_toml.path().to_path_buf())));
}
#[test]
fn load_only_comment_on_violations() {
let squawk_toml = NamedTempFile::new().expect("generate tempFile");
let file = r"
[upload_to_github]
only_comment_on_violations = true
";
fs::write(&squawk_toml, file).expect("Unable to write file");
assert_debug_snapshot!(ConfigFile::parse(Some(squawk_toml.path().to_path_buf())));
}
#[test]
fn load_included_rules() {
let squawk_toml = NamedTempFile::new().expect("generate tempFile");
let file = r#"
Expand Down
33 changes: 29 additions & 4 deletions crates/squawk/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub fn check_and_comment_on_pr(cfg: Config) -> Result<()> {
let UploadToGithubArgs {
paths,
fail_on_violations,
only_comment_on_violations,
github_private_key,
github_api_url,
github_token,
Expand All @@ -106,6 +107,14 @@ pub fn check_and_comment_on_pr(cfg: Config) -> Result<()> {
fail_on_violations
};

let only_comment_on_violations = if let Some(only_comment_on_violations_cfg) =
cfg.upload_to_github.only_comment_on_violations
{
only_comment_on_violations_cfg
} else {
only_comment_on_violations
};

let github_app = create_gh_app(
&github_api_url,
&github_install_id,
Expand Down Expand Up @@ -133,8 +142,16 @@ pub fn check_and_comment_on_pr(cfg: Config) -> Result<()> {
info!("no files checked, exiting");
return Ok(());
}
info!("generating github comment body");
let comment_body = get_comment_body(&file_results, VERSION);

let violations: usize = file_results.iter().map(|f| f.violations.len()).sum();

let comment_body = if only_comment_on_violations && violations == 0 {
info!("no violations found, posting clean summary comment");
get_clean_comment_body(file_results.len())
} else {
info!("generating github comment body");
get_comment_body(&file_results, VERSION)
};

comment_on_pr(
github_app.as_ref(),
Expand All @@ -151,8 +168,6 @@ pub fn check_and_comment_on_pr(cfg: Config) -> Result<()> {
fmt_github_annotations(&mut handle, &file_results)?;
}

let violations: usize = file_results.iter().map(|f| f.violations.len()).sum();

if fail_on_violations && violations > 0 {
let files = file_results.len();
bail!("Found {violations} violation(s) across {files} file(s)");
Expand All @@ -161,6 +176,10 @@ pub fn check_and_comment_on_pr(cfg: Config) -> Result<()> {
Ok(())
}

fn get_clean_comment_body(file_count: usize) -> String {
format!("{COMMENT_HEADER}\n\n✅ 0 violations across {file_count} file(s)")
}

fn get_comment_body(files: &[CheckReport], version: &str) -> String {
let violations_count: usize = files.iter().map(|x| x.violations.len()).sum();
let violations_emoji = get_violations_emoji(violations_count);
Expand Down Expand Up @@ -428,6 +447,12 @@ ALTER TABLE "core_recipe" ADD COLUMN "foo" integer DEFAULT 10;
assert_snapshot!(body);
}

#[test]
fn generating_clean_comment_no_violations() {
let body = super::get_clean_comment_body(8);
assert_snapshot!(body);
}

/// Ideally the logic won't leave a comment when there are no migrations but
/// better safe than sorry
#[test]
Expand Down
3 changes: 3 additions & 0 deletions crates/squawk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub struct UploadToGithubArgs {
/// Exits with an error if violations are found
#[arg(long)]
fail_on_violations: bool,
/// Only posts a report comment on violations
#[arg(long)]
only_comment_on_violations: bool,
#[arg(long, env = "SQUAWK_GITHUB_PRIVATE_KEY")]
github_private_key: Option<String>,
#[arg(long, env = "SQUAWK_GITHUB_PRIVATE_KEY_BASE64")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Ok(
),
upload_to_github: UploadToGitHubConfig {
fail_on_violations: None,
only_comment_on_violations: None,
},
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Ok(
),
upload_to_github: UploadToGitHubConfig {
fail_on_violations: None,
only_comment_on_violations: None,
},
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Ok(
assume_in_transaction: None,
upload_to_github: UploadToGitHubConfig {
fail_on_violations: None,
only_comment_on_violations: None,
},
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Ok(
assume_in_transaction: None,
upload_to_github: UploadToGitHubConfig {
fail_on_violations: None,
only_comment_on_violations: None,
},
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Ok(
assume_in_transaction: None,
upload_to_github: UploadToGitHubConfig {
fail_on_violations: None,
only_comment_on_violations: None,
},
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Ok(
fail_on_violations: Some(
true,
),
only_comment_on_violations: None,
},
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Ok(
assume_in_transaction: None,
upload_to_github: UploadToGitHubConfig {
fail_on_violations: None,
only_comment_on_violations: None,
},
},
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/squawk/src/config.rs
expression: "ConfigFile::parse(Some(squawk_toml.path().to_path_buf()))"
---
Ok(
Some(
ConfigFile {
excluded_paths: [],
excluded_rules: [],
included_rules: [],
pg_version: None,
assume_in_transaction: None,
upload_to_github: UploadToGitHubConfig {
fail_on_violations: None,
only_comment_on_violations: Some(
true,
),
},
},
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Ok(
assume_in_transaction: None,
upload_to_github: UploadToGitHubConfig {
fail_on_violations: None,
only_comment_on_violations: None,
},
},
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: crates/squawk/src/github.rs
expression: body
---
# Squawk Report

✅ 0 violations across 8 file(s)
1 change: 1 addition & 0 deletions docs/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ excluded_paths = [
]
[upload_to_github]
fail_on_violations = true
only_comment_on_violations = true
```

See the [Squawk website](https://squawkhq.com/docs/rules) for documentation on each rule with examples and reasoning.
Expand Down
20 changes: 20 additions & 0 deletions docs/docs/github_app.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,23 @@ To use Squawk as a GitHub App, Squawk needs a corresponding GitHub App so it can

<https://github.com/sbdchd/squawk/pull/14#issuecomment-647009446>

## Only comment on violations

By default, Squawk posts a PR comment on every run. To suppress the full report
when there are no violations, pass `--only-comment-on-violations` to the
`upload-to-github` subcommand:

```bash
squawk upload-to-github --only-comment-on-violations example.sql
```

Or set it in `.squawk.toml`:

```toml
[upload_to_github]
only_comment_on_violations = true
```

When no violations are found, Squawk will post a brief ✅ summary instead of
the full report.

Loading