Skip to content

Commit c644bee

Browse files
feat: Support rule type code_scanning (#2436)
* feat: support rule type code_scanning for github_organization_ruleset and github_repository_ruleset * feat: docs * feat: docs * chore: remove tag protection endpoint --------- Co-authored-by: Keegan Campbell <[email protected]>
1 parent 5214d17 commit c644bee

6 files changed

+136
-2
lines changed

github/resource_github_organization_ruleset.go

+35
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,41 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
457457
},
458458
},
459459
},
460+
"required_code_scanning": {
461+
Type: schema.TypeList,
462+
MaxItems: 1,
463+
Optional: true,
464+
Description: "Choose which tools must provide code scanning results before the reference is updated. When configured, code scanning must be enabled and have results for both the commit and the reference being updated.",
465+
Elem: &schema.Resource{
466+
Schema: map[string]*schema.Schema{
467+
"required_code_scanning_tool": {
468+
Type: schema.TypeSet,
469+
MinItems: 1,
470+
Required: true,
471+
Description: "Tools that must provide code scanning results for this rule to pass.",
472+
Elem: &schema.Resource{
473+
Schema: map[string]*schema.Schema{
474+
"alerts_threshold": {
475+
Type: schema.TypeString,
476+
Required: true,
477+
Description: "The severity level at which code scanning results that raise alerts block a reference update. Can be one of: `none`, `errors`, `errors_and_warnings`, `all`.",
478+
},
479+
"security_alerts_threshold": {
480+
Type: schema.TypeString,
481+
Required: true,
482+
Description: "The severity level at which code scanning results that raise security alerts block a reference update. Can be one of: `none`, `critical`, `high_or_higher`, `medium_or_higher`, `all`.",
483+
},
484+
"tool": {
485+
Type: schema.TypeString,
486+
Required: true,
487+
Description: "The name of a code scanning tool.",
488+
},
489+
},
490+
},
491+
},
492+
},
493+
},
494+
},
460495
},
461496
},
462497
},

github/resource_github_organization_ruleset_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ func TestGithubOrganizationRulesets(t *testing.T) {
6969
}
7070
}
7171
72+
required_code_scanning {
73+
required_code_scanning_tool {
74+
alerts_threshold = "errors"
75+
security_alerts_threshold = "high_or_higher"
76+
tool = "CodeQL"
77+
}
78+
}
79+
7280
branch_name_pattern {
7381
name = "test"
7482
negate = false

github/resource_github_repository_ruleset.go

+35
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,41 @@ func resourceGithubRepositoryRuleset() *schema.Resource {
409409
},
410410
},
411411
},
412+
"required_code_scanning": {
413+
Type: schema.TypeList,
414+
MaxItems: 1,
415+
Optional: true,
416+
Description: "Choose which tools must provide code scanning results before the reference is updated. When configured, code scanning must be enabled and have results for both the commit and the reference being updated.",
417+
Elem: &schema.Resource{
418+
Schema: map[string]*schema.Schema{
419+
"required_code_scanning_tool": {
420+
Type: schema.TypeSet,
421+
MinItems: 1,
422+
Required: true,
423+
Description: "Tools that must provide code scanning results for this rule to pass.",
424+
Elem: &schema.Resource{
425+
Schema: map[string]*schema.Schema{
426+
"alerts_threshold": {
427+
Type: schema.TypeString,
428+
Required: true,
429+
Description: "The severity level at which code scanning results that raise alerts block a reference update. Can be one of: `none`, `errors`, `errors_and_warnings`, `all`.",
430+
},
431+
"security_alerts_threshold": {
432+
Type: schema.TypeString,
433+
Required: true,
434+
Description: "The severity level at which code scanning results that raise security alerts block a reference update. Can be one of: `none`, `critical`, `high_or_higher`, `medium_or_higher`, `all`.",
435+
},
436+
"tool": {
437+
Type: schema.TypeString,
438+
Required: true,
439+
Description: "The name of a code scanning tool",
440+
},
441+
},
442+
},
443+
},
444+
},
445+
},
446+
},
412447
},
413448
},
414449
},

github/respository_rules_utils.go

+32
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,38 @@ func expandRules(input []interface{}, org bool) []*github.RepositoryRule {
362362
rulesSlice = append(rulesSlice, github.NewRequiredWorkflowsRule(params))
363363
}
364364

365+
// Required code scanning to pass before merging rule
366+
if v, ok := rulesMap["required_code_scanning"].([]interface{}); ok && len(v) != 0 {
367+
requiredCodeScanningMap := v[0].(map[string]interface{})
368+
requiredCodeScanningTools := make([]*github.RuleRequiredCodeScanningTool, 0)
369+
370+
if requiredCodeScanningInput, ok := requiredCodeScanningMap["required_code_scanning_tool"]; ok {
371+
372+
requiredCodeScanningSet := requiredCodeScanningInput.(*schema.Set)
373+
for _, codeScanningMap := range requiredCodeScanningSet.List() {
374+
codeScanningTool := codeScanningMap.(map[string]interface{})
375+
376+
// Get all parameters
377+
alertsThreshold := github.String(codeScanningTool["alerts_threshold"].(string))
378+
securityAlertsThreshold := github.String(codeScanningTool["security_alerts_threshold"].(string))
379+
tool := github.String(codeScanningTool["tool"].(string))
380+
381+
params := &github.RuleRequiredCodeScanningTool{
382+
AlertsThreshold: *alertsThreshold,
383+
SecurityAlertsThreshold: *securityAlertsThreshold,
384+
Tool: *tool,
385+
}
386+
387+
requiredCodeScanningTools = append(requiredCodeScanningTools, params)
388+
}
389+
}
390+
391+
params := &github.RequiredCodeScanningRuleParameters{
392+
RequiredCodeScanningTools: requiredCodeScanningTools,
393+
}
394+
rulesSlice = append(rulesSlice, github.NewRequiredCodeScanningRule(params))
395+
}
396+
365397
return rulesSlice
366398
}
367399

