Skip to content

Commit

Permalink
fix: differentiate between refs and sha gitab listcheckrunsforref
Browse files Browse the repository at this point in the history
Signed-off-by: Allen Shearin <[email protected]>
  • Loading branch information
ashearin committed Dec 12, 2023
1 parent 30ef6b1 commit 1a1079e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
18 changes: 14 additions & 4 deletions clients/gitlabrepo/checkruns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package gitlabrepo

import (
"fmt"
"regexp"

"github.com/xanzy/go-gitlab"

Expand All @@ -32,11 +33,20 @@ func (handler *checkrunsHandler) init(repourl *repoURL) {
}

func (handler *checkrunsHandler) listCheckRunsForRef(ref string) ([]clients.CheckRun, error) {
commit := regexp.MustCompile("^[a-f0-9]{40}$")
options := gitlab.ListProjectPipelinesOptions{
ListOptions: gitlab.ListOptions{},
}

// In Gitlab: ref refers to a branch or tag name, and sha refers to commit sha
if commit.MatchString(ref) {
options.SHA = &ref
} else {
options.Ref = &ref
}

pipelines, _, err := handler.glClient.Pipelines.ListProjectPipelines(
handler.repourl.projectID, &gitlab.ListProjectPipelinesOptions{
SHA: &ref,
ListOptions: gitlab.ListOptions{},
})
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 1a1079e

Please sign in to comment.