Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for filepath repository rules #3233

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions github/repos_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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, &params); err != nil {
return err
}
bytes, _ := json.Marshal(params)
rawParams := json.RawMessage(bytes)

r.Parameters = &rawParams
default:
r.Type = ""
Expand Down Expand Up @@ -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"`
Expand Down
14 changes: 14 additions & 0 deletions github/repos_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading