Skip to content

Commit

Permalink
feat: add support for conventional commit style PR titles
Browse files Browse the repository at this point in the history
This commit adds support for checking that a PR title is conventional commit style compliant.

Closes #296
  • Loading branch information
fallion committed Oct 11, 2021
1 parent d76466a commit 08545fd
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .commitsar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ verbose: true
commits:
strict: true
disabled: false
pull_request:
conventional: true
2 changes: 2 additions & 0 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions docs/content/configuration/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/prpipeline/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*package prpipeline handles all actions related to PR checking */
package prpipeline
27 changes: 27 additions & 0 deletions internal/prpipeline/pipeline.go
Original file line number Diff line number Diff line change
@@ -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"
}
50 changes: 50 additions & 0 deletions internal/prpipeline/run.go
Original file line number Diff line number Diff line change
@@ -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")
}
23 changes: 6 additions & 17 deletions internal/root_runner/run_pr.go → internal/prpipeline/title.go
Original file line number Diff line number Diff line change
@@ -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") {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions internal/prpipeline/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package prpipeline

type PRStyle = string

const (
JiraStyle PRStyle = "jira"
ConvetionalStyle PRStyle = "conventional"
)
33 changes: 22 additions & 11 deletions internal/root_runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 08545fd

Please sign in to comment.