Skip to content

Commit

Permalink
feat: add checking of PR title
Browse files Browse the repository at this point in the history
  • Loading branch information
fallion committed Jun 17, 2020
1 parent 440541b commit 367f626
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 2 deletions.
1 change: 1 addition & 0 deletions .commitsar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ version: 1
verbose: true
commits:
strict: true
disabled: false
10 changes: 8 additions & 2 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ jobs:
with:
go-version: 1.13
- name: Check out code into the Go module directory
uses: actions/checkout@v1

uses: actions/checkout@v2
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}
- run: go mod download
- name: Run Commitsar
run: go run main.go
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

golangci-lint:
name: runner / golangci-lint
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ require (
github.com/aevea/quoad v0.4.0
github.com/go-git/go-git/v5 v5.1.0
github.com/go-openapi/strfmt v0.19.5 // indirect
github.com/google/go-github/v32 v32.0.0
github.com/jedib0t/go-pretty v4.3.0+incompatible
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.0
github.com/stretchr/testify v1.6.1
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,17 @@ github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-github/v32 v32.0.0 h1:q74KVb22spUq0U5HqZ9VCYqQz8YRuOtL/39ZnfwO+NM=
github.com/google/go-github/v32 v32.0.0/go.mod h1:rIEpZD9CTDQwDK9GDrtMTycQNA4JU3qBsCizh3q2WCI=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down Expand Up @@ -315,6 +320,7 @@ golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3ob
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -369,6 +375,7 @@ google.golang.org/api v0.13.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsb
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.1 h1:QzqyMA1tlu6CgqCDUtU9V+ZKhLFT2dkJuANu5QaxI3I=
google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
Expand Down
20 changes: 20 additions & 0 deletions internal/root_runner/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package root_runner

import (
"errors"

"github.com/logrusorgru/aurora"
"github.com/spf13/viper"
)

Expand All @@ -16,5 +19,22 @@ func (runner *Runner) Run(options RunnerOptions, args ...string) error {
runner.Logger.Println("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 err != nil {
return err
}

if len(references) == 0 {
return errors.New("no references found in Pull Request title")
}

successMessage := aurora.Sprintf(aurora.Green("Success! Found the following JIRA issue references: %v \n"), references)

runner.Logger.Print(successMessage)
}

return nil
}
64 changes: 64 additions & 0 deletions internal/root_runner/run_pr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package root_runner

import (
"context"
"errors"
"io/ioutil"
"log"

"golang.org/x/oauth2"

"github.com/aevea/commitsar/pkg/jira"
history "github.com/aevea/git/v2"
"github.com/google/go-github/v32/github"
"github.com/spf13/viper"
)

// RunPullRequest starts the runner for the PullRequest pipeline
func (runner *Runner) RunPullRequest(jiraKeys []string) ([]string, error) {
ghToken := viper.GetString("GITHUB_TOKEN")

gitRepo, err := history.OpenGit(".", log.New(ioutil.Discard, "", 0))

if err != nil {
return nil, err
}

ctx := context.Background()

ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghToken},
)
tc := oauth2.NewClient(ctx, ts)

client := github.NewClient(tc)

currentCommit, err := gitRepo.CurrentCommit()

if err != nil {
return nil, err
}

prs, response, err := client.PullRequests.ListPullRequestsWithCommit(ctx, "aevea", "commitsar", currentCommit.Hash.String(), nil)

if err != nil {
return nil, err
}

if len(prs) == 0 {
runner.DebugLogger.Printf("current commit %s", currentCommit.Hash.String())
runner.DebugLogger.Print(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 references, nil
}
19 changes: 19 additions & 0 deletions www/docs/configuration/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,22 @@ commits:
| strict | true | Enforces strict category enforcement. |
| limit | none | Makes commitsar check only the last x commits. Useful if you want to run commitsar on master. |
| all | false | Makes commitsar check all the commits in history. **Overrides the `limit` flag** |

## Pull Request style settings

**Pull Request pipeline is still in early stages. Please report any bugs**

```yaml
pull_request:
jira_title: true
jira_keys:
- TEST
- TSLA
```

Setting `jira_title` to true will enable the pipeline. By default commitsar will use a basic regex to check for any JIRA-like references. Further scoping can be done using the `jira_keys` setting.

| Name | Default Value | Description |
| ---------- | ------------- | --------------------------------------------------------------------- |
| jira_title | false | Turns on the pipeline and will check for JIRA issues in the PR title. |
| jira_keys | none | Array of string project keys from JIRA. |
31 changes: 31 additions & 0 deletions www/docs/usage/github.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ id: github
title: Github Actions
---

## Important prefaces

### JIRA pipeline

When using [JIRA](https://commitsar.tech/docs/configuration/config-file) make sure to set `actions/checkout@v2` to pull from the pull_request HEAD. Github uses a single merge commit by default which is not referenced in git. This will cause commitsar JIRA check to fail as this commit will not be found by the API getting queried for PR by commit.

### actions/checkout@v2

When using `actions/checkout@v2` please set `fetch_depth` to 0. Currently commitsar needs full git objects to work correctly. This will be fixed in an upcoming release.

## Using the Github Action

A minimal example:
Expand Down Expand Up @@ -42,3 +52,24 @@ jobs:
- name: Commitsar check
uses: docker://aevea/commitsar
```

## Using with JIRA pipeline

This pipeline example uses the checkout at PR HEAD. <https://github.com/actions/checkout#Checkout-pull-request-HEAD-commit-instead-of-merge-commit>

```yaml
validate-commits:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13
- name: Check out code into the Go module directory
uses: actions/checkout@v2
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}
- name: Commitsar check
uses: docker://aevea/commitsar
```

1 comment on commit 367f626

@vercel
Copy link

@vercel vercel bot commented on 367f626 Jun 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.