diff --git a/.commitsar.yaml b/.commitsar.yaml index 0710ae68..4e08cf6e 100644 --- a/.commitsar.yaml +++ b/.commitsar.yaml @@ -3,3 +3,5 @@ verbose: true commits: strict: true disabled: false +pull_request: + conventional: true diff --git a/.github/workflows/linters.yml b/.github/workflows/linters.yml index b2c13954..168ceae2 100644 --- a/.github/workflows/linters.yml +++ b/.github/workflows/linters.yml @@ -24,6 +24,8 @@ jobs: - run: go mod download - name: Run Commitsar run: go run main.go + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} golangci-lint: name: runner / golangci-lint diff --git a/docs/content/configuration/config-file.md b/docs/content/configuration/config-file.md index 564de565..2d30f623 100644 --- a/docs/content/configuration/config-file.md +++ b/docs/content/configuration/config-file.md @@ -50,6 +50,21 @@ commits: **Pull Request pipeline is still in early stages. Please report any bugs** +#### Convetional style + +```yaml +pull_request: + conventional: true +``` + +Setting `conventional` to true will enable the pipeline. This is useful for teams that use squash commits and don't care about having all of the commits in the PR compliant with conventional commits. + +| Name | Default Value | Description | Available from | +| ------------ | ------------- | ----------------------------------------------------------------------- | -------------- | +| conventional | false | Turns on the pipeline and will check for a conventional commit PR title | v0.17.0 | + +#### JIRA style + ```yaml pull_request: jira_title: true diff --git a/internal/prpipeline/docs.go b/internal/prpipeline/docs.go new file mode 100644 index 00000000..e39abc58 --- /dev/null +++ b/internal/prpipeline/docs.go @@ -0,0 +1,2 @@ +/*package prpipeline handles all actions related to PR checking */ +package prpipeline diff --git a/internal/prpipeline/pipeline.go b/internal/prpipeline/pipeline.go new file mode 100644 index 00000000..225ea62c --- /dev/null +++ b/internal/prpipeline/pipeline.go @@ -0,0 +1,27 @@ +package prpipeline + +type Options struct { + // Path to the git repository + Path string + // Style is the style of PR title to enforce + Style PRStyle + // Keys checks for required keys in the PR title + Keys []string +} + +// Pip +type Pipeline struct { + options Options +} + +func New(options Options) (*Pipeline, error) { + if options.Path == "" { + options.Path = "." + } + + return &Pipeline{options: options}, nil +} + +func (pipeline *Pipeline) Name() string { + return "pr-pipeline" +} diff --git a/internal/prpipeline/run.go b/internal/prpipeline/run.go new file mode 100644 index 00000000..cf2df67c --- /dev/null +++ b/internal/prpipeline/run.go @@ -0,0 +1,50 @@ +package prpipeline + +import ( + "errors" + + "github.com/aevea/commitsar/internal/dispatcher" + "github.com/aevea/commitsar/pkg/jira" + "github.com/aevea/commitsar/pkg/text" + "github.com/aevea/quoad" + "github.com/logrusorgru/aurora" +) + +func (pipeline *Pipeline) Run() (*dispatcher.PipelineSuccess, error) { + title, err := getPRTitle(pipeline.options.Path) + + if err != nil { + return nil, err + } + + switch pipeline.options.Style { + case JiraStyle: + references, err := jira.FindReferences(pipeline.options.Keys, *title) + + if err != nil { + return nil, err + } + + if len(references) > 0 { + return &dispatcher.PipelineSuccess{ + PipelineName: pipeline.Name(), + Message: aurora.Sprintf(aurora.Green("Success! Found the following JIRA issue references: %v"), references), + }, nil + } + case ConvetionalStyle: + commit := quoad.ParseCommitMessage(*title) + + err := text.CheckMessageTitle(commit, true) + + if err != nil { + return nil, err + } + + return &dispatcher.PipelineSuccess{ + PipelineName: pipeline.Name(), + Message: aurora.Sprintf(aurora.Green("Success! PR title is compliant with conventional commit")), + }, nil + } + + return nil, errors.New("pr checking is configured, but no style has been chosen") +} diff --git a/internal/root_runner/run_pr.go b/internal/prpipeline/title.go similarity index 70% rename from internal/root_runner/run_pr.go rename to internal/prpipeline/title.go index 87b2ce9c..352bfac9 100644 --- a/internal/root_runner/run_pr.go +++ b/internal/prpipeline/title.go @@ -1,21 +1,18 @@ -package root_runner +package prpipeline import ( "context" "errors" "strings" - "golang.org/x/oauth2" - - "github.com/aevea/commitsar/pkg/jira" history "github.com/aevea/git/v3" "github.com/apex/log" "github.com/google/go-github/v32/github" "github.com/spf13/viper" + "golang.org/x/oauth2" ) -// RunPullRequest starts the runner for the PullRequest pipeline -func (runner *Runner) RunPullRequest(jiraKeys []string) ([]string, error) { +func getPRTitle(path string) (*string, error) { ghToken := viper.GetString("GITHUB_TOKEN") if !viper.IsSet("GITHUB_REPOSITORY") { @@ -24,7 +21,7 @@ func (runner *Runner) RunPullRequest(jiraKeys []string) ([]string, error) { split := strings.Split(viper.GetString("GITHUB_REPOSITORY"), "/") - gitRepo, err := history.OpenGit(".") + gitRepo, err := history.OpenGit(path) if err != nil { return nil, err @@ -55,16 +52,8 @@ func (runner *Runner) RunPullRequest(jiraKeys []string) ([]string, error) { log.Debugf("current commit %s", currentCommit.Hash.String()) log.Debugf("%s", response) - return nil, errors.New("No linked PullRequests found") - } - - title := prs[0].Title - - references, err := jira.FindReferences(jiraKeys, *title) - - if err != nil { - return nil, err + return nil, errors.New("no linked PullRequests found") } - return references, nil + return prs[0].Title, nil } diff --git a/internal/prpipeline/types.go b/internal/prpipeline/types.go new file mode 100644 index 00000000..84fe6d78 --- /dev/null +++ b/internal/prpipeline/types.go @@ -0,0 +1,8 @@ +package prpipeline + +type PRStyle = string + +const ( + JiraStyle PRStyle = "jira" + ConvetionalStyle PRStyle = "conventional" +) diff --git a/internal/root_runner/run.go b/internal/root_runner/run.go index 08269fe5..b3541c50 100644 --- a/internal/root_runner/run.go +++ b/internal/root_runner/run.go @@ -5,8 +5,8 @@ import ( "github.com/aevea/commitsar/internal/commitpipeline" "github.com/aevea/commitsar/internal/dispatcher" + "github.com/aevea/commitsar/internal/prpipeline" "github.com/apex/log" - "github.com/logrusorgru/aurora" "github.com/spf13/viper" ) @@ -37,27 +37,38 @@ func (runner *Runner) Run(options RunnerOptions, args ...string) error { log.Info("Commit section skipped due to commits.disabled set to true in .commitsar.yaml") } - if viper.GetBool("pull_request.jira_title") { - jiraKeys := viper.GetStringSlice("pull_request.jira_keys") - references, err := runner.RunPullRequest(jiraKeys) + if viper.IsSet("pull_request") { + log.Debug("PR pipeline") - if err != nil { - return err + prOptions := prpipeline.Options{ + Path: options.Path, } - if len(references) == 0 { - return errors.New("No JIRA references found in Pull Request title") + if viper.IsSet("pull_request.jira_title") { + prOptions.Style = prpipeline.JiraStyle + prOptions.Keys = viper.GetStringSlice("pull_request.jira_keys") } - successMessage := aurora.Sprintf(aurora.Green("Success! Found the following JIRA issue references: %v"), references) + if viper.IsSet("pull_request.conventional") { + prOptions.Style = prpipeline.ConvetionalStyle + } - log.Info(successMessage) + prPipe, err := prpipeline.New(prOptions) + + if err != nil { + return err + } + + pipelines = append(pipelines, prPipe) } result := dispatch.RunPipelines(pipelines) if len(result.Errors) != 0 { - return errors.New("Some errors were found") + for _, err := range result.Errors { + log.Errorf("pipeline: %s, error: %s", err.PipelineName, err.Error) + } + return errors.New("some errors were found") } for _, successMessage := range result.SuccessfulPipelines {