From 6e3d8cd9893b2d7cfe9155d04e1ca07167e5734f Mon Sep 17 00:00:00 2001 From: Ross Gustafson Date: Wed, 25 Sep 2024 15:52:40 +0100 Subject: [PATCH 1/2] Implement required functions and unmarshal config for push rules --- github/github-accessors.go | 8 ++ github/github-accessors_test.go | 10 +++ github/repos_rules.go | 78 +++++++++++++++++++ github/repos_rules_test.go | 130 ++++++++++++++++++++++++++++++++ 4 files changed, 226 insertions(+) diff --git a/github/github-accessors.go b/github/github-accessors.go index a025f781faf..9c004856bb9 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -20910,6 +20910,14 @@ func (r *Rule) GetSeverity() string { return *r.Severity } +// GetRestrictedFileExtensions returns the RestrictedFileExtensions field if it's non-nil, zero value otherwise. +func (r *RuleFileExtensionRestrictionParameters) GetRestrictedFileExtensions() []string { + if r == nil || r.RestrictedFileExtensions == nil { + return nil + } + return *r.RestrictedFileExtensions +} + // GetRestrictedFilePaths returns the RestrictedFilePaths field if it's non-nil, zero value otherwise. func (r *RuleFileParameters) GetRestrictedFilePaths() []string { if r == nil || r.RestrictedFilePaths == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index ae380160dc7..9cabf095c84 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -24299,6 +24299,16 @@ func TestRule_GetSeverity(tt *testing.T) { r.GetSeverity() } +func TestRuleFileExtensionRestrictionParameters_GetRestrictedFileExtensions(tt *testing.T) { + var zeroValue []string + r := &RuleFileExtensionRestrictionParameters{RestrictedFileExtensions: &zeroValue} + r.GetRestrictedFileExtensions() + r = &RuleFileExtensionRestrictionParameters{} + r.GetRestrictedFileExtensions() + r = nil + r.GetRestrictedFileExtensions() +} + func TestRuleFileParameters_GetRestrictedFilePaths(tt *testing.T) { var zeroValue []string r := &RuleFileParameters{RestrictedFilePaths: &zeroValue} diff --git a/github/repos_rules.go b/github/repos_rules.go index 50e75cdba1b..23c2e11b530 100644 --- a/github/repos_rules.go +++ b/github/repos_rules.go @@ -85,6 +85,21 @@ type RuleFileParameters struct { RestrictedFilePaths *[]string `json:"restricted_file_paths"` } +// RuleMaxFilePathLengthParameters represents the max_file_path_length rule parameters. +type RuleMaxFilePathLengthParameters struct { + MaxFilePathLength int `json:"max_file_path_length"` +} + +// RuleFileExtensionRestrictionParameters represents the file_extension_restriction rule parameters. +type RuleFileExtensionRestrictionParameters struct { + RestrictedFileExtensions *[]string `json:"restricted_file_extensions"` +} + +// RuleMaxFileSizeParameters represents the max_file_size rule parameters. +type RuleMaxFileSizeParameters struct { + MaxFileSize int `json:"max_file_size"` +} + // UpdateAllowsFetchAndMergeRuleParameters represents the update rule parameters. type UpdateAllowsFetchAndMergeRuleParameters struct { UpdateAllowsFetchAndMerge bool `json:"update_allows_fetch_and_merge"` @@ -255,6 +270,33 @@ func (r *RepositoryRule) UnmarshalJSON(data []byte) error { bytes, _ := json.Marshal(params) rawParams := json.RawMessage(bytes) + r.Parameters = &rawParams + case "max_file_path_length": + params := RuleMaxFilePathLengthParameters{} + if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { + return err + } + bytes, _ := json.Marshal(params) + rawParams := json.RawMessage(bytes) + + r.Parameters = &rawParams + case "file_extension_restriction": + params := RuleFileExtensionRestrictionParameters{} + if err := json.Unmarshal(*RepositoryRule.Parameters, ¶ms); err != nil { + return err + } + bytes, _ := json.Marshal(params) + rawParams := json.RawMessage(bytes) + + r.Parameters = &rawParams + case "max_file_size": + params := RuleMaxFileSizeParameters{} + 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 = "" @@ -454,6 +496,42 @@ func NewFilePathRestrictionRule(params *RuleFileParameters) (rule *RepositoryRul } } +// NewMaxFilePathLengthRule creates a rule to restrict file paths longer than the limit from being pushed. +func NewMaxFilePathLengthRule(params *RuleMaxFilePathLengthParameters) (rule *RepositoryRule) { + bytes, _ := json.Marshal(params) + + rawParams := json.RawMessage(bytes) + + return &RepositoryRule{ + Type: "max_file_path_length", + Parameters: &rawParams, + } +} + +// NewFileExtensionRestrictionRule creates a rule to restrict file extensions from being pushed to a commit. +func NewFileExtensionRestrictionRule(params *RuleFileExtensionRestrictionParameters) (rule *RepositoryRule) { + bytes, _ := json.Marshal(params) + + rawParams := json.RawMessage(bytes) + + return &RepositoryRule{ + Type: "file_extension_restriction", + Parameters: &rawParams, + } +} + +// NewMaxFileSizeRule creates a rule to restrict file sizes from being pushed to a commit. +func NewMaxFileSizeRule(params *RuleMaxFileSizeParameters) (rule *RepositoryRule) { + bytes, _ := json.Marshal(params) + + rawParams := json.RawMessage(bytes) + + return &RepositoryRule{ + Type: "max_file_size", + 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 8e123ed0f8e..12715a646f0 100644 --- a/github/repos_rules_test.go +++ b/github/repos_rules_test.go @@ -312,6 +312,48 @@ func TestRepositoryRule_UnmarshalJSON(t *testing.T) { }, wantErr: true, }, + "Valid max_file_path_length params": { + data: `{"type":"max_file_path_length","parameters":{"max_file_path_length": 255}}`, + want: NewMaxFilePathLengthRule(&RuleMaxFilePathLengthParameters{ + MaxFilePathLength: 255, + }), + }, + "Invalid max_file_path_length params": { + data: `{"type":"max_file_path_length","parameters":{"max_file_path_length": "255"}}`, + want: &RepositoryRule{ + Type: "max_file_path_length", + Parameters: nil, + }, + wantErr: true, + }, + "Valid file_extension_restriction params": { + data: `{"type":"file_extension_restriction","parameters":{"restricted_file_extensions":[".exe"]}}`, + want: NewFileExtensionRestrictionRule(&RuleFileExtensionRestrictionParameters{ + RestrictedFileExtensions: &[]string{".exe"}, + }), + }, + "Invalid file_extension_restriction params": { + data: `{"type":"file_extension_restriction","parameters":{"restricted_file_extensions":true}}`, + want: &RepositoryRule{ + Type: "file_extension_restriction", + Parameters: nil, + }, + wantErr: true, + }, + "Valid max_file_size params": { + data: `{"type":"max_file_size","parameters":{"max_file_size": 1024}}`, + want: NewMaxFileSizeRule(&RuleMaxFileSizeParameters{ + MaxFileSize: 1024, + }), + }, + "Invalid max_file_size params": { + data: `{"type":"max_file_size","parameters":{"max_file_size": "1024"}}`, + want: &RepositoryRule{ + Type: "max_file_size", + Parameters: nil, + }, + wantErr: true, + }, } for name, tc := range tests { @@ -539,6 +581,94 @@ func TestRepositoriesService_CreateRuleset(t *testing.T) { }) } +func TestRepositoriesService_CreateRulesetWithPushRules(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/repo/rulesets", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + fmt.Fprint(w, `{ + "id": 42, + "name": "ruleset", + "source_type": "Repository", + "source": "o/repo", + "enforcement": "enabled", + "target": "push", + "rules": [ + { + "type": "file_path_restriction", + "parameters": { + "restricted_file_paths": ["/a/file"] + } + }, + { + "type": "max_file_path_length", + "parameters": { + "max_file_path_length": 255 + } + }, + { + "type": "file_extension_restriction", + "parameters": { + "restricted_file_extensions": [".exe"] + } + }, + { + "type": "max_file_size", + "parameters": { + "max_file_size": 1024 + } + } + ] + }`) + }) + + ctx := context.Background() + ruleSet, _, err := client.Repositories.CreateRuleset(ctx, "o", "repo", &Ruleset{ + Name: "ruleset", + Enforcement: "enabled", + }) + if err != nil { + t.Errorf("Repositories.CreateRuleset returned error: %v", err) + } + + want := &Ruleset{ + ID: Int64(42), + Name: "ruleset", + SourceType: String("Repository"), + Source: "o/repo", + Target: String("push"), + Enforcement: "enabled", + Rules: []*RepositoryRule{ + NewFilePathRestrictionRule(&RuleFileParameters{ + RestrictedFilePaths: &[]string{"/a/file"}, + }), + NewMaxFilePathLengthRule(&RuleMaxFilePathLengthParameters{ + MaxFilePathLength: 255, + }), + NewFileExtensionRestrictionRule(&RuleFileExtensionRestrictionParameters{ + RestrictedFileExtensions: &[]string{".exe"}, + }), + NewMaxFileSizeRule(&RuleMaxFileSizeParameters{ + MaxFileSize: 1024, + }), + }, + } + if !cmp.Equal(ruleSet, want) { + t.Errorf("Repositories.CreateRuleset returned %+v, want %+v", ruleSet, want) + } + + const methodName = "CreateRuleset" + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Repositories.CreateRuleset(ctx, "o", "repo", &Ruleset{}) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + func TestRepositoriesService_GetRuleset(t *testing.T) { client, mux, _, teardown := setup() defer teardown() From b6bb7b8b9812faa7f929de9349bad800d680cd81 Mon Sep 17 00:00:00 2001 From: Ross Gustafson Date: Thu, 26 Sep 2024 14:01:13 +0100 Subject: [PATCH 2/2] Implement review suggestions --- github/github-accessors.go | 8 -------- github/github-accessors_test.go | 10 ---------- github/repos_rules.go | 4 ++-- github/repos_rules_test.go | 4 ++-- 4 files changed, 4 insertions(+), 22 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index 9c004856bb9..a025f781faf 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -20910,14 +20910,6 @@ func (r *Rule) GetSeverity() string { return *r.Severity } -// GetRestrictedFileExtensions returns the RestrictedFileExtensions field if it's non-nil, zero value otherwise. -func (r *RuleFileExtensionRestrictionParameters) GetRestrictedFileExtensions() []string { - if r == nil || r.RestrictedFileExtensions == nil { - return nil - } - return *r.RestrictedFileExtensions -} - // GetRestrictedFilePaths returns the RestrictedFilePaths field if it's non-nil, zero value otherwise. func (r *RuleFileParameters) GetRestrictedFilePaths() []string { if r == nil || r.RestrictedFilePaths == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 9cabf095c84..ae380160dc7 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -24299,16 +24299,6 @@ func TestRule_GetSeverity(tt *testing.T) { r.GetSeverity() } -func TestRuleFileExtensionRestrictionParameters_GetRestrictedFileExtensions(tt *testing.T) { - var zeroValue []string - r := &RuleFileExtensionRestrictionParameters{RestrictedFileExtensions: &zeroValue} - r.GetRestrictedFileExtensions() - r = &RuleFileExtensionRestrictionParameters{} - r.GetRestrictedFileExtensions() - r = nil - r.GetRestrictedFileExtensions() -} - func TestRuleFileParameters_GetRestrictedFilePaths(tt *testing.T) { var zeroValue []string r := &RuleFileParameters{RestrictedFilePaths: &zeroValue} diff --git a/github/repos_rules.go b/github/repos_rules.go index 23c2e11b530..d09bb71d103 100644 --- a/github/repos_rules.go +++ b/github/repos_rules.go @@ -92,12 +92,12 @@ type RuleMaxFilePathLengthParameters struct { // RuleFileExtensionRestrictionParameters represents the file_extension_restriction rule parameters. type RuleFileExtensionRestrictionParameters struct { - RestrictedFileExtensions *[]string `json:"restricted_file_extensions"` + RestrictedFileExtensions []string `json:"restricted_file_extensions"` } // RuleMaxFileSizeParameters represents the max_file_size rule parameters. type RuleMaxFileSizeParameters struct { - MaxFileSize int `json:"max_file_size"` + MaxFileSize int64 `json:"max_file_size"` } // UpdateAllowsFetchAndMergeRuleParameters represents the update rule parameters. diff --git a/github/repos_rules_test.go b/github/repos_rules_test.go index 12715a646f0..6daf753432e 100644 --- a/github/repos_rules_test.go +++ b/github/repos_rules_test.go @@ -329,7 +329,7 @@ func TestRepositoryRule_UnmarshalJSON(t *testing.T) { "Valid file_extension_restriction params": { data: `{"type":"file_extension_restriction","parameters":{"restricted_file_extensions":[".exe"]}}`, want: NewFileExtensionRestrictionRule(&RuleFileExtensionRestrictionParameters{ - RestrictedFileExtensions: &[]string{".exe"}, + RestrictedFileExtensions: []string{".exe"}, }), }, "Invalid file_extension_restriction params": { @@ -647,7 +647,7 @@ func TestRepositoriesService_CreateRulesetWithPushRules(t *testing.T) { MaxFilePathLength: 255, }), NewFileExtensionRestrictionRule(&RuleFileExtensionRestrictionParameters{ - RestrictedFileExtensions: &[]string{".exe"}, + RestrictedFileExtensions: []string{".exe"}, }), NewMaxFileSizeRule(&RuleMaxFileSizeParameters{ MaxFileSize: 1024,