Skip to content

Commit

Permalink
base changes - WIP for review
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmdo committed Jun 26, 2020
1 parent 885eea3 commit 763b691
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 45 deletions.
14 changes: 10 additions & 4 deletions cmd/ci_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand All @@ -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"))
}
Expand Down
20 changes: 14 additions & 6 deletions cmd/ci_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@ 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)
}
if len(args) > 1 {
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 {
Expand All @@ -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
Expand All @@ -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")
}
Expand Down
26 changes: 16 additions & 10 deletions cmd/ci_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import (
)

var (
projectID int
branch string
projectID int
branchName string
commitSHA string
)

// ciViewCmd represents the ci command
Expand All @@ -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 {
Expand All @@ -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)

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"))
Expand Down
18 changes: 13 additions & 5 deletions cmd/mr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}

Expand Down
16 changes: 8 additions & 8 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
}

// 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
30 changes: 18 additions & 12 deletions internal/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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)
}
})
}
}
Expand Down

0 comments on commit 763b691

Please sign in to comment.