Skip to content

Commit 16c81f2

Browse files
committed
feat: add support for Copilot code review in organization ruleset
1 parent 05a0020 commit 16c81f2

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

github/resource_github_organization_ruleset.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
300300
Default: false,
301301
Description: "All conversations on code must be resolved before a pull request can be merged. Defaults to `false`.",
302302
},
303+
"automatic_copilot_code_review_enabled": {
304+
Type: schema.TypeBool,
305+
Optional: true,
306+
Default: false,
307+
Description: "Automatically request Copilot code review. Defaults to `false`.",
308+
},
303309
},
304310
},
305311
},
@@ -646,6 +652,28 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
646652
},
647653
},
648654
},
655+
"copilot_code_review": {
656+
Type: schema.TypeList,
657+
MaxItems: 1,
658+
Optional: true,
659+
Description: "Request Copilot code review for new pull requests automatically if the author has access to Copilot code review.",
660+
Elem: &schema.Resource{
661+
Schema: map[string]*schema.Schema{
662+
"review_on_push": {
663+
Type: schema.TypeBool,
664+
Optional: true,
665+
Default: false,
666+
Description: "Copilot automatically reviews each new push to the pull reques. Defaults to `false`.",
667+
},
668+
"review_draft_pull_requests": {
669+
Type: schema.TypeBool,
670+
Optional: true,
671+
Default: false,
672+
Description: "Copilot automatically reviews draft pull requests before they are marked as ready for review. Defaults to `false`.",
673+
},
674+
},
675+
},
676+
},
649677
},
650678
},
651679
},

github/resource_github_organization_ruleset_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ func TestGithubOrganizationRulesets(t *testing.T) {
8686
pattern = "test"
8787
}
8888
89+
copilot_code_review {
90+
review_on_push = true
91+
review_draft_pull_requests = false
92+
}
93+
8994
non_fast_forward = true
9095
}
9196
}
@@ -193,6 +198,15 @@ func TestGithubOrganizationRulesets(t *testing.T) {
193198
"rules.0.required_code_scanning.0.required_code_scanning_tool.0.tool",
194199
"CodeQL",
195200
),
201+
202+
resource.TestCheckResourceAttr(
203+
"github_organization_ruleset.test", "rules.0.copilot_code_review.0.review_on_push",
204+
"true",
205+
),
206+
resource.TestCheckResourceAttr(
207+
"github_organization_ruleset.test", "rules.0.copilot_code_review.0.review_draft_pull_requests",
208+
"false",
209+
),
196210
)
197211

198212
testCase := func(t *testing.T, mode string, config string) {

github/respository_rules_utils.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,29 @@ func expandRules(input []interface{}, org bool) []*github.RepositoryRule {
547547
}
548548
rulesSlice = append(rulesSlice, github.NewFileExtensionRestrictionRule(params))
549549
}
550+
// Copilot code review rule
551+
if v, ok := rulesMap["copilot_code_review"].([]interface{}); ok && len(v) != 0 {
552+
copilotCodeReviewMap := v[0].(map[string]interface{})
553+
554+
// Create parameters map
555+
paramsMap := map[string]interface{}{
556+
"review_on_push": copilotCodeReviewMap["review_on_push"].(bool),
557+
"review_draft_pull_requests": copilotCodeReviewMap["review_draft_pull_requests"].(bool),
558+
}
559+
560+
// Marshal to JSON
561+
paramsJSON, err := json.Marshal(paramsMap)
562+
if err != nil {
563+
log.Printf("[INFO] Error marshalling copilot_code_review parameters: %v", err)
564+
} else {
565+
paramsRaw := json.RawMessage(paramsJSON)
566+
rule := &github.RepositoryRule{
567+
Type: "copilot_code_review",
568+
Parameters: &paramsRaw,
569+
}
570+
rulesSlice = append(rulesSlice, rule)
571+
}
572+
}
550573

551574
return rulesSlice
552575
}
@@ -732,6 +755,31 @@ func flattenRules(rules []*github.RepositoryRule, org bool) []interface{} {
732755
rule["min_entries_to_merge_wait_minutes"] = params.MinEntriesToMergeWaitMinutes
733756
rulesMap[v.Type] = []map[string]any{rule}
734757

758+
case "copilot_code_review":
759+
// Handle copilot_code_review rule parameters
760+
var params map[string]interface{}
761+
762+
err := json.Unmarshal(*v.Parameters, &params)
763+
if err != nil {
764+
log.Printf("[INFO] Unexpected error unmarshalling rule %s with parameters: %v",
765+
v.Type, v.Parameters)
766+
}
767+
768+
rule := make(map[string]interface{})
769+
if reviewOnPush, ok := params["review_on_push"].(bool); ok {
770+
rule["review_on_push"] = reviewOnPush
771+
} else {
772+
rule["review_on_push"] = false
773+
}
774+
775+
if reviewDraftPRs, ok := params["review_draft_pull_requests"].(bool); ok {
776+
rule["review_draft_pull_requests"] = reviewDraftPRs
777+
} else {
778+
rule["review_draft_pull_requests"] = false
779+
}
780+
781+
rulesMap[v.Type] = []map[string]interface{}{rule}
782+
735783
case "file_path_restriction":
736784
var params github.RuleFileParameters
737785
err := json.Unmarshal(*v.Parameters, &params)

website/docs/r/organization_ruleset.html.markdown

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ resource "github_organization_ruleset" "example" {
3939
required_linear_history = true
4040
required_signatures = true
4141
42+
copilot_code_review {
43+
review_on_push = true
44+
review_draft_pull_requests = false
45+
}
46+
4247
branch_name_pattern {
4348
name = "example"
4449
negate = false
@@ -120,6 +125,8 @@ The `rules` block supports the following:
120125

121126
* `committer_email_pattern` - (Optional) (Block List, Max: 1) Parameters to be used for the committer_email_pattern rule. This rule only applies to repositories within an enterprise, it cannot be applied to repositories owned by individuals or regular organizations. (see [below for nested schema](#rules.committer_email_pattern))
122127

128+
* `copilot_code_review` - (Optional) (Block List, Max: 1) Request Copilot code review for new pull requests automatically if the author has access to Copilot code review. (see [below for nested schema](#rules.copilot_code_review))
129+
123130
* `creation` - (Optional) (Boolean) Only allow users with bypass permission to create matching refs.
124131

125132
* `deletion` - (Optional) (Boolean) Only allow users with bypass permissions to delete matching refs.
@@ -190,6 +197,12 @@ The `rules` block supports the following:
190197

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

200+
#### rules.copilot_code_review ####
201+
202+
* `review_on_push` - (Optional) (Boolean) Copilot automatically reviews each new push to the pull request. Defaults to `false`.
203+
204+
* `review_draft_pull_requests` - (Optional) (Boolean) Copilot automatically reviews draft pull requests before they are marked as ready for review. Defaults to `false`.
205+
193206
#### rules.pull_request ####
194207

195208
* `dismiss_stale_reviews_on_push` - (Optional) (Boolean) New, reviewable commits pushed will dismiss previous pull request review approvals. Defaults to `false`.

0 commit comments

Comments
 (0)