Repository: google/go-github
Pull request #2831 (2023-07-15 04:09:12)

Fix repo rules bypass settings
This PR reconciles library with GitHub API for the following regarding [repo rulesets](https://docs.github.com/en/rest/repos/rules?apiVersion=2022-11-28#create-a-repository-ruleset):
- `BypassMode` should be part of `BypassActor`.
- `ActorType` possible values documentation updated to match API docs.
- `BypassMode possible values documentation updated to match API docs.
- `Ruleset` ID field changed to `Int64` pointer and added `omitempty` to allow for nil, ruleset ID is unknown prior to creation and is not required when sending a POST request to create ruleset. 

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/google/go-github/pull/2831?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
> Merging [#2831](https://app.codecov.io/gh/google/go-github/pull/2831?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (889737e) into [master](https://app.codecov.io/gh/google/go-github/commit/cd438623e5e65bad6f444d55956d5959cf98b58a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (cd43862) will **not change** coverage.
> The diff coverage is `n/a`.

```diff
@@           Coverage Diff           @@
##           master    #2831   +/-   ##
=======================================
  Coverage   98.06%   98.06%           
=======================================
  Files         137      137           
  Lines       12287    12287           
=======================================
  Hits        12049    12049           
  Misses        162      162           
  Partials       76       76           
```


| [Impacted Files](https://app.codecov.io/gh/google/go-github/pull/2831?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) | Coverage Δ | |
|---|---|---|
| [github/repos\_rules.go](https://app.codecov.io/gh/google/go-github/pull/2831?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google#diff-Z2l0aHViL3JlcG9zX3J1bGVzLmdv) | `100.00% <ø> (ø)` | |


liaodaniel: > Thank you @o-sama ! LGTM.
> 
> @liaodaniel - could you please look at these breaking API changes since you are the original author of this section of code and make sure you are happy with them?

Yep, this looks good from my end. Part of me felt that I probably should have waited a bit until the feature was out of beta before introducing it but the feature seemed too compelling to wait 😄 
gmlewis: Thank you, @liaodaniel !
Merging.

Patch:
diff --git a/github/github-accessors.go b/github/github-accessors.go
index 009b1beb..1be9493a 100644
--- a/github/github-accessors.go
+++ b/github/github-accessors.go
@@ -2238,6 +2238,14 @@ func (b *BypassActor) GetActorType() string {
 	return *b.ActorType
 }
 
+// GetBypassMode returns the BypassMode field if it's non-nil, zero value otherwise.
+func (b *BypassActor) GetBypassMode() string {
+	if b == nil || b.BypassMode == nil {
+		return ""
+	}
+	return *b.BypassMode
+}
+
 // GetApp returns the App field.
 func (c *CheckRun) GetApp() *App {
 	if c == nil {
@@ -19046,14 +19054,6 @@ func (r *RuleRequiredStatusChecks) GetIntegrationID() int64 {
 	return *r.IntegrationID
 }
 
-// GetBypassMode returns the BypassMode field if it's non-nil, zero value otherwise.
-func (r *Ruleset) GetBypassMode() string {
-	if r == nil || r.BypassMode == nil {
-		return ""
-	}
-	return *r.BypassMode
-}
-
 // GetConditions returns the Conditions field.
 func (r *Ruleset) GetConditions() *RulesetConditions {
 	if r == nil {
@@ -19062,6 +19062,14 @@ func (r *Ruleset) GetConditions() *RulesetConditions {
 	return r.Conditions
 }
 
+// GetID returns the ID field if it's non-nil, zero value otherwise.
+func (r *Ruleset) GetID() int64 {
+	if r == nil || r.ID == nil {
+		return 0
+	}
+	return *r.ID
+}
+
 // GetLinks returns the Links field.
 func (r *Ruleset) GetLinks() *RulesetLinks {
 	if r == nil {
diff --git a/github/repos_rules.go b/github/repos_rules.go
index ee9c82e7..4e6f5f13 100644
--- a/github/repos_rules.go
+++ b/github/repos_rules.go
@@ -14,8 +14,10 @@ import (
 // BypassActor represents the bypass actors from a ruleset.
 type BypassActor struct {
 	ActorID *int64 `json:"actor_id,omitempty"`
-	// Possible values for ActorType are: Team, Integration
+	// Possible values for ActorType are: RepositoryRole, Team, Integration, OrganizationAdmin
 	ActorType *string `json:"actor_type,omitempty"`
+	// Possible values for BypassMode are: always, pull_request
+	BypassMode *string `json:"bypass_mode,omitempty"`
 }
 
 // RulesetLink represents a single link object from GitHub ruleset request _links.
@@ -319,7 +321,7 @@ func NewTagNamePatternRule(params *RulePatternParameters) (rule *RepositoryRule)
 
 // Ruleset represents a GitHub ruleset object.
 type Ruleset struct {
-	ID   int64  `json:"id"`
+	ID   *int64 `json:"id,omitempty"`
 	Name string `json:"name"`
 	// Possible values for Target are branch, tag
 	Target *string `json:"target,omitempty"`
@@ -327,9 +329,7 @@ type Ruleset struct {
 	SourceType *string `json:"source_type,omitempty"`
 	Source     string  `json:"source"`
 	// Possible values for Enforcement are: disabled, active, evaluate
-	Enforcement string `json:"enforcement"`
-	// Possible values for BypassMode are: none, repository, organization
-	BypassMode   *string            `json:"bypass_mode,omitempty"`
+	Enforcement  string             `json:"enforcement"`
 	BypassActors []*BypassActor     `json:"bypass_actors,omitempty"`
 	NodeID       *string            `json:"node_id,omitempty"`
 	Links        *RulesetLinks      `json:"_links,omitempty"`


Test patch:
diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go
index 4671670c..2235cc5e 100644
--- a/github/github-accessors_test.go
+++ b/github/github-accessors_test.go
@@ -2684,6 +2684,16 @@ func TestBypassActor_GetActorType(tt *testing.T) {
 	b.GetActorType()
 }
 
+func TestBypassActor_GetBypassMode(tt *testing.T) {
+	var zeroValue string
+	b := &BypassActor{BypassMode: &zeroValue}
+	b.GetBypassMode()
+	b = &BypassActor{}
+	b.GetBypassMode()
+	b = nil
+	b.GetBypassMode()
+}
+
 func TestCheckRun_GetApp(tt *testing.T) {
 	c := &CheckRun{}
 	c.GetApp()
@@ -22209,16 +22219,6 @@ func TestRuleRequiredStatusChecks_GetIntegrationID(tt *testing.T) {
 	r.GetIntegrationID()
 }
 
-func TestRuleset_GetBypassMode(tt *testing.T) {
-	var zeroValue string
-	r := &Ruleset{BypassMode: &zeroValue}
-	r.GetBypassMode()
-	r = &Ruleset{}
-	r.GetBypassMode()
-	r = nil
-	r.GetBypassMode()
-}
-
 func TestRuleset_GetConditions(tt *testing.T) {
 	r := &Ruleset{}
 	r.GetConditions()
@@ -22226,6 +22226,16 @@ func TestRuleset_GetConditions(tt *testing.T) {
 	r.GetConditions()
 }
 
+func TestRuleset_GetID(tt *testing.T) {
+	var zeroValue int64
+	r := &Ruleset{ID: &zeroValue}
+	r.GetID()
+	r = &Ruleset{}
+	r.GetID()
+	r = nil
+	r.GetID()
+}
+
 func TestRuleset_GetLinks(tt *testing.T) {
 	r := &Ruleset{}
 	r.GetLinks()
diff --git a/github/orgs_rules_test.go b/github/orgs_rules_test.go
index 48c6c5e2..50ba4e20 100644
--- a/github/orgs_rules_test.go
+++ b/github/orgs_rules_test.go
@@ -44,13 +44,12 @@ func TestOrganizationsService_GetAllOrganizationRulesets(t *testing.T) {
 	}
 
 	want := []*Ruleset{{
-		ID:          26110,
+		ID:          Int64(26110),
 		Name:        "test ruleset",
 		Target:      String("branch"),
 		SourceType:  String("Organization"),
 		Source:      "o",
 		Enforcement: "active",
-		BypassMode:  String("none"),
 		NodeID:      String("nid"),
 		Links: &RulesetLinks{
 			Self: &RulesetLink{HRef: String("https://api.github.com/orgs/o/rulesets/26110")},
@@ -210,7 +209,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T)
 
 	ctx := context.Background()
 	ruleset, _, err := client.Organizations.CreateOrganizationRuleset(ctx, "o", &Ruleset{
-		ID:          21,
+		ID:          Int64(21),
 		Name:        "ruleset",
 		Target:      String("branch"),
 		SourceType:  String("Organization"),
@@ -296,7 +295,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoNames(t *testing.T)
 	}
 
 	want := &Ruleset{
-		ID:          21,
+		ID:          Int64(21),
 		Name:        "ruleset",
 		Target:      String("branch"),
 		SourceType:  String("Organization"),
@@ -524,7 +523,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) {
 
 	ctx := context.Background()
 	ruleset, _, err := client.Organizations.CreateOrganizationRuleset(ctx, "o", &Ruleset{
-		ID:          21,
+		ID:          Int64(21),
 		Name:        "ruleset",
 		Target:      String("branch"),
 		SourceType:  String("Organization"),
@@ -608,7 +607,7 @@ func TestOrganizationsService_CreateOrganizationRuleset_RepoIDs(t *testing.T) {
 	}
 
 	want := &Ruleset{
-		ID:          21,
+		ID:          Int64(21),
 		Name:        "ruleset",
 		Target:      String("branch"),
 		SourceType:  String("Organization"),
@@ -758,13 +757,12 @@ func TestOrganizationsService_GetOrganizationRuleset(t *testing.T) {
 	}
 
 	want := &Ruleset{
-		ID:          26110,
+		ID:          Int64(26110),
 		Name:        "test ruleset",
 		Target:      String("branch"),
 		SourceType:  String("Organization"),
 		Source:      "o",
 		Enforcement: "active",
-		BypassMode:  String("none"),
 		NodeID:      String("nid"),
 		Links: &RulesetLinks{
 			Self: &RulesetLink{HRef: String("https://api.github.com/orgs/o/rulesets/26110")},
@@ -853,7 +851,6 @@ func TestOrganizationsService_UpdateOrganizationRuleset(t *testing.T) {
 		Name:        "test ruleset",
 		Target:      String("branch"),
 		Enforcement: "active",
-		BypassMode:  String("none"),
 		Conditions: &RulesetConditions{
 			RefName: &RulesetRefConditionParameters{
 				Include: []string{"refs/heads/main", "refs/heads/master"},
@@ -875,13 +872,12 @@ func TestOrganizationsService_UpdateOrganizationRuleset(t *testing.T) {
 	}
 
 	want := &Ruleset{
-		ID:          26110,
+		ID:          Int64(26110),
 		Name:        "test ruleset",
 		Target:      String("branch"),
 		SourceType:  String("Organization"),
 		Source:      "o",
 		Enforcement: "active",
-		BypassMode:  String("none"),
 		NodeID:      String("nid"),
 		Links: &RulesetLinks{
 			Self: &RulesetLink{HRef: String("https://api.github.com/orgs/o/rulesets/26110")},
diff --git a/github/repos_rules_test.go b/github/repos_rules_test.go
index 42c7a6e2..582ec892 100644
--- a/github/repos_rules_test.go
+++ b/github/repos_rules_test.go
@@ -324,14 +324,14 @@ func TestRepositoriesService_GetAllRulesets(t *testing.T) {
 
 	want := []*Ruleset{
 		{
-			ID:          42,
+			ID:          Int64(42),
 			Name:        "ruleset",
 			SourceType:  String("Repository"),
 			Source:      "o/repo",
 			Enforcement: "enabled",
 		},
 		{
-			ID:          314,
+			ID:          Int64(314),
 			Name:        "Another ruleset",
 			SourceType:  String("Repository"),
 			Source:      "o/repo",
@@ -378,7 +378,7 @@ func TestRepositoriesService_CreateRuleset(t *testing.T) {
 	}
 
 	want := &Ruleset{
-		ID:          42,
+		ID:          Int64(42),
 		Name:        "ruleset",
 		SourceType:  String("Repository"),
 		Source:      "o/repo",
@@ -421,7 +421,7 @@ func TestRepositoriesService_GetRuleset(t *testing.T) {
 	}
 
 	want := &Ruleset{
-		ID:          42,
+		ID:          Int64(42),
 		Name:        "ruleset",
 		SourceType:  String("Organization"),
 		Source:      "o",
@@ -467,7 +467,7 @@ func TestRepositoriesService_UpdateRuleset(t *testing.T) {
 	}
 
 	want := &Ruleset{
-		ID:          42,
+		ID:          Int64(42),
 		Name:        "ruleset",
 		SourceType:  String("Repository"),
 		Source:      "o/repo",