mirror of
https://gitea.com/gitea/tea.git
synced 2026-09-15 11:28:11 +02:00
fix(pulls): report the real reason when a merge is refused (#1107)
## 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>
This commit is contained in:
committed by
Lunny Xiao
co-authored by
Lunny Xiao
parent
b2bab268d7
commit
4d09587d4c
@@ -19,8 +19,36 @@ func PullMerge(requestCtx stdctx.Context, login *config.Login, repoOwner, repoNa
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !success {
|
||||
return fmt.Errorf("failed to merge PR, is it still open?")
|
||||
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
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user