Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit 9e51828

Browse files
committed
BUG/MAJOR: do not allow messages without any valid tags
1 parent d8ed339 commit 9e51828

File tree

4 files changed

+181
-53
lines changed

4 files changed

+181
-53
lines changed

check-commit/check.go

+6
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ func (c CommitPolicyConfig) CheckSubject(rawSubject []byte) error {
169169

170170
submatch := r.FindSubmatchIndex(rawSubject)
171171
if len(submatch) == 0 { // no match
172+
if !tagOK {
173+
log.Printf("unable to find match in %s\n", rawSubject)
174+
175+
return fmt.Errorf("invalid tag or no tag found, searched through [%s]: %w",
176+
strings.Join(tagAlternative.PatchTypes, ", "), ErrTagScope)
177+
}
172178
continue
173179
}
174180

check-commit/check_data_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
var tests = []struct {
4+
name string
5+
subject string
6+
wantErr bool
7+
}{
8+
{
9+
name: "valid type and severity",
10+
subject: "BUG/MEDIUM: config: add default location of path to the configuration file",
11+
wantErr: false,
12+
},
13+
{
14+
name: "invalid type and ok severity err front",
15+
subject: "aaaBUG/MEDIUM: config: add default location of path to the configuration file",
16+
wantErr: true,
17+
},
18+
{
19+
name: "invalid type and ok severity err back",
20+
subject: "BUG/MEDIUMaaa: config: add default location of path to the configuration file",
21+
wantErr: true,
22+
},
23+
{
24+
name: "short subject",
25+
subject: "BUG/MEDIUM: config: default",
26+
wantErr: true,
27+
},
28+
{
29+
name: "missing severity",
30+
subject: "BUG/: config: default implementation",
31+
wantErr: true,
32+
},
33+
{
34+
name: "only severity",
35+
subject: "MINOR: config: default implementation",
36+
wantErr: false,
37+
},
38+
{
39+
name: "wrong tag",
40+
subject: "WRONG: config: default implementation",
41+
wantErr: true,
42+
},
43+
{
44+
name: "wrong severity",
45+
subject: "BUG/WRONG: config: default implementation",
46+
wantErr: true,
47+
},
48+
{
49+
name: "double spaces",
50+
subject: "BUG/MEDIUM: config: default implementation",
51+
wantErr: true,
52+
},
53+
{
54+
name: "trailing spaces",
55+
subject: "BUG/MEDIUM: config: default implementation ",
56+
wantErr: true,
57+
},
58+
{
59+
name: "unprocessed tags remain",
60+
subject: "BUG/MINOR: MAJOR: config: default implementation",
61+
wantErr: true,
62+
},
63+
}
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
yaml "gopkg.in/yaml.v2"
8+
)
9+
10+
const customConf = `
11+
---
12+
HelpText: "Please refer to https://github.com/haproxy/haproxy/blob/master/CONTRIBUTING#L632"
13+
PatchScopes:
14+
HAProxy Standard Scope:
15+
- MINOR
16+
- MEDIUM
17+
- MAJOR
18+
- CRITICAL
19+
PatchTypes:
20+
SPECIAL patch:
21+
Values:
22+
- SPEC
23+
HAProxy Standard Patch:
24+
Values:
25+
- BUG
26+
- BUILD
27+
- CLEANUP
28+
- DOC
29+
- LICENSE
30+
- OPTIM
31+
- RELEASE
32+
- REORG
33+
- TEST
34+
- REVERT
35+
Scope: HAProxy Standard Scope
36+
HAProxy Standard Feature Commit:
37+
Values:
38+
- MINOR
39+
- MEDIUM
40+
- MAJOR
41+
- CRITICAL
42+
TagOrder:
43+
- PatchTypes:
44+
- SPECIAL patch
45+
Optional: true
46+
- PatchTypes:
47+
- HAProxy Standard Patch
48+
- HAProxy Standard Feature Commit
49+
`
50+
51+
func LoadCommitPolicyData(config string) (CommitPolicyConfig, error) {
52+
var commitPolicy CommitPolicyConfig
53+
54+
if err := yaml.Unmarshal([]byte(config), &commitPolicy); err != nil {
55+
return CommitPolicyConfig{}, fmt.Errorf("error loading commit policy: %w", err)
56+
}
57+
58+
return commitPolicy, nil
59+
}
60+
61+
func TestDifferentPolicy(t *testing.T) {
62+
t.Parallel()
63+
64+
c, _ := LoadCommitPolicyData(customConf)
65+
66+
testsSpec := []struct {
67+
name string
68+
subject string
69+
wantErr bool
70+
}{
71+
{
72+
name: "valid type and severity",
73+
subject: "SPEC: BUG/MEDIUM: config: add default location of path to the configuration file",
74+
wantErr: false,
75+
},
76+
{
77+
name: "invalid type RANDOM",
78+
subject: "RANDOM: BUG/MEDIUM: config: add default location of path to the configuration file",
79+
wantErr: true,
80+
},
81+
{
82+
name: "invalid type",
83+
subject: "SPEC: HEHEEEEEE/MEDIUM: config: add default location of path to the configuration file",
84+
wantErr: true,
85+
},
86+
{
87+
name: "invalid severity",
88+
subject: "SPEC: BUG/HEHEEEEEE: config: add default location of path to the configuration file",
89+
wantErr: true,
90+
},
91+
{
92+
name: "no existant aditional type",
93+
subject: "SPEC: BUG/MINOR: CI: config: add default location of path to the configuration file",
94+
wantErr: true,
95+
},
96+
}
97+
testsSpec = append(testsSpec, tests...)
98+
99+
for _, tt := range testsSpec {
100+
tt := tt
101+
t.Run(tt.name, func(t *testing.T) {
102+
t.Parallel()
103+
if err := c.CheckSubject([]byte(tt.subject)); (err != nil) != tt.wantErr {
104+
t.Errorf("checkSubject() error = %v, wantErr %v", err, tt.wantErr)
105+
}
106+
})
107+
}
108+
}

check-commit/check_test.go

+4-53
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,19 @@
11
package main
22

3-
import "testing"
3+
import (
4+
"testing"
5+
)
46

57
func TestCheckSubject(t *testing.T) {
68
t.Parallel()
79

810
c, _ := LoadCommitPolicy("")
911

10-
type args struct {
11-
subject string
12-
}
13-
14-
tests := []struct {
15-
name string
16-
args args
17-
wantErr bool
18-
}{
19-
{
20-
name: "valid type and severity",
21-
args: args{subject: "BUG/MEDIUM: config: add default location of path to the configuration file"},
22-
wantErr: false,
23-
},
24-
{
25-
name: "short subject",
26-
args: args{subject: "BUG/MEDIUM: config: default"},
27-
wantErr: true,
28-
},
29-
{
30-
name: "missing severity",
31-
args: args{subject: "BUG/: config: default implementation"},
32-
wantErr: true,
33-
},
34-
{
35-
name: "wrong tag",
36-
args: args{subject: "WRONG: config: default implementation"},
37-
wantErr: true,
38-
},
39-
{
40-
name: "wrong severity",
41-
args: args{subject: "BUG/WRONG: config: default implementation"},
42-
wantErr: true,
43-
},
44-
{
45-
name: "double spaces",
46-
args: args{subject: "BUG/MEDIUM: config: default implementation"},
47-
wantErr: true,
48-
},
49-
{
50-
name: "trailing spaces",
51-
args: args{subject: "BUG/MEDIUM: config: default implementation "},
52-
wantErr: true,
53-
},
54-
{
55-
name: "unprocessed tags remain",
56-
args: args{subject: "BUG/MINOR: MAJOR: config: default implementation"},
57-
wantErr: true,
58-
},
59-
}
60-
6112
for _, tt := range tests {
6213
tt := tt
6314
t.Run(tt.name, func(t *testing.T) {
6415
t.Parallel()
65-
if err := c.CheckSubject([]byte(tt.args.subject)); (err != nil) != tt.wantErr {
16+
if err := c.CheckSubject([]byte(tt.subject)); (err != nil) != tt.wantErr {
6617
t.Errorf("checkSubject() error = %v, wantErr %v", err, tt.wantErr)
6718
}
6819
})

0 commit comments

Comments
 (0)