From bfe28f6750be101739a6c58855651e08c1de1e3e Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 14 Nov 2026 12:03:40 +1100 Subject: [PATCH] fix(taskactions): a refused merge tells the operator why, not the forge's error chain (#1872) --- internal/app/archied/task_actions.go | 10 +++++++++- internal/domain/taskactions/service.go | 20 ++++++++++++++++++-- internal/forge/merge.go | 11 +++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/internal/app/archied/task_actions.go b/internal/app/archied/task_actions.go index 1148b6e2f..4b7feb28c 100644 --- a/internal/app/archied/task_actions.go +++ b/internal/app/archied/task_actions.go @@ -2,6 +2,7 @@ package archied import ( "context" + "errors" "github.com/samcharles93/archie-core/internal/domain/taskactions" "github.com/samcharles93/archie-core/internal/domain/workflow" @@ -57,7 +58,14 @@ func (b *boot) mergePR(ctx context.Context, owner, repo string, number int) erro if err != nil { return err } - return forge.MergeOrConfirm(ctx, f, owner, repo, number) + if err := forge.MergeOrConfirm(ctx, f, owner, repo, number); err != nil { + b.log.WarnContext(ctx, "merge pull request failed", "repo", owner+"/"+repo, "pr", number, "err", err) + if errors.Is(err, forge.ErrPRClosed) { + return taskactions.ErrPRClosed + } + return err + } + return nil } func (b *boot) removeTaskLogs(id int64) error { diff --git a/internal/domain/taskactions/service.go b/internal/domain/taskactions/service.go index e44d8b6f5..4f1adf0ff 100644 --- a/internal/domain/taskactions/service.go +++ b/internal/domain/taskactions/service.go @@ -17,7 +17,10 @@ import ( ) var ( - ErrNotFound = errors.New("task not found") + ErrNotFound = errors.New("task not found") + // ErrPRClosed is MergePR's report that the pull request was closed + // without merging. + ErrPRClosed = errors.New("pull request closed without merging") ErrConflict = errors.New("task action conflict") ErrUnavailable = errors.New("runtime task control is unavailable") ) @@ -588,7 +591,10 @@ func (s Service) applyMerge(ctx context.Context, task *Task, actor Actor, o outc return o, fmt.Errorf("%w: task has no pull request", ErrConflict) } if err := s.MergePR(org.WithOrg(ctx, task.Org), task.Owner, task.Repo, task.PRNumber); err != nil { - return o, fmt.Errorf("%w: merge pull request #%d: %w", ErrConflict, task.PRNumber, err) + if errors.Is(err, ErrPRClosed) { + return o, conflict{fmt.Sprintf("Pull request #%d was closed without merging.", task.PRNumber), err} + } + return o, conflict{fmt.Sprintf("Pull request #%d could not be merged.", task.PRNumber), err} } o.event.Kind, o.event.Detail = events.KindPRMergeRequested, actor.describe(fmt.Sprintf("merged pull request #%d", task.PRNumber)) o.event.Data = map[string]any{"pr_number": task.PRNumber} @@ -679,3 +685,13 @@ func (s Service) warn(msg string, args ...any) { s.Warn(msg, args...) } } + +// conflict is an ErrConflict whose message is operator copy; the forge's own +// error stays reachable through Unwrap for logs and matching. +type conflict struct { + message string + cause error +} + +func (c conflict) Error() string { return c.message } +func (c conflict) Unwrap() []error { return []error{ErrConflict, c.cause} } diff --git a/internal/forge/merge.go b/internal/forge/merge.go index deef5c50d..0f1b89d87 100644 --- a/internal/forge/merge.go +++ b/internal/forge/merge.go @@ -8,6 +8,14 @@ import ( // PRStateMerged is the state PRState reports for a merged pull request. const PRStateMerged = "merged" +// PRStateClosed is the state PRState reports for a pull request closed +// without merging. +const PRStateClosed = "closed" + +// ErrPRClosed reports a merge refused because the pull request was closed +// without merging. +var ErrPRClosed = errors.New("pull request closed without merging") + // MergeOrConfirm merges a pull request and treats a refusal as success when // the forge reports the PR already merged: a merge outside Archie, or one that // raced the reconciler, leaves nothing to refuse. @@ -20,5 +28,8 @@ func MergeOrConfirm(ctx context.Context, f Forge, owner, repo string, number int if err == nil && state == PRStateMerged { return nil } + if err == nil && state == PRStateClosed { + return errors.Join(ErrPRClosed, mergeErr) + } return errors.Join(mergeErr, err) }