Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: differentiate between refs and sha gitab listcheckrunsforref #5

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading