forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.go
More file actions
157 lines (126 loc) · 3.28 KB
/
shared.go
File metadata and controls
157 lines (126 loc) · 3.28 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
package shared
import (
"errors"
"fmt"
"path"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/prompt"
)
const (
Active WorkflowState = "active"
DisabledManually WorkflowState = "disabled_manually"
)
type WorkflowState string
type Workflow struct {
Name string
ID int
Path string
State WorkflowState
}
type WorkflowsPayload struct {
Workflows []Workflow
}
func (w *Workflow) Disabled() bool {
return w.State != Active
}
func (w *Workflow) Base() string {
return path.Base(w.Path)
}
func GetWorkflows(client *api.Client, repo ghrepo.Interface, limit int) ([]Workflow, error) {
perPage := limit
page := 1
if limit > 100 || limit == 0 {
perPage = 100
}
workflows := []Workflow{}
for {
if limit > 0 && len(workflows) == limit {
break
}
var result WorkflowsPayload
path := fmt.Sprintf("repos/%s/actions/workflows?per_page=%d&page=%d", ghrepo.FullName(repo), perPage, page)
err := client.REST(repo.RepoHost(), "GET", path, nil, &result)
if err != nil {
return nil, err
}
for _, workflow := range result.Workflows {
workflows = append(workflows, workflow)
if limit > 0 && len(workflows) == limit {
break
}
}
if len(result.Workflows) < perPage {
break
}
page++
}
return workflows, nil
}
func SelectWorkflow(workflows []Workflow, promptMsg string, states []WorkflowState) (*Workflow, error) {
filtered := []Workflow{}
candidates := []string{}
for _, workflow := range workflows {
for _, state := range states {
if workflow.State == state {
filtered = append(filtered, workflow)
candidates = append(candidates, fmt.Sprintf("%s (%s)", workflow.Name, workflow.Base()))
break
}
}
}
var selected int
err := prompt.SurveyAskOne(&survey.Select{
Message: promptMsg,
Options: candidates,
PageSize: 15,
}, &selected)
if err != nil {
return nil, err
}
return &filtered[selected], nil
}
func ResolveWorkflow(client *api.Client, repo ghrepo.Interface, workflowSelector string) ([]Workflow, error) {
if workflowSelector == "" {
return nil, errors.New("empty workflow selector")
}
workflow, err := getWorkflowByID(client, repo, workflowSelector)
if err == nil {
return []Workflow{*workflow}, nil
} else {
var httpErr api.HTTPError
if !errors.As(err, &httpErr) || httpErr.StatusCode != 404 {
return nil, err
}
}
return getWorkflowsByName(client, repo, workflowSelector)
}
func getWorkflowByID(client *api.Client, repo ghrepo.Interface, ID string) (*Workflow, error) {
var workflow Workflow
err := client.REST(repo.RepoHost(), "GET",
fmt.Sprintf("repos/%s/actions/workflows/%s", ghrepo.FullName(repo), ID),
nil, &workflow)
if err != nil {
return nil, err
}
return &workflow, nil
}
func getWorkflowsByName(client *api.Client, repo ghrepo.Interface, name string) ([]Workflow, error) {
workflows, err := GetWorkflows(client, repo, 0)
if err != nil {
return nil, fmt.Errorf("couldn't fetch workflows for %s: %w", ghrepo.FullName(repo), err)
}
filtered := []Workflow{}
for _, workflow := range workflows {
if workflow.Disabled() {
continue
}
// TODO consider fuzzy or prefix match
if strings.EqualFold(workflow.Name, name) {
filtered = append(filtered, workflow)
}
}
return filtered, nil
}