Skip to content

Commit

Permalink
Allow querying rule set information by ID with information returned f…
Browse files Browse the repository at this point in the history
…rom GetRulesFromBranch (#3089)
  • Loading branch information
tomfeigin authored Mar 3, 2024
1 parent a25989b commit 9bb6bf4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
10 changes: 8 additions & 2 deletions github/repos_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions github/repos_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 9bb6bf4

Please sign in to comment.