Skip to content

Commit

Permalink
Check for nil pointer in update rule parameters (#2854)
Browse files Browse the repository at this point in the history
  • Loading branch information
o-sama authored Aug 3, 2023
1 parent f897b2c commit a8da03b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
18 changes: 14 additions & 4 deletions github/repos_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func (r *RepositoryRule) UnmarshalJSON(data []byte) error {
case "creation", "deletion", "required_linear_history", "required_signatures", "non_fast_forward":
r.Parameters = nil
case "update":
if RepositoryRule.Parameters == nil {
r.Parameters = nil
return nil
}
params := UpdateAllowsFetchAndMergeRuleParameters{}
if err := json.Unmarshal(*RepositoryRule.Parameters, &params); err != nil {
return err
Expand All @@ -127,6 +131,7 @@ func (r *RepositoryRule) UnmarshalJSON(data []byte) error {
rawParams := json.RawMessage(bytes)

r.Parameters = &rawParams

case "required_deployments":
params := RequiredDeploymentEnvironmentsRuleParameters{}
if err := json.Unmarshal(*RepositoryRule.Parameters, &params); err != nil {
Expand Down Expand Up @@ -185,13 +190,18 @@ func NewCreationRule() (rule *RepositoryRule) {

// NewUpdateRule creates a rule to only allow users with bypass permission to update matching refs.
func NewUpdateRule(params *UpdateAllowsFetchAndMergeRuleParameters) (rule *RepositoryRule) {
bytes, _ := json.Marshal(params)
if params != nil {
bytes, _ := json.Marshal(params)

rawParams := json.RawMessage(bytes)
rawParams := json.RawMessage(bytes)

return &RepositoryRule{
Type: "update",
Parameters: &rawParams,
}
}
return &RepositoryRule{
Type: "update",
Parameters: &rawParams,
Type: "update",
}
}

Expand Down
39 changes: 39 additions & 0 deletions github/repos_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,45 @@ func TestRepositoriesService_GetRulesForBranch(t *testing.T) {
})
}

func TestRepositoriesService_GetRulesForBranchEmptyUpdateRule(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/repo/rules/branches/branch", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `[
{
"type": "update"
}
]`)
})

ctx := context.Background()
rules, _, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch")
if err != nil {
t.Errorf("Repositories.GetRulesForBranch returned error: %v", err)
}

updateRule := NewUpdateRule(nil)

want := []*RepositoryRule{
updateRule,
}
if !cmp.Equal(rules, want) {
t.Errorf("Repositories.GetRulesForBranch returned %+v, want %+v", Stringify(rules), Stringify(want))
}

const methodName = "GetRulesForBranch"

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.GetRulesForBranch(ctx, "o", "repo", "branch")
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestRepositoriesService_GetAllRulesets(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down

0 comments on commit a8da03b

Please sign in to comment.