generated from fallion/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow commit checks to be skipped
The configuration option was set, it was however not functional.
- Loading branch information
Showing
2 changed files
with
110 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,20 @@ | ||
package root_runner | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/aevea/commitsar/pkg/text" | ||
history "github.com/aevea/git/v2" | ||
"github.com/aevea/quoad" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
"github.com/logrusorgru/aurora" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// Run executes the base command for Commitsar | ||
func (runner *Runner) Run(options RunnerOptions, args ...string) error { | ||
gitRepo, err := history.OpenGit(options.Path, runner.DebugLogger) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
err = runner.logBranch(gitRepo) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
var commits []plumbing.Hash | ||
|
||
if len(args) == 0 { | ||
commitsBetweenBranches, err := commitsBetweenBranches(gitRepo, options) | ||
if !viper.GetBool("commits.disabled") { | ||
err := runner.runCommits(options, args...) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
commits = commitsBetweenBranches | ||
} else { | ||
commitsBetweenHashes, err := commitsBetweenHashes(gitRepo, args) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
commits = commitsBetweenHashes | ||
runner.Logger.Println("Commit section skipped due to commits.disabled set to true in .commitsar.yaml") | ||
} | ||
|
||
var filteredCommits []plumbing.Hash | ||
|
||
for _, commitHash := range commits { | ||
commitObject, commitErr := gitRepo.Commit(commitHash) | ||
|
||
if commitErr != nil { | ||
return commitErr | ||
} | ||
|
||
parsedCommit := quoad.ParseCommitMessage(commitObject.Message) | ||
|
||
runner.DebugLogger.Printf("Commit found: [hash] %v [message] %v \n", parsedCommit.Hash.String(), parsedCommit.Heading) | ||
|
||
if !text.IsMergeCommit(commitObject.Message) && !text.IsInitialCommit(commitObject.Message) { | ||
filteredCommits = append(filteredCommits, commitHash) | ||
} | ||
} | ||
|
||
runner.Logger.Printf("\n%v commits filtered out\n", len(commits)-len(filteredCommits)) | ||
runner.Logger.Printf("\nFound %v commit to check\n", len(filteredCommits)) | ||
|
||
if len(filteredCommits) == 0 { | ||
return errors.New(aurora.Red("No commits found, please check you are on a branch outside of main").String()) | ||
} | ||
|
||
var faultyCommits []text.FailingCommit | ||
|
||
for _, commitHash := range filteredCommits { | ||
commitObject, commitErr := gitRepo.Commit(commitHash) | ||
|
||
if commitErr != nil { | ||
return commitErr | ||
} | ||
|
||
parsedCommit := quoad.ParseCommitMessage(commitObject.Message) | ||
|
||
textErr := text.CheckMessageTitle(parsedCommit, options.Strict) | ||
|
||
if textErr != nil { | ||
faultyCommits = append(faultyCommits, text.FailingCommit{Hash: commitHash.String(), Message: parsedCommit.Heading, Error: textErr}) | ||
} | ||
} | ||
|
||
if len(faultyCommits) != 0 { | ||
failingCommitTable := text.FormatFailingCommits(faultyCommits) | ||
failingCommitTable.SetOutputMirror(os.Stdout) | ||
failingCommitTable.Render() | ||
|
||
runner.Logger.Printf("%v of %v commits are not conventional commit compliant\n", aurora.Red(len(faultyCommits)), aurora.Red(len(commits))) | ||
|
||
runner.Logger.Print("\nExpected format is for example: chore(ci): this is a test\n") | ||
runner.Logger.Print("Please see https://www.conventionalcommits.org for help on how to structure commits\n\n") | ||
|
||
return errors.New(aurora.Red("Not all commits are conventional commits, please check the commits listed above").String()) | ||
} | ||
|
||
runner.Logger.Print(aurora.Sprintf(aurora.Green("All %v commits are conventional commit compliant\n"), len(filteredCommits))) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package root_runner | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/aevea/commitsar/pkg/text" | ||
history "github.com/aevea/git/v2" | ||
"github.com/aevea/quoad" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
"github.com/logrusorgru/aurora" | ||
) | ||
|
||
func (runner *Runner) runCommits(options RunnerOptions, args ...string) error { | ||
gitRepo, err := history.OpenGit(options.Path, runner.DebugLogger) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
err = runner.logBranch(gitRepo) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
var commits []plumbing.Hash | ||
|
||
if len(args) == 0 { | ||
commitsBetweenBranches, err := commitsBetweenBranches(gitRepo, options) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
commits = commitsBetweenBranches | ||
} else { | ||
commitsBetweenHashes, err := commitsBetweenHashes(gitRepo, args) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
commits = commitsBetweenHashes | ||
} | ||
|
||
var filteredCommits []plumbing.Hash | ||
|
||
for _, commitHash := range commits { | ||
commitObject, commitErr := gitRepo.Commit(commitHash) | ||
|
||
if commitErr != nil { | ||
return commitErr | ||
} | ||
|
||
parsedCommit := quoad.ParseCommitMessage(commitObject.Message) | ||
|
||
runner.DebugLogger.Printf("Commit found: [hash] %v [message] %v \n", parsedCommit.Hash.String(), parsedCommit.Heading) | ||
|
||
if !text.IsMergeCommit(commitObject.Message) && !text.IsInitialCommit(commitObject.Message) { | ||
filteredCommits = append(filteredCommits, commitHash) | ||
} | ||
} | ||
|
||
runner.Logger.Printf("\n%v commits filtered out\n", len(commits)-len(filteredCommits)) | ||
runner.Logger.Printf("\nFound %v commit to check\n", len(filteredCommits)) | ||
|
||
if len(filteredCommits) == 0 { | ||
return errors.New(aurora.Red("No commits found, please check you are on a branch outside of main").String()) | ||
} | ||
|
||
var faultyCommits []text.FailingCommit | ||
|
||
for _, commitHash := range filteredCommits { | ||
commitObject, commitErr := gitRepo.Commit(commitHash) | ||
|
||
if commitErr != nil { | ||
return commitErr | ||
} | ||
|
||
parsedCommit := quoad.ParseCommitMessage(commitObject.Message) | ||
|
||
textErr := text.CheckMessageTitle(parsedCommit, options.Strict) | ||
|
||
if textErr != nil { | ||
faultyCommits = append(faultyCommits, text.FailingCommit{Hash: commitHash.String(), Message: parsedCommit.Heading, Error: textErr}) | ||
} | ||
} | ||
|
||
if len(faultyCommits) != 0 { | ||
failingCommitTable := text.FormatFailingCommits(faultyCommits) | ||
failingCommitTable.SetOutputMirror(os.Stdout) | ||
failingCommitTable.Render() | ||
|
||
runner.Logger.Printf("%v of %v commits are not conventional commit compliant\n", aurora.Red(len(faultyCommits)), aurora.Red(len(commits))) | ||
|
||
runner.Logger.Print("\nExpected format is for example: chore(ci): this is a test\n") | ||
runner.Logger.Print("Please see https://www.conventionalcommits.org for help on how to structure commits\n\n") | ||
|
||
return errors.New(aurora.Red("Not all commits are conventional commits, please check the commits listed above").String()) | ||
} | ||
|
||
runner.Logger.Print(aurora.Sprintf(aurora.Green("All %v commits are conventional commit compliant\n"), len(filteredCommits))) | ||
|
||
return nil | ||
} |
c6b921f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to following URLs: