-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(CI): Check PR body and title. (#1786)
* chore(CI): Check PR body and title. * Trigger CI * Fix regex.
- Loading branch information
Showing
4 changed files
with
129 additions
and
1 deletion.
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
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) | ||
} | ||
} |
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,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 | ||
} |
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,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) | ||
} | ||
} | ||
} |