Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow an empty array of BypassActors in Ruleset struct in CreateRuleset endpoint #3174

Merged
merged 21 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2a56435
work done on merge queue addition
Matthew-Reidy Apr 17, 2024
ff15302
added support for merge queue, altered tests to include merge queue
Matthew-Reidy Apr 18, 2024
aabde61
Merge branch 'google:master' into master
Matthew-Reidy Apr 18, 2024
ae78db8
alphabetized items in repo_rules.go and AUTHORS, removed blank space …
Matthew-Reidy Apr 18, 2024
db98743
Update github/repos_rules.go
Matthew-Reidy Apr 19, 2024
905ca1c
Merge branch 'google:master' into master
Matthew-Reidy Apr 19, 2024
0fd71e1
Merge branch 'google:master' into master
Matthew-Reidy Apr 26, 2024
2c8a998
issue3106 changes
Matthew-Reidy Apr 26, 2024
13c74db
added more detail to function comment
Matthew-Reidy Apr 29, 2024
6f4c7c1
small gramatical change
Matthew-Reidy Apr 29, 2024
f43553d
Merge branch 'google:master' into master
Matthew-Reidy May 1, 2024
cadcc02
Merge branch 'google:master' into master
Matthew-Reidy May 22, 2024
133421c
added extra endpoint for issue 3137
Matthew-Reidy May 22, 2024
33bfd28
changed func param back to Ruleset
Matthew-Reidy May 30, 2024
0feea60
Update github/repos_rules.go
Matthew-Reidy May 30, 2024
aef7538
Update github/repos_rules.go
Matthew-Reidy May 30, 2024
e34b7b3
Update github/repos_rules.go
Matthew-Reidy May 30, 2024
0fd05d7
Update github/repos_rules.go
Matthew-Reidy May 30, 2024
2b20337
Merge branch 'google:master' into master
Matthew-Reidy May 30, 2024
1f7c1ef
changed struct to non-exported, code styling change in UpdateRulesetN…
Matthew-Reidy May 30, 2024
fa5bba3
fixed spelling nit
Matthew-Reidy Jul 9, 2024
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
60 changes: 60 additions & 0 deletions github/repos_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you can embed the entire Ruleset object and shadow just the bypass_actors

type rulesetNoOmitBypassActors struct {
   Ruleset
   BypassActors []*BypassActor `json:"bypass_actors"`
}

example: https://goplay.tools/snippet/7lHZci7LW0y

@gmlewis WDYT?

Then you can use it like so:

func (s *RepositoriesService) UpdateRulesetNoBypassActor(...rs *Ruleset...
  rsNoBypassActor := &rulesetNoOmitBypassActors{}
   if rs != nil {
      rsNoBypassActor := &rulesetNoOmitBypassActors{Ruleset: *rs}
   }

Copy link
Collaborator

@gmlewis gmlewis Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds great, @tomfeigin - as long as we have a unit test that demonstrates this behavior (meaning a BypassActors=nil still gets passed through in JSON as "bypass_actors":[] ).

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 @@ -507,6 +525,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 marschall ByPassActor if passed as nil or an empty array.
Matthew-Reidy marked this conversation as resolved.
Show resolved Hide resolved
//
// 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
Loading