Skip to content

Commit 4f12880

Browse files
zakiskchmouel
authored andcommitted
Remove double check for toRef and checks for fromRef
removed double checks in payload validation for toRef field in bitbucker server event payload and added checks for fromRef field and added test accordingly. Signed-off-by: Zaki Shaikh <[email protected]>
1 parent cec7dc7 commit 4f12880

File tree

3 files changed

+175
-21
lines changed

3 files changed

+175
-21
lines changed

pkg/provider/bitbucketserver/parse_payload.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,26 @@ import (
1818
// checkValidPayload checks if the payload is valid.
1919
func checkValidPayload(e *types.PullRequestEvent) error {
2020
if e.PullRequest.ToRef.Repository.Project == nil {
21-
return fmt.Errorf("bitbucket project is nil")
21+
return fmt.Errorf("bitbucket toRef project is nil")
2222
}
2323
if e.PullRequest.ToRef.Repository.Project.Key == "" {
24-
return fmt.Errorf("bitbucket project key is empty")
24+
return fmt.Errorf("bitbucket toRef project key is empty")
2525
}
2626
if e.PullRequest.ToRef.Repository.Name == "" {
2727
return fmt.Errorf("bitbucket toRef repository name is empty")
2828
}
29+
if e.PullRequest.ToRef.LatestCommit == "" {
30+
return fmt.Errorf("bitbucket toRef latest commit is empty")
31+
}
2932

30-
if e.PullRequest.ToRef.Repository.Project == nil {
31-
return fmt.Errorf("bitbucket toRef project is nil")
33+
if e.PullRequest.FromRef.Repository.Project == nil {
34+
return fmt.Errorf("bitbucket fromRef project is nil")
3235
}
33-
if e.PullRequest.ToRef.Repository.Project.Key == "" {
34-
return fmt.Errorf("bitbucket toRef project key is empty")
36+
if e.PullRequest.FromRef.Repository.Project.Key == "" {
37+
return fmt.Errorf("bitbucket fromRef project key is empty")
3538
}
36-
if e.PullRequest.ToRef.Repository.Name == "" {
37-
return fmt.Errorf("bitbucket toRef repository name is empty")
39+
if e.PullRequest.FromRef.Repository.Name == "" {
40+
return fmt.Errorf("bitbucket fromRef repository name is empty")
3841
}
3942
if e.PullRequest.FromRef.LatestCommit == "" {
4043
return fmt.Errorf("bitbucket fromRef latest commit is empty")

pkg/provider/bitbucketserver/parse_payload_test.go

Lines changed: 160 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ func TestCheckValidPayload(t *testing.T) {
2121
payloadEvent types.PullRequestEvent
2222
}{
2323
{
24-
name: "missing project",
24+
name: "missing toRef.project",
2525
payloadEvent: types.PullRequestEvent{
2626
PullRequest: bbv1.PullRequest{
2727
ToRef: bbv1.PullRequestRef{
2828
Repository: bbv1.Repository{},
2929
},
3030
},
3131
},
32-
wantErrString: "bitbucket project is nil",
32+
wantErrString: "bitbucket toRef project is nil",
3333
},
3434
{
35-
name: "empty project key",
35+
name: "empty toRef.project.key",
3636
payloadEvent: types.PullRequestEvent{
3737
PullRequest: bbv1.PullRequest{
3838
ToRef: bbv1.PullRequestRef{
@@ -42,10 +42,10 @@ func TestCheckValidPayload(t *testing.T) {
4242
},
4343
},
4444
},
45-
wantErrString: "bitbucket project key is empty",
45+
wantErrString: "bitbucket toRef project key is empty",
4646
},
4747
{
48-
name: "empty repository name",
48+
name: "empty toRef.repositoryName",
4949
payloadEvent: types.PullRequestEvent{
5050
PullRequest: bbv1.PullRequest{
5151
ToRef: bbv1.PullRequestRef{
@@ -60,7 +60,7 @@ func TestCheckValidPayload(t *testing.T) {
6060
wantErrString: "bitbucket toRef repository name is empty",
6161
},
6262
{
63-
name: "missing latest commit",
63+
name: "missing toRef.latestCommit",
6464
payloadEvent: types.PullRequestEvent{
6565
PullRequest: bbv1.PullRequest{
6666
FromRef: bbv1.PullRequestRef{},
@@ -75,6 +75,102 @@ func TestCheckValidPayload(t *testing.T) {
7575
},
7676
},
7777
},
78+
wantErrString: "bitbucket toRef latest commit is empty",
79+
},
80+
{
81+
name: "missing fromRef.project",
82+
payloadEvent: types.PullRequestEvent{
83+
PullRequest: bbv1.PullRequest{
84+
ToRef: bbv1.PullRequestRef{
85+
Repository: bbv1.Repository{
86+
Name: "repo",
87+
Project: &bbv1.Project{
88+
Key: "PROJ",
89+
Name: "repo",
90+
},
91+
},
92+
LatestCommit: "abcd",
93+
},
94+
FromRef: bbv1.PullRequestRef{
95+
Repository: bbv1.Repository{},
96+
},
97+
},
98+
},
99+
wantErrString: "bitbucket fromRef project is nil",
100+
},
101+
{
102+
name: "empty fromRef.projectKey",
103+
payloadEvent: types.PullRequestEvent{
104+
PullRequest: bbv1.PullRequest{
105+
ToRef: bbv1.PullRequestRef{
106+
Repository: bbv1.Repository{
107+
Name: "repo",
108+
Project: &bbv1.Project{
109+
Key: "PROJ",
110+
Name: "repo",
111+
},
112+
},
113+
LatestCommit: "abcd",
114+
},
115+
FromRef: bbv1.PullRequestRef{
116+
Repository: bbv1.Repository{
117+
Project: &bbv1.Project{},
118+
},
119+
},
120+
},
121+
},
122+
wantErrString: "bitbucket fromRef project key is empty",
123+
},
124+
{
125+
name: "empty fromRef.repositoryName",
126+
payloadEvent: types.PullRequestEvent{
127+
PullRequest: bbv1.PullRequest{
128+
ToRef: bbv1.PullRequestRef{
129+
Repository: bbv1.Repository{
130+
Name: "repo",
131+
Project: &bbv1.Project{
132+
Key: "PROJ",
133+
Name: "repo",
134+
},
135+
},
136+
LatestCommit: "abcd",
137+
},
138+
FromRef: bbv1.PullRequestRef{
139+
Repository: bbv1.Repository{
140+
Project: &bbv1.Project{
141+
Key: "PROJ",
142+
},
143+
},
144+
},
145+
},
146+
},
147+
wantErrString: "bitbucket fromRef repository name is empty",
148+
},
149+
{
150+
name: "missing fromRef.latestCommit",
151+
payloadEvent: types.PullRequestEvent{
152+
PullRequest: bbv1.PullRequest{
153+
ToRef: bbv1.PullRequestRef{
154+
Repository: bbv1.Repository{
155+
Name: "repo",
156+
Project: &bbv1.Project{
157+
Key: "PROJ",
158+
Name: "repo",
159+
},
160+
},
161+
LatestCommit: "abcd",
162+
},
163+
FromRef: bbv1.PullRequestRef{
164+
Repository: bbv1.Repository{
165+
Name: "repo",
166+
Project: &bbv1.Project{
167+
Key: "PROJ",
168+
Name: "repo",
169+
},
170+
},
171+
},
172+
},
173+
},
78174
wantErrString: "bitbucket fromRef latest commit is empty",
79175
},
80176
{
@@ -89,8 +185,16 @@ func TestCheckValidPayload(t *testing.T) {
89185
Name: "repo",
90186
},
91187
},
188+
LatestCommit: "abcd",
92189
},
93190
FromRef: bbv1.PullRequestRef{
191+
Repository: bbv1.Repository{
192+
Name: "repo",
193+
Project: &bbv1.Project{
194+
Key: "PROJ",
195+
Name: "repo",
196+
},
197+
},
94198
LatestCommit: "abcd",
95199
},
96200
},
@@ -109,8 +213,16 @@ func TestCheckValidPayload(t *testing.T) {
109213
Name: "repo",
110214
},
111215
},
216+
LatestCommit: "abcd",
112217
},
113218
FromRef: bbv1.PullRequestRef{
219+
Repository: bbv1.Repository{
220+
Name: "repo",
221+
Project: &bbv1.Project{
222+
Key: "PROJ",
223+
Name: "repo",
224+
},
225+
},
114226
LatestCommit: "abcd",
115227
},
116228
ID: 1,
@@ -137,8 +249,16 @@ func TestCheckValidPayload(t *testing.T) {
137249
Name: "repo",
138250
},
139251
},
252+
LatestCommit: "abcd",
140253
},
141254
FromRef: bbv1.PullRequestRef{
255+
Repository: bbv1.Repository{
256+
Name: "repo",
257+
Project: &bbv1.Project{
258+
Key: "PROJ",
259+
Name: "repo",
260+
},
261+
},
142262
LatestCommit: "abcd",
143263
},
144264
ID: 1,
@@ -168,10 +288,20 @@ func TestCheckValidPayload(t *testing.T) {
168288
},
169289
},
170290
},
171-
DisplayID: "main",
291+
DisplayID: "main",
292+
LatestCommit: "abcd",
293+
},
294+
FromRef: bbv1.PullRequestRef{
295+
Repository: bbv1.Repository{
296+
Name: "repo",
297+
Project: &bbv1.Project{
298+
Key: "PROJ",
299+
Name: "repo",
300+
},
301+
},
302+
LatestCommit: "abcd",
172303
},
173-
FromRef: bbv1.PullRequestRef{LatestCommit: "latet"},
174-
ID: 1,
304+
ID: 1,
175305
},
176306
},
177307
wantErrString: "bitbucket fromRef display ID is empty",
@@ -198,9 +328,17 @@ func TestCheckValidPayload(t *testing.T) {
198328
},
199329
},
200330
},
201-
DisplayID: "main",
331+
DisplayID: "main",
332+
LatestCommit: "abcd",
202333
},
203334
FromRef: bbv1.PullRequestRef{
335+
Repository: bbv1.Repository{
336+
Name: "repo",
337+
Project: &bbv1.Project{
338+
Key: "PROJ",
339+
Name: "repo",
340+
},
341+
},
204342
DisplayID: "feature",
205343
LatestCommit: "abcd",
206344
},
@@ -231,12 +369,17 @@ func TestCheckValidPayload(t *testing.T) {
231369
},
232370
},
233371
},
234-
DisplayID: "main",
372+
DisplayID: "main",
373+
LatestCommit: "abcd",
235374
},
236375
FromRef: bbv1.PullRequestRef{
237376
DisplayID: "feature",
238377
LatestCommit: "abcd",
239378
Repository: bbv1.Repository{
379+
Project: &bbv1.Project{
380+
Key: "PROJ",
381+
Name: "repo",
382+
},
240383
Links: &struct {
241384
Clone []bbv1.CloneLink `json:"clone,omitempty"`
242385
Self []bbv1.SelfLink `json:"self,omitempty"`
@@ -275,12 +418,17 @@ func TestCheckValidPayload(t *testing.T) {
275418
},
276419
},
277420
},
278-
DisplayID: "main",
421+
DisplayID: "main",
422+
LatestCommit: "abcd",
279423
},
280424
FromRef: bbv1.PullRequestRef{
281425
DisplayID: "feature",
282426
LatestCommit: "abcd",
283427
Repository: bbv1.Repository{
428+
Project: &bbv1.Project{
429+
Key: "PROJ",
430+
Name: "repo",
431+
},
284432
Links: &struct {
285433
Clone []bbv1.CloneLink `json:"clone,omitempty"`
286434
Self []bbv1.SelfLink `json:"self,omitempty"`

pkg/provider/bitbucketserver/test/test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,15 @@ func MakePREvent(event *info.Event, comment string) *types.PullRequestEvent {
287287
},
288288
},
289289
},
290-
DisplayID: "base",
290+
DisplayID: "base",
291+
LatestCommit: "abcd",
291292
},
292293
FromRef: bbv1.PullRequestRef{
293294
DisplayID: "head",
294295
LatestCommit: event.SHA,
295296
Repository: bbv1.Repository{
297+
Project: &bbv1.Project{Key: event.Organization},
298+
Name: event.Repository,
296299
Links: &struct {
297300
Clone []bbv1.CloneLink `json:"clone,omitempty"`
298301
Self []bbv1.SelfLink `json:"self,omitempty"`

0 commit comments

Comments
 (0)