1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "encoding/json"
11 "fmt"
12 "net/http"
13 "net/url"
14 "testing"
15 "time"
16
17 "github.com/google/go-cmp/cmp"
18 )
19
20 func TestActionsService_ListWorkflowRunsByID(t *testing.T) {
21 client, mux, _, teardown := setup()
22 defer teardown()
23
24 mux.HandleFunc("/repos/o/r/actions/workflows/29679449/runs", func(w http.ResponseWriter, r *http.Request) {
25 testMethod(t, r, "GET")
26 testFormValues(t, r, values{"per_page": "2", "page": "2"})
27 fmt.Fprint(w, `{"total_count":4,"workflow_runs":[{"id":399444496,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"id":399444497,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`)
28 })
29
30 opts := &ListWorkflowRunsOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}}
31 ctx := context.Background()
32 runs, _, err := client.Actions.ListWorkflowRunsByID(ctx, "o", "r", 29679449, opts)
33 if err != nil {
34 t.Errorf("Actions.ListWorkFlowRunsByID returned error: %v", err)
35 }
36
37 want := &WorkflowRuns{
38 TotalCount: Int(4),
39 WorkflowRuns: []*WorkflowRun{
40 {ID: Int64(399444496), RunNumber: Int(296), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
41 {ID: Int64(399444497), RunNumber: Int(296), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
42 },
43 }
44 if !cmp.Equal(runs, want) {
45 t.Errorf("Actions.ListWorkflowRunsByID returned %+v, want %+v", runs, want)
46 }
47
48 const methodName = "ListWorkflowRunsByID"
49 testBadOptions(t, methodName, func() (err error) {
50 _, _, err = client.Actions.ListWorkflowRunsByID(ctx, "\n", "\n", 29679449, opts)
51 return err
52 })
53
54 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
55 got, resp, err := client.Actions.ListWorkflowRunsByID(ctx, "o", "r", 29679449, opts)
56 if got != nil {
57 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
58 }
59 return resp, err
60 })
61 }
62
63 func TestActionsService_ListWorkflowRunsFileName(t *testing.T) {
64 client, mux, _, teardown := setup()
65 defer teardown()
66
67 mux.HandleFunc("/repos/o/r/actions/workflows/29679449/runs", func(w http.ResponseWriter, r *http.Request) {
68 testMethod(t, r, "GET")
69 testFormValues(t, r, values{"per_page": "2", "page": "2"})
70 fmt.Fprint(w, `{"total_count":4,"workflow_runs":[{"id":399444496,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"id":399444497,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`)
71 })
72
73 opts := &ListWorkflowRunsOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}}
74 ctx := context.Background()
75 runs, _, err := client.Actions.ListWorkflowRunsByFileName(ctx, "o", "r", "29679449", opts)
76 if err != nil {
77 t.Errorf("Actions.ListWorkFlowRunsByFileName returned error: %v", err)
78 }
79
80 want := &WorkflowRuns{
81 TotalCount: Int(4),
82 WorkflowRuns: []*WorkflowRun{
83 {ID: Int64(399444496), RunNumber: Int(296), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
84 {ID: Int64(399444497), RunNumber: Int(296), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
85 },
86 }
87 if !cmp.Equal(runs, want) {
88 t.Errorf("Actions.ListWorkflowRunsByFileName returned %+v, want %+v", runs, want)
89 }
90
91 const methodName = "ListWorkflowRunsByFileName"
92 testBadOptions(t, methodName, func() (err error) {
93 _, _, err = client.Actions.ListWorkflowRunsByFileName(ctx, "\n", "\n", "29679449", opts)
94 return err
95 })
96
97 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
98 got, resp, err := client.Actions.ListWorkflowRunsByFileName(ctx, "o", "r", "29679449", opts)
99 if got != nil {
100 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
101 }
102 return resp, err
103 })
104 }
105
106 func TestActionsService_GetWorkflowRunByID(t *testing.T) {
107 client, mux, _, teardown := setup()
108 defer teardown()
109
110 mux.HandleFunc("/repos/o/r/actions/runs/29679449", func(w http.ResponseWriter, r *http.Request) {
111 testMethod(t, r, "GET")
112 fmt.Fprint(w, `{"id":399444496,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}}`)
113 })
114
115 ctx := context.Background()
116 runs, _, err := client.Actions.GetWorkflowRunByID(ctx, "o", "r", 29679449)
117 if err != nil {
118 t.Errorf("Actions.GetWorkflowRunByID returned error: %v", err)
119 }
120
121 want := &WorkflowRun{
122 ID: Int64(399444496),
123 RunNumber: Int(296),
124 CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)},
125 UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
126 }
127
128 if !cmp.Equal(runs, want) {
129 t.Errorf("Actions.GetWorkflowRunByID returned %+v, want %+v", runs, want)
130 }
131
132 const methodName = "GetWorkflowRunByID"
133 testBadOptions(t, methodName, func() (err error) {
134 _, _, err = client.Actions.GetWorkflowRunByID(ctx, "\n", "\n", 29679449)
135 return err
136 })
137
138 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
139 got, resp, err := client.Actions.GetWorkflowRunByID(ctx, "o", "r", 29679449)
140 if got != nil {
141 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
142 }
143 return resp, err
144 })
145 }
146
147 func TestActionsService_GetWorkflowRunAttempt(t *testing.T) {
148 client, mux, _, teardown := setup()
149 defer teardown()
150
151 mux.HandleFunc("/repos/o/r/actions/runs/29679449/attempts/3", func(w http.ResponseWriter, r *http.Request) {
152 testMethod(t, r, "GET")
153 testFormValues(t, r, values{"exclude_pull_requests": "true"})
154 fmt.Fprint(w, `{"id":399444496,"run_number":296,"run_attempt":3,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}}`)
155 })
156
157 opts := &WorkflowRunAttemptOptions{ExcludePullRequests: Bool(true)}
158 ctx := context.Background()
159 runs, _, err := client.Actions.GetWorkflowRunAttempt(ctx, "o", "r", 29679449, 3, opts)
160 if err != nil {
161 t.Errorf("Actions.GetWorkflowRunAttempt returned error: %v", err)
162 }
163
164 want := &WorkflowRun{
165 ID: Int64(399444496),
166 RunNumber: Int(296),
167 RunAttempt: Int(3),
168 CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)},
169 UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
170 }
171
172 if !cmp.Equal(runs, want) {
173 t.Errorf("Actions.GetWorkflowRunAttempt returned %+v, want %+v", runs, want)
174 }
175
176 const methodName = "GetWorkflowRunAttempt"
177 testBadOptions(t, methodName, func() (err error) {
178 _, _, err = client.Actions.GetWorkflowRunAttempt(ctx, "\n", "\n", 29679449, 3, opts)
179 return err
180 })
181
182 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
183 got, resp, err := client.Actions.GetWorkflowRunAttempt(ctx, "o", "r", 29679449, 3, opts)
184 if got != nil {
185 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
186 }
187 return resp, err
188 })
189 }
190
191 func TestActionsService_RerunWorkflowRunByID(t *testing.T) {
192 client, mux, _, teardown := setup()
193 defer teardown()
194
195 mux.HandleFunc("/repos/o/r/actions/runs/3434/rerun", func(w http.ResponseWriter, r *http.Request) {
196 testMethod(t, r, "POST")
197 w.WriteHeader(http.StatusCreated)
198 })
199
200 ctx := context.Background()
201 resp, err := client.Actions.RerunWorkflowByID(ctx, "o", "r", 3434)
202 if err != nil {
203 t.Errorf("Actions.RerunWorkflowByID returned error: %v", err)
204 }
205 if resp.StatusCode != http.StatusCreated {
206 t.Errorf("Actions.RerunWorkflowRunByID returned status: %d, want %d", resp.StatusCode, http.StatusCreated)
207 }
208
209 const methodName = "RerunWorkflowByID"
210 testBadOptions(t, methodName, func() (err error) {
211 _, err = client.Actions.RerunWorkflowByID(ctx, "\n", "\n", 3434)
212 return err
213 })
214
215 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
216 return client.Actions.RerunWorkflowByID(ctx, "o", "r", 3434)
217 })
218 }
219
220 func TestActionsService_RerunFailedJobsByID(t *testing.T) {
221 client, mux, _, teardown := setup()
222 defer teardown()
223
224 mux.HandleFunc("/repos/o/r/actions/runs/3434/rerun-failed-jobs", func(w http.ResponseWriter, r *http.Request) {
225 testMethod(t, r, "POST")
226 w.WriteHeader(http.StatusCreated)
227 })
228
229 ctx := context.Background()
230 resp, err := client.Actions.RerunFailedJobsByID(ctx, "o", "r", 3434)
231 if err != nil {
232 t.Errorf("Actions.RerunFailedJobsByID returned error: %v", err)
233 }
234 if resp.StatusCode != http.StatusCreated {
235 t.Errorf("Actions.RerunFailedJobsByID returned status: %d, want %d", resp.StatusCode, http.StatusCreated)
236 }
237
238 const methodName = "RerunFailedJobsByID"
239 testBadOptions(t, methodName, func() (err error) {
240 _, err = client.Actions.RerunFailedJobsByID(ctx, "\n", "\n", 3434)
241 return err
242 })
243
244 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
245 return client.Actions.RerunFailedJobsByID(ctx, "o", "r", 3434)
246 })
247 }
248
249 func TestActionsService_RerunJobByID(t *testing.T) {
250 client, mux, _, teardown := setup()
251 defer teardown()
252
253 mux.HandleFunc("/repos/o/r/actions/jobs/3434/rerun", func(w http.ResponseWriter, r *http.Request) {
254 testMethod(t, r, "POST")
255 w.WriteHeader(http.StatusCreated)
256 })
257
258 ctx := context.Background()
259 resp, err := client.Actions.RerunJobByID(ctx, "o", "r", 3434)
260 if err != nil {
261 t.Errorf("Actions.RerunJobByID returned error: %v", err)
262 }
263 if resp.StatusCode != http.StatusCreated {
264 t.Errorf("Actions.RerunJobByID returned status: %d, want %d", resp.StatusCode, http.StatusCreated)
265 }
266
267 const methodName = "RerunJobByID"
268 testBadOptions(t, methodName, func() (err error) {
269 _, err = client.Actions.RerunJobByID(ctx, "\n", "\n", 3434)
270 return err
271 })
272
273 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
274 return client.Actions.RerunJobByID(ctx, "o", "r", 3434)
275 })
276 }
277
278 func TestActionsService_CancelWorkflowRunByID(t *testing.T) {
279 client, mux, _, teardown := setup()
280 defer teardown()
281
282 mux.HandleFunc("/repos/o/r/actions/runs/3434/cancel", func(w http.ResponseWriter, r *http.Request) {
283 testMethod(t, r, "POST")
284 w.WriteHeader(http.StatusAccepted)
285 })
286
287 ctx := context.Background()
288 resp, err := client.Actions.CancelWorkflowRunByID(ctx, "o", "r", 3434)
289 if _, ok := err.(*AcceptedError); !ok {
290 t.Errorf("Actions.CancelWorkflowRunByID returned error: %v (want AcceptedError)", err)
291 }
292 if resp.StatusCode != http.StatusAccepted {
293 t.Errorf("Actions.CancelWorkflowRunByID returned status: %d, want %d", resp.StatusCode, http.StatusAccepted)
294 }
295
296 const methodName = "CancelWorkflowRunByID"
297 testBadOptions(t, methodName, func() (err error) {
298 _, err = client.Actions.CancelWorkflowRunByID(ctx, "\n", "\n", 3434)
299 return err
300 })
301
302 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
303 return client.Actions.CancelWorkflowRunByID(ctx, "o", "r", 3434)
304 })
305 }
306
307 func TestActionsService_GetWorkflowRunLogs(t *testing.T) {
308 client, mux, _, teardown := setup()
309 defer teardown()
310
311 mux.HandleFunc("/repos/o/r/actions/runs/399444496/logs", func(w http.ResponseWriter, r *http.Request) {
312 testMethod(t, r, "GET")
313 http.Redirect(w, r, "http://github.com/a", http.StatusFound)
314 })
315
316 ctx := context.Background()
317 url, resp, err := client.Actions.GetWorkflowRunLogs(ctx, "o", "r", 399444496, true)
318 if err != nil {
319 t.Errorf("Actions.GetWorkflowRunLogs returned error: %v", err)
320 }
321 if resp.StatusCode != http.StatusFound {
322 t.Errorf("Actions.GetWorkflowRunLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound)
323 }
324 want := "http://github.com/a"
325 if url.String() != want {
326 t.Errorf("Actions.GetWorkflowRunLogs returned %+v, want %+v", url.String(), want)
327 }
328
329 const methodName = "GetWorkflowRunLogs"
330 testBadOptions(t, methodName, func() (err error) {
331 _, _, err = client.Actions.GetWorkflowRunLogs(ctx, "\n", "\n", 399444496, true)
332 return err
333 })
334 }
335
336 func TestActionsService_GetWorkflowRunLogs_StatusMovedPermanently_dontFollowRedirects(t *testing.T) {
337 client, mux, _, teardown := setup()
338 defer teardown()
339
340 mux.HandleFunc("/repos/o/r/actions/runs/399444496/logs", func(w http.ResponseWriter, r *http.Request) {
341 testMethod(t, r, "GET")
342 http.Redirect(w, r, "http://github.com/a", http.StatusMovedPermanently)
343 })
344
345 ctx := context.Background()
346 _, resp, _ := client.Actions.GetWorkflowRunLogs(ctx, "o", "r", 399444496, false)
347 if resp.StatusCode != http.StatusMovedPermanently {
348 t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusMovedPermanently)
349 }
350 }
351
352 func TestActionsService_GetWorkflowRunLogs_StatusMovedPermanently_followRedirects(t *testing.T) {
353 client, mux, serverURL, teardown := setup()
354 defer teardown()
355
356
357 mux.HandleFunc("/repos/o/r/actions/runs/399444496/logs", func(w http.ResponseWriter, r *http.Request) {
358 testMethod(t, r, "GET")
359 redirectURL, _ := url.Parse(serverURL + baseURLPath + "/redirect")
360 http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently)
361 })
362
363 mux.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
364 testMethod(t, r, "GET")
365 http.Redirect(w, r, "http://github.com/a", http.StatusFound)
366 })
367
368 ctx := context.Background()
369 url, resp, err := client.Actions.GetWorkflowRunLogs(ctx, "o", "r", 399444496, true)
370 if err != nil {
371 t.Errorf("Actions.GetWorkflowJobLogs returned error: %v", err)
372 }
373
374 if resp.StatusCode != http.StatusFound {
375 t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound)
376 }
377
378 want := "http://github.com/a"
379 if url.String() != want {
380 t.Errorf("Actions.GetWorkflowJobLogs returned %+v, want %+v", url.String(), want)
381 }
382
383 const methodName = "GetWorkflowRunLogs"
384 testBadOptions(t, methodName, func() (err error) {
385 _, _, err = client.Actions.GetWorkflowRunLogs(ctx, "\n", "\n", 399444496, true)
386 return err
387 })
388 }
389
390 func TestActionService_ListRepositoryWorkflowRuns(t *testing.T) {
391 client, mux, _, teardown := setup()
392 defer teardown()
393
394 mux.HandleFunc("/repos/o/r/actions/runs", func(w http.ResponseWriter, r *http.Request) {
395 testMethod(t, r, "GET")
396 testFormValues(t, r, values{"per_page": "2", "page": "2"})
397 fmt.Fprint(w, `{"total_count":2,
398 "workflow_runs":[
399 {"id":298499444,"run_number":301,"created_at":"2020-04-11T11:14:54Z","updated_at":"2020-04-11T11:14:54Z"},
400 {"id":298499445,"run_number":302,"created_at":"2020-04-11T11:14:54Z","updated_at":"2020-04-11T11:14:54Z"}]}`)
401 })
402
403 opts := &ListWorkflowRunsOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}}
404 ctx := context.Background()
405 runs, _, err := client.Actions.ListRepositoryWorkflowRuns(ctx, "o", "r", opts)
406
407 if err != nil {
408 t.Errorf("Actions.ListRepositoryWorkflowRuns returned error: %v", err)
409 }
410
411 expected := &WorkflowRuns{
412 TotalCount: Int(2),
413 WorkflowRuns: []*WorkflowRun{
414 {ID: Int64(298499444), RunNumber: Int(301), CreatedAt: &Timestamp{time.Date(2020, time.April, 11, 11, 14, 54, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.April, 11, 11, 14, 54, 0, time.UTC)}},
415 {ID: Int64(298499445), RunNumber: Int(302), CreatedAt: &Timestamp{time.Date(2020, time.April, 11, 11, 14, 54, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.April, 11, 11, 14, 54, 0, time.UTC)}},
416 },
417 }
418
419 if !cmp.Equal(runs, expected) {
420 t.Errorf("Actions.ListRepositoryWorkflowRuns returned %+v, want %+v", runs, expected)
421 }
422
423 const methodName = "ListRepositoryWorkflowRuns"
424 testBadOptions(t, methodName, func() (err error) {
425 _, _, err = client.Actions.ListRepositoryWorkflowRuns(ctx, "\n", "\n", opts)
426
427 return err
428 })
429
430 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
431 got, resp, err := client.Actions.ListRepositoryWorkflowRuns(ctx, "o", "r", opts)
432
433 if got != nil {
434 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
435 }
436 return resp, err
437 })
438 }
439
440 func TestActionService_DeleteWorkflowRun(t *testing.T) {
441 client, mux, _, teardown := setup()
442 defer teardown()
443
444 mux.HandleFunc("/repos/o/r/actions/runs/399444496", func(w http.ResponseWriter, r *http.Request) {
445 testMethod(t, r, "DELETE")
446
447 w.WriteHeader(http.StatusNoContent)
448 })
449
450 ctx := context.Background()
451 if _, err := client.Actions.DeleteWorkflowRun(ctx, "o", "r", 399444496); err != nil {
452 t.Errorf("DeleteWorkflowRun returned error: %v", err)
453 }
454
455 const methodName = "DeleteWorkflowRun"
456 testBadOptions(t, methodName, func() (err error) {
457 _, err = client.Actions.DeleteWorkflowRun(ctx, "\n", "\n", 399444496)
458 return err
459 })
460
461 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
462 return client.Actions.DeleteWorkflowRun(ctx, "o", "r", 399444496)
463 })
464 }
465
466 func TestActionService_DeleteWorkflowRunLogs(t *testing.T) {
467 client, mux, _, teardown := setup()
468 defer teardown()
469
470 mux.HandleFunc("/repos/o/r/actions/runs/399444496/logs", func(w http.ResponseWriter, r *http.Request) {
471 testMethod(t, r, "DELETE")
472
473 w.WriteHeader(http.StatusNoContent)
474 })
475
476 ctx := context.Background()
477 if _, err := client.Actions.DeleteWorkflowRunLogs(ctx, "o", "r", 399444496); err != nil {
478 t.Errorf("DeleteWorkflowRunLogs returned error: %v", err)
479 }
480
481 const methodName = "DeleteWorkflowRunLogs"
482 testBadOptions(t, methodName, func() (err error) {
483 _, err = client.Actions.DeleteWorkflowRunLogs(ctx, "\n", "\n", 399444496)
484 return err
485 })
486
487 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
488 return client.Actions.DeleteWorkflowRunLogs(ctx, "o", "r", 399444496)
489 })
490 }
491
492 func TestActionsService_GetWorkflowRunUsageByID(t *testing.T) {
493 client, mux, _, teardown := setup()
494 defer teardown()
495
496 mux.HandleFunc("/repos/o/r/actions/runs/29679449/timing", func(w http.ResponseWriter, r *http.Request) {
497 testMethod(t, r, "GET")
498 fmt.Fprint(w, `{"billable":{"UBUNTU":{"total_ms":180000,"jobs":1,"job_runs":[{"job_id":1,"duration_ms":60000}]},"MACOS":{"total_ms":240000,"jobs":2,"job_runs":[{"job_id":2,"duration_ms":30000},{"job_id":3,"duration_ms":10000}]},"WINDOWS":{"total_ms":300000,"jobs":2}},"run_duration_ms":500000}`)
499 })
500
501 ctx := context.Background()
502 workflowRunUsage, _, err := client.Actions.GetWorkflowRunUsageByID(ctx, "o", "r", 29679449)
503 if err != nil {
504 t.Errorf("Actions.GetWorkflowRunUsageByID returned error: %v", err)
505 }
506
507 want := &WorkflowRunUsage{
508 Billable: &WorkflowRunEnvironment{
509 Ubuntu: &WorkflowRunBill{
510 TotalMS: Int64(180000),
511 Jobs: Int(1),
512 JobRuns: []*WorkflowRunJobRun{
513 {
514 JobID: Int(1),
515 DurationMS: Int64(60000),
516 },
517 },
518 },
519 MacOS: &WorkflowRunBill{
520 TotalMS: Int64(240000),
521 Jobs: Int(2),
522 JobRuns: []*WorkflowRunJobRun{
523 {
524 JobID: Int(2),
525 DurationMS: Int64(30000),
526 },
527 {
528 JobID: Int(3),
529 DurationMS: Int64(10000),
530 },
531 },
532 },
533 Windows: &WorkflowRunBill{
534 TotalMS: Int64(300000),
535 Jobs: Int(2),
536 },
537 },
538 RunDurationMS: Int64(500000),
539 }
540
541 if !cmp.Equal(workflowRunUsage, want) {
542 t.Errorf("Actions.GetWorkflowRunUsageByID returned %+v, want %+v", workflowRunUsage, want)
543 }
544
545 const methodName = "GetWorkflowRunUsageByID"
546 testBadOptions(t, methodName, func() (err error) {
547 _, _, err = client.Actions.GetWorkflowRunUsageByID(ctx, "\n", "\n", 29679449)
548 return err
549 })
550
551 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
552 got, resp, err := client.Actions.GetWorkflowRunUsageByID(ctx, "o", "r", 29679449)
553 if got != nil {
554 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
555 }
556 return resp, err
557 })
558 }
559
560 func TestWorkflowRun_Marshal(t *testing.T) {
561 testJSONMarshal(t, &WorkflowRun{}, "{}")
562
563 u := &WorkflowRun{
564 ID: Int64(1),
565 Name: String("n"),
566 NodeID: String("nid"),
567 HeadBranch: String("hb"),
568 HeadSHA: String("hs"),
569 RunNumber: Int(1),
570 RunAttempt: Int(1),
571 Event: String("e"),
572 Status: String("s"),
573 Conclusion: String("c"),
574 WorkflowID: Int64(1),
575 URL: String("u"),
576 HTMLURL: String("h"),
577 PullRequests: []*PullRequest{
578 {
579 URL: String("u"),
580 ID: Int64(1),
581 Number: Int(1),
582 Head: &PullRequestBranch{
583 Ref: String("r"),
584 SHA: String("s"),
585 Repo: &Repository{
586 ID: Int64(1),
587 URL: String("s"),
588 Name: String("n"),
589 },
590 },
591 Base: &PullRequestBranch{
592 Ref: String("r"),
593 SHA: String("s"),
594 Repo: &Repository{
595 ID: Int64(1),
596 URL: String("u"),
597 Name: String("n"),
598 },
599 },
600 },
601 },
602 CreatedAt: &Timestamp{referenceTime},
603 UpdatedAt: &Timestamp{referenceTime},
604 RunStartedAt: &Timestamp{referenceTime},
605 JobsURL: String("j"),
606 LogsURL: String("l"),
607 CheckSuiteURL: String("c"),
608 ArtifactsURL: String("a"),
609 CancelURL: String("c"),
610 RerunURL: String("r"),
611 PreviousAttemptURL: String("p"),
612 HeadCommit: &HeadCommit{
613 Message: String("m"),
614 Author: &CommitAuthor{
615 Name: String("n"),
616 Email: String("e"),
617 Login: String("l"),
618 },
619 URL: String("u"),
620 Distinct: Bool(false),
621 SHA: String("s"),
622 ID: String("i"),
623 TreeID: String("tid"),
624 Timestamp: &Timestamp{referenceTime},
625 Committer: &CommitAuthor{
626 Name: String("n"),
627 Email: String("e"),
628 Login: String("l"),
629 },
630 },
631 WorkflowURL: String("w"),
632 Repository: &Repository{
633 ID: Int64(1),
634 URL: String("u"),
635 Name: String("n"),
636 },
637 HeadRepository: &Repository{
638 ID: Int64(1),
639 URL: String("u"),
640 Name: String("n"),
641 },
642 Actor: &User{
643 Login: String("l"),
644 ID: Int64(1),
645 AvatarURL: String("a"),
646 GravatarID: String("g"),
647 Name: String("n"),
648 Company: String("c"),
649 Blog: String("b"),
650 Location: String("l"),
651 Email: String("e"),
652 Hireable: Bool(true),
653 Bio: String("b"),
654 TwitterUsername: String("t"),
655 PublicRepos: Int(1),
656 Followers: Int(1),
657 Following: Int(1),
658 CreatedAt: &Timestamp{referenceTime},
659 SuspendedAt: &Timestamp{referenceTime},
660 URL: String("u"),
661 },
662 }
663
664 want := `{
665 "id": 1,
666 "name": "n",
667 "node_id": "nid",
668 "head_branch": "hb",
669 "head_sha": "hs",
670 "run_number": 1,
671 "run_attempt": 1,
672 "event": "e",
673 "status": "s",
674 "conclusion": "c",
675 "workflow_id": 1,
676 "url": "u",
677 "html_url": "h",
678 "pull_requests": [
679 {
680 "id":1,
681 "number":1,
682 "url":"u",
683 "head":{
684 "ref":"r",
685 "sha":"s",
686 "repo": {
687 "id":1,
688 "name":"n",
689 "url":"s"
690 }
691 },
692 "base": {
693 "ref":"r",
694 "sha":"s",
695 "repo": {
696 "id":1,
697 "name":"n",
698 "url":"u"
699 }
700 }
701 }
702 ],
703 "created_at": ` + referenceTimeStr + `,
704 "updated_at": ` + referenceTimeStr + `,
705 "run_started_at": ` + referenceTimeStr + `,
706 "jobs_url": "j",
707 "logs_url": "l",
708 "check_suite_url": "c",
709 "artifacts_url": "a",
710 "cancel_url": "c",
711 "rerun_url": "r",
712 "previous_attempt_url": "p",
713 "head_commit": {
714 "message": "m",
715 "author": {
716 "name": "n",
717 "email": "e",
718 "username": "l"
719 },
720 "url": "u",
721 "distinct": false,
722 "sha": "s",
723 "id": "i",
724 "tree_id": "tid",
725 "timestamp": ` + referenceTimeStr + `,
726 "committer": {
727 "name": "n",
728 "email": "e",
729 "username": "l"
730 }
731 },
732 "workflow_url": "w",
733 "repository": {
734 "id": 1,
735 "url": "u",
736 "name": "n"
737 },
738 "head_repository": {
739 "id": 1,
740 "url": "u",
741 "name": "n"
742 },
743 "actor": {
744 "login": "l",
745 "id": 1,
746 "avatar_url": "a",
747 "gravatar_id": "g",
748 "name": "n",
749 "company": "c",
750 "blog": "b",
751 "location": "l",
752 "email": "e",
753 "hireable": true,
754 "bio": "b",
755 "twitter_username": "t",
756 "public_repos": 1,
757 "followers": 1,
758 "following": 1,
759 "created_at": ` + referenceTimeStr + `,
760 "suspended_at": ` + referenceTimeStr + `,
761 "url": "u"
762 }
763 }`
764
765 testJSONMarshal(t, u, want)
766 }
767
768 func TestWorkflowRuns_Marshal(t *testing.T) {
769 testJSONMarshal(t, &WorkflowRuns{}, "{}")
770
771 u := &WorkflowRuns{
772 TotalCount: Int(1),
773 WorkflowRuns: []*WorkflowRun{
774 {
775 ID: Int64(1),
776 Name: String("n"),
777 NodeID: String("nid"),
778 HeadBranch: String("hb"),
779 HeadSHA: String("hs"),
780 RunNumber: Int(1),
781 RunAttempt: Int(1),
782 Event: String("e"),
783 Status: String("s"),
784 Conclusion: String("c"),
785 WorkflowID: Int64(1),
786 URL: String("u"),
787 HTMLURL: String("h"),
788 PullRequests: []*PullRequest{
789 {
790 URL: String("u"),
791 ID: Int64(1),
792 Number: Int(1),
793 Head: &PullRequestBranch{
794 Ref: String("r"),
795 SHA: String("s"),
796 Repo: &Repository{
797 ID: Int64(1),
798 URL: String("s"),
799 Name: String("n"),
800 },
801 },
802 Base: &PullRequestBranch{
803 Ref: String("r"),
804 SHA: String("s"),
805 Repo: &Repository{
806 ID: Int64(1),
807 URL: String("u"),
808 Name: String("n"),
809 },
810 },
811 },
812 },
813 CreatedAt: &Timestamp{referenceTime},
814 UpdatedAt: &Timestamp{referenceTime},
815 RunStartedAt: &Timestamp{referenceTime},
816 JobsURL: String("j"),
817 LogsURL: String("l"),
818 CheckSuiteURL: String("c"),
819 ArtifactsURL: String("a"),
820 CancelURL: String("c"),
821 RerunURL: String("r"),
822 PreviousAttemptURL: String("p"),
823 HeadCommit: &HeadCommit{
824 Message: String("m"),
825 Author: &CommitAuthor{
826 Name: String("n"),
827 Email: String("e"),
828 Login: String("l"),
829 },
830 URL: String("u"),
831 Distinct: Bool(false),
832 SHA: String("s"),
833 ID: String("i"),
834 TreeID: String("tid"),
835 Timestamp: &Timestamp{referenceTime},
836 Committer: &CommitAuthor{
837 Name: String("n"),
838 Email: String("e"),
839 Login: String("l"),
840 },
841 },
842 WorkflowURL: String("w"),
843 Repository: &Repository{
844 ID: Int64(1),
845 URL: String("u"),
846 Name: String("n"),
847 },
848 HeadRepository: &Repository{
849 ID: Int64(1),
850 URL: String("u"),
851 Name: String("n"),
852 },
853 Actor: &User{
854 Login: String("l"),
855 ID: Int64(1),
856 AvatarURL: String("a"),
857 GravatarID: String("g"),
858 Name: String("n"),
859 Company: String("c"),
860 Blog: String("b"),
861 Location: String("l"),
862 Email: String("e"),
863 Hireable: Bool(true),
864 Bio: String("b"),
865 TwitterUsername: String("t"),
866 PublicRepos: Int(1),
867 Followers: Int(1),
868 Following: Int(1),
869 CreatedAt: &Timestamp{referenceTime},
870 SuspendedAt: &Timestamp{referenceTime},
871 URL: String("u"),
872 },
873 },
874 },
875 }
876
877 want := `{
878 "total_count": 1,
879 "workflow_runs": [
880 {
881 "id": 1,
882 "name": "n",
883 "node_id": "nid",
884 "head_branch": "hb",
885 "head_sha": "hs",
886 "run_number": 1,
887 "run_attempt": 1,
888 "event": "e",
889 "status": "s",
890 "conclusion": "c",
891 "workflow_id": 1,
892 "url": "u",
893 "html_url": "h",
894 "pull_requests": [
895 {
896 "id":1,
897 "number":1,
898 "url":"u",
899 "head":{
900 "ref":"r",
901 "sha":"s",
902 "repo": {
903 "id":1,
904 "name":"n",
905 "url":"s"
906 }
907 },
908 "base": {
909 "ref":"r",
910 "sha":"s",
911 "repo": {
912 "id":1,
913 "name":"n",
914 "url":"u"
915 }
916 }
917 }
918 ],
919 "created_at": ` + referenceTimeStr + `,
920 "updated_at": ` + referenceTimeStr + `,
921 "run_started_at": ` + referenceTimeStr + `,
922 "jobs_url": "j",
923 "logs_url": "l",
924 "check_suite_url": "c",
925 "artifacts_url": "a",
926 "cancel_url": "c",
927 "rerun_url": "r",
928 "previous_attempt_url": "p",
929 "head_commit": {
930 "message": "m",
931 "author": {
932 "name": "n",
933 "email": "e",
934 "username": "l"
935 },
936 "url": "u",
937 "distinct": false,
938 "sha": "s",
939 "id": "i",
940 "tree_id": "tid",
941 "timestamp": ` + referenceTimeStr + `,
942 "committer": {
943 "name": "n",
944 "email": "e",
945 "username": "l"
946 }
947 },
948 "workflow_url": "w",
949 "repository": {
950 "id": 1,
951 "url": "u",
952 "name": "n"
953 },
954 "head_repository": {
955 "id": 1,
956 "url": "u",
957 "name": "n"
958 },
959 "actor": {
960 "login": "l",
961 "id": 1,
962 "avatar_url": "a",
963 "gravatar_id": "g",
964 "name": "n",
965 "company": "c",
966 "blog": "b",
967 "location": "l",
968 "email": "e",
969 "hireable": true,
970 "bio": "b",
971 "twitter_username": "t",
972 "public_repos": 1,
973 "followers": 1,
974 "following": 1,
975 "created_at": ` + referenceTimeStr + `,
976 "suspended_at": ` + referenceTimeStr + `,
977 "url": "u"
978 }
979 }
980 ]
981 }`
982
983 testJSONMarshal(t, u, want)
984 }
985
986 func TestWorkflowRunBill_Marshal(t *testing.T) {
987 testJSONMarshal(t, &WorkflowRunBill{}, "{}")
988
989 u := &WorkflowRunBill{
990 TotalMS: Int64(1),
991 Jobs: Int(1),
992 }
993
994 want := `{
995 "total_ms": 1,
996 "jobs": 1
997 }`
998
999 testJSONMarshal(t, u, want)
1000 }
1001
1002 func TestWorkflowRunEnvironment_Marshal(t *testing.T) {
1003 testJSONMarshal(t, &WorkflowRunEnvironment{}, "{}")
1004
1005 u := &WorkflowRunEnvironment{
1006 Ubuntu: &WorkflowRunBill{
1007 TotalMS: Int64(1),
1008 Jobs: Int(1),
1009 },
1010 MacOS: &WorkflowRunBill{
1011 TotalMS: Int64(1),
1012 Jobs: Int(1),
1013 },
1014 Windows: &WorkflowRunBill{
1015 TotalMS: Int64(1),
1016 Jobs: Int(1),
1017 },
1018 }
1019
1020 want := `{
1021 "UBUNTU": {
1022 "total_ms": 1,
1023 "jobs": 1
1024 },
1025 "MACOS": {
1026 "total_ms": 1,
1027 "jobs": 1
1028 },
1029 "WINDOWS": {
1030 "total_ms": 1,
1031 "jobs": 1
1032 }
1033 }`
1034
1035 testJSONMarshal(t, u, want)
1036 }
1037
1038 func TestWorkflowRunUsage_Marshal(t *testing.T) {
1039 testJSONMarshal(t, &WorkflowRunUsage{}, "{}")
1040
1041 u := &WorkflowRunUsage{
1042 Billable: &WorkflowRunEnvironment{
1043 Ubuntu: &WorkflowRunBill{
1044 TotalMS: Int64(1),
1045 Jobs: Int(1),
1046 },
1047 MacOS: &WorkflowRunBill{
1048 TotalMS: Int64(1),
1049 Jobs: Int(1),
1050 },
1051 Windows: &WorkflowRunBill{
1052 TotalMS: Int64(1),
1053 Jobs: Int(1),
1054 },
1055 },
1056 RunDurationMS: Int64(1),
1057 }
1058
1059 want := `{
1060 "billable": {
1061 "UBUNTU": {
1062 "total_ms": 1,
1063 "jobs": 1
1064 },
1065 "MACOS": {
1066 "total_ms": 1,
1067 "jobs": 1
1068 },
1069 "WINDOWS": {
1070 "total_ms": 1,
1071 "jobs": 1
1072 }
1073 },
1074 "run_duration_ms": 1
1075 }`
1076
1077 testJSONMarshal(t, u, want)
1078 }
1079
1080 func TestActionService_PendingDeployments(t *testing.T) {
1081 client, mux, _, teardown := setup()
1082 defer teardown()
1083
1084 input := &PendingDeploymentsRequest{EnvironmentIDs: []int64{3, 4}, State: "approved", Comment: ""}
1085
1086 mux.HandleFunc("/repos/o/r/actions/runs/399444496/pending_deployments", func(w http.ResponseWriter, r *http.Request) {
1087 v := new(PendingDeploymentsRequest)
1088 json.NewDecoder(r.Body).Decode(v)
1089
1090 testMethod(t, r, "POST")
1091 if !cmp.Equal(v, input) {
1092 t.Errorf("Request body = %+v, want %+v", v, input)
1093 }
1094
1095 fmt.Fprint(w, `[{"id":1}, {"id":2}]`)
1096 })
1097
1098 ctx := context.Background()
1099 deployments, _, err := client.Actions.PendingDeployments(ctx, "o", "r", 399444496, input)
1100 if err != nil {
1101 t.Errorf("Actions.PendingDeployments returned error: %v", err)
1102 }
1103
1104 want := []*Deployment{{ID: Int64(1)}, {ID: Int64(2)}}
1105 if !cmp.Equal(deployments, want) {
1106 t.Errorf("Actions.PendingDeployments returned %+v, want %+v", deployments, want)
1107 }
1108
1109 const methodName = "PendingDeployments"
1110 testBadOptions(t, methodName, func() (err error) {
1111 _, _, err = client.Actions.PendingDeployments(ctx, "\n", "\n", 399444496, input)
1112 return err
1113 })
1114
1115 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1116 got, resp, err := client.Actions.PendingDeployments(ctx, "o", "r", 399444496, input)
1117 if got != nil {
1118 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1119 }
1120 return resp, err
1121 })
1122 }
1123
View as plain text