podman
102 строки · 3.1 Кб
1package main
2
3import (
4"flag"
5"fmt"
6"os"
7"strings"
8
9"github.com/sirupsen/logrus"
10_ "github.com/vbatts/git-validation/rules/danglingwhitespace"
11_ "github.com/vbatts/git-validation/rules/dco"
12_ "github.com/vbatts/git-validation/rules/messageregexp"
13_ "github.com/vbatts/git-validation/rules/shortsubject"
14"github.com/vbatts/git-validation/validate"
15)
16
17var (
18flCommitRange = flag.String("range", "", "use this commit range instead (implies -no-travis)")
19flListRules = flag.Bool("list-rules", false, "list the rules registered")
20flRun = flag.String("run", "", "comma delimited list of rules to run. Defaults to all.")
21flVerbose = flag.Bool("v", false, "verbose")
22flDebug = flag.Bool("D", false, "debug output")
23flQuiet = flag.Bool("q", false, "less output")
24flDir = flag.String("d", ".", "git directory to validate from")
25flNoGithub = flag.Bool("no-github", false, "disables Github Actions environment checks (when env GITHUB_ACTIONS=true is set)")
26flNoTravis = flag.Bool("no-travis", false, "disables travis environment checks (when env TRAVIS=true is set)")
27flTravisPROnly = flag.Bool("travis-pr-only", true, "when on travis, only run validations if the CI-Build is checking pull-request build")
28)
29
30func init() {
31logrus.SetOutput(os.Stderr)
32}
33
34func main() {
35flag.Parse()
36
37if *flDebug {
38os.Setenv("DEBUG", "1")
39}
40if *flQuiet {
41os.Setenv("QUIET", "1")
42}
43
44if *flListRules {
45for _, r := range validate.RegisteredRules {
46fmt.Printf("%q -- %s\n", r.Name, r.Description)
47}
48return
49}
50
51if *flTravisPROnly && strings.ToLower(os.Getenv("TRAVIS_PULL_REQUEST")) == "false" {
52fmt.Printf("only to check travis PR builds and this not a PR build. yielding.\n")
53return
54}
55
56// rules to be used
57var rules []validate.Rule
58for _, r := range validate.RegisteredRules {
59// only those that are Default
60if r.Default {
61rules = append(rules, r)
62}
63}
64// or reduce the set being run to what the user provided
65if *flRun != "" {
66rules = validate.FilterRules(validate.RegisteredRules, validate.SanitizeFilters(*flRun))
67}
68if os.Getenv("DEBUG") != "" {
69logrus.Printf("%#v", rules) // XXX maybe reduce this list
70}
71
72var commitRange = *flCommitRange
73if commitRange == "" {
74if strings.ToLower(os.Getenv("TRAVIS")) == "true" && !*flNoTravis {
75if os.Getenv("TRAVIS_COMMIT_RANGE") != "" {
76commitRange = strings.Replace(os.Getenv("TRAVIS_COMMIT_RANGE"), "...", "..", 1)
77} else if os.Getenv("TRAVIS_COMMIT") != "" {
78commitRange = os.Getenv("TRAVIS_COMMIT")
79}
80}
81// https://docs.github.com/en/actions/reference/environment-variables
82if strings.ToLower(os.Getenv("GITHUB_ACTIONS")) == "true" && !*flNoGithub {
83commitRange = fmt.Sprintf("%s..%s", os.Getenv("GITHUB_SHA"), "HEAD")
84}
85}
86
87logrus.Infof("using commit range: %s", commitRange)
88runner, err := validate.NewRunner(*flDir, rules, commitRange, *flVerbose)
89if err != nil {
90logrus.Fatal(err)
91}
92
93if err := runner.Run(); err != nil {
94logrus.Fatal(err)
95}
96_, fail := runner.Results.PassFail()
97if fail > 0 {
98fmt.Printf("%d commits to fix\n", fail)
99os.Exit(1)
100}
101
102}
103