fix(taskactions): a refused merge tells the operator why, not the forge's error chain (#1872)
This commit is contained in:
3 files changed
+38
-3
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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} }
|
||||
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user