Skip to content

Commit

Permalink
Check quality gate for branch / PR
Browse files Browse the repository at this point in the history
Fixes #701.
  • Loading branch information
michaelsauter committed Jun 2, 2023
1 parent 20d8da4 commit 0952aa9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ listed in the changelog.

- Gradle task artifacts are not picked up by cache ([#700](https://github.com/opendevstack/ods-pipeline/issues/700))
- Gradle task ignores `working-dir` param ([#702](https://github.com/opendevstack/ods-pipeline/issues/702))
- SQ quality gate check does not take branch / PR into account ([#701](https://github.com/opendevstack/ods-pipeline/issues/701))

## [0.13.0] - 2023-06-01

Expand Down
8 changes: 5 additions & 3 deletions cmd/sonar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ func sonarScan(

if opts.qualityGate {
logger.Infof("Checking quality gate ...")
qualityGateResult, err := sonarClient.QualityGateGet(
sonar.QualityGateGetParams{Project: sonarProject},
)
qualityGateResult, err := sonarClient.QualityGateGet(sonar.QualityGateGetParams{
ProjectKey: sonarProject,
Branch: ctxt.GitRef,
PullRequest: ctxt.PullRequestKey,
})
if err != nil {
return fmt.Errorf("quality gate could not be retrieved: %w", err)
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/sonar/quality_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ type QualityGatePeriod struct {
}

type QualityGateGetParams struct {
Project string `json:"project"`
ProjectKey string
Branch string
PullRequest string
}

func (c *Client) QualityGateGet(p QualityGateGetParams) (*QualityGate, error) {
urlPath := fmt.Sprintf("/api/qualitygates/project_status?projectKey=%s", p.Project)
urlPath := "/api/qualitygates/project_status?projectKey=" + p.ProjectKey
if p.PullRequest != "" {
urlPath = urlPath + "&pullRequest=" + p.PullRequest
} else if p.Branch != "" {
urlPath = urlPath + "&branch=" + p.Branch
}
statusCode, response, err := c.get(urlPath)
if err != nil {
return &QualityGate{ProjectStatus: QualityGateProjectStatus{Status: QualityGateStatusNone}}, nil
Expand Down
45 changes: 35 additions & 10 deletions pkg/sonar/quality_gate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,56 @@ func TestQualityGateGet(t *testing.T) {
c := testClient(t, srv.Server.URL)

tests := map[string]struct {
Fixture string
WantStatus string
responseFixture string
params QualityGateGetParams
wantRequestURI string
wantStatus string
}{
"ERROR status": {
Fixture: "sonar/project_status_error.json",
WantStatus: "ERROR",
params: QualityGateGetParams{ProjectKey: "foo"},
responseFixture: "sonar/project_status_error.json",
wantRequestURI: "/api/qualitygates/project_status?projectKey=foo",
wantStatus: "ERROR",
},
"OK status": {
Fixture: "sonar/project_status_ok.json",
WantStatus: "OK",
params: QualityGateGetParams{ProjectKey: "foo"},
responseFixture: "sonar/project_status_ok.json",
wantRequestURI: "/api/qualitygates/project_status?projectKey=foo",
wantStatus: "OK",
},
"OK status for branch": {
params: QualityGateGetParams{ProjectKey: "foo", Branch: "bar"},
responseFixture: "sonar/project_status_ok.json",
wantRequestURI: "/api/qualitygates/project_status?projectKey=foo&branch=bar",
wantStatus: "OK",
},
"OK status for PR": {
params: QualityGateGetParams{ProjectKey: "foo", PullRequest: "123"},
responseFixture: "sonar/project_status_ok.json",
wantRequestURI: "/api/qualitygates/project_status?projectKey=foo&pullRequest=123",
wantStatus: "OK",
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
srv.EnqueueResponse(
t, "/api/qualitygates/project_status",
200, tc.Fixture,
200, tc.responseFixture,
)
got, err := c.QualityGateGet(QualityGateGetParams{})
got, err := c.QualityGateGet(tc.params)
if err != nil {
t.Fatalf("Unexpected error on request: %s", err)
}
if got.ProjectStatus.Status != tc.WantStatus {
t.Fatalf("want %s, got %s", tc.WantStatus, got.ProjectStatus.Status)
if got.ProjectStatus.Status != tc.wantStatus {
t.Fatalf("want %s, got %s", tc.wantStatus, got.ProjectStatus.Status)
}
req, err := srv.LastRequest()
if err != nil {
t.Fatal(err)
}
if req.URL.RequestURI() != tc.wantRequestURI {
t.Fatalf("want request URI %s, got %s", tc.wantRequestURI, req.URL.RequestURI())
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion test/tasks/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func checkSonarQualityGate(t *testing.T, c *kclient.Clientset, namespace, sonarP

if qualityGateFlag {
qualityGateResult, err := sonarClient.QualityGateGet(
sonar.QualityGateGetParams{Project: sonarProject},
sonar.QualityGateGetParams{ProjectKey: sonarProject},
)
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 0952aa9

Please sign in to comment.