Skip to content

Commit

Permalink
Merge pull request #361 from claytonrcarter/ci-status
Browse files Browse the repository at this point in the history
ci status --wait: actually wait for jobs to finish
  • Loading branch information
zaquestion authored Feb 20, 2020
2 parents fa99382 + ea586a4 commit da7485d
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions cmd/ci_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"log"
"os"
"text/tabwriter"
"time"

"github.com/pkg/errors"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)
Expand Down Expand Up @@ -45,37 +47,50 @@ lab ci status --wait`,
pid := rn

w := tabwriter.NewWriter(os.Stdout, 2, 4, 1, byte(' '), 0)
jobs, err := lab.CIJobs(pid, branch)
if err != nil {
log.Fatal(errors.Wrap(err, "failed to find ci jobs"))
}
jobs = latestJobs(jobs)

if len(jobs) == 0 {
return
}

wait, err := cmd.Flags().GetBool("wait")
if err != nil {
log.Fatal(err)
}

var jobs []*gitlab.Job

fmt.Fprintln(w, "Stage:\tName\t-\tStatus")
for {
// fetch all of the CI Jobs from the API
jobs, err = lab.CIJobs(pid, branch)
if err != nil {
log.Fatal(errors.Wrap(err, "failed to find ci jobs"))
}

// filter out old jobs
jobs = latestJobs(jobs)

if len(jobs) == 0 {
log.Fatal("no CI jobs found for branch ", branch, " on remote ", remote)
return
}

// print the status of all current jobs
for _, job := range jobs {
fmt.Fprintf(w, "%s:\t%s\t-\t%s\n", job.Stage, job.Name, job.Status)
}
if !wait {
break
}
if jobs[0].Pipeline.Status != "pending" &&
jobs[0].Pipeline.Status != "running" {

dontWaitForJobsToFinish := !wait ||
(jobs[0].Pipeline.Status != "pending" &&
jobs[0].Pipeline.Status != "running")
if dontWaitForJobsToFinish {
break
}

fmt.Fprintln(w)

// don't spam the api TOO much
time.Sleep(1 * time.Second)
}

fmt.Fprintf(w, "\nPipeline Status: %s\n", jobs[0].Pipeline.Status)
// exit w/ status code 1 to indicate a job failure
if wait && jobs[0].Pipeline.Status != "success" {
os.Exit(1)
}
Expand Down

0 comments on commit da7485d

Please sign in to comment.