Skip to content

Commit

Permalink
use GetCommit by ref instead of GetBranch
Browse files Browse the repository at this point in the history
more universal as applies more cases:
- branches
- tags
- commits
  • Loading branch information
lukmdo committed Jun 28, 2020
1 parent 763b691 commit 468bb69
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 23 deletions.
4 changes: 2 additions & 2 deletions cmd/ci_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions cmd/ci_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions cmd/ci_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions cmd/mr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?",
Expand All @@ -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?",
Expand Down
8 changes: 4 additions & 4 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 21 additions & 11 deletions internal/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 468bb69

Please sign in to comment.