Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
feat(ci/status): list of external jobs and fix when no jobs are found…
Browse files Browse the repository at this point in the history
… for the pipeline
  • Loading branch information
pvoliveira committed Jun 6, 2021
1 parent 178fdb0 commit 2f49658
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ _test

# VsCode
.vscode/
.devcontainer

# Dist binaries created at build with make rt
/dist
Expand Down
19 changes: 19 additions & 0 deletions api/commit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package api

import "github.com/xanzy/go-gitlab"

var GetCommitStatuses = func(client *gitlab.Client, pid interface{}, sha string) ([]*gitlab.CommitStatus, error) {
if client == nil {
client = apiClient.Lab()
}

opt := &gitlab.GetCommitStatusesOptions{
All: gitlab.Bool(true),
}

statuses, _, err := client.Commits.GetCommitStatuses(pid, sha, opt, nil)
if err != nil {
return nil, err
}
return statuses, nil
}
1 change: 1 addition & 0 deletions commands/ci/ciutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func DisplayMultiplePipelines(c *iostreams.ColorPalette, p []*gitlab.PipelineInf
if pipeline.CreatedAt != nil {
duration = c.Magenta("(" + utils.TimeToPrettyTimeAgo(*pipeline.CreatedAt) + ")")
}

var pipeState string
if pipeline.Status == "success" {
pipeState = c.Green(fmt.Sprintf("(%s) • #%d", pipeline.Status, pipeline.ID))
Expand Down
27 changes: 26 additions & 1 deletion commands/ci/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,33 @@ func TraceRun(opts *TraceOpts) error {
re := regexp.MustCompile(`(?s)\((.*)\)`)
m := re.FindAllStringSubmatch(selectedJob, -1)
opts.JobID = utils.StringToInt(m[0][1])
} else {
} else if len(jobs) > 0 {
opts.JobID = jobs[0].ID
} else {
// use commit statuses to show external jobs
cs, err := api.GetCommitStatuses(apiClient, repo.FullName(), pipeline.SHA)
if err != nil {
return nil
}

c := opts.IO.Color()

fmt.Fprint(opts.IO.StdOut, "Getting external jobs...")
for _, status := range cs {
var s string

switch status.Status {
case "success":
s = c.Green(status.Status)
case "error":
s = c.Red(status.Status)
default:
s = c.Gray(status.Status)
}
fmt.Fprintf(opts.IO.StdOut, "(%s) %s\nURL: %s\n\n", s, c.Bold(status.Name), c.Gray(status.TargetURL))
}

return nil
}
}

Expand Down

0 comments on commit 2f49658

Please sign in to comment.