1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 )
12
13
14 type Workflow struct {
15 ID *int64 `json:"id,omitempty"`
16 NodeID *string `json:"node_id,omitempty"`
17 Name *string `json:"name,omitempty"`
18 Path *string `json:"path,omitempty"`
19 State *string `json:"state,omitempty"`
20 CreatedAt *Timestamp `json:"created_at,omitempty"`
21 UpdatedAt *Timestamp `json:"updated_at,omitempty"`
22 URL *string `json:"url,omitempty"`
23 HTMLURL *string `json:"html_url,omitempty"`
24 BadgeURL *string `json:"badge_url,omitempty"`
25 }
26
27
28 type Workflows struct {
29 TotalCount *int `json:"total_count,omitempty"`
30 Workflows []*Workflow `json:"workflows,omitempty"`
31 }
32
33
34 type WorkflowUsage struct {
35 Billable *WorkflowBillMap `json:"billable,omitempty"`
36 }
37
38
39
40 type WorkflowBillMap map[string]*WorkflowBill
41
42
43 type WorkflowBill struct {
44 TotalMS *int64 `json:"total_ms,omitempty"`
45 }
46
47
48 type CreateWorkflowDispatchEventRequest struct {
49
50
51
52 Ref string `json:"ref"`
53
54
55
56 Inputs map[string]interface{} `json:"inputs,omitempty"`
57 }
58
59
60
61
62 func (s *ActionsService) ListWorkflows(ctx context.Context, owner, repo string, opts *ListOptions) (*Workflows, *Response, error) {
63 u := fmt.Sprintf("repos/%s/%s/actions/workflows", owner, repo)
64 u, err := addOptions(u, opts)
65 if err != nil {
66 return nil, nil, err
67 }
68
69 req, err := s.client.NewRequest("GET", u, nil)
70 if err != nil {
71 return nil, nil, err
72 }
73
74 workflows := new(Workflows)
75 resp, err := s.client.Do(ctx, req, &workflows)
76 if err != nil {
77 return nil, resp, err
78 }
79
80 return workflows, resp, nil
81 }
82
83
84
85
86 func (s *ActionsService) GetWorkflowByID(ctx context.Context, owner, repo string, workflowID int64) (*Workflow, *Response, error) {
87 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v", owner, repo, workflowID)
88
89 return s.getWorkflow(ctx, u)
90 }
91
92
93
94
95 func (s *ActionsService) GetWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Workflow, *Response, error) {
96 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v", owner, repo, workflowFileName)
97
98 return s.getWorkflow(ctx, u)
99 }
100
101 func (s *ActionsService) getWorkflow(ctx context.Context, url string) (*Workflow, *Response, error) {
102 req, err := s.client.NewRequest("GET", url, nil)
103 if err != nil {
104 return nil, nil, err
105 }
106
107 workflow := new(Workflow)
108 resp, err := s.client.Do(ctx, req, workflow)
109 if err != nil {
110 return nil, resp, err
111 }
112
113 return workflow, resp, nil
114 }
115
116
117
118
119 func (s *ActionsService) GetWorkflowUsageByID(ctx context.Context, owner, repo string, workflowID int64) (*WorkflowUsage, *Response, error) {
120 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/timing", owner, repo, workflowID)
121
122 return s.getWorkflowUsage(ctx, u)
123 }
124
125
126
127
128 func (s *ActionsService) GetWorkflowUsageByFileName(ctx context.Context, owner, repo, workflowFileName string) (*WorkflowUsage, *Response, error) {
129 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/timing", owner, repo, workflowFileName)
130
131 return s.getWorkflowUsage(ctx, u)
132 }
133
134 func (s *ActionsService) getWorkflowUsage(ctx context.Context, url string) (*WorkflowUsage, *Response, error) {
135 req, err := s.client.NewRequest("GET", url, nil)
136 if err != nil {
137 return nil, nil, err
138 }
139
140 workflowUsage := new(WorkflowUsage)
141 resp, err := s.client.Do(ctx, req, workflowUsage)
142 if err != nil {
143 return nil, resp, err
144 }
145
146 return workflowUsage, resp, nil
147 }
148
149
150
151
152 func (s *ActionsService) CreateWorkflowDispatchEventByID(ctx context.Context, owner, repo string, workflowID int64, event CreateWorkflowDispatchEventRequest) (*Response, error) {
153 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, workflowID)
154
155 return s.createWorkflowDispatchEvent(ctx, u, &event)
156 }
157
158
159
160
161 func (s *ActionsService) CreateWorkflowDispatchEventByFileName(ctx context.Context, owner, repo, workflowFileName string, event CreateWorkflowDispatchEventRequest) (*Response, error) {
162 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, workflowFileName)
163
164 return s.createWorkflowDispatchEvent(ctx, u, &event)
165 }
166
167 func (s *ActionsService) createWorkflowDispatchEvent(ctx context.Context, url string, event *CreateWorkflowDispatchEventRequest) (*Response, error) {
168 req, err := s.client.NewRequest("POST", url, event)
169 if err != nil {
170 return nil, err
171 }
172
173 return s.client.Do(ctx, req, nil)
174 }
175
176
177
178
179 func (s *ActionsService) EnableWorkflowByID(ctx context.Context, owner, repo string, workflowID int64) (*Response, error) {
180 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/enable", owner, repo, workflowID)
181 return s.doNewPutRequest(ctx, u)
182 }
183
184
185
186
187 func (s *ActionsService) EnableWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Response, error) {
188 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/enable", owner, repo, workflowFileName)
189 return s.doNewPutRequest(ctx, u)
190 }
191
192
193
194
195 func (s *ActionsService) DisableWorkflowByID(ctx context.Context, owner, repo string, workflowID int64) (*Response, error) {
196 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/disable", owner, repo, workflowID)
197 return s.doNewPutRequest(ctx, u)
198 }
199
200
201
202
203 func (s *ActionsService) DisableWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Response, error) {
204 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/disable", owner, repo, workflowFileName)
205 return s.doNewPutRequest(ctx, u)
206 }
207
208 func (s *ActionsService) doNewPutRequest(ctx context.Context, url string) (*Response, error) {
209 req, err := s.client.NewRequest("PUT", url, nil)
210 if err != nil {
211 return nil, err
212 }
213
214 return s.client.Do(ctx, req, nil)
215 }
216
View as plain text