Skip to content

Commit

Permalink
Remove double check for toRef and checks for fromRef
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
zakisk authored and chmouel committed Oct 31, 2024
1 parent cec7dc7 commit 4f12880
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 21 deletions.
19 changes: 11 additions & 8 deletions pkg/provider/bitbucketserver/parse_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,26 @@ import (
// checkValidPayload checks if the payload is valid.
func checkValidPayload(e *types.PullRequestEvent) error {
if e.PullRequest.ToRef.Repository.Project == nil {
return fmt.Errorf("bitbucket project is nil")
return fmt.Errorf("bitbucket toRef project is nil")
}
if e.PullRequest.ToRef.Repository.Project.Key == "" {
return fmt.Errorf("bitbucket project key is empty")
return fmt.Errorf("bitbucket toRef project key is empty")
}
if e.PullRequest.ToRef.Repository.Name == "" {
return fmt.Errorf("bitbucket toRef repository name is empty")
}
if e.PullRequest.ToRef.LatestCommit == "" {
return fmt.Errorf("bitbucket toRef latest commit is empty")
}

if e.PullRequest.ToRef.Repository.Project == nil {
return fmt.Errorf("bitbucket toRef project is nil")
if e.PullRequest.FromRef.Repository.Project == nil {
return fmt.Errorf("bitbucket fromRef project is nil")
}
if e.PullRequest.ToRef.Repository.Project.Key == "" {
return fmt.Errorf("bitbucket toRef project key is empty")
if e.PullRequest.FromRef.Repository.Project.Key == "" {
return fmt.Errorf("bitbucket fromRef project key is empty")
}
if e.PullRequest.ToRef.Repository.Name == "" {
return fmt.Errorf("bitbucket toRef repository name is empty")
if e.PullRequest.FromRef.Repository.Name == "" {
return fmt.Errorf("bitbucket fromRef repository name is empty")
}
if e.PullRequest.FromRef.LatestCommit == "" {
return fmt.Errorf("bitbucket fromRef latest commit is empty")
Expand Down
172 changes: 160 additions & 12 deletions pkg/provider/bitbucketserver/parse_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ func TestCheckValidPayload(t *testing.T) {
payloadEvent types.PullRequestEvent
}{
{
name: "missing project",
name: "missing toRef.project",
payloadEvent: types.PullRequestEvent{
PullRequest: bbv1.PullRequest{
ToRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{},
},
},
},
wantErrString: "bitbucket project is nil",
wantErrString: "bitbucket toRef project is nil",
},
{
name: "empty project key",
name: "empty toRef.project.key",
payloadEvent: types.PullRequestEvent{
PullRequest: bbv1.PullRequest{
ToRef: bbv1.PullRequestRef{
Expand All @@ -42,10 +42,10 @@ func TestCheckValidPayload(t *testing.T) {
},
},
},
wantErrString: "bitbucket project key is empty",
wantErrString: "bitbucket toRef project key is empty",
},
{
name: "empty repository name",
name: "empty toRef.repositoryName",
payloadEvent: types.PullRequestEvent{
PullRequest: bbv1.PullRequest{
ToRef: bbv1.PullRequestRef{
Expand All @@ -60,7 +60,7 @@ func TestCheckValidPayload(t *testing.T) {
wantErrString: "bitbucket toRef repository name is empty",
},
{
name: "missing latest commit",
name: "missing toRef.latestCommit",
payloadEvent: types.PullRequestEvent{
PullRequest: bbv1.PullRequest{
FromRef: bbv1.PullRequestRef{},
Expand All @@ -75,6 +75,102 @@ func TestCheckValidPayload(t *testing.T) {
},
},
},
wantErrString: "bitbucket toRef latest commit is empty",
},
{
name: "missing fromRef.project",
payloadEvent: types.PullRequestEvent{
PullRequest: bbv1.PullRequest{
ToRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{
Name: "repo",
Project: &bbv1.Project{
Key: "PROJ",
Name: "repo",
},
},
LatestCommit: "abcd",
},
FromRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{},
},
},
},
wantErrString: "bitbucket fromRef project is nil",
},
{
name: "empty fromRef.projectKey",
payloadEvent: types.PullRequestEvent{
PullRequest: bbv1.PullRequest{
ToRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{
Name: "repo",
Project: &bbv1.Project{
Key: "PROJ",
Name: "repo",
},
},
LatestCommit: "abcd",
},
FromRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{
Project: &bbv1.Project{},
},
},
},
},
wantErrString: "bitbucket fromRef project key is empty",
},
{
name: "empty fromRef.repositoryName",
payloadEvent: types.PullRequestEvent{
PullRequest: bbv1.PullRequest{
ToRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{
Name: "repo",
Project: &bbv1.Project{
Key: "PROJ",
Name: "repo",
},
},
LatestCommit: "abcd",
},
FromRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{
Project: &bbv1.Project{
Key: "PROJ",
},
},
},
},
},
wantErrString: "bitbucket fromRef repository name is empty",
},
{
name: "missing fromRef.latestCommit",
payloadEvent: types.PullRequestEvent{
PullRequest: bbv1.PullRequest{
ToRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{
Name: "repo",
Project: &bbv1.Project{
Key: "PROJ",
Name: "repo",
},
},
LatestCommit: "abcd",
},
FromRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{
Name: "repo",
Project: &bbv1.Project{
Key: "PROJ",
Name: "repo",
},
},
},
},
},
wantErrString: "bitbucket fromRef latest commit is empty",
},
{
Expand All @@ -89,8 +185,16 @@ func TestCheckValidPayload(t *testing.T) {
Name: "repo",
},
},
LatestCommit: "abcd",
},
FromRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{
Name: "repo",
Project: &bbv1.Project{
Key: "PROJ",
Name: "repo",
},
},
LatestCommit: "abcd",
},
},
Expand All @@ -109,8 +213,16 @@ func TestCheckValidPayload(t *testing.T) {
Name: "repo",
},
},
LatestCommit: "abcd",
},
FromRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{
Name: "repo",
Project: &bbv1.Project{
Key: "PROJ",
Name: "repo",
},
},
LatestCommit: "abcd",
},
ID: 1,
Expand All @@ -137,8 +249,16 @@ func TestCheckValidPayload(t *testing.T) {
Name: "repo",
},
},
LatestCommit: "abcd",
},
FromRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{
Name: "repo",
Project: &bbv1.Project{
Key: "PROJ",
Name: "repo",
},
},
LatestCommit: "abcd",
},
ID: 1,
Expand Down Expand Up @@ -168,10 +288,20 @@ func TestCheckValidPayload(t *testing.T) {
},
},
},
DisplayID: "main",
DisplayID: "main",
LatestCommit: "abcd",
},
FromRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{
Name: "repo",
Project: &bbv1.Project{
Key: "PROJ",
Name: "repo",
},
},
LatestCommit: "abcd",
},
FromRef: bbv1.PullRequestRef{LatestCommit: "latet"},
ID: 1,
ID: 1,
},
},
wantErrString: "bitbucket fromRef display ID is empty",
Expand All @@ -198,9 +328,17 @@ func TestCheckValidPayload(t *testing.T) {
},
},
},
DisplayID: "main",
DisplayID: "main",
LatestCommit: "abcd",
},
FromRef: bbv1.PullRequestRef{
Repository: bbv1.Repository{
Name: "repo",
Project: &bbv1.Project{
Key: "PROJ",
Name: "repo",
},
},
DisplayID: "feature",
LatestCommit: "abcd",
},
Expand Down Expand Up @@ -231,12 +369,17 @@ func TestCheckValidPayload(t *testing.T) {
},
},
},
DisplayID: "main",
DisplayID: "main",
LatestCommit: "abcd",
},
FromRef: bbv1.PullRequestRef{
DisplayID: "feature",
LatestCommit: "abcd",
Repository: bbv1.Repository{
Project: &bbv1.Project{
Key: "PROJ",
Name: "repo",
},
Links: &struct {
Clone []bbv1.CloneLink `json:"clone,omitempty"`
Self []bbv1.SelfLink `json:"self,omitempty"`
Expand Down Expand Up @@ -275,12 +418,17 @@ func TestCheckValidPayload(t *testing.T) {
},
},
},
DisplayID: "main",
DisplayID: "main",
LatestCommit: "abcd",
},
FromRef: bbv1.PullRequestRef{
DisplayID: "feature",
LatestCommit: "abcd",
Repository: bbv1.Repository{
Project: &bbv1.Project{
Key: "PROJ",
Name: "repo",
},
Links: &struct {
Clone []bbv1.CloneLink `json:"clone,omitempty"`
Self []bbv1.SelfLink `json:"self,omitempty"`
Expand Down
5 changes: 4 additions & 1 deletion pkg/provider/bitbucketserver/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,15 @@ func MakePREvent(event *info.Event, comment string) *types.PullRequestEvent {
},
},
},
DisplayID: "base",
DisplayID: "base",
LatestCommit: "abcd",
},
FromRef: bbv1.PullRequestRef{
DisplayID: "head",
LatestCommit: event.SHA,
Repository: bbv1.Repository{
Project: &bbv1.Project{Key: event.Organization},
Name: event.Repository,
Links: &struct {
Clone []bbv1.CloneLink `json:"clone,omitempty"`
Self []bbv1.SelfLink `json:"self,omitempty"`
Expand Down

0 comments on commit 4f12880

Please sign in to comment.