Skip to content

Commit d88d529

Browse files
authored
fix: Add validation for nil inputs in various services (#3636)
1 parent 5b41764 commit d88d529

28 files changed

+326
-0
lines changed

github/actions_hosted_runners.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ type HostedRunnerRequest struct {
107107
// If any of these conditions are violated, an appropriate error message is returned.
108108
// Otherwise, nil is returned, indicating the request is valid.
109109
func validateCreateHostedRunnerRequest(request *HostedRunnerRequest) error {
110+
if request == nil {
111+
return errors.New("request is required for creating a hosted runner")
112+
}
110113
if request.Size == "" {
111114
return errors.New("size is required for creating a hosted runner")
112115
}

github/actions_hosted_runners_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ func TestActionsService_CreateHostedRunner(t *testing.T) {
249249
request *HostedRunnerRequest
250250
expectedError string
251251
}{
252+
{
253+
name: "Missing Request",
254+
request: nil,
255+
expectedError: "validation failed: request is required for creating a hosted runner",
256+
},
252257
{
253258
name: "Missing Size",
254259
request: &HostedRunnerRequest{

github/actions_secrets.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package github
88
import (
99
"context"
1010
"encoding/json"
11+
"errors"
1112
"fmt"
1213
"strconv"
1314
)
@@ -246,6 +247,10 @@ func (s *ActionsService) putSecret(ctx context.Context, url string, eSecret *Enc
246247
//
247248
//meta:operation PUT /repos/{owner}/{repo}/actions/secrets/{secret_name}
248249
func (s *ActionsService) CreateOrUpdateRepoSecret(ctx context.Context, owner, repo string, eSecret *EncryptedSecret) (*Response, error) {
250+
if eSecret == nil {
251+
return nil, errors.New("encrypted secret must be provided")
252+
}
253+
249254
url := fmt.Sprintf("repos/%v/%v/actions/secrets/%v", owner, repo, eSecret.Name)
250255
return s.putSecret(ctx, url, eSecret)
251256
}
@@ -256,6 +261,10 @@ func (s *ActionsService) CreateOrUpdateRepoSecret(ctx context.Context, owner, re
256261
//
257262
//meta:operation PUT /orgs/{org}/actions/secrets/{secret_name}
258263
func (s *ActionsService) CreateOrUpdateOrgSecret(ctx context.Context, org string, eSecret *EncryptedSecret) (*Response, error) {
264+
if eSecret == nil {
265+
return nil, errors.New("encrypted secret must be provided")
266+
}
267+
259268
url := fmt.Sprintf("orgs/%v/actions/secrets/%v", org, eSecret.Name)
260269
return s.putSecret(ctx, url, eSecret)
261270
}
@@ -266,6 +275,10 @@ func (s *ActionsService) CreateOrUpdateOrgSecret(ctx context.Context, org string
266275
//
267276
//meta:operation PUT /repositories/{repository_id}/environments/{environment_name}/secrets/{secret_name}
268277
func (s *ActionsService) CreateOrUpdateEnvSecret(ctx context.Context, repoID int, env string, eSecret *EncryptedSecret) (*Response, error) {
278+
if eSecret == nil {
279+
return nil, errors.New("encrypted secret must be provided")
280+
}
281+
269282
url := fmt.Sprintf("repositories/%v/environments/%v/secrets/%v", repoID, env, eSecret.Name)
270283
return s.putSecret(ctx, url, eSecret)
271284
}
@@ -383,6 +396,10 @@ func (s *ActionsService) addSelectedRepoToSecret(ctx context.Context, url string
383396
//
384397
//meta:operation PUT /orgs/{org}/actions/secrets/{secret_name}/repositories/{repository_id}
385398
func (s *ActionsService) AddSelectedRepoToOrgSecret(ctx context.Context, org, name string, repo *Repository) (*Response, error) {
399+
if repo == nil {
400+
return nil, errors.New("repository must be provided")
401+
}
402+
386403
url := fmt.Sprintf("orgs/%v/actions/secrets/%v/repositories/%v", org, name, *repo.ID)
387404
return s.addSelectedRepoToSecret(ctx, url)
388405
}
@@ -402,6 +419,10 @@ func (s *ActionsService) removeSelectedRepoFromSecret(ctx context.Context, url s
402419
//
403420
//meta:operation DELETE /orgs/{org}/actions/secrets/{secret_name}/repositories/{repository_id}
404421
func (s *ActionsService) RemoveSelectedRepoFromOrgSecret(ctx context.Context, org, name string, repo *Repository) (*Response, error) {
422+
if repo == nil {
423+
return nil, errors.New("repository must be provided")
424+
}
425+
405426
url := fmt.Sprintf("orgs/%v/actions/secrets/%v/repositories/%v", org, name, *repo.ID)
406427
return s.removeSelectedRepoFromSecret(ctx, url)
407428
}

github/actions_secrets_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,10 @@ func TestActionsService_CreateOrUpdateRepoSecret(t *testing.T) {
311311
}
312312

313313
const methodName = "CreateOrUpdateRepoSecret"
314+
testBadOptions(t, methodName, func() (err error) {
315+
_, err = client.Actions.CreateOrUpdateRepoSecret(ctx, "o", "r", nil)
316+
return err
317+
})
314318
testBadOptions(t, methodName, func() (err error) {
315319
_, err = client.Actions.CreateOrUpdateRepoSecret(ctx, "\n", "\n", input)
316320
return err
@@ -491,6 +495,10 @@ func TestActionsService_CreateOrUpdateOrgSecret(t *testing.T) {
491495
}
492496

493497
const methodName = "CreateOrUpdateOrgSecret"
498+
testBadOptions(t, methodName, func() (err error) {
499+
_, err = client.Actions.CreateOrUpdateOrgSecret(ctx, "o", nil)
500+
return err
501+
})
494502
testBadOptions(t, methodName, func() (err error) {
495503
_, err = client.Actions.CreateOrUpdateOrgSecret(ctx, "\n", input)
496504
return err
@@ -585,6 +593,10 @@ func TestActionsService_AddSelectedRepoToOrgSecret(t *testing.T) {
585593
}
586594

587595
const methodName = "AddSelectedRepoToOrgSecret"
596+
testBadOptions(t, methodName, func() (err error) {
597+
_, err = client.Actions.AddSelectedRepoToOrgSecret(ctx, "o", "NAME", nil)
598+
return err
599+
})
588600
testBadOptions(t, methodName, func() (err error) {
589601
_, err = client.Actions.AddSelectedRepoToOrgSecret(ctx, "\n", "\n", repo)
590602
return err
@@ -611,6 +623,10 @@ func TestActionsService_RemoveSelectedRepoFromOrgSecret(t *testing.T) {
611623
}
612624

613625
const methodName = "RemoveSelectedRepoFromOrgSecret"
626+
testBadOptions(t, methodName, func() (err error) {
627+
_, err = client.Actions.RemoveSelectedRepoFromOrgSecret(ctx, "o", "NAME", nil)
628+
return err
629+
})
614630
testBadOptions(t, methodName, func() (err error) {
615631
_, err = client.Actions.RemoveSelectedRepoFromOrgSecret(ctx, "\n", "\n", repo)
616632
return err
@@ -821,6 +837,10 @@ func TestActionsService_CreateOrUpdateEnvSecret(t *testing.T) {
821837
}
822838

823839
const methodName = "CreateOrUpdateEnvSecret"
840+
testBadOptions(t, methodName, func() (err error) {
841+
_, err = client.Actions.CreateOrUpdateEnvSecret(ctx, 1, "e", nil)
842+
return err
843+
})
824844
testBadOptions(t, methodName, func() (err error) {
825845
_, err = client.Actions.CreateOrUpdateEnvSecret(ctx, 0.0, "\n", input)
826846
return err

github/actions_variables.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package github
77

88
import (
99
"context"
10+
"errors"
1011
"fmt"
1112
)
1213

@@ -186,6 +187,10 @@ func (s *ActionsService) patchVariable(ctx context.Context, url string, variable
186187
//
187188
//meta:operation PATCH /repos/{owner}/{repo}/actions/variables/{name}
188189
func (s *ActionsService) UpdateRepoVariable(ctx context.Context, owner, repo string, variable *ActionsVariable) (*Response, error) {
190+
if variable == nil {
191+
return nil, errors.New("variable must be provided")
192+
}
193+
189194
url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, variable.Name)
190195
return s.patchVariable(ctx, url, variable)
191196
}
@@ -196,6 +201,10 @@ func (s *ActionsService) UpdateRepoVariable(ctx context.Context, owner, repo str
196201
//
197202
//meta:operation PATCH /orgs/{org}/actions/variables/{name}
198203
func (s *ActionsService) UpdateOrgVariable(ctx context.Context, org string, variable *ActionsVariable) (*Response, error) {
204+
if variable == nil {
205+
return nil, errors.New("variable must be provided")
206+
}
207+
199208
url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, variable.Name)
200209
return s.patchVariable(ctx, url, variable)
201210
}
@@ -206,6 +215,10 @@ func (s *ActionsService) UpdateOrgVariable(ctx context.Context, org string, vari
206215
//
207216
//meta:operation PATCH /repos/{owner}/{repo}/environments/{environment_name}/variables/{name}
208217
func (s *ActionsService) UpdateEnvVariable(ctx context.Context, owner, repo, env string, variable *ActionsVariable) (*Response, error) {
218+
if variable == nil {
219+
return nil, errors.New("variable must be provided")
220+
}
221+
209222
url := fmt.Sprintf("repos/%v/%v/environments/%v/variables/%v", owner, repo, env, variable.Name)
210223
return s.patchVariable(ctx, url, variable)
211224
}
@@ -317,6 +330,13 @@ func (s *ActionsService) addSelectedRepoToVariable(ctx context.Context, url stri
317330
//
318331
//meta:operation PUT /orgs/{org}/actions/variables/{name}/repositories/{repository_id}
319332
func (s *ActionsService) AddSelectedRepoToOrgVariable(ctx context.Context, org, name string, repo *Repository) (*Response, error) {
333+
if repo == nil {
334+
return nil, errors.New("repository must be provided")
335+
}
336+
if repo.ID == nil {
337+
return nil, errors.New("id must be provided")
338+
}
339+
320340
url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories/%v", org, name, *repo.ID)
321341
return s.addSelectedRepoToVariable(ctx, url)
322342
}
@@ -336,6 +356,13 @@ func (s *ActionsService) removeSelectedRepoFromVariable(ctx context.Context, url
336356
//
337357
//meta:operation DELETE /orgs/{org}/actions/variables/{name}/repositories/{repository_id}
338358
func (s *ActionsService) RemoveSelectedRepoFromOrgVariable(ctx context.Context, org, name string, repo *Repository) (*Response, error) {
359+
if repo == nil {
360+
return nil, errors.New("repository must be provided")
361+
}
362+
if repo.ID == nil {
363+
return nil, errors.New("id must be provided")
364+
}
365+
339366
url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories/%v", org, name, *repo.ID)
340367
return s.removeSelectedRepoFromVariable(ctx, url)
341368
}

github/actions_variables_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ func TestActionsService_UpdateRepoVariable(t *testing.T) {
195195
}
196196

197197
const methodName = "UpdateRepoVariable"
198+
testBadOptions(t, methodName, func() (err error) {
199+
_, err = client.Actions.UpdateRepoVariable(ctx, "o", "r", nil)
200+
return err
201+
})
198202
testBadOptions(t, methodName, func() (err error) {
199203
_, err = client.Actions.UpdateRepoVariable(ctx, "\n", "\n", input)
200204
return err
@@ -374,6 +378,10 @@ func TestActionsService_UpdateOrgVariable(t *testing.T) {
374378
}
375379

376380
const methodName = "UpdateOrgVariable"
381+
testBadOptions(t, methodName, func() (err error) {
382+
_, err = client.Actions.UpdateOrgVariable(ctx, "o", nil)
383+
return err
384+
})
377385
testBadOptions(t, methodName, func() (err error) {
378386
_, err = client.Actions.UpdateOrgVariable(ctx, "\n", input)
379387
return err
@@ -468,6 +476,14 @@ func TestActionsService_AddSelectedRepoToOrgVariable(t *testing.T) {
468476
}
469477

470478
const methodName = "AddSelectedRepoToOrgVariable"
479+
testBadOptions(t, methodName, func() (err error) {
480+
_, err = client.Actions.AddSelectedRepoToOrgVariable(ctx, "o", "NAME", nil)
481+
return err
482+
})
483+
testBadOptions(t, methodName, func() (err error) {
484+
_, err = client.Actions.AddSelectedRepoToOrgVariable(ctx, "o", "NAME", &Repository{ID: nil})
485+
return err
486+
})
471487
testBadOptions(t, methodName, func() (err error) {
472488
_, err = client.Actions.AddSelectedRepoToOrgVariable(ctx, "\n", "\n", repo)
473489
return err
@@ -494,6 +510,14 @@ func TestActionsService_RemoveSelectedRepoFromOrgVariable(t *testing.T) {
494510
}
495511

496512
const methodName = "RemoveSelectedRepoFromOrgVariable"
513+
testBadOptions(t, methodName, func() (err error) {
514+
_, err = client.Actions.RemoveSelectedRepoFromOrgVariable(ctx, "o", "NAME", nil)
515+
return err
516+
})
517+
testBadOptions(t, methodName, func() (err error) {
518+
_, err = client.Actions.RemoveSelectedRepoFromOrgVariable(ctx, "o", "NAME", &Repository{ID: nil})
519+
return err
520+
})
497521
testBadOptions(t, methodName, func() (err error) {
498522
_, err = client.Actions.RemoveSelectedRepoFromOrgVariable(ctx, "\n", "\n", repo)
499523
return err
@@ -666,6 +690,10 @@ func TestActionsService_UpdateEnvVariable(t *testing.T) {
666690
}
667691

668692
const methodName = "UpdateEnvVariable"
693+
testBadOptions(t, methodName, func() (err error) {
694+
_, err = client.Actions.UpdateEnvVariable(ctx, "usr", "1", "e", nil)
695+
return err
696+
})
669697
testBadOptions(t, methodName, func() (err error) {
670698
_, err = client.Actions.UpdateEnvVariable(ctx, "usr", "1", "\n", input)
671699
return err

github/admin_orgs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package github
77

88
import (
99
"context"
10+
"errors"
1011
"fmt"
1112
)
1213

@@ -65,6 +66,13 @@ type RenameOrgResponse struct {
6566
//
6667
//meta:operation PATCH /admin/organizations/{org}
6768
func (s *AdminService) RenameOrg(ctx context.Context, org *Organization, newName string) (*RenameOrgResponse, *Response, error) {
69+
if org == nil {
70+
return nil, nil, errors.New("organization must be provided")
71+
}
72+
if org.Login == nil {
73+
return nil, nil, errors.New("login must be provided")
74+
}
75+
6876
return s.RenameOrgByName(ctx, *org.Login, newName)
6977
}
7078

github/admin_orgs_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ func TestAdminOrgs_Rename(t *testing.T) {
9090
}
9191

9292
const methodName = "RenameOrg"
93+
testBadOptions(t, methodName, func() (err error) {
94+
_, _, err = client.Admin.RenameOrg(ctx, nil, "the-new-octocats")
95+
return err
96+
})
97+
testBadOptions(t, methodName, func() (err error) {
98+
_, _, err = client.Admin.RenameOrg(ctx, &Organization{Login: nil}, "the-new-octocats")
99+
return err
100+
})
101+
93102
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
94103
got, resp, err := client.Admin.RenameOrg(ctx, input, "the-new-octocats")
95104
if got != nil {

0 commit comments

Comments
 (0)