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

feat: Add support for code_scanning #3256

Merged
merged 8 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 33 additions & 0 deletions github/repos_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@
RequiredWorkflows []*RuleRequiredWorkflow `json:"workflows"`
}

// RuleRequiredCodeScanningTools represents the RequiredCodeScanningTools for the RequiredCodeScanningParameters object.
type RuleRequiredCodeScanningTools struct {
AlertsThreshold string `json:"alerts_threshold"`
SecurityAlertsThreshold string `json:"security_alerts_threshold"`
Tool string `json:"tool"`
}

// RequiredCodeScanningRuleParameters represents the code_scanning rule parameters.
type RequiredCodeScanningRuleParameters struct {
RequiredCodeScanningTools []*RuleRequiredCodeScanningTools `json:"code_scanning_tools"`
}

// RepositoryRule represents a GitHub Rule.
type RepositoryRule struct {
Type string `json:"type"`
Expand Down Expand Up @@ -229,6 +241,15 @@
bytes, _ := json.Marshal(params)
rawParams := json.RawMessage(bytes)

r.Parameters = &rawParams
case "code_scanning":
params := RequiredCodeScanningRuleParameters{}
if err := json.Unmarshal(*RepositoryRule.Parameters, &params); err != nil {
return err

Check warning on line 248 in github/repos_rules.go

View check run for this annotation

Codecov / codecov/patch

github/repos_rules.go#L248

Added line #L248 was not covered by tests
}
bytes, _ := json.Marshal(params)
rawParams := json.RawMessage(bytes)

r.Parameters = &rawParams
default:
r.Type = ""
Expand Down Expand Up @@ -406,6 +427,18 @@
}
}

// NewRequiredCodeScanningRule creates a rule to require which tools must provide code scanning results before the reference is updated.
func NewRequiredCodeScanningRule(params *RequiredCodeScanningRuleParameters) (rule *RepositoryRule) {
bytes, _ := json.Marshal(params)

rawParams := json.RawMessage(bytes)

return &RepositoryRule{
Type: "code_scanning",
Parameters: &rawParams,
}
}

// NewFilePathRestrictionRule creates a rule to restrict file paths from being pushed to.
func NewFilePathRestrictionRule(params *RuleFileParameters) (rule *RepositoryRule) {
bytes, _ := json.Marshal(params)
Expand Down
12 changes: 12 additions & 0 deletions github/repos_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ func TestRepositoryRule_UnmarshalJSON(t *testing.T) {
},
}),
},
"Required code_scanning params": {
data: `{"type":"code_scanning","parameters":{"code_scanning_tools":[{"tool": "CodeQL", "security_alerts_threshold": "high_or_higher", "alerts_threshold": "errors"}]}}`,
want: NewRequiredCodeScanningRule(&RequiredCodeScanningRuleParameters{
RequiredCodeScanningTools: []*RuleRequiredCodeScanningTools{
{
Tool: "CodeQL",
SecurityAlertsThreshold: "high_or_higher",
AlertsThreshold: "errors",
},
},
}),
},
"Invalid type": {
data: `{"type":"unknown"}`,
want: &RepositoryRule{
Expand Down
Loading