feat(taskactions): rejecting a pr_open task closes its pull request
This commit is contained in:
1 parent
8aaab2b9b7
commit
ee3dbda777
13 files changed
+443
-232
No files matched your search
@@ -19,6 +19,7 @@ func (b *boot) taskActions() taskactions.Service {
|
||||
taskactionstore.MaxRetries(b.cfgHolder),
|
||||
b.cancelTask,
|
||||
b.closeIssue,
|
||||
b.closePR,
|
||||
b.removeTaskLogs,
|
||||
b.removeTaskWorktree,
|
||||
b.publishEvent,
|
||||
@@ -40,6 +41,13 @@ func (b *boot) closeIssue(ctx context.Context, owner, repo string, number int, c
|
||||
return b.forgeClient.CloseIssue(ctx, owner, repo, number, comment)
|
||||
}
|
||||
|
||||
func (b *boot) closePR(ctx context.Context, owner, repo string, number int, comment string) error {
|
||||
if b.forgeClient == nil {
|
||||
return fmt.Errorf("forge is not configured")
|
||||
}
|
||||
return b.forgeClient.ClosePR(ctx, owner, repo, number, comment)
|
||||
}
|
||||
|
||||
func (b *boot) removeTaskLogs(id int64) error {
|
||||
if b.taskLogs == nil {
|
||||
return nil
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -28,6 +28,7 @@ const (
|
||||
ForgeService_SetStateLabel_FullMethodName = "/forge.v1.ForgeService/SetStateLabel"
|
||||
ForgeService_CreatePR_FullMethodName = "/forge.v1.ForgeService/CreatePR"
|
||||
ForgeService_PRState_FullMethodName = "/forge.v1.ForgeService/PRState"
|
||||
ForgeService_ClosePR_FullMethodName = "/forge.v1.ForgeService/ClosePR"
|
||||
ForgeService_GetPullRequest_FullMethodName = "/forge.v1.ForgeService/GetPullRequest"
|
||||
ForgeService_GetPullRequestDiff_FullMethodName = "/forge.v1.ForgeService/GetPullRequestDiff"
|
||||
ForgeService_ListReviews_FullMethodName = "/forge.v1.ForgeService/ListReviews"
|
||||
@@ -63,6 +64,7 @@ type ForgeServiceClient interface {
|
||||
SetStateLabel(ctx context.Context, in *SetStateLabelRequest, opts ...grpc.CallOption) (*SetStateLabelResponse, error)
|
||||
CreatePR(ctx context.Context, in *CreatePRRequest, opts ...grpc.CallOption) (*CreatePRResponse, error)
|
||||
PRState(ctx context.Context, in *PRStateRequest, opts ...grpc.CallOption) (*PRStateResponse, error)
|
||||
ClosePR(ctx context.Context, in *ClosePRRequest, opts ...grpc.CallOption) (*ClosePRResponse, error)
|
||||
GetPullRequest(ctx context.Context, in *GetPullRequestRequest, opts ...grpc.CallOption) (*GetPullRequestResponse, error)
|
||||
GetPullRequestDiff(ctx context.Context, in *GetPullRequestDiffRequest, opts ...grpc.CallOption) (*GetPullRequestDiffResponse, error)
|
||||
ListReviews(ctx context.Context, in *ListReviewsRequest, opts ...grpc.CallOption) (*ListReviewsResponse, error)
|
||||
@@ -178,6 +180,16 @@ func (c *forgeServiceClient) PRState(ctx context.Context, in *PRStateRequest, op
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *forgeServiceClient) ClosePR(ctx context.Context, in *ClosePRRequest, opts ...grpc.CallOption) (*ClosePRResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(ClosePRResponse)
|
||||
err := c.cc.Invoke(ctx, ForgeService_ClosePR_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *forgeServiceClient) GetPullRequest(ctx context.Context, in *GetPullRequestRequest, opts ...grpc.CallOption) (*GetPullRequestResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(GetPullRequestResponse)
|
||||
@@ -319,6 +331,7 @@ type ForgeServiceServer interface {
|
||||
SetStateLabel(context.Context, *SetStateLabelRequest) (*SetStateLabelResponse, error)
|
||||
CreatePR(context.Context, *CreatePRRequest) (*CreatePRResponse, error)
|
||||
PRState(context.Context, *PRStateRequest) (*PRStateResponse, error)
|
||||
ClosePR(context.Context, *ClosePRRequest) (*ClosePRResponse, error)
|
||||
GetPullRequest(context.Context, *GetPullRequestRequest) (*GetPullRequestResponse, error)
|
||||
GetPullRequestDiff(context.Context, *GetPullRequestDiffRequest) (*GetPullRequestDiffResponse, error)
|
||||
ListReviews(context.Context, *ListReviewsRequest) (*ListReviewsResponse, error)
|
||||
@@ -371,6 +384,9 @@ func (UnimplementedForgeServiceServer) CreatePR(context.Context, *CreatePRReques
|
||||
func (UnimplementedForgeServiceServer) PRState(context.Context, *PRStateRequest) (*PRStateResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method PRState not implemented")
|
||||
}
|
||||
func (UnimplementedForgeServiceServer) ClosePR(context.Context, *ClosePRRequest) (*ClosePRResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method ClosePR not implemented")
|
||||
}
|
||||
func (UnimplementedForgeServiceServer) GetPullRequest(context.Context, *GetPullRequestRequest) (*GetPullRequestResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method GetPullRequest not implemented")
|
||||
}
|
||||
@@ -587,6 +603,24 @@ func _ForgeService_PRState_Handler(srv interface{}, ctx context.Context, dec fun
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _ForgeService_ClosePR_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(ClosePRRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(ForgeServiceServer).ClosePR(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: ForgeService_ClosePR_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(ForgeServiceServer).ClosePR(ctx, req.(*ClosePRRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _ForgeService_GetPullRequest_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetPullRequestRequest)
|
||||
if err := dec(in); err != nil {
|
||||
@@ -821,6 +855,10 @@ var ForgeService_ServiceDesc = grpc.ServiceDesc{
|
||||
MethodName: "PRState",
|
||||
Handler: _ForgeService_PRState_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "ClosePR",
|
||||
Handler: _ForgeService_ClosePR_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetPullRequest",
|
||||
Handler: _ForgeService_GetPullRequest_Handler,
|
||||
|
||||
@@ -26,7 +26,10 @@ type Task struct {
|
||||
ID int64
|
||||
Owner, Repo, Identity, Status, ParkReason string
|
||||
IssueNumber, RetryCount int
|
||||
ForgeBacked bool
|
||||
// PRNumber is the open pull request a pr_open task carries; rejecting the
|
||||
// task closes it.
|
||||
PRNumber int
|
||||
ForgeBacked bool
|
||||
// Branch is the branch this task already pushed, if any. A retry that
|
||||
// continues pushed work resumes it, so the service refuses that mode for a
|
||||
// task with no branch rather than queueing a resume that cannot land.
|
||||
@@ -196,6 +199,7 @@ type Service struct {
|
||||
MaxRetries func(*Task) int
|
||||
CancelTask func(int64) bool
|
||||
CloseIssue func(context.Context, string, string, int, string) error
|
||||
ClosePR func(context.Context, string, string, int, string) error
|
||||
RemoveLogs func(int64) error
|
||||
// RemoveWorktree discards the task's clone on archive, the reap path for a
|
||||
// worktree terminal cleanup kept because it may hold uncaptured work.
|
||||
@@ -225,6 +229,9 @@ func (s Service) Apply(ctx context.Context, scope *string, actor Actor, id int64
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if mutation.verb != "" && task.Status == taskstate.PROpen && task.PRNumber > 0 {
|
||||
s.closeRejectedPR(ctx, task, actor, mutation.verb)
|
||||
}
|
||||
if mutation.verb != "" && task.ForgeBacked {
|
||||
s.closeRejectedIssue(ctx, task, actor, mutation.verb)
|
||||
}
|
||||
@@ -513,6 +520,25 @@ func (s Service) applyArchive(ctx context.Context, task *Task, actor Actor, o ou
|
||||
return o, nil
|
||||
}
|
||||
|
||||
// closeRejectedPR closes a rejected task's pull request and records it on the
|
||||
// task's timeline. A failure is logged and leaves the PR open.
|
||||
func (s Service) closeRejectedPR(ctx context.Context, task *Task, actor Actor, verb string) {
|
||||
if s.ClosePR == nil {
|
||||
s.warn("task rejected but no forge is wired; its pull request stays open", "task", task.ID)
|
||||
return
|
||||
}
|
||||
comment := fmt.Sprintf("Closing: this task was %s in archie (%s).", verb, actor.describe("actioned"))
|
||||
if err := s.ClosePR(ctx, task.Owner, task.Repo, task.PRNumber, comment); err != nil {
|
||||
s.warn("closing rejected task's pull request failed; it stays open", "task", task.ID, "pr", task.PRNumber, "err", err)
|
||||
return
|
||||
}
|
||||
s.emit(ctx, events.Event{
|
||||
TaskID: task.ID, Kind: events.KindPRClosed,
|
||||
Detail: fmt.Sprintf("closed pull request #%d", task.PRNumber),
|
||||
Data: map[string]any{"pr_number": task.PRNumber},
|
||||
})
|
||||
}
|
||||
|
||||
func (s Service) closeRejectedIssue(ctx context.Context, task *Task, actor Actor, verb string) {
|
||||
if s.CloseIssue == nil {
|
||||
s.warn("task rejected but no forge is wired; the issue stays open and will be re-polled", "task", task.ID)
|
||||
|
||||
@@ -26,7 +26,9 @@ const (
|
||||
KindOutcome = "outcome" // data: status, detail
|
||||
KindPRMerged = "pr_merged"
|
||||
KindPRRejected = "pr_rejected"
|
||||
KindTaskDead = "task_dead"
|
||||
// KindPRClosed records archie closing a task's PR when the task is rejected.
|
||||
KindPRClosed = "pr_closed" // data: pr_number
|
||||
KindTaskDead = "task_dead"
|
||||
// Operator actions taken from the dashboard. Without these an
|
||||
// intervention is recorded in the tasks table but invisible on the
|
||||
// timeline and the activity stream -- the one kind of event most worth
|
||||
|
||||
@@ -57,6 +57,10 @@ type PullRequestForge interface {
|
||||
|
||||
// PRState returns "open", "merged", or "closed" for a PR.
|
||||
PRState(ctx context.Context, owner, repo string, number int) (string, error)
|
||||
|
||||
// ClosePR closes a pull request without merging, with an optional final
|
||||
// comment.
|
||||
ClosePR(ctx context.Context, owner, repo string, number int, comment string) error
|
||||
}
|
||||
|
||||
// PullRequest is a forge-neutral summary of an existing pull request,
|
||||
|
||||
@@ -45,6 +45,10 @@ func (n *NoopForge) CreatePR(ctx context.Context, owner, repo, title, head, base
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (n *NoopForge) ClosePR(ctx context.Context, owner, repo string, number int, comment string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NoopForge) PRState(ctx context.Context, owner, repo string, number int) (string, error) {
|
||||
return "closed", nil
|
||||
}
|
||||
|
||||
@@ -206,6 +206,13 @@ func (f *Forge) CreatePR(ctx context.Context, owner, repo, title, head, base, bo
|
||||
return int(resp.GetNumber()), err
|
||||
}
|
||||
|
||||
func (f *Forge) ClosePR(ctx context.Context, owner, repo string, number int, comment string) error {
|
||||
_, err := call(ctx, f, "close pull request", func(c forgev1.ForgeServiceClient) (*forgev1.ClosePRResponse, error) {
|
||||
return c.ClosePR(ctx, &forgev1.ClosePRRequest{Repo: ref(owner, repo), Number: int32(number), Comment: comment}) //nolint:gosec // PR numbers fit int32
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (f *Forge) PRState(ctx context.Context, owner, repo string, number int) (string, error) {
|
||||
resp, err := call(ctx, f, "pull request state", func(c forgev1.ForgeServiceClient) (*forgev1.PRStateResponse, error) {
|
||||
return c.PRState(ctx, &forgev1.PRStateRequest{Repo: ref(owner, repo), Number: int32(number)}) //nolint:gosec // issue numbers fit int32
|
||||
|
||||
@@ -16,6 +16,7 @@ func NewService(
|
||||
maxRetries func(*taskactions.Task) int,
|
||||
cancel func(int64) bool,
|
||||
closeIssue func(context.Context, string, string, int, string) error,
|
||||
closePR func(context.Context, string, string, int, string) error,
|
||||
removeLogs func(int64) error,
|
||||
removeWorktree func(*taskactions.Task) error,
|
||||
publish func(events.Event),
|
||||
@@ -26,6 +27,7 @@ func NewService(
|
||||
MaxRetries: maxRetries,
|
||||
CancelTask: cancel,
|
||||
CloseIssue: closeIssue,
|
||||
ClosePR: closePR,
|
||||
RemoveLogs: removeLogs,
|
||||
RemoveWorktree: removeWorktree,
|
||||
Publish: publish,
|
||||
|
||||
@@ -20,7 +20,7 @@ func (s Store) TaskByID(ctx context.Context, id int64) (*taskactions.Task, error
|
||||
}
|
||||
// A pinned definition that no longer parses offers no resume points.
|
||||
stages, _ := workflow.StageNames(t.WorkflowDefinitionYAML)
|
||||
return &taskactions.Task{ID: t.ID, Owner: t.Owner, Repo: t.Repo, Identity: t.Identity, Status: t.Status, ParkReason: t.ParkReason, IssueNumber: t.IssueNumber, RetryCount: t.RetryCount, Attempt: t.Attempt, Branch: t.Branch, RetryMode: t.RetryMode, ForgeBacked: t.IsForgeBacked(), ReviewGate: t.ReviewGate, RereviewRounds: t.RereviewRounds, Stages: stages}, nil
|
||||
return &taskactions.Task{ID: t.ID, Owner: t.Owner, Repo: t.Repo, Identity: t.Identity, Status: t.Status, ParkReason: t.ParkReason, IssueNumber: t.IssueNumber, PRNumber: t.PRNumber, RetryCount: t.RetryCount, Attempt: t.Attempt, Branch: t.Branch, RetryMode: t.RetryMode, ForgeBacked: t.IsForgeBacked(), ReviewGate: t.ReviewGate, RereviewRounds: t.RereviewRounds, Stages: stages}, nil
|
||||
}
|
||||
|
||||
// CancelExecution delegates to the store.
|
||||
|
||||
Reference in new issue
Block a user