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