forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrerun.go
More file actions
187 lines (161 loc) · 4.78 KB
/
rerun.go
File metadata and controls
187 lines (161 loc) · 4.78 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package rerun
import (
"errors"
"fmt"
"net/http"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/run/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/spf13/cobra"
)
type RerunOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
RunID string
OnlyFailed bool
JobID string
Prompt bool
}
func NewCmdRerun(f *cmdutil.Factory, runF func(*RerunOptions) error) *cobra.Command {
opts := &RerunOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
}
cmd := &cobra.Command{
Use: "rerun [<run-id>]",
Short: "Rerun a failed run",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
if len(args) == 0 && opts.JobID == "" {
if !opts.IO.CanPrompt() {
return cmdutil.FlagErrorf("`<run-id>` or `--job` required when not running interactively")
} else {
opts.Prompt = true
}
} else if len(args) > 0 {
opts.RunID = args[0]
}
if opts.RunID != "" && opts.JobID != "" {
return cmdutil.FlagErrorf("specify only one of `<run-id>` or `--job`")
}
if runF != nil {
return runF(opts)
}
return runRerun(opts)
},
}
cmd.Flags().BoolVar(&opts.OnlyFailed, "failed", false, "Rerun only failed jobs, including dependencies")
cmd.Flags().StringVarP(&opts.JobID, "job", "j", "", "Rerun a specific job from a run, including dependencies")
return cmd
}
func runRerun(opts *RerunOptions) error {
c, err := opts.HttpClient()
if err != nil {
return fmt.Errorf("failed to create http client: %w", err)
}
client := api.NewClientFromHTTP(c)
repo, err := opts.BaseRepo()
if err != nil {
return fmt.Errorf("failed to determine base repo: %w", err)
}
cs := opts.IO.ColorScheme()
runID := opts.RunID
jobID := opts.JobID
var selectedJob *shared.Job
if jobID != "" {
opts.IO.StartProgressIndicator()
selectedJob, err = shared.GetJob(client, repo, jobID)
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to get job: %w", err)
}
runID = fmt.Sprintf("%d", selectedJob.RunID)
}
if opts.Prompt {
runs, err := shared.GetRunsWithFilter(client, repo, nil, 10, func(run shared.Run) bool {
if run.Status != shared.Completed {
return false
}
// TODO StartupFailure indiciates a bad yaml file; such runs can never be
// rerun. But hiding them from the prompt might confuse people?
return run.Conclusion != shared.Success && run.Conclusion != shared.StartupFailure
})
if err != nil {
return fmt.Errorf("failed to get runs: %w", err)
}
if len(runs) == 0 {
return errors.New("no recent runs have failed; please specify a specific `<run-id>`")
}
runID, err = shared.PromptForRun(cs, runs)
if err != nil {
return err
}
}
if opts.JobID != "" {
err = rerunJob(client, repo, selectedJob)
if err != nil {
return err
}
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "%s Requested rerun of job %s on run %s\n",
cs.SuccessIcon(),
cs.Cyanf("%d", selectedJob.ID),
cs.Cyanf("%d", selectedJob.RunID))
}
} else {
opts.IO.StartProgressIndicator()
run, err := shared.GetRun(client, repo, runID)
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to get run: %w", err)
}
err = rerunRun(client, repo, run, opts.OnlyFailed)
if err != nil {
return err
}
if opts.IO.IsStdoutTTY() {
onlyFailedMsg := ""
if opts.OnlyFailed {
onlyFailedMsg = "(failed jobs) "
}
fmt.Fprintf(opts.IO.Out, "%s Requested rerun %sof run %s\n",
cs.SuccessIcon(),
onlyFailedMsg,
cs.Cyanf("%d", run.ID))
}
}
return nil
}
func rerunRun(client *api.Client, repo ghrepo.Interface, run *shared.Run, onlyFailed bool) error {
runVerb := "rerun"
if onlyFailed {
runVerb = "rerun-failed-jobs"
}
path := fmt.Sprintf("repos/%s/actions/runs/%d/%s", ghrepo.FullName(repo), run.ID, runVerb)
err := client.REST(repo.RepoHost(), "POST", path, nil, nil)
if err != nil {
var httpError api.HTTPError
if errors.As(err, &httpError) && httpError.StatusCode == 403 {
return fmt.Errorf("run %d cannot be rerun; its workflow file may be broken", run.ID)
}
return fmt.Errorf("failed to rerun: %w", err)
}
return nil
}
func rerunJob(client *api.Client, repo ghrepo.Interface, job *shared.Job) error {
path := fmt.Sprintf("repos/%s/actions/jobs/%d/rerun", ghrepo.FullName(repo), job.ID)
err := client.REST(repo.RepoHost(), "POST", path, nil, nil)
if err != nil {
var httpError api.HTTPError
if errors.As(err, &httpError) && httpError.StatusCode == 403 {
return fmt.Errorf("job %d cannot be rerun", job.ID)
}
return fmt.Errorf("failed to rerun: %w", err)
}
return nil
}