fix(forge): return SetStateLabel failures to the caller
A failed state-label change was dropped at three points: the forge.Forge interface returned nothing, forgeext logged the extension's gRPC error instead of returning it, and the forgerpc client and server discarded the response error. SetStateLabel now returns an error on the interface and on NoopForge, forgeext propagates the extension's error, and the forgerpc server carries it in the response envelope while the client returns it, matching the other forgerpc handlers. The proto is unchanged: a gRPC error rides the status and SetStateLabelResponse is already an empty message. TestSetStateLabelFailureReachesTheCaller pins the full chain: an extension whose SetStateLabel RPC fails, through forgeext and the forgerpc client/server, to the caller receiving the error. Refs #280
This commit is contained in:
1 parent
0a1aa8b3c2
commit
57865b51f4
5 files changed
+154
-14
No files matched your search
@@ -46,8 +46,9 @@ type IssueForge interface {
|
||||
|
||||
// SetStateLabel makes label the issue's only state label, removing any
|
||||
// other label that appears in knownLabels first. An empty label clears
|
||||
// all state labels (terminal states).
|
||||
SetStateLabel(ctx context.Context, owner, repo string, number int, label string, knownLabels []string)
|
||||
// all state labels (terminal states). A failed label change is returned
|
||||
// so the caller can decide what to do about it.
|
||||
SetStateLabel(ctx context.Context, owner, repo string, number int, label string, knownLabels []string) error
|
||||
}
|
||||
|
||||
// PullRequestForge is pull-request-level operations.
|
||||
|
||||
@@ -38,7 +38,8 @@ func (n *NoopForge) React(ctx context.Context, owner, repo string, number int, r
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NoopForge) SetStateLabel(ctx context.Context, owner, repo string, number int, label string, knownLabels []string) {
|
||||
func (n *NoopForge) SetStateLabel(ctx context.Context, owner, repo string, number int, label string, knownLabels []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NoopForge) CreatePR(ctx context.Context, owner, repo, title, head, base, body string) (int, error) {
|
||||
|
||||
@@ -350,8 +350,8 @@ func (s *Server) handleSetStateLabel(ctx context.Context, identity string, msg *
|
||||
s.respond(msg, Response{Envelope: natsrpc.NewEnvelope(err)})
|
||||
return
|
||||
}
|
||||
client.SetStateLabel(ctx, req.Owner, req.Repo, req.Number, req.Label, req.KnownLabels)
|
||||
s.respond(msg, Response{})
|
||||
err = client.SetStateLabel(ctx, req.Owner, req.Repo, req.Number, req.Label, req.KnownLabels)
|
||||
s.respond(msg, Response{Envelope: natsrpc.NewEnvelope(err)})
|
||||
}
|
||||
|
||||
func (s *Server) respond(msg *nats.Msg, v any) {
|
||||
@@ -424,9 +424,13 @@ func (c *Client) LinkBranch(ctx context.Context, owner, repo string, issueNumber
|
||||
|
||||
// SetStateLabel remains available for RPC compatibility with older workers;
|
||||
// the current workflow deliberately never calls it.
|
||||
func (c *Client) SetStateLabel(ctx context.Context, owner, repo string, number int, label string, knownLabels []string) {
|
||||
func (c *Client) SetStateLabel(ctx context.Context, owner, repo string, number int, label string, knownLabels []string) error {
|
||||
req := SetStateLabelRequest{Target: c.target(owner, repo), Number: number, Label: label, KnownLabels: knownLabels}
|
||||
_, _ = natsrpc.Call[Response](ctx, c.rpc(), c.subject(SubjectSetStateLabel), req)
|
||||
resp, err := natsrpc.Call[Response](ctx, c.rpc(), c.subject(SubjectSetStateLabel), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return resp.Err()
|
||||
}
|
||||
|
||||
// CreateReviewComments submits one review on the PR through the daemon.
|
||||
|
||||
@@ -48,12 +48,21 @@ type Instance struct {
|
||||
Settings map[string]string
|
||||
}
|
||||
|
||||
// extensionHost is the process supervision a Forge drives: starting the
|
||||
// extension, stopping it, and reporting whether it is alive. It is an
|
||||
// interface so a test can serve an extension in process.
|
||||
type extensionHost interface {
|
||||
Start(ctx context.Context, spec extension.Spec, surface string, impl goplugin.Plugin) (any, error)
|
||||
Stop(name string)
|
||||
Alive(name string) bool
|
||||
}
|
||||
|
||||
// Forge calls a forge extension. It owns its process: a call that finds the
|
||||
// process dead starts it again and retries once, and a process that cannot
|
||||
// start fails the call. Callers already retry and report a failing forge, so
|
||||
// nothing is dropped silently.
|
||||
type Forge struct {
|
||||
host *extension.Host
|
||||
host extensionHost
|
||||
inst Instance
|
||||
log *slog.Logger
|
||||
|
||||
@@ -200,15 +209,11 @@ func (f *Forge) React(ctx context.Context, owner, repo string, number int, react
|
||||
return err
|
||||
}
|
||||
|
||||
// SetStateLabel has no error to return; a failure is logged and the next
|
||||
// state change corrects the label.
|
||||
func (f *Forge) SetStateLabel(ctx context.Context, owner, repo string, number int, label string, knownLabels []string) {
|
||||
func (f *Forge) SetStateLabel(ctx context.Context, owner, repo string, number int, label string, knownLabels []string) error {
|
||||
_, err := call(ctx, f, "set state label", func(c forgev1.ForgeServiceClient) (*forgev1.SetStateLabelResponse, error) {
|
||||
return c.SetStateLabel(ctx, &forgev1.SetStateLabelRequest{Repo: ref(owner, repo), Number: int32(number), Label: label, KnownLabels: knownLabels}) // issue numbers fit int32
|
||||
})
|
||||
if err != nil {
|
||||
f.log.WarnContext(ctx, "set state label", "owner", owner, "repo", repo, "number", number, "err", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (f *Forge) CreatePR(ctx context.Context, owner, repo, title, head, base, body string) (int, error) {
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package forgeext
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
goplugin "github.com/hashicorp/go-plugin"
|
||||
natsserver "github.com/nats-io/nats-server/v2/server"
|
||||
"github.com/nats-io/nats.go"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
forgev1 "github.com/samcharles93/archie-core/internal/contracts/forge/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/forge"
|
||||
"github.com/samcharles93/archie-core/internal/forgerpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/extension"
|
||||
)
|
||||
|
||||
// failingLabelExtension is a forge extension whose state-label change always
|
||||
// fails, so the test can watch the failure travel to the host.
|
||||
type failingLabelExtension struct {
|
||||
forgev1.UnimplementedForgeServiceServer
|
||||
}
|
||||
|
||||
func (failingLabelExtension) Configure(context.Context, *forgev1.ConfigureRequest) (*forgev1.ConfigureResponse, error) {
|
||||
return &forgev1.ConfigureResponse{}, nil
|
||||
}
|
||||
|
||||
func (failingLabelExtension) SetStateLabel(context.Context, *forgev1.SetStateLabelRequest) (*forgev1.SetStateLabelResponse, error) {
|
||||
return nil, status.Error(codes.Internal, "label write failed")
|
||||
}
|
||||
|
||||
// stubHost stands in for the process host: Start hands back an
|
||||
// already-connected extension client instead of launching one.
|
||||
type stubHost struct {
|
||||
client forgev1.ForgeServiceClient
|
||||
}
|
||||
|
||||
func (h stubHost) Start(context.Context, extension.Spec, string, goplugin.Plugin) (any, error) {
|
||||
return h.client, nil
|
||||
}
|
||||
|
||||
func (stubHost) Stop(string) {}
|
||||
func (stubHost) Alive(string) bool { return true }
|
||||
|
||||
type runsByToken map[string]*workflow.Task
|
||||
|
||||
func (r runsByToken) TaskForCredential(_ context.Context, token string) (*workflow.Task, error) {
|
||||
if task, ok := r[token]; ok {
|
||||
return task, nil
|
||||
}
|
||||
return nil, errors.New("unknown")
|
||||
}
|
||||
|
||||
// TestSetStateLabelFailureReachesTheCaller pins the whole chain: an
|
||||
// extension whose SetStateLabel RPC fails, through the forgeext adapter and
|
||||
// the forgerpc server, to the forgerpc client's caller receiving the error.
|
||||
func TestSetStateLabelFailureReachesTheCaller(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
|
||||
// The extension: a gRPC server whose state-label change fails.
|
||||
lc := net.ListenConfig{}
|
||||
ln, err := lc.Listen(ctx, "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
gs := grpc.NewServer()
|
||||
forgev1.RegisterForgeServiceServer(gs, failingLabelExtension{})
|
||||
go func() { _ = gs.Serve(ln) }()
|
||||
t.Cleanup(gs.Stop)
|
||||
conn, err := grpc.NewClient(ln.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = conn.Close() })
|
||||
|
||||
f := &Forge{
|
||||
host: stubHost{client: forgev1.NewForgeServiceClient(conn)},
|
||||
inst: Instance{Spec: extension.Spec{Name: "stub"}},
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
}
|
||||
|
||||
// The daemon: forgerpc served over an embedded broker, resolving runs to
|
||||
// the extension above.
|
||||
ns, err := natsserver.NewServer(&natsserver.Options{Host: "127.0.0.1", Port: -1, NoLog: true, NoSigs: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ns.Start()
|
||||
if !ns.ReadyForConnections(10 * time.Second) {
|
||||
t.Fatal("embedded nats server did not become ready")
|
||||
}
|
||||
t.Cleanup(ns.Shutdown)
|
||||
nc, err := nats.Connect(ns.ClientURL())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(nc.Close)
|
||||
|
||||
fs := &forgerpc.Server{
|
||||
Runs: runsByToken{"live": {Identity: "bot", Owner: "acme", Repo: "app"}},
|
||||
Resolve: func(context.Context, *workflow.Task) (forge.Forge, error) {
|
||||
return f, nil
|
||||
},
|
||||
Log: slog.New(slog.DiscardHandler),
|
||||
}
|
||||
unsubscribe, err := fs.RegisterFor(nc, "bot")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(unsubscribe)
|
||||
|
||||
client := &forgerpc.Client{Conn: nc, Identity: "bot", Credential: "live", Timeout: 5 * time.Second}
|
||||
err = client.SetStateLabel(ctx, "acme", "app", 7, "in-progress", []string{"in-progress", "blocked"})
|
||||
if err == nil {
|
||||
t.Fatal("SetStateLabel succeeded; the extension's failure must reach the caller")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "label write failed") {
|
||||
t.Fatalf("SetStateLabel error = %q, want the extension's failure", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user