From d0cacea39a3f9fcaaac9cf2cda0aa830b5758385 Mon Sep 17 00:00:00 2001 From: Zlatko Bratkovic Date: Thu, 27 Nov 2025 17:18:55 +0100 Subject: [PATCH] MEDIUM: add support for junit file format reporting --- .aspell.yml | 1 + aspell/aspell.go | 5 ++- aspell/aspell_test.go | 4 +- aspell_test.go | 3 +- check.go | 64 +++++++++++++++++++++++-------- check_different_policy_test.go | 3 +- check_test.go | 4 +- go.mod | 1 + go.sum | 2 + junit/junit.go | 14 +++++++ main.go | 69 ++++++++++++++++++++++++++++------ 11 files changed, 138 insertions(+), 32 deletions(-) create mode 100644 junit/junit.go diff --git a/.aspell.yml b/.aspell.yml index 3fbae90..a673475 100644 --- a/.aspell.yml +++ b/.aspell.yml @@ -36,3 +36,4 @@ allowed: - url - english - lang + - junit diff --git a/aspell/aspell.go b/aspell/aspell.go index 79adfed..d873eeb 100644 --- a/aspell/aspell.go +++ b/aspell/aspell.go @@ -9,6 +9,7 @@ import ( "sort" "strings" + "github.com/haproxytech/check-commit/v5/junit" "github.com/haproxytech/check-commit/v5/match" "github.com/fatih/camelcase" @@ -121,7 +122,7 @@ func (a Aspell) checkSingle(data string, allowedWords []string) error { return nil } -func (a Aspell) Check(subjects []string, commitsFull []string, content []map[string]string) error { +func (a Aspell) Check(subjects []string, commitsFull []string, content []map[string]string, junitSuite junit.Interface) error { var commitsFullData []string for _, c := range commitsFull { commit := []string{} @@ -171,6 +172,7 @@ func (a Aspell) Check(subjects []string, commitsFull []string, content []map[str imports = match.GetImportWordsFromGoFile(name) } if err := a.checkSingle(v, imports); err != nil { + junitSuite.AddMessageFailed(name, "aspell check failed", err.Error()) log.Println(name, err.Error()) response += fmt.Sprintf("%s\n", err) } @@ -183,6 +185,7 @@ func (a Aspell) Check(subjects []string, commitsFull []string, content []map[str for _, subject := range checks { if err := a.checkSingle(subject, []string{}); err != nil { + junitSuite.AddMessageFailed("commit message", "aspell check failed", err.Error()) log.Println("commit message", err.Error()) response += fmt.Sprintf("%s\n", err) } diff --git a/aspell/aspell_test.go b/aspell/aspell_test.go index 92af5bf..97a8553 100644 --- a/aspell/aspell_test.go +++ b/aspell/aspell_test.go @@ -2,6 +2,8 @@ package aspell import ( "testing" + + "github.com/haproxytech/check-commit/v5/junit" ) func Test_checkWithAspell(t *testing.T) { @@ -107,7 +109,7 @@ func TestAspell_Check(t *testing.T) { AllowedWords: tt.fields.AllowedWords, HelpText: tt.fields.HelpText, } - if err := a.Check(tt.args.subjects, tt.args.commitsFull, tt.args.content); (err != nil) != tt.wantErr { + if err := a.Check(tt.args.subjects, tt.args.commitsFull, tt.args.content, &junit.JunitSuiteDummy{}); (err != nil) != tt.wantErr { t.Errorf("Aspell.Check() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/aspell_test.go b/aspell_test.go index e609ff9..58be702 100644 --- a/aspell_test.go +++ b/aspell_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/haproxytech/check-commit/v5/aspell" + "github.com/haproxytech/check-commit/v5/junit" ) func Test_Aspell(t *testing.T) { @@ -35,7 +36,7 @@ func Test_Aspell(t *testing.T) { } err = aspell.Check([]string{"subject"}, []string{"body"}, []map[string]string{ {filename: readme}, - }) + }, &junit.JunitSuiteDummy{}) if err != nil { t.Errorf("checkWithAspell() error = %v", err) } diff --git a/check.go b/check.go index f300ad3..5f7e9a7 100644 --- a/check.go +++ b/check.go @@ -16,6 +16,7 @@ import ( "unicode/utf8" "github.com/google/go-github/v56/github" + "github.com/haproxytech/check-commit/v5/junit" gitlab "gitlab.com/gitlab-org/api/client-go" git "github.com/go-git/go-git/v5" @@ -90,24 +91,33 @@ TagOrder: var ErrSubjectMessageFormat = errors.New("invalid subject message format") -func checkSubjectText(subject string) error { +func checkSubjectText(subject string, junitSuite junit.Interface) error { subjectLen := utf8.RuneCountInString(subject) subjectParts := strings.Fields(subject) subjectPartsLen := len(subjectParts) if subject != strings.Join(subjectParts, " ") { + junitSuite.AddMessageFailed(ErrSubjectMessageFormat.Error(), "malformatted subject string (trailing or double spaces?)", fmt.Sprintf("subject: %s", subject)) return fmt.Errorf( "malformatted subject string (trailing or double spaces?): '%s' (%w)", subject, ErrSubjectMessageFormat) } if subjectPartsLen < MINSUBJECTPARTS || subjectPartsLen > MAXSUBJECTPARTS { + junitSuite.AddMessageFailed( + ErrSubjectMessageFormat.Error(), + fmt.Sprintf("subject word count out of bounds [words %d < %d < %d]", MINSUBJECTPARTS, subjectPartsLen, MAXSUBJECTPARTS), + fmt.Sprintf("subject: %s", subject)) return fmt.Errorf( "subject word count out of bounds [words %d < %d < %d] '%s': %w", MINSUBJECTPARTS, subjectPartsLen, MAXSUBJECTPARTS, subjectParts, ErrSubjectMessageFormat) } if subjectLen < MINSUBJECTLEN || subjectLen > MAXSUBJECTLEN { + junitSuite.AddMessageFailed( + ErrSubjectMessageFormat.Error(), + fmt.Sprintf("subject length out of bounds [len %d < %d < %d]", MINSUBJECTLEN, subjectLen, MAXSUBJECTLEN), + fmt.Sprintf("subject: %s", subject)) return fmt.Errorf( "subject length out of bounds [len %d < %d < %d] '%s': %w", MINSUBJECTLEN, subjectLen, MAXSUBJECTLEN, subject, ErrSubjectMessageFormat) @@ -128,6 +138,7 @@ func (c CommitPolicyConfig) CheckPatchTypes(tag, severity string, patchTypeName } if c.PatchTypes[patchTypeName].Scope == "" { + log.Printf("unable to verify severity %s without definitions", severity) break // subject has severity but there is no definition to verify it @@ -148,10 +159,11 @@ func (c CommitPolicyConfig) CheckPatchTypes(tag, severity string, patchTypeName var ErrTagScope = errors.New("invalid tag and or severity") -func (c CommitPolicyConfig) CheckSubject(rawSubject []byte) error { +func (c CommitPolicyConfig) CheckSubject(rawSubject []byte, junitSuite junit.Interface) error { // check for ascii-only before anything else for i := 0; i < len(rawSubject); i++ { if rawSubject[i] > unicode.MaxASCII { + junitSuite.AddMessageFailed("", "non-ascii characters detected in commit subject", fmt.Sprintf("subject: %s", rawSubject)) log.Printf("non-ascii characters detected in in subject:\n%s", hex.Dump(rawSubject)) return fmt.Errorf("non-ascii characters in commit subject: %w", ErrTagScope) @@ -174,6 +186,7 @@ func (c CommitPolicyConfig) CheckSubject(rawSubject []byte) error { submatch := r.FindSubmatchIndex(rawSubject) if len(submatch) == 0 { // no match if !tagOK { + junitSuite.AddMessageFailed("", "invalid or missing tag/severity in commit message", fmt.Sprintf("subject: %s", rawSubject)) log.Printf("unable to find match in %s\n", rawSubject) return fmt.Errorf("invalid tag or no tag found, searched through [%s]: %w", @@ -199,6 +212,7 @@ func (c CommitPolicyConfig) CheckSubject(rawSubject []byte) error { candidates = append(candidates, string(tagPart)) if !tagOK { + junitSuite.AddMessageFailed("", "invalid tag/severity in commit message", fmt.Sprintf("subject: %s", rawSubject)) log.Printf("unable to find match in %s\n", candidates) return fmt.Errorf("invalid tag or no tag found, searched through [%s]: %w", @@ -208,10 +222,11 @@ func (c CommitPolicyConfig) CheckSubject(rawSubject []byte) error { submatch := r.FindSubmatchIndex(rawSubject) if len(submatch) != 0 { // no match + junitSuite.AddMessageFailed("", "unprocessed tags detected in commit message", fmt.Sprintf("subject: %s", rawSubject)) return fmt.Errorf("detected unprocessed tags, %w", ErrTagScope) } - return checkSubjectText(string(rawSubject)) + return checkSubjectText(string(rawSubject), junitSuite) } func (c CommitPolicyConfig) IsEmpty() bool { @@ -268,7 +283,7 @@ func LoadCommitPolicy(filename string) (CommitPolicyConfig, error) { return commitPolicy, nil } -func getGithubCommitData() ([]string, []string, []map[string]string, error) { +func getGithubCommitData(junitSuite junit.Interface) ([]string, []string, []map[string]string, error) { token := os.Getenv("API_TOKEN") repo := os.Getenv("GITHUB_REPOSITORY") ref := os.Getenv("GITHUB_REF") @@ -285,6 +300,7 @@ func getGithubCommitData() ([]string, []string, []map[string]string, error) { if event == "pull_request" { repoSlice := strings.SplitN(repo, "/", 2) if len(repoSlice) < 2 { + junitSuite.AddMessageFailed("", "error fetching owner and project from repo", fmt.Sprintf("invalid repository format: %s", repo)) return nil, nil, nil, fmt.Errorf("error fetching owner and project from repo %s", repo) } owner := repoSlice[0] @@ -292,15 +308,18 @@ func getGithubCommitData() ([]string, []string, []map[string]string, error) { refSlice := strings.SplitN(ref, "/", 4) if len(refSlice) < 3 { - return nil, nil, nil, fmt.Errorf("error fetching pr from ref %s", ref) + junitSuite.AddMessageFailed("", "error fetching PR number from ref", fmt.Sprintf("invalid ref format: %s", ref)) + return nil, nil, nil, fmt.Errorf("error fetching PR from ref %s", ref) } prNo, err := strconv.Atoi(refSlice[2]) if err != nil { - return nil, nil, nil, fmt.Errorf("Error fetching pr number from %s: %w", refSlice[2], err) + junitSuite.AddMessageFailed("", "error fetching PR number from ref", fmt.Sprintf("invalid pr number: %s", refSlice[2])) + return nil, nil, nil, fmt.Errorf("Error fetching PR number from %s: %w", refSlice[2], err) } commits, _, err := githubClient.PullRequests.ListCommits(ctx, owner, project, prNo, &github.ListOptions{}) if err != nil { + junitSuite.AddMessageFailed("", "error fetching commits", err.Error()) return nil, nil, nil, fmt.Errorf("error fetching commits: %w", err) } @@ -315,6 +334,7 @@ func getGithubCommitData() ([]string, []string, []map[string]string, error) { } if len(l) > 1 { if l[1] != "" { + junitSuite.AddMessageFailed("", "empty line between subject and body is required", fmt.Sprintf("%s %s", hash, l[0])) return nil, nil, nil, fmt.Errorf("empty line between subject and body is required: %s %s", hash, l[0]) } } @@ -326,6 +346,7 @@ func getGithubCommitData() ([]string, []string, []map[string]string, error) { files, _, err := githubClient.PullRequests.ListFiles(ctx, owner, project, prNo, &github.ListOptions{}) if err != nil { + junitSuite.AddMessageFailed("", "error fetching files", err.Error()) return nil, nil, nil, fmt.Errorf("error fetching files: %w", err) } content := map[string]string{} @@ -339,13 +360,15 @@ func getGithubCommitData() ([]string, []string, []map[string]string, error) { } return subjects, messages, diffs, nil } else { + junitSuite.AddMessageFailed("", "unsupported event name", fmt.Sprintf("unsupported event name: %s", event)) return nil, nil, nil, fmt.Errorf("unsupported event name: %s", event) } } -func getLocalCommitData() ([]string, []string, []map[string]string, error) { +func getLocalCommitData(junitSuite junit.Interface) ([]string, []string, []map[string]string, error) { repo, err := git.PlainOpen(".") if err != nil { + junitSuite.AddMessageFailed("", "error opening local git repository", err.Error()) return nil, nil, nil, err } @@ -353,6 +376,7 @@ func getLocalCommitData() ([]string, []string, []map[string]string, error) { Order: git.LogOrderCommitterTime, }) if err != nil { + junitSuite.AddMessageFailed("", "error getting git log iterator", err.Error()) return nil, nil, nil, err } @@ -372,6 +396,7 @@ func getLocalCommitData() ([]string, []string, []map[string]string, error) { break } if err != nil { + junitSuite.AddMessageFailed("", "error iterating through git commits", err.Error()) return nil, nil, nil, err } if committer == "" { @@ -392,6 +417,7 @@ func getLocalCommitData() ([]string, []string, []map[string]string, error) { } if len(l) > 1 { if l[1] != "" { + junitSuite.AddMessageFailed("", "empty line between subject and body is required", fmt.Sprintf("%s %s", commitHash, l[0])) return nil, nil, nil, fmt.Errorf("empty line between subject and body is required: %s %s", commitHash, l[0]) } } @@ -409,6 +435,7 @@ func getLocalCommitData() ([]string, []string, []map[string]string, error) { tree2, _ := commit2.Tree() changes, err := object.DiffTree(tree2, tree1) if err != nil { + junitSuite.AddMessageFailed("", "error getting git commit changes", err.Error()) return nil, nil, nil, err } @@ -416,6 +443,7 @@ func getLocalCommitData() ([]string, []string, []map[string]string, error) { for _, change := range changes { patch, err := change.Patch() if err != nil { + junitSuite.AddMessageFailed("", "error getting git patch", err.Error()) return nil, nil, nil, err } for _, file := range patch.FilePatches() { @@ -455,7 +483,7 @@ func cleanGitPatch(patch string) string { return patch } -func getGitlabCommitData() ([]string, []string, []map[string]string, error) { +func getGitlabCommitData(junitSuite junit.Interface) ([]string, []string, []map[string]string, error) { gitlab_url := os.Getenv("CI_API_V4_URL") token := os.Getenv("API_TOKEN") mri := os.Getenv("CI_MERGE_REQUEST_IID") @@ -463,20 +491,24 @@ func getGitlabCommitData() ([]string, []string, []map[string]string, error) { gitlabClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(gitlab_url)) if err != nil { - log.Fatalf("Failed to create gitlab client: %v", err) + junitSuite.AddMessageFailed("", "failed to create gitlab client", err.Error()) + return nil, nil, nil, fmt.Errorf("failed to create gitlab client: %w", err) } mrIID, err := strconv.Atoi(mri) if err != nil { + junitSuite.AddMessageFailed("", "invalid merge request id", err.Error()) return nil, nil, nil, fmt.Errorf("invalid merge request id %s", mri) } projectID, err := strconv.Atoi(project) if err != nil { + junitSuite.AddMessageFailed("", "invalid project id", err.Error()) return nil, nil, nil, fmt.Errorf("invalid project id %s", project) } commits, _, err := gitlabClient.MergeRequests.GetMergeRequestCommits(projectID, mrIID, &gitlab.GetMergeRequestCommitsOptions{}) if err != nil { + junitSuite.AddMessageFailed("", "error fetching commits", err.Error()) return nil, nil, nil, fmt.Errorf("error fetching commits: %w", err) } @@ -489,6 +521,7 @@ func getGitlabCommitData() ([]string, []string, []map[string]string, error) { if len(l) > 0 { if len(l) > 1 { if l[1] != "" { + junitSuite.AddMessageFailed("", "empty line between subject and body is required", fmt.Sprintf("%s %s", hash, l[0])) return nil, nil, nil, fmt.Errorf("empty line between subject and body is required: %s %s", hash, l[0]) } } @@ -497,6 +530,7 @@ func getGitlabCommitData() ([]string, []string, []map[string]string, error) { messages = append(messages, c.Message) diff, _, err := gitlabClient.MergeRequests.ListMergeRequestDiffs(projectID, mrIID, &gitlab.ListMergeRequestDiffsOptions{}) if err != nil { + junitSuite.AddMessageFailed("", "error fetching commit changes", err.Error()) return nil, nil, nil, fmt.Errorf("error fetching commit changes: %w", err) } content := map[string]string{} @@ -513,25 +547,25 @@ func getGitlabCommitData() ([]string, []string, []map[string]string, error) { return subjects, messages, diffs, nil } -func getCommitData(repoEnv string) ([]string, []string, []map[string]string, error) { +func getCommitData(repoEnv string, junitSuite junit.Interface) ([]string, []string, []map[string]string, error) { if repoEnv == GITHUB { - return getGithubCommitData() + return getGithubCommitData(junitSuite) } else if repoEnv == GITLAB { - return getGitlabCommitData() + return getGitlabCommitData(junitSuite) } else if repoEnv == LOCAL { - return getLocalCommitData() + return getLocalCommitData(junitSuite) } return nil, nil, nil, fmt.Errorf("unrecognized git environment %s", repoEnv) } var ErrSubjectList = errors.New("subjects contain errors") -func (c CommitPolicyConfig) CheckSubjectList(subjects []string) error { +func (c CommitPolicyConfig) CheckSubjectList(subjects []string, junitSuite junit.Interface) error { errors := false for _, subject := range subjects { subject = strings.Trim(subject, "'") - if err := c.CheckSubject([]byte(subject)); err != nil { + if err := c.CheckSubject([]byte(subject), junitSuite); err != nil { log.Printf("%s, original subject message '%s'", err, subject) errors = true diff --git a/check_different_policy_test.go b/check_different_policy_test.go index 1d2af5f..a7a0d16 100644 --- a/check_different_policy_test.go +++ b/check_different_policy_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/haproxytech/check-commit/v5/junit" yaml "gopkg.in/yaml.v3" ) @@ -100,7 +101,7 @@ func TestDifferentPolicy(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() - if err := c.CheckSubject([]byte(tt.subject)); (err != nil) != tt.wantErr { + if err := c.CheckSubject([]byte(tt.subject), &junit.JunitSuiteDummy{}); (err != nil) != tt.wantErr { t.Errorf("checkSubject() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/check_test.go b/check_test.go index 9ae4801..fe4e2f1 100644 --- a/check_test.go +++ b/check_test.go @@ -2,6 +2,8 @@ package main import ( "testing" + + "github.com/haproxytech/check-commit/v5/junit" ) func TestCheckSubject(t *testing.T) { @@ -13,7 +15,7 @@ func TestCheckSubject(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() - if err := c.CheckSubject([]byte(tt.subject)); (err != nil) != tt.wantErr { + if err := c.CheckSubject([]byte(tt.subject), &junit.JunitSuiteDummy{}); (err != nil) != tt.wantErr { t.Errorf("checkSubject() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/go.mod b/go.mod index e4a257e..ff441cd 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/go-git/go-git/v5 v5.16.2 github.com/google/go-github/v56 v56.0.0 github.com/joho/godotenv v1.5.1 + github.com/oktalz/junit-report v0.0.0-20251126212431-230eb8c3b576 gitlab.com/gitlab-org/api/client-go v0.159.0 golang.org/x/oauth2 v0.32.0 gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index c1164f7..e64d8f3 100644 --- a/go.sum +++ b/go.sum @@ -66,6 +66,8 @@ github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxec github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/oktalz/junit-report v0.0.0-20251126212431-230eb8c3b576 h1:7E/JjSc8xXcQfTxedppSYfIOu7WjyU3MZ18/awsphls= +github.com/oktalz/junit-report v0.0.0-20251126212431-230eb8c3b576/go.mod h1:UgkN1O7/5uF/xpuaz5+WOBlFwqXU1qY41z5xRnizH34= github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k= github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY= github.com/pjbgf/sha1cd v0.4.0 h1:NXzbL1RvjTUi6kgYZCX3fPwwl27Q1LJndxtUDVfJGRY= diff --git a/junit/junit.go b/junit/junit.go new file mode 100644 index 0000000..31fadc0 --- /dev/null +++ b/junit/junit.go @@ -0,0 +1,14 @@ +package junit + +type Interface interface { + AddMessageFailed(name, message, details string) + AddMessageOK(name, message, details string) +} + +type JunitSuiteDummy struct{} + +func (j *JunitSuiteDummy) AddMessageFailed(name, message, details string) { +} + +func (j *JunitSuiteDummy) AddMessageOK(name, message, details string) { +} diff --git a/main.go b/main.go index 1340dea..9a42ed3 100644 --- a/main.go +++ b/main.go @@ -7,10 +7,14 @@ import ( "path" "github.com/haproxytech/check-commit/v5/aspell" + "github.com/haproxytech/check-commit/v5/junit" "github.com/haproxytech/check-commit/v5/version" "github.com/joho/godotenv" + junit_report "github.com/oktalz/junit-report" ) +var exitCode = 0 + func main() { _ = godotenv.Load(".env") err := version.Set() @@ -32,48 +36,89 @@ func main() { } } log.SetFlags(log.LstdFlags | log.Lshortfile) - var repoPath string + // JUNIT_FILE + ts := junit_report.NewTestSuites() + junitFile := os.Getenv("JUNIT_FILE") + var junitSuite junit.Interface + if junitFile != "" { + junitSuite = ts.GetOrCreateSuite("check-commit") + } else { + junitSuite = &junit.JunitSuiteDummy{} + } + start(junitSuite) + if junitFile != "" { + if exitCode == 0 { + junitSuite.AddMessageOK("check-commit", "check-commit completed successfully", "") + } + log.Printf("JUNIT_FILE is set to %s\n", junitFile) + err := ts.Write(junitFile) + if err != nil { + log.Fatalf("failed to save junit report: %s", err) + } + } + os.Exit(exitCode) +} + +func start(junitSuite junit.Interface) { + var repoPath string if len(os.Args) < requiredCmdlineArgs { repoPath = "." } else { repoPath = os.Args[1] } - aspellCheck, err := aspell.New(path.Join(repoPath, ".aspell.yml")) + aspellConfigFile := path.Join(repoPath, ".aspell.yml") + aspellCheck, err := aspell.New(aspellConfigFile) if err != nil { - log.Fatalf("error reading aspell exceptions: %s", err) + junitSuite.AddMessageFailed(".aspell.yml", "error reading aspell configuration", err.Error()) + log.Printf("error reading aspell configuration: %s", err) + exitCode = 1 + return } commitPolicy, err := LoadCommitPolicy(path.Join(repoPath, ".check-commit.yml")) if err != nil { - log.Fatalf("error reading configuration: %s", err) + junitSuite.AddMessageFailed(".check-commit.yml", "error reading configuration", err.Error()) + log.Printf("error reading configuration: %s", err) + exitCode = 1 + return } if commitPolicy.IsEmpty() { + junitSuite.AddMessageOK("", "using empty configuration", "") log.Printf("WARNING: using empty configuration (i.e. no verification)") } gitEnv, err := readGitEnvironment() if err != nil { - log.Fatalf("couldn't auto-detect running environment, please set GITHUB_REF and GITHUB_BASE_REF manually: %s", err) + junitSuite.AddMessageFailed("", "couldn't auto-detect running environment, please set GITHUB_REF and GITHUB_BASE_REF manually", err.Error()) + log.Printf("couldn't auto-detect running environment, please set GITHUB_REF and GITHUB_BASE_REF manually: %s", err) + exitCode = 1 + return } - subjects, messages, content, err := getCommitData(gitEnv) + subjects, messages, content, err := getCommitData(gitEnv, junitSuite) if err != nil { - log.Fatalf("error getting commit data: %s", err) + log.Printf("error getting commit data: %s", err) + exitCode = 1 + return } - if err := commitPolicy.CheckSubjectList(subjects); err != nil { - log.Printf("encountered one or more commit message errors\n") - log.Fatalf("%s\n", commitPolicy.HelpText) + if err := commitPolicy.CheckSubjectList(subjects, junitSuite); err != nil { + junitSuite.AddMessageFailed("commit subject check", "commit subject policy violation", commitPolicy.HelpText) + log.Printf("%s\n", commitPolicy.HelpText) + exitCode = 1 + return } - err = aspellCheck.Check(subjects, messages, content) + err = aspellCheck.Check(subjects, messages, content, junitSuite) if err != nil { log.Printf("encountered one or more commit message spelling errors\n") // log.Fatalf("%s\n", err) - log.Fatalf("%s\n", aspellCheck.HelpText) + log.Printf("%s\n", aspellCheck.HelpText) + exitCode = 1 + return } log.Printf("check completed without errors\n")