@@ -18,7 +18,6 @@ package cherrypickunapproved
18
18
19
19
import (
20
20
"encoding/json"
21
- "fmt"
22
21
"reflect"
23
22
"regexp"
24
23
"testing"
@@ -90,7 +89,7 @@ type fakePruner struct{}
90
89
91
90
func (fp * fakePruner ) PruneComments (shouldPrune func (github.IssueComment ) bool ) {}
92
91
93
- func makeFakePullRequestEvent (action github.PullRequestEventAction , branch , oldBranch string ) github.PullRequestEvent {
92
+ func makeFakePullRequestEvent (action github.PullRequestEventAction , branch string , changes json. RawMessage ) github.PullRequestEvent {
94
93
event := github.PullRequestEvent {
95
94
Action : action ,
96
95
PullRequest : github.PullRequest {
@@ -100,103 +99,107 @@ func makeFakePullRequestEvent(action github.PullRequestEventAction, branch, oldB
100
99
},
101
100
}
102
101
103
- if oldBranch != "" {
104
- event .Changes = json . RawMessage ( fmt . Sprintf ( `{"base": {"ref": {"from": "%s"}, "sha": {"from": "sha"}}}` , oldBranch ))
102
+ if changes != nil {
103
+ event .Changes = changes
105
104
}
106
105
107
106
return event
108
107
}
109
108
110
109
func TestCherryPickUnapprovedLabel (t * testing.T ) {
111
110
var testcases = []struct {
112
- name string
113
- branch string
114
- previousBranch string
115
- action github.PullRequestEventAction
116
- labels []string
117
- added []string
118
- removed []string
119
- expectComment bool
111
+ name string
112
+ branch string
113
+ changes json. RawMessage
114
+ action github.PullRequestEventAction
115
+ labels []string
116
+ added []string
117
+ removed []string
118
+ expectComment bool
120
119
}{
121
120
{
122
- name : "unsupported PR action -> no-op" ,
123
- branch : "release-1.10" ,
124
- previousBranch : "" ,
125
- action : github .PullRequestActionClosed ,
126
- labels : []string {},
127
- added : []string {},
128
- removed : []string {},
129
- expectComment : false ,
121
+ name : "unsupported PR action -> no-op" ,
122
+ branch : "release-1.10" ,
123
+ action : github .PullRequestActionClosed ,
124
+ labels : []string {},
125
+ added : []string {},
126
+ removed : []string {},
127
+ expectComment : false ,
130
128
},
131
129
{
132
- name : "branch that does match regexp -> no-op" ,
133
- branch : "master" ,
134
- previousBranch : "" ,
135
- action : github .PullRequestActionOpened ,
136
- labels : []string {},
137
- added : []string {},
138
- removed : []string {},
139
- expectComment : false ,
130
+ name : "branch that does match regexp -> no-op" ,
131
+ branch : "master" ,
132
+ action : github .PullRequestActionOpened ,
133
+ labels : []string {},
134
+ added : []string {},
135
+ removed : []string {},
136
+ expectComment : false ,
140
137
},
141
138
{
142
- name : "has cpUnapproved -> no-op" ,
143
- branch : "release-1.10" ,
144
- previousBranch : "" ,
145
- action : github .PullRequestActionOpened ,
146
- labels : []string {labels .CpUnapproved },
147
- added : []string {},
148
- removed : []string {},
149
- expectComment : false ,
139
+ name : "has cpUnapproved -> no-op" ,
140
+ branch : "release-1.10" ,
141
+ action : github .PullRequestActionOpened ,
142
+ labels : []string {labels .CpUnapproved },
143
+ added : []string {},
144
+ removed : []string {},
145
+ expectComment : false ,
150
146
},
151
147
{
152
- name : "has both cpApproved and cpUnapproved -> remove cpUnapproved" ,
153
- branch : "release-1.10" ,
154
- previousBranch : "" ,
155
- action : github .PullRequestActionOpened ,
156
- labels : []string {labels .CpApproved , labels .CpUnapproved },
157
- added : []string {},
158
- removed : []string {labels .CpUnapproved },
159
- expectComment : false ,
148
+ name : "has both cpApproved and cpUnapproved -> remove cpUnapproved" ,
149
+ branch : "release-1.10" ,
150
+ action : github .PullRequestActionOpened ,
151
+ labels : []string {labels .CpApproved , labels .CpUnapproved },
152
+ added : []string {},
153
+ removed : []string {labels .CpUnapproved },
154
+ expectComment : false ,
160
155
},
161
156
{
162
- name : "does not have any labels, PR opened against a release branch -> add cpUnapproved and comment" ,
163
- branch : "release-1.10" ,
164
- previousBranch : "" ,
165
- action : github .PullRequestActionOpened ,
166
- labels : []string {},
167
- added : []string {labels .CpUnapproved },
168
- removed : []string {},
169
- expectComment : true ,
157
+ name : "does not have any labels, PR opened against a release branch -> add cpUnapproved and comment" ,
158
+ branch : "release-1.10" ,
159
+ action : github .PullRequestActionOpened ,
160
+ labels : []string {},
161
+ added : []string {labels .CpUnapproved },
162
+ removed : []string {},
163
+ expectComment : true ,
170
164
},
171
165
{
172
- name : "does not have any labels, PR reopened against a release branch -> add cpUnapproved and comment" ,
173
- branch : "release-1.10" ,
174
- previousBranch : "" ,
175
- action : github .PullRequestActionReopened ,
176
- labels : []string {},
177
- added : []string {labels .CpUnapproved },
178
- removed : []string {},
179
- expectComment : true ,
166
+ name : "does not have any labels, PR reopened against a release branch -> add cpUnapproved and comment" ,
167
+ branch : "release-1.10" ,
168
+ action : github .PullRequestActionReopened ,
169
+ labels : []string {},
170
+ added : []string {labels .CpUnapproved },
171
+ removed : []string {},
172
+ expectComment : true ,
180
173
},
181
174
{
182
- name : "PR base branch master edited to release -> add cpUnapproved and comment" ,
183
- branch : "release-1.10" ,
184
- previousBranch : "master" ,
185
- action : github . PullRequestActionEdited ,
186
- labels : []string {},
187
- added : []string {labels .CpUnapproved },
188
- removed : []string {},
189
- expectComment : true ,
175
+ name : "PR base branch master edited to release -> add cpUnapproved and comment" ,
176
+ branch : "release-1.10" ,
177
+ action : github . PullRequestActionEdited ,
178
+ changes : json . RawMessage ( `{"base": {"ref": {"from": "master"}, "sha": {"from": "sha"}}}` ) ,
179
+ labels : []string {},
180
+ added : []string {labels .CpUnapproved },
181
+ removed : []string {},
182
+ expectComment : true ,
190
183
},
191
184
{
192
- name : "PR base branch edited from release to master -> remove cpApproved and cpUnapproved" ,
193
- branch : "master" ,
194
- previousBranch : "release-1.10" ,
195
- action : github .PullRequestActionEdited ,
196
- labels : []string {labels .CpApproved , labels .CpUnapproved },
197
- added : []string {},
198
- removed : []string {labels .CpApproved , labels .CpUnapproved },
199
- expectComment : false ,
185
+ name : "PR base branch edited from release to master -> remove cpApproved and cpUnapproved" ,
186
+ branch : "master" ,
187
+ action : github .PullRequestActionEdited ,
188
+ changes : json .RawMessage (`{"base": {"ref": {"from": "release-1.10"}, "sha": {"from": "sha"}}}` ),
189
+ labels : []string {labels .CpApproved , labels .CpUnapproved },
190
+ added : []string {},
191
+ removed : []string {labels .CpApproved , labels .CpUnapproved },
192
+ expectComment : false ,
193
+ },
194
+ {
195
+ name : "PR title changed -> no-op" ,
196
+ branch : "release-1.10" ,
197
+ action : github .PullRequestActionEdited ,
198
+ changes : json .RawMessage (`{"title": {"from": "Update README.md"}}` ),
199
+ labels : []string {labels .CpApproved , labels .CpUnapproved },
200
+ added : []string {},
201
+ removed : []string {},
202
+ expectComment : false ,
200
203
},
201
204
}
202
205
@@ -208,7 +211,7 @@ func TestCherryPickUnapprovedLabel(t *testing.T) {
208
211
commentsAdded : make (map [int ][]string , 0 ),
209
212
}
210
213
211
- event := makeFakePullRequestEvent (tc .action , tc .branch , tc .previousBranch )
214
+ event := makeFakePullRequestEvent (tc .action , tc .branch , tc .changes )
212
215
branchRe := regexp .MustCompile (`^release-.*$` )
213
216
comment := "dummy cumment"
214
217
err := handlePR (fc , logrus .WithField ("plugin" , "fake-cherrypick-unapproved" ), & event , & fakePruner {}, branchRe , comment )
0 commit comments