Skip to content

Commit

Permalink
support more options in upload-to-github subcommand (#353)
Browse files Browse the repository at this point in the history
- support configuration for fail_on_violations
- support parent flags in upload-to-github command
  • Loading branch information
chdsbd authored Jun 13, 2024
1 parent f0dba18 commit a7d59a9
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 35 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## v1.1.0 - 2024-06-13

### Changed

- support configuration for "fail_on_violations" via `upload_to_github.fail_on_violations` (#353)
- support root flags within upload-to-github subcommand (#353)

## v1.0.0 - 2024-06-11

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "squawk"
version = "1.0.0"
version = "1.1.0"
authors = ["Steve Dignam <steve@dignam.xyz>"]
edition = "2018"
license = "GPL-3.0"
Expand Down
18 changes: 18 additions & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ impl std::fmt::Display for ConfigError {
}
}

#[derive(Debug, Default, Deserialize)]
pub struct UploadToGitHubConfig {
#[serde(default)]
pub fail_on_violations: Option<bool>,
}

#[derive(Debug, Default, Deserialize)]
pub struct Config {
#[serde(default)]
Expand All @@ -46,6 +52,8 @@ pub struct Config {
pub pg_version: Option<Version>,
#[serde(default)]
pub assume_in_transaction: Option<bool>,
#[serde(default)]
pub upload_to_github: UploadToGitHubConfig,
}

impl Config {
Expand Down Expand Up @@ -148,4 +156,14 @@ assume_in_transaction = false
fs::write(&squawk_toml, file).expect("Unable to write file");
assert_debug_snapshot!(Config::parse(Some(squawk_toml.path().to_path_buf())));
}
#[test]
fn test_load_fail_on_violations() {
let squawk_toml = NamedTempFile::new().expect("generate tempFile");
let file = r#"
[upload_to_github]
fail_on_violations = true
"#;
fs::write(&squawk_toml, file).expect("Unable to write file");
assert_debug_snapshot!(Config::parse(Some(squawk_toml.path().to_path_buf())));
}
}
25 changes: 16 additions & 9 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct Opt {
/// --exclude-path=005_user_ids.sql --exclude-path=009_account_emails.sql
///
/// --exclude-path='*user_ids.sql'
#[structopt(long = "exclude-path")]
#[structopt(long = "exclude-path", global = true)]
excluded_path: Option<Vec<String>>,
/// Exclude specific warnings
///
Expand All @@ -58,14 +58,15 @@ struct Opt {
short = "e",
long = "exclude",
value_name = "rule",
use_delimiter = true
use_delimiter = true,
global = true
)]
excluded_rules: Option<Vec<RuleViolationKind>>,
/// Specify postgres version
///
/// For example:
/// --pg-version=13.0
#[structopt(long)]
#[structopt(long, global = true)]
pg_version: Option<Version>,
/// List all available rules
#[structopt(long)]
Expand All @@ -85,17 +86,22 @@ struct Opt {
#[structopt(subcommand)]
cmd: Option<Command>,
/// Enable debug logging output
#[structopt(long)]
#[structopt(long, global = true)]
verbose: bool,
/// Path to the squawk config file (.squawk.toml)
#[structopt(short = "c", long = "config")]
#[structopt(short = "c", long = "config", global = true)]
config_path: Option<PathBuf>,
/// Assume that a transaction will wrap each SQL file when run by a migration tool
///
/// Use --no-assume-in-transaction to override any config file that sets this
#[structopt(long)]
#[structopt(long, global = true)]
assume_in_transaction: bool,
#[structopt(long, hidden = true, conflicts_with = "assume-in-transaction")]
#[structopt(
long,
hidden = true,
conflicts_with = "assume-in-transaction",
global = true
)]
no_assume_in_transaction: bool,
}

