From ed264f2d1925a69a760195bfd295a09d74be2b46 Mon Sep 17 00:00:00 2001 From: grahamhar Date: Sun, 11 Aug 2024 17:47:55 +0100 Subject: [PATCH 1/3] Add support for filepath repository rules --- github/repos_rules.go | 28 ++++++++++++++++++++++++++++ github/repos_rules_test.go | 14 ++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/github/repos_rules.go b/github/repos_rules.go index 2827f85af1c..0a5f3a75fa5 100644 --- a/github/repos_rules.go +++ b/github/repos_rules.go @@ -79,6 +79,11 @@ type RulePatternParameters struct { Pattern string `json:"pattern"` } +// RuleFileParameters represents a list of file paths +type RuleFileParameters struct { + RestrictedFilePaths *[]string `json:"restricted_file_paths"` +} + // UpdateAllowsFetchAndMergeRuleParameters represents the update rule parameters. type UpdateAllowsFetchAndMergeRuleParameters struct { UpdateAllowsFetchAndMerge bool `json:"update_allows_fetch_and_merge"` @@ -213,6 +218,15 @@ func (r *RepositoryRule) UnmarshalJSON(data []byte) error { bytes, _ := json.Marshal(params) rawParams := json.RawMessage(bytes) + r.Parameters = &rawParams + case "file_path_restriction": + params := RuleFileParameters{} + if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { + return err + } + bytes, _ := json.Marshal(params) + rawParams := json.RawMessage(bytes) + r.Parameters = &rawParams default: r.Type = "" @@ -390,6 +404,20 @@ func NewRequiredWorkflowsRule(params *RequiredWorkflowsRuleParameters) (rule *Re } } +// NewFilePathRestrictionRule creates a rule to restrict file paths from being pushed to. +func NewFilePathRestrictionRule(params *RuleFileParameters) (rule *RepositoryRule) { + bytes, _ := json.Marshal(params) + + rawParams := json.RawMessage(bytes) + + return &RepositoryRule{ + Type: "file_path_restriction", + Parameters: &rawParams, + } +} + +// BypassActor represents a bypass actor object. + // Ruleset represents a GitHub ruleset object. type Ruleset struct { ID *int64 `json:"id,omitempty"` diff --git a/github/repos_rules_test.go b/github/repos_rules_test.go index 065d3385a3b..c338e37e335 100644 --- a/github/repos_rules_test.go +++ b/github/repos_rules_test.go @@ -182,6 +182,20 @@ func TestRepositoryRule_UnmarshalJSON(t *testing.T) { }, wantErr: true, }, + "Valid file_path_restriction params": { + data: `{"type":"file_path_restriction","parameters":{"restricted_file_paths":["/a/file"]}}`, + want: NewFilePathRestrictionRule(&RuleFileParameters{ + RestrictedFilePaths: &[]string{"/a/file"}, + }), + }, + "Invalid file_path_restriction params": { + data: `{"type":"file_path_restriction","parameters":{"restricted_file_paths":true}}`, + want: &RepositoryRule{ + Type: "file_path_restriction", + Parameters: nil, + }, + wantErr: true, + }, "Valid pull_request params": { data: `{ "type":"pull_request", From 2d08ad44cdf65ae649efeccfb126e369ef58539f Mon Sep 17 00:00:00 2001 From: grahamhar Date: Sun, 11 Aug 2024 19:48:34 +0100 Subject: [PATCH 2/3] Updates after running lint/generate --- github/github-accessors.go | 8 ++++++++ github/github-accessors_test.go | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/github/github-accessors.go b/github/github-accessors.go index 7d4a0782869..86d3b28613c 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -20846,6 +20846,14 @@ func (r *Rule) GetSeverity() string { return *r.Severity } +// GetRestrictedFilePaths returns the RestrictedFilePaths field if it's non-nil, zero value otherwise. +func (r *RuleFileParameters) GetRestrictedFilePaths() []string { + if r == nil || r.RestrictedFilePaths == nil { + return nil + } + return *r.RestrictedFilePaths +} + // GetName returns the Name field if it's non-nil, zero value otherwise. func (r *RulePatternParameters) GetName() string { if r == nil || r.Name == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index a401ecc1693..11d8a3bff42 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -24225,6 +24225,16 @@ func TestRule_GetSeverity(tt *testing.T) { r.GetSeverity() } +func TestRuleFileParameters_GetRestrictedFilePaths(tt *testing.T) { + var zeroValue []string + r := &RuleFileParameters{RestrictedFilePaths: &zeroValue} + r.GetRestrictedFilePaths() + r = &RuleFileParameters{} + r.GetRestrictedFilePaths() + r = nil + r.GetRestrictedFilePaths() +} + func TestRulePatternParameters_GetName(tt *testing.T) { var zeroValue string r := &RulePatternParameters{Name: &zeroValue} From 1918bd0d2c51393f16b86e2bdc5d2ccac43f6d52 Mon Sep 17 00:00:00 2001 From: Graham Hargreaves Date: Mon, 12 Aug 2024 14:02:25 +0100 Subject: [PATCH 3/3] Address PR comments. Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/repos_rules.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/github/repos_rules.go b/github/repos_rules.go index 0a5f3a75fa5..17fa170cf99 100644 --- a/github/repos_rules.go +++ b/github/repos_rules.go @@ -79,7 +79,7 @@ type RulePatternParameters struct { Pattern string `json:"pattern"` } -// RuleFileParameters represents a list of file paths +// RuleFileParameters represents a list of file paths. type RuleFileParameters struct { RestrictedFilePaths *[]string `json:"restricted_file_paths"` } @@ -416,8 +416,6 @@ func NewFilePathRestrictionRule(params *RuleFileParameters) (rule *RepositoryRul } } -// BypassActor represents a bypass actor object. - // Ruleset represents a GitHub ruleset object. type Ruleset struct { ID *int64 `json:"id,omitempty"`