-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(ci status) support a short text based pipeline/job status
- Loading branch information
1 parent
78c18e0
commit ca87d10
Showing
5 changed files
with
176 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"text/tabwriter" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"github.com/zaquestion/lab/internal/git" | ||
lab "github.com/zaquestion/lab/internal/gitlab" | ||
) | ||
|
||
// ciStatusCmd represents the run command | ||
var ciStatusCmd = &cobra.Command{ | ||
Use: "status [branch]", | ||
Aliases: []string{"run"}, | ||
Short: "Textual representation of a CI pipeline", | ||
Long: ``, | ||
Example: `lab ci status | ||
lab ci status --wait`, | ||
RunE: nil, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
branch, err := git.CurrentBranch() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if len(args) > 1 { | ||
branch = args[1] | ||
} | ||
remote := determineSourceRemote(branch) | ||
if len(args) > 0 { | ||
ok, err := git.IsRemote(args[0]) | ||
if err != nil || !ok { | ||
log.Fatal(args[0], " is not a remote:", err) | ||
} | ||
remote = args[0] | ||
} | ||
rn, err := git.PathWithNameSpace(remote) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
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) | ||
} | ||
|
||
fmt.Fprintln(w, "Stage:\tName\t-\tStatus") | ||
for { | ||
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" { | ||
break | ||
} | ||
fmt.Fprintln(w) | ||
} | ||
|
||
fmt.Fprintf(w, "\nPipeline Status: %s\n", jobs[0].Pipeline.Status) | ||
if wait && jobs[0].Pipeline.Status != "success" { | ||
os.Exit(1) | ||
} | ||
w.Flush() | ||
}, | ||
} | ||
|
||
func init() { | ||
ciStatusCmd.MarkZshCompPositionalArgumentCustom(1, "__lab_completion_remote_branches") | ||
ciStatusCmd.Flags().Bool("wait", false, "Continuously print the status and wait to exit until the pipeline finishes. Exit code indicates pipeline status") | ||
ciCmd.AddCommand(ciStatusCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cmd | ||
|
||
import ( | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_ciStatus(t *testing.T) { | ||
t.Parallel() | ||
repo := copyTestRepo(t) | ||
cmd := exec.Command("../lab_bin", "fetch", "origin") | ||
cmd.Dir = repo | ||
if b, err := cmd.CombinedOutput(); err != nil { | ||
t.Log(string(b)) | ||
t.Fatal(err) | ||
} | ||
|
||
cmd = exec.Command("../lab_bin", "checkout", "origin/ci_test_pipeline") | ||
cmd.Dir = repo | ||
if b, err := cmd.CombinedOutput(); err != nil { | ||
t.Log(string(b)) | ||
t.Fatal(err) | ||
} | ||
|
||
cmd = exec.Command("../lab_bin", "checkout", "-b", "ci_test_pipeline") | ||
cmd.Dir = repo | ||
if b, err := cmd.CombinedOutput(); err != nil { | ||
t.Log(string(b)) | ||
t.Fatal(err) | ||
} | ||
|
||
cmd = exec.Command("../lab_bin", "ci", "status") | ||
cmd.Dir = repo | ||
|
||
b, err := cmd.CombinedOutput() | ||
if err != nil { | ||
t.Log(string(b)) | ||
t.Fatal(err) | ||
} | ||
out := string(b) | ||
assert.Contains(t, out, `Stage: Name - Status | ||
build: build1 - success | ||
build: build2 - success | ||
build: build2:fails - failed | ||
test: test1 - success | ||
test: test2 - success | ||
test: test2:really_a_long_name_for - success | ||
test: test2:no_suffix:test - success | ||
test: test3 - success | ||
deploy: deploy1 - success | ||
deploy: deploy2 - manual | ||
deploy: deploy3:no_sufix:deploy - success | ||
deploy: deploy4 - success | ||
deploy: deploy5:really_a_long_name_for - success | ||
deploy: deploy5 - success | ||
deploy: deploy6 - success | ||
deploy: deploy7 - success | ||
deploy: deploy8 - success | ||
deploy: deploy9 - success | ||
deploy: deploy10 - success`) | ||
|
||
assert.Contains(t, out, "Pipeline Status: success") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters