Skip to content

Commit

Permalink
gitlab: Use pipeline ID for getting jobs
Browse files Browse the repository at this point in the history
We are currently passing a commit SHA, which we use to get a list of
matching pipelines, from which we pick the most recent one.

However that same pipeline is already available in the LastPipeline
property of the commit object, so just use that directly.
  • Loading branch information
fmuellner committed Dec 14, 2020
1 parent c17efd0 commit 6f7973e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cmd/ci_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ lab ci status --wait`,
if err != nil {
log.Fatal(err)
}
commitSHA = commit.ID
pipelineID = commit.LastPipeline.ID

w := tabwriter.NewWriter(os.Stdout, 2, 4, 1, byte(' '), 0)

Expand All @@ -56,7 +56,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, commitSHA)
jobs, err = lab.CIJobs(pid, pipelineID)
if err != nil {
log.Fatal(errors.Wrap(err, "failed to find ci jobs"))
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/ci_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ var ciTraceCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}
commitSHA = commit.ID
pipelineID = commit.LastPipeline.ID

err = doTrace(context.Background(), os.Stdout, projectID, commitSHA, jobName)
err = doTrace(context.Background(), os.Stdout, projectID, pipelineID, jobName)
if err != nil {
log.Fatal(err)
}
},
}

func doTrace(ctx context.Context, w io.Writer, pid interface{}, sha, name string) error {
func doTrace(ctx context.Context, w io.Writer, pid interface{}, pipelineID int, name string) error {
var (
once sync.Once
offset int64
Expand All @@ -66,7 +66,7 @@ func doTrace(ctx context.Context, w io.Writer, pid interface{}, sha, name string
if ctx.Err() == context.Canceled {
break
}
trace, job, err := lab.CITrace(pid, sha, name)
trace, job, err := lab.CITrace(pid, pipelineID, name)
if err != nil || job == nil || trace == nil {
return errors.Wrap(err, "failed to find job")
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/ci_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
)

var (
projectID int
refName string
commitSHA string
projectID int
refName string
pipelineID int
)

// ciViewCmd represents the ci command
Expand Down Expand Up @@ -67,7 +67,7 @@ Feedback Encouraged!: https://github.com/zaquestion/lab/issues`,
if err != nil {
log.Fatal(err)
}
commitSHA = commit.ID
pipelineID = commit.LastPipeline.ID
root := tview.NewPages()
root.SetBorderPadding(1, 1, 2, 2)

Expand Down Expand Up @@ -177,7 +177,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, commitSHA, curJob.Name)
err := doTrace(ctx, os.Stdout, projectID, pipelineID, curJob.Name)
if err != nil {
a.Stop()
log.Fatal(err)
Expand Down Expand Up @@ -336,7 +336,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, commitSHA, curJob.Name)
err := doTrace(context.Background(), vtclean.NewWriter(tview.ANSIWriter(tv), true), projectID, pipelineID, curJob.Name)
if err != nil {
app.Stop()
log.Fatal(err)
Expand Down Expand Up @@ -479,7 +479,7 @@ func updateJobs(app *tview.Application, jobsCh chan []*gitlab.Job) {
time.Sleep(time.Second * 1)
continue
}
jobs, err := lab.CIJobs(projectID, commitSHA)
jobs, err := lab.CIJobs(projectID, pipelineID)
if len(jobs) == 0 || err != nil {
app.Stop()
log.Fatal(errors.Wrap(err, "failed to find ci jobs"))
Expand Down
17 changes: 5 additions & 12 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,24 +833,17 @@ func (s JobSorter) Less(i, j int) bool {
return time.Time(*s.Jobs[i].CreatedAt).Before(time.Time(*s.Jobs[j].CreatedAt))
}

// CIJobs returns a list of jobs in a pipeline for a given sha. The jobs are
// CIJobs returns a list of jobs in the pipeline with given id. The jobs are
// returned sorted by their CreatedAt time
func CIJobs(pid interface{}, sha string) ([]*gitlab.Job, error) {
pipelines, _, err := lab.Pipelines.ListProjectPipelines(pid, &gitlab.ListProjectPipelinesOptions{
SHA: gitlab.String(sha),
})
if len(pipelines) == 0 || err != nil {
return nil, err
}
target := pipelines[0].ID
func CIJobs(pid interface{}, id int) ([]*gitlab.Job, error) {
opts := &gitlab.ListJobsOptions{
ListOptions: gitlab.ListOptions{
PerPage: 500,
},
}
list := make([]*gitlab.Job, 0)
for {
jobs, resp, err := lab.Jobs.ListPipelineJobs(pid, target, opts)
jobs, resp, err := lab.Jobs.ListPipelineJobs(pid, id, opts)
if err != nil {
return nil, err
}
Expand All @@ -874,8 +867,8 @@ func CIJobs(pid interface{}, sha string) ([]*gitlab.Job, error) {
// 1. Last Running Job
// 2. First Pending Job
// 3. Last Job in Pipeline
func CITrace(pid interface{}, sha, name string) (io.Reader, *gitlab.Job, error) {
jobs, err := CIJobs(pid, sha)
func CITrace(pid interface{}, id int, name string) (io.Reader, *gitlab.Job, error) {
jobs, err := CIJobs(pid, id)
if len(jobs) == 0 || err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 6f7973e

Please sign in to comment.