Skip to content

Commit bc40d00

Browse files
committed
Add handling of AllowedMergeMethods
It's a required field in the API and go-github doesn't use `omitempty`, so it submits `nil` if it isn't sent explicitly. This change tries to keep it in the state, without having a configuration option for it (poor choice?) And it defaults to all 3 available merge methods if it can't set something from the state. Signed-off-by: Timo Sand <[email protected]>
1 parent 2740389 commit bc40d00

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

github/util_rules.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package github
22

33
import (
4+
"log"
45
"reflect"
56
"sort"
67

78
"github.com/google/go-github/v81/github"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
)
1011

12+
var DEFAULT_PULL_REQUEST_MERGE_METHODS = []github.PullRequestMergeMethod{github.PullRequestMergeMethodMerge, github.PullRequestMergeMethodRebase, github.PullRequestMergeMethodSquash}
13+
1114
// Helper function to safely convert interface{} to int, handling both int and float64.
1215
func toInt(v any) int {
1316
switch val := v.(type) {
@@ -36,6 +39,24 @@ func toInt64(v any) int64 {
3639
}
3740
}
3841

42+
func toPullRequestMergeMethods(input []string) []github.PullRequestMergeMethod {
43+
mergeMethods := make([]github.PullRequestMergeMethod, 0)
44+
for _, method := range input {
45+
switch method {
46+
case "merge":
47+
mergeMethods = append(mergeMethods, github.PullRequestMergeMethodMerge)
48+
case "rebase":
49+
mergeMethods = append(mergeMethods, github.PullRequestMergeMethodRebase)
50+
case "squash":
51+
mergeMethods = append(mergeMethods, github.PullRequestMergeMethodSquash)
52+
}
53+
}
54+
if len(mergeMethods) == 0 {
55+
mergeMethods = append(mergeMethods, github.PullRequestMergeMethodMerge) // We need to send at least one method to the API. Defaulting to merge.
56+
}
57+
return mergeMethods
58+
}
59+
3960
func resourceGithubRulesetObject(d *schema.ResourceData, org string) github.RepositoryRuleset {
4061
isOrgLevel := len(org) > 0
4162

@@ -295,13 +316,19 @@ func expandRules(input []any, org bool) *github.RepositoryRulesetRules {
295316
// Pull request rule
296317
if v, ok := rulesMap["pull_request"].([]any); ok && len(v) != 0 {
297318
pullRequestMap := v[0].(map[string]any)
319+
allowedMergeMethods := pullRequestMap["allowed_merge_methods"]
320+
if allowedMergeMethods != nil {
321+
allowedMergeMethods = toPullRequestMergeMethods(allowedMergeMethods.([]string))
322+
} else {
323+
allowedMergeMethods = DEFAULT_PULL_REQUEST_MERGE_METHODS
324+
}
298325
params := &github.PullRequestRuleParameters{
299-
AllowedMergeMethods: []github.PullRequestMergeMethod{github.PullRequestMergeMethodMerge, github.PullRequestMergeMethodSquash, github.PullRequestMergeMethodRebase},
300326
DismissStaleReviewsOnPush: pullRequestMap["dismiss_stale_reviews_on_push"].(bool),
301327
RequireCodeOwnerReview: pullRequestMap["require_code_owner_review"].(bool),
302328
RequireLastPushApproval: pullRequestMap["require_last_push_approval"].(bool),
303329
RequiredApprovingReviewCount: toInt(pullRequestMap["required_approving_review_count"]),
304330
RequiredReviewThreadResolution: pullRequestMap["required_review_thread_resolution"].(bool),
331+
AllowedMergeMethods: allowedMergeMethods.([]github.PullRequestMergeMethod),
305332
}
306333
rulesetRules.PullRequest = params
307334
}
@@ -542,7 +569,9 @@ func flattenRules(rules *github.RepositoryRulesetRules, org bool) []any {
542569
"require_last_push_approval": rules.PullRequest.RequireLastPushApproval,
543570
"required_approving_review_count": rules.PullRequest.RequiredApprovingReviewCount,
544571
"required_review_thread_resolution": rules.PullRequest.RequiredReviewThreadResolution,
572+
"allowed_merge_methods": rules.PullRequest.AllowedMergeMethods,
545573
})
574+
log.Printf("[DEBUG] Flattened Pull Request rules slice request slice: %#v", pullRequestSlice)
546575
rulesMap["pull_request"] = pullRequestSlice
547576
}
548577

0 commit comments

Comments
 (0)