From 4f12880fd440a6a34c2080001c09cfee4a52c122 Mon Sep 17 00:00:00 2001 From: Zaki Shaikh Date: Thu, 31 Oct 2024 19:30:39 +0530 Subject: [PATCH] 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 --- pkg/provider/bitbucketserver/parse_payload.go | 19 +- .../bitbucketserver/parse_payload_test.go | 172 ++++++++++++++++-- pkg/provider/bitbucketserver/test/test.go | 5 +- 3 files changed, 175 insertions(+), 21 deletions(-) diff --git a/pkg/provider/bitbucketserver/parse_payload.go b/pkg/provider/bitbucketserver/parse_payload.go index da030f820..a625217d0 100644 --- a/pkg/provider/bitbucketserver/parse_payload.go +++ b/pkg/provider/bitbucketserver/parse_payload.go @@ -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") diff --git a/pkg/provider/bitbucketserver/parse_payload_test.go b/pkg/provider/bitbucketserver/parse_payload_test.go index 5dabc449b..4c80ded7b 100644 --- a/pkg/provider/bitbucketserver/parse_payload_test.go +++ b/pkg/provider/bitbucketserver/parse_payload_test.go @@ -21,7 +21,7 @@ func TestCheckValidPayload(t *testing.T) { payloadEvent types.PullRequestEvent }{ { - name: "missing project", + name: "missing toRef.project", payloadEvent: types.PullRequestEvent{ PullRequest: bbv1.PullRequest{ ToRef: bbv1.PullRequestRef{ @@ -29,10 +29,10 @@ func TestCheckValidPayload(t *testing.T) { }, }, }, - 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{ @@ -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{ @@ -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{}, @@ -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", }, { @@ -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", }, }, @@ -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, @@ -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, @@ -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", @@ -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", }, @@ -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"` @@ -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"` diff --git a/pkg/provider/bitbucketserver/test/test.go b/pkg/provider/bitbucketserver/test/test.go index d3d395c9f..ccce7d748 100644 --- a/pkg/provider/bitbucketserver/test/test.go +++ b/pkg/provider/bitbucketserver/test/test.go @@ -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"`