From 763b691dcb3537b9d8999377779147e95e7e494b Mon Sep 17 00:00:00 2001 From: Lukasz Dobrzanski Date: Fri, 26 Jun 2020 19:32:02 +0100 Subject: [PATCH] base changes - WIP for review --- cmd/ci_status.go | 14 ++++++++++---- cmd/ci_trace.go | 20 ++++++++++++++------ cmd/ci_view.go | 26 ++++++++++++++++---------- cmd/mr_create.go | 18 +++++++++++++----- internal/gitlab/gitlab.go | 16 ++++++++-------- internal/gitlab/gitlab_test.go | 30 ++++++++++++++++++------------ 6 files changed, 79 insertions(+), 45 deletions(-) diff --git a/cmd/ci_status.go b/cmd/ci_status.go index 71c8a1f7..bb58ee34 100644 --- a/cmd/ci_status.go +++ b/cmd/ci_status.go @@ -24,15 +24,16 @@ var ciStatusCmd = &cobra.Command{ lab ci status --wait`, RunE: nil, Run: func(cmd *cobra.Command, args []string) { - branch, err := git.CurrentBranch() + var err error + branchName, err = git.CurrentBranch() if err != nil { log.Fatal(err) } if len(args) > 1 { - branch = args[1] + branchName = args[1] } - remote := determineSourceRemote(branch) + remote := determineSourceRemote(branchName) if len(args) > 0 { ok, err := git.IsRemote(args[0]) if err != nil || !ok { @@ -45,6 +46,11 @@ lab ci status --wait`, log.Fatal(err) } pid := rn + branch, err := lab.GetBranch(pid, branchName) + if err != nil { + log.Fatal(err) + } + commitSHA = branch.Commit.ID w := tabwriter.NewWriter(os.Stdout, 2, 4, 1, byte(' '), 0) @@ -58,7 +64,7 @@ lab ci status --wait`, fmt.Fprintln(w, "Stage:\tName\t-\tStatus") for { // fetch all of the CI Jobs from the API - jobs, err = lab.CIJobs(pid, branch) + jobs, err = lab.CIJobs(pid, commitSHA) if err != nil { log.Fatal(errors.Wrap(err, "failed to find ci jobs")) } diff --git a/cmd/ci_trace.go b/cmd/ci_trace.go index ba2f704e..0d732533 100644 --- a/cmd/ci_trace.go +++ b/cmd/ci_trace.go @@ -27,9 +27,10 @@ var ciTraceCmd = &cobra.Command{ var ( remote string jobName string + err error ) - branch, err := git.CurrentBranch() + branchName, err = git.CurrentBranch() if err != nil { log.Fatal(err) } @@ -37,10 +38,10 @@ var ciTraceCmd = &cobra.Command{ jobName = args[1] if strings.Contains(args[1], ":") { ps := strings.Split(args[1], ":") - branch, jobName = ps[0], ps[1] + branchName, jobName = ps[0], ps[1] } } - remote = determineSourceRemote(branch) + remote = determineSourceRemote(branchName) if len(args) > 0 { ok, err := git.IsRemote(args[0]) if err != nil || !ok { @@ -57,14 +58,21 @@ var ciTraceCmd = &cobra.Command{ if err != nil { log.Fatal(err) } - err = doTrace(context.Background(), os.Stdout, project.ID, branch, jobName) + projectID = project.ID + branch, err := lab.GetBranch(projectID, branchName) + if err != nil { + log.Fatal(err) + } + commitSHA = branch.Commit.ID + + err = doTrace(context.Background(), os.Stdout, project.ID, commitSHA, jobName) if err != nil { log.Fatal(err) } }, } -func doTrace(ctx context.Context, w io.Writer, pid interface{}, branch, name string) error { +func doTrace(ctx context.Context, w io.Writer, pid interface{}, sha, name string) error { var ( once sync.Once offset int64 @@ -73,7 +81,7 @@ func doTrace(ctx context.Context, w io.Writer, pid interface{}, branch, name str if ctx.Err() == context.Canceled { break } - trace, job, err := lab.CITrace(pid, branch, name) + trace, job, err := lab.CITrace(pid, sha, name) if err != nil || job == nil || trace == nil { return errors.Wrap(err, "failed to find job") } diff --git a/cmd/ci_view.go b/cmd/ci_view.go index 14f39b61..2ffae872 100644 --- a/cmd/ci_view.go +++ b/cmd/ci_view.go @@ -24,8 +24,9 @@ import ( ) var ( - projectID int - branch string + projectID int + branchName string + commitSHA string ) // ciViewCmd represents the ci command @@ -49,15 +50,15 @@ Feedback Encouraged!: https://github.com/zaquestion/lab/issues`, remote string err error ) - branch, err = git.CurrentBranch() + branchName, err = git.CurrentBranch() if err != nil { log.Fatal(err) } if len(args) > 1 { - branch = args[1] + branchName = args[1] } - remote = determineSourceRemote(branch) + remote = determineSourceRemote(branchName) if len(args) > 0 { ok, err := git.IsRemote(args[0]) if err != nil || !ok { @@ -77,6 +78,11 @@ Feedback Encouraged!: https://github.com/zaquestion/lab/issues`, log.Fatal(err) } projectID = project.ID + branch, err := lab.GetBranch(projectID, branchName) + if err != nil { + log.Fatal(err) + } + commitSHA = branch.Commit.ID root := tview.NewPages() root.SetBorderPadding(1, 1, 2, 2) @@ -92,7 +98,7 @@ Feedback Encouraged!: https://github.com/zaquestion/lab/issues`, var navi navigator a.SetInputCapture(inputCapture(a, root, navi, inputCh)) - go updateJobs(a, jobsCh, project.ID, branch) + go updateJobs(a, jobsCh) go func() { defer recoverPanic(a) for { @@ -186,7 +192,7 @@ func inputCapture(a *tview.Application, root *tview.Pages, navi navigator, input a.Suspend(func() { ctx, cancel := context.WithCancel(context.Background()) go func() { - err := doTrace(ctx, os.Stdout, projectID, branch, curJob.Name) + err := doTrace(ctx, os.Stdout, projectID, commitSHA, curJob.Name) if err != nil { a.Stop() log.Fatal(err) @@ -345,7 +351,7 @@ func jobsView(app *tview.Application, jobsCh chan []*gitlab.Job, inputCh chan st tv.SetBorderPadding(0, 0, 1, 1).SetBorder(true) go func() { - err := doTrace(context.Background(), vtclean.NewWriter(tview.ANSIWriter(tv), true), projectID, branch, curJob.Name) + err := doTrace(context.Background(), vtclean.NewWriter(tview.ANSIWriter(tv), true), projectID, commitSHA, curJob.Name) if err != nil { app.Stop() log.Fatal(err) @@ -481,14 +487,14 @@ func recoverPanic(app *tview.Application) { } } -func updateJobs(app *tview.Application, jobsCh chan []*gitlab.Job, pid interface{}, branch string) { +func updateJobs(app *tview.Application, jobsCh chan []*gitlab.Job) { defer recoverPanic(app) for { if modalVisible { time.Sleep(time.Second * 1) continue } - jobs, err := lab.CIJobs(pid, branch) + jobs, err := lab.CIJobs(projectID, commitSHA) if len(jobs) == 0 || err != nil { app.Stop() log.Fatal(errors.Wrap(err, "failed to find ci jobs")) diff --git a/cmd/mr_create.go b/cmd/mr_create.go index 2f278b1c..768461c9 100644 --- a/cmd/mr_create.go +++ b/cmd/mr_create.go @@ -96,8 +96,12 @@ func runMRCreate(cmd *cobra.Command, args []string) { if err != nil { log.Fatal(err) } - if !lab.BranchPushed(p.ID, branch) { - log.Fatalf("aborting MR, source branch %s not present on remote %s. did you forget to push?", branch, sourceRemote) + if _, err := lab.GetBranch(p.ID, branch); err != nil { + err = errors.Wrapf( + err, + "aborting MR, source branch %s not present on remote %s. did you forget to push?", + branch, sourceRemote) + log.Fatal(err) } targetRemote := forkedFromRemote @@ -117,10 +121,14 @@ func runMRCreate(cmd *cobra.Command, args []string) { log.Fatal(err) } targetBranch := "master" - if len(args) > 1 { + if len(args) > 1 && targetBranch != args[1] { targetBranch = args[1] - if !lab.BranchPushed(targetProject.ID, targetBranch) { - log.Fatalf("aborting MR, target branch %s not present on remote %s. did you forget to push?", targetBranch, targetRemote) + if _, err := lab.GetBranch(targetProject.ID, targetBranch); err != nil { + err = errors.Wrapf( + err, + "aborting MR, target branch %s not present on remote %s. did you forget to push?", + targetBranch, targetRemote) + log.Fatal(err) } } diff --git a/internal/gitlab/gitlab.go b/internal/gitlab/gitlab.go index f555fdb0..92d96748 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 } -// BranchPushed checks if a branch exists on a GitLab project -func BranchPushed(pid interface{}, branch string) bool { +// GetBranch looks up a Gitlab Branch by name. +func GetBranch(pid interface{}, branch string) (*gitlab.Branch, error) { b, _, err := lab.Branches.GetBranch(pid, branch) if err != nil { - return false + return nil, err } - return b != nil + return b, nil } // LabelList gets a list of labels on a GitLab Project @@ -698,9 +698,9 @@ func ProjectList(opts gitlab.ListProjectsOptions, n int) ([]*gitlab.Project, err // CIJobs returns a list of jobs in a pipeline for a given sha. The jobs are // returned sorted by their CreatedAt time -func CIJobs(pid interface{}, branch string) ([]*gitlab.Job, error) { +func CIJobs(pid interface{}, sha string) ([]*gitlab.Job, error) { pipelines, _, err := lab.Pipelines.ListProjectPipelines(pid, &gitlab.ListProjectPipelinesOptions{ - Ref: gitlab.String(branch), + SHA: gitlab.String(sha), }) if len(pipelines) == 0 || err != nil { return nil, err @@ -739,8 +739,8 @@ func CIJobs(pid interface{}, branch string) ([]*gitlab.Job, error) { // 1. Last Running Job // 2. First Pending Job // 3. Last Job in Pipeline -func CITrace(pid interface{}, branch, name string) (io.Reader, *gitlab.Job, error) { - jobs, err := CIJobs(pid, branch) +func CITrace(pid interface{}, sha, name string) (io.Reader, *gitlab.Job, error) { + jobs, err := CIJobs(pid, sha) if len(jobs) == 0 || err != nil { return nil, nil, err } diff --git a/internal/gitlab/gitlab_test.go b/internal/gitlab/gitlab_test.go index 57b2bafd..76d30ff0 100644 --- a/internal/gitlab/gitlab_test.go +++ b/internal/gitlab/gitlab_test.go @@ -111,14 +111,19 @@ func TestLint(t *testing.T) { } } -func TestBranchPushed(t *testing.T) { +func TestGetBranch(t *testing.T) { tests := []struct { - desc string - branch string - expected bool + desc string + branch string + ok bool }{ { - "alpha is pushed", + "branch not pushed", + "not_a_branch", + false, + }, + { + "branch is pushed", "mrtest", true, }, @@ -127,18 +132,19 @@ func TestBranchPushed(t *testing.T) { "needs/encode", true, }, - { - "alpha not pushed", - "not_a_branch", - false, - }, } for _, test := range tests { t.Run(test.desc, func(t *testing.T) { test := test t.Parallel() - ok := BranchPushed(4181224, test.branch) - require.Equal(t, test.expected, ok) + b, err := GetBranch(4181224, test.branch) + if test.ok { + require.NoError(t, err) + require.Equal(t, test.branch, b.Name) + } else { + require.Error(t, err) + require.Nil(t, b) + } }) } }