@@ -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
1518func 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 {
0 commit comments