This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
status.go
160 lines (144 loc) · 4.33 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package status
import (
"fmt"
"time"
ciTraceCmd "github.com/profclems/glab/commands/ci/trace"
"github.com/profclems/glab/commands/cmdutils"
"github.com/profclems/glab/internal/git"
"github.com/profclems/glab/internal/utils"
"github.com/profclems/glab/pkg/api"
"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
"github.com/gosuri/uilive"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
)
func NewCmdStatus(f *cmdutils.Factory) *cobra.Command {
var pipelineStatusCmd = &cobra.Command{
Use: "status [flags]",
Short: `View a running CI pipeline on current or other branch specified`,
Aliases: []string{"stats"},
Example: heredoc.Doc(`
$ glab ci status --live
$ glab ci status --branch=master // Get pipeline for master branch
$ glab ci status // Get pipeline for current branch
`),
Long: ``,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
apiClient, err := f.HttpClient()
if err != nil {
return err
}
repo, err := f.BaseRepo()
if err != nil {
return err
}
branch, _ := cmd.Flags().GetString("branch")
live, _ := cmd.Flags().GetBool("live")
if branch == "" {
branch, err = git.CurrentBranch()
if err != nil {
return err
}
}
l := &gitlab.ListProjectPipelinesOptions{
Ref: gitlab.String(branch),
Sort: gitlab.String("desc"),
}
l.Page = 1
l.PerPage = 1
pipes, err := api.GetPipelines(apiClient, l, repo.FullName())
if err != nil {
return err
}
if len(pipes) > 0 {
runningPipeline := pipes[0]
isRunning := true
retry := "Exit"
writer := uilive.New()
// start listening for updates and render
writer.Start()
defer writer.Stop()
for isRunning {
jobs, err := api.GetPipelineJobs(apiClient, runningPipeline.ID, repo.FullName())
if err != nil {
return err
}
for _, job := range jobs {
duration := utils.PrettyTimeAgo(time.Duration(job.Duration))
var status string
switch s := job.Status; s {
case "failed":
if job.AllowFailure {
status = utils.Yellow(s)
} else {
status = utils.Red(s)
}
case "success":
status = utils.Green(s)
default:
status = utils.Gray(s)
}
//fmt.Println(job.Tag)
fmt.Fprintf(writer, "(%s) • %s\t%s\t\t%s\n", status, utils.Gray(duration), job.Stage, job.Name)
}
fmt.Fprintf(writer.Newline(), "\n%s\n", runningPipeline.WebURL)
fmt.Fprintf(writer.Newline(), "SHA: %s\n", runningPipeline.SHA)
fmt.Fprintf(writer.Newline(), "Pipeline State: %s\n\n", runningPipeline.Status)
if runningPipeline.Status == "running" && live {
pipes, err = api.GetPipelines(apiClient, l, repo.FullName())
if err != nil {
return err
}
runningPipeline = pipes[0]
} else {
prompt := &survey.Select{
Message: "Choose an action:",
Options: []string{"View Logs", "Retry", "Exit"},
Default: "Exit",
}
_ = survey.AskOne(prompt, &retry)
if retry != "" && retry != "Exit" {
if retry == "View Logs" {
isRunning = false
} else {
_, err = api.RetryPipeline(apiClient, runningPipeline.ID, repo.FullName())
if err != nil {
return err
}
pipes, err = api.GetPipelines(apiClient, l, repo.FullName())
if err != nil {
return err
}
runningPipeline = pipes[0]
isRunning = true
}
} else {
isRunning = false
}
}
if retry == "View Logs" {
// ToDo: bad idea to call another sub-command. should be fixed to avoid cyclo imports
// and the a shared function placed in the ciutils sub-module
return ciTraceCmd.TraceRun(&ciTraceCmd.TraceOpts{
Branch: branch,
JobID: 0,
BaseRepo: f.BaseRepo,
HTTPClient: f.HttpClient,
IO: f.IO,
})
}
}
return nil
}
redCheck := utils.Red("✘")
fmt.Fprintf(f.IO.StdOut, "%s No pipelines running or available on %s branch\n", redCheck, branch)
return nil
},
}
pipelineStatusCmd.Flags().BoolP("live", "l", false, "Show status in real-time till pipeline ends")
pipelineStatusCmd.Flags().StringP("branch", "b", "", "Check pipeline status for a branch. (Default is current branch)")
return pipelineStatusCmd
}