Skip to content

Commit 0d2ec1d

Browse files
committed
Chore: address feedback from CR
- renamed AllowedBranches to AllowedRefs - use t.Run() in tests
1 parent 1d2f53d commit 0d2ec1d

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

receiver.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515
const ZeroSHA = "0000000000000000000000000000000000000000"
1616

1717
type Receiver struct {
18-
Debug bool
19-
MasterOnly bool
20-
AllowedBranches []string
21-
TmpDir string
22-
HandlerFunc func(*HookInfo, string) error
18+
Debug bool
19+
MasterOnly bool
20+
AllowedRefs []string
21+
TmpDir string
22+
HandlerFunc func(*HookInfo, string) error
2323
}
2424

2525
func ReadCommitMessage(sha string) (string, error) {
@@ -49,15 +49,15 @@ func IsForcePush(hook *HookInfo) (bool, error) {
4949

5050
func (r *Receiver) CheckAllowedBranch(hook *HookInfo) error {
5151
if r.MasterOnly { // for BC
52-
r.AllowedBranches = append(r.AllowedBranches, "refs/heads/master")
52+
r.AllowedRefs = append(r.AllowedRefs, "refs/heads/master")
5353
}
5454

55-
if len(r.AllowedBranches) == 0 {
55+
if len(r.AllowedRefs) == 0 {
5656
return nil
5757
}
5858

59-
if !slices.Contains(r.AllowedBranches, hook.Ref) {
60-
return fmt.Errorf("cannot push branch, allowed branches: %s", strings.Join(r.AllowedBranches, ", "))
59+
if !slices.Contains(r.AllowedRefs, hook.Ref) {
60+
return fmt.Errorf("cannot push branch, allowed branches: %s", strings.Join(r.AllowedRefs, ", "))
6161
}
6262

6363
return nil

receiver_test.go

+26-22
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,21 @@ func TestMasterOnly(t *testing.T) {
3232
}
3333

3434
for _, tc := range testCases {
35-
r := &gitkit.Receiver{
36-
MasterOnly: tc.masterOnly,
37-
}
35+
t.Run(tc.name, func(t *testing.T) {
36+
r := &gitkit.Receiver{
37+
MasterOnly: tc.masterOnly,
38+
}
3839

39-
err := r.CheckAllowedBranch(&gitkit.HookInfo{
40-
Ref: tc.ref,
41-
})
40+
err := r.CheckAllowedBranch(&gitkit.HookInfo{
41+
Ref: tc.ref,
42+
})
4243

43-
if !tc.isErr {
44-
assert.NoError(t, err, "expected no error: %s", tc.name)
45-
} else {
46-
assert.Error(t, err, "expected an error: %s", tc.name)
47-
}
44+
if !tc.isErr {
45+
assert.NoError(t, err, "expected no error: %s", tc.name)
46+
} else {
47+
assert.Error(t, err, "expected an error: %s", tc.name)
48+
}
49+
})
4850
}
4951
}
5052

@@ -77,18 +79,20 @@ func TestAllowedBranches(t *testing.T) {
7779
}
7880

7981
for _, tc := range testCases {
80-
r := &gitkit.Receiver{
81-
AllowedBranches: tc.allowedBranches,
82-
}
82+
t.Run(tc.name, func(t *testing.T) {
83+
r := &gitkit.Receiver{
84+
AllowedRefs: tc.allowedBranches,
85+
}
8386

84-
err := r.CheckAllowedBranch(&gitkit.HookInfo{
85-
Ref: tc.ref,
86-
})
87+
err := r.CheckAllowedBranch(&gitkit.HookInfo{
88+
Ref: tc.ref,
89+
})
8790

88-
if !tc.isErr {
89-
assert.NoError(t, err, "expected no error: %s", tc.name)
90-
} else {
91-
assert.Error(t, err, "expected an error: %s", tc.name)
92-
}
91+
if !tc.isErr {
92+
assert.NoError(t, err, "expected no error: %s", tc.name)
93+
} else {
94+
assert.Error(t, err, "expected an error: %s", tc.name)
95+
}
96+
})
9397
}
9498
}

0 commit comments

Comments
 (0)