github.com/google/go-github/v68@v68.0.0/github/actions_required_workflows_test.go (about) 1 // Copyright 2023 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 "fmt" 11 "net/http" 12 "testing" 13 "time" 14 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestActionsService_ListOrgRequiredWorkflows(t *testing.T) { 19 t.Parallel() 20 client, mux, _ := setup(t) 21 22 mux.HandleFunc("/orgs/o/actions/required_workflows", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 25 fmt.Fprint(w, `{"total_count":4,"required_workflows": [ 26 { 27 "id": 30433642, 28 "name": "Required CI", 29 "path": ".github/workflows/ci.yml", 30 "scope": "selected", 31 "ref": "refs/head/main", 32 "state": "active", 33 "selected_repositories_url": "https://api.github.com/organizations/org/actions/required_workflows/1/repositories", 34 "created_at": "2020-01-22T19:33:08Z", 35 "updated_at": "2020-01-22T19:33:08Z" 36 }, 37 { 38 "id": 30433643, 39 "name": "Required Linter", 40 "path": ".github/workflows/lint.yml", 41 "scope": "all", 42 "ref": "refs/head/main", 43 "state": "active", 44 "created_at": "2020-01-22T19:33:08Z", 45 "updated_at": "2020-01-22T19:33:08Z" 46 } 47 ] 48 }`) 49 }) 50 opts := &ListOptions{Page: 2, PerPage: 2} 51 ctx := context.Background() 52 jobs, _, err := client.Actions.ListOrgRequiredWorkflows(ctx, "o", opts) 53 54 if err != nil { 55 t.Errorf("Actions.ListOrgRequiredWorkflows returned error: %v", err) 56 } 57 58 want := &OrgRequiredWorkflows{ 59 TotalCount: Ptr(4), 60 RequiredWorkflows: []*OrgRequiredWorkflow{ 61 {ID: Ptr(int64(30433642)), Name: Ptr("Required CI"), Path: Ptr(".github/workflows/ci.yml"), Scope: Ptr("selected"), Ref: Ptr("refs/head/main"), State: Ptr("active"), SelectedRepositoriesURL: Ptr("https://api.github.com/organizations/org/actions/required_workflows/1/repositories"), CreatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)}}, 62 {ID: Ptr(int64(30433643)), Name: Ptr("Required Linter"), Path: Ptr(".github/workflows/lint.yml"), Scope: Ptr("all"), Ref: Ptr("refs/head/main"), State: Ptr("active"), CreatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)}}, 63 }, 64 } 65 if !cmp.Equal(jobs, want) { 66 t.Errorf("Actions.ListOrgRequiredWorkflows returned %+v, want %+v", jobs, want) 67 } 68 const methodName = "ListOrgRequiredWorkflows" 69 testBadOptions(t, methodName, func() (err error) { 70 _, _, err = client.Actions.ListOrgRequiredWorkflows(ctx, "\n", opts) 71 return err 72 }) 73 74 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 75 got, resp, err := client.Actions.ListOrgRequiredWorkflows(ctx, "o", opts) 76 if got != nil { 77 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 78 } 79 return resp, err 80 }) 81 } 82 83 func TestActionsService_CreateRequiredWorkflow(t *testing.T) { 84 t.Parallel() 85 client, mux, _ := setup(t) 86 87 mux.HandleFunc("/orgs/o/actions/required_workflows", func(w http.ResponseWriter, r *http.Request) { 88 testMethod(t, r, "POST") 89 testHeader(t, r, "Content-Type", "application/json") 90 testBody(t, r, `{"workflow_file_path":".github/workflows/ci.yaml","repository_id":53,"scope":"selected","selected_repository_ids":[32,91]}`+"\n") 91 fmt.Fprint(w, `{ 92 "id": 2, 93 "name": "Required CI", 94 "path": ".github/workflows/ci.yml", 95 "scope": "selected", 96 "ref": "refs/head/main", 97 "state": "active", 98 "selected_repositories_url": "https://api.github.com/orgs/octo-org/actions/required_workflows/2/repositories", 99 "created_at": "2020-01-22T19:33:08Z", 100 "updated_at": "2020-01-22T19:33:08Z", 101 "repository": { 102 "id": 53, 103 "name": "Hello-World", 104 "url": "https://api.github.com/repos/o/Hello-World"}}`) 105 }) 106 input := &CreateUpdateRequiredWorkflowOptions{ 107 WorkflowFilePath: Ptr(".github/workflows/ci.yaml"), 108 RepositoryID: Ptr(int64(53)), 109 Scope: Ptr("selected"), 110 SelectedRepositoryIDs: &SelectedRepoIDs{32, 91}, 111 } 112 ctx := context.Background() 113 requiredWokflow, _, err := client.Actions.CreateRequiredWorkflow(ctx, "o", input) 114 if err != nil { 115 t.Errorf("Actions.CreateRequiredWorkflow returned error: %v", err) 116 } 117 want := &OrgRequiredWorkflow{ 118 ID: Ptr(int64(2)), 119 Name: Ptr("Required CI"), 120 Path: Ptr(".github/workflows/ci.yml"), 121 Scope: Ptr("selected"), 122 Ref: Ptr("refs/head/main"), 123 State: Ptr("active"), 124 SelectedRepositoriesURL: Ptr("https://api.github.com/orgs/octo-org/actions/required_workflows/2/repositories"), 125 CreatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)}, 126 UpdatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)}, 127 Repository: &Repository{ID: Ptr(int64(53)), URL: Ptr("https://api.github.com/repos/o/Hello-World"), Name: Ptr("Hello-World")}, 128 } 129 130 if !cmp.Equal(requiredWokflow, want) { 131 t.Errorf("Actions.CreateRequiredWorkflow returned %+v, want %+v", requiredWokflow, want) 132 } 133 134 const methodName = "CreateRequiredWorkflow" 135 testBadOptions(t, methodName, func() (err error) { 136 _, _, err = client.Actions.CreateRequiredWorkflow(ctx, "\n", input) 137 return err 138 }) 139 140 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 141 got, resp, err := client.Actions.CreateRequiredWorkflow(ctx, "o", input) 142 if got != nil { 143 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 144 } 145 return resp, err 146 }) 147 } 148 149 func TestActionsService_GetRequiredWorkflowByID(t *testing.T) { 150 t.Parallel() 151 client, mux, _ := setup(t) 152 153 mux.HandleFunc("/orgs/o/actions/required_workflows/12345", func(w http.ResponseWriter, r *http.Request) { 154 testMethod(t, r, "GET") 155 fmt.Fprint(w, `{ 156 "id": 12345, 157 "name": "Required CI", 158 "path": ".github/workflows/ci.yml", 159 "scope": "selected", 160 "ref": "refs/head/main", 161 "state": "active", 162 "selected_repositories_url": "https://api.github.com/orgs/o/actions/required_workflows/12345/repositories", 163 "created_at": "2020-01-22T19:33:08Z", 164 "updated_at": "2020-01-22T19:33:08Z", 165 "repository":{ 166 "id": 1296269, 167 "url": "https://api.github.com/repos/o/Hello-World", 168 "name": "Hello-World" 169 } 170 }`) 171 }) 172 ctx := context.Background() 173 jobs, _, err := client.Actions.GetRequiredWorkflowByID(ctx, "o", 12345) 174 175 if err != nil { 176 t.Errorf("Actions.GetRequiredWorkflowByID returned error: %v", err) 177 } 178 179 want := &OrgRequiredWorkflow{ 180 ID: Ptr(int64(12345)), Name: Ptr("Required CI"), Path: Ptr(".github/workflows/ci.yml"), Scope: Ptr("selected"), Ref: Ptr("refs/head/main"), State: Ptr("active"), SelectedRepositoriesURL: Ptr("https://api.github.com/orgs/o/actions/required_workflows/12345/repositories"), CreatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)}, Repository: &Repository{ID: Ptr(int64(1296269)), URL: Ptr("https://api.github.com/repos/o/Hello-World"), Name: Ptr("Hello-World")}, 181 } 182 if !cmp.Equal(jobs, want) { 183 t.Errorf("Actions.GetRequiredWorkflowByID returned %+v, want %+v", jobs, want) 184 } 185 const methodName = "GetRequiredWorkflowByID" 186 testBadOptions(t, methodName, func() (err error) { 187 _, _, err = client.Actions.GetRequiredWorkflowByID(ctx, "\n", 1) 188 return err 189 }) 190 191 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 192 got, resp, err := client.Actions.GetRequiredWorkflowByID(ctx, "o", 12345) 193 if got != nil { 194 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 195 } 196 return resp, err 197 }) 198 } 199 200 func TestActionsService_UpdateRequiredWorkflow(t *testing.T) { 201 t.Parallel() 202 client, mux, _ := setup(t) 203 204 mux.HandleFunc("/orgs/o/actions/required_workflows/12345", func(w http.ResponseWriter, r *http.Request) { 205 testMethod(t, r, "PATCH") 206 testHeader(t, r, "Content-Type", "application/json") 207 testBody(t, r, `{"workflow_file_path":".github/workflows/ci.yaml","repository_id":53,"scope":"selected","selected_repository_ids":[32,91]}`+"\n") 208 fmt.Fprint(w, `{ 209 "id": 12345, 210 "name": "Required CI", 211 "path": ".github/workflows/ci.yml", 212 "scope": "selected", 213 "ref": "refs/head/main", 214 "state": "active", 215 "selected_repositories_url": "https://api.github.com/orgs/octo-org/actions/required_workflows/12345/repositories", 216 "created_at": "2020-01-22T19:33:08Z", 217 "updated_at": "2020-01-22T19:33:08Z", 218 "repository": { 219 "id": 53, 220 "name": "Hello-World", 221 "url": "https://api.github.com/repos/o/Hello-World"}}`) 222 }) 223 input := &CreateUpdateRequiredWorkflowOptions{ 224 WorkflowFilePath: Ptr(".github/workflows/ci.yaml"), 225 RepositoryID: Ptr(int64(53)), 226 Scope: Ptr("selected"), 227 SelectedRepositoryIDs: &SelectedRepoIDs{32, 91}, 228 } 229 ctx := context.Background() 230 231 requiredWokflow, _, err := client.Actions.UpdateRequiredWorkflow(ctx, "o", 12345, input) 232 233 if err != nil { 234 t.Errorf("Actions.UpdateRequiredWorkflow returned error: %v", err) 235 } 236 want := &OrgRequiredWorkflow{ 237 ID: Ptr(int64(12345)), 238 Name: Ptr("Required CI"), 239 Path: Ptr(".github/workflows/ci.yml"), 240 Scope: Ptr("selected"), 241 Ref: Ptr("refs/head/main"), 242 State: Ptr("active"), 243 SelectedRepositoriesURL: Ptr("https://api.github.com/orgs/octo-org/actions/required_workflows/12345/repositories"), 244 CreatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)}, 245 UpdatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)}, 246 Repository: &Repository{ID: Ptr(int64(53)), URL: Ptr("https://api.github.com/repos/o/Hello-World"), Name: Ptr("Hello-World")}, 247 } 248 249 if !cmp.Equal(requiredWokflow, want) { 250 t.Errorf("Actions.UpdateRequiredWorkflow returned %+v, want %+v", requiredWokflow, want) 251 } 252 253 const methodName = "UpdateRequiredWorkflow" 254 testBadOptions(t, methodName, func() (err error) { 255 _, _, err = client.Actions.UpdateRequiredWorkflow(ctx, "\n", 12345, input) 256 return err 257 }) 258 259 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 260 got, resp, err := client.Actions.UpdateRequiredWorkflow(ctx, "o", 12345, input) 261 if got != nil { 262 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 263 } 264 return resp, err 265 }) 266 } 267 268 func TestActionsService_DeleteRequiredWorkflow(t *testing.T) { 269 t.Parallel() 270 client, mux, _ := setup(t) 271 272 mux.HandleFunc("/orgs/o/actions/required_workflows/12345", func(w http.ResponseWriter, r *http.Request) { 273 testMethod(t, r, "DELETE") 274 w.WriteHeader(http.StatusNoContent) 275 }) 276 ctx := context.Background() 277 _, err := client.Actions.DeleteRequiredWorkflow(ctx, "o", 12345) 278 279 if err != nil { 280 t.Errorf("Actions.DeleteRequiredWorkflow returned error: %v", err) 281 } 282 283 const methodName = "DeleteRequiredWorkflow" 284 testBadOptions(t, methodName, func() (err error) { 285 _, err = client.Actions.DeleteRequiredWorkflow(ctx, "\n", 12345) 286 return err 287 }) 288 289 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 290 return client.Actions.DeleteRequiredWorkflow(ctx, "o", 12345) 291 }) 292 } 293 294 func TestActionsService_ListRequiredWorkflowSelectedRepos(t *testing.T) { 295 t.Parallel() 296 client, mux, _ := setup(t) 297 298 mux.HandleFunc("/orgs/o/actions/required_workflows/12345/repositories", func(w http.ResponseWriter, r *http.Request) { 299 testMethod(t, r, "GET") 300 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 301 fmt.Fprint(w, `{"total_count":1, 302 "repositories": [{ 303 "id": 1296269, 304 "url": "https://api.github.com/repos/o/Hello-World", 305 "name": "Hello-World" 306 }] 307 }`) 308 }) 309 opts := &ListOptions{Page: 2, PerPage: 2} 310 ctx := context.Background() 311 jobs, _, err := client.Actions.ListRequiredWorkflowSelectedRepos(ctx, "o", 12345, opts) 312 313 if err != nil { 314 t.Errorf("Actions.ListRequiredWorkflowSelectedRepositories returned error: %v", err) 315 } 316 317 want := &RequiredWorkflowSelectedRepos{ 318 TotalCount: Ptr(1), 319 Repositories: []*Repository{ 320 {ID: Ptr(int64(1296269)), URL: Ptr("https://api.github.com/repos/o/Hello-World"), Name: Ptr("Hello-World")}, 321 }, 322 } 323 if !cmp.Equal(jobs, want) { 324 t.Errorf("Actions.ListRequiredWorkflowSelectedRepositories returned %+v, want %+v", jobs, want) 325 } 326 const methodName = "ListRequiredWorkflowSelectedRepositories" 327 testBadOptions(t, methodName, func() (err error) { 328 _, _, err = client.Actions.ListRequiredWorkflowSelectedRepos(ctx, "\n", 12345, opts) 329 return err 330 }) 331 332 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 333 got, resp, err := client.Actions.ListRequiredWorkflowSelectedRepos(ctx, "o", 12345, opts) 334 if got != nil { 335 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 336 } 337 return resp, err 338 }) 339 } 340 341 func TestActionsService_SetRequiredWorkflowSelectedRepos(t *testing.T) { 342 t.Parallel() 343 client, mux, _ := setup(t) 344 345 mux.HandleFunc("/orgs/o/actions/required_workflows/12345/repositories", func(w http.ResponseWriter, r *http.Request) { 346 testMethod(t, r, "PUT") 347 testHeader(t, r, "Content-Type", "application/json") 348 testBody(t, r, `{"selected_repository_ids":[32,91]}`+"\n") 349 w.WriteHeader(http.StatusNoContent) 350 }) 351 ctx := context.Background() 352 _, err := client.Actions.SetRequiredWorkflowSelectedRepos(ctx, "o", 12345, SelectedRepoIDs{32, 91}) 353 354 if err != nil { 355 t.Errorf("Actions.SetRequiredWorkflowSelectedRepositories returned error: %v", err) 356 } 357 358 const methodName = "SetRequiredWorkflowSelectedRepositories" 359 testBadOptions(t, methodName, func() (err error) { 360 _, err = client.Actions.SetRequiredWorkflowSelectedRepos(ctx, "\n", 12345, SelectedRepoIDs{32, 91}) 361 return err 362 }) 363 364 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 365 return client.Actions.SetRequiredWorkflowSelectedRepos(ctx, "o", 12345, SelectedRepoIDs{32, 91}) 366 }) 367 } 368 369 func TestActionsService_AddRepoToRequiredWorkflow(t *testing.T) { 370 t.Parallel() 371 client, mux, _ := setup(t) 372 373 mux.HandleFunc("/orgs/o/actions/required_workflows/12345/repositories/32", func(w http.ResponseWriter, r *http.Request) { 374 testMethod(t, r, "PUT") 375 w.WriteHeader(http.StatusNoContent) 376 }) 377 ctx := context.Background() 378 _, err := client.Actions.AddRepoToRequiredWorkflow(ctx, "o", 12345, 32) 379 380 if err != nil { 381 t.Errorf("Actions.AddRepoToRequiredWorkflow returned error: %v", err) 382 } 383 384 const methodName = "AddRepoToRequiredWorkflow" 385 testBadOptions(t, methodName, func() (err error) { 386 _, err = client.Actions.AddRepoToRequiredWorkflow(ctx, "\n", 12345, 32) 387 return err 388 }) 389 390 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 391 return client.Actions.AddRepoToRequiredWorkflow(ctx, "o", 12345, 32) 392 }) 393 } 394 395 func TestActionsService_RemoveRepoFromRequiredWorkflow(t *testing.T) { 396 t.Parallel() 397 client, mux, _ := setup(t) 398 399 mux.HandleFunc("/orgs/o/actions/required_workflows/12345/repositories/32", func(w http.ResponseWriter, r *http.Request) { 400 testMethod(t, r, "DELETE") 401 w.WriteHeader(http.StatusNoContent) 402 }) 403 ctx := context.Background() 404 _, err := client.Actions.RemoveRepoFromRequiredWorkflow(ctx, "o", 12345, 32) 405 406 if err != nil { 407 t.Errorf("Actions.RemoveRepoFromRequiredWorkflow returned error: %v", err) 408 } 409 410 const methodName = "RemoveRepoFromRequiredWorkflow" 411 testBadOptions(t, methodName, func() (err error) { 412 _, err = client.Actions.RemoveRepoFromRequiredWorkflow(ctx, "\n", 12345, 32) 413 return err 414 }) 415 416 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 417 return client.Actions.RemoveRepoFromRequiredWorkflow(ctx, "o", 12345, 32) 418 }) 419 } 420 421 func TestActionsService_ListRepoRequiredWorkflows(t *testing.T) { 422 t.Parallel() 423 client, mux, _ := setup(t) 424 425 mux.HandleFunc("/repos/o/r/actions/required_workflows", func(w http.ResponseWriter, r *http.Request) { 426 testMethod(t, r, "GET") 427 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 428 fmt.Fprint(w, `{"total_count":1,"required_workflows": [ 429 { 430 "id": 30433642, 431 "node_id": "MDg6V29ya2Zsb3cxNjEzMzU=", 432 "name": "Required CI", 433 "path": ".github/workflows/ci.yml", 434 "state": "active", 435 "created_at": "2020-01-22T19:33:08Z", 436 "updated_at": "2020-01-22T19:33:08Z", 437 "url": "https://api.github.com/repos/o/r/actions/required_workflows/161335", 438 "html_url": "https://github.com/o/r/blob/master/o/hello-world/.github/workflows/required_ci.yaml", 439 "badge_url": "https://github.com/o/r/workflows/required/o/hello-world/.github/workflows/required_ci.yaml/badge.svg", 440 "source_repository":{ 441 "id": 1296269, 442 "url": "https://api.github.com/repos/o/Hello-World", 443 "name": "Hello-World" 444 } 445 } 446 ] 447 }`) 448 }) 449 opts := &ListOptions{Page: 2, PerPage: 2} 450 ctx := context.Background() 451 jobs, _, err := client.Actions.ListRepoRequiredWorkflows(ctx, "o", "r", opts) 452 453 if err != nil { 454 t.Errorf("Actions.ListRepoRequiredWorkflows returned error: %v", err) 455 } 456 457 want := &RepoRequiredWorkflows{ 458 TotalCount: Ptr(1), 459 RequiredWorkflows: []*RepoRequiredWorkflow{ 460 {ID: Ptr(int64(30433642)), NodeID: Ptr("MDg6V29ya2Zsb3cxNjEzMzU="), Name: Ptr("Required CI"), Path: Ptr(".github/workflows/ci.yml"), State: Ptr("active"), CreatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)}, URL: Ptr("https://api.github.com/repos/o/r/actions/required_workflows/161335"), BadgeURL: Ptr("https://github.com/o/r/workflows/required/o/hello-world/.github/workflows/required_ci.yaml/badge.svg"), HTMLURL: Ptr("https://github.com/o/r/blob/master/o/hello-world/.github/workflows/required_ci.yaml"), SourceRepository: &Repository{ID: Ptr(int64(1296269)), URL: Ptr("https://api.github.com/repos/o/Hello-World"), Name: Ptr("Hello-World")}}, 461 }, 462 } 463 if !cmp.Equal(jobs, want) { 464 t.Errorf("Actions.ListRepoRequiredWorkflows returned %+v, want %+v", jobs, want) 465 } 466 const methodName = "ListRepoRequiredWorkflows" 467 testBadOptions(t, methodName, func() (err error) { 468 _, _, err = client.Actions.ListRepoRequiredWorkflows(ctx, "\n", "\n", opts) 469 return err 470 }) 471 472 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 473 got, resp, err := client.Actions.ListRepoRequiredWorkflows(ctx, "o", "r", opts) 474 if got != nil { 475 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 476 } 477 return resp, err 478 }) 479 }