Skip to content

Commit be1b1e0

Browse files
committed
Fixed type conversion for allowed_merge_methods
Signed-off-by: Timo Sand <[email protected]>
1 parent e2b8db9 commit be1b1e0

1 file changed

Lines changed: 12 additions & 19 deletions

File tree

github/util_rules.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,17 @@ func toInt64(v any) int64 {
3939
}
4040
}
4141

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-
}
42+
func toPullRequestMergeMethods(input any) []github.PullRequestMergeMethod {
43+
value, ok := input.([]any)
44+
if !ok || value == nil || len(value) == 0 {
45+
log.Printf("[DEBUG] No allowed merge methods provided, using default: %#v", input)
46+
return DEFAULT_PULL_REQUEST_MERGE_METHODS
5347
}
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.
48+
mergeMethods := make([]github.PullRequestMergeMethod, 0, len(value))
49+
for _, item := range value {
50+
if method, ok := item.(string); ok {
51+
mergeMethods = append(mergeMethods, github.PullRequestMergeMethod(method))
52+
}
5653
}
5754
return mergeMethods
5855
}
@@ -317,18 +314,14 @@ func expandRules(input []any, org bool) *github.RepositoryRulesetRules {
317314
if v, ok := rulesMap["pull_request"].([]any); ok && len(v) != 0 {
318315
pullRequestMap := v[0].(map[string]any)
319316
allowedMergeMethods := pullRequestMap["allowed_merge_methods"]
320-
if allowedMergeMethods != nil {
321-
allowedMergeMethods = toPullRequestMergeMethods(allowedMergeMethods.([]string))
322-
} else {
323-
allowedMergeMethods = DEFAULT_PULL_REQUEST_MERGE_METHODS
324-
}
317+
325318
params := &github.PullRequestRuleParameters{
326319
DismissStaleReviewsOnPush: pullRequestMap["dismiss_stale_reviews_on_push"].(bool),
327320
RequireCodeOwnerReview: pullRequestMap["require_code_owner_review"].(bool),
328321
RequireLastPushApproval: pullRequestMap["require_last_push_approval"].(bool),
329322
RequiredApprovingReviewCount: toInt(pullRequestMap["required_approving_review_count"]),
330323
RequiredReviewThreadResolution: pullRequestMap["required_review_thread_resolution"].(bool),
331-
AllowedMergeMethods: allowedMergeMethods.([]github.PullRequestMergeMethod),
324+
AllowedMergeMethods: toPullRequestMergeMethods(allowedMergeMethods),
332325
}
333326
rulesetRules.PullRequest = params
334327
}

0 commit comments

Comments
 (0)