mirror of
https://gitea.com/gitea/tea.git
synced 2026-09-10 15:36:37 -04:00
## Problem `tea pr merge <index>` reports the same misleading error for every refusal: ``` failed to merge PR, is it still open? ``` The PR usually *is* still open — `tea pr <index>` shows it as open and lists `Conflicting files` — so the message sends users looking in the wrong direction. ## Root cause Gitea answers an unmergeable PR with a 405 and a body naming the actual cause. The SDK's `MergePullRequest` is built on `getStatusCode`, which returns only the status code and never calls `statusCodeToErr`, so the body is discarded. tea receives `success=false, err=nil` with no server explanation to pass on, and fell back to guessing that the PR might be closed. ## Changes - Derive the refusal reason from the pull request when a merge fails: already merged, closed, draft, or not mergeable. - When the PR looks mergeable but was refused anyway, name the conditions tea cannot observe (required status checks, requested reviews, branch protection) instead of guessing. - Include the PR index in the error. - Add table-driven tests for every reason, plus the case where the follow-up PR lookup fails. The extra API call happens only on the failure path. Fixes #1022 --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/gitea/tea/pulls/1107 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Jan Baer <jan.s.baer@googlemail.com>
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package task
|
|
|
|
import (
|
|
stdctx "context"
|
|
"fmt"
|
|
|
|
gitea "gitea.dev/sdk"
|
|
|
|
"gitea.dev/tea/modules/config"
|
|
)
|
|
|
|
// PullMerge merges a PR
|
|
func PullMerge(requestCtx stdctx.Context, login *config.Login, repoOwner, repoName string, index int64, opt gitea.MergePullRequestOption) error {
|
|
client := login.Client()
|
|
success, _, err := client.PullRequests.MergePullRequest(requestCtx, repoOwner, repoName, index, opt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if success {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("failed to merge PR #%d: %s", index,
|
|
mergeFailureReason(requestCtx, client, repoOwner, repoName, index))
|
|
}
|
|
|
|
// mergeFailureReason returns why merging was refused. The SDK reports refusal as
|
|
// success=false and discards Gitea's explanatory body, so the reason has to be
|
|
// re-derived from the PR. Costs one API call, on the failure path only.
|
|
func mergeFailureReason(requestCtx stdctx.Context, client *gitea.Client, repoOwner, repoName string, index int64) string {
|
|
// Fallback naming the conditions tea cannot observe, used when the PR looks
|
|
// mergeable but the merge was refused anyway.
|
|
const refused = "the server refused the merge; check required status checks, requested reviews, or branch protection rules"
|
|
|
|
pr, _, err := client.PullRequests.GetPullRequest(requestCtx, repoOwner, repoName, index)
|
|
if err != nil || pr == nil {
|
|
return refused
|
|
}
|
|
|
|
switch {
|
|
case pr.HasMerged:
|
|
return "it has already been merged"
|
|
case pr.State == gitea.StateClosed:
|
|
return "it is closed"
|
|
case pr.Draft:
|
|
return "it is a draft; mark it ready for review first"
|
|
case !pr.Mergeable:
|
|
return "it has conflicting files or is otherwise not mergeable"
|
|
default:
|
|
return refused
|
|
}
|
|
}
|