Skip to content

Commit

Permalink
Add support for filepath repository rules (google#3233)
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamhar authored Aug 19, 2024
1 parent 3085a9c commit 3f44592
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
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

0 comments on commit 3f44592

Please sign in to comment.