Skip to content

Commit

Permalink
🌱 differentiate between refs and sha gitab (#3729)
Browse files Browse the repository at this point in the history
* fix: differentiate between refs and sha gitab listcheckrunsforref

Signed-off-by: Allen Shearin <[email protected]>

* address pr comments

Signed-off-by: Allen Shearin <[email protected]>

* style: move gitlab call to one line

Signed-off-by: Allen Shearin <[email protected]>

* update gitlab api comments

Signed-off-by: Allen Shearin <[email protected]>

---------

Signed-off-by: Allen Shearin <[email protected]>
  • Loading branch information
ashearin authored Dec 18, 2023
1 parent f4bf574 commit df7d888
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
21 changes: 16 additions & 5 deletions clients/gitlabrepo/checkruns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ package gitlabrepo

import (
"fmt"
"regexp"

"github.com/xanzy/go-gitlab"

"github.com/ossf/scorecard/v4/clients"
)

var gitCommitHashRegex = regexp.MustCompile(`^[a-fA-F0-9]{40}$`)

type checkrunsHandler struct {
glClient *gitlab.Client
repourl *repoURL
Expand All @@ -32,11 +35,19 @@ func (handler *checkrunsHandler) init(repourl *repoURL) {
}

func (handler *checkrunsHandler) listCheckRunsForRef(ref string) ([]clients.CheckRun, error) {
pipelines, _, err := handler.glClient.Pipelines.ListProjectPipelines(
handler.repourl.projectID, &gitlab.ListProjectPipelinesOptions{
SHA: &ref,
ListOptions: gitlab.ListOptions{},
})
var options gitlab.ListProjectPipelinesOptions

if gitCommitHashRegex.MatchString(ref) {
options.SHA = &ref
} else {
options.Ref = &ref
}

// Notes for Gitlab ListProjectPipelines endpoint:
// Only full SHA works for SHA param, Short SHA does not work
// Branch names work for Ref Param, tags and SHAs do not work
// Reference: https://docs.gitlab.com/ee/api/pipelines.html#list-project-pipelines
pipelines, _, err := handler.glClient.Pipelines.ListProjectPipelines(handler.repourl.projectID, &options)
if err != nil {
return nil, fmt.Errorf("request for pipelines returned error: %w", err)
}
Expand Down
6 changes: 5 additions & 1 deletion clients/gitlabrepo/checkruns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ func Test_CheckRuns(t *testing.T) {
tests := []struct {
name string
responsePath string
ref string
want []clients.CheckRun
wantErr bool
}{
{
name: "valid checkruns",
responsePath: "./testdata/valid-checkruns",
ref: "main",
want: []clients.CheckRun{
{
Status: "queued",
Expand All @@ -47,11 +49,13 @@ func Test_CheckRuns(t *testing.T) {
{
name: "valid checkruns with zero results",
responsePath: "./testdata/valid-checkruns-1",
ref: "eb94b618fb5865b26e80fdd8ae531b7a63ad851a",
wantErr: false,
},
{
name: "failure fetching the checkruns",
responsePath: "./testdata/invalid-checkruns-result",
ref: "main",
wantErr: true,
},
}
Expand All @@ -77,7 +81,7 @@ func Test_CheckRuns(t *testing.T) {
commitSHA: clients.HeadSHA,
}
handler.init(&repoURL)
got, err := handler.listCheckRunsForRef("main")
got, err := handler.listCheckRunsForRef(tt.ref)
if (err != nil) != tt.wantErr {
t.Fatalf("checkRuns error: %v, wantedErr: %t", err, tt.wantErr)
}
Expand Down

0 comments on commit df7d888

Please sign in to comment.