Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 0b2f5a0

Browse files
author
Michael Sauter
committed
Check Bitbucket build status in E2E test
Closes #187.
1 parent c41cb49 commit 0b2f5a0

File tree

9 files changed

+65
-46
lines changed

9 files changed

+65
-46
lines changed

pkg/bitbucket/branches.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ type Branch struct {
1616
}
1717

1818
type BranchPage struct {
19-
Size int `json:"size"`
20-
Limit int `json:"limit"`
21-
IsLastPage bool `json:"isLastPage"`
22-
Values []Branch
23-
Start int `json:"start"`
19+
Size int `json:"size"`
20+
Limit int `json:"limit"`
21+
IsLastPage bool `json:"isLastPage"`
22+
Values []Branch `json:"values"`
23+
Start int `json:"start"`
2424
}
2525

2626
type BranchListParams struct {

pkg/bitbucket/build_status.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bitbucket
33
import (
44
"encoding/json"
55
"fmt"
6+
"net/url"
67
)
78

89
const (
@@ -41,26 +42,39 @@ func (c *Client) BuildStatusCreate(gitCommit string, payload BuildStatusCreatePa
4142
return nil
4243
}
4344

44-
type BuildStatusPage struct {
45-
Size int `json:"size"`
46-
Limit int `json:"limit"`
47-
Islastpage bool `json:"isLastPage"`
48-
BuildStatus []BuildStatus `json:"values"`
49-
Start int `json:"start"`
50-
}
5145
type BuildStatus struct {
5246
State string `json:"state"`
5347
Key string `json:"key"`
5448
Name string `json:"name"`
5549
URL string `json:"url"`
5650
Description string `json:"description"`
57-
Dateadded int64 `json:"dateAdded"`
51+
DateAdded int64 `json:"dateAdded"`
52+
}
53+
54+
type BuildStatusPage struct {
55+
Size int `json:"size"`
56+
Limit int `json:"limit"`
57+
IsLastPage bool `json:"isLastPage"`
58+
Values []BuildStatus `json:"values"`
59+
Start int `json:"start"`
60+
}
61+
62+
type BuildStatusListParams struct {
63+
// OrderBy determines how the results should be ordered.
64+
// Options are NEWEST, OLDEST, STATUS. Defaults to NEWEST if not provided.
65+
OrderBy string `json:"orderBy"`
5866
}
5967

60-
// BuildStatusGet gets the build statuses associated with a commit.
68+
// BuildStatusList gets the build statuses associated with a commit.
6169
// https://docs.atlassian.com/bitbucket-server/rest/7.13.0/bitbucket-build-rest.html#idp8
62-
func (c *Client) BuildStatusGet(gitCommit string) (*BuildStatus, error) {
63-
urlPath := fmt.Sprintf("/rest/build-status/1.0/commits/%s", gitCommit)
70+
func (c *Client) BuildStatusList(gitCommit string, params BuildStatusListParams) (*BuildStatusPage, error) {
71+
q := url.Values{}
72+
q.Add("orderBy", params.OrderBy)
73+
urlPath := fmt.Sprintf(
74+
"/rest/build-status/1.0/commits/%s?%s",
75+
gitCommit,
76+
q.Encode(),
77+
)
6478
statusCode, response, err := c.get(urlPath)
6579
if err != nil {
6680
return nil, fmt.Errorf("request returned error: %w", err)
@@ -75,7 +89,7 @@ func (c *Client) BuildStatusGet(gitCommit string) (*BuildStatus, error) {
7589
"could not unmarshal response: %w. status code: %d, body: %s", err, statusCode, string(response),
7690
)
7791
}
78-
return &BuildStatusPage.BuildStatus[0], nil // return the newest by default
92+
return &BuildStatusPage, nil
7993
case 401:
8094
return nil, fmt.Errorf("you are not permitted to get the build status of git commit %s", gitCommit)
8195
default:

pkg/bitbucket/commits.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ type Commit struct {
2727
}
2828

2929
type CommitPage struct {
30-
Size int `json:"size"`
31-
Limit int `json:"limit"`
32-
IsLastPage bool `json:"isLastPage"`
33-
Values []Commit
34-
Start int `json:"start"`
35-
AuthorCount int `json:"authorCount"`
36-
TotalCount int `json:"totalCount"`
30+
Size int `json:"size"`
31+
Limit int `json:"limit"`
32+
IsLastPage bool `json:"isLastPage"`
33+
Values []Commit `json:"values"`
34+
Start int `json:"start"`
35+
AuthorCount int `json:"authorCount"`
36+
TotalCount int `json:"totalCount"`
3737
}
3838

3939
type PullRequestPage struct {

pkg/bitbucket/tags.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ type Tag struct {
1616
}
1717

1818
type TagPage struct {
19-
Size int `json:"size"`
20-
Limit int `json:"limit"`
21-
IsLastPage bool `json:"isLastPage"`
22-
Values []Tag
23-
Start int `json:"start"`
19+
Size int `json:"size"`
20+
Limit int `json:"limit"`
21+
IsLastPage bool `json:"isLastPage"`
22+
Values []Tag `json:"values"`
23+
Start int `json:"start"`
2424
}
2525

2626
type TagCreatePayload struct {

pkg/tasktesting/bitbucket.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,18 @@ func BitbucketClientOrFatal(t *testing.T, c *kclient.Clientset, namespace string
3333
})
3434
return bitbucketClient
3535
}
36+
37+
func CheckBitbucketBuildStatus(t *testing.T, c *bitbucket.Client, gitCommit, wantBuildStatus string) {
38+
buildStatusPage, err := c.BuildStatusList(gitCommit, bitbucket.BuildStatusListParams{})
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
if buildStatusPage == nil || len(buildStatusPage.Values) == 0 {
43+
t.Fatal("no build status found")
44+
}
45+
buildStatus := buildStatusPage.Values[0]
46+
if buildStatus.State != wantBuildStatus {
47+
t.Fatalf("Got: %s, want: %s", buildStatus.State, wantBuildStatus)
48+
}
49+
50+
}

test/e2e/e2e_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ func TestWebhookInterceptor(t *testing.T) {
148148
t.Log(logs)
149149
t.Fatal()
150150
}
151+
152+
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, odsContext.GitCommitSHA, bitbucket.BuildStatusSuccessful)
151153
}
152154

153155
func waitForServiceToBeReady(t *testing.T, clientset *k8s.Clientset, ns, name string, timeout time.Duration) error {

test/tasks/common_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"time"
1111

1212
"github.com/opendevstack/pipeline/internal/kubernetes"
13-
"github.com/opendevstack/pipeline/pkg/bitbucket"
1413
"github.com/opendevstack/pipeline/pkg/config"
1514
"github.com/opendevstack/pipeline/pkg/pipelinectxt"
1615
"github.com/opendevstack/pipeline/pkg/sonar"
@@ -177,14 +176,3 @@ func createODSYML(wsDir string, o *config.ODS) error {
177176
filename := filepath.Join(wsDir, "ods.yaml")
178177
return ioutil.WriteFile(filename, y, 0644)
179178
}
180-
181-
func checkBuildStatus(t *testing.T, c *bitbucket.Client, gitCommit, wantBuildStatus string) {
182-
buildStatus, err := c.BuildStatusGet(gitCommit)
183-
if err != nil {
184-
t.Fatal(err)
185-
}
186-
if buildStatus.State != wantBuildStatus {
187-
t.Fatalf("Got: %s, want: %s", buildStatus.State, wantBuildStatus)
188-
}
189-
190-
}

test/tasks/ods-finish_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestTaskODSFinish(t *testing.T) {
3232
WantRunSuccess: true,
3333
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
3434
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
35-
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusFailed)
35+
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusFailed)
3636
},
3737
},
3838
"set bitbucket build status to successful and upload artifacts to temporary Nexus repository": {
@@ -50,7 +50,7 @@ func TestTaskODSFinish(t *testing.T) {
5050
WantRunSuccess: true,
5151
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
5252
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
53-
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
53+
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
5454
checkArtifactsAreInNexus(t, ctxt, nexus.TemporaryRepositoryDefault)
5555
},
5656
},
@@ -73,7 +73,7 @@ func TestTaskODSFinish(t *testing.T) {
7373
WantRunSuccess: true,
7474
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
7575
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
76-
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
76+
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
7777
checkArtifactsAreInNexus(t, ctxt, nexus.PermanentRepositoryDefault)
7878
},
7979
},

test/tasks/ods-start_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestTaskODSStart(t *testing.T) {
5757
checkODSContext(t, wsDir, ctxt.ODS)
5858

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

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

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

147147
},
148148
},

0 commit comments

Comments
 (0)