diff --git a/cmd/pint/ci.go b/cmd/pint/ci.go index c122bedb..188ef224 100644 --- a/cmd/pint/ci.go +++ b/cmd/pint/ci.go @@ -19,6 +19,8 @@ import ( "github.com/urfave/cli/v2" ) +var baseBranchFlag = "base-branch" + var ciCmd = &cli.Command{ Name: "ci", Usage: "Lint CI changes", @@ -30,6 +32,12 @@ var ciCmd = &cli.Command{ Value: false, Usage: "Require all rules to have an owner set via comment", }, + &cli.StringFlag{ + Name: baseBranchFlag, + Aliases: []string{"b"}, + Value: "", + Usage: "Set base branch to use for PR checks (main, master, ...)", + }, }, } @@ -45,6 +53,9 @@ func actionCI(c *cli.Context) error { } baseBranch := strings.Split(meta.cfg.CI.BaseBranch, "/")[len(strings.Split(meta.cfg.CI.BaseBranch, "/"))-1] + if c.String(baseBranchFlag) != "" { + baseBranch = c.String(baseBranchFlag) + } currentBranch, err := git.CurrentBranch(git.RunGit) if err != nil { return fmt.Errorf("failed to get the name of current branch") diff --git a/cmd/pint/tests/0082_ci_base_branch_flag.txt b/cmd/pint/tests/0082_ci_base_branch_flag.txt new file mode 100644 index 00000000..e1ba4a75 --- /dev/null +++ b/cmd/pint/tests/0082_ci_base_branch_flag.txt @@ -0,0 +1,33 @@ +mkdir testrepo +cd testrepo +exec git init --initial-branch=main . + +cp ../src/v1.yml rules.yml +cp ../src/.pint.hcl . +env GIT_AUTHOR_NAME=pint +env GIT_AUTHOR_EMAIL=pint@example.com +env GIT_COMMITTER_NAME=pint +env GIT_COMMITTER_EMAIL=pint@example.com +exec git add . +exec git commit -am 'import rules and config' + +pint.ok --no-color ci --base-branch=main +! stdout . +cmp stderr ../stderr.txt + +-- stderr.txt -- +level=info msg="Loading configuration file" path=.pint.hcl +level=info msg="Running from base branch, skipping checks" branch=main +-- src/v1.yml -- +- record: rule1 + expr: sum(foo) by(job) +- record: rule2 + expr: sum(foo) bi(job) + +-- src/.pint.hcl -- +ci { + baseBranch = "foo" +} +parser { + relaxed = [".*"] +} diff --git a/docs/changelog.md b/docs/changelog.md index 5b605237..39ee11d1 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -6,6 +6,10 @@ - Fixed line reporting on some strict parser errors. +### Added + +- Added `--base-branch` flag to `pint ci` command. + ## v0.24.0 ### Added