Skip to content

Commit 7c49841

Browse files
committed
Properly consider flag to enable issues
Signed-off-by: Galo Navarro <[email protected]>
1 parent 5dc5cea commit 7c49841

File tree

5 files changed

+164
-4
lines changed

5 files changed

+164
-4
lines changed

cmd/action_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ func TestGetLabelerConfigV1(t *testing.T) {
146146
Label: "TestAuthorCanMerge",
147147
AuthorCanMerge: "True",
148148
},
149+
{
150+
Label: "TestIsAuthorInTeam",
151+
AuthorInTeam: "team1",
152+
},
149153
},
150154
}
151155

pkg/labeler.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ func (l *Labeler) HandleEvent(
116116
case *gh.PullRequestTargetEvent:
117117
err = l.ExecuteOn(wrapPrAsTarget(event.PullRequest))
118118
case *gh.IssuesEvent:
119+
config, cfgErr := l.FetchRepoConfig()
120+
if cfgErr != nil {
121+
return cfgErr
122+
}
123+
if !config.Issues {
124+
log.Println("Issues must be explicitly enabled in order to process issues in event mode")
125+
return nil
126+
}
119127
err = l.ExecuteOn(wrapIssueAsTarget(event.Issue))
120128
default:
121129
log.Printf("Event type is not supported, please review your workflow config")
@@ -298,8 +306,8 @@ func (l *Labeler) ProcessAllIssues(owner, repo string) {
298306
}
299307

300308
if !config.Issues {
301-
log.Println("Issues must be explicitly enabled in order to " +
302-
"process issues in the scheduled execution mode")
309+
log.Println("Issues must be explicitly enabled in order to process issues in the scheduled execution mode")
310+
return
303311
}
304312

305313
issues, err := l.GitHubFacade.ListIssuesByRepo(owner, repo)

pkg/labeler_test.go

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"sort"
1111
"strings"
1212
"testing"
13+
"time"
14+
15+
gh "github.com/google/go-github/v50/github"
1316
)
1417

