github.com/google/go-github/v66@v66.0.0/github/actions_workflow_jobs_test.go (about) 1 // Copyright 2020 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "errors" 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_ListWorkflowJobs(t *testing.T) { 21 t.Parallel() 22 client, mux, _ := setup(t) 23 24 mux.HandleFunc("/repos/o/r/actions/runs/29679449/jobs", 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,"jobs":[{"id":399444496,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"},{"id":399444497,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"}]}`) 28 }) 29 30 opts := &ListWorkflowJobsOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}} 31 ctx := context.Background() 32 jobs, _, err := client.Actions.ListWorkflowJobs(ctx, "o", "r", 29679449, opts) 33 if err != nil { 34 t.Errorf("Actions.ListWorkflowJobs returned error: %v", err) 35 } 36 37 want := &Jobs{ 38 TotalCount: Int(4), 39 Jobs: []*WorkflowJob{ 40 {ID: Int64(399444496), RunID: Int64(29679449), StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 41 {ID: Int64(399444497), RunID: Int64(29679449), StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 42 }, 43 } 44 if !cmp.Equal(jobs, want) { 45 t.Errorf("Actions.ListWorkflowJobs returned %+v, want %+v", jobs, want) 46 } 47 48 const methodName = "ListWorkflowJobs" 49 testBadOptions(t, methodName, func() (err error) { 50 _, _, err = client.Actions.ListWorkflowJobs(ctx, "\n", "\n", 29679449, opts) 51 return err 52 }) 53 54 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 55 got, resp, err := client.Actions.ListWorkflowJobs(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_ListWorkflowJobs_Filter(t *testing.T) { 64 t.Parallel() 65 client, mux, _ := setup(t) 66 67 mux.HandleFunc("/repos/o/r/actions/runs/29679449/jobs", func(w http.ResponseWriter, r *http.Request) { 68 testMethod(t, r, "GET") 69 testFormValues(t, r, values{"filter": "all", "per_page": "2", "page": "2"}) 70 fmt.Fprint(w, `{"total_count":4,"jobs":[{"id":399444496,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"},{"id":399444497,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"}]}`) 71 }) 72 73 opts := &ListWorkflowJobsOptions{Filter: "all", ListOptions: ListOptions{Page: 2, PerPage: 2}} 74 ctx := context.Background() 75 jobs, _, err := client.Actions.ListWorkflowJobs(ctx, "o", "r", 29679449, opts) 76 if err != nil { 77 t.Errorf("Actions.ListWorkflowJobs returned error: %v", err) 78 } 79 80 want := &Jobs{ 81 TotalCount: Int(4), 82 Jobs: []*WorkflowJob{ 83 {ID: Int64(399444496), RunID: Int64(29679449), StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 84 {ID: Int64(399444497), RunID: Int64(29679449), StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 85 }, 86 } 87 if !cmp.Equal(jobs, want) { 88 t.Errorf("Actions.ListWorkflowJobs returned %+v, want %+v", jobs, want) 89 } 90 } 91 92 func TestActionsService_ListWorkflowJobsAttempt(t *testing.T) { 93 t.Parallel() 94 client, mux, _ := setup(t) 95 96 mux.HandleFunc("/repos/o/r/actions/runs/29679449/attempts/1/jobs", func(w http.ResponseWriter, r *http.Request) { 97 testMethod(t, r, "GET") 98 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 99 fmt.Fprint(w, `{"total_count":4,"jobs":[{"id":399444496,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z","run_attempt":2},{"id":399444497,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z","run_attempt":2}]}`) 100 }) 101 opts := &ListOptions{Page: 2, PerPage: 2} 102 ctx := context.Background() 103 jobs, _, err := client.Actions.ListWorkflowJobsAttempt(ctx, "o", "r", 29679449, 1, opts) 104 if err != nil { 105 t.Errorf("Actions.ListWorkflowJobsAttempt returned error: %v", err) 106 } 107 108 want := &Jobs{ 109 TotalCount: Int(4), 110 Jobs: []*WorkflowJob{ 111 { 112 ID: Int64(399444496), 113 RunID: Int64(29679449), 114 StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, 115 CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, 116 RunAttempt: Int64(2), 117 }, 118 { 119 ID: Int64(399444497), 120 RunID: Int64(29679449), 121 StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, 122 CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, 123 RunAttempt: Int64(2), 124 }, 125 }, 126 } 127 if !cmp.Equal(jobs, want) { 128 t.Errorf("Actions.ListWorkflowJobsAttempt returned %+v, want %+v", jobs, want) 129 } 130 131 const methodName = "ListWorkflowJobsAttempt" 132 testBadOptions(t, methodName, func() (err error) { 133 _, _, err = client.Actions.ListWorkflowJobsAttempt(ctx, "\n", "\n", 29679449, 1, opts) 134 return err 135 }) 136 137 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 138 got, resp, err := client.Actions.ListWorkflowJobsAttempt(ctx, "o", "r", 29679449, 1, opts) 139 if got != nil { 140 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 141 } 142 return resp, err 143 }) 144 } 145 146 func TestActionsService_GetWorkflowJobByID(t *testing.T) { 147 t.Parallel() 148 client, mux, _ := setup(t) 149 150 mux.HandleFunc("/repos/o/r/actions/jobs/399444496", func(w http.ResponseWriter, r *http.Request) { 151 testMethod(t, r, "GET") 152 fmt.Fprint(w, `{"id":399444496,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"}`) 153 }) 154 155 ctx := context.Background() 156 job, _, err := client.Actions.GetWorkflowJobByID(ctx, "o", "r", 399444496) 157 if err != nil { 158 t.Errorf("Actions.GetWorkflowJobByID returned error: %v", err) 159 } 160 161 want := &WorkflowJob{ 162 ID: Int64(399444496), 163 StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, 164 CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, 165 } 166 if !cmp.Equal(job, want) { 167 t.Errorf("Actions.GetWorkflowJobByID returned %+v, want %+v", job, want) 168 } 169 170 const methodName = "GetWorkflowJobByID" 171 testBadOptions(t, methodName, func() (err error) { 172 _, _, err = client.Actions.GetWorkflowJobByID(ctx, "\n", "\n", 399444496) 173 return err 174 }) 175 176 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 177 got, resp, err := client.Actions.GetWorkflowJobByID(ctx, "o", "r", 399444496) 178 if got != nil { 179 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 180 } 181 return resp, err 182 }) 183 } 184 185 func TestActionsService_GetWorkflowJobLogs(t *testing.T) { 186 t.Parallel() 187 client, mux, _ := setup(t) 188 189 mux.HandleFunc("/repos/o/r/actions/jobs/399444496/logs", func(w http.ResponseWriter, r *http.Request) { 190 testMethod(t, r, "GET") 191 http.Redirect(w, r, "http://github.com/a", http.StatusFound) 192 }) 193 194 ctx := context.Background() 195 url, resp, err := client.Actions.GetWorkflowJobLogs(ctx, "o", "r", 399444496, 1) 196 if err != nil { 197 t.Errorf("Actions.GetWorkflowJobLogs returned error: %v", err) 198 } 199 if resp.StatusCode != http.StatusFound { 200 t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound) 201 } 202 want := "http://github.com/a" 203 if url.String() != want { 204 t.Errorf("Actions.GetWorkflowJobLogs returned %+v, want %+v", url.String(), want) 205 } 206 207 const methodName = "GetWorkflowJobLogs" 208 testBadOptions(t, methodName, func() (err error) { 209 _, _, err = client.Actions.GetWorkflowJobLogs(ctx, "\n", "\n", 399444496, 1) 210 return err 211 }) 212 213 // Add custom round tripper 214 client.client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) { 215 return nil, errors.New("failed to get workflow logs") 216 }) 217 testBadOptions(t, methodName, func() (err error) { 218 _, _, err = client.Actions.GetWorkflowJobLogs(ctx, "o", "r", 399444496, 1) 219 return err 220 }) 221 } 222 223 func TestActionsService_GetWorkflowJobLogs_StatusMovedPermanently_dontFollowRedirects(t *testing.T) { 224 t.Parallel() 225 client, mux, _ := setup(t) 226 227 mux.HandleFunc("/repos/o/r/actions/jobs/399444496/logs", func(w http.ResponseWriter, r *http.Request) { 228 testMethod(t, r, "GET") 229 http.Redirect(w, r, "http://github.com/a", http.StatusMovedPermanently) 230 }) 231 232 ctx := context.Background() 233 _, resp, _ := client.Actions.GetWorkflowJobLogs(ctx, "o", "r", 399444496, 0) 234 if resp.StatusCode != http.StatusMovedPermanently { 235 t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusMovedPermanently) 236 } 237 } 238 239 func TestActionsService_GetWorkflowJobLogs_StatusMovedPermanently_followRedirects(t *testing.T) { 240 t.Parallel() 241 client, mux, serverURL := setup(t) 242 243 // Mock a redirect link, which leads to an archive link 244 mux.HandleFunc("/repos/o/r/actions/jobs/399444496/logs", func(w http.ResponseWriter, r *http.Request) { 245 testMethod(t, r, "GET") 246 redirectURL, _ := url.Parse(serverURL + baseURLPath + "/redirect") 247 http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently) 248 }) 249 250 mux.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) { 251 testMethod(t, r, "GET") 252 http.Redirect(w, r, "http://github.com/a", http.StatusFound) 253 }) 254 255 ctx := context.Background() 256 url, resp, err := client.Actions.GetWorkflowJobLogs(ctx, "o", "r", 399444496, 1) 257 if err != nil { 258 t.Errorf("Actions.GetWorkflowJobLogs returned error: %v", err) 259 } 260 261 if resp.StatusCode != http.StatusFound { 262 t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound) 263 } 264 265 want := "http://github.com/a" 266 if url.String() != want { 267 t.Errorf("Actions.GetWorkflowJobLogs returned %+v, want %+v", url.String(), want) 268 } 269 } 270 271 func TestTaskStep_Marshal(t *testing.T) { 272 t.Parallel() 273 testJSONMarshal(t, &TaskStep{}, "{}") 274 275 u := &TaskStep{ 276 Name: String("n"), 277 Status: String("s"), 278 Conclusion: String("c"), 279 Number: Int64(1), 280 StartedAt: &Timestamp{referenceTime}, 281 CompletedAt: &Timestamp{referenceTime}, 282 } 283 284 want := `{ 285 "name": "n", 286 "status": "s", 287 "conclusion": "c", 288 "number": 1, 289 "started_at": ` + referenceTimeStr + `, 290 "completed_at": ` + referenceTimeStr + ` 291 }` 292 293 testJSONMarshal(t, u, want) 294 } 295 296 func TestWorkflowJob_Marshal(t *testing.T) { 297 t.Parallel() 298 testJSONMarshal(t, &WorkflowJob{}, "{}") 299 300 u := &WorkflowJob{ 301 ID: Int64(1), 302 RunID: Int64(1), 303 RunURL: String("r"), 304 NodeID: String("n"), 305 HeadBranch: String("b"), 306 HeadSHA: String("h"), 307 URL: String("u"), 308 HTMLURL: String("h"), 309 Status: String("s"), 310 Conclusion: String("c"), 311 CreatedAt: &Timestamp{referenceTime}, 312 StartedAt: &Timestamp{referenceTime}, 313 CompletedAt: &Timestamp{referenceTime}, 314 Name: String("n"), 315 Steps: []*TaskStep{ 316 { 317 Name: String("n"), 318 Status: String("s"), 319 Conclusion: String("c"), 320 Number: Int64(1), 321 StartedAt: &Timestamp{referenceTime}, 322 CompletedAt: &Timestamp{referenceTime}, 323 }, 324 }, 325 CheckRunURL: String("c"), 326 WorkflowName: String("w"), 327 } 328 329 want := `{ 330 "id": 1, 331 "run_id": 1, 332 "run_url": "r", 333 "node_id": "n", 334 "head_branch": "b", 335 "head_sha": "h", 336 "url": "u", 337 "html_url": "h", 338 "status": "s", 339 "conclusion": "c", 340 "created_at": ` + referenceTimeStr + `, 341 "started_at": ` + referenceTimeStr + `, 342 "completed_at": ` + referenceTimeStr + `, 343 "name": "n", 344 "steps": [{ 345 "name": "n", 346 "status": "s", 347 "conclusion": "c", 348 "number": 1, 349 "started_at": ` + referenceTimeStr + `, 350 "completed_at": ` + referenceTimeStr + ` 351 }], 352 "check_run_url": "c", 353 "workflow_name": "w" 354 }` 355 356 testJSONMarshal(t, u, want) 357 } 358 359 func TestJobs_Marshal(t *testing.T) { 360 t.Parallel() 361 testJSONMarshal(t, &Jobs{}, "{}") 362 363 u := &Jobs{ 364 TotalCount: Int(1), 365 Jobs: []*WorkflowJob{ 366 { 367 ID: Int64(1), 368 RunID: Int64(1), 369 RunURL: String("r"), 370 NodeID: String("n"), 371 HeadBranch: String("b"), 372 HeadSHA: String("h"), 373 URL: String("u"), 374 HTMLURL: String("h"), 375 Status: String("s"), 376 Conclusion: String("c"), 377 CreatedAt: &Timestamp{referenceTime}, 378 StartedAt: &Timestamp{referenceTime}, 379 CompletedAt: &Timestamp{referenceTime}, 380 Name: String("n"), 381 Steps: []*TaskStep{ 382 { 383 Name: String("n"), 384 Status: String("s"), 385 Conclusion: String("c"), 386 Number: Int64(1), 387 StartedAt: &Timestamp{referenceTime}, 388 CompletedAt: &Timestamp{referenceTime}, 389 }, 390 }, 391 CheckRunURL: String("c"), 392 RunAttempt: Int64(2), 393 WorkflowName: String("w"), 394 }, 395 }, 396 } 397 398 want := `{ 399 "total_count": 1, 400 "jobs": [{ 401 "id": 1, 402 "run_id": 1, 403 "run_url": "r", 404 "node_id": "n", 405 "head_branch": "b", 406 "head_sha": "h", 407 "url": "u", 408 "html_url": "h", 409 "status": "s", 410 "conclusion": "c", 411 "created_at": ` + referenceTimeStr + `, 412 "started_at": ` + referenceTimeStr + `, 413 "completed_at": ` + referenceTimeStr + `, 414 "name": "n", 415 "steps": [{ 416 "name": "n", 417 "status": "s", 418 "conclusion": "c", 419 "number": 1, 420 "started_at": ` + referenceTimeStr + `, 421 "completed_at": ` + referenceTimeStr + ` 422 }], 423 "check_run_url": "c", 424 "run_attempt": 2, 425 "workflow_name": "w" 426 }] 427 }` 428 429 testJSONMarshal(t, u, want) 430 }