Skip to content

Commit

Permalink
Allow an empty array of BypassActors in Ruleset struct in CreateRules…
Browse files Browse the repository at this point in the history
…et endpoint (#3174)

Fixes: #3137.
  • Loading branch information
Matthew-Reidy committed Jul 9, 2024
1 parent 63de08b commit 2204dce
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
60 changes: 60 additions & 0 deletions github/repos_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,24 @@ type Ruleset struct {
Rules []*RepositoryRule `json:"rules,omitempty"`
}

// rulesetNoOmitBypassActors represents a GitHub ruleset object. The struct does not omit bypassActors if the field is nil or an empty array is passed.
type rulesetNoOmitBypassActors struct {
ID *int64 `json:"id,omitempty"`
Name string `json:"name"`
// Possible values for Target are branch, tag
Target *string `json:"target,omitempty"`
// Possible values for SourceType are: Repository, Organization
SourceType *string `json:"source_type,omitempty"`
Source string `json:"source"`
// Possible values for Enforcement are: disabled, active, evaluate
Enforcement string `json:"enforcement"`
BypassActors []*BypassActor `json:"bypass_actors"`
NodeID *string `json:"node_id,omitempty"`
Links *RulesetLinks `json:"_links,omitempty"`
Conditions *RulesetConditions `json:"conditions,omitempty"`
Rules []*RepositoryRule `json:"rules,omitempty"`
}

// GetRulesForBranch gets all the rules that apply to the specified branch.
//
// GitHub API docs: https://docs.github.com/rest/repos/rules#get-rules-for-a-branch
Expand Down Expand Up @@ -520,6 +538,48 @@ func (s *RepositoriesService) UpdateRuleset(ctx context.Context, owner, repo str
return ruleset, resp, nil
}

// UpdateRulesetNoBypassActor updates a ruleset for the specified repository.
//
// This function is necessary as the UpdateRuleset function does not marshal ByPassActor if passed as nil or an empty array.
//
// GitHub API docs: https://docs.github.com/rest/repos/rules#update-a-repository-ruleset
//
//meta:operation PUT /repos/{owner}/{repo}/rulesets/{ruleset_id}
func (s *RepositoriesService) UpdateRulesetNoBypassActor(ctx context.Context, owner, repo string, rulesetID int64, rs *Ruleset) (*Ruleset, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/rulesets/%v", owner, repo, rulesetID)

rsNoBypassActor := &rulesetNoOmitBypassActors{}

if rs != nil {
rsNoBypassActor = &rulesetNoOmitBypassActors{
ID: rs.ID,
Name: rs.Name,
Target: rs.Target,
SourceType: rs.SourceType,
Source: rs.Source,
Enforcement: rs.Enforcement,
BypassActors: rs.BypassActors,
NodeID: rs.NodeID,
Links: rs.Links,
Conditions: rs.Conditions,
Rules: rs.Rules,
}
}

req, err := s.client.NewRequest("PUT", u, rsNoBypassActor)
if err != nil {
return nil, nil, err
}

var ruleSet *Ruleset
resp, err := s.client.Do(ctx, req, &ruleSet)
if err != nil {
return nil, resp, err
}

return ruleSet, resp, nil
}

// DeleteRuleset deletes a ruleset for the specified repository.
//
// GitHub API docs: https://docs.github.com/rest/repos/rules#delete-a-repository-ruleset
Expand Down
53 changes: 53 additions & 0 deletions github/repos_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,58 @@ func TestRepositoriesService_GetRuleset(t *testing.T) {
})
}

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

rs := &Ruleset{
Name: "ruleset",
Source: "o/repo",
Enforcement: "enabled",
}

mux.HandleFunc("/repos/o/repo/rulesets/42", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
fmt.Fprint(w, `{
"id": 42,
"name": "ruleset",
"source_type": "Repository",
"source": "o/repo",
"enforcement": "enabled"
}`)
})

ctx := context.Background()

ruleSet, _, err := client.Repositories.UpdateRulesetNoBypassActor(ctx, "o", "repo", 42, rs)

if err != nil {
t.Errorf("Repositories.UpdateRulesetNoBypassActor returned error: %v \n", err)
}

want := &Ruleset{
ID: Int64(42),
Name: "ruleset",
SourceType: String("Repository"),
Source: "o/repo",
Enforcement: "enabled",
}

if !cmp.Equal(ruleSet, want) {
t.Errorf("Repositories.UpdateRulesetNoBypassActor returned %+v, want %+v", ruleSet, want)
}

const methodName = "UpdateRulesetNoBypassActor"

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

func TestRepositoriesService_UpdateRuleset(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down Expand Up @@ -556,6 +608,7 @@ func TestRepositoriesService_UpdateRuleset(t *testing.T) {
Source: "o/repo",
Enforcement: "enabled",
}

if !cmp.Equal(ruleSet, want) {
t.Errorf("Repositories.UpdateRuleset returned %+v, want %+v", ruleSet, want)
}
Expand Down

0 comments on commit 2204dce

Please sign in to comment.