From 3ccb7c220d4304de585cfe8c275f86bbc225fee1 Mon Sep 17 00:00:00 2001 From: Allen Shearin Date: Tue, 12 Dec 2023 11:35:59 -0700 Subject: [PATCH] fix: differentiate between refs and sha gitab listcheckrunsforref --- clients/gitlabrepo/checkruns.go | 18 ++++++++++++++---- clients/gitlabrepo/checkruns_test.go | 6 +++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/clients/gitlabrepo/checkruns.go b/clients/gitlabrepo/checkruns.go index 80f62546e112..8bb9a5963433 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,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) } diff --git a/clients/gitlabrepo/checkruns_test.go b/clients/gitlabrepo/checkruns_test.go index c11ff2b54086..94921ead4be3 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, }, } @@ -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) }