Skip to content

Commit

Permalink
chore(CI): Check PR body and title. (ChainSafe#1786)
Browse files Browse the repository at this point in the history
* chore(CI): Check PR body and title.

* Trigger CI

* Fix regex.
  • Loading branch information
arijitAD committed Sep 17, 2021
1 parent 0064836 commit bd4e06d
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .github/PULL_REQUEST/pull_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"os"

"github.com/ChainSafe/gossamer/lib/utils"
)

func main() {
title := os.Getenv("RAW_TITLE")
body := os.Getenv("RAW_BODY")
err := utils.CheckPRDescription(title, body)
if err != nil {
os.Exit(1)
}
}
15 changes: 14 additions & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,17 @@ jobs:
- uses: actions/checkout@v2

- name: Run go vet
run: go vet ./...
run: go vet ./...

check-description:
name: Checks PR has title and body description
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Checks PR has title and body description
run: |
go run .github/PULL_REQUEST/pull_request.go
env:
RAW_TITLE: ${{ github.event.pull_request.title }}
RAW_BODY: ${{ github.event.pull_request.body }}
44 changes: 44 additions & 0 deletions lib/utils/pull_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package utils

import (
"fmt"
"regexp"
"strings"
)

const (
titleRegex = `[A-Za-z]+\([A-Za-z/]+\):.+[A-Za-z]+`
bodyRegex = `## Changes.*- .*[A-Za-z0-9].*## Tests.*[A-Za-z].*## Issues.*- .*[A-Za-z0-9].*## Primary Reviewer.*- @.+[A-Za-z0-9].*`
)

// CheckPRDescription matches the PR title and body according to the PR template.
func CheckPRDescription(title, body string) error {
match, err := regexp.MatchString(titleRegex, title)
if err != nil || !match {
return fmt.Errorf("title pattern is not valid: %w match %t", err, match)
}

var bodyData string
// Remove comment from PR body.
for {
start := strings.Index(body, "<!--")
end := strings.Index(body, "-->")
if start < 0 || end < 0 {
break
}

bodyData = bodyData + body[:start]
body = body[end+4:]
}
bodyData = bodyData + body

lineSplit := strings.Split(bodyData, "\n")
joinedLine := strings.Join(lineSplit, "")

// Regex for body data
match, err = regexp.MatchString(bodyRegex, joinedLine)
if err != nil || !match {
return fmt.Errorf("body pattern is not valid: %w match %t", err, match)
}
return nil
}
55 changes: 55 additions & 0 deletions lib/utils/pull_request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package utils

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_PR_Checks(t *testing.T) {
tests := []struct {
title string
body string
valid bool
}{
{
title: "",
body: "",
valid: false,
},
{
title: "abc(abc): abc",
body: "",
valid: false,
},
{
title: `feat(dot/rpc): implement chain_subscribeAllHeads RPC`,
body: `## Changes\n\n<!--\nPlease provide a brief but specific list of changes made, describe the changes\nin functionality rather than the changes in code.\n-->\n\n- changes for demo :123\n\n## Tests\n\n<!--\nDetails on how to run tests relevant to the changes within this pull request.\n-->\n\n- tests for demo:123{}\n\n## Issues\n\n<!--\nPlease link any issues that this pull request is related to and use the GitHub\nsupported format for automatically closing issues (ie, closes #123, fixes #123)\n-->\n\n- issues for demo:43434\n\n## Primary Reviewer\n\n<!--\nPlease indicate one of the code owners that are required to review prior to merging changes (e.g. @noot)\n-->\n\n- @noot for demo:12`,
valid: true,
},
{
title: "abc(): abc",
body: "",
valid: false,
},
{
title: "(abc): abc",
body: "",
valid: false,
},
{
title: "abc(abc):",
body: "",
valid: false,
},
}

for _, test := range tests {
err := CheckPRDescription(test.title, test.body)
if test.valid {
require.NoError(t, err, "title", test.title, "body", test.body)
} else {
require.Error(t, err, "title", test.title, "body", test.body)
}
}
}

0 comments on commit bd4e06d

Please sign in to comment.