github.com/google/go-github/v70@v70.0.0/github/actions_variables_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_ListRepoVariables(t *testing.T) { 19 t.Parallel() 20 client, mux, _ := setup(t) 21 22 mux.HandleFunc("/repos/o/r/actions/variables", 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,"variables":[{"name":"A","value":"AA","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"name":"B","value":"BB","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`) 26 }) 27 28 opts := &ListOptions{Page: 2, PerPage: 2} 29 ctx := context.Background() 30 variables, _, err := client.Actions.ListRepoVariables(ctx, "o", "r", opts) 31 if err != nil { 32 t.Errorf("Actions.ListRepoVariables returned error: %v", err) 33 } 34 35 want := &ActionsVariables{ 36 TotalCount: 4, 37 Variables: []*ActionsVariable{ 38 {Name: "A", Value: "AA", 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)}}, 39 {Name: "B", Value: "BB", 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)}}, 40 }, 41 } 42 if !cmp.Equal(variables, want) { 43 t.Errorf("Actions.ListRepoVariables returned %+v, want %+v", variables, want) 44 } 45 46 const methodName = "ListRepoVariables" 47 testBadOptions(t, methodName, func() (err error) { 48 _, _, err = client.Actions.ListRepoVariables(ctx, "\n", "\n", opts) 49 return err 50 }) 51 52 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 53 got, resp, err := client.Actions.ListRepoVariables(ctx, "o", "r", opts) 54 if got != nil { 55 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 56 } 57 return resp, err 58 }) 59 } 60 61 func TestActionsService_ListRepoOrgVariables(t *testing.T) { 62 t.Parallel() 63 client, mux, _ := setup(t) 64 65 mux.HandleFunc("/repos/o/r/actions/organization-variables", func(w http.ResponseWriter, r *http.Request) { 66 testMethod(t, r, "GET") 67 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 68 fmt.Fprint(w, `{"total_count":4,"variables":[{"name":"A","value":"AA","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"name":"B","value":"BB","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`) 69 }) 70 71 opts := &ListOptions{Page: 2, PerPage: 2} 72 ctx := context.Background() 73 variables, _, err := client.Actions.ListRepoOrgVariables(ctx, "o", "r", opts) 74 if err != nil { 75 t.Errorf("Actions.ListRepoOrgVariables returned error: %v", err) 76 } 77 78 want := &ActionsVariables{ 79 TotalCount: 4, 80 Variables: []*ActionsVariable{ 81 {Name: "A", Value: "AA", 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)}}, 82 {Name: "B", Value: "BB", 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)}}, 83 }, 84 } 85 if !cmp.Equal(variables, want) { 86 t.Errorf("Actions.ListRepoOrgVariables returned %+v, want %+v", variables, want) 87 } 88 89 const methodName = "ListRepoOrgVariables" 90 testBadOptions(t, methodName, func() (err error) { 91 _, _, err = client.Actions.ListRepoOrgVariables(ctx, "\n", "\n", opts) 92 return err 93 }) 94 95 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 96 got, resp, err := client.Actions.ListRepoOrgVariables(ctx, "o", "r", opts) 97 if got != nil { 98 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 99 } 100 return resp, err 101 }) 102 } 103 104 func TestActionsService_GetRepoVariable(t *testing.T) { 105 t.Parallel() 106 client, mux, _ := setup(t) 107 108 mux.HandleFunc("/repos/o/r/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { 109 testMethod(t, r, "GET") 110 fmt.Fprint(w, `{"name":"NAME","value":"VALUE","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`) 111 }) 112 113 ctx := context.Background() 114 variable, _, err := client.Actions.GetRepoVariable(ctx, "o", "r", "NAME") 115 if err != nil { 116 t.Errorf("Actions.GetRepoVariable returned error: %v", err) 117 } 118 119 want := &ActionsVariable{ 120 Name: "NAME", 121 Value: "VALUE", 122 CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, 123 UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, 124 } 125 if !cmp.Equal(variable, want) { 126 t.Errorf("Actions.GetRepoVariable returned %+v, want %+v", variable, want) 127 } 128 129 const methodName = "GetRepoVariable" 130 testBadOptions(t, methodName, func() (err error) { 131 _, _, err = client.Actions.GetRepoVariable(ctx, "\n", "\n", "\n") 132 return err 133 }) 134 135 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 136 got, resp, err := client.Actions.GetRepoVariable(ctx, "o", "r", "NAME") 137 if got != nil { 138 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 139 } 140 return resp, err 141 }) 142 } 143 144 func TestActionsService_CreateRepoVariable(t *testing.T) { 145 t.Parallel() 146 client, mux, _ := setup(t) 147 148 mux.HandleFunc("/repos/o/r/actions/variables", func(w http.ResponseWriter, r *http.Request) { 149 testMethod(t, r, "POST") 150 testHeader(t, r, "Content-Type", "application/json") 151 testBody(t, r, `{"name":"NAME","value":"VALUE"}`+"\n") 152 w.WriteHeader(http.StatusCreated) 153 }) 154 155 input := &ActionsVariable{ 156 Name: "NAME", 157 Value: "VALUE", 158 } 159 ctx := context.Background() 160 _, err := client.Actions.CreateRepoVariable(ctx, "o", "r", input) 161 if err != nil { 162 t.Errorf("Actions.CreateRepoVariable returned error: %v", err) 163 } 164 165 const methodName = "CreateRepoVariable" 166 testBadOptions(t, methodName, func() (err error) { 167 _, err = client.Actions.CreateRepoVariable(ctx, "\n", "\n", input) 168 return err 169 }) 170 171 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 172 return client.Actions.CreateRepoVariable(ctx, "o", "r", input) 173 }) 174 } 175 176 func TestActionsService_UpdateRepoVariable(t *testing.T) { 177 t.Parallel() 178 client, mux, _ := setup(t) 179 180 mux.HandleFunc("/repos/o/r/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { 181 testMethod(t, r, "PATCH") 182 testHeader(t, r, "Content-Type", "application/json") 183 testBody(t, r, `{"name":"NAME","value":"VALUE"}`+"\n") 184 w.WriteHeader(http.StatusNoContent) 185 }) 186 187 input := &ActionsVariable{ 188 Name: "NAME", 189 Value: "VALUE", 190 } 191 ctx := context.Background() 192 _, err := client.Actions.UpdateRepoVariable(ctx, "o", "r", input) 193 if err != nil { 194 t.Errorf("Actions.UpdateRepoVariable returned error: %v", err) 195 } 196 197 const methodName = "UpdateRepoVariable" 198 testBadOptions(t, methodName, func() (err error) { 199 _, err = client.Actions.UpdateRepoVariable(ctx, "\n", "\n", input) 200 return err 201 }) 202 203 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 204 return client.Actions.UpdateRepoVariable(ctx, "o", "r", input) 205 }) 206 } 207 208 func TestActionsService_DeleteRepoVariable(t *testing.T) { 209 t.Parallel() 210 client, mux, _ := setup(t) 211 212 mux.HandleFunc("/repos/o/r/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { 213 testMethod(t, r, "DELETE") 214 }) 215 216 ctx := context.Background() 217 _, err := client.Actions.DeleteRepoVariable(ctx, "o", "r", "NAME") 218 if err != nil { 219 t.Errorf("Actions.( returned error: %v", err) 220 } 221 222 const methodName = "DeleteRepoVariable" 223 testBadOptions(t, methodName, func() (err error) { 224 _, err = client.Actions.DeleteRepoVariable(ctx, "\n", "\n", "\n") 225 return err 226 }) 227 228 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 229 return client.Actions.DeleteRepoVariable(ctx, "o", "r", "NAME") 230 }) 231 } 232 233 func TestActionsService_ListOrgVariables(t *testing.T) { 234 t.Parallel() 235 client, mux, _ := setup(t) 236 237 mux.HandleFunc("/orgs/o/actions/variables", func(w http.ResponseWriter, r *http.Request) { 238 testMethod(t, r, "GET") 239 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 240 fmt.Fprint(w, `{"total_count":3,"variables":[{"name":"A","value":"AA","created_at":"2019-08-10T14:59:22Z","updated_at":"2020-01-10T14:59:22Z","visibility":"private"},{"name":"B","value":"BB","created_at":"2019-08-10T14:59:22Z","updated_at":"2020-01-10T14:59:22Z","visibility":"all"},{"name":"C","value":"CC","created_at":"2019-08-10T14:59:22Z","updated_at":"2020-01-10T14:59:22Z","visibility":"selected","selected_repositories_url":"https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories"}]}`) 241 }) 242 243 opts := &ListOptions{Page: 2, PerPage: 2} 244 ctx := context.Background() 245 variables, _, err := client.Actions.ListOrgVariables(ctx, "o", opts) 246 if err != nil { 247 t.Errorf("Actions.ListOrgVariables returned error: %v", err) 248 } 249 250 want := &ActionsVariables{ 251 TotalCount: 3, 252 Variables: []*ActionsVariable{ 253 {Name: "A", Value: "AA", CreatedAt: &Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: Ptr("private")}, 254 {Name: "B", Value: "BB", CreatedAt: &Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: Ptr("all")}, 255 {Name: "C", Value: "CC", CreatedAt: &Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: Ptr("selected"), SelectedRepositoriesURL: Ptr("https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories")}, 256 }, 257 } 258 if !cmp.Equal(variables, want) { 259 t.Errorf("Actions.ListOrgVariables returned %+v, want %+v", variables, want) 260 } 261 262 const methodName = "ListOrgVariables" 263 testBadOptions(t, methodName, func() (err error) { 264 _, _, err = client.Actions.ListOrgVariables(ctx, "\n", opts) 265 return err 266 }) 267 268 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 269 got, resp, err := client.Actions.ListOrgVariables(ctx, "o", opts) 270 if got != nil { 271 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 272 } 273 return resp, err 274 }) 275 } 276 277 func TestActionsService_GetOrgVariable(t *testing.T) { 278 t.Parallel() 279 client, mux, _ := setup(t) 280 281 mux.HandleFunc("/orgs/o/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { 282 testMethod(t, r, "GET") 283 fmt.Fprint(w, `{"name":"NAME","value":"VALUE","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z","visibility":"selected","selected_repositories_url":"https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories"}`) 284 }) 285 286 ctx := context.Background() 287 variable, _, err := client.Actions.GetOrgVariable(ctx, "o", "NAME") 288 if err != nil { 289 t.Errorf("Actions.GetOrgVariable returned error: %v", err) 290 } 291 292 want := &ActionsVariable{ 293 Name: "NAME", 294 Value: "VALUE", 295 CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, 296 UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, 297 Visibility: Ptr("selected"), 298 SelectedRepositoriesURL: Ptr("https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories"), 299 } 300 if !cmp.Equal(variable, want) { 301 t.Errorf("Actions.GetOrgVariable returned %+v, want %+v", variable, want) 302 } 303 304 const methodName = "GetOrgVariable" 305 testBadOptions(t, methodName, func() (err error) { 306 _, _, err = client.Actions.GetOrgVariable(ctx, "\n", "\n") 307 return err 308 }) 309 310 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 311 got, resp, err := client.Actions.GetOrgVariable(ctx, "o", "NAME") 312 if got != nil { 313 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 314 } 315 return resp, err 316 }) 317 } 318 319 func TestActionsService_CreateOrgVariable(t *testing.T) { 320 t.Parallel() 321 client, mux, _ := setup(t) 322 323 mux.HandleFunc("/orgs/o/actions/variables", func(w http.ResponseWriter, r *http.Request) { 324 testMethod(t, r, "POST") 325 testHeader(t, r, "Content-Type", "application/json") 326 testBody(t, r, `{"name":"NAME","value":"VALUE","visibility":"selected","selected_repository_ids":[1296269,1269280]}`+"\n") 327 w.WriteHeader(http.StatusCreated) 328 }) 329 330 input := &ActionsVariable{ 331 Name: "NAME", 332 Value: "VALUE", 333 Visibility: Ptr("selected"), 334 SelectedRepositoryIDs: &SelectedRepoIDs{1296269, 1269280}, 335 } 336 ctx := context.Background() 337 _, err := client.Actions.CreateOrgVariable(ctx, "o", input) 338 if err != nil { 339 t.Errorf("Actions.CreateOrgVariable returned error: %v", err) 340 } 341 342 const methodName = "CreateOrgVariable" 343 testBadOptions(t, methodName, func() (err error) { 344 _, err = client.Actions.CreateOrgVariable(ctx, "\n", input) 345 return err 346 }) 347 348 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 349 return client.Actions.CreateOrgVariable(ctx, "o", input) 350 }) 351 } 352 353 func TestActionsService_UpdateOrgVariable(t *testing.T) { 354 t.Parallel() 355 client, mux, _ := setup(t) 356 357 mux.HandleFunc("/orgs/o/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { 358 testMethod(t, r, "PATCH") 359 testHeader(t, r, "Content-Type", "application/json") 360 testBody(t, r, `{"name":"NAME","value":"VALUE","visibility":"selected","selected_repository_ids":[1296269,1269280]}`+"\n") 361 w.WriteHeader(http.StatusNoContent) 362 }) 363 364 input := &ActionsVariable{ 365 Name: "NAME", 366 Value: "VALUE", 367 Visibility: Ptr("selected"), 368 SelectedRepositoryIDs: &SelectedRepoIDs{1296269, 1269280}, 369 } 370 ctx := context.Background() 371 _, err := client.Actions.UpdateOrgVariable(ctx, "o", input) 372 if err != nil { 373 t.Errorf("Actions.UpdateOrgVariable returned error: %v", err) 374 } 375 376 const methodName = "UpdateOrgVariable" 377 testBadOptions(t, methodName, func() (err error) { 378 _, err = client.Actions.UpdateOrgVariable(ctx, "\n", input) 379 return err 380 }) 381 382 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 383 return client.Actions.UpdateOrgVariable(ctx, "o", input) 384 }) 385 } 386 387 func TestActionsService_ListSelectedReposForOrgVariable(t *testing.T) { 388 t.Parallel() 389 client, mux, _ := setup(t) 390 391 mux.HandleFunc("/orgs/o/actions/variables/NAME/repositories", func(w http.ResponseWriter, r *http.Request) { 392 testMethod(t, r, "GET") 393 fmt.Fprintf(w, `{"total_count":1,"repositories":[{"id":1}]}`) 394 }) 395 396 opts := &ListOptions{Page: 2, PerPage: 2} 397 ctx := context.Background() 398 repos, _, err := client.Actions.ListSelectedReposForOrgVariable(ctx, "o", "NAME", opts) 399 if err != nil { 400 t.Errorf("Actions.( returned error: %v", err) 401 } 402 403 want := &SelectedReposList{ 404 TotalCount: Ptr(1), 405 Repositories: []*Repository{ 406 {ID: Ptr(int64(1))}, 407 }, 408 } 409 if !cmp.Equal(repos, want) { 410 t.Errorf("Actions.( returned %+v, want %+v", repos, want) 411 } 412 413 const methodName = "ListSelectedReposForOrgVariable" 414 testBadOptions(t, methodName, func() (err error) { 415 _, _, err = client.Actions.ListSelectedReposForOrgVariable(ctx, "\n", "\n", opts) 416 return err 417 }) 418 419 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 420 got, resp, err := client.Actions.ListSelectedReposForOrgVariable(ctx, "o", "NAME", opts) 421 if got != nil { 422 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 423 } 424 return resp, err 425 }) 426 } 427 428 func TestActionsService_SetSelectedReposForOrgSVariable(t *testing.T) { 429 t.Parallel() 430 client, mux, _ := setup(t) 431 432 mux.HandleFunc("/orgs/o/actions/variables/NAME/repositories", func(w http.ResponseWriter, r *http.Request) { 433 testMethod(t, r, "PUT") 434 testHeader(t, r, "Content-Type", "application/json") 435 testBody(t, r, `{"selected_repository_ids":[64780797]}`+"\n") 436 }) 437 438 ctx := context.Background() 439 _, err := client.Actions.SetSelectedReposForOrgVariable(ctx, "o", "NAME", SelectedRepoIDs{64780797}) 440 if err != nil { 441 t.Errorf("Actions.( returned error: %v", err) 442 } 443 444 const methodName = "SetSelectedReposForOrgVariable" 445 testBadOptions(t, methodName, func() (err error) { 446 _, err = client.Actions.SetSelectedReposForOrgVariable(ctx, "\n", "\n", SelectedRepoIDs{64780797}) 447 return err 448 }) 449 450 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 451 return client.Actions.SetSelectedReposForOrgVariable(ctx, "o", "NAME", SelectedRepoIDs{64780797}) 452 }) 453 } 454 455 func TestActionsService_AddSelectedRepoToOrgVariable(t *testing.T) { 456 t.Parallel() 457 client, mux, _ := setup(t) 458 459 mux.HandleFunc("/orgs/o/actions/variables/NAME/repositories/1234", func(w http.ResponseWriter, r *http.Request) { 460 testMethod(t, r, "PUT") 461 }) 462 463 repo := &Repository{ID: Ptr(int64(1234))} 464 ctx := context.Background() 465 _, err := client.Actions.AddSelectedRepoToOrgVariable(ctx, "o", "NAME", repo) 466 if err != nil { 467 t.Errorf("Actions.AddSelectedRepoToOrgVariable returned error: %v", err) 468 } 469 470 const methodName = "AddSelectedRepoToOrgVariable" 471 testBadOptions(t, methodName, func() (err error) { 472 _, err = client.Actions.AddSelectedRepoToOrgVariable(ctx, "\n", "\n", repo) 473 return err 474 }) 475 476 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 477 return client.Actions.AddSelectedRepoToOrgVariable(ctx, "o", "NAME", repo) 478 }) 479 } 480 481 func TestActionsService_RemoveSelectedRepoFromOrgVariable(t *testing.T) { 482 t.Parallel() 483 client, mux, _ := setup(t) 484 485 mux.HandleFunc("/orgs/o/actions/variables/NAME/repositories/1234", func(w http.ResponseWriter, r *http.Request) { 486 testMethod(t, r, "DELETE") 487 }) 488 489 repo := &Repository{ID: Ptr(int64(1234))} 490 ctx := context.Background() 491 _, err := client.Actions.RemoveSelectedRepoFromOrgVariable(ctx, "o", "NAME", repo) 492 if err != nil { 493 t.Errorf("Actions.RemoveSelectedRepoFromOrgVariable returned error: %v", err) 494 } 495 496 const methodName = "RemoveSelectedRepoFromOrgVariable" 497 testBadOptions(t, methodName, func() (err error) { 498 _, err = client.Actions.RemoveSelectedRepoFromOrgVariable(ctx, "\n", "\n", repo) 499 return err 500 }) 501 502 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 503 return client.Actions.RemoveSelectedRepoFromOrgVariable(ctx, "o", "NAME", repo) 504 }) 505 } 506 507 func TestActionsService_DeleteOrgVariable(t *testing.T) { 508 t.Parallel() 509 client, mux, _ := setup(t) 510 511 mux.HandleFunc("/orgs/o/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { 512 testMethod(t, r, "DELETE") 513 }) 514 515 ctx := context.Background() 516 _, err := client.Actions.DeleteOrgVariable(ctx, "o", "NAME") 517 if err != nil { 518 t.Errorf("Actions.DeleteOrgVariable returned error: %v", err) 519 } 520 521 const methodName = "DeleteOrgVariable" 522 testBadOptions(t, methodName, func() (err error) { 523 _, err = client.Actions.DeleteOrgVariable(ctx, "\n", "\n") 524 return err 525 }) 526 527 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 528 return client.Actions.DeleteOrgVariable(ctx, "o", "NAME") 529 }) 530 } 531 532 func TestActionsService_ListEnvVariables(t *testing.T) { 533 t.Parallel() 534 client, mux, _ := setup(t) 535 536 mux.HandleFunc("/repos/usr/1/environments/e/variables", func(w http.ResponseWriter, r *http.Request) { 537 testMethod(t, r, "GET") 538 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 539 fmt.Fprint(w, `{"total_count":4,"variables":[{"name":"A","value":"AA","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"name":"B","value":"BB","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`) 540 }) 541 542 opts := &ListOptions{Page: 2, PerPage: 2} 543 ctx := context.Background() 544 variables, _, err := client.Actions.ListEnvVariables(ctx, "usr", "1", "e", opts) 545 if err != nil { 546 t.Errorf("Actions.ListEnvVariables returned error: %v", err) 547 } 548 549 want := &ActionsVariables{ 550 TotalCount: 4, 551 Variables: []*ActionsVariable{ 552 {Name: "A", Value: "AA", 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)}}, 553 {Name: "B", Value: "BB", 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)}}, 554 }, 555 } 556 if !cmp.Equal(variables, want) { 557 t.Errorf("Actions.ListEnvVariables returned %+v, want %+v", variables, want) 558 } 559 560 const methodName = "ListEnvVariables" 561 testBadOptions(t, methodName, func() (err error) { 562 _, _, err = client.Actions.ListEnvVariables(ctx, "usr", "0", "\n", opts) 563 return err 564 }) 565 566 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 567 got, resp, err := client.Actions.ListEnvVariables(ctx, "usr", "1", "e", opts) 568 if got != nil { 569 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 570 } 571 return resp, err 572 }) 573 } 574 575 func TestActionsService_GetEnvVariable(t *testing.T) { 576 t.Parallel() 577 client, mux, _ := setup(t) 578 579 mux.HandleFunc("/repos/usr/1/environments/e/variables/variable", func(w http.ResponseWriter, r *http.Request) { 580 testMethod(t, r, "GET") 581 fmt.Fprint(w, `{"name":"variable","value":"VAR","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`) 582 }) 583 584 ctx := context.Background() 585 variable, _, err := client.Actions.GetEnvVariable(ctx, "usr", "1", "e", "variable") 586 if err != nil { 587 t.Errorf("Actions.GetEnvVariable returned error: %v", err) 588 } 589 590 want := &ActionsVariable{ 591 Name: "variable", 592 Value: "VAR", 593 CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, 594 UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, 595 } 596 if !cmp.Equal(variable, want) { 597 t.Errorf("Actions.GetEnvVariable returned %+v, want %+v", variable, want) 598 } 599 600 const methodName = "GetEnvVariable" 601 testBadOptions(t, methodName, func() (err error) { 602 _, _, err = client.Actions.GetEnvVariable(ctx, "usr", "0", "\n", "\n") 603 return err 604 }) 605 606 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 607 got, resp, err := client.Actions.GetEnvVariable(ctx, "usr", "1", "e", "variable") 608 if got != nil { 609 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 610 } 611 return resp, err 612 }) 613 } 614 615 func TestActionsService_CreateEnvVariable(t *testing.T) { 616 t.Parallel() 617 client, mux, _ := setup(t) 618 619 mux.HandleFunc("/repos/usr/1/environments/e/variables", func(w http.ResponseWriter, r *http.Request) { 620 testMethod(t, r, "POST") 621 testHeader(t, r, "Content-Type", "application/json") 622 testBody(t, r, `{"name":"variable","value":"VAR"}`+"\n") 623 w.WriteHeader(http.StatusCreated) 624 }) 625 626 input := &ActionsVariable{ 627 Name: "variable", 628 Value: "VAR", 629 } 630 ctx := context.Background() 631 _, err := client.Actions.CreateEnvVariable(ctx, "usr", "1", "e", input) 632 if err != nil { 633 t.Errorf("Actions.CreateEnvVariable returned error: %v", err) 634 } 635 636 const methodName = "CreateEnvVariable" 637 testBadOptions(t, methodName, func() (err error) { 638 _, err = client.Actions.CreateEnvVariable(ctx, "usr", "0", "\n", input) 639 return err 640 }) 641 642 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 643 return client.Actions.CreateEnvVariable(ctx, "usr", "1", "e", input) 644 }) 645 } 646 647 func TestActionsService_UpdateEnvVariable(t *testing.T) { 648 t.Parallel() 649 client, mux, _ := setup(t) 650 651 mux.HandleFunc("/repos/usr/1/environments/e/variables/variable", func(w http.ResponseWriter, r *http.Request) { 652 testMethod(t, r, "PATCH") 653 testHeader(t, r, "Content-Type", "application/json") 654 testBody(t, r, `{"name":"variable","value":"VAR"}`+"\n") 655 w.WriteHeader(http.StatusNoContent) 656 }) 657 658 input := &ActionsVariable{ 659 Name: "variable", 660 Value: "VAR", 661 } 662 ctx := context.Background() 663 _, err := client.Actions.UpdateEnvVariable(ctx, "usr", "1", "e", input) 664 if err != nil { 665 t.Errorf("Actions.UpdateEnvVariable returned error: %v", err) 666 } 667 668 const methodName = "UpdateEnvVariable" 669 testBadOptions(t, methodName, func() (err error) { 670 _, err = client.Actions.UpdateEnvVariable(ctx, "usr", "1", "\n", input) 671 return err 672 }) 673 674 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 675 return client.Actions.UpdateEnvVariable(ctx, "usr", "1", "e", input) 676 }) 677 } 678 679 func TestActionsService_DeleteEnvVariable(t *testing.T) { 680 t.Parallel() 681 client, mux, _ := setup(t) 682 683 mux.HandleFunc("/repos/usr/1/environments/e/variables/variable", func(w http.ResponseWriter, r *http.Request) { 684 testMethod(t, r, "DELETE") 685 }) 686 687 ctx := context.Background() 688 _, err := client.Actions.DeleteEnvVariable(ctx, "usr", "1", "e", "variable") 689 if err != nil { 690 t.Errorf("Actions.DeleteEnvVariable returned error: %v", err) 691 } 692 693 const methodName = "DeleteEnvVariable" 694 testBadOptions(t, methodName, func() (err error) { 695 _, err = client.Actions.DeleteEnvVariable(ctx, "usr", "0", "\n", "\n") 696 return err 697 }) 698 699 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 700 return client.Actions.DeleteEnvVariable(ctx, "usr", "1", "r", "variable") 701 }) 702 } 703 704 func TestActionVariable_Marshal(t *testing.T) { 705 t.Parallel() 706 testJSONMarshal(t, &ActionsVariable{}, "{}") 707 708 av := &ActionsVariable{ 709 Name: "n", 710 Value: "v", 711 CreatedAt: &Timestamp{referenceTime}, 712 UpdatedAt: &Timestamp{referenceTime}, 713 Visibility: Ptr("v"), 714 SelectedRepositoriesURL: Ptr("s"), 715 SelectedRepositoryIDs: &SelectedRepoIDs{1, 2, 3}, 716 } 717 718 want := fmt.Sprintf(`{ 719 "name": "n", 720 "value": "v", 721 "created_at": %s, 722 "updated_at": %s, 723 "visibility": "v", 724 "selected_repositories_url": "s", 725 "selected_repository_ids": [1,2,3] 726 }`, referenceTimeStr, referenceTimeStr) 727 728 fmt.Println(want) 729 730 testJSONMarshal(t, av, want) 731 }