-
Notifications
You must be signed in to change notification settings - Fork 53
/
verify.go
170 lines (135 loc) · 3.87 KB
/
verify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Command verifypr provides a tool to verify omni PRs against the conventional commit template.
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"regexp"
"strings"
cc "github.com/leodido/go-conventionalcommits"
"github.com/leodido/go-conventionalcommits/parser"
)
var (
descRegex = regexp.MustCompile(`^[a-z][-\w\s]+$`) // e.g. "add foo-bar"
scopeRegex = regexp.MustCompile(`^[*\w]+(/[*\w]+)?$`) // e.g. "*" or "foo" or "foo/bar"
)
// run runs the verification.
func run() error {
pr, err := prFromEnv()
if err != nil {
return err
}
// Skip dependabot PRs.
if strings.Contains(pr.Title, "deps") && strings.Contains(pr.Body, "dependabot") {
return nil
}
log.Printf("Verifying omni PR against template\n")
log.Printf("PR Title: %s\n", pr.Title)
log.Printf("## PR Body:\n%s\n####\n", pr.Body)
// Convert PR title and body to conventional commit message.
commitMsg := fmt.Sprintf("%s\n\n%s", pr.Title, pr.Body)
return verify(commitMsg)
}
type PR struct {
Title string `json:"title"`
Body string `json:"body"`
ID string `json:"node_id"`
}
// prFromEnv returns the PR by parsing it from "GITHUB_PR" env var or an error.
func prFromEnv() (PR, error) {
const prEnv = "GITHUB_PR"
prJSON, ok := os.LookupEnv(prEnv)
if !ok || strings.TrimSpace(prJSON) == "" {
return PR{}, errors.New("env variable not set")
}
var pr PR
if err := json.Unmarshal([]byte(prJSON), &pr); err != nil {
return PR{}, err
}
if pr.Title == "" || pr.Body == "" || pr.ID == "" {
return PR{}, errors.New("pr field not set")
}
return pr, nil
}
// verify returns an error if the commit message doesn't correspond to the omni conventional commit template.
func verify(commitMsg string) error {
// Fix line endings, since conventional commit parser doesn't support CRLF.
commitMsg = strings.ReplaceAll(commitMsg, "\r\n", "\n")
// Parse conventional commit message.
m := parser.NewMachine()
m.WithTypes(cc.TypesConventional)
msg, err := m.Parse([]byte(commitMsg))
if err != nil {
return fmt.Errorf("parse conventional commit message: %w", err)
}
commit, ok := msg.(*cc.ConventionalCommit)
if !ok {
return errors.New("message is not a conventional commit")
}
// Verify conventional commit message is valid.
if !commit.Ok() {
return errors.New("conventional commit not ok")
}
// Verify title is valid.
if err := verifyDescription(commit.Description); err != nil {
return err
}
// Verify body is non-empty.
if commit.Body == nil || *commit.Body == "" {
return errors.New("body empty")
}
// Verify footer is valid.
if err := verifyFooter(commit); err != nil {
return err
}
// Verify scope is valid.
if err := verifyScope(commit); err != nil {
return err
}
return nil
}
func verifyDescription(description string) error {
const maxLen = 50
if len(description) > maxLen {
return errors.New("description too long")
}
if !descRegex.MatchString(description) {
return errors.New("description doesn't match regex")
}
return nil
}
func verifyFooter(commit *cc.ConventionalCommit) error {
if len(commit.Footers) == 0 {
return errors.New("missing `task` footer")
}
if len(commit.Footers) > 1 {
return errors.New("invalid number of footers, only `task` required")
}
if len(commit.Footers["task"]) != 1 {
return errors.New("invalid number of task footers, only one required")
}
task := commit.Footers["task"][0]
if task == "" {
return errors.New("task footer empty")
} else if task == "none" {
// None is also fine
} else if !strings.HasPrefix(task, "https://app.asana.com/") {
return errors.New("only asana tasks allowed")
}
return nil
}
func verifyScope(commit *cc.ConventionalCommit) error {
if commit.Scope == nil {
return errors.New("scope not set")
}
scope := *commit.Scope
if scope == "" {
return errors.New("scope empty")
}
if !scopeRegex.MatchString(scope) {
return errors.New("scope doesn't match regex")
}
return nil
}