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} diff --git a/github/repos_rules.go b/github/repos_rules.go index 2827f85af1c..17fa170cf99 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,18 @@ 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, + } +} + // 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",