Skip to content

Commit 2204dce

Browse files
Allow an empty array of BypassActors in Ruleset struct in CreateRuleset endpoint (#3174)
Fixes: #3137.
1 parent 63de08b commit 2204dce

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

github/repos_rules.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,24 @@ type Ruleset struct {
408408
Rules []*RepositoryRule `json:"rules,omitempty"`
409409
}
410410

411+
// rulesetNoOmitBypassActors represents a GitHub ruleset object. The struct does not omit bypassActors if the field is nil or an empty array is passed.
412+
type rulesetNoOmitBypassActors struct {
413+
ID *int64 `json:"id,omitempty"`
414+
Name string `json:"name"`
415+
// Possible values for Target are branch, tag
416+
Target *string `json:"target,omitempty"`
417+
// Possible values for SourceType are: Repository, Organization
418+
SourceType *string `json:"source_type,omitempty"`
419+
Source string `json:"source"`
420+
// Possible values for Enforcement are: disabled, active, evaluate
421+
Enforcement string `json:"enforcement"`
422+
BypassActors []*BypassActor `json:"bypass_actors"`
423+
NodeID *string `json:"node_id,omitempty"`
424+
Links *RulesetLinks `json:"_links,omitempty"`
425+
Conditions *RulesetConditions `json:"conditions,omitempty"`
426+
Rules []*RepositoryRule `json:"rules,omitempty"`
427+
}
428+
411429
// GetRulesForBranch gets all the rules that apply to the specified branch.
412430
//
413431
// GitHub API docs: https://docs.github.com/rest/repos/rules#get-rules-for-a-branch
@@ -520,6 +538,48 @@ func (s *RepositoriesService) UpdateRuleset(ctx context.Context, owner, repo str
520538
return ruleset, resp, nil
521539
}
522540

541+
// UpdateRulesetNoBypassActor updates a ruleset for the specified repository.
542+
//
543+
// This function is necessary as the UpdateRuleset function does not marshal ByPassActor if passed as nil or an empty array.
544+
//
545+
// GitHub API docs: https://docs.github.com/rest/repos/rules#update-a-repository-ruleset
546+
//
547+
//meta:operation PUT /repos/{owner}/{repo}/rulesets/{ruleset_id}
548+
func (s *RepositoriesService) UpdateRulesetNoBypassActor(ctx context.Context, owner, repo string, rulesetID int64, rs *Ruleset) (*Ruleset, *Response, error) {
549+
u := fmt.Sprintf("repos/%v/%v/rulesets/%v", owner, repo, rulesetID)
550+
551+
rsNoBypassActor := &rulesetNoOmitBypassActors{}
552+
553+
if rs != nil {
554+
rsNoBypassActor = &rulesetNoOmitBypassActors{
555+
ID: rs.ID,
556+
Name: rs.Name,
557+
Target: rs.Target,
558+
SourceType: rs.SourceType,
559+
Source: rs.Source,
560+
Enforcement: rs.Enforcement,
561+
BypassActors: rs.BypassActors,
562+
NodeID: rs.NodeID,
563+
Links: rs.Links,
564+
Conditions: rs.Conditions,
565+
Rules: rs.Rules,
566+
}
567+
}
568+
569+
req, err := s.client.NewRequest("PUT", u, rsNoBypassActor)
570+
if err != nil {
571+
return nil, nil, err
572+
}
573+
574+
var ruleSet *Ruleset
575+
resp, err := s.client.Do(ctx, req, &ruleSet)
576+
if err != nil {
577+
return nil, resp, err
578+
}
579+
580+
return ruleSet, resp, nil
581+
}
582+
523583
// DeleteRuleset deletes a ruleset for the specified repository.
524584
//
525585
// GitHub API docs: https://docs.github.com/rest/repos/rules#delete-a-repository-ruleset

github/repos_rules_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,58 @@ func TestRepositoriesService_GetRuleset(t *testing.T) {
525525
})
526526
}
527527

528+
func TestRepositoriesService_UpdateRulesetNoBypassActor(t *testing.T) {
529+
client, mux, _, teardown := setup()
530+
defer teardown()
531+
532+
rs := &Ruleset{
533+
Name: "ruleset",
534+
Source: "o/repo",
535+
Enforcement: "enabled",
536+
}
537+
538+
mux.HandleFunc("/repos/o/repo/rulesets/42", func(w http.ResponseWriter, r *http.Request) {
539+
testMethod(t, r, "PUT")
540+
fmt.Fprint(w, `{
541+
"id": 42,
542+
"name": "ruleset",
543+
"source_type": "Repository",
544+
"source": "o/repo",
545+
"enforcement": "enabled"
546+
}`)
547+
})
548+
549+
ctx := context.Background()
550+
551+
ruleSet, _, err := client.Repositories.UpdateRulesetNoBypassActor(ctx, "o", "repo", 42, rs)
552+
553+
if err != nil {
554+
t.Errorf("Repositories.UpdateRulesetNoBypassActor returned error: %v \n", err)
555+
}
556+
557+
want := &Ruleset{
558+
ID: Int64(42),
559+
Name: "ruleset",
560+
SourceType: String("Repository"),
561+
Source: "o/repo",
562+
Enforcement: "enabled",
563+
}
564+
565+
if !cmp.Equal(ruleSet, want) {
566+
t.Errorf("Repositories.UpdateRulesetNoBypassActor returned %+v, want %+v", ruleSet, want)
567+
}
568+
569+
const methodName = "UpdateRulesetNoBypassActor"
570+
571+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
572+
got, resp, err := client.Repositories.UpdateRulesetNoBypassActor(ctx, "o", "repo", 42, nil)
573+
if got != nil {
574+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
575+
}
576+
return resp, err
577+
})
578+
}
579+
528580
func TestRepositoriesService_UpdateRuleset(t *testing.T) {
529581
client, mux, _, teardown := setup()
530582
defer teardown()
@@ -556,6 +608,7 @@ func TestRepositoriesService_UpdateRuleset(t *testing.T) {
556608
Source: "o/repo",
557609
Enforcement: "enabled",
558610
}
611+
559612
if !cmp.Equal(ruleSet, want) {
560613
t.Errorf("Repositories.UpdateRuleset returned %+v, want %+v", ruleSet, want)
561614
}

0 commit comments

Comments
 (0)