Skip to content

Commit

Permalink
Check Bitbucket build status in E2E test
Browse files Browse the repository at this point in the history
Closes #187.
  • Loading branch information
michaelsauter committed Oct 6, 2021
1 parent c41cb49 commit 51d13ac
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 45 deletions.
10 changes: 5 additions & 5 deletions pkg/bitbucket/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type Branch struct {
}

type BranchPage struct {
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []Branch
Start int `json:"start"`
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []Branch `json:"values"`
Start int `json:"start"`
}

type BranchListParams struct {
Expand Down
23 changes: 12 additions & 11 deletions pkg/bitbucket/build_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,26 @@ func (c *Client) BuildStatusCreate(gitCommit string, payload BuildStatusCreatePa
return nil
}

type BuildStatusPage struct {
Size int `json:"size"`
Limit int `json:"limit"`
Islastpage bool `json:"isLastPage"`
BuildStatus []BuildStatus `json:"values"`
Start int `json:"start"`
}
type BuildStatus struct {
State string `json:"state"`
Key string `json:"key"`
Name string `json:"name"`
URL string `json:"url"`
Description string `json:"description"`
Dateadded int64 `json:"dateAdded"`
DateAdded int64 `json:"dateAdded"`
}

type BuildStatusPage struct {
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []BuildStatus `json:"values"` // newest build status appears first
Start int `json:"start"`
}

// BuildStatusGet gets the build statuses associated with a commit.
// BuildStatusList gets the build statuses associated with a commit.
// https://docs.atlassian.com/bitbucket-server/rest/7.13.0/bitbucket-build-rest.html#idp8
func (c *Client) BuildStatusGet(gitCommit string) (*BuildStatus, error) {
func (c *Client) BuildStatusList(gitCommit string) (*BuildStatusPage, error) {
urlPath := fmt.Sprintf("/rest/build-status/1.0/commits/%s", gitCommit)
statusCode, response, err := c.get(urlPath)
if err != nil {
Expand All @@ -75,7 +76,7 @@ func (c *Client) BuildStatusGet(gitCommit string) (*BuildStatus, error) {
"could not unmarshal response: %w. status code: %d, body: %s", err, statusCode, string(response),
)
}
return &BuildStatusPage.BuildStatus[0], nil // return the newest by default
return &BuildStatusPage, nil
case 401:
return nil, fmt.Errorf("you are not permitted to get the build status of git commit %s", gitCommit)
default:
Expand Down
14 changes: 7 additions & 7 deletions pkg/bitbucket/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ type Commit struct {
}

type CommitPage struct {
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []Commit
Start int `json:"start"`
AuthorCount int `json:"authorCount"`
TotalCount int `json:"totalCount"`
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []Commit `json:"values"`
Start int `json:"start"`
AuthorCount int `json:"authorCount"`
TotalCount int `json:"totalCount"`
}

type PullRequestPage struct {
Expand Down
10 changes: 5 additions & 5 deletions pkg/bitbucket/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type Tag struct {
}

type TagPage struct {
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []Tag
Start int `json:"start"`
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []Tag `json:"values"`
Start int `json:"start"`
}

type TagCreatePayload struct {
Expand Down
15 changes: 15 additions & 0 deletions pkg/tasktesting/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,18 @@ func BitbucketClientOrFatal(t *testing.T, c *kclient.Clientset, namespace string
})
return bitbucketClient
}

func CheckBitbucketBuildStatus(t *testing.T, c *bitbucket.Client, gitCommit, wantBuildStatus string) {
buildStatusPage, err := c.BuildStatusList(gitCommit)
if err != nil {
t.Fatal(err)
}
if buildStatusPage == nil || len(buildStatusPage.Values) == 0 {
t.Fatal("no build status found")
}
buildStatus := buildStatusPage.Values[0]
if buildStatus.State != wantBuildStatus {
t.Fatalf("Got: %s, want: %s", buildStatus.State, wantBuildStatus)
}

}
2 changes: 2 additions & 0 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ func TestWebhookInterceptor(t *testing.T) {
t.Log(logs)
t.Fatal()
}

tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, odsContext.GitCommitSHA, bitbucket.BuildStatusSuccessful)
}

func waitForServiceToBeReady(t *testing.T, clientset *k8s.Clientset, ns, name string, timeout time.Duration) error {
Expand Down
12 changes: 0 additions & 12 deletions test/tasks/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/opendevstack/pipeline/internal/kubernetes"
"github.com/opendevstack/pipeline/pkg/bitbucket"
"github.com/opendevstack/pipeline/pkg/config"
"github.com/opendevstack/pipeline/pkg/pipelinectxt"
"github.com/opendevstack/pipeline/pkg/sonar"
Expand Down Expand Up @@ -177,14 +176,3 @@ func createODSYML(wsDir string, o *config.ODS) error {
filename := filepath.Join(wsDir, "ods.yaml")
return ioutil.WriteFile(filename, y, 0644)
}

func checkBuildStatus(t *testing.T, c *bitbucket.Client, gitCommit, wantBuildStatus string) {
buildStatus, err := c.BuildStatusGet(gitCommit)
if err != nil {
t.Fatal(err)
}
if buildStatus.State != wantBuildStatus {
t.Fatalf("Got: %s, want: %s", buildStatus.State, wantBuildStatus)
}

}
6 changes: 3 additions & 3 deletions test/tasks/ods-finish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestTaskODSFinish(t *testing.T) {
WantRunSuccess: true,
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusFailed)
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusFailed)
},
},
"set bitbucket build status to successful and upload artifacts to temporary Nexus repository": {
Expand All @@ -50,7 +50,7 @@ func TestTaskODSFinish(t *testing.T) {
WantRunSuccess: true,
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
checkArtifactsAreInNexus(t, ctxt, nexus.TemporaryRepositoryDefault)
},
},
Expand All @@ -73,7 +73,7 @@ func TestTaskODSFinish(t *testing.T) {
WantRunSuccess: true,
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
checkArtifactsAreInNexus(t, ctxt, nexus.PermanentRepositoryDefault)
},
},
Expand Down
4 changes: 2 additions & 2 deletions test/tasks/ods-start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestTaskODSStart(t *testing.T) {
checkODSContext(t, wsDir, ctxt.ODS)

bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusInProgress)
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusInProgress)

downloadedArtifact := filepath.Join(wsDir, pipelinectxt.PipelineRunsPath, "foo-zh9gt0.json")
if _, err := os.Stat(downloadedArtifact); os.IsNotExist(err) {
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestTaskODSStart(t *testing.T) {
checkFileContent(t, destinationArtifactsBaseDir, xUnitFileSource, xUnitContent)

bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusInProgress)
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusInProgress)

},
},
Expand Down

0 comments on commit 51d13ac

Please sign in to comment.