From 468bb6916394a58f32ee962b98cdbdbbefac920f Mon Sep 17 00:00:00 2001 From: Lukasz Dobrzanski Date: Sun, 28 Jun 2020 21:21:40 +0100 Subject: [PATCH] use GetCommit by ref instead of GetBranch more universal as applies more cases: - branches - tags - commits --- cmd/ci_status.go | 4 ++-- cmd/ci_trace.go | 4 ++-- cmd/ci_view.go | 5 +++-- cmd/mr_create.go | 4 ++-- internal/gitlab/gitlab.go | 8 ++++---- internal/gitlab/gitlab_test.go | 32 +++++++++++++++++++++----------- 6 files changed, 34 insertions(+), 23 deletions(-) diff --git a/cmd/ci_status.go b/cmd/ci_status.go index bb58ee34..97fcbf2b 100644 --- a/cmd/ci_status.go +++ b/cmd/ci_status.go @@ -46,11 +46,11 @@ lab ci status --wait`, log.Fatal(err) } pid := rn - branch, err := lab.GetBranch(pid, branchName) + commit, err := lab.GetCommit(pid, branchName) if err != nil { log.Fatal(err) } - commitSHA = branch.Commit.ID + commitSHA = commit.ID w := tabwriter.NewWriter(os.Stdout, 2, 4, 1, byte(' '), 0) diff --git a/cmd/ci_trace.go b/cmd/ci_trace.go index 0d732533..18450b12 100644 --- a/cmd/ci_trace.go +++ b/cmd/ci_trace.go @@ -59,11 +59,11 @@ var ciTraceCmd = &cobra.Command{ log.Fatal(err) } projectID = project.ID - branch, err := lab.GetBranch(projectID, branchName) + commit, err := lab.GetCommit(projectID, branchName) if err != nil { log.Fatal(err) } - commitSHA = branch.Commit.ID + commitSHA = commit.ID err = doTrace(context.Background(), os.Stdout, project.ID, commitSHA, jobName) if err != nil { diff --git a/cmd/ci_view.go b/cmd/ci_view.go index 2ffae872..84846923 100644 --- a/cmd/ci_view.go +++ b/cmd/ci_view.go @@ -56,6 +56,7 @@ Feedback Encouraged!: https://github.com/zaquestion/lab/issues`, } if len(args) > 1 { + // branchName may also be a tag branchName = args[1] } remote = determineSourceRemote(branchName) @@ -78,11 +79,11 @@ Feedback Encouraged!: https://github.com/zaquestion/lab/issues`, log.Fatal(err) } projectID = project.ID - branch, err := lab.GetBranch(projectID, branchName) + commit, err := lab.GetCommit(projectID, branchName) if err != nil { log.Fatal(err) } - commitSHA = branch.Commit.ID + commitSHA = commit.ID root := tview.NewPages() root.SetBorderPadding(1, 1, 2, 2) diff --git a/cmd/mr_create.go b/cmd/mr_create.go index 768461c9..ce437daf 100644 --- a/cmd/mr_create.go +++ b/cmd/mr_create.go @@ -96,7 +96,7 @@ func runMRCreate(cmd *cobra.Command, args []string) { if err != nil { log.Fatal(err) } - if _, err := lab.GetBranch(p.ID, branch); err != nil { + if _, err := lab.GetCommit(p.ID, branch); err != nil { err = errors.Wrapf( err, "aborting MR, source branch %s not present on remote %s. did you forget to push?", @@ -123,7 +123,7 @@ func runMRCreate(cmd *cobra.Command, args []string) { targetBranch := "master" if len(args) > 1 && targetBranch != args[1] { targetBranch = args[1] - if _, err := lab.GetBranch(targetProject.ID, targetBranch); err != nil { + if _, err := lab.GetCommit(targetProject.ID, targetBranch); err != nil { err = errors.Wrapf( err, "aborting MR, target branch %s not present on remote %s. did you forget to push?", diff --git a/internal/gitlab/gitlab.go b/internal/gitlab/gitlab.go index 92d96748..4fd8c46a 100644 --- a/internal/gitlab/gitlab.go +++ b/internal/gitlab/gitlab.go @@ -506,13 +506,13 @@ func IssueListDiscussions(project string, issueNum int) ([]*gitlab.Discussion, e return discussions, nil } -// GetBranch looks up a Gitlab Branch by name. -func GetBranch(pid interface{}, branch string) (*gitlab.Branch, error) { - b, _, err := lab.Branches.GetBranch(pid, branch) +// GetCommit returns top Commit by ref (hash, branch or tag). +func GetCommit(pid interface{}, ref string) (*gitlab.Commit, error) { + c, _, err := lab.Commits.GetCommit(pid, ref) if err != nil { return nil, err } - return b, nil + return c, nil } // LabelList gets a list of labels on a GitLab Project diff --git a/internal/gitlab/gitlab_test.go b/internal/gitlab/gitlab_test.go index 76d30ff0..eba6849a 100644 --- a/internal/gitlab/gitlab_test.go +++ b/internal/gitlab/gitlab_test.go @@ -111,36 +111,46 @@ func TestLint(t *testing.T) { } } -func TestGetBranch(t *testing.T) { +func TestGetCommit(t *testing.T) { tests := []struct { - desc string - branch string - ok bool + desc string + ref string + ok bool + expectID string }{ { - "branch not pushed", + "not pushed", "not_a_branch", false, + "", + }, + { + "pushed branch", + "mrtest", // branch name + true, + "54fd49a2ac60aeeef5ddc75efecd49f85f7ba9b0", }, { - "branch is pushed", - "mrtest", + "pushed branch, neeeds encoding", + "needs/encode", // branch name true, + "381f2b123dd404e8046ea42d5785061aa3b6674b", }, { - "needs encoding is pushed", - "needs/encode", + "pushed sha", + "700e056463504690c11d63727bf25a380f303be9", true, + "700e056463504690c11d63727bf25a380f303be9", }, } for _, test := range tests { t.Run(test.desc, func(t *testing.T) { test := test t.Parallel() - b, err := GetBranch(4181224, test.branch) + b, err := GetCommit(4181224, test.ref) if test.ok { require.NoError(t, err) - require.Equal(t, test.branch, b.Name) + require.Equal(t, test.expectID, b.ID) } else { require.Error(t, err) require.Nil(t, b)