|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "sort" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/cli/cli/api" |
| 11 | + "github.com/cli/cli/context" |
| 12 | + "github.com/cli/cli/internal/ghrepo" |
| 13 | + "github.com/cli/cli/pkg/cmd/pr/shared" |
| 14 | + "github.com/cli/cli/pkg/cmdutil" |
| 15 | + "github.com/cli/cli/pkg/iostreams" |
| 16 | + "github.com/cli/cli/utils" |
| 17 | + "github.com/spf13/cobra" |
| 18 | +) |
| 19 | + |
| 20 | +type ChecksOptions struct { |
| 21 | + HttpClient func() (*http.Client, error) |
| 22 | + IO *iostreams.IOStreams |
| 23 | + BaseRepo func() (ghrepo.Interface, error) |
| 24 | + Branch func() (string, error) |
| 25 | + Remotes func() (context.Remotes, error) |
| 26 | + |
| 27 | + SelectorArg string |
| 28 | +} |
| 29 | + |
| 30 | +func NewCmdChecks(f *cmdutil.Factory, runF func(*ChecksOptions) error) *cobra.Command { |
| 31 | + opts := &ChecksOptions{ |
| 32 | + IO: f.IOStreams, |
| 33 | + HttpClient: f.HttpClient, |
| 34 | + Branch: f.Branch, |
| 35 | + Remotes: f.Remotes, |
| 36 | + BaseRepo: f.BaseRepo, |
| 37 | + } |
| 38 | + |
| 39 | + cmd := &cobra.Command{ |
| 40 | + Use: "checks", |
| 41 | + Short: "Show CI status for a single pull request", |
| 42 | + Args: cobra.MaximumNArgs(1), |
| 43 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 44 | + // support `-R, --repo` override |
| 45 | + opts.BaseRepo = f.BaseRepo |
| 46 | + |
| 47 | + if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && len(args) == 0 { |
| 48 | + return &cmdutil.FlagError{Err: errors.New("argument required when using the --repo flag")} |
| 49 | + } |
| 50 | + |
| 51 | + if len(args) > 0 { |
| 52 | + opts.SelectorArg = args[0] |
| 53 | + } |
| 54 | + |
| 55 | + if runF != nil { |
| 56 | + return runF(opts) |
| 57 | + } |
| 58 | + |
| 59 | + return checksRun(opts) |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + return cmd |
| 64 | +} |
| 65 | + |
| 66 | +func checksRun(opts *ChecksOptions) error { |
| 67 | + httpClient, err := opts.HttpClient() |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + apiClient := api.NewClientFromHTTP(httpClient) |
| 72 | + |
| 73 | + pr, _, err := shared.PRFromArgs(apiClient, opts.BaseRepo, opts.Branch, opts.Remotes, opts.SelectorArg) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + if len(pr.Commits.Nodes) == 0 { |
| 79 | + return nil |
| 80 | + } |
| 81 | + |
| 82 | + rollup := pr.Commits.Nodes[0].Commit.StatusCheckRollup.Contexts.Nodes |
| 83 | + if len(rollup) == 0 { |
| 84 | + return nil |
| 85 | + } |
| 86 | + |
| 87 | + passing := 0 |
| 88 | + failing := 0 |
| 89 | + pending := 0 |
| 90 | + |
| 91 | + type output struct { |
| 92 | + mark string |
| 93 | + bucket string |
| 94 | + name string |
| 95 | + elapsed string |
| 96 | + link string |
| 97 | + } |
| 98 | + |
| 99 | + outputs := []output{} |
| 100 | + |
| 101 | + for _, c := range pr.Commits.Nodes[0].Commit.StatusCheckRollup.Contexts.Nodes { |
| 102 | + mark := "" |
| 103 | + bucket := "" |
| 104 | + state := c.State |
| 105 | + if state == "" { |
| 106 | + if c.Status == "COMPLETED" { |
| 107 | + state = c.Conclusion |
| 108 | + } else { |
| 109 | + state = c.Status |
| 110 | + } |
| 111 | + } |
| 112 | + switch state { |
| 113 | + case "SUCCESS", "NEUTRAL", "SKIPPED": |
| 114 | + mark = utils.GreenCheck() |
| 115 | + passing++ |
| 116 | + bucket = "pass" |
| 117 | + case "ERROR", "FAILURE", "CANCELLED", "TIMED_OUT", "ACTION_REQUIRED": |
| 118 | + mark = utils.RedX() |
| 119 | + failing++ |
| 120 | + bucket = "fail" |
| 121 | + case "EXPECTED", "REQUESTED", "QUEUED", "PENDING", "IN_PROGRESS", "STALE": |
| 122 | + mark = utils.YellowDash() |
| 123 | + pending++ |
| 124 | + bucket = "pending" |
| 125 | + default: |
| 126 | + panic(fmt.Errorf("unsupported status: %q", state)) |
| 127 | + } |
| 128 | + |
| 129 | + elapsed := "" |
| 130 | + zeroTime := time.Time{} |
| 131 | + |
| 132 | + if c.StartedAt != zeroTime && c.CompletedAt != zeroTime { |
| 133 | + e := c.CompletedAt.Sub(c.StartedAt) |
| 134 | + if e > 0 { |
| 135 | + elapsed = e.String() |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + link := c.DetailsURL |
| 140 | + if link == "" { |
| 141 | + link = c.TargetURL |
| 142 | + } |
| 143 | + |
| 144 | + name := c.Name |
| 145 | + if name == "" { |
| 146 | + name = c.Context |
| 147 | + } |
| 148 | + |
| 149 | + outputs = append(outputs, output{mark, bucket, name, elapsed, link}) |
| 150 | + } |
| 151 | + |
| 152 | + sort.Slice(outputs, func(i, j int) bool { |
| 153 | + if outputs[i].bucket == outputs[j].bucket { |
| 154 | + return outputs[i].name < outputs[j].name |
| 155 | + } else { |
| 156 | + if outputs[i].bucket == "fail" { |
| 157 | + return true |
| 158 | + } else if outputs[i].bucket == "pending" && outputs[j].bucket == "success" { |
| 159 | + return true |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return false |
| 164 | + }) |
| 165 | + |
| 166 | + tp := utils.NewTablePrinter(opts.IO) |
| 167 | + |
| 168 | + for _, o := range outputs { |
| 169 | + if opts.IO.IsStdoutTTY() { |
| 170 | + tp.AddField(o.mark, nil, nil) |
| 171 | + tp.AddField(o.name, nil, nil) |
| 172 | + tp.AddField(o.elapsed, nil, nil) |
| 173 | + tp.AddField(o.link, nil, nil) |
| 174 | + } else { |
| 175 | + tp.AddField(o.name, nil, nil) |
| 176 | + tp.AddField(o.bucket, nil, nil) |
| 177 | + if o.elapsed == "" { |
| 178 | + tp.AddField("0", nil, nil) |
| 179 | + } else { |
| 180 | + tp.AddField(o.elapsed, nil, nil) |
| 181 | + } |
| 182 | + tp.AddField(o.link, nil, nil) |
| 183 | + } |
| 184 | + |
| 185 | + tp.EndRow() |
| 186 | + } |
| 187 | + |
| 188 | + summary := "" |
| 189 | + if failing+passing+pending > 0 { |
| 190 | + if failing > 0 { |
| 191 | + summary = "Some checks were not successful" |
| 192 | + } else if pending > 0 { |
| 193 | + summary = "Some checks are still pending" |
| 194 | + } else { |
| 195 | + summary = "All checks were successful" |
| 196 | + } |
| 197 | + |
| 198 | + tallies := fmt.Sprintf( |
| 199 | + "%d failing, %d successful, and %d pending checks", |
| 200 | + failing, passing, pending) |
| 201 | + |
| 202 | + summary = fmt.Sprintf("%s\n%s", utils.Bold(summary), tallies) |
| 203 | + } |
| 204 | + |
| 205 | + if opts.IO.IsStdoutTTY() { |
| 206 | + fmt.Fprintln(opts.IO.Out, summary) |
| 207 | + fmt.Fprintln(opts.IO.Out) |
| 208 | + } |
| 209 | + |
| 210 | + return tp.Render() |
| 211 | +} |
0 commit comments