Expand Down Expand Up @@ -124,14 +130,14 @@ fn main() {
let excluded_rules = if let Some(excluded_rules) = opts.excluded_rules {
excluded_rules
} else {
conf.excluded_rules
conf.excluded_rules.clone()
};

// the --exclude-path flag completely overrides the configuration file.
let excluded_paths = if let Some(excluded_paths) = opts.excluded_path {
excluded_paths
} else {
conf.excluded_paths
conf.excluded_paths.clone()
};
let pg_version = if let Some(pg_version) = opts.pg_version {
Some(pg_version)
Expand Down Expand Up @@ -175,6 +181,7 @@ fn main() {
exit(
check_and_comment_on_pr(
subcommand,
&conf,
is_stdin,
opts.stdin_filepath,
&excluded_rules,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: cli/src/config.rs
expression: "Config::parse(Some(squawk_toml.path().to_path_buf()))"

---
Ok(
Some(
Expand All @@ -12,6 +11,9 @@ Ok(
assume_in_transaction: Some(
false,
),
upload_to_github: UploadToGitHubConfig {
fail_on_violations: None,
},
},
),
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: cli/src/config.rs
expression: "Config::parse(Some(squawk_toml.path().to_path_buf()))"

---
Ok(
Some(
Expand All @@ -24,6 +23,9 @@ Ok(
assume_in_transaction: Some(
true,
),
upload_to_github: UploadToGitHubConfig {
fail_on_violations: None,
},
},
),
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: cli/src/config.rs
expression: "Config::parse(Some(squawk_toml.path().to_path_buf()))"

---
Ok(
Some(
Expand All @@ -12,6 +11,9 @@ Ok(
excluded_rules: [],
pg_version: None,
assume_in_transaction: None,
upload_to_github: UploadToGitHubConfig {
fail_on_violations: None,
},
},
),
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: cli/src/config.rs
expression: "Config::parse(Some(squawk_toml.path().to_path_buf()))"

---
Ok(
Some(
Expand All @@ -12,6 +11,9 @@ Ok(
],
pg_version: None,
assume_in_transaction: None,
upload_to_github: UploadToGitHubConfig {
fail_on_violations: None,
},
},
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: cli/src/config.rs
expression: "Config::parse(Some(squawk_toml.path().to_path_buf()))"
---
Ok(
Some(
Config {
excluded_paths: [],
excluded_rules: [],
pg_version: None,
assume_in_transaction: None,
upload_to_github: UploadToGitHubConfig {
fail_on_violations: Some(
true,
),
},
},
),
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: cli/src/config.rs
expression: "Config::parse(Some(squawk_toml.path().to_path_buf()))"

---
Ok(
Some(
Expand All @@ -18,6 +17,9 @@ Ok(
},
),
assume_in_transaction: None,
upload_to_github: UploadToGitHubConfig {
fail_on_violations: None,
},
},
),
)
31 changes: 14 additions & 17 deletions cli/src/subcommand.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![allow(clippy::too_many_arguments)]
use crate::config::Config;
use crate::{
file_finding::{find_paths, FindFilesError},
reporter::{check_files, get_comment_body, CheckFilesError},
};

use log::info;
use squawk_github::{actions, app, comment_on_pr, GitHubApi, GithubError};
use squawk_linter::{versions::Version, violations::RuleViolationKind};
Expand Down Expand Up @@ -83,12 +84,6 @@ pub enum Command {
UploadToGithub {
/// Paths to search
paths: Vec<String>,
/// Exclude specific warnings
///
/// For example:
/// --exclude=require-concurrent-index-creation,ban-drop-database
#[structopt(short, long, use_delimiter = true)]
exclude: Option<Vec<RuleViolationKind>>,
/// Exits with an error if violations are found
#[structopt(long)]
fail_on_violations: bool,
Expand Down Expand Up @@ -132,11 +127,6 @@ fn get_github_private_key(
}
}

fn concat(a: &[RuleViolationKind], b: &[RuleViolationKind]) -> Vec<RuleViolationKind> {
// from: https://stackoverflow.com/a/53476705/3720597
[a, b].concat()
}

fn create_gh_app(
github_install_id: Option<i64>,
github_app_id: Option<i64>,
Expand Down Expand Up @@ -166,16 +156,16 @@ fn create_gh_app(

pub fn check_and_comment_on_pr(
cmd: Command,
cfg: &Config,
is_stdin: bool,
stdin_path: Option<String>,
root_cmd_exclude: &[RuleViolationKind],
root_cmd_exclude_paths: &[String],
exclude: &[RuleViolationKind],
exclude_paths: &[String],
pg_version: Option<Version>,
assume_in_transaction: bool,
) -> Result<(), SquawkError> {
let Command::UploadToGithub {
paths,
exclude,
fail_on_violations,
github_private_key,
github_token,
Expand All @@ -187,6 +177,13 @@ pub fn check_and_comment_on_pr(
github_private_key_base64,
} = cmd;

let fail_on_violations =
if let Some(fail_on_violations_cfg) = cfg.upload_to_github.fail_on_violations {
fail_on_violations_cfg
} else {
fail_on_violations
};

let github_app = create_gh_app(
github_install_id,
github_app_id,
Expand All @@ -195,14 +192,14 @@ pub fn check_and_comment_on_pr(
github_private_key_base64,
)?;

let found_paths = find_paths(&paths, root_cmd_exclude_paths)?;
let found_paths = find_paths(&paths, exclude_paths)?;

info!("checking files");
let file_results = check_files(
&found_paths,
is_stdin,
stdin_path,
&concat(&exclude.unwrap_or_default(), root_cmd_exclude),
exclude,
pg_version,
assume_in_transaction,
)?;
Expand Down
2 changes: 2 additions & 0 deletions docs/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ excluded_paths = [
"005_user_ids.sql",
"*user_ids.sql",
]
[upload_to_github]
fail_on_violations = true
```


Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{
squawk = final.rustPlatform.buildRustPackage {
pname = "squawk";
version = "1.0.0";
version = "1.1.0";

cargoLock = {
lockFile = ./Cargo.lock;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "squawk-cli",
"version": "1.0.0",
"version": "1.1.0",
"description": "linter for PostgreSQL, focused on migrations",
"repository": "git@github.com:sbdchd/squawk.git",
"author": "Steve Dignam <steve@dignam.xyz>",
Expand Down

0 comments on commit a7d59a9

Please sign in to comment.