1518
func loadPayload(name string) ([]byte, error) {
@@ -29,10 +32,149 @@ type TestCase struct {
2932
expectedLabels []string
3033
}
3134

32-
func TestHandleEvent(t *testing.T) {
35+
func TestProcessAllIssues(t *testing.T) {
36+
type call struct {
37+
issueNo int
38+
labels []string
39+
}
40+
41+
fakeIssues := []*gh.Issue{
42+
{
43+
Number: gh.Int(1),
44+
Title: gh.String("Testy test"),
45+
Body: gh.String("This is the description!"),
46+
User: &gh.User{Login: gh.String("srvaroa")},
47+
State: gh.String("open"),
48+
CreatedAt: &gh.Timestamp{Time: time.Now()},
49+
Labels: []*gh.Label{},
50+
RepositoryURL: gh.String("https://api.github.com/repos/srvaroa/labeler"),
51+
},
52+
}
53+
54+
makeLabeler := func(cfg LabelerConfigV1, calls *[]call) Labeler {
55+
return Labeler{
56+
FetchRepoConfig: func() (*LabelerConfigV1, error) { return &cfg, nil },
57+
GetCurrentLabels: func(target *Target) ([]string, error) { return []string{"ShouldStay"}, nil },
58+
ReplaceLabels: func(target *Target, labels []string) error {
59+
*calls = append(*calls, call{issueNo: target.IssueNo, labels: labels})
60+
return nil
61+
},
62+
GitHubFacade: &GitHubFacade{
63+
ListIssuesByRepo: func(owner, repo string) ([]*gh.Issue, error) {
64+
return fakeIssues, nil
65+
},
66+
},
67+
}
68+
}
69+
70+
t.Run("Does not process issues if Issues flag is not set", func(t *testing.T) {
71+
var calls []call
72+
l := makeLabeler(LabelerConfigV1{Version: 1, Labels: []LabelMatcher{{Label: "Test", Title: "^Testy.*t"}}}, &calls)
73+
l.ProcessAllIssues("srvaroa", "labeler")
74+
if len(calls) != 0 {
75+
t.Errorf("Expected no issues processed, got %d", len(calls))
76+
}
77+
})
78+
79+
t.Run("Does not process issues if Issues config is set to False", func(t *testing.T) {
80+
var calls []call
81+
l := makeLabeler(LabelerConfigV1{Version: 1, Issues: false, Labels: []LabelMatcher{{Label: "Test", Title: "^Testy.*t"}}}, &calls)
82+
l.ProcessAllIssues("srvaroa", "labeler")
83+
if len(calls) != 0 {
84+
t.Errorf("Expected no issues processed, got %d", len(calls))
85+
}
86+
})
3387

88+
t.Run("Does not process issues if Issues config is unset (zero value)", func(t *testing.T) {
89+
var calls []call
90+
l := makeLabeler(LabelerConfigV1{Version: 1, Labels: []LabelMatcher{{Label: "Test", Title: "^Testy.*t"}}}, &calls)
91+
l.ProcessAllIssues("srvaroa", "labeler")
92+
if len(calls) != 0 {
93+
t.Errorf("Expected no issues processed, got %d", len(calls))
94+
}
95+
})
96+
97+
t.Run("Processes issues if Issues flag is set", func(t *testing.T) {
98+
var calls []call
99+
l := makeLabeler(LabelerConfigV1{Version: 1, Issues: true, Labels: []LabelMatcher{{Label: "Test", Title: "^Testy.*t"}}}, &calls)
100+
l.ProcessAllIssues("srvaroa", "labeler")
101+
if len(calls) != 1 {
102+
t.Errorf("Expected 1 issue processed, got %d", len(calls))
103+
}
104+
if len(calls) > 0 {
105+
expected := []string{"ShouldStay", "Test"}
106+
got := calls[0].labels
107+
sort.Strings(expected)
108+
sort.Strings(got)
109+
if !reflect.DeepEqual(expected, got) {
110+
t.Errorf("Expected labels %v, got %+v", expected, got)
111+
}
112+
}
113+
})
114+
}
115+
116+
func TestHandleEvent(t *testing.T) {
34117
// These all use the payload in payload files
35118
testCases := []TestCase{
119+
{
120+
event: "issues",
121+
payloads: []string{"issue_open"},
122+
name: "Do not process issues if Issues flag is not set",
123+
config: LabelerConfigV1{Version: 1, Labels: []LabelMatcher{{Label: "Test", Title: "^Testy.*t"}}},
124+
initialLabels: []string{"ShouldStay"},
125+
expectedLabels: []string{"ShouldStay"},
126+
},
127+
{
128+
event: "issues",
129+
payloads: []string{"issue_open"},
130+
name: "Do not process issues if Issues config is set to False",
131+
config: LabelerConfigV1{
132+
Version: 1,
133+
Issues: false,
134+
Labels: []LabelMatcher{
135+
{
136+
Label: "TestIssueLabelUnset",
137+
Title: "^Testy.*t",
138+
},
139+
},
140+
},
141+
initialLabels: []string{"ShouldStay"},
142+
expectedLabels: []string{"ShouldStay"},
143+
},
144+
{
145+
event: "issues",
146+
payloads: []string{"issue_open"},
147+
name: "Do not process issues if Issues config is unset (zero value)",
148+
config: LabelerConfigV1{
149+
Version: 1,
150+
// Issues field is omitted (zero value)
151+
Labels: []LabelMatcher{
152+
{
153+
Label: "TestIssueLabelUnset",
154+
Title: "^Testy.*t",
155+
},
156+
},
157+
},
158+
initialLabels: []string{"ShouldStay"},
159+
expectedLabels: []string{"ShouldStay"},
160+
},
161+
{
162+
event: "issues",
163+
payloads: []string{"issue_open"},
164+
name: "Process issues if Issues flag is set",
165+
config: LabelerConfigV1{
166+
Version: 1,
167+
Issues: true,
168+
Labels: []LabelMatcher{
169+
{
170+
Label: "TestIssueLabel",
171+
Title: "^Testy.*t",
172+
},
173+
},
174+
},
175+
initialLabels: []string{"ShouldStay"},
176+
expectedLabels: []string{"ShouldStay", "TestIssueLabel"},
177+
},
36178
{
37179
event: "pull_request",
38180
payloads: []string{"create_pr", "reopen_pr"},
@@ -216,7 +358,7 @@ func TestHandleEvent(t *testing.T) {
216358
Label: "Getting Old",
217359
AgeRange: &DurationConfig{
218360
AtLeast: "7d",
219-
AtMost: "14d",
361+
AtMost: "14d",
220362
},
221363
},
222364
{

test_data/config_v1.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ labels:
2727
mergeable: True
2828
- label: "TestAuthorCanMerge"
2929
author-can-merge: True
30+
- label: "TestIsAuthorInTeam"
31+
author-in-team: "team1"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version: 1
2+
labels:
3+
- label: "TestIsAuthorInTeam"
4+
author-in-team: "team-a"

0 commit comments

Comments
 (0)