From 9a1884446bd8db24059d68b355070adde4ce297e Mon Sep 17 00:00:00 2001 From: Allen Shearin Date: Mon, 11 Dec 2023 17:20:39 -0700 Subject: [PATCH] fix: differentiate between refs and sha gitab listcheckrunsforref --- clients/gitlabrepo/checkruns.go | 13 +++++++++++++ clients/gitlabrepo/checkruns_test.go | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/clients/gitlabrepo/checkruns.go b/clients/gitlabrepo/checkruns.go index 80f62546e112..4e483cec243f 100644 --- a/clients/gitlabrepo/checkruns.go +++ b/clients/gitlabrepo/checkruns.go @@ -16,6 +16,7 @@ package gitlabrepo import ( "fmt" + "regexp" "github.com/xanzy/go-gitlab" @@ -32,6 +33,18 @@ 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, diff --git a/clients/gitlabrepo/checkruns_test.go b/clients/gitlabrepo/checkruns_test.go index c11ff2b54086..3aecadcdca86 100644 --- a/clients/gitlabrepo/checkruns_test.go +++ b/clients/gitlabrepo/checkruns_test.go @@ -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", @@ -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, }, }