Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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