|
1 | 1 | package github |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "log" |
4 | 5 | "reflect" |
5 | 6 | "sort" |
6 | 7 |
|
7 | 8 | "github.com/google/go-github/v81/github" |
8 | 9 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
9 | 10 | ) |
10 | 11 |
|
| 12 | +var DEFAULT_PULL_REQUEST_MERGE_METHODS = []github.PullRequestMergeMethod{github.PullRequestMergeMethodMerge, github.PullRequestMergeMethodRebase, github.PullRequestMergeMethodSquash} |
| 13 | + |
11 | 14 | // Helper function to safely convert interface{} to int, handling both int and float64. |
12 | 15 | func toInt(v any) int { |
13 | 16 | switch val := v.(type) { |
@@ -36,6 +39,24 @@ func toInt64(v any) int64 { |
36 | 39 | } |
37 | 40 | } |
38 | 41 |
|
| 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 | + |
39 | 60 | func resourceGithubRulesetObject(d *schema.ResourceData, org string) github.RepositoryRuleset { |
40 | 61 | isOrgLevel := len(org) > 0 |
41 | 62 |
|
@@ -295,13 +316,19 @@ func expandRules(input []any, org bool) *github.RepositoryRulesetRules { |
295 | 316 | // Pull request rule |
296 | 317 | if v, ok := rulesMap["pull_request"].([]any); ok && len(v) != 0 { |
297 | 318 | 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 | + } |
298 | 325 | params := &github.PullRequestRuleParameters{ |
299 | | - AllowedMergeMethods: []github.PullRequestMergeMethod{github.PullRequestMergeMethodMerge, github.PullRequestMergeMethodSquash, github.PullRequestMergeMethodRebase}, |
300 | 326 | DismissStaleReviewsOnPush: pullRequestMap["dismiss_stale_reviews_on_push"].(bool), |
301 | 327 | RequireCodeOwnerReview: pullRequestMap["require_code_owner_review"].(bool), |
302 | 328 | RequireLastPushApproval: pullRequestMap["require_last_push_approval"].(bool), |
303 | 329 | RequiredApprovingReviewCount: toInt(pullRequestMap["required_approving_review_count"]), |
304 | 330 | RequiredReviewThreadResolution: pullRequestMap["required_review_thread_resolution"].(bool), |
| 331 | + AllowedMergeMethods: allowedMergeMethods.([]github.PullRequestMergeMethod), |
305 | 332 | } |
306 | 333 | rulesetRules.PullRequest = params |
307 | 334 | } |
@@ -542,7 +569,9 @@ func flattenRules(rules *github.RepositoryRulesetRules, org bool) []any { |
542 | 569 | "require_last_push_approval": rules.PullRequest.RequireLastPushApproval, |
543 | 570 | "required_approving_review_count": rules.PullRequest.RequiredApprovingReviewCount, |
544 | 571 | "required_review_thread_resolution": rules.PullRequest.RequiredReviewThreadResolution, |
| 572 | + "allowed_merge_methods": rules.PullRequest.AllowedMergeMethods, |
545 | 573 | }) |
| 574 | + log.Printf("[DEBUG] Flattened Pull Request rules slice request slice: %#v", pullRequestSlice) |
546 | 575 | rulesMap["pull_request"] = pullRequestSlice |
547 | 576 | } |
548 | 577 |
|
|
0 commit comments