Repository: google/go-github
Pull request #3089 (2024-03-03 10:36:09)

Allow querying rule set information by ID with information returned from GetRulesFromBranch
The purpose of this commit is to allow further querying of rulesets for a specific branch, at the organization or the repository level.
To achieve this, this commit adds the `ruleset_id` and `ruleset_source_type` fields to the RepositoryRule object according to the returned information from the github API.


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/google/go-github/pull/3089?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 92.87%. Comparing base [(`20fc901`)](https://app.codecov.io/gh/google/go-github/commit/20fc90190626bc9ff64da10ffdbb3be9f1ba85be?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) to head [(`ed74bb4`)](https://app.codecov.io/gh/google/go-github/pull/3089?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).
> Report is 23 commits behind head on master.


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #3089      +/-   ##
==========================================
- Coverage   97.71%   92.87%   -4.85%     
==========================================
  Files         152      170      +18     
  Lines       13326    11403    -1923     
==========================================
- Hits        13021    10590    -2431     
- Misses        215      723     +508     
  Partials       90       90              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/google/go-github/pull/3089?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).


Patch:
diff --git a/github/repos_rules.go b/github/repos_rules.go
index 479806c2..9f4e0245 100644
--- a/github/repos_rules.go
+++ b/github/repos_rules.go
@@ -112,8 +112,11 @@ type RequiredWorkflowsRuleParameters struct {
 
 // RepositoryRule represents a GitHub Rule.
 type RepositoryRule struct {
-	Type       string           `json:"type"`
-	Parameters *json.RawMessage `json:"parameters,omitempty"`
+	Type              string           `json:"type"`
+	Parameters        *json.RawMessage `json:"parameters,omitempty"`
+	RulesetSourceType string           `json:"ruleset_source_type"`
+	RulesetSource     string           `json:"ruleset_source"`
+	RulesetID         int64            `json:"ruleset_id"`
 }
 
 // UnmarshalJSON implements the json.Unmarshaler interface.
@@ -125,6 +128,9 @@ func (r *RepositoryRule) UnmarshalJSON(data []byte) error {
 		return err
 	}
 
+	r.RulesetID = RepositoryRule.RulesetID
+	r.RulesetSourceType = RepositoryRule.RulesetSourceType
+	r.RulesetSource = RepositoryRule.RulesetSource
 	r.Type = RepositoryRule.Type
 
 	switch RepositoryRule.Type {


Test patch:
diff --git a/github/repos_rules_test.go b/github/repos_rules_test.go
index cd8e49c2..cbc3e5ce 100644
--- a/github/repos_rules_test.go
+++ b/github/repos_rules_test.go
@@ -28,6 +28,20 @@ func TestRepositoryRule_UnmarshalJSON(t *testing.T) {
 			},
 			wantErr: true,
 		},
+		"With Metadata": {
+			data: `{
+                    "type": "creation",
+					"ruleset_source_type": "Repository",
+					"ruleset_source": "google",
+					"ruleset_id": 1984
+           		   }`,
+			want: &RepositoryRule{
+				RulesetSource:     "google",
+				RulesetSourceType: "Repository",
+				RulesetID:         1984,
+				Type:              "creation",
+			},
+		},
 		"Valid creation": {
 			data: `{"type":"creation"}`,
 			want: NewCreationRule(),
@@ -262,9 +276,15 @@ func TestRepositoriesService_GetRulesForBranch(t *testing.T) {
 		testMethod(t, r, "GET")
 		fmt.Fprint(w, `[
 			{
+			  "ruleset_id": 42069,
+			  "ruleset_source_type": "Repository",
+			  "ruleset_source": "google",
 			  "type": "creation"
 			},
 			{
+			  "ruleset_id": 42069,
+			  "ruleset_source_type": "Organization",
+			  "ruleset_source": "google",
 			  "type": "update",
 			  "parameters": {
 			    "update_allows_fetch_and_merge": true
@@ -280,9 +300,15 @@ func TestRepositoriesService_GetRulesForBranch(t *testing.T) {
 	}
 
 	creationRule := NewCreationRule()
+	creationRule.RulesetID = 42069
+	creationRule.RulesetSource = "google"
+	creationRule.RulesetSourceType = "Repository"
 	updateRule := NewUpdateRule(&UpdateAllowsFetchAndMergeRuleParameters{
 		UpdateAllowsFetchAndMerge: true,
 	})
+	updateRule.RulesetID = 42069
+	updateRule.RulesetSource = "google"
+	updateRule.RulesetSourceType = "Organization"
 
 	want := []*RepositoryRule{
 		creationRule,