Skip to content

Commit 3f44592

Browse files
authored
Add support for filepath repository rules (#3233)
1 parent 3085a9c commit 3f44592

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

github/github-accessors.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/repos_rules.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ type RulePatternParameters struct {
7979
Pattern string `json:"pattern"`
8080
}
8181

82+
// RuleFileParameters represents a list of file paths.
83+
type RuleFileParameters struct {
84+
RestrictedFilePaths *[]string `json:"restricted_file_paths"`
85+
}
86+
8287
// UpdateAllowsFetchAndMergeRuleParameters represents the update rule parameters.
8388
type UpdateAllowsFetchAndMergeRuleParameters struct {
8489
UpdateAllowsFetchAndMerge bool `json:"update_allows_fetch_and_merge"`
@@ -213,6 +218,15 @@ func (r *RepositoryRule) UnmarshalJSON(data []byte) error {
213218
bytes, _ := json.Marshal(params)
214219
rawParams := json.RawMessage(bytes)
215220

221+
r.Parameters = &rawParams
222+
case "file_path_restriction":
223+
params := RuleFileParameters{}
224+
if err := json.Unmarshal(*RepositoryRule.Parameters, &params); err != nil {
225+
return err
226+
}
227+
bytes, _ := json.Marshal(params)
228+
rawParams := json.RawMessage(bytes)
229+
216230
r.Parameters = &rawParams
217231
default:
218232
r.Type = ""
@@ -390,6 +404,18 @@ func NewRequiredWorkflowsRule(params *RequiredWorkflowsRuleParameters) (rule *Re
390404
}
391405
}
392406

407+
// NewFilePathRestrictionRule creates a rule to restrict file paths from being pushed to.
408+
func NewFilePathRestrictionRule(params *RuleFileParameters) (rule *RepositoryRule) {
409+
bytes, _ := json.Marshal(params)
410+
411+
rawParams := json.RawMessage(bytes)
412+
413+
return &RepositoryRule{
414+
Type: "file_path_restriction",
415+
Parameters: &rawParams,
416+
}
417+
}
418+
393419
// Ruleset represents a GitHub ruleset object.
394420
type Ruleset struct {
395421
ID *int64 `json:"id,omitempty"`

github/repos_rules_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ func TestRepositoryRule_UnmarshalJSON(t *testing.T) {
182182
},
183183
wantErr: true,
184184
},
185+
"Valid file_path_restriction params": {
186+
data: `{"type":"file_path_restriction","parameters":{"restricted_file_paths":["/a/file"]}}`,
187+
want: NewFilePathRestrictionRule(&RuleFileParameters{
188+
RestrictedFilePaths: &[]string{"/a/file"},
189+
}),
190+
},
191+
"Invalid file_path_restriction params": {
192+
data: `{"type":"file_path_restriction","parameters":{"restricted_file_paths":true}}`,
193+
want: &RepositoryRule{
194+
Type: "file_path_restriction",
195+
Parameters: nil,
196+
},
197+
wantErr: true,
198+
},
185199
"Valid pull_request params": {
186200
data: `{
187201
"type":"pull_request",

0 commit comments

Comments
 (0)