website/docs/r/organization_ruleset.html.markdown

+14
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ The `rules` block supports the following:
9191

9292
* `required_workflows` - (Optional) (Block List, Max: 1) Define which Actions workflows must pass before changes can be merged into a branch matching the rule. Multiple workflows can be specified. (see [below for nested schema](#rules.required_workflows))
9393

94+
* `required_code_scanning` - (Optional) (Block List, Max: 1) Define which tools must provide code scanning results before the reference is updated. When configured, code scanning must be enabled and have results for both the commit and the reference being updated. Multiple code scanning tools can be specified. (see [below for nested schema](#rules.required_code_scanning))
95+
9496
* `tag_name_pattern` - (Optional) (Block List, Max: 1) Parameters to be used for the tag_name_pattern rule. This rule only applies to repositories within an enterprise, it cannot be applied to repositories owned by individuals or regular organizations. Conflicts with `branch_name_pattern` as it only applies to rulesets with target `tag`. (see [below for nested schema](#rules.tag_name_pattern))
9597

9698
* `update` - (Optional) (Boolean) Only allow users with bypass permission to update matching refs.
@@ -171,6 +173,18 @@ The `rules` block supports the following:
171173

172174
* `ref` - (Optional) (String) The optional ref from which to fetch the workflow. Defaults to `master`.
173175

176+
#### rules.required_code_scanning ####
177+
178+
* `required_code_scanning_tool` - (Required) (Block Set, Min: 1) Actions code scanning tools that are required. Multiple can be defined. (see [below for nested schema](#rules.required_workflows.required_code_scanning_tool))
179+
180+
#### rules.required_code_scanning.required_code_scanning_tool ####
181+
182+
* `alerts_threshold` - (Required) (String) The severity level at which code scanning results that raise alerts block a reference update. Can be one of: `none`, `errors`, `errors_and_warnings`, `all`.
183+
184+
* `security_alerts_threshold` - (Required) (String) The severity level at which code scanning results that raise security alerts block a reference update. Can be one of: `none`, `critical`, `high_or_higher`, `medium_or_higher`, `all`.
185+
186+
* `tool` - (Required) (String) The name of a code scanning tool.
187+
174188
#### rules.tag_name_pattern ####
175189

176190
* `operator` - (Required) (String) The operator to use for matching. Can be one of: `starts_with`, `ends_with`, `contains`, `regex`.

website/docs/r/repository_ruleset.html.markdown

+12-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ The `rules` block supports the following:
101101

102102
* `tag_name_pattern` - (Optional) (Block List, Max: 1) Parameters to be used for the tag_name_pattern rule. This rule only applies to repositories within an enterprise, it cannot be applied to repositories owned by individuals or regular organizations. Conflicts with `branch_name_pattern` as it only applied to rulesets with target `tag`. (see [below for nested schema](#rules.tag_name_pattern))
103103

104+
* `required_code_scanning` - (Optional) (Block List, Max: 1) Define which tools must provide code scanning results before the reference is updated. When configured, code scanning must be enabled and have results for both the commit and the reference being updated. Multiple code scanning tools can be specified. (see [below for nested schema](#rules.required_code_scanning))
105+
104106
* `update` - (Optional) (Boolean) Only allow users with bypass permission to update matching refs.
105107

106108
* `update_allows_fetch_and_merge` - (Optional) (Boolean) Branch can pull changes from its upstream repository. This is only applicable to forked repositories. Requires `update` to be set to `true`. Note: behaviour is affected by a known bug on the GitHub side which may cause issues when using this parameter.
@@ -179,8 +181,6 @@ The `rules` block supports the following:
179181

180182
* `integration_id` - (Optional) (Number) The optional integration ID that this status check must originate from.
181183

182-
183-
184184
#### rules.tag_name_pattern ####
185185

186186
* `operator` - (Required) (String) The operator to use for matching. Can be one of: `starts_with`, `ends_with`, `contains`, `regex`.
@@ -191,7 +191,17 @@ The `rules` block supports the following:
191191

192192
* `negate` - (Optional) (Boolean) If true, the rule will fail if the pattern matches.
193193

194+
#### rules.required_code_scanning ####
195+
196+
* `required_code_scanning_tool` - (Required) (Block Set, Min: 1) Actions code scanning tools that are required. Multiple can be defined. (see [below for nested schema](#rules.required_workflows.required_code_scanning_tool))
197+
198+
#### rules.required_code_scanning.required_code_scanning_tool ####
199+
200+
* `alerts_threshold` - (Required) (String) The severity level at which code scanning results that raise alerts block a reference update. Can be one of: `none`, `errors`, `errors_and_warnings`, `all`.
201+
202+
* `security_alerts_threshold` - (Required) (String) The severity level at which code scanning results that raise security alerts block a reference update. Can be one of: `none`, `critical`, `high_or_higher`, `medium_or_higher`, `all`.
194203

204+
* `tool` - (Required) (String) The name of a code scanning tool.
195205

196206
#### bypass_actors ####
197207

0 commit comments

Comments
 (0)