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: use the pipeline model for commits
This commit allows us to start using pipelines inside the project. This still isn't the final form, but it is a good base to start.
- Loading branch information
Showing
15 changed files
with
239 additions
and
142 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
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
2 changes: 1 addition & 1 deletion
2
...nal/root_runner/commits_between_hashes.go → .../commitpipeline/commits_between_hashes.go
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,4 +1,4 @@ | ||
package root_runner | ||
package commitpipeline | ||
|
||
import ( | ||
"strings" | ||
|
2 changes: 1 addition & 1 deletion
2
...oot_runner/commits_between_hashes_test.go → ...itpipeline/commits_between_hashes_test.go
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,4 +1,4 @@ | ||
package root_runner | ||
package commitpipeline | ||
|
||
import ( | ||
"testing" | ||
|
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,2 @@ | ||
// Package commitpipeline handles all work related to commits | ||
package commitpipeline |
2 changes: 1 addition & 1 deletion
2
internal/root_runner/identify_same_branch.go → ...al/commitpipeline/identify_same_branch.go
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,4 +1,4 @@ | ||
package root_runner | ||
package commitpipeline | ||
|
||
import ( | ||
history "github.com/aevea/git/v2" | ||
|
6 changes: 3 additions & 3 deletions
6
internal/root_runner/log_branch.go → internal/commitpipeline/log_branch.go
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,16 +1,16 @@ | ||
package root_runner | ||
package commitpipeline | ||
|
||
import history "github.com/aevea/git/v2" | ||
|
||
// logBranch outputs the branch which is being checked into the console | ||
func (runner *Runner) logBranch(gitRepo *history.Git) error { | ||
func (pipeline *Pipeline) logBranch(gitRepo *history.Git) error { | ||
branch, err := gitRepo.CurrentBranch() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
runner.Logger.Printf("Starting analysis of commits on branch %s", branch.Name()) | ||
pipeline.Logger.Printf("Starting analysis of commits on branch %s", branch.Name()) | ||
|
||
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
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,56 @@ | ||
package commitpipeline | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
) | ||
|
||
type Pipeline struct { | ||
Logger *log.Logger | ||
DebugLogger *log.Logger | ||
args []string | ||
options Options | ||
} | ||
|
||
type Options struct { | ||
// Path to repository | ||
Path string | ||
// UpstreamBranch is the branch against which to check | ||
UpstreamBranch string | ||
// Limit will limit how far back to check on upstream branch. | ||
Limit int | ||
// AllCommits will check all the commits on the upstream branch. Regardless of Limit setting. | ||
AllCommits bool | ||
Strict bool | ||
} | ||
|
||
func New(logger, debugLogger *log.Logger, options *Options, args ...string) (*Pipeline, error) { | ||
if logger == nil { | ||
logger = log.New(os.Stdout, "", 0) | ||
} | ||
if debugLogger == nil { | ||
debugLogger = log.New(ioutil.Discard, "[DEBUG] ", 0) | ||
} | ||
|
||
if options == nil { | ||
options = &Options{ | ||
Path: ".", | ||
UpstreamBranch: "master", | ||
Limit: 0, | ||
AllCommits: false, | ||
Strict: true, | ||
} | ||
} | ||
|
||
return &Pipeline{ | ||
Logger: logger, | ||
DebugLogger: debugLogger, | ||
options: *options, | ||
args: args, | ||
}, nil | ||
} | ||
|
||
func (Pipeline) Name() string { | ||
return "commit-pipeline" | ||
} |
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,108 @@ | ||
package commitpipeline | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/aevea/commitsar/internal/dispatcher" | ||
"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 (pipeline *Pipeline) Run() (*dispatcher.PipelineSuccess, error) { | ||
gitRepo, err := history.OpenGit(pipeline.options.Path, pipeline.DebugLogger) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = pipeline.logBranch(gitRepo) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var commits []plumbing.Hash | ||
|
||
if len(pipeline.args) == 0 { | ||
commitsBetweenBranches, err := pipeline.commitsBetweenBranches(gitRepo) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
commits = commitsBetweenBranches | ||
} else { | ||
commitsBetweenHashes, err := commitsBetweenHashes(gitRepo, pipeline.args) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
commits = commitsBetweenHashes | ||
} | ||
|
||
var filteredCommits []plumbing.Hash | ||
|
||
for _, commitHash := range commits { | ||
commitObject, commitErr := gitRepo.Commit(commitHash) | ||
|
||
if commitErr != nil { | ||
return nil, commitErr | ||
} | ||
|
||
parsedCommit := quoad.ParseCommitMessage(commitObject.Message) | ||
|
||
pipeline.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) | ||
} | ||
} | ||
|
||
pipeline.Logger.Printf("\n%v commits filtered out\n", len(commits)-len(filteredCommits)) | ||
pipeline.Logger.Printf("\nFound %v commit to check\n", len(filteredCommits)) | ||
|
||
if len(filteredCommits) == 0 { | ||
return nil, 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 nil, commitErr | ||
} | ||
|
||
parsedCommit := quoad.ParseCommitMessage(commitObject.Message) | ||
|
||
textErr := text.CheckMessageTitle(parsedCommit, pipeline.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() | ||
|
||
pipeline.Logger.Printf("%v of %v commits are not conventional commit compliant\n", aurora.Red(len(faultyCommits)), aurora.Red(len(commits))) | ||
|
||
pipeline.Logger.Print("\nExpected format is for example: chore(ci): this is a test\n") | ||
pipeline.Logger.Print("Please see https://www.conventionalcommits.org for help on how to structure commits\n\n") | ||
|
||
return nil, errors.New(aurora.Red("Not all commits are conventional commits, please check the commits listed above").String()) | ||
} | ||
|
||
return &dispatcher.PipelineSuccess{ | ||
Message: aurora.Sprintf(aurora.Green("All %v commits are conventional commit compliant\n"), len(filteredCommits)), | ||
PipelineName: pipeline.Name(), | ||
}, 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
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
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
Oops, something went wrong.