diff --git a/api/cmd/server/main.go b/api/cmd/server/main.go index ea30fca970..d97d718416 100644 --- a/api/cmd/server/main.go +++ b/api/cmd/server/main.go @@ -19,6 +19,7 @@ import ( "time" "github.com/google/uuid" + "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgxpool" @@ -34,6 +35,8 @@ import ( "github.com/vtmocanu/uzi/api/internal/healthsvc" "github.com/vtmocanu/uzi/api/internal/hostedsvc" "github.com/vtmocanu/uzi/api/internal/hub" + "github.com/vtmocanu/uzi/api/internal/issuetracker" + "github.com/vtmocanu/uzi/api/internal/issuetrackersvc" mw "github.com/vtmocanu/uzi/api/internal/middleware" "github.com/vtmocanu/uzi/api/internal/notifysvc" "github.com/vtmocanu/uzi/api/internal/oidc" @@ -1001,6 +1004,20 @@ func run() error { wsvc.SetRepoGuard(pcheck) h := handler.New(pool, q, cfg, box, svc, wsvc, pcheck, liveHub, settingsCache) + // External issue tracker connections and the repo link (PRD #4173 M4). The link and + // the reconnect rule run in transactions on the pool. An empty allowlist + // (UZI_ISSUE_TRACKER_ALLOWED_BASE_URLS) still boots: Connect then refuses every URL. + itSvc := issuetrackersvc.New(q, box, cfg.IssueTrackerBaseURLAllowed, issuetracker.New). + UseTransactions(pool, func(tx pgx.Tx) issuetrackersvc.Querier { return q.WithTx(tx) }) + h.SetIssueTrackerService(itSvc) + // The same service is forgesvc's driver source for a linked repo's issue side (DL-5); + // without it a linked repo fails closed. + svc.SetIssueTrackerDrivers(itSvc) + // DL-5: the services M2 left on their own forge factories resolve a linked repo's + // issue side through forgesvc too; each fails closed for a linked repo without it. + // provisionsvc needs none: enabling a linked repo only records column rows. + wsvc.SetIssueSources(svc) + lifecycle.SetIssueSources(svc) svc.SetMergedPredecessorRecovery(q, h.RecoveryService()) var bgWG sync.WaitGroup @@ -1055,6 +1072,7 @@ func run() error { // schedule that came due while the api was down fire promptly after a restart. if cfg.SchedulerCheckInterval > 0 { scheduler := schedsvc.New(q, wsvc, svc, settingsCache, notifier, vlt, cfg.SchedulerCheckInterval, slog.Default()) + scheduler.SetIssueSources(svc) scheduler.SetCIRepair(schedsvc.CIRepairConfig{MaxJobs: cfg.CIFixMaxJobs, LogTailBytes: cfg.CIFixLogTailBytes, MaxAttempts: cfg.CIAutofixMaxAttempts, ConfigPaths: cfg.CIAutofixConfigPaths, ArtifactMaxFiles: cfg.CIArtifactMaxFiles, ArtifactMaxBytes: cfg.CIArtifactMaxBytes}) // Admin-health loop-beat: the scheduler is one of the four loops (PRD #1484 M2, D15). // Registered ONLY inside this started-block, so a not-started scheduler @@ -1888,6 +1906,8 @@ func (g gateSubmitter) ConfirmProposalForUser(ctx context.Context, userID, runID return slacksvc.CreatedIssue{}, slacksvc.ErrChatProposalHandled case errors.Is(err, workersvc.ErrProposalNotFound): return slacksvc.CreatedIssue{}, slacksvc.ErrChatProposalGone + case errors.Is(err, workersvc.ErrRepoNeverEnabled): + return slacksvc.CreatedIssue{}, slacksvc.ErrChatProposalRepoNeverEnabled case errors.Is(err, workersvc.ErrProposalRepoGone), errors.Is(err, workersvc.ErrForgeBuild), errors.Is(err, workersvc.ErrForgeIssueWrite), @@ -2006,6 +2026,7 @@ func steerRunCardMessage(err error) string { func isInternalStartRunErr(err error) bool { switch { case errors.Is(err, workersvc.ErrRepoNotFound), errors.Is(err, workersvc.ErrIssueNotFound), + errors.Is(err, workersvc.ErrRepoNeverEnabled), errors.Is(err, workersvc.ErrNotPRDIssue), errors.Is(err, workersvc.ErrActiveRunExists), errors.Is(err, workersvc.ErrBranchInUse), errors.Is(err, workersvc.ErrDescriptionTooLarge), errors.Is(err, workersvc.ErrForgeIssueRead): @@ -2022,6 +2043,8 @@ func startRunCardMessage(err error) string { switch { case errors.Is(err, workersvc.ErrRepoNotFound): return "That repo isn't yours, or it no longer exists." + case errors.Is(err, workersvc.ErrRepoNeverEnabled): + return "That repo has never been enabled; enable it in uzi first." case errors.Is(err, workersvc.ErrIssueNotFound): return "That issue isn't on this repo's board." case errors.Is(err, workersvc.ErrNotPRDIssue): diff --git a/api/internal/forge/forgetest/golden.go b/api/internal/forge/forgetest/golden.go new file mode 100644 index 0000000000..875b9005db --- /dev/null +++ b/api/internal/forge/forgetest/golden.go @@ -0,0 +1,41 @@ +package forgetest + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// UpdateGoldenEnv, when set to 1, makes AssertGolden rewrite the golden file +// instead of comparing against it. +const UpdateGoldenEnv = "UZI_UPDATE_GOLDEN" + +// AssertGolden compares lines (one recorded forge call per line) with +// testdata/ in the calling test's package directory. With +// UZI_UPDATE_GOLDEN=1 it writes the file instead. A missing golden file fails +// the test and names the variable, so a new golden is never created silently. +func AssertGolden(t testing.TB, name string, lines []string) { + t.Helper() + path := filepath.Join("testdata", name) + got := strings.Join(lines, "\n") + if len(lines) > 0 { + got += "\n" + } + if os.Getenv(UpdateGoldenEnv) == "1" { + if err := os.MkdirAll(filepath.Dir(path), 0o750); err != nil { + t.Fatalf("golden %s: mkdir: %v", path, err) + } + if err := os.WriteFile(path, []byte(got), 0o600); err != nil { + t.Fatalf("golden %s: write: %v", path, err) + } + return + } + want, err := os.ReadFile(path) //nolint:gosec // G304: a golden file under the calling test package testdata dir + if err != nil { + t.Fatalf("golden %s unreadable (%v); generate it with %s=1", path, err, UpdateGoldenEnv) + } + if string(want) != got { + t.Fatalf("forge calls differ from golden %s (regenerate only if the change is intended, with %s=1)\n--- want\n%s--- got\n%s", path, UpdateGoldenEnv, want, got) + } +} diff --git a/api/internal/forge/forgetest/recorder.go b/api/internal/forge/forgetest/recorder.go new file mode 100644 index 0000000000..b16a6e8cfc --- /dev/null +++ b/api/internal/forge/forgetest/recorder.go @@ -0,0 +1,370 @@ +package forgetest + +import ( + "context" + "fmt" + "reflect" + "sort" + "strings" + "sync" + "time" + + "github.com/vtmocanu/uzi/api/internal/forge" +) + +// Recorder wraps a forge.Forge and appends one deterministic line per call: +// the method name plus its arguments, formatted by formatArg. It exists so a +// test can pin the exact set of forge calls a code path makes (a golden +// record, see AssertGolden) and notice when a later refactor changes it. +// Only calls are recorded, never results; the wrapped Forge supplies those. +// +// Safe for concurrent use. Calls() keeps call order. +// +// Recorder implements only forge.Forge. It hides any optional interface the +// wrapped value also implements (for example ProjectBoardSyncer), so code that +// type-asserts for one sees the Recorder as lacking it; a path that does so +// cannot be recorded through this wrapper. +type Recorder struct { + inner forge.Forge + + mu sync.Mutex // guards calls + calls []string +} + +var _ forge.Forge = (*Recorder)(nil) + +// NewRecorder wraps inner. A test usually passes a hand-written stub that +// embeds BaseFake and overrides the methods the path under test reads. +func NewRecorder(inner forge.Forge) *Recorder { return &Recorder{inner: inner} } + +// Calls returns a copy of the recorded lines in call order. +func (r *Recorder) Calls() []string { + r.mu.Lock() + defer r.mu.Unlock() + return append([]string(nil), r.calls...) +} + +func (r *Recorder) record(method string, args ...any) { + parts := make([]string, len(args)) + for i, a := range args { + parts[i] = formatArg(reflect.ValueOf(a)) + } + line := method + "(" + strings.Join(parts, ", ") + ")" + r.mu.Lock() + r.calls = append(r.calls, line) + r.mu.Unlock() +} + +var timeType = reflect.TypeOf(time.Time{}) + +// formatArg renders v deterministically: times as RFC3339 UTC (zero time as +// ""), pointers dereferenced, structs as {Field:value ...} in field +// order, slices as [a b] (a nil slice or map as "nil", so it differs from an +// empty one), maps with sorted keys, strings quoted. A value it cannot render +// stably (unexported field, func, chan) becomes "" rather +// than panicking. +func formatArg(v reflect.Value) string { + if !v.IsValid() { + return "" + } + if !v.CanInterface() { + // Unexported struct field: reflect refuses Interface() on it. + return "" + } + if v.Type() == timeType { + t := v.Interface().(time.Time) + if t.IsZero() { + return "" + } + return t.UTC().Format(time.RFC3339Nano) + } + switch v.Kind() { + case reflect.Pointer, reflect.Interface: + if v.IsNil() { + return "" + } + return formatArg(v.Elem()) + case reflect.String: + return fmt.Sprintf("%q", v.String()) + case reflect.Struct: + var b strings.Builder + b.WriteString("{") + for i := 0; i < v.NumField(); i++ { + if i > 0 { + b.WriteString(" ") + } + b.WriteString(v.Type().Field(i).Name + ":" + formatArg(v.Field(i))) + } + b.WriteString("}") + return b.String() + case reflect.Slice, reflect.Array: + if v.Kind() == reflect.Slice && v.IsNil() { + return "nil" + } + parts := make([]string, v.Len()) + for i := range parts { + parts[i] = formatArg(v.Index(i)) + } + return "[" + strings.Join(parts, " ") + "]" + case reflect.Map: + if v.IsNil() { + return "nil" + } + type kv struct{ k, v string } + var kvs []kv + for it := v.MapRange(); it.Next(); { + kvs = append(kvs, kv{formatArg(it.Key()), formatArg(it.Value())}) + } + sort.Slice(kvs, func(i, j int) bool { return kvs[i].k < kvs[j].k }) + parts := make([]string, len(kvs)) + for i, e := range kvs { + parts[i] = e.k + ":" + e.v + } + return "map[" + strings.Join(parts, " ") + "]" + case reflect.Func, reflect.Chan, reflect.UnsafePointer, reflect.Uintptr: + // Printing these would expose an address. + return "" + default: + return fmt.Sprint(v.Interface()) + } +} + +// RepositoryAuthorEligibility records the call and delegates to the wrapped Forge. +func (r *Recorder) RepositoryAuthorEligibility(ctx context.Context, a0 int64, a1 int64) (forge.AuthorEligibility, error) { + r.record("RepositoryAuthorEligibility", a0, a1) + return r.inner.RepositoryAuthorEligibility(ctx, a0, a1) +} + +// VerifyToken records the call and delegates to the wrapped Forge. +func (r *Recorder) VerifyToken(ctx context.Context) (forge.BotIdentity, error) { + r.record("VerifyToken") + return r.inner.VerifyToken(ctx) +} + +// GetProject records the call and delegates to the wrapped Forge. +func (r *Recorder) GetProject(ctx context.Context, a0 int64) (forge.Project, error) { + r.record("GetProject", a0) + return r.inner.GetProject(ctx, a0) +} + +// ListProjects records the call and delegates to the wrapped Forge. +func (r *Recorder) ListProjects(ctx context.Context) ([]forge.Project, error) { + r.record("ListProjects") + return r.inner.ListProjects(ctx) +} + +// ListLabels records the call and delegates to the wrapped Forge. +func (r *Recorder) ListLabels(ctx context.Context, a0 int64) ([]forge.Label, error) { + r.record("ListLabels", a0) + return r.inner.ListLabels(ctx, a0) +} + +// EnsureLabels records the call and delegates to the wrapped Forge. +func (r *Recorder) EnsureLabels(ctx context.Context, a0 int64, a1 []forge.Label) error { + r.record("EnsureLabels", a0, a1) + return r.inner.EnsureLabels(ctx, a0, a1) +} + +// ListIssues records the call and delegates to the wrapped Forge. +func (r *Recorder) ListIssues(ctx context.Context, a0 int64, a1 forge.ListIssuesOptions) ([]forge.Issue, error) { + r.record("ListIssues", a0, a1) + return r.inner.ListIssues(ctx, a0, a1) +} + +// GetIssue records the call and delegates to the wrapped Forge. +func (r *Recorder) GetIssue(ctx context.Context, a0 int64, a1 int64) (forge.Issue, error) { + r.record("GetIssue", a0, a1) + return r.inner.GetIssue(ctx, a0, a1) +} + +// CreateIssue records the call and delegates to the wrapped Forge. +func (r *Recorder) CreateIssue(ctx context.Context, a0 int64, a1 string, a2 string, a3 []string) (forge.Issue, error) { + r.record("CreateIssue", a0, a1, a2, a3) + return r.inner.CreateIssue(ctx, a0, a1, a2, a3) +} + +// UpdateIssueLabels records the call and delegates to the wrapped Forge. +func (r *Recorder) UpdateIssueLabels(ctx context.Context, a0 int64, a1 int64, a2 []string, a3 []string) error { + r.record("UpdateIssueLabels", a0, a1, a2, a3) + return r.inner.UpdateIssueLabels(ctx, a0, a1, a2, a3) +} + +// UpdateIssueDescription records the call and delegates to the wrapped Forge. +func (r *Recorder) UpdateIssueDescription(ctx context.Context, a0 int64, a1 int64, a2 string) error { + r.record("UpdateIssueDescription", a0, a1, a2) + return r.inner.UpdateIssueDescription(ctx, a0, a1, a2) +} + +// SetIssueState records the call and delegates to the wrapped Forge. +func (r *Recorder) SetIssueState(ctx context.Context, a0 int64, a1 int64, a2 forge.IssueState) error { + r.record("SetIssueState", a0, a1, a2) + return r.inner.SetIssueState(ctx, a0, a1, a2) +} + +// UserExists records the call and delegates to the wrapped Forge. +func (r *Recorder) UserExists(ctx context.Context, a0 string) (bool, error) { + r.record("UserExists", a0) + return r.inner.UserExists(ctx, a0) +} + +// ListIssueLabelEvents records the call and delegates to the wrapped Forge. +func (r *Recorder) ListIssueLabelEvents(ctx context.Context, a0 int64, a1 int64) ([]forge.LabelEvent, error) { + r.record("ListIssueLabelEvents", a0, a1) + return r.inner.ListIssueLabelEvents(ctx, a0, a1) +} + +// ListIssueComments records the call and delegates to the wrapped Forge. +func (r *Recorder) ListIssueComments(ctx context.Context, a0 int64, a1 int64) ([]forge.IssueComment, error) { + r.record("ListIssueComments", a0, a1) + return r.inner.ListIssueComments(ctx, a0, a1) +} + +// CreateIssueNote records the call and delegates to the wrapped Forge. +func (r *Recorder) CreateIssueNote(ctx context.Context, a0 int64, a1 int64, a2 string) (forge.IssueNote, error) { + r.record("CreateIssueNote", a0, a1, a2) + return r.inner.CreateIssueNote(ctx, a0, a1, a2) +} + +// GetMergeRequest records the call and delegates to the wrapped Forge. +func (r *Recorder) GetMergeRequest(ctx context.Context, a0 int64, a1 int64) (forge.MergeRequest, error) { + r.record("GetMergeRequest", a0, a1) + return r.inner.GetMergeRequest(ctx, a0, a1) +} + +// ListMergeRequestComments records the call and delegates to the wrapped Forge. +func (r *Recorder) ListMergeRequestComments(ctx context.Context, a0 int64, a1 int64) ([]forge.MRComment, error) { + r.record("ListMergeRequestComments", a0, a1) + return r.inner.ListMergeRequestComments(ctx, a0, a1) +} + +// ReplyMergeRequestComment records the call and delegates to the wrapped Forge. +func (r *Recorder) ReplyMergeRequestComment(ctx context.Context, a0 int64, a1 int64, a2 string, a3 string) error { + r.record("ReplyMergeRequestComment", a0, a1, a2, a3) + return r.inner.ReplyMergeRequestComment(ctx, a0, a1, a2, a3) +} + +// ResolveMergeRequestThread records the call and delegates to the wrapped Forge. +func (r *Recorder) ResolveMergeRequestThread(ctx context.Context, a0 int64, a1 int64, a2 string) error { + r.record("ResolveMergeRequestThread", a0, a1, a2) + return r.inner.ResolveMergeRequestThread(ctx, a0, a1, a2) +} + +// TokenInfo records the call and delegates to the wrapped Forge. +func (r *Recorder) TokenInfo(ctx context.Context) (forge.TokenInfo, error) { + r.record("TokenInfo") + return r.inner.TokenInfo(ctx) +} + +// ProjectRole records the call and delegates to the wrapped Forge. +func (r *Recorder) ProjectRole(ctx context.Context, a0 int64, a1 int64) (forge.Role, bool, error) { + r.record("ProjectRole", a0, a1) + return r.inner.ProjectRole(ctx, a0, a1) +} + +// DefaultBranchProtection records the call and delegates to the wrapped Forge. +func (r *Recorder) DefaultBranchProtection(ctx context.Context, a0 int64, a1 string, a2 int64) (forge.BranchProtection, error) { + r.record("DefaultBranchProtection", a0, a1, a2) + return r.inner.DefaultBranchProtection(ctx, a0, a1, a2) +} + +// LatestPipeline records the call and delegates to the wrapped Forge. +func (r *Recorder) LatestPipeline(ctx context.Context, a0 int64, a1 string) (forge.Pipeline, error) { + r.record("LatestPipeline", a0, a1) + return r.inner.LatestPipeline(ctx, a0, a1) +} + +// LatestMRPipeline records the call and delegates to the wrapped Forge. +func (r *Recorder) LatestMRPipeline(ctx context.Context, a0 int64, a1 int64) (forge.Pipeline, error) { + r.record("LatestMRPipeline", a0, a1) + return r.inner.LatestMRPipeline(ctx, a0, a1) +} + +// ListPipelineJobs records the call and delegates to the wrapped Forge. +func (r *Recorder) ListPipelineJobs(ctx context.Context, a0 int64, a1 int64) ([]forge.Job, error) { + r.record("ListPipelineJobs", a0, a1) + return r.inner.ListPipelineJobs(ctx, a0, a1) +} + +// ListPipelineJobAttempts records the call and delegates to the wrapped Forge. +func (r *Recorder) ListPipelineJobAttempts(ctx context.Context, a0 int64, a1 int64) ([]forge.Job, error) { + r.record("ListPipelineJobAttempts", a0, a1) + return r.inner.ListPipelineJobAttempts(ctx, a0, a1) +} + +// JobLogTail records the call and delegates to the wrapped Forge. +func (r *Recorder) JobLogTail(ctx context.Context, a0 int64, a1 int64, a2 int) (string, error) { + r.record("JobLogTail", a0, a1, a2) + return r.inner.JobLogTail(ctx, a0, a1, a2) +} + +// ListMergeRequestRefs records the call and delegates to the wrapped Forge. +func (r *Recorder) ListMergeRequestRefs(ctx context.Context, a0 int64, a1 forge.ListMergeRequestsOptions) ([]forge.MergeRequestRef, error) { + r.record("ListMergeRequestRefs", a0, a1) + return r.inner.ListMergeRequestRefs(ctx, a0, a1) +} + +// GetMergeRequestSummary records the call and delegates to the wrapped Forge. +func (r *Recorder) GetMergeRequestSummary(ctx context.Context, a0 int64, a1 int64) (forge.MergeRequestSummary, error) { + r.record("GetMergeRequestSummary", a0, a1) + return r.inner.GetMergeRequestSummary(ctx, a0, a1) +} + +// ListChecks records the call and delegates to the wrapped Forge. +func (r *Recorder) ListChecks(ctx context.Context, a0 int64, a1 string) ([]forge.Check, error) { + r.record("ListChecks", a0, a1) + return r.inner.ListChecks(ctx, a0, a1) +} + +// ListWorkflowRuns records the call and delegates to the wrapped Forge. +func (r *Recorder) ListWorkflowRuns(ctx context.Context, a0 int64, a1 forge.ListWorkflowRunsOptions) ([]forge.WorkflowRun, error) { + r.record("ListWorkflowRuns", a0, a1) + return r.inner.ListWorkflowRuns(ctx, a0, a1) +} + +// ListRunArtifacts records the call and delegates to the wrapped Forge. +func (r *Recorder) ListRunArtifacts(ctx context.Context, a0 int64, a1 int64) ([]forge.Artifact, error) { + r.record("ListRunArtifacts", a0, a1) + return r.inner.ListRunArtifacts(ctx, a0, a1) +} + +// DownloadArtifact records the call and delegates to the wrapped Forge. +func (r *Recorder) DownloadArtifact(ctx context.Context, a0 int64, a1 int64, a2 int64) ([]byte, error) { + r.record("DownloadArtifact", a0, a1, a2) + return r.inner.DownloadArtifact(ctx, a0, a1, a2) +} + +// GetWorkflowRun records the call and delegates to the wrapped Forge. +func (r *Recorder) GetWorkflowRun(ctx context.Context, a0 int64, a1 int64) (forge.WorkflowRun, error) { + r.record("GetWorkflowRun", a0, a1) + return r.inner.GetWorkflowRun(ctx, a0, a1) +} + +// ListMergeRequestReviews records the call and delegates to the wrapped Forge. +func (r *Recorder) ListMergeRequestReviews(ctx context.Context, a0 int64, a1 int64) ([]forge.Review, error) { + r.record("ListMergeRequestReviews", a0, a1) + return r.inner.ListMergeRequestReviews(ctx, a0, a1) +} + +// ProjectCIConfigPath records the call and delegates to the wrapped Forge. +func (r *Recorder) ProjectCIConfigPath(ctx context.Context, a0 int64) (string, error) { + r.record("ProjectCIConfigPath", a0) + return r.inner.ProjectCIConfigPath(ctx, a0) +} + +// BranchHead records the call and delegates to the wrapped Forge. +func (r *Recorder) BranchHead(ctx context.Context, a0 int64, a1 string) (string, error) { + r.record("BranchHead", a0, a1) + return r.inner.BranchHead(ctx, a0, a1) +} + +// RefHead records the call and delegates to the wrapped Forge. +func (r *Recorder) RefHead(ctx context.Context, a0 int64, a1 string) (string, error) { + r.record("RefHead", a0, a1) + return r.inner.RefHead(ctx, a0, a1) +} + +// CompareAncestry records the call and delegates to the wrapped Forge. +func (r *Recorder) CompareAncestry(ctx context.Context, a0 int64, a1 string, a2 string) (forge.Ancestry, error) { + r.record("CompareAncestry", a0, a1, a2) + return r.inner.CompareAncestry(ctx, a0, a1, a2) +} diff --git a/api/internal/forge/forgetest/recorder_test.go b/api/internal/forge/forgetest/recorder_test.go new file mode 100644 index 0000000000..6ecb6f03bd --- /dev/null +++ b/api/internal/forge/forgetest/recorder_test.go @@ -0,0 +1,63 @@ +package forgetest + +import ( + "context" + "os" + "path/filepath" + "reflect" + "slices" + "testing" + "time" + + "github.com/vtmocanu/uzi/api/internal/forge" +) + +func TestRecorderFormatsArgumentsDeterministically(t *testing.T) { + rec := NewRecorder(&BaseFake{}) + ctx := context.Background() + after := time.Date(2026, 3, 4, 5, 6, 7, 0, time.FixedZone("x", 3600)) // 04:06:07 UTC + + _, _ = rec.ListIssues(ctx, 42, forge.ListIssuesOptions{Labels: []string{"uzi"}, UpdatedAfter: &after}) + _, _ = rec.ListIssues(ctx, 42, forge.ListIssuesOptions{Labels: []string{}}) + _ = rec.UpdateIssueLabels(ctx, 42, 7, []string{"a"}, nil) + + want := []string{ + `ListIssues(42, {Labels:["uzi"] UpdatedAfter:2026-03-04T04:06:07Z State:"" Limit:0})`, + `ListIssues(42, {Labels:[] UpdatedAfter: State:"" Limit:0})`, + `UpdateIssueLabels(42, 7, ["a"], nil)`, + } + if got := rec.Calls(); !slices.Equal(got, want) { + t.Fatalf("calls = %q, want %q", got, want) + } +} + +func TestRecorderFormatMapAndUnformattable(t *testing.T) { + type hidden struct{ n int } + if got, want := formatArg(reflectValue(hidden{3})), "{n:}"; got != want { + t.Fatalf("unexported field = %s, want %s", got, want) + } + if got := formatArg(reflectValue(func() {})); got != "" { + t.Fatalf("func = %s", got) + } + m := map[string]int{"b": 2, "a": 1} + for range 20 { + if got := formatArg(reflectValue(m)); got != `map["a":1 "b":2]` { + t.Fatalf("map format = %s", got) + } + } +} + +func TestAssertGoldenWritesThenMatches(t *testing.T) { + dir := t.TempDir() + t.Chdir(dir) + t.Setenv(UpdateGoldenEnv, "1") + AssertGolden(t, "x.golden", []string{"A()", "B(1)"}) + t.Setenv(UpdateGoldenEnv, "") + AssertGolden(t, "x.golden", []string{"A()", "B(1)"}) + b, err := os.ReadFile(filepath.Join(dir, "testdata", "x.golden")) //nolint:gosec // G304: the test temp dir + if err != nil || string(b) != "A()\nB(1)\n" { + t.Fatalf("golden = %q, %v", b, err) + } +} + +func reflectValue(v any) reflect.Value { return reflect.ValueOf(v) } diff --git a/api/internal/forgesvc/board_columns.go b/api/internal/forgesvc/board_columns.go index d09bf11fd2..4102f73f4f 100644 --- a/api/internal/forgesvc/board_columns.go +++ b/api/internal/forgesvc/board_columns.go @@ -29,6 +29,28 @@ type LabelSeedError struct{ Err error } func (e *LabelSeedError) Error() string { return e.Err.Error() } func (e *LabelSeedError) Unwrap() error { return e.Err } +// EnsureBoardColumnRows is the store-only half of EnsureBoardColumns, for a repo linked +// to an external issue tracker (PRD #4173 M4): it records the DefaultColumns rows when +// the repo has none and never calls an issue tracker, because uzi creates no labels or +// tags there (writes arrive in M5). Same one-statement insert, same idempotence. +func EnsureBoardColumnRows(ctx context.Context, q BoardColumnStore, repoID uuid.UUID) error { + count, err := q.CountBoardColumns(ctx, repoID) + if err != nil { + return fmt.Errorf("count board columns: %w", err) + } + if count > 0 { + return nil + } + names := make([]string, len(DefaultColumns)) + for i, col := range DefaultColumns { + names[i] = col.Name + } + if err := q.InsertDefaultBoardColumns(ctx, store.InsertDefaultBoardColumnsParams{RepoID: repoID, LabelNames: names}); err != nil { + return fmt.Errorf("insert default board columns: %w", err) + } + return nil +} + // EnsureBoardColumns gives a repo its board columns when it has none: it // ensures the DefaultColumns labels exist on the forge, then records them as // board_columns rows in DefaultColumns order. A repo that already has any diff --git a/api/internal/forgesvc/issue_write_guard.go b/api/internal/forgesvc/issue_write_guard.go new file mode 100644 index 0000000000..f796935ac0 --- /dev/null +++ b/api/internal/forgesvc/issue_write_guard.go @@ -0,0 +1,165 @@ +package forgesvc + +import ( + "context" + "errors" + "fmt" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/issuesource" +) + +// ErrIssueWriteNotOffered is returned by the write guard for every write to a +// linked external issue tracker: YouTrack writes arrive in PRD #4173 M5, so until +// then no request is sent (S1: uzi stays read-only against the tracker). +// +// It is the issuesource sentinel itself, so a package that cannot import forgesvc +// (workersvc) can wrap and match the same value. +var ErrIssueWriteNotOffered = issuesource.ErrIssueWriteNotOffered + +// linkedIssueError marks an *forge.IssueNotFoundError returned by a linked +// repo's issue reads. A forge not-found is never marked, so IssueWriteSkippable +// can tell "the linked issue moved or is gone" (D10: skip, never retry forever) +// from an ordinary forge not-found that existing callers handle on their own. +type linkedIssueError struct{ err error } + +func (e *linkedIssueError) Error() string { return e.err.Error() } +func (e *linkedIssueError) Unwrap() error { return e.err } + +// IssueWriteSkippable reports whether a caller doing issue-side bookkeeping +// should treat err as a permanent skip: the write is not offered for a linked +// repo, or the linked issue no longer exists where the repo's binding looks. +// A plain forge not-found is not skippable. +func IssueWriteSkippable(err error) bool { + if errors.Is(err, ErrIssueWriteNotOffered) { + return true + } + var marked *linkedIssueError + return errors.As(err, &marked) +} + +// issueWriteGuard wraps a linked repo's issue tracker driver. Reads pass +// through; every write is refused before any request. +type issueWriteGuard struct { + inner forge.IssueTracker +} + +var _ forge.IssueTracker = (*issueWriteGuard)(nil) + +func markNotFound(err error) error { + var missing *forge.IssueNotFoundError + if errors.As(err, &missing) { + return &linkedIssueError{err: err} + } + return err +} + +func (g *issueWriteGuard) ListLabels(ctx context.Context, projectID int64) ([]forge.Label, error) { + return g.inner.ListLabels(ctx, projectID) +} + +func (g *issueWriteGuard) ListIssues(ctx context.Context, projectID int64, opts forge.ListIssuesOptions) ([]forge.Issue, error) { + return g.inner.ListIssues(ctx, projectID, opts) +} + +func (g *issueWriteGuard) GetIssue(ctx context.Context, projectID, issueIID int64) (forge.Issue, error) { + iss, err := g.inner.GetIssue(ctx, projectID, issueIID) + return iss, markNotFound(err) +} + +func (g *issueWriteGuard) ListIssueLabelEvents(ctx context.Context, projectID, issueIID int64) ([]forge.LabelEvent, error) { + ev, err := g.inner.ListIssueLabelEvents(ctx, projectID, issueIID) + return ev, markNotFound(err) +} + +func (g *issueWriteGuard) ListIssueComments(ctx context.Context, projectID, issueIID int64) ([]forge.IssueComment, error) { + c, err := g.inner.ListIssueComments(ctx, projectID, issueIID) + return c, markNotFound(err) +} + +func (g *issueWriteGuard) refuse(op string) error { + return fmt.Errorf("%s: %w", op, ErrIssueWriteNotOffered) +} + +func (g *issueWriteGuard) EnsureLabels(context.Context, int64, []forge.Label) error { + return g.refuse("ensure labels") +} + +func (g *issueWriteGuard) CreateIssue(context.Context, int64, string, string, []string) (forge.Issue, error) { + return forge.Issue{}, g.refuse("create issue") +} + +func (g *issueWriteGuard) UpdateIssueLabels(context.Context, int64, int64, []string, []string) error { + return g.refuse("update issue labels") +} + +func (g *issueWriteGuard) UpdateIssueDescription(context.Context, int64, int64, string) error { + return g.refuse("update issue description") +} + +func (g *issueWriteGuard) SetIssueState(context.Context, int64, int64, forge.IssueState) error { + return g.refuse("set issue state") +} + +func (g *issueWriteGuard) CreateIssueNote(context.Context, int64, int64, string) (forge.IssueNote, error) { + return forge.IssueNote{}, g.refuse("create issue note") +} + +// UnavailableIssueTracker is the issue side handed to a linked repo whose resolver +// failed (for example a connection removed from the allowlist): every read returns +// cause, which is NOT skippable so no marker is settled over an outage, and every +// write is refused by the write guard. It lets the poller's issue-side steps fail +// or skip per candidate while the code-host steps carry on, instead of dereferencing +// a nil tracker. +func UnavailableIssueTracker(cause error) forge.IssueTracker { + return &issueWriteGuard{inner: unavailableIssueTracker{cause: cause}} +} + +type unavailableIssueTracker struct{ cause error } + +func (u unavailableIssueTracker) ListLabels(context.Context, int64) ([]forge.Label, error) { + return nil, u.cause +} +func (u unavailableIssueTracker) EnsureLabels(context.Context, int64, []forge.Label) error { + return u.cause +} +func (u unavailableIssueTracker) ListIssues(context.Context, int64, forge.ListIssuesOptions) ([]forge.Issue, error) { + return nil, u.cause +} +func (u unavailableIssueTracker) GetIssue(context.Context, int64, int64) (forge.Issue, error) { + return forge.Issue{}, u.cause +} +func (u unavailableIssueTracker) CreateIssue(context.Context, int64, string, string, []string) (forge.Issue, error) { + return forge.Issue{}, u.cause +} +func (u unavailableIssueTracker) UpdateIssueLabels(context.Context, int64, int64, []string, []string) error { + return u.cause +} +func (u unavailableIssueTracker) UpdateIssueDescription(context.Context, int64, int64, string) error { + return u.cause +} +func (u unavailableIssueTracker) SetIssueState(context.Context, int64, int64, forge.IssueState) error { + return u.cause +} +func (u unavailableIssueTracker) ListIssueLabelEvents(context.Context, int64, int64) ([]forge.LabelEvent, error) { + return nil, u.cause +} +func (u unavailableIssueTracker) ListIssueComments(context.Context, int64, int64) ([]forge.IssueComment, error) { + return nil, u.cause +} +func (u unavailableIssueTracker) CreateIssueNote(context.Context, int64, int64, string) (forge.IssueNote, error) { + return forge.IssueNote{}, u.cause +} + +// linkedIssueSource marks the guard (and so UnavailableIssueTracker) as a linked +// repo's issue side. A wrapper placed around a resolver's result must forward it, +// or SyncIssues will treat the wrapped tracker as a forge driver. +func (*issueWriteGuard) linkedIssueSource() {} + +// isLinkedTracker reports whether tr is a linked repo's issue side, the only thing +// SyncIssues dispatches on: the choice comes from the resolver's own output, never +// from a separate caller flag that could disagree with it. +func isLinkedTracker(tr forge.IssueTracker) bool { + _, ok := tr.(interface{ linkedIssueSource() }) + return ok +} diff --git a/api/internal/forgesvc/issue_write_guard_test.go b/api/internal/forgesvc/issue_write_guard_test.go new file mode 100644 index 0000000000..cf3b5b76d4 --- /dev/null +++ b/api/internal/forgesvc/issue_write_guard_test.go @@ -0,0 +1,174 @@ +package forgesvc + +import ( + "context" + "errors" + "fmt" + "testing" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/board" + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/store" +) + +func TestIssueWriteSkippableClassifier(t *testing.T) { + notFound := &forge.IssueNotFoundError{Err: errors.New("404")} + linked := &issueWriteGuard{inner: &stubDriverNotFound{err: notFound}} + _, markedErr := linked.GetIssue(context.Background(), 1, 2) + + tests := []struct { + name string + err error + want bool + }{ + {"nil", nil, false}, + {"write not offered", ErrIssueWriteNotOffered, true}, + {"write not offered, wrapped", fmt.Errorf("move: %w", ErrIssueWriteNotOffered), true}, + {"linked not found", markedErr, true}, + {"linked not found, wrapped", fmt.Errorf("patch: %w", markedErr), true}, + {"plain forge not found", notFound, false}, + {"plain forge not found, wrapped", fmt.Errorf("x: %w", notFound), false}, + {"other error", errors.New("boom"), false}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if got := IssueWriteSkippable(tc.err); got != tc.want { + t.Fatalf("IssueWriteSkippable(%v) = %v, want %v", tc.err, got, tc.want) + } + }) + } + var asNotFound *forge.IssueNotFoundError + if !errors.As(markedErr, &asNotFound) { + t.Fatal("the marked error must still unwrap to the forge not-found") + } +} + +// stubDriverNotFound answers every issue read with err. +type stubDriverNotFound struct { + fakeForge + err error +} + +func (s *stubDriverNotFound) GetIssue(context.Context, int64, int64) (forge.Issue, error) { + return forge.Issue{}, s.err +} +func (s *stubDriverNotFound) ListIssueComments(context.Context, int64, int64) ([]forge.IssueComment, error) { + return nil, s.err +} +func (s *stubDriverNotFound) ListIssueLabelEvents(context.Context, int64, int64) ([]forge.LabelEvent, error) { + return nil, s.err +} + +func TestGuardMarksOnlyNotFoundReads(t *testing.T) { + g := &issueWriteGuard{inner: &stubDriverNotFound{err: errors.New("503")}} + if _, err := g.GetIssue(context.Background(), 1, 2); IssueWriteSkippable(err) { + t.Fatalf("a transient read failure must not be skippable: %v", err) + } + g = &issueWriteGuard{inner: &stubDriverNotFound{err: &forge.IssueNotFoundError{Err: errors.New("404")}}} + if _, err := g.ListIssueComments(context.Background(), 1, 2); !IssueWriteSkippable(err) { + t.Fatalf("a not-found comment read must be skippable: %v", err) + } + if _, err := g.ListIssueLabelEvents(context.Background(), 1, 2); !IssueWriteSkippable(err) { + t.Fatalf("a not-found label event read must be skippable: %v", err) + } +} + +// A linked repo's card is never moved; the MR edge is still recorded (D10). +func TestGuardedMRMoveOnALinkedRepoRecordsTheMRState(t *testing.T) { + runID, repoID := uuid.New(), uuid.New() + st := &fakeStore{ + candidates: []store.ListMRWatchCandidatesRow{candidate(runID, 9, 13, mrTxt("opened"))}, + issue: mrIssue(repoID, 9, "opened", "PRD", board.ColumnHumanReview), + columns: mrCols(), + } + f := &fakeForge{mr: forgeMR(13, "closed")} + guarded := &issueWriteGuard{inner: f} + + if err := newTestService(st).SyncMRStates(context.Background(), repoID, testProjectID, f, guarded); err != nil { + t.Fatal(err) + } + assertNoMove(t, f) + assertRecorded(t, st, runID, "closed") +} + +// A plain (unlinked) forge failure on the same edge still leaves the edge. +func TestGuardedMRMoveForgeFailureStillDefers(t *testing.T) { + runID, repoID := uuid.New(), uuid.New() + st := &fakeStore{ + candidates: []store.ListMRWatchCandidatesRow{candidate(runID, 9, 13, mrTxt("opened"))}, + issue: mrIssue(repoID, 9, "opened", "PRD", board.ColumnHumanReview), + columns: mrCols(), + } + f := &fakeForge{mr: forgeMR(13, "closed"), updateErr: errors.New("forge down")} + if err := newTestService(st).SyncMRStates(context.Background(), repoID, testProjectID, f, f); err != nil { + t.Fatal(err) + } + assertNoRecord(t, st) +} + +// The PRD link patch settles its marker on a linked repo: the description write is +// not offered, so retrying it would never end. +func TestPRDLinkPatchSettlesWhenWritesAreNotOffered(t *testing.T) { + s, st, f, id := newPRDFixture(t, nil) + guarded := &issueWriteGuard{inner: f} + if err := s.SyncPRDLinkPatches(context.Background(), uuid.New(), fixProject, f, guarded); err != nil { + t.Fatal(err) + } + enumerated(t, st) + if len(f.descriptionUpdates) != 0 { + t.Fatalf("a request reached the tracker: %+v", f.descriptionUpdates) + } + if len(st.prdSettled) != 1 || st.prdSettled[0] != id { + t.Fatalf("settled = %v, want the run %v", st.prdSettled, id) + } +} + +func TestPRDLinkPatchSettlesWhenTheLinkedIssueIsGone(t *testing.T) { + s, st, f, id := newPRDFixture(t, nil) + gone := &stubDriverNotFound{fakeForge: *f, err: &forge.IssueNotFoundError{Err: errors.New("404")}} + guarded := &issueWriteGuard{inner: gone} + if err := s.SyncPRDLinkPatches(context.Background(), uuid.New(), fixProject, f, guarded); err != nil { + t.Fatal(err) + } + enumerated(t, st) + if len(st.prdSettled) != 1 || st.prdSettled[0] != id { + t.Fatalf("settled = %v, want the run %v", st.prdSettled, id) + } +} + +// A plain forge not-found on an unlinked repo keeps the marker, as before. +func TestPRDLinkPatchKeepsTheMarkerOnAPlainForgeNotFound(t *testing.T) { + s, st, f, _ := newPRDFixture(t, func(_ *store.ListPRDLinkPatchCandidatesRow, _ *fakeStore, f *fakeForge) { + f.getIssueErrByIID = map[int64]error{fixIssueIID: &forge.IssueNotFoundError{Err: errors.New("404")}} + }) + runPRDPatch(t, s, f) + enumerated(t, st) + if len(st.prdSettled) != 0 { + t.Fatalf("an unlinked repo's not-found must not settle: %v", st.prdSettled) + } +} + +// An unresolvable linked repo's issue side must never be nil and must not settle any +// marker: reads fail with the cause (not skippable), writes are refused. +func TestUnavailableIssueTrackerReadsFailAndWritesAreRefused(t *testing.T) { + cause := errors.New("tracker unavailable") + tr := UnavailableIssueTracker(cause) + ctx := context.Background() + if _, err := tr.GetIssue(ctx, 1, 2); !errors.Is(err, cause) || IssueWriteSkippable(err) { + t.Fatalf("GetIssue error = %v, want the cause and not skippable", err) + } + if _, err := tr.ListIssues(ctx, 1, forge.ListIssuesOptions{}); !errors.Is(err, cause) { + t.Fatalf("ListIssues error = %v", err) + } + if err := tr.UpdateIssueDescription(ctx, 1, 2, "d"); !IssueWriteSkippable(err) { + t.Fatalf("a write must be refused as not offered, got %v", err) + } + if !isLinkedTracker(tr) { + t.Fatal("the unavailable tracker must dispatch as linked") + } + if isLinkedTracker(&fakeForge{}) { + t.Fatal("a forge driver must not dispatch as linked") + } +} diff --git a/api/internal/forgesvc/linked_issue_sync.go b/api/internal/forgesvc/linked_issue_sync.go new file mode 100644 index 0000000000..92473a5de6 --- /dev/null +++ b/api/internal/forgesvc/linked_issue_sync.go @@ -0,0 +1,42 @@ +package forgesvc + +import ( + "context" + "errors" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" +) + +// ErrLinkedSyncNotReady is returned by the issue sync dispatchers for a repo linked +// to an external issue tracker until the linked intake lands (PRD #4173 M4 intake). +// It is a typed refusal, not a forge failure: callers log it and carry on with +// every code-host step, and never map it to a forge error class. +var ErrLinkedSyncNotReady = errors.New("linked issue sync is not available yet") + +// SyncIssues is the one entry point for a full issue sync into the cache. tr is the +// repo's issue side as IssueTrackerForRepo returned it: a linked repo's tracker runs +// the linked sync, which this milestone leaves as ErrLinkedSyncNotReady so the intake +// milestone replaces a single function; anything else is a forge driver and runs +// FullSync. The choice is derived from tr, so it cannot disagree with it. +func (s *Service) SyncIssues(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, tr forge.IssueTracker) (Marks, error) { + if isLinkedTracker(tr) { + return Marks{}, s.syncLinkedIssues(ctx, repoID) + } + return s.FullSync(ctx, repoID, forgeProjectID, tr) +} + +// SyncIssuesIncremental is SyncIssues for the poller's between-reconcile tick. +// For a linked repo the caller's marks come back unchanged with the typed error. +func (s *Service) SyncIssuesIncremental(ctx context.Context, repoID uuid.UUID, forgeProjectID int64, tr forge.IssueTracker, m Marks) (Marks, error) { + if isLinkedTracker(tr) { + return m, s.syncLinkedIssues(ctx, repoID) + } + return s.IncrementalSync(ctx, repoID, forgeProjectID, tr, m) +} + +// syncLinkedIssues is the linked repo's sync body; replaced by the intake milestone. +func (s *Service) syncLinkedIssues(context.Context, uuid.UUID) error { + return ErrLinkedSyncNotReady +} diff --git a/api/internal/forgesvc/linked_issue_sync_test.go b/api/internal/forgesvc/linked_issue_sync_test.go new file mode 100644 index 0000000000..7e6293c9d1 --- /dev/null +++ b/api/internal/forgesvc/linked_issue_sync_test.go @@ -0,0 +1,46 @@ +package forgesvc + +import ( + "context" + "errors" + "testing" + "time" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" +) + +func TestSyncIssuesDispatchesOnTheLink(t *testing.T) { + marks := Marks{PRD: time.Unix(100, 0)} + + t.Run("linked: no forge call, typed error, marks kept", func(t *testing.T) { + f := &fakeForge{} + guarded := &issueWriteGuard{inner: f} + s := newTestService(&fakeStore{}) + if _, err := s.SyncIssues(context.Background(), uuid.New(), testProjectID, guarded); !errors.Is(err, ErrLinkedSyncNotReady) { + t.Fatalf("SyncIssues error = %v, want ErrLinkedSyncNotReady", err) + } + got, err := s.SyncIssuesIncremental(context.Background(), uuid.New(), testProjectID, guarded, marks) + if !errors.Is(err, ErrLinkedSyncNotReady) || got != marks { + t.Fatalf("SyncIssuesIncremental = (%+v, %v)", got, err) + } + if len(f.listCalls) != 0 { + t.Fatalf("a linked sync reached the forge: %+v", f.listCalls) + } + }) + + t.Run("unlinked: the forge sync, same calls", func(t *testing.T) { + viaDispatcher := &fakeForge{issues: []forge.Issue{issueAt(1, recordedMarkBase)}} + direct := &fakeForge{issues: []forge.Issue{issueAt(1, recordedMarkBase)}} + if _, err := newTestService(&fakeStore{}).SyncIssues(context.Background(), uuid.New(), testProjectID, viaDispatcher); err != nil { + t.Fatal(err) + } + if _, err := newTestService(&fakeStore{}).FullSync(context.Background(), uuid.New(), testProjectID, direct); err != nil { + t.Fatal(err) + } + if len(viaDispatcher.listCalls) == 0 || len(viaDispatcher.listCalls) != len(direct.listCalls) { + t.Fatalf("dispatcher made %d list calls, direct FullSync %d", len(viaDispatcher.listCalls), len(direct.listCalls)) + } + }) +} diff --git a/api/internal/forgesvc/linked_mr_lanes_livedb_test.go b/api/internal/forgesvc/linked_mr_lanes_livedb_test.go new file mode 100644 index 0000000000..a121fde4f7 --- /dev/null +++ b/api/internal/forgesvc/linked_mr_lanes_livedb_test.go @@ -0,0 +1,117 @@ +package forgesvc + +import ( + "context" + "fmt" + "os" + "testing" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/store" +) + +// PRD #4173 M4: the two MR-watch lanes stay disjoint and a linked repo's issue runs +// move to the board-free lane. An unlinked repo's runs match exactly as before. +func TestMRWatchLanesForLinkedAndUnlinkedReposLiveDB(t *testing.T) { + ctx := context.Background() + dsn := os.Getenv("UZI_TEST_DATABASE_URL") + if dsn == "" { + t.Skip("UZI_TEST_DATABASE_URL not set; run via e2e/run-store-it.sh for live-DB coverage") + } + if err := store.Migrate(ctx, dsn); err != nil { + t.Fatalf("migrate: %v", err) + } + pool, err := store.OpenPool(ctx, dsn) + if err != nil { + t.Fatalf("open pool: %v", err) + } + t.Cleanup(pool.Close) + q := store.New(pool) + exec := func(sql string, args ...any) { + t.Helper() + if _, err := pool.Exec(ctx, sql, args...); err != nil { + t.Fatalf("exec %q: %v", sql, err) + } + } + + user, forgeConn, itConn := uuid.New(), uuid.New(), uuid.New() + linkedRepo, plainRepo := uuid.New(), uuid.New() + exec(`INSERT INTO users (id, email, password_hash) VALUES ($1, $2, 'x')`, user, fmt.Sprintf("lanes-%s@e2e", user)) + exec(`INSERT INTO forge_connections (id, user_id, forge_type, base_url, bot_username, bot_forge_user_id, token_ciphertext) + VALUES ($1, $2, 'gitlab', $3, 'bot', 1, $4)`, forgeConn, user, "https://"+uuid.NewString()+".forge.e2e", []byte{1}) + exec(`INSERT INTO issue_tracker_connections (id, user_id, kind, base_url, account_id, account_login, token_ciphertext) + VALUES ($1, $2, 'youtrack', $3, '1-1', 'bot', $4)`, itConn, user, "https://"+uuid.NewString()+".yt.e2e", []byte{1}) + exec(`INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, + issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at) + VALUES ($1, $2, 1, $3, 'https://forge.e2e/g/l', 'main', true, $4, '0-1', 'UZI', '5-1', 'uzi', now())`, + linkedRepo, forgeConn, "g/linked-"+linkedRepo.String(), itConn) + exec(`INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled) + VALUES ($1, $2, 2, $3, 'https://forge.e2e/g/p', 'main', true)`, plainRepo, forgeConn, "g/plain-"+plainRepo.String()) + + // Each repo gets the same two runs: a completed issue run with an opened MR (the + // board lane's candidate on an unlinked repo) and a prompt run (the board-free + // lane's, always). + runs := map[uuid.UUID]map[string]uuid.UUID{} + for _, repo := range []uuid.UUID{linkedRepo, plainRepo} { + runs[repo] = map[string]uuid.UUID{"issue": uuid.New(), "prompt": uuid.New()} + exec(`INSERT INTO issues (repo_id, forge_issue_iid, title, state, labels, web_url, forge_updated_at, synced_at) + VALUES ($1, 7, 't', 'opened', '["Human Review"]', 'https://forge.e2e/i/7', now(), now())`, repo) + ins := `INSERT INTO runs (id, user_id, repo_id, kind, issue_iid, issue_title, issue_description, branch, mr_iid, mr_state, status) + VALUES ($1, $2, $3, $4, $5, 't', 'd', $6, $7, 'opened', 'completed')` + exec(ins, runs[repo]["issue"], user, repo, "issue", 7, "agent/issue-7-"+repo.String(), 13) + exec(ins, runs[repo]["prompt"], user, repo, "prompt", nil, "uzi/prompt-"+repo.String(), 21) + } + + boardLane := func(repo uuid.UUID) map[uuid.UUID]bool { + t.Helper() + rows, err := q.ListMRWatchCandidates(ctx, repo) + if err != nil { + t.Fatal(err) + } + got := map[uuid.UUID]bool{} + for _, r := range rows { + got[r.ID] = true + } + return got + } + freeLane := func(repo uuid.UUID) map[uuid.UUID]bool { + t.Helper() + rows, err := q.ListBoardFreeMRStateWatchCandidates(ctx, repo) + if err != nil { + t.Fatal(err) + } + got := map[uuid.UUID]bool{} + for _, r := range rows { + got[r.ID] = true + } + return got + } + + t.Run("unlinked repo: lanes exactly as before", func(t *testing.T) { + b, f := boardLane(plainRepo), freeLane(plainRepo) + r := runs[plainRepo] + if len(b) != 1 || !b[r["issue"]] { + t.Errorf("board lane = %v, want only the issue run", b) + } + if len(f) != 1 || !f[r["prompt"]] { + t.Errorf("board-free lane = %v, want the prompt run only", f) + } + }) + + t.Run("linked repo: the issue run moves to the board-free lane", func(t *testing.T) { + b, f := boardLane(linkedRepo), freeLane(linkedRepo) + r := runs[linkedRepo] + if len(b) != 0 { + t.Errorf("board lane = %v, want none for a linked repo", b) + } + if len(f) != 2 || !f[r["issue"]] || !f[r["prompt"]] { + t.Errorf("board-free lane = %v, want both runs", f) + } + for _, id := range r { + if b[id] && f[id] { + t.Errorf("run %v is in both lanes", id) + } + } + }) +} diff --git a/api/internal/forgesvc/merged_predecessor.go b/api/internal/forgesvc/merged_predecessor.go index 4aefd5e69a..4571d30552 100644 --- a/api/internal/forgesvc/merged_predecessor.go +++ b/api/internal/forgesvc/merged_predecessor.go @@ -182,7 +182,10 @@ func (s *Service) verifyMergedPredecessor(ctx context.Context, row store.ListMer }() // The candidate query takes a fresh exact owner-scoped connection snapshot. // Never reuse the ordinary tick's connection or cached MR state. - f, err := s.CodeHostForRepo(ctx, row.RepoID, row.ForgeType, row.BaseUrl, row.TokenCiphertext) + // Code-host side only (S2): the candidate row is already a fresh owner-scoped + // connection snapshot, so the driver is built from it directly and never reads + // the repo's issue tracker link. + f, err := s.ForgeForConnection(row.ForgeType, row.BaseUrl, row.TokenCiphertext) if err != nil { slog.Warn("merged predecessor reconciliation refused", "run_id", row.RunID, "reason", "client_unavailable") return diff --git a/api/internal/forgesvc/mr_watch.go b/api/internal/forgesvc/mr_watch.go index 06fe3b54ca..1be0b4d802 100644 --- a/api/internal/forgesvc/mr_watch.go +++ b/api/internal/forgesvc/mr_watch.go @@ -225,6 +225,13 @@ func (s *Service) guardedMRMove(ctx context.Context, repoID uuid.UUID, forgeProj } if _, err := s.AutoMove(ctx, tr, forgeProjectID, issue, cols, target); err != nil { + if IssueWriteSkippable(err) { + // A linked repo's card is never moved by uzi (writes arrive in M5) or its + // issue is gone: consume the edge so recordMRState still records the MR + // state, and never retry forever (PRD #4173 D10). + slog.Info("forgesvc: MR-state move skipped for a linked repo", "repo", repoID, "issue", issueIID, "target", target, "reason", err) + return moveSkipped + } // Forge error: leave mr_state unadvanced so the next tick re-observes this // edge and retries once the forge is reachable again. slog.Warn("forgesvc: MR-state move failed, will retry", "repo", repoID, "issue", issueIID, "target", target, "error", err) @@ -239,8 +246,9 @@ func (s *Service) guardedMRMove(ctx context.Context, repoID uuid.UUID, forgeProj // so no run is ever recorded by both on one tick: ListMRWatchCandidates requires a // non-null issue_iid (via its issues JOIN) AND kind NOT IN ('prompt','self_improve'), // while ListBoardFreeMRStateWatchCandidates owns exactly the complement -// (issue_iid IS NULL OR kind IN ('prompt','self_improve')) — a run cannot satisfy both -// predicates at once. Best-effort: a write failure only means the same edge is +// (issue_iid IS NULL OR kind IN ('prompt','self_improve'), plus EVERY run of a repo linked +// to an external issue tracker, which the board lane excludes — PRD #4173 M4) — a run +// cannot satisfy both predicates at once. Best-effort: a write failure only means the same edge is // re-observed next tick, and the guards make the retry a no-op if the card has // already moved. func (s *Service) recordMRState(ctx context.Context, runID uuid.UUID, state string) { diff --git a/api/internal/forgesvc/prd_link_patch.go b/api/internal/forgesvc/prd_link_patch.go index eb6ee2e61c..734c233e0f 100644 --- a/api/internal/forgesvc/prd_link_patch.go +++ b/api/internal/forgesvc/prd_link_patch.go @@ -158,6 +158,10 @@ func (s *Service) patchOnePRDLink(ctx context.Context, forgeProjectID int64, ch issue, err := tr.GetIssue(ctx, forgeProjectID, c.IssueIid.Int64) if err != nil { + if IssueWriteSkippable(err) { + s.settlePRDLink(ctx, lg, c.ID, "PRD link patch: the linked issue is gone; nothing to patch") + return + } // Case 1 again: a failed read is not evidence about the description. lg.Warn("PRD link patch: read issue", "error", err) return @@ -178,6 +182,10 @@ func (s *Service) patchOnePRDLink(ctx context.Context, forgeProjectID int64, ch } if err := tr.UpdateIssueDescription(ctx, forgeProjectID, c.IssueIid.Int64, desc); err != nil { + if IssueWriteSkippable(err) { + s.settlePRDLink(ctx, lg, c.ID, "PRD link patch: issue writes are not offered for this repo; nothing to patch") + return + } // Leave the marker: the write is what we are retrying. lg.Warn("PRD link patch: update issue description", "error", err) return diff --git a/api/internal/forgesvc/projectsync.go b/api/internal/forgesvc/projectsync.go index 3ac7d96b03..cc3fdd385d 100644 --- a/api/internal/forgesvc/projectsync.go +++ b/api/internal/forgesvc/projectsync.go @@ -61,6 +61,9 @@ const ( var ( // ErrProjectSyncDisabled is the instance kill-switch being off (settings default). ErrProjectSyncDisabled = errors.New("github project sync is disabled instance-wide") + // ErrProjectSyncLinkedRepo is an adopt, provision or resync on a repo linked to an + // external issue tracker (PRD #4173 M4): a project's items are forge issue numbers. + ErrProjectSyncLinkedRepo = errors.New("github project sync is not available on a repo linked to an issue tracker") // ErrProjectSyncNotGitHub is an adopt against a non-GitHub repo. ErrProjectSyncNotGitHub = errors.New("project sync is only supported on github repos") // ErrProjectSyncUnsupported is a forge whose driver does not implement @@ -245,8 +248,10 @@ func (s *ProjectSyncService) projectSyncResolve(ctx context.Context, repoID uuid return repo, syncer, f, nil } -// projectSyncPreamble runs the preconditions the write paths (Adopt, Provision, -// SetVisibility, ShareWithUser, Unshare) and the RepoOwnerType read share: everything +// projectSyncPreamble runs the preconditions the share and visibility paths +// (SetVisibility, ShareWithUser, Unshare) and the RepoOwnerType read share (the paths +// that project issue numbers, Adopt, Provision, Resync and AutoCreateColumns, use +// projectSyncSeedPreamble instead): everything // projectSyncResolve checks, plus the live scope preflight (ensureProjectScope, which // adds MissingScope to the sentinel set). The check order is exactly what the admin // handler maps to a 4xx — keeping the entry points DRY without changing any observable @@ -262,6 +267,28 @@ func (s *ProjectSyncService) projectSyncPreamble(ctx context.Context, repoID uui return repo, syncer, nil } +// projectSyncSeedPreamble is projectSyncPreamble for the paths that project cached +// issue numbers onto a board (Adopt, Provision, Resync, AutoCreateColumns): it refuses +// a never-enabled repo (PRD 4173 D13) after the pure resolve and BEFORE the live scope +// preflight or any other forge call. The share and visibility paths keep +// projectSyncPreamble; they store no issue number. +func (s *ProjectSyncService) projectSyncSeedPreamble(ctx context.Context, repoID uuid.UUID) (store.GetRepoByIDRow, forge.ProjectBoardSyncer, error) { + repo, syncer, f, err := s.projectSyncResolve(ctx, repoID) + if err != nil { + return store.GetRepoByIDRow{}, nil, err + } + if err := store.RefuseNeverEnabled(repo.AwaitingFirstEnable); err != nil { + return store.GetRepoByIDRow{}, nil, err + } + if repo.IssueTrackerConnectionID.Valid { + return store.GetRepoByIDRow{}, nil, ErrProjectSyncLinkedRepo + } + if err := ensureProjectScope(ctx, f); err != nil { + return store.GetRepoByIDRow{}, nil, err + } + return repo, syncer, nil +} + // ProjectSyncStatus is the health/observability snapshot of a repo's project sync // link (PRD #364 M7), returned by ProjectSyncStatus for the admin health endpoint. // It carries no secret and no forge coordinate the UI cannot already see — just the diff --git a/api/internal/forgesvc/projectsync_linked_test.go b/api/internal/forgesvc/projectsync_linked_test.go new file mode 100644 index 0000000000..c41955da2c --- /dev/null +++ b/api/internal/forgesvc/projectsync_linked_test.go @@ -0,0 +1,72 @@ +package forgesvc + +import ( + "context" + "errors" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgtype" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/store" +) + +func linkedGithubRow(repoID uuid.UUID) store.GetRepoByIDRow { + row := githubRepoRow(repoID) + row.IssueTrackerConnectionID = pgtype.UUID{Bytes: uuid.New(), Valid: true} + return row +} + +// PRD #4173 M4: a project's items are forge issue numbers, so a repo linked to an +// issue tracker is never projected onto a GitHub Projects v2 board. +func TestProjectSyncSkipsALinkedRepo(t *testing.T) { + repoID := uuid.New() + t.Run("forward move and reverse sync do nothing", func(t *testing.T) { + syncer := forwardSyncer() + st := &fakeProjectStore{ + repo: linkedGithubRow(repoID), + link: forwardLink(t, map[string]string{"In Progress": "opt_ip"}), + item: store.GithubProjectItem{RepoID: repoID, ForgeIssueIid: 7, ItemNodeID: "item7"}, + issues: []store.Issue{{ForgeIssueIid: 7, State: "opened", Labels: labelsJSON(t)}}, + } + svc := NewProjectSync(st, fakeForgeBuilder{f: syncer}, fakeSyncSettings{enabled: true}, nil) + mover := &fakeMover{} + svc.SetMover(mover) + + if err := svc.ForwardMove(context.Background(), repoID, 7, "In Progress"); err != nil { + t.Fatal(err) + } + if err := svc.ReverseSync(context.Background(), repoID); err != nil { + t.Fatal(err) + } + if len(syncer.setCalls) != 0 || len(mover.calls) != 0 || len(st.markerSets) != 0 || len(st.items) != 0 { + t.Fatalf("a linked repo was projected: set=%v moves=%v markers=%v items=%v", syncer.setCalls, mover.calls, st.markerSets, st.items) + } + }) + t.Run("creating or adopting a link is refused", func(t *testing.T) { + for name, call := range map[string]func(*ProjectSyncService) error{ + "Adopt": func(s *ProjectSyncService) error { return s.Adopt(context.Background(), repoID, 7, forge.OwnerUser) }, + "Provision": func(s *ProjectSyncService) error { + return s.Provision(context.Background(), repoID, forge.OwnerUser, "") + }, + "Resync": func(s *ProjectSyncService) error { _, err := s.Resync(context.Background(), repoID); return err }, + "AutoCreateColumns": func(s *ProjectSyncService) error { + _, err := s.AutoCreateColumns(context.Background(), repoID) + return err + }, + } { + t.Run(name, func(t *testing.T) { + syncer := provisionSyncer("f", nil) + st := &fakeProjectStore{repo: linkedGithubRow(repoID)} + svc := NewProjectSync(st, fakeForgeBuilder{f: syncer}, fakeSyncSettings{enabled: true}, nil) + if err := call(svc); !errors.Is(err, ErrProjectSyncLinkedRepo) { + t.Fatalf("error = %v, want ErrProjectSyncLinkedRepo", err) + } + if syncer.tokenInfoCalls != 0 || len(syncer.createProjectCalls) != 0 || len(st.links) != 0 { + t.Errorf("a refused call reached the forge or wrote a link") + } + }) + } + }) +} diff --git a/api/internal/forgesvc/projectsync_never_enabled_test.go b/api/internal/forgesvc/projectsync_never_enabled_test.go new file mode 100644 index 0000000000..572801ea0e --- /dev/null +++ b/api/internal/forgesvc/projectsync_never_enabled_test.go @@ -0,0 +1,55 @@ +package forgesvc + +import ( + "context" + "errors" + "testing" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// PRD 4173 D13: the project-sync writers that project issue numbers onto a board +// refuse a never-enabled repo before the scope preflight or any other forge call. +func TestProjectSyncWritersRefuseNeverEnabledRepo(t *testing.T) { + for _, tc := range []struct { + name string + call func(svc *ProjectSyncService, repoID uuid.UUID) error + }{ + {"Adopt", func(svc *ProjectSyncService, id uuid.UUID) error { + return svc.Adopt(context.Background(), id, 7, forge.OwnerUser) + }}, + {"Provision", func(svc *ProjectSyncService, id uuid.UUID) error { + return svc.Provision(context.Background(), id, forge.OwnerUser, "") + }}, + {"Resync", func(svc *ProjectSyncService, id uuid.UUID) error { + _, err := svc.Resync(context.Background(), id) + return err + }}, + {"AutoCreateColumns", func(svc *ProjectSyncService, id uuid.UUID) error { + _, err := svc.AutoCreateColumns(context.Background(), id) + return err + }}, + } { + t.Run(tc.name, func(t *testing.T) { + repoID := uuid.New() + row := githubRepoRow(repoID) + row.AwaitingFirstEnable = true + syncer := provisionSyncer("f", nil) + st := &fakeProjectStore{repo: row} + svc := NewProjectSync(st, fakeForgeBuilder{f: syncer}, fakeSyncSettings{enabled: true}, nil) + err := tc.call(svc, repoID) + if !errors.Is(err, store.ErrRepoNeverEnabled) { + t.Fatalf("error = %v, want store.ErrRepoNeverEnabled", err) + } + if syncer.tokenInfoCalls != 0 || len(syncer.createProjectCalls) != 0 { + t.Errorf("forge calls: TokenInfo=%d CreateProject=%d; want none", syncer.tokenInfoCalls, len(syncer.createProjectCalls)) + } + if len(st.links) != 0 { + t.Errorf("a refused call wrote a link") + } + }) + } +} diff --git a/api/internal/forgesvc/projectsync_provision.go b/api/internal/forgesvc/projectsync_provision.go index 9ea660a3af..17cd48d3a5 100644 --- a/api/internal/forgesvc/projectsync_provision.go +++ b/api/internal/forgesvc/projectsync_provision.go @@ -31,7 +31,7 @@ import ( // An unintrospectable PAT proceeds best-effort — the first mutation surfaces the // real error, captured in the link row's last_error. func (s *ProjectSyncService) Adopt(ctx context.Context, repoID uuid.UUID, projectNumber int, ownerKind forge.ProjectV2OwnerKind) error { - repo, syncer, err := s.projectSyncPreamble(ctx, repoID) + repo, syncer, err := s.projectSyncSeedPreamble(ctx, repoID) if err != nil { return err } @@ -85,7 +85,7 @@ func (s *ProjectSyncService) Adopt(ctx context.Context, repoID uuid.UUID, projec // Preconditions and the CLEAR sentinels it returns are identical to Adopt (see // projectSyncPreamble). title defaults to a sensible value when empty. func (s *ProjectSyncService) Provision(ctx context.Context, repoID uuid.UUID, ownerKind forge.ProjectV2OwnerKind, title string) error { - repo, syncer, err := s.projectSyncPreamble(ctx, repoID) + repo, syncer, err := s.projectSyncSeedPreamble(ctx, repoID) if err != nil { return err } @@ -508,7 +508,7 @@ func (s *ProjectSyncService) Resync(ctx context.Context, repoID uuid.UUID) (stri return "", fmt.Errorf("project sync: resync load link: %w", err) } - repo, syncer, err := s.projectSyncPreamble(ctx, repoID) + repo, syncer, err := s.projectSyncSeedPreamble(ctx, repoID) if err != nil { return "", err } @@ -610,7 +610,7 @@ func (s *ProjectSyncService) AutoCreateColumns(ctx context.Context, repoID uuid. return "", fmt.Errorf("project sync: autocreate load link: %w", err) } - repo, syncer, err := s.projectSyncPreamble(ctx, repoID) + repo, syncer, err := s.projectSyncSeedPreamble(ctx, repoID) if err != nil { return "", err } diff --git a/api/internal/forgesvc/projectsync_sync.go b/api/internal/forgesvc/projectsync_sync.go index ee1075ec6c..e5447e178e 100644 --- a/api/internal/forgesvc/projectsync_sync.go +++ b/api/internal/forgesvc/projectsync_sync.go @@ -69,6 +69,10 @@ func (s *ProjectSyncService) ForwardMove(ctx context.Context, repoID uuid.UUID, s.log.Warn("project sync: forward move load repo", "repo", repoID, "issue", issueIID, "error", err) return nil } + if repo.IssueTrackerConnectionID.Valid { + s.log.Info("project sync: forward move skipped, repo is linked to an issue tracker", "repo", repoID, "issue", issueIID) + return nil + } if repo.ForgeType != string(forge.TypeGitHub) { return nil } @@ -278,6 +282,10 @@ func (s *ProjectSyncService) ReverseSync(ctx context.Context, repoID uuid.UUID) s.log.Warn("project sync: reverse load repo", "repo", repoID, "error", err) return nil } + if repo.IssueTrackerConnectionID.Valid { + s.log.Info("project sync: reverse sync skipped, repo is linked to an issue tracker", "repo", repoID) + return nil + } if repo.ForgeType != string(forge.TypeGitHub) { return nil } diff --git a/api/internal/forgesvc/recorded_calls_test.go b/api/internal/forgesvc/recorded_calls_test.go new file mode 100644 index 0000000000..c9df3d2ba1 --- /dev/null +++ b/api/internal/forgesvc/recorded_calls_test.go @@ -0,0 +1,122 @@ +package forgesvc + +import ( + "context" + "testing" + "time" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/board" + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// The recorded-call tests below pin, for a repo with NO issue-tracker link, +// every forge call each forgesvc sync path makes (PRD 4173 M4: a repo with no +// link is byte-for-byte unchanged in its forge calls). The golden files under +// testdata/ were captured on the code before the tracker-link rerouting; a +// change to one is a change to the forge traffic of every unlinked repo and +// must be deliberate (regenerate with UZI_UPDATE_GOLDEN=1). + +var recordedMarkBase = time.Date(2026, 3, 4, 5, 6, 7, 0, time.UTC) + +func TestRecordedCallsFullSync(t *testing.T) { + st := &fakeStore{} + f := &fakeForge{ + issues: []forge.Issue{issueAt(1, recordedMarkBase)}, + openIssues: []forge.Issue{issueAt(2, recordedMarkBase.Add(time.Minute))}, + findingIssues: []forge.Issue{issueAt(3, recordedMarkBase.Add(2*time.Minute))}, + } + rec := forgetest.NewRecorder(f) + if _, err := newTestService(st).FullSync(context.Background(), uuid.New(), testProjectID, rec); err != nil { + t.Fatalf("FullSync: %v", err) + } + forgetest.AssertGolden(t, "recorded_calls_forgesvc_fullsync.golden", rec.Calls()) +} + +func TestRecordedCallsIncrementalSync(t *testing.T) { + st := &fakeStore{} + f := &fakeForge{ + issues: []forge.Issue{issueAt(1, recordedMarkBase.Add(time.Hour))}, + openIssues: []forge.Issue{issueAt(2, recordedMarkBase.Add(time.Hour))}, + findingIssues: []forge.Issue{issueAt(3, recordedMarkBase.Add(time.Hour))}, + } + rec := forgetest.NewRecorder(f) + marks := Marks{PRD: recordedMarkBase, Open: recordedMarkBase.Add(time.Minute), Finding: recordedMarkBase.Add(2 * time.Minute)} + if _, err := newTestService(st).IncrementalSync(context.Background(), uuid.New(), testProjectID, rec, marks); err != nil { + t.Fatalf("IncrementalSync: %v", err) + } + forgetest.AssertGolden(t, "recorded_calls_forgesvc_incrementalsync.golden", rec.Calls()) +} + +// TestRecordedCallsSyncMRStates drives one open->closed edge and one +// closed->open edge, each through its own recorder, into one golden. +func TestRecordedCallsSyncMRStates(t *testing.T) { + var lines []string + for _, tc := range []struct { + name, stored, observed, from string + }{ + {"opened->closed", "opened", "closed", board.ColumnHumanReview}, + {"closed->opened", "closed", "opened", board.ColumnInProgress}, + } { + runID, repoID := uuid.New(), uuid.New() + st := &fakeStore{ + candidates: []store.ListMRWatchCandidatesRow{candidate(runID, 9, 13, mrTxt(tc.stored))}, + issue: mrIssue(repoID, 9, "opened", "PRD", tc.from), + columns: mrCols(), + } + f := &fakeForge{mr: forgeMR(13, tc.observed)} + rec := forgetest.NewRecorder(f) + if err := newTestService(st).SyncMRStates(context.Background(), repoID, testProjectID, rec, rec); err != nil { + t.Fatalf("SyncMRStates %s: %v", tc.name, err) + } + lines = append(lines, "# "+tc.name) + lines = append(lines, rec.Calls()...) + } + forgetest.AssertGolden(t, "recorded_calls_forgesvc_mrstates.golden", lines) +} + +func TestRecordedCallsSyncBoardFreeMRStates(t *testing.T) { + runID, repoID := uuid.New(), uuid.New() + st := &fakeStore{boardFreeCandidates: []store.ListBoardFreeMRStateWatchCandidatesRow{ + boardFreeCand(runID, 13, strptr(forge.MRStateOpened)), + }} + f := &fakeForge{mr: forgeMR(13, forge.MRStateMerged)} + svc := newTestService(st) + svc.SetReworkCanceller(&fakeReworkCanceller{}) + rec := forgetest.NewRecorder(f) + if err := svc.SyncBoardFreeMRStates(context.Background(), repoID, testProjectID, rec); err != nil { + t.Fatalf("SyncBoardFreeMRStates: %v", err) + } + forgetest.AssertGolden(t, "recorded_calls_forgesvc_boardfreemrstates.golden", rec.Calls()) +} + +func TestRecordedCallsSyncPRDLinkPatches(t *testing.T) { + s, _, f, _ := newPRDFixture(t, nil) + rec := forgetest.NewRecorder(f) + if err := s.SyncPRDLinkPatches(context.Background(), uuid.New(), fixProject, rec, rec); err != nil { + t.Fatalf("SyncPRDLinkPatches: %v", err) + } + if len(f.descriptionUpdates) != 1 { + t.Fatalf("fixture must reach the description write; updates = %+v", f.descriptionUpdates) + } + forgetest.AssertGolden(t, "recorded_calls_forgesvc_prdlinkpatches.golden", rec.Calls()) +} + +// emptyBoardStore is a BoardColumnStore for a repo that has no columns yet. +type emptyBoardStore struct{} + +func (emptyBoardStore) CountBoardColumns(context.Context, uuid.UUID) (int64, error) { return 0, nil } +func (emptyBoardStore) InsertDefaultBoardColumns(context.Context, store.InsertDefaultBoardColumnsParams) error { + return nil +} + +func TestRecordedCallsEnsureBoardColumns(t *testing.T) { + rec := forgetest.NewRecorder(&fakeForge{}) + if err := EnsureBoardColumns(context.Background(), emptyBoardStore{}, rec, uuid.New(), testProjectID); err != nil { + t.Fatalf("EnsureBoardColumns: %v", err) + } + forgetest.AssertGolden(t, "recorded_calls_forgesvc_ensureboardcolumns.golden", rec.Calls()) +} diff --git a/api/internal/forgesvc/repo_sides.go b/api/internal/forgesvc/repo_sides.go index 87c1e1dfd9..65b825e219 100644 --- a/api/internal/forgesvc/repo_sides.go +++ b/api/internal/forgesvc/repo_sides.go @@ -2,30 +2,84 @@ package forgesvc import ( "context" + "errors" + "fmt" "github.com/google/uuid" "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/issuetracker" + "github.com/vtmocanu/uzi/api/internal/store" ) -// IssueTrackerForRepo returns the issue side of a repo (D5 of PRD #4173, the IssueTracker / -// CodeHost split). Through M3: the repo's own forge driver, built exactly as -// ForgeForConnection builds it. From PRD #4173 M4 it maps repoID to a linked external issue tracker. -// The caller has already resolved the connection through its own owner-scoped -// lookup, so forgeType, baseURL and tokenCiphertext come from that row. -func (s *Service) IssueTrackerForRepo(ctx context.Context, repoID uuid.UUID, forgeType, baseURL string, tokenCiphertext []byte) (forge.IssueTracker, error) { - f, err := s.ForgeForConnection(forgeType, baseURL, tokenCiphertext) +// IssueTrackerDrivers builds the driver of a linked external issue tracker for +// one repo binding. *issuetrackersvc.Service satisfies it. Optional on a Service: +// without it a linked repo fails closed (PRD #4173 DL-5). +type IssueTrackerDrivers interface { + DriverFor(ctx context.Context, userID, connID uuid.UUID, binding issuetracker.Binding, stateField, open, closed string) (issuetracker.Driver, error) +} + +// SetIssueTrackerDrivers wires the linked-issue-tracker driver source. +func (s *Service) SetIssueTrackerDrivers(d IssueTrackerDrivers) { s.itDrivers = d } + +// errNoRepoStore is returned by the resolvers on a Service built without queries: a +// resolver must fail, not dereference a nil store. +var errNoRepoStore = errors.New("forgesvc: no repo store wired") + +// repoSource reads the one owner-scoped row both resolvers use. +func (s *Service) repoSource(ctx context.Context, userID, repoID uuid.UUID) (store.GetRepoIssueSourceForUserRow, error) { + if s.q == nil { + return store.GetRepoIssueSourceForUserRow{}, errNoRepoStore + } + return s.q.GetRepoIssueSourceForUser(ctx, store.GetRepoIssueSourceForUserParams{ID: repoID, UserID: userID}) +} + +// ErrLinkedRepoUnresolvable is returned for a linked repo whose issue side +// cannot be built: no driver source wired, or a link whose connection is missing +// for the repo's owner. There is deliberately no fallback to the forge. +var ErrLinkedRepoUnresolvable = errors.New("repo is linked to an issue tracker that cannot be resolved") + +// IssueTrackerForRepo returns the issue side of a repo (D5, D10 of PRD #4173), +// bound by repo row and owner, never by forge project number. An unlinked repo +// gets its forge driver, built as ForgeForConnection builds it. A linked repo +// gets the issue tracker driver for its stored binding, wrapped in the write +// guard; a link that cannot be resolved is an error. +func (s *Service) IssueTrackerForRepo(ctx context.Context, userID, repoID uuid.UUID) (forge.IssueTracker, error) { + src, err := s.repoSource(ctx, userID, repoID) if err != nil { - return nil, err + return nil, fmt.Errorf("read repo issue source: %w", err) } - return f, nil + if !src.IssueTrackerConnectionID.Valid { + f, err := s.ForgeForConnection(src.ForgeType, src.BaseUrl, src.TokenCiphertext) + if err != nil { + return nil, err + } + return f, nil + } + if !src.LinkedConnectionID.Valid || s.itDrivers == nil || + !src.IssueTrackerProjectID.Valid || !src.IssueTrackerProjectShort.Valid || + !src.EligibilityTagID.Valid || !src.EligibilityTagName.Valid { + return nil, ErrLinkedRepoUnresolvable + } + d, err := s.itDrivers.DriverFor(ctx, userID, uuid.UUID(src.LinkedConnectionID.Bytes), issuetracker.Binding{ + Project: issuetracker.ProjectRef{ID: src.IssueTrackerProjectID.String, ShortName: src.IssueTrackerProjectShort.String}, + ProjectID: src.ForgeProjectID, + EligibilityTag: issuetracker.TagRef{ID: src.EligibilityTagID.String, Name: src.EligibilityTagName.String}, + }, "", "", "") + if err != nil { + return nil, fmt.Errorf("build linked issue tracker: %w", err) + } + return &issueWriteGuard{inner: d}, nil } // CodeHostForRepo returns the code-host side of a repo: always the repo's own -// forge driver, built as ForgeForConnection builds it. See IssueTrackerForRepo -// for the connection-resolution contract. -func (s *Service) CodeHostForRepo(ctx context.Context, repoID uuid.UUID, forgeType, baseURL string, tokenCiphertext []byte) (forge.CodeHost, error) { - f, err := s.ForgeForConnection(forgeType, baseURL, tokenCiphertext) +// forge driver, built as ForgeForConnection builds it, never reading the link (S2). +func (s *Service) CodeHostForRepo(ctx context.Context, userID, repoID uuid.UUID) (forge.CodeHost, error) { + src, err := s.repoSource(ctx, userID, repoID) + if err != nil { + return nil, fmt.Errorf("read repo connection: %w", err) + } + f, err := s.ForgeForConnection(src.ForgeType, src.BaseUrl, src.TokenCiphertext) if err != nil { return nil, err } diff --git a/api/internal/forgesvc/repo_sides_test.go b/api/internal/forgesvc/repo_sides_test.go index ced96b9a81..9316e07cf6 100644 --- a/api/internal/forgesvc/repo_sides_test.go +++ b/api/internal/forgesvc/repo_sides_test.go @@ -7,76 +7,339 @@ import ( "time" "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgtype" "github.com/vtmocanu/uzi/api/internal/forge" "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/issuetracker" "github.com/vtmocanu/uzi/api/internal/secretbox" + "github.com/vtmocanu/uzi/api/internal/store" ) type sidesFake struct{ forgetest.BaseFake } -func TestRepoSidesResolveRepoForge(t *testing.T) { - const plain = "plain-value" - const baseURL = "https://forge.example.test" +// GetRepoIssueSourceForUser satisfies IssueStore for the sync fakes that never +// resolve a repo side. +func (*fakeStore) GetRepoIssueSourceForUser(context.Context, store.GetRepoIssueSourceForUserParams) (store.GetRepoIssueSourceForUserRow, error) { + return store.GetRepoIssueSourceForUserRow{}, pgx.ErrNoRows +} + +// sourceStore serves the one owner-scoped row the resolvers read, keyed by repo id, +// and answers pgx.ErrNoRows for any other user like the real query. +type sourceStore struct { + IssueStore // nil: the resolvers use nothing else + owner map[uuid.UUID]uuid.UUID + rows map[uuid.UUID]store.GetRepoIssueSourceForUserRow +} + +func (s *sourceStore) GetRepoIssueSourceForUser(_ context.Context, a store.GetRepoIssueSourceForUserParams) (store.GetRepoIssueSourceForUserRow, error) { + row, ok := s.rows[a.ID] + if !ok || s.owner[a.ID] != a.UserID { + return store.GetRepoIssueSourceForUserRow{}, pgx.ErrNoRows + } + return row, nil +} + +// stubDriver is the issue tracker driver the fake driver source hands out. It embeds +// the interface so only the methods a test drives exist; a write that reaches it is +// recorded. +type stubDriver struct { + issuetracker.Driver + reads, writes []string + getIssueErr error +} + +func (d *stubDriver) GetIssue(context.Context, int64, int64) (forge.Issue, error) { + d.reads = append(d.reads, "GetIssue") + return forge.Issue{}, d.getIssueErr +} +func (d *stubDriver) ListIssues(context.Context, int64, forge.ListIssuesOptions) ([]forge.Issue, error) { + d.reads = append(d.reads, "ListIssues") + return nil, nil +} +func (d *stubDriver) EnsureLabels(context.Context, int64, []forge.Label) error { + d.writes = append(d.writes, "EnsureLabels") + return nil +} +func (d *stubDriver) UpdateIssueLabels(context.Context, int64, int64, []string, []string) error { + d.writes = append(d.writes, "UpdateIssueLabels") + return nil +} +func (d *stubDriver) UpdateIssueDescription(context.Context, int64, int64, string) error { + d.writes = append(d.writes, "UpdateIssueDescription") + return nil +} +func (d *stubDriver) SetIssueState(context.Context, int64, int64, forge.IssueState) error { + d.writes = append(d.writes, "SetIssueState") + return nil +} +func (d *stubDriver) CreateIssue(context.Context, int64, string, string, []string) (forge.Issue, error) { + d.writes = append(d.writes, "CreateIssue") + return forge.Issue{}, nil +} +func (d *stubDriver) CreateIssueNote(context.Context, int64, int64, string) (forge.IssueNote, error) { + d.writes = append(d.writes, "CreateIssueNote") + return forge.IssueNote{}, nil +} + +type fakeDrivers struct { + driver *stubDriver + err error + calls int + gotUser, gotConn uuid.UUID + gotBinding issuetracker.Binding + gotState, gotOpen, gotC string +} + +func (f *fakeDrivers) DriverFor(_ context.Context, userID, connID uuid.UUID, b issuetracker.Binding, stateField, open, closed string) (issuetracker.Driver, error) { + f.calls++ + f.gotUser, f.gotConn, f.gotBinding = userID, connID, b + f.gotState, f.gotOpen, f.gotC = stateField, open, closed + if f.err != nil { + return nil, f.err + } + return f.driver, nil +} + +type sidesHarness struct { + svc *Service + rec *forgetest.Recorder + drivers *fakeDrivers + user uuid.UUID + built int + builtURL string +} + +// newSidesHarness builds a Service whose forge builder returns a recorder, and one +// repo row per call of addRepo. +func newSidesHarness(t *testing.T) (*sidesHarness, *sourceStore) { + t.Helper() box, err := secretbox.New([]byte("0123456789abcdefghijklmnopqrstuv")) if err != nil { t.Fatal(err) } - fake := &sidesFake{} - var gotType forge.Type - var gotURL, gotToken string - s := NewWithForgeBuilder(nil, box, time.Second, nil, func(kind forge.Type, u, pat string, _ time.Duration) (forge.Forge, error) { - gotType, gotURL, gotToken = kind, u, pat - return fake, nil + h := &sidesHarness{ + rec: forgetest.NewRecorder(&sidesFake{}), + drivers: &fakeDrivers{driver: &stubDriver{}}, + user: uuid.New(), + } + src := &sourceStore{owner: map[uuid.UUID]uuid.UUID{}, rows: map[uuid.UUID]store.GetRepoIssueSourceForUserRow{}} + h.svc = NewWithForgeBuilder(src, box, time.Second, nil, func(_ forge.Type, u, _ string, _ time.Duration) (forge.Forge, error) { + h.built++ + h.builtURL = u + return h.rec, nil }) - sealed, err := s.EncryptToken(plain) + return h, src +} + +func (h *sidesHarness) addRepo(t *testing.T, src *sourceStore, owner uuid.UUID, link func(*store.GetRepoIssueSourceForUserRow)) uuid.UUID { + t.Helper() + sealed, err := h.svc.EncryptToken("forge-token") if err != nil { t.Fatal(err) } - repoID := uuid.New() + id := uuid.New() + row := store.GetRepoIssueSourceForUserRow{ + ID: id, ForgeProjectID: 4242, ForgeType: string(forge.TypeGitLab), + BaseUrl: "https://forge.example.test", TokenCiphertext: sealed, + } + if link != nil { + link(&row) + } + src.rows[id] = row + src.owner[id] = owner + return id +} - tr, err := s.IssueTrackerForRepo(context.Background(), repoID, string(forge.TypeGitLab), baseURL, sealed) - if err != nil { - t.Fatal(err) +func linkTo(conn uuid.UUID, resolved bool) func(*store.GetRepoIssueSourceForUserRow) { + return func(r *store.GetRepoIssueSourceForUserRow) { + r.IssueTrackerConnectionID = pgtype.UUID{Bytes: conn, Valid: true} + r.IssueTrackerProjectID = pgtype.Text{String: "0-7", Valid: true} + r.IssueTrackerProjectShort = pgtype.Text{String: "ABC", Valid: true} + r.EligibilityTagID = pgtype.Text{String: "5-1", Valid: true} + r.EligibilityTagName = pgtype.Text{String: "uzi", Valid: true} + r.LinkedConnectionID = pgtype.UUID{Bytes: conn, Valid: resolved} } - if tr != forge.IssueTracker(fake) { - t.Errorf("IssueTrackerForRepo = %v, want the built driver", tr) +} + +// writeAll drives every write the issue side offers and returns how many were +// accepted (returned no error). +func writeAll(ctx context.Context, tr forge.IssueTracker, projectID int64) (accepted int, errs []error) { + ops := []func() error{ + func() error { return tr.UpdateIssueLabels(ctx, projectID, 9, []string{"a"}, nil) }, + func() error { _, err := tr.CreateIssueNote(ctx, projectID, 9, "note"); return err }, + func() error { _, err := tr.CreateIssue(ctx, projectID, "t", "d", nil); return err }, + func() error { return tr.SetIssueState(ctx, projectID, 9, forge.StateClosed) }, + func() error { return tr.UpdateIssueDescription(ctx, projectID, 9, "d") }, + func() error { return tr.EnsureLabels(ctx, projectID, []forge.Label{{Name: "x"}}) }, } - if gotType != forge.TypeGitLab || gotURL != baseURL || gotToken != plain { - t.Errorf("builder got (%q, %q, %q)", gotType, gotURL, gotToken) + for _, op := range ops { + if err := op(); err != nil { + errs = append(errs, err) + continue + } + accepted++ } + return accepted, errs +} + +// The resolver-level recorded test (PRD #4173 M4): a repo that was never linked +// reaches the forge for every issue write, exactly as before. +func TestIssueTrackerForRepoUnlinkedWritesReachTheForge(t *testing.T) { + h, src := newSidesHarness(t) + repo := h.addRepo(t, src, h.user, nil) - gotType, gotURL, gotToken = "", "", "" - ch, err := s.CodeHostForRepo(context.Background(), repoID, string(forge.TypeGitLab), baseURL, sealed) + tr, err := h.svc.IssueTrackerForRepo(context.Background(), h.user, repo) if err != nil { t.Fatal(err) } - if ch != forge.CodeHost(fake) { - t.Errorf("CodeHostForRepo = %v, want the built driver", ch) + // BaseFake answers its writes with a not-stubbed error; what matters is that each + // call reached the recorder (the forge), and that no guard answered first. + _, errs := writeAll(context.Background(), tr, 4242) + for _, e := range errs { + if errors.Is(e, ErrIssueWriteNotOffered) { + t.Fatalf("an unlinked repo's write was refused by the guard: %v", e) + } + } + if got := len(h.rec.Calls()); got != 6 { + t.Fatalf("recorder saw %d calls, want 6 writes: %v", got, h.rec.Calls()) + } + if h.drivers.calls != 0 { + t.Fatalf("the issue tracker driver source was used for an unlinked repo") } - if gotType != forge.TypeGitLab || gotURL != baseURL || gotToken != plain { - t.Errorf("builder got (%q, %q, %q)", gotType, gotURL, gotToken) + if tr != forge.IssueTracker(h.rec) { + t.Fatalf("an unlinked repo's issue side must be the forge driver itself, got %T", tr) } } -func TestRepoSidesPassBuilderErrorThrough(t *testing.T) { - box, err := secretbox.New([]byte("0123456789abcdefghijklmnopqrstuv")) +func TestIssueTrackerForRepoLinkedBuildsTheBoundDriverBehindTheGuard(t *testing.T) { + h, src := newSidesHarness(t) + h.svc.SetIssueTrackerDrivers(h.drivers) + conn := uuid.New() + repo := h.addRepo(t, src, h.user, linkTo(conn, true)) + + tr, err := h.svc.IssueTrackerForRepo(context.Background(), h.user, repo) if err != nil { t.Fatal(err) } - boom := errors.New("builder failed") - s := NewWithForgeBuilder(nil, box, time.Second, nil, func(forge.Type, string, string, time.Duration) (forge.Forge, error) { - return nil, boom + d := h.drivers + want := issuetracker.Binding{ + Project: issuetracker.ProjectRef{ID: "0-7", ShortName: "ABC"}, + ProjectID: 4242, + EligibilityTag: issuetracker.TagRef{ID: "5-1", Name: "uzi"}, + } + if d.calls != 1 || d.gotUser != h.user || d.gotConn != conn || d.gotBinding != want { + t.Fatalf("DriverFor got (calls %d, user %v, conn %v, binding %+v), want the repo row's binding for its owner", d.calls, d.gotUser, d.gotConn, d.gotBinding) + } + if d.gotState != "" || d.gotOpen != "" || d.gotC != "" { + t.Errorf("state mapping arguments must be empty until M5, got %q %q %q", d.gotState, d.gotOpen, d.gotC) + } + if h.built != 0 { + t.Errorf("a linked repo's issue side must not build the forge driver") + } + + accepted, errs := writeAll(context.Background(), tr, 4242) + if accepted != 0 || len(errs) != 6 { + t.Fatalf("accepted %d writes with %d errors, want none accepted", accepted, len(errs)) + } + for _, e := range errs { + if !errors.Is(e, ErrIssueWriteNotOffered) || !IssueWriteSkippable(e) { + t.Errorf("write error = %v, want ErrIssueWriteNotOffered", e) + } + } + if len(d.driver.writes) != 0 { + t.Fatalf("writes reached the driver: %v", d.driver.writes) + } + if _, err := tr.ListIssues(context.Background(), 4242, forge.ListIssuesOptions{}); err != nil { + t.Fatal(err) + } + if len(d.driver.reads) != 1 { + t.Fatalf("reads must pass through, got %v", d.driver.reads) + } +} + +func TestIssueTrackerForRepoLinkedFailsClosed(t *testing.T) { + t.Run("connection missing for the owner", func(t *testing.T) { + h, src := newSidesHarness(t) + h.svc.SetIssueTrackerDrivers(h.drivers) + repo := h.addRepo(t, src, h.user, linkTo(uuid.New(), false)) + tr, err := h.svc.IssueTrackerForRepo(context.Background(), h.user, repo) + if !errors.Is(err, ErrLinkedRepoUnresolvable) || tr != nil { + t.Fatalf("got (%v, %v), want (nil, ErrLinkedRepoUnresolvable)", tr, err) + } + if h.drivers.calls != 0 || h.built != 0 { + t.Fatalf("no driver of either kind may be built (drivers %d, forge %d)", h.drivers.calls, h.built) + } }) - sealed, err := s.EncryptToken("t") + t.Run("no driver source wired", func(t *testing.T) { + h, src := newSidesHarness(t) + repo := h.addRepo(t, src, h.user, linkTo(uuid.New(), true)) + tr, err := h.svc.IssueTrackerForRepo(context.Background(), h.user, repo) + if !errors.Is(err, ErrLinkedRepoUnresolvable) || tr != nil { + t.Fatalf("got (%v, %v), want (nil, ErrLinkedRepoUnresolvable)", tr, err) + } + if h.built != 0 { + t.Fatal("a linked repo must never fall back to its forge driver") + } + }) + t.Run("driver build error", func(t *testing.T) { + h, src := newSidesHarness(t) + boom := errors.New("not allowed") + h.drivers.err = boom + h.svc.SetIssueTrackerDrivers(h.drivers) + repo := h.addRepo(t, src, h.user, linkTo(uuid.New(), true)) + tr, err := h.svc.IssueTrackerForRepo(context.Background(), h.user, repo) + if !errors.Is(err, boom) || tr != nil || h.built != 0 { + t.Fatalf("got (%v, %v), forge built %d", tr, err, h.built) + } + }) + t.Run("another owner", func(t *testing.T) { + h, src := newSidesHarness(t) + h.svc.SetIssueTrackerDrivers(h.drivers) + repo := h.addRepo(t, src, h.user, linkTo(uuid.New(), true)) + for name, call := range map[string]func() error{ + "issue side": func() error { _, err := h.svc.IssueTrackerForRepo(context.Background(), uuid.New(), repo); return err }, + "code host": func() error { _, err := h.svc.CodeHostForRepo(context.Background(), uuid.New(), repo); return err }, + } { + if err := call(); !errors.Is(err, pgx.ErrNoRows) { + t.Errorf("%s: error = %v, want pgx.ErrNoRows", name, err) + } + } + if h.drivers.calls != 0 || h.built != 0 { + t.Fatal("a foreign owner must reach no driver") + } + }) +} + +// The code-host side never reads the link (S2). +func TestCodeHostForRepoIgnoresTheLink(t *testing.T) { + h, src := newSidesHarness(t) + h.svc.SetIssueTrackerDrivers(h.drivers) + repo := h.addRepo(t, src, h.user, linkTo(uuid.New(), false)) + + ch, err := h.svc.CodeHostForRepo(context.Background(), h.user, repo) if err != nil { t.Fatal(err) } - tr, err := s.IssueTrackerForRepo(context.Background(), uuid.New(), string(forge.TypeGitLab), "https://x.test", sealed) + if ch != forge.CodeHost(h.rec) { + t.Fatalf("CodeHostForRepo = %T, want the forge driver", ch) + } + if h.builtURL != "https://forge.example.test" || h.drivers.calls != 0 { + t.Fatalf("forge url %q, issue tracker drivers used %d times", h.builtURL, h.drivers.calls) + } +} + +func TestRepoSidesPassBuilderErrorThrough(t *testing.T) { + h, src := newSidesHarness(t) + boom := errors.New("builder failed") + h.svc.forgeBuilder = func(forge.Type, string, string, time.Duration) (forge.Forge, error) { return nil, boom } + repo := h.addRepo(t, src, h.user, nil) + tr, err := h.svc.IssueTrackerForRepo(context.Background(), h.user, repo) if !errors.Is(err, boom) || tr != nil { t.Errorf("IssueTrackerForRepo = (%v, %v), want (nil, builder error)", tr, err) } - ch, err := s.CodeHostForRepo(context.Background(), uuid.New(), string(forge.TypeGitLab), "https://x.test", sealed) + ch, err := h.svc.CodeHostForRepo(context.Background(), h.user, repo) if !errors.Is(err, boom) || ch != nil { t.Errorf("CodeHostForRepo = (%v, %v), want (nil, builder error)", ch, err) } diff --git a/api/internal/forgesvc/service.go b/api/internal/forgesvc/service.go index 8f3c071c99..0a0e5bd00f 100644 --- a/api/internal/forgesvc/service.go +++ b/api/internal/forgesvc/service.go @@ -106,6 +106,9 @@ func HasPRDLink(description string) bool { // be unit-tested against a fake store (and a mocked Forge) without a live // database. *store.Queries satisfies it. type IssueStore interface { + // GetRepoIssueSourceForUser is the one owner-scoped row the per-repo resolvers + // (IssueTrackerForRepo, CodeHostForRepo) read. + GetRepoIssueSourceForUser(ctx context.Context, arg store.GetRepoIssueSourceForUserParams) (store.GetRepoIssueSourceForUserRow, error) RemoveCachedIssueLabel(context.Context, store.RemoveCachedIssueLabelParams) (store.Issue, error) UpsertIssue(ctx context.Context, arg store.UpsertIssueParams) (store.Issue, error) // UpsertIssueLabels is the label-only cache-write variant used by AutoMove and @@ -188,6 +191,10 @@ type Service struct { groupCursors map[uuid.UUID]store.FindingGroupCursor forgeBuilder ForgeBuilder + // itDrivers builds a linked repo's issue tracker driver (SetIssueTrackerDrivers). + // nil means a linked repo fails closed. + itDrivers IssueTrackerDrivers + // findingGroupSince is the optional pending-warning age clock; nil uses time.Since. findingGroupSince func(time.Time) time.Duration diff --git a/api/internal/forgesvc/testdata/recorded_calls_forgesvc_boardfreemrstates.golden b/api/internal/forgesvc/testdata/recorded_calls_forgesvc_boardfreemrstates.golden new file mode 100644 index 0000000000..90e5c6e1eb --- /dev/null +++ b/api/internal/forgesvc/testdata/recorded_calls_forgesvc_boardfreemrstates.golden @@ -0,0 +1 @@ +GetMergeRequest(77, 13) diff --git a/api/internal/forgesvc/testdata/recorded_calls_forgesvc_ensureboardcolumns.golden b/api/internal/forgesvc/testdata/recorded_calls_forgesvc_ensureboardcolumns.golden new file mode 100644 index 0000000000..21fc4037c1 --- /dev/null +++ b/api/internal/forgesvc/testdata/recorded_calls_forgesvc_ensureboardcolumns.golden @@ -0,0 +1 @@ +EnsureLabels(77, [{Name:"Planned" Color:"#6699cc"} {Name:"In Progress" Color:"#1f75cb"} {Name:"Human Review" Color:"#6e49cb"} {Name:"Later" Color:"#999999"}]) diff --git a/api/internal/forgesvc/testdata/recorded_calls_forgesvc_fullsync.golden b/api/internal/forgesvc/testdata/recorded_calls_forgesvc_fullsync.golden new file mode 100644 index 0000000000..1ba92c8112 --- /dev/null +++ b/api/internal/forgesvc/testdata/recorded_calls_forgesvc_fullsync.golden @@ -0,0 +1,3 @@ +ListIssues(77, {Labels:["uzi"] UpdatedAfter: State:"" Limit:0}) +ListIssues(77, {Labels:nil UpdatedAfter: State:"opened" Limit:0}) +ListIssues(77, {Labels:["agent-found"] UpdatedAfter: State:"" Limit:0}) diff --git a/api/internal/forgesvc/testdata/recorded_calls_forgesvc_incrementalsync.golden b/api/internal/forgesvc/testdata/recorded_calls_forgesvc_incrementalsync.golden new file mode 100644 index 0000000000..d9571690f3 --- /dev/null +++ b/api/internal/forgesvc/testdata/recorded_calls_forgesvc_incrementalsync.golden @@ -0,0 +1,3 @@ +ListIssues(77, {Labels:["uzi"] UpdatedAfter:2026-03-04T05:06:07Z State:"" Limit:0}) +ListIssues(77, {Labels:nil UpdatedAfter:2026-03-04T05:07:07Z State:"opened" Limit:0}) +ListIssues(77, {Labels:["agent-found"] UpdatedAfter:2026-03-04T05:08:07Z State:"" Limit:0}) diff --git a/api/internal/forgesvc/testdata/recorded_calls_forgesvc_mrstates.golden b/api/internal/forgesvc/testdata/recorded_calls_forgesvc_mrstates.golden new file mode 100644 index 0000000000..bd7536244d --- /dev/null +++ b/api/internal/forgesvc/testdata/recorded_calls_forgesvc_mrstates.golden @@ -0,0 +1,8 @@ +# opened->closed +GetMergeRequest(77, 13) +EnsureLabels(77, [{Name:"In Progress" Color:"#1f75cb"}]) +UpdateIssueLabels(77, 9, ["In Progress"], ["Human Review"]) +# closed->opened +GetMergeRequest(77, 13) +EnsureLabels(77, [{Name:"Human Review" Color:"#6e49cb"}]) +UpdateIssueLabels(77, 9, ["Human Review"], ["In Progress"]) diff --git a/api/internal/forgesvc/testdata/recorded_calls_forgesvc_prdlinkpatches.golden b/api/internal/forgesvc/testdata/recorded_calls_forgesvc_prdlinkpatches.golden new file mode 100644 index 0000000000..f0c09af88f --- /dev/null +++ b/api/internal/forgesvc/testdata/recorded_calls_forgesvc_prdlinkpatches.golden @@ -0,0 +1,3 @@ +GetMergeRequest(4242, 99) +GetIssue(4242, 7) +UpdateIssueDescription(4242, 7, "Implements prds/done/72-x.md end to end. (edited)") diff --git a/api/internal/handler/admin_github_project_sync.go b/api/internal/handler/admin_github_project_sync.go index 3de75aa4d5..cccfa7c54e 100644 --- a/api/internal/handler/admin_github_project_sync.go +++ b/api/internal/handler/admin_github_project_sync.go @@ -460,7 +460,10 @@ func writeProjectSyncError(w http.ResponseWriter, op string, err error) { httpx.Error(w, http.StatusNotFound, "repo not found") case errors.Is(err, forgesvc.ErrProjectSyncNotLinked): httpx.Error(w, http.StatusNotFound, err.Error()) + case errors.Is(store.MapNeverEnabled(err), store.ErrRepoNeverEnabled): + writeNeverEnabled(w) case errors.Is(err, forgesvc.ErrProjectSyncDisabled), + errors.Is(err, forgesvc.ErrProjectSyncLinkedRepo), errors.Is(err, forgesvc.ErrProjectSyncAlreadyLinked): httpx.Error(w, http.StatusConflict, err.Error()) case errors.Is(err, forgesvc.ErrProjectSyncNotGitHub), diff --git a/api/internal/handler/board.go b/api/internal/handler/board.go index 1ece5851fa..92a57358f5 100644 --- a/api/internal/handler/board.go +++ b/api/internal/handler/board.go @@ -313,6 +313,11 @@ func (h *Handler) GetBoard(w http.ResponseWriter, r *http.Request) { if !ok { return } + // PRD 4173 D13: a never-enabled repo is neither seeded nor synced; the board opens + // only after the first enable. + if refuseNeverEnabled(w, repo.AwaitingFirstEnable) { + return + } count, err := h.q.CountBoardColumns(r.Context(), repo.ID) if err != nil { @@ -356,16 +361,19 @@ func (h *Handler) ensureHumanReviewColumn(w http.ResponseWriter, r *http.Request return true // already present — idempotent, no forge or DB work } - tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return false } - // Color matches DefaultColumns' Human Review entry. - if err := tr.EnsureLabels(r.Context(), repo.ForgeProjectID, []forge.Label{{Name: board.ColumnHumanReview, Color: "#6e49cb"}}); err != nil { - slog.Warn("ensure Human Review label on the forge", "repo", repo.PathWithNamespace, "error", err) - return true // non-fatal: render with existing columns, retry next load + // Color matches DefaultColumns' Human Review entry. A linked issue tracker gets no + // label write (PRD #4173 M4); the column row is still added below. + if !repo.IssueTrackerConnectionID.Valid { + if err := tr.EnsureLabels(r.Context(), repo.ForgeProjectID, []forge.Label{{Name: board.ColumnHumanReview, Color: "#6e49cb"}}); err != nil { + slog.Warn("ensure Human Review label on the forge", "repo", repo.PathWithNamespace, "error", err) + return true // non-fatal: render with existing columns, retry next load + } } // Bump the columns Human Review displaces up one so the new column lands right // after In Progress with a distinct position — the same RELATIVE placement fresh @@ -423,13 +431,22 @@ func humanReviewPlacement(cols []store.BoardColumn) (pos int, needed bool) { // writing an error) only when the label-seeding step fails; an import failure // is logged but non-fatal (the board still renders and Refresh retries). func (h *Handler) seedBoard(w http.ResponseWriter, r *http.Request, repo store.GetRepoForUserRow) bool { - tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return false } - if err := forgesvc.EnsureBoardColumns(r.Context(), h.q, tr, repo.ID, repo.ForgeProjectID); err != nil { + linked := repo.IssueTrackerConnectionID.Valid + var boardErr error + if linked { + // No labels are created on a linked issue tracker (writes arrive in M5): the + // column rows are seeded from the store alone. + boardErr = forgesvc.EnsureBoardColumnRows(r.Context(), h.q, repo.ID) + } else { + boardErr = forgesvc.EnsureBoardColumns(r.Context(), h.q, tr, repo.ID, repo.ForgeProjectID) + } + if err := boardErr; err != nil { var seedErr *forgesvc.LabelSeedError if errors.As(err, &seedErr) { httpx.Error(w, http.StatusBadGateway, "could not seed board labels on the forge: "+seedErr.Error()) @@ -439,7 +456,7 @@ func (h *Handler) seedBoard(w http.ResponseWriter, r *http.Request, repo store.G httpx.Error(w, http.StatusInternalServerError, "internal error") return false } - if _, err := h.svc.FullSync(r.Context(), repo.ID, repo.ForgeProjectID, tr); err != nil { + if _, err := h.svc.SyncIssues(r.Context(), repo.ID, repo.ForgeProjectID, tr); err != nil { // Non-fatal: the board renders empty and the poller/Refresh will fill it. slog.Warn("initial board import failed", "repo", repo.PathWithNamespace, "error", err) } @@ -704,6 +721,10 @@ func (h *Handler) ConfigureColumns(w http.ResponseWriter, r *http.Request) { if !ok { return } + // PRD 4173 D13: no label seeding on a never-enabled repo. + if refuseNeverEnabled(w, repo.AwaitingFirstEnable) { + return + } var req configureColumnsRequest if err := httpx.DecodeJSON(r, &req); err != nil { httpx.Error(w, http.StatusBadRequest, "invalid request body") @@ -741,7 +762,7 @@ func (h *Handler) ConfigureColumns(w http.ResponseWriter, r *http.Request) { return } - tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -753,6 +774,9 @@ func (h *Handler) ConfigureColumns(w http.ResponseWriter, r *http.Request) { labels[i] = forge.Label{Name: n, Color: defaultColumnColor} } if err := tr.EnsureLabels(r.Context(), repo.ForgeProjectID, labels); err != nil { + if writeIssueWriteNotOffered(w, err) { + return + } httpx.Error(w, http.StatusBadGateway, "could not ensure column labels on the forge: "+err.Error()) return } @@ -996,7 +1020,7 @@ func (h *Handler) MoveIssue(w http.ResponseWriter, r *http.Request) { return } - tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -1021,6 +1045,9 @@ func (h *Handler) MoveIssue(w http.ResponseWriter, r *http.Request) { updated, err = h.svc.AutoMove(r.Context(), tr, repo.ForgeProjectID, issue, cols, target) } if err != nil { + if writeIssueWriteNotOffered(w, err) { + return + } httpx.Error(w, http.StatusBadGateway, "could not update the issue on the forge: "+err.Error()) return } @@ -1136,7 +1163,7 @@ func (h *Handler) PromoteIssue(w http.ResponseWriter, r *http.Request) { return } - tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -1148,6 +1175,9 @@ func (h *Handler) PromoteIssue(w http.ResponseWriter, r *http.Request) { // with no forge call, so a double click costs nothing. updated, err := h.svc.SetIssueLabel(r.Context(), tr, repo.ForgeProjectID, issue, label, forgesvc.PromoteLabelColor, true) if err != nil { + if writeIssueWriteNotOffered(w, err) { + return + } httpx.Error(w, http.StatusBadGateway, "could not update the issue label on the forge: "+err.Error()) return } @@ -1221,13 +1251,20 @@ func (h *Handler) SyncRepo(w http.ResponseWriter, r *http.Request) { if !ok { return } - tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + if refuseNeverEnabled(w, repo.AwaitingFirstEnable) { + return + } + tr, err := h.issueTrackerForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return } - if _, err := h.svc.FullSync(r.Context(), repo.ID, repo.ForgeProjectID, tr); err != nil { + if _, err := h.svc.SyncIssues(r.Context(), repo.ID, repo.ForgeProjectID, tr); err != nil { + if errors.Is(err, forgesvc.ErrLinkedSyncNotReady) { + httpx.Error(w, http.StatusConflict, "issue sync for a repo linked to an issue tracker is not available yet") + return + } httpx.Error(w, http.StatusBadGateway, "sync failed: "+err.Error()) return } diff --git a/api/internal/handler/chat.go b/api/internal/handler/chat.go index a5aad67de0..d00cacf4f6 100644 --- a/api/internal/handler/chat.go +++ b/api/internal/handler/chat.go @@ -10,6 +10,7 @@ import ( "github.com/google/uuid" "github.com/vtmocanu/uzi/api/internal/httpx" + "github.com/vtmocanu/uzi/api/internal/issuesource" mw "github.com/vtmocanu/uzi/api/internal/middleware" "github.com/vtmocanu/uzi/api/internal/workersvc" ) @@ -346,6 +347,10 @@ func (h *Handler) ConfirmProposal(w http.ResponseWriter, r *http.Request) { switch { case errors.Is(err, workersvc.ErrProposalRepoGone): httpx.Error(w, http.StatusNotFound, "the proposal's target repo is no longer available") + case errors.Is(err, workersvc.ErrRepoNeverEnabled): + writeNeverEnabled(w) + case errors.Is(err, issuesource.ErrIssueWriteNotOffered): + writeIssueWriteNotOffered(w, err) case errors.Is(err, workersvc.ErrForgeIssueWrite): // err wraps the driver's already-redacted message. httpx.Error(w, http.StatusBadGateway, err.Error()) diff --git a/api/internal/handler/ci_fix.go b/api/internal/handler/ci_fix.go index dc99d26c40..05e3aea1eb 100644 --- a/api/internal/handler/ci_fix.go +++ b/api/internal/handler/ci_fix.go @@ -32,6 +32,10 @@ func (h *Handler) CreateCIFixRun(w http.ResponseWriter, r *http.Request) { if !ok { return } + // PRD 4173 D13: no run on a never-enabled repo, refused before any pipeline read. + if refuseNeverEnabled(w, repo.AwaitingFirstEnable) { + return + } user, _ := mw.UserFromContext(r.Context()) var req struct { Ref string `json:"ref"` @@ -64,7 +68,7 @@ func (h *Handler) CreateCIFixRun(w http.ResponseWriter, r *http.Request) { return } - ch, err := h.codeHostForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + ch, err := h.codeHostForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -122,6 +126,8 @@ func (h *Handler) CreateCIFixRun(w http.ResponseWriter, r *http.Request) { // GetMergeRequestSummary (PAT-redacted by the driver); database and forge // construction errors are logged in workersvc and never reach this body. httpx.Error(w, http.StatusBadGateway, err.Error()) + case errors.Is(err, workersvc.ErrRepoNeverEnabled): + writeNeverEnabled(w) case errors.Is(err, workersvc.ErrRepoNotFound): httpx.Error(w, http.StatusNotFound, "repo not found") case errors.Is(err, workersvc.ErrActiveFixExists): diff --git a/api/internal/handler/findings_file.go b/api/internal/handler/findings_file.go index f903d6a88a..e3186100ce 100644 --- a/api/internal/handler/findings_file.go +++ b/api/internal/handler/findings_file.go @@ -91,6 +91,10 @@ func (h *Handler) FileFinding(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusInternalServerError, "internal error") return } + // PRD 4173 D13: before the claim and the forge write. + if refuseNeverEnabled(w, repo.AwaitingFirstEnable) { + return + } // (3) Resolve the filed title/description (D4). The default is the deterministic template // over the STORED row (agent text never rides in raw — RenderFinding routes each field @@ -149,7 +153,7 @@ func (h *Handler) FileFinding(w http.ResponseWriter, r *http.Request) { return } - tr, err := h.issueTrackerForRepo(ctx, repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(ctx, repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { h.revertFindingClaim(ctx, finding) slog.Error("file finding: build forge for connection", "error", err) @@ -162,6 +166,9 @@ func (h *Handler) FileFinding(w http.ResponseWriter, r *http.Request) { // claim and is a 502 (an upstream forge problem), not a duplicate issue. if err := tr.EnsureLabels(ctx, repo.ForgeProjectID, requiredLabels); err != nil { h.revertFindingClaim(ctx, finding) + if writeIssueWriteNotOffered(w, err) { + return + } httpx.Error(w, http.StatusBadGateway, "could not ensure the finding label on the forge: "+err.Error()) return } @@ -171,6 +178,9 @@ func (h *Handler) FileFinding(w http.ResponseWriter, r *http.Request) { // The forge rejected the write: the claim reverts, nothing is persisted, the finding // stays open and re-fileable. err is already PAT-redacted by the driver. h.revertFindingClaim(ctx, finding) + if writeIssueWriteNotOffered(w, err) { + return + } httpx.Error(w, http.StatusBadGateway, "could not create the issue on the forge: "+err.Error()) return } diff --git a/api/internal/handler/findings_group_file.go b/api/internal/handler/findings_group_file.go index e7aed0316e..4d9b85fadb 100644 --- a/api/internal/handler/findings_group_file.go +++ b/api/internal/handler/findings_group_file.go @@ -14,6 +14,7 @@ import ( "github.com/jackc/pgx/v5" "github.com/vtmocanu/uzi/api/internal/apitypes" "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forgesvc" "github.com/vtmocanu/uzi/api/internal/httpx" "github.com/vtmocanu/uzi/api/internal/issuedraft" mw "github.com/vtmocanu/uzi/api/internal/middleware" @@ -184,7 +185,16 @@ func (h *Handler) FileFindingGroup(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusInternalServerError, "internal error") return } - tr, err := h.issueTrackerForRepo(ctx, repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + // PRD 4173 D13: the group is already claimed, so release it before refusing; nothing + // has touched the forge. + if store.RefuseNeverEnabled(repo.AwaitingFirstEnable) != nil { + if !releaseFindingGroupOrReport(w, op.ID, dispositionIDs, release) { + return + } + writeNeverEnabled(w) + return + } + tr, err := h.issueTrackerForRepo(ctx, repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { if !releaseFindingGroupOrReport(w, op.ID, dispositionIDs, release) { return @@ -208,6 +218,9 @@ func (h *Handler) FileFindingGroup(w http.ResponseWriter, r *http.Request) { if !releaseFindingGroupOrReport(w, op.ID, dispositionIDs, release) { return } + if writeIssueWriteNotOffered(w, err) { + return + } httpx.Error(w, http.StatusBadGateway, "could not ensure the finding label on the forge: "+err.Error()) return } @@ -226,11 +239,16 @@ func (h *Handler) FileFindingGroup(w http.ResponseWriter, r *http.Request) { } created, err := tr.CreateIssue(callCtx, repo.ForgeProjectID, title, description, filedLabels) if err != nil { - if forge.IsCreateIssueDefinitiveRejection(err) { + // The write guard refuses before any request, so nothing was created: release the + // group like a definitive rejection instead of marking it uncertain. + if errors.Is(err, forgesvc.ErrIssueWriteNotOffered) || forge.IsCreateIssueDefinitiveRejection(err) { if !h.releaseDefinitiveFindingGroup(user.ID, op.ID) { writeStoppedFindingGroup(w, op.ID, dispositionIDs, "in_flight") return } + if writeIssueWriteNotOffered(w, err) { + return + } httpx.Error(w, http.StatusBadGateway, "could not create the issue on the forge: "+err.Error()) return } diff --git a/api/internal/handler/forge.go b/api/internal/handler/forge.go index 47ddd5027f..963dce3af2 100644 --- a/api/internal/handler/forge.go +++ b/api/internal/handler/forge.go @@ -845,12 +845,12 @@ func (h *Handler) importAfterEnable(ctx context.Context, userID, repoID uuid.UUI slog.Warn("import after enable: read repo", "error", err) return } - tr, err := h.issueTrackerForRepo(ctx, row.ID, row.ForgeType, row.BaseUrl, row.TokenCiphertext) + tr, err := h.issueTrackerForRepo(ctx, row.UserID, row.ID, row.IssueTrackerConnectionID, row.ForgeType, row.BaseUrl, row.TokenCiphertext) if err != nil { slog.Warn("import after enable: build forge", "error", err) return } - if _, err := h.svc.FullSync(ctx, row.ID, row.ForgeProjectID, tr); err != nil { + if _, err := h.svc.SyncIssues(ctx, row.ID, row.ForgeProjectID, tr); err != nil { slog.Warn("import after enable failed; the poller retries", "repo", row.PathWithNamespace, "error", err) } } diff --git a/api/internal/handler/forgeview.go b/api/internal/handler/forgeview.go index a0b5a59945..fbc5909639 100644 --- a/api/internal/handler/forgeview.go +++ b/api/internal/handler/forgeview.go @@ -185,7 +185,7 @@ func (h *Handler) ListPulls(w http.ResponseWriter, r *http.Request) { if !ok { return } - ch, err := h.codeHostForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + ch, err := h.codeHostForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("forge view: build forge", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -294,7 +294,7 @@ func (h *Handler) GetPull(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusBadRequest, "invalid pull id") return } - ch, err := h.codeHostForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + ch, err := h.codeHostForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("forge view: build forge", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -393,7 +393,7 @@ func (h *Handler) ListCIRuns(w http.ResponseWriter, r *http.Request) { if limit > forgeViewCIRunsMax { limit = forgeViewCIRunsMax } - ch, err := h.codeHostForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + ch, err := h.codeHostForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("forge view: build forge", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -445,7 +445,7 @@ func (h *Handler) GetCIRun(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusBadRequest, "invalid run id") return } - ch, err := h.codeHostForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + ch, err := h.codeHostForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("forge view: build forge", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") diff --git a/api/internal/handler/handler.go b/api/internal/handler/handler.go index 2da5f93a0d..4268f097b4 100644 --- a/api/internal/handler/handler.go +++ b/api/internal/handler/handler.go @@ -30,6 +30,7 @@ import ( "github.com/vtmocanu/uzi/api/internal/hostedsvc" "github.com/vtmocanu/uzi/api/internal/httpx" "github.com/vtmocanu/uzi/api/internal/hub" + "github.com/vtmocanu/uzi/api/internal/issuetrackersvc" mw "github.com/vtmocanu/uzi/api/internal/middleware" "github.com/vtmocanu/uzi/api/internal/notifysvc" "github.com/vtmocanu/uzi/api/internal/oidc" @@ -187,6 +188,9 @@ type Handler struct { // — which is mounted only when WORKER_HOSTING_ENABLED is true, so it stays nil // on a compose stack and in every test that does not exercise hosting. hsvc *hostedsvc.Service + // issueTrackers serves the issue tracker connection routes and the repo link + // (PRD #4173 M4); set by SetIssueTrackerService. Left unset, those routes answer 503. + issueTrackers *issuetrackersvc.Service // usagePoker pokes the rate-limit poller when a user saves/replaces their // Anthropic token (PRD #53 D3b), so their meters appear within seconds instead // of up to a full poll interval. Wired via SetUsagePoker; nil-safe (the poller @@ -343,6 +347,10 @@ type ProjectSyncer interface { // github-project-sync routes then return a clean 500 rather than panic. func (h *Handler) SetProjectSync(p ProjectSyncer) { h.projectSync = p } +// SetIssueTrackerService wires the issue tracker connection and link service (PRD #4173 M4) +// after construction. Safe to leave unset: its routes then answer 503 rather than panic. +func (h *Handler) SetIssueTrackerService(s *issuetrackersvc.Service) { h.issueTrackers = s } + // SetAgentSourceReconciler wires the agent-source reconciler (PRD #602 M4) after // construction. Safe to leave unset — the admin agent-source endpoints then return a // clean 500 rather than panic (struct-literal test handlers that don't exercise them). @@ -476,24 +484,30 @@ func (h *Handler) templateWriteStore() agentTemplateWriteStore { return h.q } -// issueTrackerForRepo builds the issue-tracker half of a repo's forge, going through -// the injected forgeFactory when one is set and otherwise through -// forgesvc.Service.IssueTrackerForRepo. It returns a nil issue tracker whenever err != nil, -// on both paths. nil-safe only when a factory is injected; in production h.svc is -// always wired. -func (h *Handler) issueTrackerForRepo(ctx context.Context, repoID uuid.UUID, forgeType, baseURL string, tokenCiphertext []byte) (forge.IssueTracker, error) { +// issueTrackerForRepo builds the issue-tracker half of a repo's forge for the repo's +// owner (PRD #4173 DL-5). link is the repo row's issue_tracker_connection_id: the +// injected forgeFactory test seam is taken only for an unlinked row (a linked row +// through it is an error, so no test or wiring gap can reach the forge for a linked +// repo); otherwise forgesvc.Service.IssueTrackerForRepo resolves the row itself. It +// returns a nil issue tracker whenever err != nil. nil-safe only when a factory is +// injected; in production h.svc is always wired. +func (h *Handler) issueTrackerForRepo(ctx context.Context, userID, repoID uuid.UUID, link pgtype.UUID, forgeType, baseURL string, tokenCiphertext []byte) (forge.IssueTracker, error) { if h.forgeFactory != nil { + if link.Valid { + return nil, forgesvc.ErrLinkedRepoUnresolvable + } f, err := h.forgeFactory(forgeType, baseURL, tokenCiphertext) if err != nil { return nil, err } return f, nil } - return h.svc.IssueTrackerForRepo(ctx, repoID, forgeType, baseURL, tokenCiphertext) + return h.svc.IssueTrackerForRepo(ctx, userID, repoID) } -// codeHostForRepo is issueTrackerForRepo for the code-host half. -func (h *Handler) codeHostForRepo(ctx context.Context, repoID uuid.UUID, forgeType, baseURL string, tokenCiphertext []byte) (forge.CodeHost, error) { +// codeHostForRepo is issueTrackerForRepo for the code-host half; the link never +// changes it (S2), so the factory seam is taken for linked rows too. +func (h *Handler) codeHostForRepo(ctx context.Context, userID, repoID uuid.UUID, _ pgtype.UUID, forgeType, baseURL string, tokenCiphertext []byte) (forge.CodeHost, error) { if h.forgeFactory != nil { f, err := h.forgeFactory(forgeType, baseURL, tokenCiphertext) if err != nil { @@ -501,7 +515,7 @@ func (h *Handler) codeHostForRepo(ctx context.Context, repoID uuid.UUID, forgeTy } return f, nil } - return h.svc.CodeHostForRepo(ctx, repoID, forgeType, baseURL, tokenCiphertext) + return h.svc.CodeHostForRepo(ctx, userID, repoID) } // memo returns the process-wide forge-view read memo (PRD #1255 M2b), constructing @@ -961,6 +975,7 @@ func (h *Handler) Routes(authLimiter, forgeLimiter, slackDMLimiter, chatLimiter, h.mountAgentRoutes(r) h.mountAdminRoutes(r, forgeLimiter, authLimiter) h.mountForgeRoutes(r, forgeLimiter) + h.mountIssueTrackerRoutes(r, forgeLimiter) h.mountRepoRoutes(r, forgeLimiter, boardOrderLimiter) h.mountWorkersRoutes(r, hostedLimiter) // The stable external API (PRD #1907): its own auth (RequireV1Caller, Bearer diff --git a/api/internal/handler/issue_trackers.go b/api/internal/handler/issue_trackers.go new file mode 100644 index 0000000000..f8086890da --- /dev/null +++ b/api/internal/handler/issue_trackers.go @@ -0,0 +1,300 @@ +package handler + +import ( + "errors" + "log/slog" + "net/http" + "strings" + "time" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/httpx" + "github.com/vtmocanu/uzi/api/internal/issuetracker" + "github.com/vtmocanu/uzi/api/internal/issuetrackersvc" + mw "github.com/vtmocanu/uzi/api/internal/middleware" +) + +// issueTrackerConnectionDTO is the wire form of a connection. It has no token or +// ciphertext field, like issuetrackersvc.Connection. +type issueTrackerConnectionDTO struct { + ID uuid.UUID `json:"id"` + Kind string `json:"kind"` + BaseURL string `json:"base_url"` + AccountID string `json:"account_id"` + AccountLogin string `json:"account_login"` + CreatedAt time.Time `json:"created_at"` + LastVerifiedAt time.Time `json:"last_verified_at"` +} + +// cleanTrackerText drops control, bidirectional, zero-width and other invisible characters +// from text that came from the issue tracker, so a hostile name cannot reorder or hide +// text in the browser. The predicate is the one the driver and the service already use. +func cleanTrackerText(s string) string { + return strings.Map(func(r rune) rune { + if issuetracker.InvisibleRune(r) { + return -1 + } + return r + }, s) +} + +func toIssueTrackerConnectionDTO(c issuetrackersvc.Connection) issueTrackerConnectionDTO { + return issueTrackerConnectionDTO{ + ID: c.ID, Kind: c.Kind, BaseURL: c.BaseURL, AccountID: c.AccountID, + AccountLogin: c.AccountLogin, CreatedAt: c.CreatedAt, LastVerifiedAt: c.LastVerifiedAt, + } +} + +type issueTrackerAccountDTO struct { + ID string `json:"id"` + Login string `json:"login"` +} + +type issueTrackerGroupDTO struct { + ID string `json:"id"` + Name string `json:"name"` + AllUsers bool `json:"all_users"` + UsersCount int `json:"users_count"` +} + +type issueTrackerSharingDTO struct { + Groups []issueTrackerGroupDTO `json:"groups"` + Users []issueTrackerAccountDTO `json:"users"` +} + +func toIssueTrackerSharingDTO(s issuetracker.SharingSettings) issueTrackerSharingDTO { + out := issueTrackerSharingDTO{ + Groups: make([]issueTrackerGroupDTO, 0, len(s.Groups)), + Users: make([]issueTrackerAccountDTO, 0, len(s.Users)), + } + for _, g := range s.Groups { + out.Groups = append(out.Groups, issueTrackerGroupDTO{ID: cleanTrackerText(g.ID), Name: cleanTrackerText(g.Name), AllUsers: g.AllUsers, UsersCount: g.UsersCount}) + } + for _, u := range s.Users { + out.Users = append(out.Users, issueTrackerAccountDTO{ID: cleanTrackerText(u.ID), Login: cleanTrackerText(u.Login)}) + } + return out +} + +type issueTrackerProjectDTO struct { + ID string `json:"id"` + ShortName string `json:"short_name"` + Name string `json:"name"` +} + +// issueTrackerTagDTO is a tag with the two sharing verdicts a link requires, judged for +// the connection's account. The verdicts speak about who can apply the tag only; they do +// not say who can edit issues in a project. +type issueTrackerTagDTO struct { + ID string `json:"id"` + Name string `json:"name"` + Owner issueTrackerAccountDTO `json:"owner"` + Read issueTrackerSharingDTO `json:"read"` + Use issueTrackerSharingDTO `json:"use"` + AccountCanApply bool `json:"account_can_apply"` + OthersCanApply bool `json:"others_can_apply"` +} + +type issueTrackerLinkDTO struct { + RepoID uuid.UUID `json:"repo_id"` + ConnectionID uuid.UUID `json:"connection_id"` + ProjectID string `json:"project_id"` + ProjectShortName string `json:"project_short_name"` + TagID string `json:"tag_id"` + TagName string `json:"tag_name"` + LinkedAt time.Time `json:"linked_at"` +} + +type createIssueTrackerConnectionRequest struct { + Kind string `json:"kind"` + BaseURL string `json:"base_url"` + Token string `json:"token"` +} + +type linkRepoToIssueTrackerRequest struct { + ConnectionID string `json:"connection_id"` + ProjectID string `json:"project_id"` + TagID string `json:"tag_id"` +} + +// issueTrackerRequest returns the signed-in user and the service, or writes the refusal: +// 401 without a user, 503 when no service is wired. +func (h *Handler) issueTrackerRequest(w http.ResponseWriter, r *http.Request) (uuid.UUID, *issuetrackersvc.Service, bool) { + user, ok := mw.UserFromContext(r.Context()) + if !ok { + httpx.Error(w, http.StatusUnauthorized, "authentication required") + return uuid.Nil, nil, false + } + if h.issueTrackers == nil { + httpx.Error(w, http.StatusServiceUnavailable, "issue trackers are not configured") + return uuid.Nil, nil, false + } + return user.ID, h.issueTrackers, true +} + +// writeIssueTrackerError maps an issuetrackersvc failure to its status. Messages are +// already safe to show: tracker text is token-redacted by the driver, and an internal +// failure carries a fixed message. +func writeIssueTrackerError(w http.ResponseWriter, err error) { + var e *issuetrackersvc.Error + if !errors.As(err, &e) { + slog.Error("issue tracker", "error", err) + httpx.Error(w, http.StatusInternalServerError, "internal error") + return + } + switch e.Kind { + case issuetrackersvc.KindBadRequest: + httpx.Error(w, http.StatusBadRequest, e.Message) + case issuetrackersvc.KindNotFound: + httpx.Error(w, http.StatusNotFound, e.Message) + case issuetrackersvc.KindConflict: + httpx.Error(w, http.StatusConflict, e.Message) + case issuetrackersvc.KindBadGateway: + httpx.Error(w, http.StatusBadGateway, e.Message) + default: + httpx.Error(w, http.StatusInternalServerError, "internal error") + } +} + +// CreateIssueTrackerConnection verifies a token against the issue tracker and stores it +// (POST /api/issue-trackers/connections). With the issue tracker allowlist empty every +// base URL is refused. +func (h *Handler) CreateIssueTrackerConnection(w http.ResponseWriter, r *http.Request) { + userID, svc, ok := h.issueTrackerRequest(w, r) + if !ok { + return + } + var req createIssueTrackerConnectionRequest + if err := httpx.DecodeJSON(r, &req); err != nil { + httpx.Error(w, http.StatusBadRequest, "invalid request body") + return + } + conn, err := svc.Connect(r.Context(), userID, req.Kind, req.BaseURL, req.Token) + if err != nil { + writeIssueTrackerError(w, err) + return + } + httpx.JSON(w, http.StatusCreated, map[string]any{"connection": toIssueTrackerConnectionDTO(conn)}) +} + +// ListIssueTrackerConnections lists the user's issue tracker connections. +func (h *Handler) ListIssueTrackerConnections(w http.ResponseWriter, r *http.Request) { + userID, svc, ok := h.issueTrackerRequest(w, r) + if !ok { + return + } + conns, err := svc.List(r.Context(), userID) + if err != nil { + writeIssueTrackerError(w, err) + return + } + out := make([]issueTrackerConnectionDTO, 0, len(conns)) + for _, c := range conns { + out = append(out, toIssueTrackerConnectionDTO(c)) + } + httpx.JSON(w, http.StatusOK, map[string]any{"connections": out}) +} + +// DeleteIssueTrackerConnection deletes a connection; 409 while a repo is linked to it. +func (h *Handler) DeleteIssueTrackerConnection(w http.ResponseWriter, r *http.Request) { + userID, svc, ok := h.issueTrackerRequest(w, r) + if !ok { + return + } + id, ok := httpx.PathUUID(w, r, "id", "connection") + if !ok { + return + } + if err := svc.Delete(r.Context(), userID, id); err != nil { + writeIssueTrackerError(w, err) + return + } + w.WriteHeader(http.StatusNoContent) +} + +// ListIssueTrackerProjects lists the projects the connection's token can see. +func (h *Handler) ListIssueTrackerProjects(w http.ResponseWriter, r *http.Request) { + userID, svc, ok := h.issueTrackerRequest(w, r) + if !ok { + return + } + id, ok := httpx.PathUUID(w, r, "id", "connection") + if !ok { + return + } + projects, err := svc.ListProjects(r.Context(), userID, id) + if err != nil { + writeIssueTrackerError(w, err) + return + } + out := make([]issueTrackerProjectDTO, 0, len(projects)) + for _, p := range projects { + out = append(out, issueTrackerProjectDTO{ID: cleanTrackerText(p.ID), ShortName: cleanTrackerText(p.ShortName), Name: cleanTrackerText(p.Name)}) + } + httpx.JSON(w, http.StatusOK, map[string]any{"projects": out}) +} + +// ListIssueTrackerTags lists the tags the connection's token can see, each with its +// owner, sharing settings and whether the connection's account (and anyone else) can +// apply it. +func (h *Handler) ListIssueTrackerTags(w http.ResponseWriter, r *http.Request) { + userID, svc, ok := h.issueTrackerRequest(w, r) + if !ok { + return + } + id, ok := httpx.PathUUID(w, r, "id", "connection") + if !ok { + return + } + views, err := svc.ListTagsWithVerdicts(r.Context(), userID, id) + if err != nil { + writeIssueTrackerError(w, err) + return + } + out := make([]issueTrackerTagDTO, 0, len(views)) + for _, v := range views { + out = append(out, issueTrackerTagDTO{ + ID: cleanTrackerText(v.Tag.ID), Name: cleanTrackerText(v.Tag.Name), + Owner: issueTrackerAccountDTO{ID: cleanTrackerText(v.Tag.Owner.ID), Login: cleanTrackerText(v.Tag.Owner.Login)}, + Read: toIssueTrackerSharingDTO(v.Tag.Read), + Use: toIssueTrackerSharingDTO(v.Tag.Use), + AccountCanApply: v.AccountCanApply, + OthersCanApply: v.OthersCanApply, + }) + } + httpx.JSON(w, http.StatusOK, map[string]any{"tags": out}) +} + +// LinkRepoToIssueTracker links a never-enabled repo to one issue tracker project and +// eligibility tag (PUT /api/repos/{id}/issue-tracker-link). Refusals are 409 with the +// reason; there is no unlink or relink. +func (h *Handler) LinkRepoToIssueTracker(w http.ResponseWriter, r *http.Request) { + userID, svc, ok := h.issueTrackerRequest(w, r) + if !ok { + return + } + repoID, ok := httpx.PathUUID(w, r, "id", "repo") + if !ok { + return + } + var req linkRepoToIssueTrackerRequest + if err := httpx.DecodeJSON(r, &req); err != nil { + httpx.Error(w, http.StatusBadRequest, "invalid request body") + return + } + connID, err := uuid.Parse(req.ConnectionID) + if err != nil { + httpx.Error(w, http.StatusBadRequest, "invalid connection_id") + return + } + link, err := svc.LinkRepo(r.Context(), userID, repoID, connID, req.ProjectID, req.TagID) + if err != nil { + writeIssueTrackerError(w, err) + return + } + httpx.JSON(w, http.StatusOK, map[string]any{"link": issueTrackerLinkDTO{ + RepoID: link.RepoID, ConnectionID: link.ConnectionID, ProjectID: link.ProjectID, + ProjectShortName: link.ProjectShort, TagID: link.TagID, TagName: link.TagName, LinkedAt: link.LinkedAt, + }}) +} diff --git a/api/internal/handler/issue_trackers_livedb_test.go b/api/internal/handler/issue_trackers_livedb_test.go new file mode 100644 index 0000000000..378aa6a61d --- /dev/null +++ b/api/internal/handler/issue_trackers_livedb_test.go @@ -0,0 +1,281 @@ +package handler + +// Live-DB tests of the issue tracker routes (PRD #4173 M4) through the real router: owner +// scoping, the allowlist, the link and its refusals, and the connection delete and reconnect +// rules. Skipped unless UZI_TEST_DATABASE_URL points at a throwaway Postgres. + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" + + "github.com/vtmocanu/uzi/api/internal/issuetracker" + "github.com/vtmocanu/uzi/api/internal/issuetracker/issuetrackertest" + "github.com/vtmocanu/uzi/api/internal/issuetrackersvc" + "github.com/vtmocanu/uzi/api/internal/store" +) + +const itTestHost = "https://issues.example.com" + +var ( + itBot = issuetracker.Account{ID: "1-1", Login: "bot"} + itOther = issuetracker.Account{ID: "1-2", Login: "maintainer"} +) + +// itDriver answers for the account its token names: a token ending in "-other" is the +// second account. +type itDriver struct { + issuetrackertest.BaseFake + token string +} + +func (d *itDriver) VerifyToken(context.Context) (issuetracker.Account, error) { + if strings.HasSuffix(d.token, "-other") { + return itOther, nil + } + return itBot, nil +} + +func (*itDriver) ListProjects(context.Context) ([]issuetracker.Project, error) { + return []issuetracker.Project{{ID: "0-1", ShortName: "UZI", Name: "Uzi\u202e"}}, nil +} + +func (*itDriver) ListTags(context.Context) ([]issuetracker.Tag, error) { + all := issuetracker.Group{ID: "675-0", Name: "All Users", AllUsers: true} + return []issuetracker.Tag{ + {ID: "5-1", Name: "uzi-ready", Owner: itBot, Use: issuetracker.SharingSettings{Groups: []issuetracker.Group{all}}}, + {ID: "5-2", Name: "private", Owner: itBot}, + // Hostile names: a bidi override and a newline must not reach the browser. + {ID: "5-3", Name: "evil\u202ename\n", Owner: issuetracker.Account{ID: "1-2", Login: "mal\u202elory"}, + Use: issuetracker.SharingSettings{Groups: []issuetracker.Group{{ID: "675-9", Name: "gr\noup\u200b"}}, Users: []issuetracker.Account{{ID: "1-3", Login: "us\u202eer"}}}}, + }, nil +} + +type itFixture struct { + t *testing.T + pool *pgxpool.Pool + q *store.Queries + h *Handler + router http.Handler + user uuid.UUID + jwt string + forgeConn uuid.UUID +} + +func newITFixture(t *testing.T, allowed bool) *itFixture { + t.Helper() + h, router, pool := cliLiveDB(t) + q := store.New(pool) + allow := func(u string) bool { return allowed && u == itTestHost } + h.SetIssueTrackerService(issuetrackersvc.New(q, h.box, allow, + func(_ issuetracker.Kind, o issuetracker.Options) (issuetracker.Driver, error) { + return &itDriver{token: o.Token}, nil + }).UseTransactions(pool, func(tx pgx.Tx) issuetrackersvc.Querier { return q.WithTx(tx) })) + f := &itFixture{t: t, pool: pool, q: q, h: h, router: router, user: cliSeedUser(t, pool, false)} + f.jwt = cliMintJWT(t, pool, f.user) + t.Cleanup(func() { _, _ = pool.Exec(context.Background(), `DELETE FROM users WHERE id = $1`, f.user) }) + f.forgeConn = uuid.New() + cliMustExec(t, pool, `INSERT INTO forge_connections (id, user_id, forge_type, base_url, bot_username, bot_forge_user_id, token_ciphertext) + VALUES ($1, $2, 'gitlab', $3, 'bot', 1, $4)`, f.forgeConn, f.user, "https://"+f.forgeConn.String()+".e2e", []byte{0x1}) + return f +} + +// freshRepo is a repo as the membership listing creates it. +func (f *itFixture) freshRepo() uuid.UUID { + f.t.Helper() + r, err := f.q.UpsertRepo(context.Background(), store.UpsertRepoParams{ + ConnectionID: f.forgeConn, ForgeProjectID: int64(uuid.New().ID()), PathWithNamespace: "g/r", WebUrl: "https://forge.e2e/g/r", + }) + if err != nil { + f.t.Fatal(err) + } + return r.ID +} + +func (f *itFixture) do(jwt, method, path, body string) *httptest.ResponseRecorder { + f.t.Helper() + return cookieReq(f.t, f.router, method, path, jwt, body) +} + +func (f *itFixture) connect(jwt, token string) (int, string, uuid.UUID) { + f.t.Helper() + rec := f.do(jwt, http.MethodPost, "/api/issue-trackers/connections", + fmt.Sprintf(`{"kind":"youtrack","base_url":%q,"token":%q}`, itTestHost, token)) + var out struct { + Connection struct { + ID uuid.UUID `json:"id"` + } `json:"connection"` + } + _ = json.Unmarshal(rec.Body.Bytes(), &out) + return rec.Code, rec.Body.String(), out.Connection.ID +} + +func itToken(suffix string) string { return "perm-it-route-token-0123456789abcdef-" + suffix } + +func linkBody(conn uuid.UUID, tag string) string { + return fmt.Sprintf(`{"connection_id":%q,"project_id":"0-1","tag_id":%q}`, conn, tag) +} + +func TestIssueTrackerConnectionRoutesLiveDB(t *testing.T) { + f := newITFixture(t, true) + token := itToken("a") + code, body, conn := f.connect(f.jwt, token) + if code != http.StatusCreated { + t.Fatalf("connect = %d\n%s", code, body) + } + // The response never carries the token or any ciphertext field. + if strings.Contains(body, token) || strings.Contains(strings.ToLower(body), "ciphertext") || strings.Contains(body, `"token"`) { + t.Fatalf("connect response leaks secret material: %s", body) + } + rec := f.do(f.jwt, http.MethodGet, "/api/issue-trackers/connections", "") + if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), conn.String()) || strings.Contains(rec.Body.String(), token) { + t.Fatalf("list = %d %s", rec.Code, rec.Body.String()) + } + rec = f.do(f.jwt, http.MethodGet, "/api/issue-trackers/connections/"+conn.String()+"/projects", "") + projectsBody := rec.Body.String() + if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"short_name":"UZI"`) { + t.Fatalf("projects = %d %s", rec.Code, rec.Body.String()) + } + rec = f.do(f.jwt, http.MethodGet, "/api/issue-trackers/connections/"+conn.String()+"/tags", "") + var tags struct { + Tags []struct { + ID string `json:"id"` + AccountCanApply bool `json:"account_can_apply"` + OthersCanApply bool `json:"others_can_apply"` + } `json:"tags"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &tags); err != nil || rec.Code != http.StatusOK || len(tags.Tags) != 3 { + t.Fatalf("tags = %d %s (%v)", rec.Code, rec.Body.String(), err) + } + for _, tg := range tags.Tags { + if tg.ID == "5-3" { + continue + } + want := tg.ID == "5-1" // the shared tag; the private one has no one else + if !tg.AccountCanApply || tg.OthersCanApply != want { + t.Errorf("tag %s: verdicts = %v/%v", tg.ID, tg.AccountCanApply, tg.OthersCanApply) + } + } + // Names from the tracker reach the browser without control, bidi or zero-width characters. + for _, resp := range []string{projectsBody, rec.Body.String()} { + for _, bad := range []string{`\u202e`, `\u200b`, `\n`} { + if strings.Contains(resp, bad) { + t.Errorf("response still carries %s: %s", bad, resp) + } + } + } + if !strings.Contains(rec.Body.String(), `"name":"evilname"`) || !strings.Contains(rec.Body.String(), `"login":"mallory"`) || !strings.Contains(projectsBody, `"name":"Uzi"}`) { + t.Errorf("sanitised names missing: %s | %s", rec.Body.String(), projectsBody) + } + if rec = f.do(f.jwt, http.MethodDelete, "/api/issue-trackers/connections/"+conn.String(), ""); rec.Code != http.StatusNoContent { + t.Fatalf("delete = %d %s", rec.Code, rec.Body.String()) + } +} + +func TestIssueTrackerConnectionRefusedWithAnEmptyAllowlistLiveDB(t *testing.T) { + f := newITFixture(t, false) + code, body, _ := f.connect(f.jwt, itToken("a")) + if code != http.StatusBadRequest || !strings.Contains(body, "allowed") { + t.Fatalf("connect = %d %s, want 400 naming the allowlist", code, body) + } + var n int + if err := f.pool.QueryRow(context.Background(), `SELECT count(*) FROM issue_tracker_connections WHERE user_id = $1`, f.user).Scan(&n); err != nil || n != 0 { + t.Fatalf("stored %d connections (%v)", n, err) + } +} + +func TestIssueTrackerRoutesAreOwnerScopedLiveDB(t *testing.T) { + f := newITFixture(t, true) + _, _, conn := f.connect(f.jwt, itToken("a")) + repo := f.freshRepo() + stranger := cliSeedUser(t, f.pool, false) + t.Cleanup(func() { _, _ = f.pool.Exec(context.Background(), `DELETE FROM users WHERE id = $1`, stranger) }) + sjwt := cliMintJWT(t, f.pool, stranger) + + for _, rt := range []struct{ method, path, body string }{ + {http.MethodGet, "/api/issue-trackers/connections/" + conn.String() + "/projects", ""}, + {http.MethodGet, "/api/issue-trackers/connections/" + conn.String() + "/tags", ""}, + {http.MethodDelete, "/api/issue-trackers/connections/" + conn.String(), ""}, + // The owner's repo is not the stranger's: the link route answers 404 for it too. + {http.MethodPut, "/api/repos/" + repo.String() + "/issue-tracker-link", linkBody(conn, "5-1")}, + } { + if rec := f.do(sjwt, rt.method, rt.path, rt.body); rec.Code != http.StatusNotFound { + t.Errorf("%s %s as a stranger = %d, want 404\n%s", rt.method, rt.path, rec.Code, rec.Body.String()) + } + } + // The owner's repo with the stranger's connection id. + _, _, theirConn := f.connect(sjwt, itToken("b")) + if rec := f.do(f.jwt, http.MethodPut, "/api/repos/"+repo.String()+"/issue-tracker-link", linkBody(theirConn, "5-1")); rec.Code != http.StatusNotFound { + t.Errorf("link with a foreign connection = %d, want 404\n%s", rec.Code, rec.Body.String()) + } + // And the repo is still the owner's, unlinked. + var linked bool + if err := f.pool.QueryRow(context.Background(), `SELECT issue_tracker_connection_id IS NOT NULL FROM repos WHERE id = $1`, repo).Scan(&linked); err != nil || linked { + t.Fatalf("linked = %v (%v)", linked, err) + } +} + +func TestIssueTrackerLinkRouteLiveDB(t *testing.T) { + f := newITFixture(t, true) + _, _, conn := f.connect(f.jwt, itToken("a")) + path := func(repo uuid.UUID) string { return "/api/repos/" + repo.String() + "/issue-tracker-link" } + + repo := f.freshRepo() + rec := f.do(f.jwt, http.MethodPut, path(repo), linkBody(conn, "5-1")) + if rec.Code != http.StatusOK { + t.Fatalf("link = %d\n%s", rec.Code, rec.Body.String()) + } + var out struct { + Link struct { + RepoID, ConnectionID uuid.UUID + ProjectShortName string `json:"project_short_name"` + TagName string `json:"tag_name"` + } `json:"link"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatal(err) + } + if out.Link.ProjectShortName != "UZI" || out.Link.TagName != "uzi-ready" { + t.Fatalf("link body = %s", rec.Body.String()) + } + + if rec = f.do(f.jwt, http.MethodPut, path(repo), linkBody(conn, "5-1")); rec.Code != http.StatusConflict { + t.Errorf("relink = %d, want 409\n%s", rec.Code, rec.Body.String()) + } + enabled := f.freshRepo() + if _, err := f.q.SetRepoEnabledForUser(context.Background(), store.SetRepoEnabledForUserParams{ID: enabled, Enabled: true, UserID: f.user}); err != nil { + t.Fatal(err) + } + if rec = f.do(f.jwt, http.MethodPut, path(enabled), linkBody(conn, "5-1")); rec.Code != http.StatusConflict { + t.Errorf("link of an enabled repo = %d, want 409\n%s", rec.Code, rec.Body.String()) + } + // A tag nobody else can apply is refused with the reason, as a 409. + if rec = f.do(f.jwt, http.MethodPut, path(f.freshRepo()), linkBody(conn, "5-2")); rec.Code != http.StatusConflict || !strings.Contains(rec.Body.String(), "named users") { + t.Errorf("link with a private tag = %d, want 409 saying what to change\n%s", rec.Code, rec.Body.String()) + } + if rec = f.do(f.jwt, http.MethodPut, path(f.freshRepo()), linkBody(conn, "5-404")); rec.Code != http.StatusBadRequest { + t.Errorf("link with an unknown tag = %d, want 400\n%s", rec.Code, rec.Body.String()) + } + if rec = f.do(f.jwt, http.MethodPut, path(f.freshRepo()), `{"connection_id":"nope"}`); rec.Code != http.StatusBadRequest { + t.Errorf("link with a malformed connection id = %d, want 400", rec.Code) + } + + // With a repo linked: the connection cannot be deleted, and cannot change account. + if rec = f.do(f.jwt, http.MethodDelete, "/api/issue-trackers/connections/"+conn.String(), ""); rec.Code != http.StatusConflict { + t.Errorf("delete of a linked connection = %d, want 409\n%s", rec.Code, rec.Body.String()) + } + if code, body, _ := f.connect(f.jwt, itToken("other")); code != http.StatusConflict || !strings.Contains(body, "different account") { + t.Errorf("reconnect with another account = %d, want 409\n%s", code, body) + } + if code, body, id := f.connect(f.jwt, itToken("rotated")); code != http.StatusCreated || id != conn { + t.Errorf("same-account reconnect = %d %s (id %s, want %s)", code, body, id, conn) + } +} diff --git a/api/internal/handler/issue_trackers_test.go b/api/internal/handler/issue_trackers_test.go new file mode 100644 index 0000000000..a5fa8d9555 --- /dev/null +++ b/api/internal/handler/issue_trackers_test.go @@ -0,0 +1,105 @@ +package handler + +import ( + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/vtmocanu/uzi/api/internal/auth" + "github.com/vtmocanu/uzi/api/internal/config" + mw "github.com/vtmocanu/uzi/api/internal/middleware" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// issueTrackerRoutes is every issue tracker route of PRD #4173 M4, with a method and a path +// that resolve through the real router. +var issueTrackerRoutes = []struct{ method, path, body string }{ + {http.MethodPost, "/api/issue-trackers/connections", `{"kind":"youtrack","base_url":"https://x.example.com","token":"t"}`}, + {http.MethodGet, "/api/issue-trackers/connections", ""}, + {http.MethodDelete, "/api/issue-trackers/connections/11111111-1111-1111-1111-111111111111", ""}, + {http.MethodGet, "/api/issue-trackers/connections/11111111-1111-1111-1111-111111111111/projects", ""}, + {http.MethodGet, "/api/issue-trackers/connections/11111111-1111-1111-1111-111111111111/tags", ""}, + {http.MethodPut, "/api/repos/22222222-2222-2222-2222-222222222222/issue-tracker-link", `{"connection_id":"11111111-1111-1111-1111-111111111111","project_id":"0-1","tag_id":"5-1"}`}, +} + +func issueTrackerTestRouter() http.Handler { + h := &Handler{cfg: config.Config{JWTSecret: cliTestSecret, AuthTokenTTL: time.Hour}} + lim := mw.NewLimiter(100000, time.Minute, nil) + return h.Routes(lim, lim, lim, lim, lim, lim, lim, lim, lim, lim, lim) +} + +// Every issue tracker route is session-only: no cookie is a 401 whatever the method. +func TestIssueTrackerRoutesRefuseARequestWithoutASession(t *testing.T) { + router := issueTrackerTestRouter() + for _, rt := range issueTrackerRoutes { + t.Run(rt.method+" "+rt.path, func(t *testing.T) { + req := httptest.NewRequest(rt.method, rt.path, strings.NewReader(rt.body)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + router.ServeHTTP(rec, req) + if rec.Code != http.StatusUnauthorized { + t.Fatalf("status = %d, want 401\n%s", rec.Code, rec.Body.String()) + } + }) + } +} + +// A session cookie without the CSRF header is refused on every state-changing route, and a +// Bearer token does not stand in for the cookie. +func TestIssueTrackerRoutesRefuseAStateChangeWithoutCSRF(t *testing.T) { + router := issueTrackerTestRouter() + for _, rt := range issueTrackerRoutes { + if rt.method == http.MethodGet { + continue + } + t.Run(rt.method+" "+rt.path, func(t *testing.T) { + req := httptest.NewRequest(rt.method, rt.path, strings.NewReader(rt.body)) + req.Header.Set("Content-Type", "application/json") + req.AddCookie(&http.Cookie{Name: auth.AuthCookieName, Value: "any-session-value"}) //nolint:gosec // G124: test-only client cookie on an httptest request. + rec := httptest.NewRecorder() + router.ServeHTTP(rec, req) + if rec.Code != http.StatusForbidden { + t.Fatalf("status = %d, want 403\n%s", rec.Code, rec.Body.String()) + } + + bearer := httptest.NewRequest(rt.method, rt.path, strings.NewReader(rt.body)) + bearer.Header.Set("Authorization", "Bearer uzc_whatever") + bearer.Header.Set("Content-Type", "application/json") + rec = httptest.NewRecorder() + router.ServeHTTP(rec, bearer) + if rec.Code != http.StatusUnauthorized { + t.Fatalf("bearer status = %d, want 401\n%s", rec.Code, rec.Body.String()) + } + }) + } +} + +// A handler with no issue tracker service answers an authenticated request with 503, and an +// unauthenticated one with 401, rather than panicking. +func TestIssueTrackerRoutesAnswer503WithoutAService(t *testing.T) { + h := &Handler{} + handlers := map[string]http.HandlerFunc{ + "create": h.CreateIssueTrackerConnection, "list": h.ListIssueTrackerConnections, + "delete": h.DeleteIssueTrackerConnection, "projects": h.ListIssueTrackerProjects, + "tags": h.ListIssueTrackerTags, "link": h.LinkRepoToIssueTracker, + } + for name, fn := range handlers { + t.Run(name, func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/", nil) + rec := httptest.NewRecorder() + fn(rec, req) + if rec.Code != http.StatusUnauthorized { + t.Fatalf("no user: status = %d, want 401", rec.Code) + } + req = req.WithContext(mw.ContextWithUser(context.Background(), store.User{})) + rec = httptest.NewRecorder() + fn(rec, req) + if rec.Code != http.StatusServiceUnavailable || !strings.Contains(rec.Body.String(), "issue trackers are not configured") { + t.Fatalf("status = %d %s, want 503 naming the missing configuration", rec.Code, rec.Body.String()) + } + }) + } +} diff --git a/api/internal/handler/issue_write_refusal.go b/api/internal/handler/issue_write_refusal.go new file mode 100644 index 0000000000..5a88f3fcea --- /dev/null +++ b/api/internal/handler/issue_write_refusal.go @@ -0,0 +1,21 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/vtmocanu/uzi/api/internal/forgesvc" + "github.com/vtmocanu/uzi/api/internal/httpx" +) + +// writeIssueWriteNotOffered answers the typed refusal the linked-repo write guard +// returns (PRD #4173 M4: YouTrack writes arrive in M5) as a 409 instead of a 502/500, +// and reports whether it did. Callers that claimed something before the write have +// already released it by the time they call this. +func writeIssueWriteNotOffered(w http.ResponseWriter, err error) bool { + if !errors.Is(err, forgesvc.ErrIssueWriteNotOffered) { + return false + } + httpx.Error(w, http.StatusConflict, "issue writes on a repo linked to an issue tracker are not offered yet") + return true +} diff --git a/api/internal/handler/issues.go b/api/internal/handler/issues.go index 757fdbc523..74d55f16e7 100644 --- a/api/internal/handler/issues.go +++ b/api/internal/handler/issues.go @@ -37,6 +37,11 @@ func (h *Handler) CreateIssue(w http.ResponseWriter, r *http.Request) { if !ok { return } + // PRD 4173 D13: refused before the forge write, so nothing is created that the + // issue-number guard would then strand. + if refuseNeverEnabled(w, repo.AwaitingFirstEnable) { + return + } var req createIssueRequest if err := httpx.DecodeJSON(r, &req); err != nil { httpx.Error(w, http.StatusBadRequest, "invalid request body") @@ -52,7 +57,7 @@ func (h *Handler) CreateIssue(w http.ResponseWriter, r *http.Request) { return } - tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -65,6 +70,9 @@ func (h *Handler) CreateIssue(w http.ResponseWriter, r *http.Request) { created, err := tr.CreateIssue(r.Context(), repo.ForgeProjectID, title, req.Description, []string{uziLabel}) if err != nil { // err is already PAT-redacted by the driver. + if writeIssueWriteNotOffered(w, err) { + return + } httpx.Error(w, http.StatusBadGateway, "could not create the issue on the forge: "+err.Error()) return } @@ -207,7 +215,7 @@ func (h *Handler) GetIssueDetail(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusBadRequest, "invalid issue id") return } - tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") diff --git a/api/internal/handler/judge_admin_issue.go b/api/internal/handler/judge_admin_issue.go index 7dc548ee92..035137c50e 100644 --- a/api/internal/handler/judge_admin_issue.go +++ b/api/internal/handler/judge_admin_issue.go @@ -235,6 +235,10 @@ func (h *Handler) AdminFileJudgeIssue(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusInternalServerError, "internal error") return } + // PRD 4173 D13: before the claim and the forge write. + if refuseNeverEnabled(w, repo.AwaitingFirstEnable) { + return + } // Labels are assembled server-side (Decision 3/6): the single uzi run-eligibility label // (PRD #764), never from the request body. @@ -259,7 +263,7 @@ func (h *Handler) AdminFileJudgeIssue(w http.ResponseWriter, r *http.Request) { return } - tr, err := h.issueTrackerForRepo(ctx, repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(ctx, repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { h.revertFiledClaim(ctx, claimID) slog.Error("admin file issue: build forge for connection", "error", err) @@ -271,6 +275,9 @@ func (h *Handler) AdminFileJudgeIssue(w http.ResponseWriter, r *http.Request) { // Forge rejected the write: the claim reverts, nothing persists, the coordinate is // fileable again. err is already PAT-redacted by the driver. h.revertFiledClaim(ctx, claimID) + if writeIssueWriteNotOffered(w, err) { + return + } httpx.Error(w, http.StatusBadGateway, "could not create the issue on the forge: "+err.Error()) return } diff --git a/api/internal/handler/labels.go b/api/internal/handler/labels.go index 26d6a802f9..ce21258238 100644 --- a/api/internal/handler/labels.go +++ b/api/internal/handler/labels.go @@ -27,7 +27,7 @@ func (h *Handler) CheckRepoLabels(w http.ResponseWriter, r *http.Request) { httpx.RespondDecodeError(w, err, "invalid request body") return } - tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("check repo labels: build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -58,7 +58,7 @@ func (h *Handler) EnsureRepoLabels(w http.ResponseWriter, r *http.Request) { httpx.RespondDecodeError(w, err, "invalid request body") return } - tr, err := h.issueTrackerForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("ensure repo labels: build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") @@ -67,6 +67,9 @@ func (h *Handler) EnsureRepoLabels(w http.ResponseWriter, r *http.Request) { ensured, err := ensureRepoLabels(r.Context(), tr, repo.ForgeProjectID, req.Labels) if err != nil { // err is already PAT-redacted by the driver; an upstream forge write failure is a 502. + if writeIssueWriteNotOffered(w, err) { + return + } httpx.Error(w, http.StatusBadGateway, "could not ensure the labels on the forge: "+err.Error()) return } diff --git a/api/internal/handler/linked_repo_collision_livedb_test.go b/api/internal/handler/linked_repo_collision_livedb_test.go new file mode 100644 index 0000000000..a76cfe4818 --- /dev/null +++ b/api/internal/handler/linked_repo_collision_livedb_test.go @@ -0,0 +1,317 @@ +package handler + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "strconv" + "strings" + "testing" + "time" + + "github.com/go-chi/chi/v5" + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/forgesvc" + "github.com/vtmocanu/uzi/api/internal/issuetracker" + "github.com/vtmocanu/uzi/api/internal/issuetracker/youtracktest" + "github.com/vtmocanu/uzi/api/internal/issuetrackersvc" + mw "github.com/vtmocanu/uzi/api/internal/middleware" + "github.com/vtmocanu/uzi/api/internal/poller" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// collisionForge is the forge both connections talk to: it answers one issue and +// accepts the writes, so a call that reaches it is visible in its recorder. +type collisionForge struct{ forgetest.BaseFake } + +func (*collisionForge) GetIssue(_ context.Context, _, iid int64) (forge.Issue, error) { + return forge.Issue{IID: iid, Title: "forge card", State: "opened", Description: "from the forge", Labels: []string{"uzi"}, UpdatedAt: time.Now()}, nil +} +func (*collisionForge) ListIssues(context.Context, int64, forge.ListIssuesOptions) ([]forge.Issue, error) { + return nil, nil +} +func (*collisionForge) ListLabels(context.Context, int64) ([]forge.Label, error) { return nil, nil } +func (*collisionForge) GetMergeRequest(_ context.Context, _, iid int64) (forge.MergeRequest, error) { + return forge.MergeRequest{IID: iid, State: forge.MRStateOpened}, nil +} + +type pairRepoStore struct { + q *store.Queries + repos map[uuid.UUID]bool +} + +func (p pairRepoStore) ListEnabledReposWithConnections(ctx context.Context) ([]store.ListEnabledReposWithConnectionsRow, error) { + rows, err := p.q.ListEnabledReposWithConnections(ctx) + var out []store.ListEnabledReposWithConnectionsRow + for _, r := range rows { + if p.repos[r.ID] { + out = append(out, r) + } + } + return out, err +} + +func issueSideCalls(calls []string) []string { + var out []string + for _, c := range calls { + for _, p := range []string{"ListIssues(", "GetIssue(", "ListLabels(", "EnsureLabels(", "UpdateIssueLabels(", "ListIssueLabelEvents(", "ListIssueComments("} { + if strings.HasPrefix(c, p) { + out = append(out, c) + } + } + } + return out +} + +// PRD #4173 M4 acceptance "collision": two forge connections, two repos with the SAME +// forge project id, one linked to YouTrack and one not. Each repo's issue side, a poller +// tick and a handler issue read reach only its own fake. +func TestLinkedAndUnlinkedRepoWithTheSameForgeProjectIDDoNotCollideLiveDB(t *testing.T) { + h, pool, q, box, _ := fileIssueLiveDB(t) + ctx := context.Background() + + srv := youtracktest.NewServer(t) + def, ok := http.DefaultTransport.(*http.Transport) + if !ok { + t.Fatal("http.DefaultTransport is not an *http.Transport") + } + saved := def.TLSClientConfig + def.TLSClientConfig = srv.Client().Transport.(*http.Transport).TLSClientConfig.Clone() + t.Cleanup(func() { def.TLSClientConfig = saved }) + + recA := forgetest.NewRecorder(&collisionForge{}) + recB := forgetest.NewRecorder(&collisionForge{}) + urlA := "https://" + uuid.NewString() + ".forge-a.test" + urlB := "https://" + uuid.NewString() + ".forge-b.test" + svc := forgesvc.NewWithForgeBuilder(q, box, 5*time.Second, nil, func(_ forge.Type, u, _ string, _ time.Duration) (forge.Forge, error) { + if u == urlA { + return recA, nil + } + return recB, nil + }) + itSvc := issuetrackersvc.New(q, box, func(u string) bool { return u == srv.URL }, issuetracker.New) + svc.SetIssueTrackerDrivers(itSvc) + h.svc = svc + + sealed, err := svc.EncryptToken("forge-token") + if err != nil { + t.Fatal(err) + } + userA := store.User{ID: uuid.New(), Email: fmt.Sprintf("col-a-%s@e2e", uuid.NewString()[:8])} + userB := store.User{ID: uuid.New(), Email: fmt.Sprintf("col-b-%s@e2e", uuid.NewString()[:8])} + connA, connB, repoL, repoP := uuid.New(), uuid.New(), uuid.New(), uuid.New() + exec := func(sql string, args ...any) { + t.Helper() + if _, err := pool.Exec(ctx, sql, args...); err != nil { + t.Fatalf("exec: %v", err) + } + } + exec(`INSERT INTO users (id, email, password_hash) VALUES ($1, $2, 'x'), ($3, $4, 'x')`, userA.ID, userA.Email, userB.ID, userB.Email) + exec(`INSERT INTO forge_connections (id, user_id, forge_type, base_url, bot_username, bot_forge_user_id, token_ciphertext) + VALUES ($1, $2, 'gitlab', $3, 'bot-a', 1, $4), ($5, $6, 'gitlab', $7, 'bot-b', 2, $4)`, connA, userA.ID, urlA, sealed, connB, userB.ID, urlB) + yt, err := itSvc.Connect(ctx, userA.ID, "youtrack", srv.URL, srv.Token) + if err != nil { + t.Fatal(err) + } + const projectID = 77 // the SAME forge project id on both connections + exec(`INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, + issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at) + VALUES ($1, $2, $3, $4, 'https://forge-a.test/g/l', 'main', true, $5, $6, $7, $8, $9, now())`, + repoL, connA, projectID, "g/linked-"+repoL.String(), yt.ID, youtracktest.ProjectID, youtracktest.ProjectShortName, youtracktest.EligibilityTagID, youtracktest.EligibilityTagName) + exec(`INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled) + VALUES ($1, $2, $3, $4, 'https://forge-b.test/g/p', 'main', true)`, repoP, connB, projectID, "g/plain-"+repoP.String()) + issueN := srv.SeedIssue("a youtrack card", "yt body", youtracktest.EligibilityTagID) + + // 1. The resolver. The linked repo reaches only YouTrack, the plain one only its forge. + srv.ResetRequests() + trL, err := svc.IssueTrackerForRepo(ctx, userA.ID, repoL) + if err != nil { + t.Fatal(err) + } + got, err := trL.GetIssue(ctx, projectID, issueN) + if err != nil { + t.Fatalf("linked GetIssue: %v", err) + } + if got.Title != "a youtrack card" { + t.Fatalf("linked repo read %+v, want the YouTrack card", got) + } + if len(srv.Requests()) == 0 { + t.Fatal("the YouTrack fake saw no request for the linked repo") + } + if c := issueSideCalls(recA.Calls()); len(c) != 0 { + t.Fatalf("the linked repo's forge saw issue-side calls: %v", c) + } + ytBefore := len(srv.Requests()) + trP, err := svc.IssueTrackerForRepo(ctx, userB.ID, repoP) + if err != nil { + t.Fatal(err) + } + if got, err := trP.GetIssue(ctx, projectID, issueN); err != nil || got.Title != "forge card" { + t.Fatalf("plain GetIssue = %+v, %v", got, err) + } + if len(srv.Requests()) != ytBefore { + t.Fatal("the plain repo's read reached YouTrack") + } + if c := issueSideCalls(recB.Calls()); len(c) != 1 { + t.Fatalf("the plain repo's forge issue-side calls = %v, want its one GetIssue", c) + } + // Binding is by repo row and owner: the other owner cannot resolve either repo. + for name, call := range map[string]func() error{ + "linked, wrong owner": func() error { _, err := svc.IssueTrackerForRepo(ctx, userB.ID, repoL); return err }, + "plain, wrong owner": func() error { _, err := svc.IssueTrackerForRepo(ctx, userA.ID, repoP); return err }, + } { + if err := call(); err == nil { + t.Errorf("%s: resolved", name) + } + } + + // 2. A poller tick per repo. The plain repo's tick makes issue-side forge calls and + // never reaches YouTrack; the linked repo's tick makes no issue-side forge call. + tick := func(repo uuid.UUID) { + e := poller.New(svc, pairRepoStore{q: q, repos: map[uuid.UUID]bool{repo: true}}, time.Minute, 3) + e.PollOnce(ctx) + } + ytBefore, aBefore, bBefore := len(srv.Requests()), len(recA.Calls()), len(recB.Calls()) + tick(repoP) + if len(srv.Requests()) != ytBefore { + t.Fatal("the plain repo's poller tick reached YouTrack") + } + if len(recA.Calls()) != aBefore { + t.Fatal("the plain repo's poller tick reached the linked repo's forge") + } + if c := issueSideCalls(recB.Calls()[bBefore:]); len(c) == 0 { + t.Fatal("the plain repo's poller tick made no issue-side forge call") + } + bBefore = len(recB.Calls()) + tick(repoL) + if len(recB.Calls()) != bBefore { + t.Fatal("the linked repo's poller tick reached the plain repo's forge") + } + if c := issueSideCalls(recA.Calls()[aBefore:]); len(c) != 0 { + t.Fatalf("the linked repo's poller tick made issue-side forge calls: %v", c) + } + + // 3. A handler issue read per repo. + read := func(user store.User, repo uuid.UUID) *httptest.ResponseRecorder { + r := httptest.NewRequest(http.MethodGet, "/x", nil) + rctx := chi.NewRouteContext() + rctx.URLParams.Add("id", repo.String()) + rctx.URLParams.Add("iid", strconv.FormatInt(issueN, 10)) + r = r.WithContext(context.WithValue(mw.ContextWithUser(r.Context(), user), chi.RouteCtxKey, rctx)) + rr := httptest.NewRecorder() + h.GetIssueDetail(rr, r) + return rr + } + ytBefore, aBefore, bBefore = len(srv.Requests()), len(recA.Calls()), len(recB.Calls()) + if rr := read(userB, repoP); rr.Code != http.StatusOK || !strings.Contains(rr.Body.String(), "forge card") { + t.Fatalf("plain handler read = %d %s", rr.Code, rr.Body.String()) + } + if len(srv.Requests()) != ytBefore || len(recA.Calls()) != aBefore || len(recB.Calls()) != bBefore+1 { + t.Fatalf("the plain handler read crossed over (yt %d->%d, A %d->%d, B %d->%d)", ytBefore, len(srv.Requests()), aBefore, len(recA.Calls()), bBefore, len(recB.Calls())) + } + ytBefore, bBefore = len(srv.Requests()), len(recB.Calls()) + rr := read(userA, repoL) + if rr.Code != http.StatusOK || !strings.Contains(rr.Body.String(), "a youtrack card") { + t.Fatalf("linked handler read = %d %s", rr.Code, rr.Body.String()) + } + if len(srv.Requests()) == ytBefore { + t.Fatal("the linked handler read did not reach YouTrack") + } + if len(recB.Calls()) != bBefore { + t.Fatal("the linked handler read reached the plain repo's forge") + } + if c := issueSideCalls(recA.Calls()[aBefore:]); len(c) != 0 { + t.Fatalf("the linked handler read made issue-side forge calls: %v", c) + } +} + +// The same collision with ONE owner: two forge connections of the same user, forge +// project id 77 on both, one repo linked. The binding is by repo row, so the owner +// being the same does not blur the two. +func TestSameOwnerLinkedAndUnlinkedRepoWithTheSameForgeProjectIDLiveDB(t *testing.T) { + h, pool, q, box, _ := fileIssueLiveDB(t) + ctx := context.Background() + srv := youtracktest.NewServer(t) + def, ok := http.DefaultTransport.(*http.Transport) + if !ok { + t.Fatal("http.DefaultTransport is not an *http.Transport") + } + saved := def.TLSClientConfig + def.TLSClientConfig = srv.Client().Transport.(*http.Transport).TLSClientConfig.Clone() + t.Cleanup(func() { def.TLSClientConfig = saved }) + + recA := forgetest.NewRecorder(&collisionForge{}) + recB := forgetest.NewRecorder(&collisionForge{}) + urlA := "https://" + uuid.NewString() + ".forge-a.test" + urlB := "https://" + uuid.NewString() + ".forge-b.test" + svc := forgesvc.NewWithForgeBuilder(q, box, 5*time.Second, nil, func(_ forge.Type, u, _ string, _ time.Duration) (forge.Forge, error) { + if u == urlA { + return recA, nil + } + return recB, nil + }) + itSvc := issuetrackersvc.New(q, box, func(u string) bool { return u == srv.URL }, issuetracker.New) + svc.SetIssueTrackerDrivers(itSvc) + h.svc = svc + sealed, err := svc.EncryptToken("forge-token") + if err != nil { + t.Fatal(err) + } + user := store.User{ID: uuid.New(), Email: fmt.Sprintf("col-same-%s@e2e", uuid.NewString()[:8])} + connA, connB, repoL, repoP := uuid.New(), uuid.New(), uuid.New(), uuid.New() + exec := func(sql string, args ...any) { + t.Helper() + if _, err := pool.Exec(ctx, sql, args...); err != nil { + t.Fatalf("exec: %v", err) + } + } + exec(`INSERT INTO users (id, email, password_hash) VALUES ($1, $2, 'x')`, user.ID, user.Email) + exec(`INSERT INTO forge_connections (id, user_id, forge_type, base_url, bot_username, bot_forge_user_id, token_ciphertext) + VALUES ($1, $2, 'gitlab', $3, 'bot-a', 1, $4), ($5, $2, 'gitlab', $6, 'bot-b', 2, $4)`, connA, user.ID, urlA, sealed, connB, urlB) + yt, err := itSvc.Connect(ctx, user.ID, "youtrack", srv.URL, srv.Token) + if err != nil { + t.Fatal(err) + } + const projectID = 77 + exec(`INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, + issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at) + VALUES ($1, $2, $3, $4, 'https://forge-a.test/g/l', 'main', true, $5, $6, $7, $8, $9, now())`, + repoL, connA, projectID, "g/linked-"+repoL.String(), yt.ID, youtracktest.ProjectID, youtracktest.ProjectShortName, youtracktest.EligibilityTagID, youtracktest.EligibilityTagName) + exec(`INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled) + VALUES ($1, $2, $3, $4, 'https://forge-b.test/g/p', 'main', true)`, repoP, connB, projectID, "g/plain-"+repoP.String()) + issueN := srv.SeedIssue("a youtrack card", "yt body", youtracktest.EligibilityTagID) + srv.ResetRequests() + + read := func(repo uuid.UUID) *httptest.ResponseRecorder { + r := httptest.NewRequest(http.MethodGet, "/x", nil) + rctx := chi.NewRouteContext() + rctx.URLParams.Add("id", repo.String()) + rctx.URLParams.Add("iid", strconv.FormatInt(issueN, 10)) + r = r.WithContext(context.WithValue(mw.ContextWithUser(r.Context(), user), chi.RouteCtxKey, rctx)) + rr := httptest.NewRecorder() + h.GetIssueDetail(rr, r) + return rr + } + if rr := read(repoP); rr.Code != http.StatusOK || !strings.Contains(rr.Body.String(), "forge card") { + t.Fatalf("plain read = %d %s", rr.Code, rr.Body.String()) + } + if n := len(srv.Requests()); n != 0 { + t.Fatalf("the plain repo's read reached YouTrack (%d requests)", n) + } + if c := issueSideCalls(recB.Calls()); len(c) != 1 || len(issueSideCalls(recA.Calls())) != 0 { + t.Fatalf("plain read: forge B %v, forge A %v", c, issueSideCalls(recA.Calls())) + } + if rr := read(repoL); rr.Code != http.StatusOK || !strings.Contains(rr.Body.String(), "a youtrack card") { + t.Fatalf("linked read = %d %s", rr.Code, rr.Body.String()) + } + if len(srv.Requests()) == 0 { + t.Fatal("the linked repo's read did not reach YouTrack") + } + if len(issueSideCalls(recA.Calls())) != 0 || len(issueSideCalls(recB.Calls())) != 1 { + t.Fatalf("the linked read touched a forge: A %v, B %v", recA.Calls(), recB.Calls()) + } +} diff --git a/api/internal/handler/linked_repo_filing_livedb_test.go b/api/internal/handler/linked_repo_filing_livedb_test.go new file mode 100644 index 0000000000..5b88a912d1 --- /dev/null +++ b/api/internal/handler/linked_repo_filing_livedb_test.go @@ -0,0 +1,82 @@ +package handler + +import ( + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/issuetracker" +) + +// linkedDrivers hands out the BaseFake-backed driver; every write is refused by the +// guard before it could reach it. +type linkedDrivers struct{} + +func (linkedDrivers) DriverFor(_ context.Context, _, _ uuid.UUID, _ issuetracker.Binding, _, _, _ string) (issuetracker.Driver, error) { + return &itDriver{}, nil +} + +// Every filing entry point on a repo linked to an issue tracker answers 409 (never 502 or +// an "uncertain" 202), releases what it claimed, and sends nothing to the forge. +func TestLinkedRepoFilingRoutesAreRefusedAndReleaseTheirClaimLiveDB(t *testing.T) { + f := seedNeverEnabledFixture(t) + ctx := context.Background() + f.h.svc.SetIssueTrackerDrivers(linkedDrivers{}) + itConn := uuid.New() + mustExecT(ctx, t, f.pool, `INSERT INTO issue_tracker_connections (id, user_id, kind, base_url, account_id, account_login, token_ciphertext) + VALUES ($1, $2, 'youtrack', $3, '1-1', 'bot', $4)`, itConn, f.admin.ID, "https://"+uuid.NewString()+".yt.test", []byte{1}) + // Link the fixture's repo (the marker only ever goes true to false). + mustExecT(ctx, t, f.pool, `UPDATE repos SET enabled = true, awaiting_first_enable = false, + issue_tracker_connection_id = $2, issue_tracker_project_id = '0-1', issue_tracker_project_short = 'UZI', + eligibility_tag_id = '5-1', eligibility_tag_name = 'uzi', issue_tracker_linked_at = now() WHERE id = $1`, f.repoID, itConn) + repoParam := map[string]string{"id": f.repoID.String()} + + cases := []struct { + name string + call func(w http.ResponseWriter) + }{ + {"FileFinding", func(w http.ResponseWriter) { + f.h.FileFinding(w, f.request(http.MethodPost, map[string]any{}, map[string]string{"id": f.findID.String()})) + }}, + {"FileFindingGroup", func(w http.ResponseWriter) { + f.h.FileFindingGroup(w, f.request(http.MethodPost, map[string]any{"ids": []string{f.dispID.String()}}, nil)) + }}, + {"FileIssue", func(w http.ResponseWriter) { + f.h.FileIssue(w, f.request(http.MethodPost, map[string]string{"repo_id": f.repoID.String(), "title": "t", "description": "d"}, + map[string]string{"id": f.runID.String(), "recID": f.recID.String()})) + }}, + {"AdminFileJudgeIssue", func(w http.ResponseWriter) { + f.h.AdminFileJudgeIssue(w, f.request(http.MethodPost, adminFileIssueRequest{ + Category: "improve_agent", Target: "ne-" + f.repoID.String(), RepoID: f.repoID.String(), Title: "t", Description: "d", + }, nil)) + }}, + {"ConfigureColumns", func(w http.ResponseWriter) { + f.h.ConfigureColumns(w, f.request(http.MethodPut, map[string]any{"columns": []map[string]string{{"label_name": "a"}}}, repoParam)) + }}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + rr := httptest.NewRecorder() + tc.call(rr) + if rr.Code != http.StatusConflict || !strings.Contains(rr.Body.String(), "not offered yet") { + t.Fatalf("status = %d body = %s, want the 409 refusal", rr.Code, rr.Body.String()) + } + if n := f.hits.Load(); n != 0 { + t.Errorf("the forge saw %d request(s), want 0", n) + } + }) + } + + var status string + if err := f.pool.QueryRow(ctx, `SELECT status FROM finding_dispositions WHERE repo_id = $1`, f.repoID).Scan(&status); err != nil || status != "open" { + t.Errorf("finding disposition = %q (%v), want open: the group claim must be released, not left uncertain", status, err) + } + var filed int + if err := f.pool.QueryRow(ctx, `SELECT count(*) FROM recommendation_filed_issues WHERE filed_repo_id = $1`, f.repoID).Scan(&filed); err != nil || filed != 0 { + t.Errorf("recommendation filed rows = %d (%v), want 0", filed, err) + } +} diff --git a/api/internal/handler/linked_repo_writes_livedb_test.go b/api/internal/handler/linked_repo_writes_livedb_test.go new file mode 100644 index 0000000000..171ab2c50e --- /dev/null +++ b/api/internal/handler/linked_repo_writes_livedb_test.go @@ -0,0 +1,193 @@ +package handler + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "strconv" + "strings" + "testing" + "time" + + "github.com/go-chi/chi/v5" + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgtype" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/forgesvc" + "github.com/vtmocanu/uzi/api/internal/issuetracker" + "github.com/vtmocanu/uzi/api/internal/issuetracker/youtracktest" + "github.com/vtmocanu/uzi/api/internal/issuetrackersvc" + mw "github.com/vtmocanu/uzi/api/internal/middleware" + "github.com/vtmocanu/uzi/api/internal/store" +) + +type linkedHandlerFixture struct { + h *Handler + srv *youtracktest.Server + rec *forgetest.Recorder + user store.User + repo uuid.UUID + issueN int64 +} + +// newLinkedHandlerFixture seeds one enabled repo linked to the YouTrack fake, a cached +// card for one of its issues, and a Handler whose forgesvc resolves through the real +// issuetrackersvc. Every forge call lands in rec. +func newLinkedHandlerFixture(t *testing.T, withColumns bool) *linkedHandlerFixture { + t.Helper() + h, pool, q, box, _ := fileIssueLiveDB(t) + ctx := context.Background() + srv := youtracktest.NewServer(t) + def, ok := http.DefaultTransport.(*http.Transport) + if !ok { + t.Fatal("http.DefaultTransport is not an *http.Transport") + } + saved := def.TLSClientConfig + def.TLSClientConfig = srv.Client().Transport.(*http.Transport).TLSClientConfig.Clone() + t.Cleanup(func() { def.TLSClientConfig = saved }) + + rec := forgetest.NewRecorder(&collisionForge{}) + svc := forgesvc.NewWithForgeBuilder(q, box, 5*time.Second, nil, func(forge.Type, string, string, time.Duration) (forge.Forge, error) { return rec, nil }) + itSvc := issuetrackersvc.New(q, box, func(u string) bool { return u == srv.URL }, issuetracker.New) + svc.SetIssueTrackerDrivers(itSvc) + h.svc = svc + sealed, err := svc.EncryptToken("forge-token") + if err != nil { + t.Fatal(err) + } + f := &linkedHandlerFixture{h: h, srv: srv, rec: rec, repo: uuid.New(), + user: store.User{ID: uuid.New(), Email: fmt.Sprintf("lw-%s@e2e", uuid.NewString()[:8])}} + exec := func(sql string, args ...any) { + t.Helper() + if _, err := pool.Exec(ctx, sql, args...); err != nil { + t.Fatalf("exec: %v", err) + } + } + conn := uuid.New() + exec(`INSERT INTO users (id, email, password_hash) VALUES ($1, $2, 'x')`, f.user.ID, f.user.Email) + exec(`INSERT INTO forge_connections (id, user_id, forge_type, base_url, bot_username, bot_forge_user_id, token_ciphertext) + VALUES ($1, $2, 'gitlab', $3, 'bot', 1, $4)`, conn, f.user.ID, "https://"+uuid.NewString()+".forge.test", sealed) + yt, err := itSvc.Connect(ctx, f.user.ID, "youtrack", srv.URL, srv.Token) + if err != nil { + t.Fatal(err) + } + exec(`INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, + issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at) + VALUES ($1, $2, 77, $3, 'https://forge.test/g/l', 'main', true, $4, $5, $6, $7, $8, now())`, + f.repo, conn, "g/linked-"+f.repo.String(), yt.ID, youtracktest.ProjectID, youtracktest.ProjectShortName, youtracktest.EligibilityTagID, youtracktest.EligibilityTagName) + f.issueN = srv.SeedIssue("a youtrack card", "yt body", youtracktest.EligibilityTagID) + exec(`INSERT INTO issues (repo_id, forge_issue_iid, title, state, labels, web_url, forge_updated_at, synced_at) + VALUES ($1, $2, 'card', 'opened', '[]', 'https://yt.test/i', now(), now())`, f.repo, f.issueN) + if withColumns { + exec(`INSERT INTO board_columns(repo_id,label_name,position) VALUES($1,'In Progress',0),($1,'Human Review',1)`, f.repo) + } + srv.ResetRequests() + return f +} + +func (f *linkedHandlerFixture) call(handler http.HandlerFunc, method string, body any) *httptest.ResponseRecorder { + var rdr *strings.Reader + if body != nil { + b, _ := json.Marshal(body) + rdr = strings.NewReader(string(b)) + } else { + rdr = strings.NewReader("") + } + r := httptest.NewRequest(method, "/x", rdr) + rctx := chi.NewRouteContext() + rctx.URLParams.Add("id", f.repo.String()) + rctx.URLParams.Add("iid", strconv.FormatInt(f.issueN, 10)) + r = r.WithContext(context.WithValue(mw.ContextWithUser(r.Context(), f.user), chi.RouteCtxKey, rctx)) + rr := httptest.NewRecorder() + handler(rr, r) + return rr +} + +func (f *linkedHandlerFixture) assertNothingSent(t *testing.T) { + t.Helper() + if n := len(f.srv.Requests()); n != 0 { + t.Errorf("the issue tracker saw %d request(s), want none: %v", n, f.srv.Requests()) + } + if c := f.rec.Calls(); len(c) != 0 { + t.Errorf("the forge saw calls %v, want none", c) + } +} + +// Every issue write on a linked repo is a clear 409 and sends nothing anywhere. +func TestLinkedRepoIssueWritesAreRefusedWith409LiveDB(t *testing.T) { + f := newLinkedHandlerFixture(t, true) + cases := []struct { + name string + do func() *httptest.ResponseRecorder + }{ + {"create issue", func() *httptest.ResponseRecorder { + return f.call(f.h.CreateIssue, http.MethodPost, map[string]string{"title": "t", "description": "d"}) + }}, + {"move issue", func() *httptest.ResponseRecorder { + return f.call(f.h.MoveIssue, http.MethodPost, map[string]string{"to_column": "In Progress"}) + }}, + {"promote issue", func() *httptest.ResponseRecorder { + return f.call(f.h.PromoteIssue, http.MethodPost, nil) + }}, + {"ensure labels", func() *httptest.ResponseRecorder { + return f.call(f.h.EnsureRepoLabels, http.MethodPost, map[string][]string{"labels": {"uzi"}}) + }}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + rr := tc.do() + if rr.Code != http.StatusConflict || !strings.Contains(rr.Body.String(), "issue writes on a repo linked to an issue tracker are not offered yet") { + t.Fatalf("status = %d body = %s, want the 409 refusal", rr.Code, rr.Body.String()) + } + f.assertNothingSent(t) + }) + } +} + +// A manual issue sync on a linked repo is a 409 until the linked intake lands, not a 502. +func TestLinkedRepoSyncIsA409UntilIntakeLandsLiveDB(t *testing.T) { + f := newLinkedHandlerFixture(t, true) + rr := f.call(f.h.SyncRepo, http.MethodPost, nil) + if rr.Code != http.StatusConflict || !strings.Contains(rr.Body.String(), "not available yet") { + t.Fatalf("status = %d body = %s", rr.Code, rr.Body.String()) + } + f.assertNothingSent(t) +} + +// Opening the board of a linked repo seeds its column rows without any label write. +func TestLinkedRepoBoardSeedsColumnsWithoutLabelWritesLiveDB(t *testing.T) { + f := newLinkedHandlerFixture(t, false) + rr := f.call(f.h.GetBoard, http.MethodGet, nil) + if rr.Code != http.StatusOK { + t.Fatalf("status = %d body = %s", rr.Code, rr.Body.String()) + } + n, err := f.h.q.CountBoardColumns(context.Background(), f.repo) + if err != nil || n != int64(len(forgesvc.DefaultColumns)) { + t.Fatalf("board columns = %d (%v), want the %d defaults", n, err, len(forgesvc.DefaultColumns)) + } + f.assertNothingSent(t) +} + +// The forgeFactory test seam is for an unlinked row only: a linked row through it is an +// error, so a test or wiring gap can never reach the forge for a linked repo's issues. +func TestForgeFactorySeamRefusesALinkedRow(t *testing.T) { + built := 0 + h := &Handler{forgeFactory: func(string, string, []byte) (forge.Forge, error) { + built++ + return &collisionForge{}, nil + }} + linked := pgtype.UUID{Bytes: uuid.New(), Valid: true} + if tr, err := h.issueTrackerForRepo(context.Background(), uuid.New(), uuid.New(), linked, "gitlab", "https://x.test", nil); err == nil || tr != nil || built != 0 { + t.Fatalf("linked row through the seam = (%v, %v), forge built %d times", tr, err, built) + } + if tr, err := h.issueTrackerForRepo(context.Background(), uuid.New(), uuid.New(), pgtype.UUID{}, "gitlab", "https://x.test", nil); err != nil || tr == nil || built != 1 { + t.Fatalf("unlinked row through the seam = (%v, %v), built %d", tr, err, built) + } + if ch, err := h.codeHostForRepo(context.Background(), uuid.New(), uuid.New(), linked, "gitlab", "https://x.test", nil); err != nil || ch == nil { + t.Fatalf("the code-host side ignores the link: (%v, %v)", ch, err) + } +} diff --git a/api/internal/handler/mrrework_run.go b/api/internal/handler/mrrework_run.go index 12f0fc0d0c..1239095c68 100644 --- a/api/internal/handler/mrrework_run.go +++ b/api/internal/handler/mrrework_run.go @@ -157,7 +157,7 @@ func (h *Handler) StartRunRework(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusConflict, "MR rework is unavailable: the trusted review bot list could not be read") return } - ch, err := h.codeHostForRepo(r.Context(), repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + ch, err := h.codeHostForRepo(r.Context(), repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Error("mr-rework: build forge for connection", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") diff --git a/api/internal/handler/mrrework_run_eligibility_livedb_test.go b/api/internal/handler/mrrework_run_eligibility_livedb_test.go index 47026f94c0..b8789477ad 100644 --- a/api/internal/handler/mrrework_run_eligibility_livedb_test.go +++ b/api/internal/handler/mrrework_run_eligibility_livedb_test.go @@ -75,7 +75,7 @@ func newReworkRig(t *testing.T, st settings.Store, ttl time.Duration) *reworkRig t.Fatal(err) } f := &reworkForge{answers: map[int64]forge.AuthorEligibility{}} - h.svc = forgesvc.NewWithForgeBuilder(nil, box, time.Second, nil, + h.svc = forgesvc.NewWithForgeBuilder(q, box, time.Second, nil, func(forge.Type, string, string, time.Duration) (forge.Forge, error) { return f, nil }) h.box = box tx := workersvc.New(q, box, workersvc.Params{CustodyHoldLimit: defaultCustodyLimit()}) diff --git a/api/internal/handler/never_enabled.go b/api/internal/handler/never_enabled.go new file mode 100644 index 0000000000..f7293ca9ce --- /dev/null +++ b/api/internal/handler/never_enabled.go @@ -0,0 +1,39 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/vtmocanu/uzi/api/internal/httpx" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// writeNeverEnabled answers the typed refusal for an operation on a repo that has +// never been enabled (PRD 4173 D13): 409 with a message that says what to do. The +// body is the fixed sentinel text, never a database error. +func writeNeverEnabled(w http.ResponseWriter) { + httpx.Error(w, http.StatusConflict, store.ErrRepoNeverEnabled.Error()) +} + +// refuseNeverEnabled writes the 409 and returns true when the repo row's +// AwaitingFirstEnable marker is set. Handlers call it right after loading the repo +// row, before any forge or issue tracker call, so the database guard never strands +// an issue the forge already created. +func refuseNeverEnabled(w http.ResponseWriter, awaitingFirstEnable bool) bool { + if store.RefuseNeverEnabled(awaitingFirstEnable) == nil { + return false + } + writeNeverEnabled(w) + return true +} + +// writeIfNeverEnabled writes the 409 and returns true when err is the typed refusal +// or carries the issue-number guard's SQLSTATE (store.MapNeverEnabled), so a handler +// that would otherwise answer 500 answers 409. +func writeIfNeverEnabled(w http.ResponseWriter, err error) bool { + if !errors.Is(store.MapNeverEnabled(err), store.ErrRepoNeverEnabled) { + return false + } + writeNeverEnabled(w) + return true +} diff --git a/api/internal/handler/never_enabled_gates_livedb_test.go b/api/internal/handler/never_enabled_gates_livedb_test.go new file mode 100644 index 0000000000..dc34995bc9 --- /dev/null +++ b/api/internal/handler/never_enabled_gates_livedb_test.go @@ -0,0 +1,325 @@ +package handler + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "log/slog" + "net/http" + "net/http/httptest" + "strings" + "sync/atomic" + "testing" + + "github.com/go-chi/chi/v5" + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgxpool" + + mw "github.com/vtmocanu/uzi/api/internal/middleware" + "github.com/vtmocanu/uzi/api/internal/schedsvc" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// PRD 4173 D13: an operation on a repo that has never been enabled is refused with a +// 409 BEFORE any forge or issue tracker call. The fixture's forge is a tripwire: every +// request it receives is counted, and each test asserts the count stayed zero. + +type neverEnabledFixture struct { + h *Handler + pool *pgxpool.Pool + admin store.User + repoID uuid.UUID // awaiting_first_enable = true + goodRepo uuid.UUID // an ordinary enabled repo of the same owner + runID uuid.UUID + recID uuid.UUID + findID uuid.UUID + dispID uuid.UUID + hits *atomic.Int64 +} + +func seedNeverEnabledFixture(t *testing.T) *neverEnabledFixture { + t.Helper() + h, pool, q, box, _ := fileIssueLiveDB(t) + ctx := context.Background() + hits := &atomic.Int64{} + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + hits.Add(1) + w.WriteHeader(http.StatusInternalServerError) + })) + t.Cleanup(srv.Close) + + f := &neverEnabledFixture{ + h: h, pool: pool, hits: hits, + admin: store.User{ID: uuid.New(), Email: fmt.Sprintf("ne-%s@e2e", uuid.NewString()[:8]), IsAdmin: true}, + repoID: uuid.New(), + goodRepo: uuid.New(), + runID: uuid.New(), + } + sealed, err := box.Seal([]byte("glpat-dummy-token")) + if err != nil { + t.Fatalf("seal token: %v", err) + } + connID := uuid.New() + mustExecT(ctx, t, pool, `INSERT INTO users (id, email, password_hash, is_admin) VALUES ($1, $2, 'x', true)`, f.admin.ID, f.admin.Email) + mustExecT(ctx, t, pool, + `INSERT INTO forge_connections (id, user_id, forge_type, base_url, bot_username, bot_forge_user_id, token_ciphertext) + VALUES ($1, $2, 'gitlab', $3, 'bot-ne', 1, $4)`, connID, f.admin.ID, srv.URL, sealed) + // The marker is set only at INSERT (the guard refuses setting it back later), and no + // issue number may be stored against this repo, so the review's run lives on goodRepo. + mustExecT(ctx, t, pool, + `INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, awaiting_first_enable) + VALUES ($1, $2, 1, $3, 'https://forge.example/g/ne', 'main', false, true)`, f.repoID, connID, "g/ne-"+f.repoID.String()[:8]) + mustExecT(ctx, t, pool, + `INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled) + VALUES ($1, $2, 2, $3, 'https://forge.example/g/ok', 'main', true)`, f.goodRepo, connID, "g/ok-"+f.goodRepo.String()[:8]) + mustExecT(ctx, t, pool, + `INSERT INTO runs (id, user_id, repo_id, issue_iid, issue_title, issue_description, status, kind) + VALUES ($1, $2, $3, 42, 'Do X', 'd', 'completed', 'issue')`, f.runID, f.admin.ID, f.goodRepo) + + recs, _ := json.Marshal([]map[string]string{{"category": "improve_agent", "target": "ne-" + f.repoID.String(), "rationale_md": "r", "confidence": "medium"}}) + reviewID, err := q.UpsertRunReviewWithRecommendations(ctx, store.UpsertRunReviewWithRecommendationsParams{ + TargetRunID: f.runID, UserID: f.admin.ID, Verdict: "issues", SummaryMd: "s", JudgeModel: "haiku", Status: "complete", Recommendations: recs, + }) + if err != nil { + t.Fatalf("seed review: %v", err) + } + list, err := q.ListRecommendationsForReview(ctx, reviewID) + if err != nil || len(list) != 1 { + t.Fatalf("seed recommendation: %d rows, %v", len(list), err) + } + f.recID = list[0].ID + + finding, err := q.InsertFinding(ctx, store.InsertFindingParams{ + RunID: f.runID, UserID: f.admin.ID, RepoID: f.repoID, + Location: "internal/ne.go#loop", Title: "t", DescriptionMd: "d", Labels: []byte(`[]`), Confidence: "high", + }) + if err != nil { + t.Fatalf("InsertFinding: %v", err) + } + f.findID = finding.ID + if err := f.pool.QueryRow(ctx, `INSERT INTO finding_dispositions (user_id, repo_id, location, content_hash, last_title) + VALUES ($1, $2, $3, 'h', 't') RETURNING id`, f.admin.ID, f.repoID, finding.Location).Scan(&f.dispID); err != nil { + t.Fatalf("seed disposition: %v", err) + } + return f +} + +func (f *neverEnabledFixture) request(method string, body any, params map[string]string) *http.Request { + var rdr *strings.Reader + if body != nil { + b, _ := json.Marshal(body) + rdr = strings.NewReader(string(b)) + } else { + rdr = strings.NewReader("") + } + r := httptest.NewRequest(method, "/x", rdr) + rctx := chi.NewRouteContext() + for k, v := range params { + rctx.URLParams.Add(k, v) + } + return r.WithContext(context.WithValue(mw.ContextWithUser(r.Context(), f.admin), chi.RouteCtxKey, rctx)) +} + +func (f *neverEnabledFixture) wantRefused(t *testing.T, rr *httptest.ResponseRecorder) { + t.Helper() + if rr.Code != http.StatusConflict { + t.Fatalf("status = %d, want 409; body=%s", rr.Code, rr.Body.String()) + } + if !strings.Contains(rr.Body.String(), "never been enabled") { + t.Errorf("body = %s, want the never-enabled message", rr.Body.String()) + } + if n := f.hits.Load(); n != 0 { + t.Errorf("the forge saw %d request(s), want 0", n) + } +} + +func TestNeverEnabledRepoRefusedBeforeForgeCallLiveDB(t *testing.T) { + f := seedNeverEnabledFixture(t) + ctx := context.Background() + repoParam := map[string]string{"id": f.repoID.String()} + + cases := []struct { + name string + call func(w http.ResponseWriter) + }{ + {"GetBoard", func(w http.ResponseWriter) { f.h.GetBoard(w, f.request(http.MethodGet, nil, repoParam)) }}, + {"SyncRepo", func(w http.ResponseWriter) { f.h.SyncRepo(w, f.request(http.MethodPost, nil, repoParam)) }}, + {"CreateIssue", func(w http.ResponseWriter) { + f.h.CreateIssue(w, f.request(http.MethodPost, map[string]string{"title": "t", "description": "d"}, repoParam)) + }}, + {"CreateSchedule", func(w http.ResponseWriter) { + f.h.CreateSchedule(w, f.request(http.MethodPost, map[string]any{"target": "prompt"}, repoParam)) + }}, + {"EnableCatalogSchedule", func(w http.ResponseWriter) { + f.h.EnableCatalogSchedule(w, f.request(http.MethodPost, nil, map[string]string{"id": f.repoID.String(), "slug": "anything"})) + }}, + {"FileFinding", func(w http.ResponseWriter) { + f.h.FileFinding(w, f.request(http.MethodPost, map[string]any{}, map[string]string{"id": f.findID.String()})) + }}, + {"FileFindingGroup", func(w http.ResponseWriter) { + f.h.FileFindingGroup(w, f.request(http.MethodPost, map[string]any{"ids": []string{f.dispID.String()}}, nil)) + }}, + {"FileIssue", func(w http.ResponseWriter) { + f.h.FileIssue(w, f.request(http.MethodPost, map[string]string{"repo_id": f.repoID.String(), "title": "t", "description": "d"}, + map[string]string{"id": f.runID.String(), "recID": f.recID.String()})) + }}, + {"AdminFileJudgeIssue", func(w http.ResponseWriter) { + f.h.AdminFileJudgeIssue(w, f.request(http.MethodPost, adminFileIssueRequest{ + Category: "improve_agent", Target: "ne-" + f.repoID.String(), RepoID: f.repoID.String(), Title: "t", Description: "d", + }, nil)) + }}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + rr := httptest.NewRecorder() + tc.call(rr) + f.wantRefused(t, rr) + }) + } + + // A refused filing leaves its claim released: the coordinate is still open. + var status string + if err := f.pool.QueryRow(ctx, `SELECT status FROM finding_dispositions WHERE repo_id = $1`, f.repoID).Scan(&status); err != nil || status != "open" { + t.Errorf("finding disposition = %q (%v), want open", status, err) + } + var filed int + if err := f.pool.QueryRow(ctx, `SELECT count(*) FROM recommendation_filed_issues WHERE filed_repo_id = $1`, f.repoID).Scan(&filed); err != nil || filed != 0 { + t.Errorf("recommendation filed rows = %d (%v), want 0", filed, err) + } +} + +func TestPatchScheduleRepointToNeverEnabledRepoLiveDB(t *testing.T) { + f := seedNeverEnabledFixture(t) + ctx := context.Background() + schedID := uuid.New() + mustExecT(ctx, t, f.pool, + `INSERT INTO run_schedules (id, user_id, repo_id, target, prompt, timing, cron_expr, timezone, next_fire_at, auto_approve, status, enabled) + VALUES ($1, $2, $3, 'prompt', 'do a thing', 'recurring', '0 3 * * *', 'UTC', now() + interval '1 day', true, 'active', true)`, + schedID, f.admin.ID, f.goodRepo) + + rr := httptest.NewRecorder() + f.h.PatchSchedule(rr, f.request(http.MethodPatch, map[string]any{"repo_id": f.repoID.String()}, map[string]string{"id": schedID.String()})) + f.wantRefused(t, rr) + var repo uuid.UUID + if err := f.pool.QueryRow(ctx, `SELECT repo_id FROM run_schedules WHERE id = $1`, schedID).Scan(&repo); err != nil || repo != f.goodRepo { + t.Errorf("schedule repo = %s (%v), want it unchanged", repo, err) + } +} + +// The database guard refuses an issue number on an awaiting repo with UZ413; the +// handler mapping turns that into the same 409 instead of a 500. +func TestNeverEnabledGuardErrorMapsTo409LiveDB(t *testing.T) { + f := seedNeverEnabledFixture(t) + ctx := context.Background() + _, err := f.pool.Exec(ctx, + `INSERT INTO runs (id, user_id, repo_id, issue_iid, issue_title, issue_description, status, kind) + VALUES ($1, $2, $3, 7, 'x', 'd', 'queued', 'issue')`, uuid.New(), f.admin.ID, f.repoID) + var pgErr *pgconn.PgError + if !errors.As(err, &pgErr) || pgErr.Code != "UZ413" { + t.Fatalf("direct insert error = %v, want SQLSTATE UZ413", err) + } + if !errors.Is(store.MapNeverEnabled(err), store.ErrRepoNeverEnabled) { + t.Fatalf("MapNeverEnabled(%v) did not map to ErrRepoNeverEnabled", err) + } + rr := httptest.NewRecorder() + if !writeIfNeverEnabled(rr, err) { + t.Fatal("writeIfNeverEnabled did not recognise the guard error") + } + f.wantRefused(t, rr) + if writeIfNeverEnabled(httptest.NewRecorder(), errors.New("boom")) { + t.Error("an unrelated error was mapped to the never-enabled refusal") + } +} + +// insertSchedule seeds a recurring schedule of the given target on repo for the fixture user. +func (f *neverEnabledFixture) insertSchedule(t *testing.T, repo uuid.UUID, target string) uuid.UUID { + t.Helper() + id := uuid.New() + if target == "issue" { + mustExecT(context.Background(), t, f.pool, + `INSERT INTO run_schedules (id, user_id, repo_id, target, issue_iid, timing, cron_expr, timezone, next_fire_at, auto_approve, status, enabled) + VALUES ($1, $2, $3, 'issue', 7, 'recurring', '0 3 * * *', 'UTC', now() + interval '1 day', true, 'active', true)`, id, f.admin.ID, repo) + return id + } + mustExecT(context.Background(), t, f.pool, + `INSERT INTO run_schedules (id, user_id, repo_id, target, prompt, timing, cron_expr, timezone, next_fire_at, auto_approve, status, enabled) + VALUES ($1, $2, $3, 'prompt', 'do a thing', 'recurring', '0 3 * * *', 'UTC', now() + interval '1 day', true, 'active', true)`, id, f.admin.ID, repo) + return id +} + +// The run and schedule entry points that reach the service or the scheduler answer 409 for +// an awaiting repo, with no forge call. +func TestNeverEnabledRunsAndSchedulesRefusedLiveDB(t *testing.T) { + f := seedNeverEnabledFixture(t) + ctx := context.Background() + f.h.wsvc.SetForges(f.h.svc) + f.h.scheduler = schedsvc.New(f.h.q, f.h.wsvc, f.h.svc, f.h.settings, nil, nil, 0, slog.Default()) + repoParam := map[string]string{"id": f.repoID.String()} + + // A prompt schedule can sit on an awaiting repo at the database level (no issue number), + // which is what RunScheduleNow must still refuse. + awaitingPrompt := f.insertSchedule(t, f.repoID, "prompt") + goodIssue := f.insertSchedule(t, f.goodRepo, "issue") + goodPrompt := f.insertSchedule(t, f.goodRepo, "prompt") + + chat, err := f.h.wsvc.CreateChatRun(ctx, f.admin.ID, "hello") + if err != nil { + t.Fatalf("CreateChatRun: %v", err) + } + prop, err := f.h.q.CreateIssueProposal(ctx, store.CreateIssueProposalParams{ + RunID: chat.ID, RepoID: f.repoID, Title: "t", Description: "d", Labels: []byte(`[]`), + }) + if err != nil { + t.Fatalf("CreateIssueProposal: %v", err) + } + + cases := []struct { + name string + call func(w http.ResponseWriter) + }{ + {"RunScheduleNow", func(w http.ResponseWriter) { + f.h.RunScheduleNow(w, f.request(http.MethodPost, nil, map[string]string{"id": awaitingPrompt.String()})) + }}, + {"CreateRun", func(w http.ResponseWriter) { + f.h.CreateRun(w, f.request(http.MethodPost, map[string]any{"issue_iid": 7}, repoParam)) + }}, + {"CreateTaskRun", func(w http.ResponseWriter) { + f.h.CreateTaskRun(w, f.request(http.MethodPost, map[string]any{"context": "do it"}, repoParam)) + }}, + {"CreateCIFixRun", func(w http.ResponseWriter) { + f.h.CreateCIFixRun(w, f.request(http.MethodPost, map[string]any{"ref": "main"}, repoParam)) + }}, + {"ConfigureColumns", func(w http.ResponseWriter) { + f.h.ConfigureColumns(w, f.request(http.MethodPut, map[string]any{"columns": []string{"a"}}, repoParam)) + }}, + {"ConfirmProposal", func(w http.ResponseWriter) { + f.h.ConfirmProposal(w, f.request(http.MethodPost, nil, map[string]string{"id": chat.ID.String(), "pid": prop.ID.String()})) + }}, + {"CloneSchedule prompt", func(w http.ResponseWriter) { + f.h.CloneSchedule(w, f.request(http.MethodPost, map[string]any{"repo_id": f.repoID.String()}, map[string]string{"id": goodPrompt.String()})) + }}, + {"CloneSchedule issue", func(w http.ResponseWriter) { + f.h.CloneSchedule(w, f.request(http.MethodPost, map[string]any{"repo_id": f.repoID.String()}, map[string]string{"id": goodIssue.String()})) + }}, + {"AddScheduleRepo prompt", func(w http.ResponseWriter) { + f.h.AddScheduleRepo(w, f.request(http.MethodPost, map[string]any{"repo_id": f.repoID.String()}, map[string]string{"id": goodPrompt.String()})) + }}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + rr := httptest.NewRecorder() + tc.call(rr) + f.wantRefused(t, rr) + }) + } + var n int + if err := f.pool.QueryRow(ctx, `SELECT count(*) FROM run_schedules WHERE repo_id = $1`, f.repoID).Scan(&n); err != nil || n != 1 { + t.Errorf("schedules on the awaiting repo = %d (%v), want only the seeded one", n, err) + } + var status string + if err := f.pool.QueryRow(ctx, `SELECT status FROM issue_proposals WHERE id = $1`, prop.ID).Scan(&status); err != nil || status != "pending" { + t.Errorf("proposal status = %q (%v), want pending after the refused confirm", status, err) + } +} diff --git a/api/internal/handler/recorded_calls_livedb_test.go b/api/internal/handler/recorded_calls_livedb_test.go new file mode 100644 index 0000000000..d5f7fbc187 --- /dev/null +++ b/api/internal/handler/recorded_calls_livedb_test.go @@ -0,0 +1,118 @@ +package handler + +import ( + "context" + "fmt" + "net/http" + "testing" + "time" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/forgesvc" + "github.com/vtmocanu/uzi/api/internal/privcheck" +) + +// Recorded-call test (PRD 4173 M4): for a repo with no issue-tracker link, +// pin every forge call the repo-scoped handlers make: the first board open +// (default-column seed plus initial import), the Refresh sync, a board card +// move, a new board issue, promote, the issue page, column configuration, and a repo enable (live guard, column seed, import +// after enable). The forge is reached +// through the real forgesvc builder seam and the real privcheck guard, so the +// calls are the production ones. The golden was captured before the +// tracker-link rerouting; regenerate only deliberately, with +// UZI_UPDATE_GOLDEN=1. + +// rcRepoForge answers the issue and guard reads those handlers make. +type rcRepoForge struct{ forgetest.BaseFake } + +func (*rcRepoForge) ListIssues(_ context.Context, _ int64, opts forge.ListIssuesOptions) ([]forge.Issue, error) { + at := time.Date(2026, 5, 6, 7, 8, 9, 0, time.UTC) + issues := []forge.Issue{{IID: 5, Title: "t", State: "opened", Labels: opts.Labels, UpdatedAt: at}} + if len(opts.Labels) == 0 { + // The additive open fetch also returns an issue without the uzi label, + // the card the Promote step labels. + issues = append(issues, forge.Issue{IID: 9, Title: "unlabelled", State: "opened", UpdatedAt: at}) + } + return issues, nil +} +func (*rcRepoForge) GetIssue(_ context.Context, _, iid int64) (forge.Issue, error) { + return forge.Issue{IID: iid, Title: "t", State: "opened", Description: "body", Labels: []string{"uzi"}, UpdatedAt: time.Date(2026, 5, 6, 7, 8, 9, 0, time.UTC)}, nil +} +func (*rcRepoForge) UpdateIssueLabels(context.Context, int64, int64, []string, []string) error { + return nil +} +func (*rcRepoForge) CreateIssue(_ context.Context, _ int64, title, _ string, labels []string) (forge.Issue, error) { + return forge.Issue{IID: 6, Title: title, State: "opened", Labels: labels, UpdatedAt: time.Date(2026, 5, 6, 7, 8, 9, 0, time.UTC)}, nil +} +func (*rcRepoForge) EnsureLabels(context.Context, int64, []forge.Label) error { return nil } +func (*rcRepoForge) VerifyToken(context.Context) (forge.BotIdentity, error) { + return forge.BotIdentity{ForgeUserID: 9, Username: "bot"}, nil +} +func (*rcRepoForge) ProjectRole(context.Context, int64, int64) (forge.Role, bool, error) { + return forge.RoleWrite, true, nil +} +func (*rcRepoForge) DefaultBranchProtection(context.Context, int64, string, int64) (forge.BranchProtection, error) { + return forge.BranchProtection{Protected: true}, nil +} + +func TestRecordedCallsRepoHandlersLiveDB(t *testing.T) { + h, router, pool := cliLiveDB(t) + rec := forgetest.NewRecorder(&rcRepoForge{}) + h.svc = forgesvc.NewWithForgeBuilder(h.q, h.box, time.Second, nil, func(forge.Type, string, string, time.Duration) (forge.Forge, error) { + return rec, nil + }) + h.pcheck = privcheck.NewService(h.q, h.svc) + + sealed, err := h.svc.EncryptToken("fixture") + if err != nil { + t.Fatal(err) + } + owner := cliSeedUser(t, pool, false) + jwt := cliMintJWT(t, pool, owner) + connID := uuid.New() + cliMustExec(t, pool, + `INSERT INTO forge_connections (id, user_id, forge_type, base_url, bot_username, bot_forge_user_id, token_ciphertext) + VALUES ($1, $2, 'gitlab', 'https://forge.e2e', 'bot', 9, $3)`, connID, owner, sealed) + seedRepo := func(projectID int64, enabled bool) uuid.UUID { + id := uuid.New() + cliMustExec(t, pool, + `INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled) + VALUES ($1, $2, $3, $4, $5, 'main', $6)`, + id, connID, projectID, fmt.Sprintf("g/rc-%d", projectID), fmt.Sprintf("https://forge.e2e/g/rc-%d", projectID), enabled) + return id + } + // Project ids are distinct per fixture so the golden names which repo a call + // belongs to. + tracked := seedRepo(4173001, true) // enabled, no board columns yet + toEnable := seedRepo(4173002, false) + + var lines []string + step := func(name, method, path, body string, want int) { + t.Helper() + before := len(rec.Calls()) + w := cookieReq(t, router, method, path, jwt, body) + if w.Code != want { + t.Fatalf("%s: status = %d, want %d, body %s", name, w.Code, want, w.Body.String()) + } + calls := rec.Calls()[before:] + if len(calls) == 0 { + t.Fatalf("%s: no forge call recorded", name) + } + lines = append(lines, "# "+name) + lines = append(lines, calls...) + } + step("GetBoard, repo with no columns (board seed)", http.MethodGet, fmt.Sprintf("/api/repos/%s/board", tracked), "", http.StatusOK) + step("SyncRepo (Refresh)", http.MethodPost, fmt.Sprintf("/api/repos/%s/sync", tracked), "", http.StatusOK) + // Issue 5 was cached by the import above; drag it into a column. + step("MoveIssue (board card move)", http.MethodPost, fmt.Sprintf("/api/repos/%s/issues/5/move", tracked), `{"to_column":"In Progress"}`, http.StatusOK) + step("CreateIssue (board new issue)", http.MethodPost, fmt.Sprintf("/api/repos/%s/issues", tracked), `{"title":"new card","description":"body"}`, http.StatusCreated) + step("PromoteIssue (board promote, SetIssueLabel)", http.MethodPost, fmt.Sprintf("/api/repos/%s/issues/9/promote", tracked), "", http.StatusOK) + step("GetIssueDetail (issue page)", http.MethodGet, fmt.Sprintf("/api/repos/%s/issues/5", tracked), "", http.StatusOK) + step("ConfigureColumns", http.MethodPut, fmt.Sprintf("/api/repos/%s/board/columns", tracked), `{"columns":[{"label_name":"In Progress"},{"label_name":"Later"}]}`, http.StatusOK) + step("SetRepoEnabled true (guard, column seed, importAfterEnable)", http.MethodPut, fmt.Sprintf("/api/repos/%s", toEnable), `{"enabled":true}`, http.StatusOK) + + forgetest.AssertGolden(t, "recorded_calls_handler_repo_handlers.golden", lines) +} diff --git a/api/internal/handler/recorded_calls_worker_forge_test.go b/api/internal/handler/recorded_calls_worker_forge_test.go new file mode 100644 index 0000000000..1db122829f --- /dev/null +++ b/api/internal/handler/recorded_calls_worker_forge_test.go @@ -0,0 +1,95 @@ +package handler + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgtype" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/forgesvc" + "github.com/vtmocanu/uzi/api/internal/runkind" + "github.com/vtmocanu/uzi/api/internal/store" + "github.com/vtmocanu/uzi/api/internal/workersvc" +) + +// Recorded-call tests (PRD 4173 M4): for a repo with no issue-tracker link, +// pin every forge call the worker issue read routes make, for an issue run. +// The goldens were captured before the tracker-link rerouting; regenerate only +// deliberately, with UZI_UPDATE_GOLDEN=1. + +// rcIssueForge answers the issue reads the three routes make. +type rcIssueForge struct{ forgetest.BaseFake } + +func (*rcIssueForge) GetIssue(_ context.Context, _, iid int64) (forge.Issue, error) { + return forge.Issue{IID: iid, Title: "t", State: "opened", Description: "body", Labels: []string{"uzi"}, AuthorForgeUserID: 2, UpdatedAt: time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)}, nil +} +func (*rcIssueForge) ListIssues(context.Context, int64, forge.ListIssuesOptions) ([]forge.Issue, error) { + return []forge.Issue{{IID: 8, Title: "t8", State: "opened"}}, nil +} +func (*rcIssueForge) ListIssueComments(context.Context, int64, int64) ([]forge.IssueComment, error) { + return []forge.IssueComment{{AuthorForgeUserID: 5, Body: "c"}}, nil +} +func (*rcIssueForge) ListIssueLabelEvents(context.Context, int64, int64) ([]forge.LabelEvent, error) { + return []forge.LabelEvent{{ID: 1, Action: "add", LabelName: "uzi"}}, nil +} +func (*rcIssueForge) RepositoryAuthorEligibility(context.Context, int64, int64) (forge.AuthorEligibility, error) { + return forge.AuthorEligible, nil +} + +// rcSealedHandler wires a Handler whose worker-forge routes read an owned issue +// run (issue 7) and reach rec through the forgesvc builder seam. +func rcSealedHandler(t *testing.T, rec *forgetest.Recorder) *Handler { + t.Helper() + box := newForgeBox(t) + svc := forgesvc.NewWithForgeBuilder(nil, box, time.Second, nil, func(forge.Type, string, string, time.Duration) (forge.Forge, error) { + return rec, nil + }) + sealed, err := svc.EncryptToken("fixture") + if err != nil { + t.Fatal(err) + } + st := &forgeHandlerStore{ + ownedRun: store.Run{ + ID: uuid.New(), UserID: uuid.New(), Kind: runkind.Issue, + RepoID: pgtype.UUID{Bytes: uuid.New(), Valid: true}, + IssueIid: pgtype.Int8{Int64: 7, Valid: true}, + }, + connRow: store.GetRunForgeConnForWorkerRow{ForgeProjectID: forgeTestProjectID, ForgeType: "gitlab", BaseUrl: "https://forge.test", TokenCiphertext: sealed}, + } + return &Handler{ + wsvc: workersvc.New(st, box, workersvc.Params{CustodyHoldLimit: defaultCustodyLimit()}), + svc: svc, + } +} + +func TestRecordedCallsWorkerForgeIssueRoutes(t *testing.T) { + rec := forgetest.NewRecorder(&rcIssueForge{}) + h := rcSealedHandler(t, rec) + run := map[string]string{"id": uuid.New().String()} + withIID := func(iid string) map[string]string { + return map[string]string{"id": run["id"], "iid": iid} + } + var lines []string + step := func(name string, call func(w http.ResponseWriter, r *http.Request), req *http.Request) { + t.Helper() + before := len(rec.Calls()) + w := httptest.NewRecorder() + call(w, req) + if w.Code != http.StatusOK { + t.Fatalf("%s: status = %d, body %q", name, w.Code, w.Body.String()) + } + lines = append(lines, "# "+name) + lines = append(lines, rec.Calls()[before:]...) + } + step("WorkerForgeGetIssue own issue", h.WorkerForgeGetIssue, forgeReq(http.MethodGet, "/x", true, withIID("7"))) + step("WorkerForgeGetIssue other issue", h.WorkerForgeGetIssue, forgeReq(http.MethodGet, "/x", true, withIID("8"))) + step("WorkerForgeListIssues", h.WorkerForgeListIssues, forgeReq(http.MethodGet, "/x?state=opened&labels=uzi,PRD&updated_after=2026-03-04T05:06:07Z", true, run)) + step("WorkerForgeListIssueLabelEvents", h.WorkerForgeListIssueLabelEvents, forgeReq(http.MethodGet, "/x", true, withIID("8"))) + forgetest.AssertGolden(t, "recorded_calls_handler_worker_issue_routes.golden", lines) +} diff --git a/api/internal/handler/review_issue_file.go b/api/internal/handler/review_issue_file.go index 3238ef1231..9de8cf3f9c 100644 --- a/api/internal/handler/review_issue_file.go +++ b/api/internal/handler/review_issue_file.go @@ -123,6 +123,10 @@ func (h *Handler) FileIssue(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusInternalServerError, "internal error") return } + // PRD 4173 D13: before the claim and the forge write. + if refuseNeverEnabled(w, repo.AwaitingFirstEnable) { + return + } // Labels are assembled server-side (Decision 3/6): the single uzi run-eligibility // label (PRD #764), NEVER autopilot and NEVER from the request body — the API @@ -149,7 +153,7 @@ func (h *Handler) FileIssue(w http.ResponseWriter, r *http.Request) { return } - tr, err := h.issueTrackerForRepo(ctx, repo.ID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) + tr, err := h.issueTrackerForRepo(ctx, repo.UserID, repo.ID, repo.IssueTrackerConnectionID, repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { h.revertFiledClaim(ctx, claimID) slog.Error("file issue: build forge for connection", "error", err) @@ -161,6 +165,9 @@ func (h *Handler) FileIssue(w http.ResponseWriter, r *http.Request) { // Forge rejected the write (mock state E): the claim reverts, nothing is // persisted, the draft stays open. err is already PAT-redacted by the driver. h.revertFiledClaim(ctx, claimID) + if writeIssueWriteNotOffered(w, err) { + return + } httpx.Error(w, http.StatusBadGateway, "could not create the issue on the forge: "+err.Error()) return } diff --git a/api/internal/handler/route_limiter_mounts_test.go b/api/internal/handler/route_limiter_mounts_test.go index f1b4602d0e..89cf32b526 100644 --- a/api/internal/handler/route_limiter_mounts_test.go +++ b/api/internal/handler/route_limiter_mounts_test.go @@ -324,6 +324,7 @@ var wantRouteMounts = []routeMount{ {"DELETE", "/api/admin/products/{id}/egress-profiles/{name}", noLimiter}, {"DELETE", "/api/agent-templates/{id}", noLimiter}, {"DELETE", "/api/forge/connections/{id}", noLimiter}, + {"DELETE", "/api/issue-trackers/connections/{id}", noLimiter}, {"DELETE", "/api/me/cli-tokens/{id}", noLimiter}, // PRD #1907 M5: revoke one of the caller's own product tokens, a cookie-only, // owner-scoped DB write → noLimiter, like the CLI-token revoke above. @@ -450,6 +451,11 @@ var wantRouteMounts = []routeMount{ {"GET", "/api/forge/config", noLimiter}, {"GET", "/api/forge/connections/", noLimiter}, {"GET", "/api/forge/connections/{id}/projects", limForge}, + // PRD #4173 M4: the issue tracker connection list reads only the store; projects and tags + // call the issue tracker, so they ride the per-user forge budget. + {"GET", "/api/issue-trackers/connections/", noLimiter}, + {"GET", "/api/issue-trackers/connections/{id}/projects", limForge}, + {"GET", "/api/issue-trackers/connections/{id}/tags", limForge}, {"GET", "/api/health", noLimiter}, {"GET", "/api/me/cli-tokens/", noLimiter}, // PRD #1907 M5: the caller's own product-token list and the mint picker, cookie-only @@ -751,6 +757,7 @@ var wantRouteMounts = []routeMount{ {"POST", "/api/forge/connections/", noLimiter}, {"POST", "/api/forge/connections/{id}/privilege-check", limForge}, {"POST", "/api/forge/connections/{id}/verify", limForge}, + {"POST", "/api/issue-trackers/connections/", limForge}, {"POST", "/api/me/cli-tokens/", noLimiter}, {"POST", "/api/me/cli-tokens/revoke-all", noLimiter}, // PRD #3254: start-preview is a read that takes a token choice in a body; it writes nothing. @@ -1075,6 +1082,8 @@ var wantRouteMounts = []routeMount{ {"PUT", "/api/repos/{id}/board/prefs", noLimiter}, // PRD #557 board-access visibility write: same owner-or-admin group → noLimiter. {"PUT", "/api/repos/{id}/github-project-sync/visibility", noLimiter}, + // PRD #4173 M4: the repo link reads the issue tracker (token, projects, tags). + {"PUT", "/api/repos/{id}/issue-tracker-link", limForge}, {"PUT", "/api/repos/{id}/tool-profile", noLimiter}, // PRD #35 Decision 7, the per-run toggle. noLimiter for the same reason as // /me/wait-on-limit: one owner-scoped boolean UPDATE, no spend, no forge write, diff --git a/api/internal/handler/routes_issue_trackers.go b/api/internal/handler/routes_issue_trackers.go new file mode 100644 index 0000000000..d1ef04590f --- /dev/null +++ b/api/internal/handler/routes_issue_trackers.go @@ -0,0 +1,28 @@ +package handler + +import ( + "github.com/go-chi/chi/v5" + + mw "github.com/vtmocanu/uzi/api/internal/middleware" +) + +// mountIssueTrackerRoutes registers the cookie-only external issue tracker group +// (PRD #4173 M4): connection create/list/delete and the project and tag reads a +// link screen needs. Like the forge group, POST creates a connection from a +// token, so no v1 CLI verb reaches it. The repo link route is registered in +// mountRepoRoutes, the one registration site of the /repos tree. +func (h *Handler) mountIssueTrackerRoutes(r chi.Router, forgeLimiter *mw.Limiter) { + r.Group(func(r chi.Router) { + r.Use(h.requireAuth()) + + r.Route("/issue-trackers/connections", func(r chi.Router) { + // Connect, projects and tags call the issue tracker, so they ride the + // per-user budget the forge routes use; list and delete read only the store. + r.With(forgeLimiter.PerUserMiddleware).Post("/", h.CreateIssueTrackerConnection) + r.Get("/", h.ListIssueTrackerConnections) + r.Delete("/{id}", h.DeleteIssueTrackerConnection) + r.With(forgeLimiter.PerUserMiddleware).Get("/{id}/projects", h.ListIssueTrackerProjects) + r.With(forgeLimiter.PerUserMiddleware).Get("/{id}/tags", h.ListIssueTrackerTags) + }) + }) +} diff --git a/api/internal/handler/routes_repos.go b/api/internal/handler/routes_repos.go index b23ac33cf4..f814347fc3 100644 --- a/api/internal/handler/routes_repos.go +++ b/api/internal/handler/routes_repos.go @@ -102,6 +102,11 @@ func (h *Handler) mountRepoRoutes(r chi.Router, forgeLimiter, boardOrderLimiter r.With(forgeLimiter.PerUserMiddleware).Post("/{id}/override-request", h.RequestGuardrailOverride) // Repo-skills opt-in toggle (PRD #16): repo owner or admin. r.Patch("/{id}", h.PatchRepo) + // Link a never-enabled repo to an issue tracker project (PRD #4173 M4): web-only + // like the repo enable it must precede. It reads the issue tracker (token, project + // and tag lists), so it carries the per-user forge budget; owner-scoped in the + // service (404 for a foreign repo or connection). No unlink or relink route exists. + r.With(forgeLimiter.PerUserMiddleware).Put("/{id}/issue-tracker-link", h.LinkRepoToIssueTracker) // GitHub Projects v2 sync, owner-or-admin (issue #534, PRD #364 follow-up): // relocated out of /admin (D4). Owner path is guarded by a GetRepoForUser // preflight inside each handler; admin skips it. Instance flag still gates. diff --git a/api/internal/handler/runs_lifecycle.go b/api/internal/handler/runs_lifecycle.go index e3c76a3a20..d35c42e330 100644 --- a/api/internal/handler/runs_lifecycle.go +++ b/api/internal/handler/runs_lifecycle.go @@ -344,6 +344,9 @@ func (h *Handler) writeStartRunError(w http.ResponseWriter, r *http.Request, err httpx.Error(w, http.StatusBadGateway, err.Error()) case errors.Is(err, workersvc.ErrRepoNotFound): httpx.Error(w, http.StatusNotFound, "repo not found") + case errors.Is(store.MapNeverEnabled(err), store.ErrRepoNeverEnabled): + // PRD 4173 D13: the service gate, or the database guard's UZ413. + writeNeverEnabled(w) case errors.Is(err, workersvc.ErrIssueNotFound): httpx.Error(w, http.StatusNotFound, "issue not found on this repo's board") case errors.Is(err, workersvc.ErrDescriptionTooLarge): diff --git a/api/internal/handler/schedules.go b/api/internal/handler/schedules.go index 1b220ece94..e86853f67e 100644 --- a/api/internal/handler/schedules.go +++ b/api/internal/handler/schedules.go @@ -54,6 +54,10 @@ func (h *Handler) CreateSchedule(w http.ResponseWriter, r *http.Request) { if !ok { return } + // PRD 4173 D13: no schedule on a never-enabled repo. + if refuseNeverEnabled(w, repo.AwaitingFirstEnable) { + return + } user, _ := mw.UserFromContext(r.Context()) var req apitypes.ScheduleRequest @@ -135,6 +139,9 @@ func (h *Handler) CreateSchedule(w http.ResponseWriter, r *http.Request) { CredentialOverrideSecretID: credCols.secretID, }) if err != nil { + if writeIfNeverEnabled(w, err) { + return + } slog.Error("create run schedule", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return @@ -249,10 +256,15 @@ func (h *Handler) PatchSchedule(w http.ResponseWriter, r *http.Request) { if repoint { // Ownership mirror (create parity, PRD #344 D3): 404 on a foreign/absent repo. // No enabled/guardrail/blocked gate is added here — those run at fire time. - if _, rerr := qw.GetRepoForUser(r.Context(), store.GetRepoForUserParams{ID: repoID, UserID: user.ID}); rerr != nil { + target, rerr := qw.GetRepoForUser(r.Context(), store.GetRepoForUserParams{ID: repoID, UserID: user.ID}) + if rerr != nil { httpx.Error(w, http.StatusNotFound, "repo not found") return } + // PRD 4173 D13: a schedule may not be repointed at a never-enabled repo. + if refuseNeverEnabled(w, target.AwaitingFirstEnable) { + return + } // A schedule with CI-watch episode history keeps it only against the repo whose // workflow keys it recorded, so repointing it is refused (PRD #2865). Checked // on qw: for a ci_watch schedule that is the locked transaction, so a fire that @@ -351,6 +363,9 @@ func (h *Handler) PatchSchedule(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusConflict, "another repo in this schedule's group already uses that repo") return } + if writeIfNeverEnabled(w, err) { + return + } slog.Error("update run schedule", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return @@ -468,6 +483,8 @@ func (h *Handler) RunScheduleNow(w http.ResponseWriter, r *http.Request) { switch { case errors.Is(err, workersvc.ErrRepoNotFound): httpx.Error(w, http.StatusNotFound, "the schedule's repo is disconnected or no longer owned by you") + case errors.Is(store.MapNeverEnabled(err), store.ErrRepoNeverEnabled): + writeNeverEnabled(w) case errors.Is(err, schedsvc.ErrBadConfig): httpx.Error(w, http.StatusBadRequest, "the schedule's configuration is invalid") default: diff --git a/api/internal/handler/schedules_catalog.go b/api/internal/handler/schedules_catalog.go index 3376b2ff2d..5a841077a7 100644 --- a/api/internal/handler/schedules_catalog.go +++ b/api/internal/handler/schedules_catalog.go @@ -72,6 +72,10 @@ func (h *Handler) EnableCatalogSchedule(w http.ResponseWriter, r *http.Request) if !ok { return } + // PRD 4173 D13: no schedule on a never-enabled repo. + if refuseNeverEnabled(w, repo.AwaitingFirstEnable) { + return + } user, _ := mw.UserFromContext(r.Context()) slug := chi.URLParam(r, "slug") job, ok := schedtmpl.BySlug(slug) @@ -162,6 +166,9 @@ func (h *Handler) EnableCatalogSchedule(w http.ResponseWriter, r *http.Request) httpx.JSON(w, http.StatusOK, h.scheduleDTOWithLabel(r.Context(), existing, repo.PathWithNamespace)) return } + if writeIfNeverEnabled(w, err) { + return + } slog.Error("enable default schedule", "slug", slug, "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return diff --git a/api/internal/handler/schedules_clone.go b/api/internal/handler/schedules_clone.go index d103b23cc8..b2362f8dc4 100644 --- a/api/internal/handler/schedules_clone.go +++ b/api/internal/handler/schedules_clone.go @@ -65,10 +65,15 @@ func (h *Handler) CloneSchedule(w http.ResponseWriter, r *http.Request) { return } // Ownership mirror (create parity): 404 on a foreign/absent target repo. - if _, gerr := h.q.GetRepoForUser(r.Context(), store.GetRepoForUserParams{ID: rid, UserID: user.ID}); gerr != nil { + target, gerr := h.q.GetRepoForUser(r.Context(), store.GetRepoForUserParams{ID: rid, UserID: user.ID}) + if gerr != nil { httpx.Error(w, http.StatusNotFound, "repo not found") return } + // PRD 4173 D13: no schedule on a never-enabled repo. + if refuseNeverEnabled(w, target.AwaitingFirstEnable) { + return + } targetRepo = rid } @@ -164,6 +169,9 @@ func (h *Handler) CloneSchedule(w http.ResponseWriter, r *http.Request) { CredentialOverrideSecretID: cur.CredentialOverrideSecretID, }) if err != nil { + if writeIfNeverEnabled(w, err) { + return + } slog.Error("clone run schedule", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return @@ -223,10 +231,15 @@ func (h *Handler) AddScheduleRepo(w http.ResponseWriter, r *http.Request) { return } // Ownership mirror (clone parity): 404 on a foreign/absent target repo. - if _, gerr := h.q.GetRepoForUser(ctx, store.GetRepoForUserParams{ID: targetRepo, UserID: user.ID}); gerr != nil { + target, gerr := h.q.GetRepoForUser(ctx, store.GetRepoForUserParams{ID: targetRepo, UserID: user.ID}) + if gerr != nil { httpx.Error(w, http.StatusNotFound, "repo not found") return } + // PRD 4173 D13: no schedule on a never-enabled repo. + if refuseNeverEnabled(w, target.AwaitingFirstEnable) { + return + } if status, msg := h.validateScheduleRemoval(r.Context(), apitypes.ScheduleRequest{Target: cur.Target, Timing: cur.Timing, Labels: scheduleLabelsToSlice(cur.Labels), RemoveLabelOnDispatch: &cur.RemoveLabelOnDispatch}, schedtmpl.SelectorLabel); status != 0 { httpx.Error(w, status, msg) @@ -308,6 +321,9 @@ func (h *Handler) AddScheduleRepo(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusConflict, "this schedule already has a sibling on that repo") return } + if writeIfNeverEnabled(w, err) { + return + } slog.Error("add schedule repo: create sibling", "error", err) httpx.Error(w, http.StatusInternalServerError, "internal error") return diff --git a/api/internal/handler/testdata/recorded_calls_handler_repo_handlers.golden b/api/internal/handler/testdata/recorded_calls_handler_repo_handlers.golden new file mode 100644 index 0000000000..4907fc8733 --- /dev/null +++ b/api/internal/handler/testdata/recorded_calls_handler_repo_handlers.golden @@ -0,0 +1,29 @@ +# GetBoard, repo with no columns (board seed) +EnsureLabels(4173001, [{Name:"Planned" Color:"#6699cc"} {Name:"In Progress" Color:"#1f75cb"} {Name:"Human Review" Color:"#6e49cb"} {Name:"Later" Color:"#999999"}]) +ListIssues(4173001, {Labels:["uzi"] UpdatedAfter: State:"" Limit:0}) +ListIssues(4173001, {Labels:nil UpdatedAfter: State:"opened" Limit:0}) +ListIssues(4173001, {Labels:["agent-found"] UpdatedAfter: State:"" Limit:0}) +# SyncRepo (Refresh) +ListIssues(4173001, {Labels:["uzi"] UpdatedAfter: State:"" Limit:0}) +ListIssues(4173001, {Labels:nil UpdatedAfter: State:"opened" Limit:0}) +ListIssues(4173001, {Labels:["agent-found"] UpdatedAfter: State:"" Limit:0}) +# MoveIssue (board card move) +EnsureLabels(4173001, [{Name:"In Progress" Color:"#1f75cb"}]) +UpdateIssueLabels(4173001, 5, ["In Progress"], nil) +# CreateIssue (board new issue) +CreateIssue(4173001, "new card", "body", ["uzi"]) +# PromoteIssue (board promote, SetIssueLabel) +EnsureLabels(4173001, [{Name:"uzi" Color:"#2da160"}]) +UpdateIssueLabels(4173001, 9, ["uzi"], nil) +# GetIssueDetail (issue page) +GetIssue(4173001, 5) +# ConfigureColumns +EnsureLabels(4173001, [{Name:"In Progress" Color:"#8c8c8c"} {Name:"Later" Color:"#8c8c8c"}]) +# SetRepoEnabled true (guard, column seed, importAfterEnable) +VerifyToken() +ProjectRole(4173002, 9) +DefaultBranchProtection(4173002, "main", 9) +EnsureLabels(4173002, [{Name:"Planned" Color:"#6699cc"} {Name:"In Progress" Color:"#1f75cb"} {Name:"Human Review" Color:"#6e49cb"} {Name:"Later" Color:"#999999"}]) +ListIssues(4173002, {Labels:["uzi"] UpdatedAfter: State:"" Limit:0}) +ListIssues(4173002, {Labels:nil UpdatedAfter: State:"opened" Limit:0}) +ListIssues(4173002, {Labels:["agent-found"] UpdatedAfter: State:"" Limit:0}) diff --git a/api/internal/handler/testdata/recorded_calls_handler_worker_issue_routes.golden b/api/internal/handler/testdata/recorded_calls_handler_worker_issue_routes.golden new file mode 100644 index 0000000000..01978afc31 --- /dev/null +++ b/api/internal/handler/testdata/recorded_calls_handler_worker_issue_routes.golden @@ -0,0 +1,11 @@ +# WorkerForgeGetIssue own issue +GetIssue(4242, 7) +ListIssueComments(4242, 7) +# WorkerForgeGetIssue other issue +GetIssue(4242, 8) +RepositoryAuthorEligibility(4242, 2) +ListIssueComments(4242, 8) +# WorkerForgeListIssues +ListIssues(4242, {Labels:["uzi" "PRD"] UpdatedAfter:2026-03-04T05:06:07Z State:"opened" Limit:51}) +# WorkerForgeListIssueLabelEvents +ListIssueLabelEvents(4242, 8) diff --git a/api/internal/handler/worker_chat.go b/api/internal/handler/worker_chat.go index 2db6154aa9..5bb66ef91b 100644 --- a/api/internal/handler/worker_chat.go +++ b/api/internal/handler/worker_chat.go @@ -332,6 +332,8 @@ func (h *Handler) WorkerCreateProposal(w http.ResponseWriter, r *http.Request) { httpx.Error(w, http.StatusConflict, "the chat has ended; cannot add a proposal") case errors.Is(err, workersvc.ErrRepoNotFound): httpx.Error(w, http.StatusNotFound, "repo not found for this worker's user") + case errors.Is(err, workersvc.ErrRepoNeverEnabled): + writeNeverEnabled(w) case errors.Is(err, workersvc.ErrProposalCapReached): httpx.Error(w, http.StatusConflict, "too many pending proposals for this chat; resolve some first") default: diff --git a/api/internal/handler/worker_forge.go b/api/internal/handler/worker_forge.go index 27b1aa4b6e..3fb0b71117 100644 --- a/api/internal/handler/worker_forge.go +++ b/api/internal/handler/worker_forge.go @@ -1,6 +1,7 @@ package handler import ( + "context" "encoding/json" "errors" "log/slog" @@ -13,6 +14,7 @@ import ( "github.com/vtmocanu/uzi/api/internal/apitypes" "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forgesvc" "github.com/vtmocanu/uzi/api/internal/httpx" "github.com/vtmocanu/uzi/api/internal/issueinput" mw "github.com/vtmocanu/uzi/api/internal/middleware" @@ -98,6 +100,21 @@ func (h *Handler) resolveForgeRun(w http.ResponseWriter, r *http.Request) (worke return conn, f, true } +// workerIssueTracker is the issue side a worker route reads for the run's repo (PRD +// #4173 DL-5): the forge driver already built for an unlinked repo, byte-identical to +// before M4, and the owner-scoped resolver for a linked one. The worker never receives +// an issue tracker token (S5): it only reaches the tracker through these routes. A +// linked repo that cannot be resolved is an error, never the forge. +func (h *Handler) workerIssueTracker(ctx context.Context, conn workersvc.ForgeConn, f forge.Forge) (forge.IssueTracker, error) { + if !conn.IssueTrackerConnectionID.Valid { + return f, nil + } + if h.svc == nil && h.forgeFactory == nil { + return nil, forgesvc.ErrLinkedRepoUnresolvable + } + return h.issueTrackerForRepo(ctx, conn.UserID, conn.RepoID, conn.IssueTrackerConnectionID, conn.ForgeType, conn.BaseUrl, conn.TokenCiphertext) +} + // forgeDriverError logs the (already PAT-redacted) driver error server-side and // answers with the fixed generic 502 — the URL the SDK embeds (host + project id) // must never reach the agent. @@ -127,7 +144,12 @@ func (h *Handler) WorkerForgeGetIssue(w http.ResponseWriter, r *http.Request) { if !ok { return } - ch, tr := forge.CodeHost(f), forge.IssueTracker(f) + ch := forge.CodeHost(f) + tr, err := h.workerIssueTracker(r.Context(), conn, f) + if err != nil { + h.forgeDriverError(w, err) + return + } iid, err := parseInt64(chi.URLParam(r, "iid")) if err != nil { httpx.Error(w, http.StatusBadRequest, forgeErrInvalid) @@ -230,7 +252,11 @@ func (h *Handler) WorkerForgeListIssues(w http.ResponseWriter, r *http.Request) if !ok { return } - tr := forge.IssueTracker(f) + tr, err := h.workerIssueTracker(r.Context(), conn, f) + if err != nil { + h.forgeDriverError(w, err) + return + } q := r.URL.Query() var opts forge.ListIssuesOptions switch q.Get("state") { @@ -304,7 +330,11 @@ func (h *Handler) WorkerForgeListIssueLabelEvents(w http.ResponseWriter, r *http if !ok { return } - tr := forge.IssueTracker(f) + tr, err := h.workerIssueTracker(r.Context(), conn, f) + if err != nil { + h.forgeDriverError(w, err) + return + } iid, err := parseInt64(chi.URLParam(r, "iid")) if err != nil { httpx.Error(w, http.StatusBadRequest, forgeErrInvalid) diff --git a/api/internal/handler/worker_forge_linked_livedb_test.go b/api/internal/handler/worker_forge_linked_livedb_test.go new file mode 100644 index 0000000000..7cc6ce21b6 --- /dev/null +++ b/api/internal/handler/worker_forge_linked_livedb_test.go @@ -0,0 +1,194 @@ +package handler + +import ( + "context" + "crypto/sha256" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/forgesvc" + "github.com/vtmocanu/uzi/api/internal/issuetracker" + "github.com/vtmocanu/uzi/api/internal/issuetracker/youtracktest" + "github.com/vtmocanu/uzi/api/internal/issuetrackersvc" +) + +// linkedWorkerRun is one worker-held run on a repo linked to the YouTrack fake, plus a +// second run on an unlinked repo of the same owner, both on one recorded forge. +type linkedWorkerRun struct { + router http.Handler + srv *youtracktest.Server + rec *forgetest.Recorder + token string + linked uuid.UUID // run on the linked repo + unlinked uuid.UUID // run on the unlinked repo + issueN int64 +} + +func (w linkedWorkerRun) get(t *testing.T, path string) *httptest.ResponseRecorder { + t.Helper() + req := httptest.NewRequest(http.MethodGet, "/api/worker/runs/"+path, nil) + req.Header.Set("Authorization", "Bearer "+w.token) + rr := httptest.NewRecorder() + w.router.ServeHTTP(rr, req) + return rr +} + +func newLinkedWorkerRun(t *testing.T, wireDrivers bool) linkedWorkerRun { + t.Helper() + h, router, pool := cliLiveDB(t) + q := h.q + ctx := context.Background() + + srv := youtracktest.NewServer(t) + def, ok := http.DefaultTransport.(*http.Transport) + if !ok { + t.Fatal("http.DefaultTransport is not an *http.Transport") + } + saved := def.TLSClientConfig + def.TLSClientConfig = srv.Client().Transport.(*http.Transport).TLSClientConfig.Clone() + t.Cleanup(func() { def.TLSClientConfig = saved }) + + rec := forgetest.NewRecorder(&collisionForge{}) + forgeURL := "https://" + uuid.NewString() + ".forge.test" + svc := forgesvc.NewWithForgeBuilder(q, h.box, 5*time.Second, nil, func(forge.Type, string, string, time.Duration) (forge.Forge, error) { return rec, nil }) + itSvc := issuetrackersvc.New(q, h.box, func(u string) bool { return u == srv.URL }, issuetracker.New) + if wireDrivers { + svc.SetIssueTrackerDrivers(itSvc) + } + h.svc = svc + + sealed, err := svc.EncryptToken("forge-token") + if err != nil { + t.Fatal(err) + } + owner := cliSeedUser(t, pool, false) + conn := uuid.New() + cliMustExec(t, pool, `INSERT INTO forge_connections (id, user_id, forge_type, base_url, bot_username, bot_forge_user_id, token_ciphertext) + VALUES ($1, $2, 'gitlab', $3, 'bot', 1, $4)`, conn, owner, forgeURL, sealed) + yt, err := itSvc.Connect(ctx, owner, "youtrack", srv.URL, srv.Token) + if err != nil { + t.Fatal(err) + } + const projectID = 77 + repoL, repoP := uuid.New(), uuid.New() + cliMustExec(t, pool, `INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, + issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at) + VALUES ($1, $2, $3, $4, 'https://forge.test/g/l', 'main', true, $5, $6, $7, $8, $9, now())`, + repoL, conn, projectID, "g/linked-"+repoL.String(), yt.ID, youtracktest.ProjectID, youtracktest.ProjectShortName, youtracktest.EligibilityTagID, youtracktest.EligibilityTagName) + cliMustExec(t, pool, `INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled) + VALUES ($1, $2, $3, $4, 'https://forge.test/g/p', 'main', true)`, repoP, conn, projectID+1, "g/plain-"+repoP.String()) + + issueN := srv.SeedIssue("a youtrack card", "yt body", youtracktest.EligibilityTagID) + srv.SeedComment(issueN, "someone", "a youtrack comment") + + token := "linked-worker-" + uuid.NewString() + sum := sha256.Sum256([]byte(token)) + worker := uuid.New() + cliMustExec(t, pool, "INSERT INTO workers(id,user_id,name,token_hash,status) VALUES($1,$2,'linked-worker',$3,'online')", worker, owner, sum[:]) + seedRun := func(repo uuid.UUID, iid int64) uuid.UUID { + id := uuid.New() + cliMustExec(t, pool, `INSERT INTO runs (id, user_id, repo_id, issue_iid, issue_title, issue_description, status, kind, worker_id, claim_generation) + VALUES ($1, $2, $3, $4, 'Do X', 'desc', 'running', 'issue', $5, 1)`, id, owner, repo, iid, worker) + return id + } + srv.ResetRequests() + return linkedWorkerRun{router: router, srv: srv, rec: rec, token: token, linked: seedRun(repoL, issueN), unlinked: seedRun(repoP, 8), issueN: issueN} +} + +func (w linkedWorkerRun) forgeIssueCalls() []string { return issueSideCalls(w.rec.Calls()) } + +func (w linkedWorkerRun) eligibilityCalls() int { + n := 0 + for _, c := range w.rec.Calls() { + if strings.HasPrefix(c, "RepositoryAuthorEligibility(") { + n++ + } + } + return n +} + +// PRD #4173 M4 (DL-5, S2, S5): a worker's issue routes for a run on a linked repo reach +// the issue tracker through the API only; the merge-request routes keep the forge; an +// unlinked run is unchanged. The issue author on a linked repo is never looked up as a +// forge account. +func TestWorkerForgeIssueRoutesForLinkedRunReachYouTrackOnlyLiveDB(t *testing.T) { + w := newLinkedWorkerRun(t, true) + other := w.srv.SeedIssue("another card", "other body", youtracktest.EligibilityTagID) + w.srv.SeedComment(other, "someone", "another comment") + + for name, path := range map[string]string{ + "own issue": fmt.Sprintf("%s/forge/issues/%d", w.linked, w.issueN), + "other issue": fmt.Sprintf("%s/forge/issues/%d", w.linked, other), + "list": fmt.Sprintf("%s/forge/issues", w.linked), + "label events": fmt.Sprintf("%s/forge/issues/%d/label-events", w.linked, w.issueN), + } { + before := len(w.srv.Requests()) + rr := w.get(t, path) + if rr.Code != http.StatusOK { + t.Fatalf("%s: status %d body %s", name, rr.Code, rr.Body.String()) + } + if len(w.srv.Requests()) == before { + t.Errorf("%s: the request did not reach YouTrack", name) + } + if c := w.forgeIssueCalls(); len(c) != 0 { + t.Fatalf("%s: a linked run's issue read reached the forge: %v", name, c) + } + } + if n := w.eligibilityCalls(); n != 0 { + t.Fatalf("the forge was asked about a YouTrack issue author %d times; a linked repo's author is never a forge account", n) + } + + // The merge-request route keeps the forge driver (S2) and never touches YouTrack. + before := len(w.srv.Requests()) + if rr := w.get(t, w.linked.String()+"/forge/merge-requests/3"); rr.Code != http.StatusOK { + t.Fatalf("merge request route: %d %s", rr.Code, rr.Body.String()) + } + if len(w.srv.Requests()) != before { + t.Fatal("the merge-request route reached YouTrack") + } + if !strings.Contains(strings.Join(w.rec.Calls(), "\n"), "GetMergeRequest(") { + t.Fatalf("the merge-request route did not reach the forge: %v", w.rec.Calls()) + } + + // An unlinked run is byte-identical to before: its issue read goes to the forge only. + before = len(w.srv.Requests()) + if rr := w.get(t, w.unlinked.String()+"/forge/issues/9"); rr.Code != http.StatusOK || !strings.Contains(rr.Body.String(), `"iid":9`) { + t.Fatalf("unlinked issue read: %d %s", rr.Code, rr.Body.String()) + } + if len(w.srv.Requests()) != before { + t.Fatal("an unlinked run's issue read reached YouTrack") + } + if c := w.forgeIssueCalls(); len(c) == 0 { + t.Fatal("an unlinked run's issue read did not reach the forge") + } +} + +// A linked run whose resolver is not wired fails closed: a generic 502, no forge issue +// call and no YouTrack request. +func TestWorkerForgeIssueRoutesForLinkedRunFailClosedWithoutResolverLiveDB(t *testing.T) { + w := newLinkedWorkerRun(t, false) + for _, path := range []string{ + fmt.Sprintf("%s/forge/issues/%d", w.linked, w.issueN), + fmt.Sprintf("%s/forge/issues", w.linked), + fmt.Sprintf("%s/forge/issues/%d/label-events", w.linked, w.issueN), + } { + rr := w.get(t, path) + if rr.Code != http.StatusBadGateway { + t.Fatalf("%s: status %d, want 502 (body %s)", path, rr.Code, rr.Body.String()) + } + } + if c := w.forgeIssueCalls(); len(c) != 0 { + t.Fatalf("a linked run with no resolver fell back to the forge: %v", c) + } + if n := len(w.srv.Requests()); n != 0 { + t.Fatalf("YouTrack saw %d requests", n) + } +} diff --git a/api/internal/issuesource/issuesource.go b/api/internal/issuesource/issuesource.go new file mode 100644 index 0000000000..e44539b7fa --- /dev/null +++ b/api/internal/issuesource/issuesource.go @@ -0,0 +1,45 @@ +// Package issuesource is the seam the services that read or write a repo's issues +// use to reach a linked external issue tracker (PRD #4173 DL-5). A service keeps +// its forge factory for the code-host side and for a repo whose link is NULL; for a +// linked repo it asks Sources, and a service with no Sources wired fails closed +// instead of falling back to the forge. +package issuesource + +import ( + "context" + "errors" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" +) + +// Sources resolves the issue side of a repo, bound by repo row and owner. +// *forgesvc.Service satisfies it. +type Sources interface { + IssueTrackerForRepo(ctx context.Context, userID, repoID uuid.UUID) (forge.IssueTracker, error) +} + +// ErrNotWired is returned for a linked repo when the caller has no Sources: there +// is deliberately no fallback to the forge, whose issue numbers mean nothing there. +var ErrNotWired = errors.New("repo is linked to an issue tracker but no issue tracker source is wired") + +// ErrIssueWriteNotOffered is returned for every write to a linked external issue +// tracker (YouTrack writes arrive in PRD #4173 M5). forgesvc.ErrIssueWriteNotOffered +// is this same value; it lives here so a package that cannot import forgesvc can +// wrap and match it. +var ErrIssueWriteNotOffered = errors.New("issue writes on a repo linked to an issue tracker are not offered yet") + +// For returns the issue side a caller should use for one repo row. linked is +// whether the row's issue_tracker_connection_id is set. An unlinked row gets +// unlinked untouched (the caller's own forge driver, byte-identical to before M4); +// a linked row gets src's resolver, or ErrNotWired when src is nil. +func For(ctx context.Context, src Sources, linked bool, userID, repoID uuid.UUID, unlinked forge.IssueTracker) (forge.IssueTracker, error) { + if !linked { + return unlinked, nil + } + if src == nil { + return nil, ErrNotWired + } + return src.IssueTrackerForRepo(ctx, userID, repoID) +} diff --git a/api/internal/issuesource/issuesource_test.go b/api/internal/issuesource/issuesource_test.go new file mode 100644 index 0000000000..d225d01101 --- /dev/null +++ b/api/internal/issuesource/issuesource_test.go @@ -0,0 +1,47 @@ +package issuesource + +import ( + "context" + "errors" + "testing" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" +) + +type stubSources struct { + tr forge.IssueTracker + err error + calls int +} + +func (s *stubSources) IssueTrackerForRepo(context.Context, uuid.UUID, uuid.UUID) (forge.IssueTracker, error) { + s.calls++ + return s.tr, s.err +} + +func TestForUnlinkedReturnsTheCallersDriverWithoutAskingSources(t *testing.T) { + own, src := &forgetest.BaseFake{}, &stubSources{tr: &forgetest.BaseFake{}} + got, err := For(context.Background(), src, false, uuid.New(), uuid.New(), own) + if err != nil || got != forge.IssueTracker(own) || src.calls != 0 { + t.Fatalf("got=%v err=%v calls=%d", got, err, src.calls) + } +} + +func TestForLinkedUsesSourcesAndFailsClosedWithout(t *testing.T) { + own, resolved := &forgetest.BaseFake{}, &forgetest.BaseFake{} + src := &stubSources{tr: resolved} + got, err := For(context.Background(), src, true, uuid.New(), uuid.New(), own) + if err != nil || got != forge.IssueTracker(resolved) { + t.Fatalf("got=%v err=%v", got, err) + } + if got, err := For(context.Background(), nil, true, uuid.New(), uuid.New(), own); got != nil || !errors.Is(err, ErrNotWired) { + t.Fatalf("no sources: got=%v err=%v, want ErrNotWired and no forge fallback", got, err) + } + boom := errors.New("cannot resolve") + if _, err := For(context.Background(), &stubSources{err: boom}, true, uuid.New(), uuid.New(), own); !errors.Is(err, boom) { + t.Fatalf("resolver error lost: %v", err) + } +} diff --git a/api/internal/issuetracker/connection_service_test.go b/api/internal/issuetracker/connection_service_test.go index 4b455a4a56..6d7ed55cef 100644 --- a/api/internal/issuetracker/connection_service_test.go +++ b/api/internal/issuetracker/connection_service_test.go @@ -15,6 +15,7 @@ import ( "unicode" "github.com/google/uuid" + "github.com/jackc/pgx/v5" "github.com/vtmocanu/uzi/api/internal/config" "github.com/vtmocanu/uzi/api/internal/issuetracker" @@ -27,10 +28,19 @@ import ( // memQuerier is a minimal in-memory issuetrackersvc.Querier: only the upsert // Connect uses is real, the rest are unreachable from these tests. type memQuerier struct { + // Querier is left nil: the link queries are never reached by these tests, + // so calling one panics loudly instead of answering. + issuetrackersvc.Querier mu sync.Mutex rows []store.IssueTrackerConnection } +// LockIssueTrackerConnectionByIdentityForUpdate reports no existing row, so +// Connect takes its first-connect path (these tests never reconnect). +func (m *memQuerier) LockIssueTrackerConnectionByIdentityForUpdate(context.Context, store.LockIssueTrackerConnectionByIdentityForUpdateParams) (store.IssueTrackerConnection, error) { + return store.IssueTrackerConnection{}, pgx.ErrNoRows +} + func (m *memQuerier) UpsertIssueTrackerConnection(_ context.Context, a store.UpsertIssueTrackerConnectionParams) (store.IssueTrackerConnection, error) { m.mu.Lock() defer m.mu.Unlock() diff --git a/api/internal/issuetrackersvc/connect.go b/api/internal/issuetrackersvc/connect.go index 30ca821ecc..53c110edb0 100644 --- a/api/internal/issuetrackersvc/connect.go +++ b/api/internal/issuetrackersvc/connect.go @@ -2,9 +2,11 @@ package issuetrackersvc import ( "context" + "errors" "strings" "github.com/google/uuid" + "github.com/jackc/pgx/v5" "github.com/vtmocanu/uzi/api/internal/issuetracker" "github.com/vtmocanu/uzi/api/internal/store" @@ -17,6 +19,11 @@ import ( // verification, account check, seal, upsert. A refused base URL never reaches the driver factory, so no outbound // request or token use happens for it. The token is trimmed of surrounding // whitespace, and it appears in no returned error or log line. +// +// Reconnecting never changes the account of a connection that a repo links to +// (PRD #4173 D11, D14): the new token must belong to the stored account, else the +// result is a Conflict. The check takes the row lock FOR UPDATE and then counts the +// linked repos in a separate statement; the upsert's own WHERE is only a last defence. func (s *Service) Connect(ctx context.Context, userID uuid.UUID, kind, baseURL, token string) (Connection, error) { parsedKind, ok := issuetracker.ParseKind(strings.TrimSpace(kind)) if !ok { @@ -59,17 +66,75 @@ func (s *Service) Connect(ctx context.Context, userID uuid.UUID, kind, baseURL, s.log.Error("seal issue tracker token", "error", err) return Connection{}, internal() } - row, err := s.q.UpsertIssueTrackerConnection(ctx, store.UpsertIssueTrackerConnectionParams{ + params := store.UpsertIssueTrackerConnectionParams{ UserID: userID, Kind: string(parsedKind), BaseUrl: norm, AccountID: account.ID, AccountLogin: account.Login, TokenCiphertext: sealed, + } + var row store.IssueTrackerConnection + err = s.inTx(ctx, func(q Querier) error { + // The existing row is locked FOR UPDATE first, so a link (which holds the row + // FOR SHARE) and this reconnect cannot interleave: the link either commits + // before the count below and is seen by it, or the lock waits for it. + // + // When no row exists yet the row is inserted with ON CONFLICT DO NOTHING. If + // someone else inserted it meanwhile that returns no row, and the locked path + // is entered once more: the row exists now, may already carry a link, and only + // the lock and a fresh count statement can see that. An upsert's own WHERE + // cannot: its subquery reads the statement's snapshot, taken before the wait + // on a link's lock, so it is kept only as a last defence (see the query). + for attempt := 0; ; attempt++ { + existing, err := q.LockIssueTrackerConnectionByIdentityForUpdate(ctx, store.LockIssueTrackerConnectionByIdentityForUpdateParams{ + UserID: userID, Kind: string(parsedKind), BaseUrl: norm, + }) + switch { + case errors.Is(err, pgx.ErrNoRows): + if attempt == 0 { + row, err = q.InsertIssueTrackerConnectionIfAbsent(ctx, store.InsertIssueTrackerConnectionIfAbsentParams(params)) + if err == nil { + return nil // a first connect: nothing can link to a row that did not exist + } + if errors.Is(err, pgx.ErrNoRows) { + continue // inserted by another connect meanwhile: lock it and count + } + return err + } + // Still no row to lock after the re-entry: the row the insert lost to was + // deleted meanwhile. Fall through to the upsert, which inserts a row with a + // new id that no link can reference yet. Accepted boundary: if a third + // connect re-inserts the row and a link commits on it while this upsert + // waits, only the upsert's weaker WHERE stands in the way (see the query). + case err != nil: + return err + case existing.AccountID != account.ID: + // A separate statement after the lock, so READ COMMITTED sees every link + // committed before the lock was granted. + n, err := q.CountReposLinkedToIssueTrackerConnection(ctx, existing.ID) + if err != nil { + return err + } + if n > 0 { + return errReconnectChangesAccount() + } + } + row, err = q.UpsertIssueTrackerConnection(ctx, params) + if errors.Is(err, pgx.ErrNoRows) { + // The upsert's WHERE refused (defence in depth; see the query). + return errReconnectChangesAccount() + } + return err + } }) if err != nil { - s.log.Error("upsert issue tracker connection", "error", err) - return Connection{}, internal() + return Connection{}, s.txError("connect issue tracker", err) } return toConnection(row), nil } + +func errReconnectChangesAccount() *Error { + return conflict("this connection is linked to a repo and the new token belongs to a different account; " + + "use a token of the account the connection already stands for, or connect to a different base URL") +} diff --git a/api/internal/issuetrackersvc/errors.go b/api/internal/issuetrackersvc/errors.go index 3cab1d6534..745609234a 100644 --- a/api/internal/issuetrackersvc/errors.go +++ b/api/internal/issuetrackersvc/errors.go @@ -12,6 +12,9 @@ const ( KindBadGateway // KindInternal: a database or crypto failure, already logged (500). KindInternal + // KindConflict: the request is well formed but the current state refuses it, + // for example linking a repo that was already enabled (HTTP 409). + KindConflict ) // Error is the typed failure of every Service method. Message is safe to show @@ -27,3 +30,5 @@ func (e *Error) Error() string { return e.Message } func internal() *Error { return &Error{Kind: KindInternal, Message: "internal error"} } func notFound() *Error { return &Error{Kind: KindNotFound, Message: "connection not found"} } + +func conflict(msg string) *Error { return &Error{Kind: KindConflict, Message: msg} } diff --git a/api/internal/issuetrackersvc/link.go b/api/internal/issuetrackersvc/link.go new file mode 100644 index 0000000000..d397f74a8e --- /dev/null +++ b/api/internal/issuetrackersvc/link.go @@ -0,0 +1,248 @@ +package issuetrackersvc + +import ( + "context" + "errors" + "time" + "unicode/utf8" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "github.com/vtmocanu/uzi/api/internal/issuetracker" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// Bounds the repos_issue_tracker_link_shape CHECK (migration 00425) enforces. +const ( + maxProjectShortLen = 44 + maxTagNameLen = 256 +) + +// Link is the stored link of one repo to one issue tracker project. +type Link struct { + RepoID uuid.UUID + ConnectionID uuid.UUID + ProjectID string + ProjectShort string + TagID string + TagName string + LinkedAt time.Time +} + +// LinkRepo links a repo that has never been enabled to one project of one of +// the user's issue tracker connections, with the tag that marks eligible issues +// (PRD #4173 D13, D14). It is refused unless the repo is still awaiting its +// first enable and is not linked yet; there is no relink and no unlink. +// +// The check against the issue tracker is read-only (token, project list, tag +// list); it never writes to the tracker. The tag must be one the connection's +// account can apply and that someone else can apply too. That is a statement +// about the tag's sharing settings only: it does not prove that anyone can edit +// issues in the project, and no message here says it does. The database part runs +// in one transaction holding the repo row lock the first enable also takes, so +// the link and the first enable end in exactly one of the two outcomes. +func (s *Service) LinkRepo(ctx context.Context, userID, repoID, connID uuid.UUID, projectID, tagID string) (Link, error) { + src, err := s.q.GetRepoIssueSourceForUser(ctx, store.GetRepoIssueSourceForUserParams{ID: repoID, UserID: userID}) + if errors.Is(err, pgx.ErrNoRows) { + return Link{}, &Error{Kind: KindNotFound, Message: "repo not found"} + } + if err != nil { + s.log.Error("read repo for issue tracker link", "error", err) + return Link{}, internal() + } + if err := linkable(src.AwaitingFirstEnable, src.IssueTrackerConnectionID.Valid); err != nil { + return Link{}, err + } + row, err := s.getRow(ctx, userID, connID) + if err != nil { + return Link{}, err + } + if !issuetracker.ValidProjectID(projectID) { + return Link{}, &Error{Kind: KindBadRequest, Message: "invalid project id"} + } + + account, project, tag, err := s.checkLinkTarget(ctx, userID, connID, row, projectID, tagID) + if err != nil { + return Link{}, err + } + + var repo store.Repo + err = s.inTx(ctx, func(q Querier) error { + lock, err := q.LockRepoForLinkForUser(ctx, store.LockRepoForLinkForUserParams{ID: repoID, UserID: userID}) + if errors.Is(err, pgx.ErrNoRows) { + return &Error{Kind: KindNotFound, Message: "repo not found"} + } + if err != nil { + return err + } + if err := linkable(lock.AwaitingFirstEnable, lock.IssueTrackerConnectionID.Valid); err != nil { + return err + } + // FOR SHARE: a reconnect of this connection that changes its account holds + // the row FOR UPDATE, so it either finished before this read (and the + // account below differs) or waits for this transaction. + crow, err := q.LockIssueTrackerConnectionForShareForUser(ctx, store.LockIssueTrackerConnectionForShareForUserParams{ID: connID, UserID: userID}) + if errors.Is(err, pgx.ErrNoRows) { + return notFound() + } + if err != nil { + return err + } + if crow.AccountID != account.ID { + return errAccountChanged() + } + stores, err := q.RepoStoresIssueNumber(ctx, repoID) + if err != nil { + return err + } + if stores { + return conflict("this repo already stores issue numbers and cannot be linked to an issue tracker") + } + repo, err = q.SetRepoIssueTrackerLink(ctx, store.SetRepoIssueTrackerLinkParams{ + ConnectionID: connID, + ProjectID: project.ID, + ProjectShort: project.ShortName, + TagID: tag.ID, + TagName: tag.Name, + RepoID: repoID, + UserID: userID, + }) + if errors.Is(err, pgx.ErrNoRows) { + return conflict("the repo could not be linked: it was enabled, linked or removed meanwhile") + } + return err + }) + if err != nil { + return Link{}, s.txError("link repo to issue tracker", err) + } + return Link{ + RepoID: repo.ID, + ConnectionID: connID, + ProjectID: repo.IssueTrackerProjectID.String, + ProjectShort: repo.IssueTrackerProjectShort.String, + TagID: repo.EligibilityTagID.String, + TagName: repo.EligibilityTagName.String, + LinkedAt: repo.IssueTrackerLinkedAt.Time, + }, nil +} + +// linkable refuses a repo that was enabled at some point or is already linked. +func linkable(awaiting, linked bool) *Error { + if linked { + return conflict("this repo is already linked to an issue tracker project; a link cannot be changed") + } + if !awaiting { + return conflict("only a repo that has never been enabled can be linked to an issue tracker; this one was enabled before") + } + return nil +} + +func errAccountChanged() *Error { + return conflict("the connection's token now belongs to a different account than the one this connection was stored for; reconnect first") +} + +// checkLinkTarget is the read-only part of LinkRepo. It returns the verified +// account and the project and tag exactly as the tracker listed them. +func (s *Service) checkLinkTarget(ctx context.Context, userID, connID uuid.UUID, row store.IssueTrackerConnection, projectID, tagID string) (issuetracker.Account, issuetracker.Project, issuetracker.Tag, error) { + var ( + noAccount issuetracker.Account + noProject issuetracker.Project + noTag issuetracker.Tag + ) + d, err := s.DriverFor(ctx, userID, connID, issuetracker.Binding{}, "", "", "") + if err != nil { + return noAccount, noProject, noTag, err + } + account, err := d.VerifyToken(ctx) + if err != nil { + return noAccount, noProject, noTag, &Error{Kind: KindBadGateway, Message: "token verification failed: " + err.Error()} + } + if err := validateAccount(account); err != nil { + return noAccount, noProject, noTag, &Error{Kind: KindBadGateway, Message: err.Error()} + } + if account.ID != row.AccountID { + return noAccount, noProject, noTag, errAccountChanged() + } + + projects, err := d.ListProjects(ctx) + if err != nil { + return noAccount, noProject, noTag, &Error{Kind: KindBadGateway, Message: "could not list projects: " + err.Error()} + } + var project issuetracker.Project + found := false + for _, p := range projects { + if p.ID == projectID { + project, found = p, true + break + } + } + if !found { + return noAccount, noProject, noTag, &Error{Kind: KindBadRequest, Message: "project not found among the projects this connection can see"} + } + if utf8.RuneCountInString(project.ShortName) > maxProjectShortLen || + issuetracker.ValidProjectRef(issuetracker.ProjectRef{ID: project.ID, ShortName: project.ShortName}) != nil { + return noAccount, noProject, noTag, &Error{Kind: KindBadRequest, Message: "the project's short name cannot be used for a link: it must start with a letter, hold only letters, digits and underscores, and be at most 44 characters"} + } + + tags, err := d.ListTags(ctx) + if err != nil { + return noAccount, noProject, noTag, &Error{Kind: KindBadGateway, Message: "could not list tags: " + err.Error()} + } + var tag issuetracker.Tag + found = false + for _, t := range tags { + if t.ID == tagID { + tag, found = t, true + break + } + } + if !found { + return noAccount, noProject, noTag, &Error{Kind: KindBadRequest, Message: "tag not found among the tags this connection's account can see"} + } + if utf8.RuneCountInString(tag.Name) > maxTagNameLen || + issuetracker.ValidTagRef(issuetracker.TagRef{ID: tag.ID, Name: tag.Name}) != nil { + return noAccount, noProject, noTag, &Error{Kind: KindBadRequest, Message: "the tag's name cannot be used as an eligibility tag: rename it in the issue tracker (no braces, no control characters)"} + } + if !issuetracker.AccountCanApply(tag, account) { + return noAccount, noProject, noTag, conflict("the connection's account cannot apply this tag; in the issue tracker, share the tag's use settings with this account, " + + "or with All Users, or pick a tag the account owns") + } + if !issuetracker.OthersCanApply(tag, account) { + return noAccount, noProject, noTag, conflict("nobody other than the connection's account can apply this tag; in the issue tracker, " + + "share the tag's use settings with named users or with All Users. " + + "This only checks who can apply the tag; it does not show who can edit issues in the project") + } + return account, project, tag, nil +} + +// TagView is a listed tag with the two sharing verdicts the link requires, +// judged for the connection's stored account. +type TagView struct { + Tag issuetracker.Tag + AccountCanApply bool + OthersCanApply bool +} + +// ListTagsWithVerdicts lists the tags the connection's token can see, each with +// whether the connection's account can apply it and whether anyone else can. +func (s *Service) ListTagsWithVerdicts(ctx context.Context, userID, connID uuid.UUID) ([]TagView, error) { + row, err := s.getRow(ctx, userID, connID) + if err != nil { + return nil, err + } + tags, err := s.ListTags(ctx, userID, connID) + if err != nil { + return nil, err + } + account := issuetracker.Account{ID: row.AccountID, Login: row.AccountLogin} + out := make([]TagView, 0, len(tags)) + for _, t := range tags { + out = append(out, TagView{ + Tag: t, + AccountCanApply: issuetracker.AccountCanApply(t, account), + OthersCanApply: issuetracker.OthersCanApply(t, account), + }) + } + return out, nil +} diff --git a/api/internal/issuetrackersvc/link_fake_test.go b/api/internal/issuetrackersvc/link_fake_test.go new file mode 100644 index 0000000000..e38f90f216 --- /dev/null +++ b/api/internal/issuetrackersvc/link_fake_test.go @@ -0,0 +1,139 @@ +package issuetrackersvc + +import ( + "context" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgtype" + + "github.com/vtmocanu/uzi/api/internal/store" +) + +// fakeRepo is the link-relevant slice of a repos row. +type fakeRepo struct { + userID uuid.UUID + awaiting bool + linkedConn uuid.UUID // zero when unlinked + projectID string + projectShort string + tagID string + tagName string + storesNumbers bool +} + +func (f *fakeQuerier) addRepo(user uuid.UUID, awaiting bool) uuid.UUID { + f.mu.Lock() + defer f.mu.Unlock() + if f.repos == nil { + f.repos = map[uuid.UUID]*fakeRepo{} + } + id := uuid.New() + f.repos[id] = &fakeRepo{userID: user, awaiting: awaiting} + return id +} + +func (f *fakeQuerier) repo(id uuid.UUID) *fakeRepo { + f.mu.Lock() + defer f.mu.Unlock() + return f.repos[id] +} + +func (f *fakeQuerier) ownedRepo(id, user uuid.UUID) (*fakeRepo, error) { + r := f.repos[id] + if r == nil || r.userID != user { + return nil, pgx.ErrNoRows + } + return r, nil +} + +func (f *fakeQuerier) GetRepoIssueSourceForUser(_ context.Context, a store.GetRepoIssueSourceForUserParams) (store.GetRepoIssueSourceForUserRow, error) { + f.mu.Lock() + defer f.mu.Unlock() + r, err := f.ownedRepo(a.ID, a.UserID) + if err != nil { + return store.GetRepoIssueSourceForUserRow{}, err + } + return store.GetRepoIssueSourceForUserRow{ + ID: a.ID, AwaitingFirstEnable: r.awaiting, + IssueTrackerConnectionID: pgtype.UUID{Bytes: r.linkedConn, Valid: r.linkedConn != uuid.Nil}, + }, nil +} + +func (f *fakeQuerier) LockRepoForLinkForUser(_ context.Context, a store.LockRepoForLinkForUserParams) (store.LockRepoForLinkForUserRow, error) { + f.mu.Lock() + defer f.mu.Unlock() + r, err := f.ownedRepo(a.ID, a.UserID) + if err != nil { + return store.LockRepoForLinkForUserRow{}, err + } + return store.LockRepoForLinkForUserRow{ + ID: a.ID, AwaitingFirstEnable: r.awaiting, + IssueTrackerConnectionID: pgtype.UUID{Bytes: r.linkedConn, Valid: r.linkedConn != uuid.Nil}, + }, nil +} + +func (f *fakeQuerier) RepoStoresIssueNumber(_ context.Context, id uuid.UUID) (bool, error) { + f.mu.Lock() + defer f.mu.Unlock() + if r := f.repos[id]; r != nil { + return r.storesNumbers, nil + } + return false, nil +} + +func (f *fakeQuerier) SetRepoIssueTrackerLink(_ context.Context, a store.SetRepoIssueTrackerLinkParams) (store.Repo, error) { + f.mu.Lock() + defer f.mu.Unlock() + r, err := f.ownedRepo(a.RepoID, a.UserID) + if err != nil || !r.awaiting || r.linkedConn != uuid.Nil { + return store.Repo{}, pgx.ErrNoRows + } + r.linkedConn, r.projectID, r.projectShort, r.tagID, r.tagName = a.ConnectionID, a.ProjectID, a.ProjectShort, a.TagID, a.TagName + now := pgtype.Timestamptz{Valid: true} + text := func(s string) pgtype.Text { return pgtype.Text{String: s, Valid: true} } + return store.Repo{ + ID: a.RepoID, IssueTrackerConnectionID: pgtype.UUID{Bytes: a.ConnectionID, Valid: true}, + IssueTrackerProjectID: text(a.ProjectID), IssueTrackerProjectShort: text(a.ProjectShort), + EligibilityTagID: text(a.TagID), EligibilityTagName: text(a.TagName), IssueTrackerLinkedAt: now, + }, nil +} + +func (f *fakeQuerier) CountReposLinkedToIssueTrackerConnection(_ context.Context, id uuid.UUID) (int64, error) { + f.mu.Lock() + defer f.mu.Unlock() + var n int64 + for _, r := range f.repos { + if r.linkedConn == id { + n++ + } + } + return n, nil +} + +func (f *fakeQuerier) LockIssueTrackerConnectionByIdentityForUpdate(_ context.Context, a store.LockIssueTrackerConnectionByIdentityForUpdateParams) (store.IssueTrackerConnection, error) { + f.mu.Lock() + defer f.mu.Unlock() + for _, r := range f.rows { + if r.UserID == a.UserID && r.Kind == a.Kind && r.BaseUrl == a.BaseUrl { + return r, nil + } + } + return store.IssueTrackerConnection{}, pgx.ErrNoRows +} + +func (f *fakeQuerier) LockIssueTrackerConnectionForShareForUser(ctx context.Context, a store.LockIssueTrackerConnectionForShareForUserParams) (store.IssueTrackerConnection, error) { + return f.GetIssueTrackerConnectionForUser(ctx, store.GetIssueTrackerConnectionForUserParams(a)) +} + +func (f *fakeQuerier) InsertIssueTrackerConnectionIfAbsent(ctx context.Context, a store.InsertIssueTrackerConnectionIfAbsentParams) (store.IssueTrackerConnection, error) { + f.mu.Lock() + for _, r := range f.rows { + if r.UserID == a.UserID && r.Kind == a.Kind && r.BaseUrl == a.BaseUrl { + f.mu.Unlock() + return store.IssueTrackerConnection{}, pgx.ErrNoRows + } + } + f.mu.Unlock() + return f.UpsertIssueTrackerConnection(ctx, store.UpsertIssueTrackerConnectionParams(a)) +} diff --git a/api/internal/issuetrackersvc/link_livedb_test.go b/api/internal/issuetrackersvc/link_livedb_test.go new file mode 100644 index 0000000000..6fce9adb15 --- /dev/null +++ b/api/internal/issuetrackersvc/link_livedb_test.go @@ -0,0 +1,711 @@ +package issuetrackersvc + +// Live-DB tests of the repo link and the reconnect rule (PRD #4173 M4, D13). They need +// UZI_TEST_DATABASE_URL pointing at a throwaway Postgres and run through +// e2e/run-store-it.sh, which lists this package. + +import ( + "context" + "fmt" + "io" + "log/slog" + "os" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgtype" + "github.com/jackc/pgx/v5/pgxpool" + + "github.com/vtmocanu/uzi/api/internal/issuetracker" + "github.com/vtmocanu/uzi/api/internal/secretbox" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// Assembled at runtime, like every token fixture here. +var ( + tokenA = testToken("link", "livedb", "A") + tokenB = testToken("link", "livedb", "B") +) + +// liveDriver reports the account its token stands for and can pause in VerifyToken. +type liveDriver struct { + fakeDriver + h *liveHarness +} + +func (d *liveDriver) VerifyToken(ctx context.Context) (issuetracker.Account, error) { + acct := linkBot + if d.opts.Token == tokenB { + acct = linkOther + } + if hook := d.h.afterVerify.Load(); hook != nil { + (*hook)() + } + return acct, nil +} + +type liveHarness struct { + t *testing.T + pool *pgxpool.Pool + q *store.Queries + svc *Service + user uuid.UUID + forgeConn uuid.UUID + conn uuid.UUID + projects []issuetracker.Project + tags []issuetracker.Tag + // afterVerify, when set, runs inside every driver VerifyToken after the account is chosen, + // so a test can pause a link or reconnect between its tracker check and its transaction. + afterVerify atomic.Pointer[func()] + nextProject atomic.Int64 +} + +func newLiveHarness(t *testing.T) *liveHarness { + t.Helper() + dsn := os.Getenv("UZI_TEST_DATABASE_URL") + if dsn == "" { + t.Skip("UZI_TEST_DATABASE_URL not set; run via ./e2e/run-store-it.sh for live-DB coverage") + } + ctx := context.Background() + if err := store.Migrate(ctx, dsn); err != nil { + t.Fatalf("migrate: %v", err) + } + pool, err := store.OpenPool(ctx, dsn) + if err != nil { + t.Fatalf("open pool: %v", err) + } + t.Cleanup(pool.Close) + + key := make([]byte, secretbox.KeySize) + for i := range key { + key[i] = byte(i*3 + 5) + } + box, err := secretbox.New(key) + if err != nil { + t.Fatal(err) + } + q := store.New(pool) + h := &liveHarness{t: t, pool: pool, q: q, user: uuid.New(), forgeConn: uuid.New()} + h.projects = []issuetracker.Project{{ID: "0-1", ShortName: "UZI", Name: "Uzi"}} + h.tags = []issuetracker.Tag{{ID: "5-1", Name: "uzi-ready", Owner: linkBot, Use: issuetracker.SharingSettings{Groups: []issuetracker.Group{linkAll}}}} + h.svc = New(q, box, func(u string) bool { return u == testHost }, + func(_ issuetracker.Kind, o issuetracker.Options) (issuetracker.Driver, error) { + d := &liveDriver{h: h} + d.opts, d.projects, d.tags = o, h.projects, h.tags + return d, nil + }).UseTransactions(pool, func(tx pgx.Tx) Querier { return q.WithTx(tx) }) + h.svc.log = slog.New(slog.NewTextHandler(io.Discard, nil)) + + h.exec(`INSERT INTO users (id, email, password_hash, is_admin, vault_mode) VALUES ($1, $2, 'x', false, 'unprotected')`, + h.user, fmt.Sprintf("itsvc-%s@e2e", h.user)) + t.Cleanup(func() { + // Repos and connections cascade from the user. + if _, err := pool.Exec(context.Background(), `DELETE FROM users WHERE id = $1`, h.user); err != nil { + t.Errorf("cleanup user: %v", err) + } + }) + h.exec(`INSERT INTO forge_connections (id, user_id, forge_type, base_url, bot_username, bot_forge_user_id, token_ciphertext) + VALUES ($1, $2, 'gitlab', $3, 'bot', 1, $4)`, h.forgeConn, h.user, "https://"+h.forgeConn.String()+".e2e", []byte{0x1}) + c, err := h.svc.Connect(ctx, h.user, "youtrack", testHost, tokenA) + if err != nil { + t.Fatal(err) + } + h.conn = c.ID + return h +} + +func (h *liveHarness) exec(sql string, args ...any) { + h.t.Helper() + if _, err := h.pool.Exec(context.Background(), sql, args...); err != nil { + h.t.Fatalf("exec %q: %v", sql, err) + } +} + +// freshRepo is a repo the way the membership listing creates it: awaiting its first enable. +func (h *liveHarness) freshRepo() uuid.UUID { + h.t.Helper() + r, err := h.q.UpsertRepo(context.Background(), store.UpsertRepoParams{ + ConnectionID: h.forgeConn, ForgeProjectID: 1000 + h.nextProject.Add(1), + PathWithNamespace: "g/r", WebUrl: "https://forge.e2e/g/r", DefaultBranch: pgtype.Text{String: "main", Valid: true}, + }) + if err != nil { + h.t.Fatal(err) + } + if !r.AwaitingFirstEnable { + h.t.Fatal("UpsertRepo did not mark the repo awaiting its first enable") + } + return r.ID +} + +func (h *liveHarness) setEnabled(repo uuid.UUID, enabled bool) { + h.t.Helper() + if _, err := h.q.SetRepoEnabledForUser(context.Background(), store.SetRepoEnabledForUserParams{ID: repo, Enabled: enabled, UserID: h.user}); err != nil { + h.t.Fatal(err) + } +} + +func (h *liveHarness) link(repo uuid.UUID) error { + _, err := h.svc.LinkRepo(context.Background(), h.user, repo, h.conn, "0-1", "5-1") + return err +} + +func (h *liveHarness) repoRow(repo uuid.UUID) store.Repo { + h.t.Helper() + var r store.Repo + rows, err := h.pool.Query(context.Background(), `SELECT * FROM repos WHERE id = $1`, repo) + if err != nil { + h.t.Fatal(err) + } + r, err = pgx.CollectOneRow(rows, pgx.RowToStructByPos[store.Repo]) + if err != nil { + h.t.Fatal(err) + } + return r +} + +func (h *liveHarness) connAccount() string { + h.t.Helper() + row, err := h.q.GetIssueTrackerConnectionForUser(context.Background(), store.GetIssueTrackerConnectionForUserParams{ID: h.conn, UserID: h.user}) + if err != nil { + h.t.Fatal(err) + } + return row.AccountID +} + +func TestLinkRepoLiveDB(t *testing.T) { + t.Run("a fresh repo links", func(t *testing.T) { + h := newLiveHarness(t) + repo := h.freshRepo() + if err := h.link(repo); err != nil { + t.Fatal(err) + } + r := h.repoRow(repo) + if r.IssueTrackerConnectionID.Bytes != h.conn || r.IssueTrackerProjectShort.String != "UZI" || r.EligibilityTagName.String != "uzi-ready" || !r.AwaitingFirstEnable { + t.Fatalf("row = %+v", r) + } + }) + t.Run("relink is refused and changes nothing", func(t *testing.T) { + h := newLiveHarness(t) + repo := h.freshRepo() + if err := h.link(repo); err != nil { + t.Fatal(err) + } + h.tags[0].Name = "renamed" + _ = wantKind(t, h.link(repo), KindConflict) + if got := h.repoRow(repo).EligibilityTagName.String; got != "uzi-ready" { + t.Fatalf("tag name after a refused relink = %q", got) + } + }) + t.Run("a repo that predates the marker is refused", func(t *testing.T) { + h := newLiveHarness(t) + repo := uuid.New() + // Raw insert, the way every row that existed at the migration looks: not awaiting. + h.exec(`INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch) + VALUES ($1, $2, 7, 'g/old', 'https://forge.e2e/g/old', 'main')`, repo, h.forgeConn) + _ = wantKind(t, h.link(repo), KindConflict) + if h.repoRow(repo).IssueTrackerConnectionID.Valid { + t.Fatal("the repo was linked") + } + }) + t.Run("a repo enabled since is refused", func(t *testing.T) { + h := newLiveHarness(t) + repo := h.freshRepo() + h.setEnabled(repo, true) + _ = wantKind(t, h.link(repo), KindConflict) + }) + t.Run("a repo enabled and disabled again is refused", func(t *testing.T) { + h := newLiveHarness(t) + repo := h.freshRepo() + h.setEnabled(repo, true) + h.setEnabled(repo, false) + if h.repoRow(repo).AwaitingFirstEnable { + t.Fatal("disabling put the marker back") + } + _ = wantKind(t, h.link(repo), KindConflict) + }) + t.Run("a repo of another user is not found", func(t *testing.T) { + h := newLiveHarness(t) + repo := h.freshRepo() + _, err := h.svc.LinkRepo(context.Background(), uuid.New(), repo, h.conn, "0-1", "5-1") + _ = wantKind(t, err, KindNotFound) + }) +} + +func TestConnectionWithALinkedRepoLiveDB(t *testing.T) { + ctx := context.Background() + h := newLiveHarness(t) + repo := h.freshRepo() + if err := h.link(repo); err != nil { + t.Fatal(err) + } + + _ = wantKind(t, h.svc.Delete(ctx, h.user, h.conn), KindConflict) + // The database refuses it too, which is what a delete racing the link meets. + _, err := h.q.DeleteIssueTrackerConnectionForUser(ctx, store.DeleteIssueTrackerConnectionForUserParams{ID: h.conn, UserID: h.user}) + if !isForeignKeyViolation(err) { + t.Fatalf("raw delete err = %v, want a foreign key violation", err) + } + if got := h.svc.txError("x", err); got.Kind != KindConflict { + t.Fatalf("a foreign key violation maps to kind %d, want Conflict", got.Kind) + } + + _, err = h.svc.Connect(ctx, h.user, "youtrack", testHost, tokenB) + if e := wantKind(t, err, KindConflict); !strings.Contains(e.Message, "different account") { + t.Errorf("message %q", e.Message) + } + if h.connAccount() != linkBot.ID { + t.Fatal("a refused reconnect changed the account") + } + // The same account may rotate its token. + if _, err := h.svc.Connect(ctx, h.user, "youtrack", testHost, tokenA+"-rotated"); err != nil { + t.Fatalf("same-account reconnect: %v", err) + } +} + +func TestReconnectWithAnotherAccountWhenUnlinkedLiveDB(t *testing.T) { + h := newLiveHarness(t) + if _, err := h.svc.Connect(context.Background(), h.user, "youtrack", testHost, tokenB); err != nil { + t.Fatal(err) + } + if h.connAccount() != linkOther.ID { + t.Fatal("the account did not change") + } + if err := h.svc.Delete(context.Background(), h.user, h.conn); err != nil { + t.Fatalf("an unlinked connection deletes: %v", err) + } +} + +// Link racing the first enable: whichever statement takes the repo row lock first decides, and +// each iteration ends in exactly one of the two D13 outcomes. +func TestLinkRacingFirstEnableLiveDB(t *testing.T) { + h := newLiveHarness(t) + ctx := context.Background() + linkedFirst, enabledFirst := 0, 0 + for iter := range 30 { + repo := h.freshRepo() + var linkErr, enableErr error + var ready, done sync.WaitGroup + start := make(chan struct{}) + run := func(fn func()) { + ready.Add(1) + done.Add(1) + go func() { + defer done.Done() + ready.Done() + <-start + fn() + }() + } + // Odd iterations release the enable when the link's tracker check has finished, so it + // lands in the window of the link's own transaction; even ones start both together. + checked := make(chan struct{}) + var once sync.Once + hook := func() { once.Do(func() { close(checked) }) } + h.afterVerify.Store(&hook) + run(func() { linkErr = h.link(repo) }) + run(func() { + if iter%2 == 1 { + <-checked + } + _, enableErr = h.q.SetRepoEnabledForUser(ctx, store.SetRepoEnabledForUserParams{ID: repo, Enabled: true, UserID: h.user}) + }) + ready.Wait() + close(start) + done.Wait() + + if enableErr != nil { + t.Fatalf("iteration %d: enable: %v", iter, enableErr) + } + r := h.repoRow(repo) + if !r.Enabled || r.AwaitingFirstEnable { + t.Fatalf("iteration %d: repo not enabled with the marker cleared: %+v", iter, r) + } + switch { + case linkErr == nil && r.IssueTrackerConnectionID.Valid: + linkedFirst++ // the link committed first and the enable then enabled a linked repo + case linkErr != nil && !r.IssueTrackerConnectionID.Valid: + _ = wantKind(t, linkErr, KindConflict) // the enable committed first and the link was refused + enabledFirst++ + default: + t.Fatalf("iteration %d: link err = %v but linked = %v", iter, linkErr, r.IssueTrackerConnectionID.Valid) + } + } + t.Logf("link first %d, enable first %d", linkedFirst, enabledFirst) +} + +// waitForLockWait polls until a backend is blocked on a lock in a statement containing needle. +func waitForLockWait(t *testing.T, pool *pgxpool.Pool, needle string) { + t.Helper() + deadline := time.Now().Add(10 * time.Second) + for time.Now().Before(deadline) { + var n int + err := pool.QueryRow(context.Background(), + `SELECT count(*) FROM pg_stat_activity WHERE wait_event_type = 'Lock' AND query LIKE '%' || $1 || '%' AND pid <> pg_backend_pid()`, needle).Scan(&n) + if err != nil { + t.Fatal(err) + } + if n > 0 { + return + } + time.Sleep(20 * time.Millisecond) + } + t.Fatalf("no backend waited on a lock in a statement containing %q", needle) +} + +// The deterministic halves of the race: the repo row lock is held by one transaction, the link +// waits behind it, and then sees whatever that transaction committed. +func TestLinkWaitsForTheRepoRowLockLiveDB(t *testing.T) { + ctx := context.Background() + cases := []struct { + name string + hold func(t *testing.T, h *liveHarness, tx pgx.Tx, repo uuid.UUID) + wantLink bool + }{ + {"the enable commits first: the link is refused", func(t *testing.T, h *liveHarness, tx pgx.Tx, repo uuid.UUID) { + if _, err := h.q.WithTx(tx).SetRepoEnabledForUser(ctx, store.SetRepoEnabledForUserParams{ID: repo, Enabled: true, UserID: h.user}); err != nil { + t.Fatal(err) + } + }, false}, + {"the holder changes nothing: the link proceeds", func(*testing.T, *liveHarness, pgx.Tx, uuid.UUID) {}, true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + h := newLiveHarness(t) + repo := h.freshRepo() + tx, err := h.pool.Begin(ctx) + if err != nil { + t.Fatal(err) + } + defer tx.Rollback(ctx) //nolint:errcheck // no-op after Commit + if _, err := h.q.WithTx(tx).LockRepoForLinkForUser(ctx, store.LockRepoForLinkForUserParams{ID: repo, UserID: h.user}); err != nil { + t.Fatal(err) + } + result := make(chan error, 1) + go func() { result <- h.link(repo) }() + + waitForLockWait(t, h.pool, "FOR UPDATE OF r") + select { + case err := <-result: + t.Fatalf("the link finished (%v) while the repo row lock was held", err) + default: + } + tc.hold(t, h, tx, repo) + if err := tx.Commit(ctx); err != nil { + t.Fatal(err) + } + err = <-result + if tc.wantLink { + if err != nil { + t.Fatal(err) + } + } else { + _ = wantKind(t, err, KindConflict) + } + if got := h.repoRow(repo).IssueTrackerConnectionID.Valid; got != tc.wantLink { + t.Fatalf("linked = %v, want %v", got, tc.wantLink) + } + }) + } +} + +// A reconnect that changes the account takes the connection row FOR UPDATE; the link's +// transaction holds it FOR SHARE. A link whose tracker check saw the old account is refused when +// the reconnect committed in between, and a reconnect that starts after a link committed is refused. +func TestLinkRacingAReconnectLiveDB(t *testing.T) { + ctx := context.Background() + + t.Run("the reconnect commits between the link's check and its transaction", func(t *testing.T) { + h := newLiveHarness(t) + repo := h.freshRepo() + verified, release := make(chan struct{}), make(chan struct{}) + hook := func() { + select { + case <-verified: + default: + close(verified) + <-release + } + } + h.afterVerify.Store(&hook) + result := make(chan error, 1) + go func() { result <- h.link(repo) }() + <-verified // the link has verified the old account and is paused + + noPause := func() {} + h.afterVerify.Store(&noPause) + if _, err := h.svc.Connect(ctx, h.user, "youtrack", testHost, tokenB); err != nil { + t.Fatalf("reconnect with nothing linked: %v", err) + } + close(release) + e := wantKind(t, <-result, KindConflict) + if !strings.Contains(e.Message, "reconnect") { + t.Errorf("message %q", e.Message) + } + if h.repoRow(repo).IssueTrackerConnectionID.Valid || h.connAccount() != linkOther.ID { + t.Fatal("expected an unlinked repo and the new account") + } + }) + + t.Run("the link commits while the reconnect waits for the connection row", func(t *testing.T) { + h := newLiveHarness(t) + repo := h.freshRepo() + tx, err := h.pool.Begin(ctx) + if err != nil { + t.Fatal(err) + } + defer tx.Rollback(ctx) //nolint:errcheck // no-op after Commit + qtx := h.q.WithTx(tx) + // The link's own steps, held open: the connection FOR SHARE, then the link write. + if _, err := qtx.LockIssueTrackerConnectionForShareForUser(ctx, store.LockIssueTrackerConnectionForShareForUserParams{ID: h.conn, UserID: h.user}); err != nil { + t.Fatal(err) + } + result := make(chan error, 1) + go func() { + _, err := h.svc.Connect(ctx, h.user, "youtrack", testHost, tokenB) + result <- err + }() + waitForLockWait(t, h.pool, "FOR UPDATE") + if _, err := qtx.SetRepoIssueTrackerLink(ctx, store.SetRepoIssueTrackerLinkParams{ + ConnectionID: h.conn, ProjectID: "0-1", ProjectShort: "UZI", TagID: "5-1", TagName: "uzi-ready", RepoID: repo, UserID: h.user, + }); err != nil { + t.Fatal(err) + } + if err := tx.Commit(ctx); err != nil { + t.Fatal(err) + } + _ = wantKind(t, <-result, KindConflict) + if h.connAccount() != linkBot.ID || !h.repoRow(repo).IssueTrackerConnectionID.Valid { + t.Fatal("expected a linked repo and the unchanged account") + } + }) + + // Released together, 25 times: the final state is always one of the consistent ones. A + // reconnect that began after the link's tracker check but committed first refuses the link; a + // link that committed first refuses the reconnect; a reconnect that finished before the link's + // check was read makes the link verify the new account, which is then the stored one. + t.Run("released together", func(t *testing.T) { + h := newLiveHarness(t) + var refusedLink, refusedReconnect, both int + for iter := range 25 { + // Reset to the starting state: nothing linked, the connection stands for the bot. + h.exec(`UPDATE repos SET issue_tracker_connection_id=NULL, issue_tracker_project_id=NULL, issue_tracker_project_short=NULL, + eligibility_tag_id=NULL, eligibility_tag_name=NULL, issue_tracker_linked_at=NULL WHERE issue_tracker_connection_id = $1`, h.conn) + if _, err := h.svc.Connect(ctx, h.user, "youtrack", testHost, tokenA); err != nil { + t.Fatalf("iteration %d: reset: %v", iter, err) + } + repo := h.freshRepo() + var linkErr, connErr error + var ready, done sync.WaitGroup + start := make(chan struct{}) + run := func(fn func()) { + ready.Add(1) + done.Add(1) + go func() { + defer done.Done() + ready.Done() + <-start + fn() + }() + } + run(func() { linkErr = h.link(repo) }) + run(func() { _, connErr = h.svc.Connect(ctx, h.user, "youtrack", testHost, tokenB) }) + ready.Wait() + close(start) + done.Wait() + + linked := h.repoRow(repo).IssueTrackerConnectionID.Valid + acct := h.connAccount() + switch { + case linkErr == nil && connErr != nil: // the link won + _ = wantKind(t, connErr, KindConflict) + if !linked || acct != linkBot.ID { + t.Fatalf("iteration %d: link won but linked=%v account=%s", iter, linked, acct) + } + refusedReconnect++ + case linkErr != nil && connErr == nil: // the reconnect won + _ = wantKind(t, linkErr, KindConflict) + if linked || acct != linkOther.ID { + t.Fatalf("iteration %d: reconnect won but linked=%v account=%s", iter, linked, acct) + } + refusedLink++ + case linkErr == nil && connErr == nil: // the reconnect finished before the link looked + if !linked || acct != linkOther.ID { + t.Fatalf("iteration %d: both succeeded but linked=%v account=%s", iter, linked, acct) + } + both++ + default: + t.Fatalf("iteration %d: both failed: link %v, reconnect %v", iter, linkErr, connErr) + } + } + t.Logf("link won %d, reconnect won %d, reconnect then link %d", refusedReconnect, refusedLink, both) + }) +} + +// The link reads the connection FOR SHARE. A reconnect holds the row FOR UPDATE; the link +// must wait for it and then see the new account. A plain read would not wait and would link +// against the account it verified. +func TestLinkWaitsForAReconnectHoldingTheConnectionRowLiveDB(t *testing.T) { + ctx := context.Background() + h := newLiveHarness(t) + repo := h.freshRepo() + tx, err := h.pool.Begin(ctx) + if err != nil { + t.Fatal(err) + } + defer tx.Rollback(ctx) //nolint:errcheck // no-op after Commit + if _, err := h.q.WithTx(tx).LockIssueTrackerConnectionByIdentityForUpdate(ctx, store.LockIssueTrackerConnectionByIdentityForUpdateParams{ + UserID: h.user, Kind: "youtrack", BaseUrl: testHost, + }); err != nil { + t.Fatal(err) + } + result := make(chan error, 1) + go func() { result <- h.link(repo) }() + + waitForLockWait(t, h.pool, "FOR SHARE") + select { + case err := <-result: + t.Fatalf("the link finished (%v) while the connection row was locked FOR UPDATE", err) + default: + } + if _, err := tx.Exec(ctx, `UPDATE issue_tracker_connections SET account_id = $2 WHERE id = $1`, h.conn, linkOther.ID); err != nil { + t.Fatal(err) + } + if err := tx.Commit(ctx); err != nil { + t.Fatal(err) + } + e := wantKind(t, <-result, KindConflict) + if !strings.Contains(e.Message, "reconnect") { + t.Errorf("message %q", e.Message) + } + if h.repoRow(repo).IssueTrackerConnectionID.Valid { + t.Fatal("the repo was linked") + } +} + +// blindQuerier hides the locked pre-check from Connect: the identity lock finds no row and the +// link count is zero, as when a link commits between those reads and the upsert. +type blindQuerier struct{ *store.Queries } + +func (blindQuerier) LockIssueTrackerConnectionByIdentityForUpdate(context.Context, store.LockIssueTrackerConnectionByIdentityForUpdateParams) (store.IssueTrackerConnection, error) { + return store.IssueTrackerConnection{}, pgx.ErrNoRows +} + +// The upsert's WHERE is a last defence for the reconnect rule: with the locked pre-check +// blinded and no transactions wired, it still refuses when the link was committed before the +// statement began. (It cannot see a link that commits during its own wait; see +// TestFirstConnectRacingAnotherConnectAndALinkLiveDB.) +func TestUpsertWhereRefusesAnAccountChangeOnALinkedConnectionLiveDB(t *testing.T) { + ctx := context.Background() + h := newLiveHarness(t) + bare := New(blindQuerier{h.q}, h.svc.box, h.svc.allowed, h.svc.newDriver) + bare.log = h.svc.log + if err := h.link(h.freshRepo()); err != nil { + t.Fatal(err) + } + _, err := bare.Connect(ctx, h.user, "youtrack", testHost, tokenB) + if e := wantKind(t, err, KindConflict); !strings.Contains(e.Message, "different account") { + t.Errorf("message %q", e.Message) + } + if h.connAccount() != linkBot.ID { + t.Fatal("the account changed") + } + if _, err := bare.Connect(ctx, h.user, "youtrack", testHost, testToken("rotated")); err != nil { + t.Fatalf("same-account rotation: %v", err) + } +} + +// gateQuerier pauses the first identity lock after it has found no row, so a test can +// interleave other connects and a link between that read and the statement that follows. +type gateQuerier struct { + *store.Queries + once *sync.Once + locked chan struct{} + release chan struct{} +} + +func (g gateQuerier) LockIssueTrackerConnectionByIdentityForUpdate(ctx context.Context, a store.LockIssueTrackerConnectionByIdentityForUpdateParams) (store.IssueTrackerConnection, error) { + row, err := g.Queries.LockIssueTrackerConnectionByIdentityForUpdate(ctx, a) + g.once.Do(func() { + close(g.locked) + <-g.release + }) + return row, err +} + +// T1 is a first connect (account A) whose identity lock found no row. T2, another connect of +// the same identity (account B), inserts and commits. T3 links a repo to that row and has not +// committed yet. T1 must not overwrite the row's account once T3 commits: with a plain upsert +// its NOT EXISTS subquery reads the snapshot from before it waited on T3 and does not see the +// link. The fix inserts with DO NOTHING and re-enters the locked path (lock, then a fresh count). +func TestFirstConnectRacingAnotherConnectAndALinkLiveDB(t *testing.T) { + ctx := context.Background() + h := newLiveHarness(t) + + // A second user with no connection yet, so T1's identity lock finds no row. + user := uuid.New() + h.exec(`INSERT INTO users (id, email, password_hash, is_admin, vault_mode) VALUES ($1, $2, 'x', false, 'unprotected')`, + user, fmt.Sprintf("itsvc-%s@e2e", user)) + t.Cleanup(func() { _, _ = h.pool.Exec(context.Background(), `DELETE FROM users WHERE id = $1`, user) }) + forgeConn := uuid.New() + h.exec(`INSERT INTO forge_connections (id, user_id, forge_type, base_url, bot_username, bot_forge_user_id, token_ciphertext) + VALUES ($1, $2, 'gitlab', $3, 'bot', 1, $4)`, forgeConn, user, "https://"+forgeConn.String()+".e2e", []byte{0x1}) + repo, err := h.q.UpsertRepo(ctx, store.UpsertRepoParams{ConnectionID: forgeConn, ForgeProjectID: 4242, PathWithNamespace: "g/r", WebUrl: "https://forge.e2e/g/r"}) + if err != nil { + t.Fatal(err) + } + + g := gateQuerier{Queries: h.q, once: new(sync.Once), locked: make(chan struct{}), release: make(chan struct{})} + t1 := New(g, h.svc.box, h.svc.allowed, h.svc.newDriver). + UseTransactions(h.pool, func(tx pgx.Tx) Querier { + return gateQuerier{Queries: h.q.WithTx(tx), once: g.once, locked: g.locked, release: g.release} + }) + t1.log = h.svc.log + result := make(chan error, 1) + go func() { + _, err := t1.Connect(ctx, user, "youtrack", testHost, tokenA) // account A (the bot) + result <- err + }() + <-g.locked // T1 looked, found no row, and is paused + + c2, err := h.svc.Connect(ctx, user, "youtrack", testHost, tokenB) // T2: account B, committed + if err != nil { + t.Fatal(err) + } + tx, err := h.pool.Begin(ctx) // T3: the link, held open + if err != nil { + t.Fatal(err) + } + defer tx.Rollback(ctx) //nolint:errcheck // no-op after Commit + qtx := h.q.WithTx(tx) + if _, err := qtx.LockIssueTrackerConnectionForShareForUser(ctx, store.LockIssueTrackerConnectionForShareForUserParams{ID: c2.ID, UserID: user}); err != nil { + t.Fatal(err) + } + if _, err := qtx.SetRepoIssueTrackerLink(ctx, store.SetRepoIssueTrackerLinkParams{ + ConnectionID: c2.ID, ProjectID: "0-1", ProjectShort: "UZI", TagID: "5-1", TagName: "uzi-ready", RepoID: repo.ID, UserID: user, + }); err != nil { + t.Fatal(err) + } + + close(g.release) // T1 goes on and must now wait on T3's row lock + // Whichever statement T1 runs next (the lock, or an upsert) waits on the connection row. + waitForLockWait(t, h.pool, "issue_tracker_connections") + if err := tx.Commit(ctx); err != nil { + t.Fatal(err) + } + e := wantKind(t, <-result, KindConflict) + if !strings.Contains(e.Message, "different account") { + t.Errorf("message %q", e.Message) + } + row, err := h.q.GetIssueTrackerConnectionForUser(ctx, store.GetIssueTrackerConnectionForUserParams{ID: c2.ID, UserID: user}) + if err != nil { + t.Fatal(err) + } + if row.AccountID != linkOther.ID { + t.Fatalf("the linked connection's account changed to %q", row.AccountID) + } +} diff --git a/api/internal/issuetrackersvc/link_test.go b/api/internal/issuetrackersvc/link_test.go new file mode 100644 index 0000000000..3d1f63548d --- /dev/null +++ b/api/internal/issuetrackersvc/link_test.go @@ -0,0 +1,312 @@ +package issuetrackersvc + +import ( + "context" + "strings" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgconn" + + "github.com/vtmocanu/uzi/api/internal/issuetracker" +) + +var ( + linkBot = issuetracker.Account{ID: "1-1", Login: "bot"} + linkOther = issuetracker.Account{ID: "1-2", Login: "maintainer"} + linkAll = issuetracker.Group{ID: "675-0", Name: "All Users", AllUsers: true} +) + +// linkHarness is a harness whose connection is already stored and whose +// tracker lists one project and a set of tags. +type linkHarness struct { + *harness + user, conn uuid.UUID +} + +func newLinkHarness(t *testing.T) *linkHarness { + t.Helper() + h := newHarness(t) + h.template.projects = []issuetracker.Project{{ID: "0-1", ShortName: "UZI", Name: "Uzi"}} + h.template.tags = []issuetracker.Tag{ + {ID: "5-1", Name: "uzi-ready", Owner: linkBot, Use: issuetracker.SharingSettings{Groups: []issuetracker.Group{linkAll}}}, + } + user := uuid.New() + c, err := h.svc.Connect(context.Background(), user, "youtrack", testHost, testToken("link")) + if err != nil { + t.Fatal(err) + } + return &linkHarness{harness: h, user: user, conn: c.ID} +} + +func (h *linkHarness) link(repo uuid.UUID, project, tag string) (Link, error) { + return h.svc.LinkRepo(context.Background(), h.user, repo, h.conn, project, tag) +} + +func TestLinkRepoStoresTheListedValues(t *testing.T) { + h := newLinkHarness(t) + repo := h.q.addRepo(h.user, true) + l, err := h.link(repo, "0-1", "5-1") + if err != nil { + t.Fatal(err) + } + if l.RepoID != repo || l.ConnectionID != h.conn || l.ProjectID != "0-1" || l.ProjectShort != "UZI" || l.TagID != "5-1" || l.TagName != "uzi-ready" { + t.Fatalf("link = %+v", l) + } + if r := h.q.repo(repo); r.linkedConn != h.conn || r.projectShort != "UZI" || r.tagName != "uzi-ready" { + t.Fatalf("stored = %+v", r) + } +} + +func TestLinkRepoRefusesAnUnusableTag(t *testing.T) { + cases := []struct { + name string + tag issuetracker.Tag + wantMsg string + }{ + {"account cannot apply: owned by someone else, not shared with the account", + issuetracker.Tag{ID: "5-2", Name: "theirs", Owner: linkOther}, "cannot apply this tag"}, + {"account cannot apply: only a one-member group is named", + issuetracker.Tag{ID: "5-2", Name: "theirs", Owner: linkOther, + Use: issuetracker.SharingSettings{Groups: []issuetracker.Group{{ID: "675-2", Name: "Solo", UsersCount: 1}}}}, "cannot apply this tag"}, + {"nobody else can apply: owned by the account and not shared", + issuetracker.Tag{ID: "5-2", Name: "mine", Owner: linkBot}, "named users"}, + {"nobody else can apply: shared only with the account itself", + issuetracker.Tag{ID: "5-2", Name: "mine", Owner: linkBot, + Use: issuetracker.SharingSettings{Users: []issuetracker.Account{linkBot}}}, "named users"}, + {"nobody else can apply: a one-member group may be the account", + issuetracker.Tag{ID: "5-2", Name: "mine", Owner: linkBot, + Use: issuetracker.SharingSettings{Groups: []issuetracker.Group{{ID: "675-2", Name: "Solo", UsersCount: 1}}}}, "named users"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + h := newLinkHarness(t) + h.template.tags = append(h.template.tags, tc.tag) + repo := h.q.addRepo(h.user, true) + _, err := h.link(repo, "0-1", tc.tag.ID) + e := wantKind(t, err, KindConflict) + if !strings.Contains(e.Message, tc.wantMsg) { + t.Errorf("message %q lacks %q", e.Message, tc.wantMsg) + } + if h.q.repo(repo).linkedConn != uuid.Nil { + t.Error("the repo was linked") + } + }) + } +} + +// The refusal says what to change in the tracker and never claims that the tag's +// sharing shows who can edit issues in the project (PRD #4173 D14). +func TestLinkRepoMessagesDoNotClaimEditAccess(t *testing.T) { + h := newLinkHarness(t) + h.template.tags = append(h.template.tags, issuetracker.Tag{ID: "5-2", Name: "mine", Owner: linkBot}) + _, err := h.link(h.q.addRepo(h.user, true), "0-1", "5-2") + msg := wantKind(t, err, KindConflict).Message + if !strings.Contains(msg, "does not show who can edit issues in the project") { + t.Errorf("message %q does not disclaim edit access", msg) + } + for _, claim := range []string{"can edit issues in the project.", "proves"} { + if strings.Contains(msg, claim) { + t.Errorf("message %q claims %q", msg, claim) + } + } +} + +func TestLinkRepoRefusesWhenTheTokenNowStandsForAnotherAccount(t *testing.T) { + h := newLinkHarness(t) + h.template.account = linkOther // the stored connection is for linkBot + repo := h.q.addRepo(h.user, true) + e := wantKind(t, errOf(h.link(repo, "0-1", "5-1")), KindConflict) + if !strings.Contains(e.Message, "reconnect") { + t.Errorf("message %q does not say to reconnect", e.Message) + } + if h.q.repo(repo).linkedConn != uuid.Nil { + t.Error("the repo was linked") + } +} + +func TestLinkRepoRefusesUnknownProjectAndTag(t *testing.T) { + for name, ids := range map[string][2]string{ + "unknown project": {"0-9", "5-1"}, + "unknown tag": {"0-1", "5-9"}, + "malformed id": {"not an id", "5-1"}, + } { + t.Run(name, func(t *testing.T) { + h := newLinkHarness(t) + repo := h.q.addRepo(h.user, true) + _ = wantKind(t, errOf(h.link(repo, ids[0], ids[1])), KindBadRequest) + if h.q.repo(repo).linkedConn != uuid.Nil { + t.Error("the repo was linked") + } + }) + } +} + +func TestLinkRepoRefusesNamesTheStoreCannotHold(t *testing.T) { + cases := map[string]func(*linkHarness){ + "short name with a dash": func(h *linkHarness) { h.template.projects[0].ShortName = "A-B" }, + "short name too long": func(h *linkHarness) { h.template.projects[0].ShortName = "A" + strings.Repeat("b", 44) }, + "tag name with a brace": func(h *linkHarness) { h.template.tags[0].Name = "uzi{ready" }, + "tag name with a newline": func(h *linkHarness) { h.template.tags[0].Name = "uzi\nready" }, + } + for name, mutate := range cases { + t.Run(name, func(t *testing.T) { + h := newLinkHarness(t) + mutate(h) + _ = wantKind(t, errOf(h.link(h.q.addRepo(h.user, true), "0-1", "5-1")), KindBadRequest) + }) + } +} + +func TestLinkRepoRefusesARepoThatIsNotAwaitingOrAlreadyLinked(t *testing.T) { + h := newLinkHarness(t) + enabled := h.q.addRepo(h.user, false) + built := len(h.built) + _ = wantKind(t, errOf(h.link(enabled, "0-1", "5-1")), KindConflict) + if len(h.built) != built { + t.Error("the tracker was contacted for a repo that cannot be linked") + } + + fresh := h.q.addRepo(h.user, true) + if _, err := h.link(fresh, "0-1", "5-1"); err != nil { + t.Fatal(err) + } + e := wantKind(t, errOf(h.link(fresh, "0-1", "5-1")), KindConflict) + if !strings.Contains(e.Message, "already linked") { + t.Errorf("relink message %q", e.Message) + } +} + +func TestLinkRepoIsOwnerScoped(t *testing.T) { + h := newLinkHarness(t) + foreignRepo := h.q.addRepo(uuid.New(), true) + _ = wantKind(t, errOf(h.link(foreignRepo, "0-1", "5-1")), KindNotFound) + + repo := h.q.addRepo(h.user, true) + _, err := h.svc.LinkRepo(context.Background(), uuid.New(), repo, h.conn, "0-1", "5-1") + _ = wantKind(t, err, KindNotFound) + _, err = h.svc.LinkRepo(context.Background(), h.user, repo, uuid.New(), "0-1", "5-1") + _ = wantKind(t, err, KindNotFound) + if h.q.repo(repo).linkedConn != uuid.Nil { + t.Error("the repo was linked") + } +} + +func TestLinkRepoRefusesARepoThatStoresIssueNumbers(t *testing.T) { + h := newLinkHarness(t) + repo := h.q.addRepo(h.user, true) + h.q.repos[repo].storesNumbers = true + _ = wantKind(t, errOf(h.link(repo, "0-1", "5-1")), KindConflict) + if h.q.repo(repo).linkedConn != uuid.Nil { + t.Error("the repo was linked") + } +} + +func TestLinkRepoTrackerFailuresAreBadGateway(t *testing.T) { + cases := map[string]func(*linkHarness){ + "verify": func(h *linkHarness) { h.template.verifyErr = context.DeadlineExceeded }, + "projects": func(h *linkHarness) { h.template.projectErr = context.DeadlineExceeded }, + "tags": func(h *linkHarness) { h.template.tagErr = context.DeadlineExceeded }, + } + for name, mutate := range cases { + t.Run(name, func(t *testing.T) { + h := newLinkHarness(t) + mutate(h) + _ = wantKind(t, errOf(h.link(h.q.addRepo(h.user, true), "0-1", "5-1")), KindBadGateway) + }) + } +} + +func TestReconnectRuleWhenARepoIsLinked(t *testing.T) { + ctx := context.Background() + h := newLinkHarness(t) + repo := h.q.addRepo(h.user, true) + if _, err := h.link(repo, "0-1", "5-1"); err != nil { + t.Fatal(err) + } + reconnect := func() (Connection, error) { + return h.svc.Connect(ctx, h.user, "youtrack", testHost, testToken("rotated")) + } + + h.template.account = linkOther + e := wantKind(t, errOf(reconnect()), KindConflict) + if !strings.Contains(e.Message, "different account") { + t.Errorf("message %q", e.Message) + } + if got := h.q.rows[0]; got.AccountID != linkBot.ID { + t.Fatalf("the refused reconnect changed the stored account to %q", got.AccountID) + } + + // Token rotation for the same account stays allowed, even with a new login. + h.template.account = issuetracker.Account{ID: linkBot.ID, Login: "bot-renamed"} + c, err := reconnect() + if err != nil { + t.Fatal(err) + } + if c.ID != h.conn || c.AccountLogin != "bot-renamed" { + t.Fatalf("rotated connection = %+v", c) + } +} + +func TestReconnectWithAnotherAccountIsAllowedWhenNothingIsLinked(t *testing.T) { + h := newLinkHarness(t) + h.template.account = linkOther + c, err := h.svc.Connect(context.Background(), h.user, "youtrack", testHost, testToken("rotated")) + if err != nil { + t.Fatal(err) + } + if c.ID != h.conn || c.AccountID != linkOther.ID { + t.Fatalf("connection = %+v", c) + } +} + +func TestDeleteRefusedWhileARepoIsLinked(t *testing.T) { + ctx := context.Background() + h := newLinkHarness(t) + repo := h.q.addRepo(h.user, true) + if _, err := h.link(repo, "0-1", "5-1"); err != nil { + t.Fatal(err) + } + _ = wantKind(t, h.svc.Delete(ctx, h.user, h.conn), KindConflict) + if len(h.q.rows) != 1 { + t.Fatal("the connection was deleted") + } + // A foreign id stays NotFound and never reveals the link count. + _ = wantKind(t, h.svc.Delete(ctx, uuid.New(), h.conn), KindNotFound) + + // A repo linked between the count and the delete: the foreign key says no. + other := newLinkHarness(t) + other.q.deleteErr = &pgconn.PgError{Code: "23503"} + _ = wantKind(t, other.svc.Delete(ctx, other.user, other.conn), KindConflict) + + // Unlinked, it is deletable. + free := newLinkHarness(t) + if err := free.svc.Delete(ctx, free.user, free.conn); err != nil { + t.Fatal(err) + } +} + +func TestListTagsWithVerdicts(t *testing.T) { + h := newLinkHarness(t) + h.template.tags = append(h.template.tags, + issuetracker.Tag{ID: "5-2", Name: "mine", Owner: linkBot}, + issuetracker.Tag{ID: "5-3", Name: "theirs", Owner: linkOther}) + views, err := h.svc.ListTagsWithVerdicts(context.Background(), h.user, h.conn) + if err != nil { + t.Fatal(err) + } + want := map[string][2]bool{"uzi-ready": {true, true}, "mine": {true, false}, "theirs": {false, true}} + if len(views) != len(want) { + t.Fatalf("got %d views", len(views)) + } + for _, v := range views { + if w := want[v.Tag.Name]; v.AccountCanApply != w[0] || v.OthersCanApply != w[1] { + t.Errorf("%s: verdicts = %v/%v, want %v", v.Tag.Name, v.AccountCanApply, v.OthersCanApply, w) + } + } + _, err = h.svc.ListTagsWithVerdicts(context.Background(), uuid.New(), h.conn) + _ = wantKind(t, err, KindNotFound) +} + +// errOf drops a value and keeps the error, so a refusal reads as one expression. +func errOf[T any](_ T, err error) error { return err } diff --git a/api/internal/issuetrackersvc/link_youtrack_test.go b/api/internal/issuetrackersvc/link_youtrack_test.go new file mode 100644 index 0000000000..6001db5953 --- /dev/null +++ b/api/internal/issuetrackersvc/link_youtrack_test.go @@ -0,0 +1,123 @@ +package issuetrackersvc + +import ( + "context" + "net/http" + "slices" + "strings" + "testing" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/issuetracker" + "github.com/vtmocanu/uzi/api/internal/issuetracker/youtracktest" + "github.com/vtmocanu/uzi/api/internal/secretbox" +) + +// ytHarness runs the Service over the real YouTrack driver against the +// youtracktest fake, with the fake's certificate trusted for the test only. +type ytHarness struct { + svc *Service + q *fakeQuerier + srv *youtracktest.Server + user, conn uuid.UUID +} + +func newYTHarness(t *testing.T) *ytHarness { + t.Helper() + srv := youtracktest.NewServer(t) + // The driver builds its transport from a clone of http.DefaultTransport and + // takes no TLS option by design, so trust the fake there and restore after. + def, ok := http.DefaultTransport.(*http.Transport) + if !ok { + t.Fatal("http.DefaultTransport is not an *http.Transport") + } + saved := def.TLSClientConfig + def.TLSClientConfig = srv.Client().Transport.(*http.Transport).TLSClientConfig.Clone() + t.Cleanup(func() { def.TLSClientConfig = saved }) + + key := make([]byte, secretbox.KeySize) + box, err := secretbox.New(key) + if err != nil { + t.Fatal(err) + } + q := &fakeQuerier{} + svc := New(q, box, func(u string) bool { return u == srv.URL }, issuetracker.New) + user := uuid.New() + c, err := svc.Connect(context.Background(), user, "youtrack", srv.URL, srv.Token) + if err != nil { + t.Fatal(err) + } + return &ytHarness{svc: svc, q: q, srv: srv, user: user, conn: c.ID} +} + +func (h *ytHarness) link(repo uuid.UUID, tag string) error { + _, err := h.svc.LinkRepo(context.Background(), h.user, repo, h.conn, youtracktest.ProjectID, tag) + return err +} + +// A successful link check only reads: every request is a GET and no issue +// gains or loses a tag (PRD #4173 M4: the link writes nothing to the tracker). +func TestLinkRepoOverYouTrackSendsOnlyReads(t *testing.T) { + h := newYTHarness(t) + issue := h.srv.SeedIssue("a card", "body", youtracktest.EligibilityTagID) + before := h.srv.IssueTagIDs(issue) + tagsBefore := len(h.srv.TagsNamed(youtracktest.EligibilityTagName)) + repo := h.q.addRepo(h.user, true) + + h.srv.ResetRequests() + l, err := h.svc.LinkRepo(context.Background(), h.user, repo, h.conn, youtracktest.ProjectID, youtracktest.EligibilityTagID) + if err != nil { + t.Fatal(err) + } + if l.ProjectShort != youtracktest.ProjectShortName || l.TagName != youtracktest.EligibilityTagName { + t.Fatalf("link = %+v", l) + } + reqs := h.srv.Requests() + if len(reqs) == 0 { + t.Fatal("the link check made no request to the tracker") + } + for _, r := range reqs { + if r.Method != http.MethodGet { + t.Errorf("request %s %s is not a read", r.Method, r.Path) + } + } + if got := h.srv.IssueTagIDs(issue); !slices.Equal(got, before) { + t.Errorf("issue tags changed from %v to %v", before, got) + } + if n := len(h.srv.TagsNamed(youtracktest.EligibilityTagName)); n != tagsBefore { + t.Errorf("tags named %q went from %d to %d", youtracktest.EligibilityTagName, tagsBefore, n) + } +} + +func TestLinkRepoOverYouTrackRefusesByTheTagsRealSharing(t *testing.T) { + h := newYTHarness(t) + solo := h.srv.SeedTag("solo", youtracktest.OtherLogin, + youtracktest.Sharing{Groups: []string{youtracktest.AllUsersGroupID}}, + youtracktest.Sharing{Groups: []string{youtracktest.SoloGroupID}}) + hidden := h.srv.SeedTag("hidden", youtracktest.OtherLogin, youtracktest.Sharing{}, youtracktest.Sharing{}) + cases := []struct { + name string + tag string + kind Kind + msg string + }{ + {"bot owns it, shared with nobody", youtracktest.SameNameTagID, KindConflict, "named users"}, + {"other owns it, use group lacks the bot", solo, KindConflict, "cannot apply"}, + {"the bot cannot see it", hidden, KindBadRequest, "not found"}, + {"no such tag", "5-123456", KindBadRequest, "not found"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + repo := h.q.addRepo(h.user, true) + e := wantKind(t, h.link(repo, tc.tag), tc.kind) + if !strings.Contains(e.Message, tc.msg) { + t.Errorf("message %q lacks %q", e.Message, tc.msg) + } + if h.q.repo(repo).linkedConn != uuid.Nil { + t.Error("the repo was linked") + } + }) + } + _ = wantKind(t, errOf(h.svc.LinkRepo(context.Background(), h.user, h.q.addRepo(h.user, true), h.conn, "0-999", youtracktest.EligibilityTagID)), KindBadRequest) +} diff --git a/api/internal/issuetrackersvc/read.go b/api/internal/issuetrackersvc/read.go index 649988afc2..1df3842ea1 100644 --- a/api/internal/issuetrackersvc/read.go +++ b/api/internal/issuetrackersvc/read.go @@ -34,8 +34,28 @@ func (s *Service) Get(ctx context.Context, userID, connID uuid.UUID) (Connection } // Delete removes one of the user's connections; a missing or foreign id is NotFound. +// A connection that a repo links to is a Conflict (checked first, and again by the +// foreign key when a link lands in between): a link cannot be removed. func (s *Service) Delete(ctx context.Context, userID, connID uuid.UUID) error { + // The owner-scoped read first, so a foreign id is NotFound and the link count + // below can never reveal that another user's connection exists. + if _, err := s.getRow(ctx, userID, connID); err != nil { + return err + } + linked, err := s.q.CountReposLinkedToIssueTrackerConnection(ctx, connID) + if err != nil { + s.log.Error("count repos linked to issue tracker connection", "error", err) + return internal() + } + if linked > 0 { + return errConnectionLinked + } n, err := s.q.DeleteIssueTrackerConnectionForUser(ctx, store.DeleteIssueTrackerConnectionForUserParams{ID: connID, UserID: userID}) + if isForeignKeyViolation(err) { + // A repo linked between the count and the delete: the repos foreign key + // refuses the delete, which is the same answer. + return errConnectionLinked + } if err != nil { s.log.Error("delete issue tracker connection", "error", err) return internal() diff --git a/api/internal/issuetrackersvc/service.go b/api/internal/issuetrackersvc/service.go index 98401ee21c..ed7d6a692e 100644 --- a/api/internal/issuetrackersvc/service.go +++ b/api/internal/issuetrackersvc/service.go @@ -5,6 +5,7 @@ import ( "log/slog" "github.com/google/uuid" + "github.com/jackc/pgx/v5" "github.com/vtmocanu/uzi/api/internal/issuetracker" "github.com/vtmocanu/uzi/api/internal/secretbox" @@ -14,9 +15,17 @@ import ( // Querier is the slice of *store.Queries the service uses. type Querier interface { UpsertIssueTrackerConnection(ctx context.Context, arg store.UpsertIssueTrackerConnectionParams) (store.IssueTrackerConnection, error) + InsertIssueTrackerConnectionIfAbsent(ctx context.Context, arg store.InsertIssueTrackerConnectionIfAbsentParams) (store.IssueTrackerConnection, error) ListIssueTrackerConnectionsByUser(ctx context.Context, userID uuid.UUID) ([]store.IssueTrackerConnection, error) GetIssueTrackerConnectionForUser(ctx context.Context, arg store.GetIssueTrackerConnectionForUserParams) (store.IssueTrackerConnection, error) DeleteIssueTrackerConnectionForUser(ctx context.Context, arg store.DeleteIssueTrackerConnectionForUserParams) (int64, error) + LockIssueTrackerConnectionByIdentityForUpdate(ctx context.Context, arg store.LockIssueTrackerConnectionByIdentityForUpdateParams) (store.IssueTrackerConnection, error) + LockIssueTrackerConnectionForShareForUser(ctx context.Context, arg store.LockIssueTrackerConnectionForShareForUserParams) (store.IssueTrackerConnection, error) + CountReposLinkedToIssueTrackerConnection(ctx context.Context, connectionID uuid.UUID) (int64, error) + GetRepoIssueSourceForUser(ctx context.Context, arg store.GetRepoIssueSourceForUserParams) (store.GetRepoIssueSourceForUserRow, error) + LockRepoForLinkForUser(ctx context.Context, arg store.LockRepoForLinkForUserParams) (store.LockRepoForLinkForUserRow, error) + RepoStoresIssueNumber(ctx context.Context, repoID uuid.UUID) (bool, error) + SetRepoIssueTrackerLink(ctx context.Context, arg store.SetRepoIssueTrackerLinkParams) (store.Repo, error) } var _ Querier = (*store.Queries)(nil) @@ -32,6 +41,9 @@ type Service struct { allowed func(string) bool newDriver NewDriverFunc log *slog.Logger + // begin and bind run a unit of work in one transaction (see UseTransactions). + begin TxBeginner + bind func(pgx.Tx) Querier } // New builds a Service. allowed reports whether a normalized base URL is on the diff --git a/api/internal/issuetrackersvc/service_test.go b/api/internal/issuetrackersvc/service_test.go index dd3e8c9d98..10980cb0e8 100644 --- a/api/internal/issuetrackersvc/service_test.go +++ b/api/internal/issuetrackersvc/service_test.go @@ -25,8 +25,11 @@ const testHost = "https://issues.example.com" // fakeQuerier is an in-memory Querier keyed like the real table. type fakeQuerier struct { - mu sync.Mutex - rows []store.IssueTrackerConnection + mu sync.Mutex + rows []store.IssueTrackerConnection + repos map[uuid.UUID]*fakeRepo // link-side state; see link_fake_test.go + // deleteErr, when set, is what DeleteIssueTrackerConnectionForUser answers. + deleteErr error } func (f *fakeQuerier) UpsertIssueTrackerConnection(_ context.Context, a store.UpsertIssueTrackerConnectionParams) (store.IssueTrackerConnection, error) { @@ -75,6 +78,9 @@ func (f *fakeQuerier) GetIssueTrackerConnectionForUser(_ context.Context, a stor func (f *fakeQuerier) DeleteIssueTrackerConnectionForUser(_ context.Context, a store.DeleteIssueTrackerConnectionForUserParams) (int64, error) { f.mu.Lock() defer f.mu.Unlock() + if f.deleteErr != nil { + return 0, f.deleteErr + } for i, r := range f.rows { if r.ID == a.ID && r.UserID == a.UserID { f.rows = append(f.rows[:i], f.rows[i+1:]...) diff --git a/api/internal/issuetrackersvc/tx.go b/api/internal/issuetrackersvc/tx.go new file mode 100644 index 0000000000..f15fbf461d --- /dev/null +++ b/api/internal/issuetrackersvc/tx.go @@ -0,0 +1,71 @@ +package issuetrackersvc + +import ( + "context" + "errors" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +// Postgres SQLSTATEs the service maps. UZ413 is the issue-number guard's own +// code (migration 00425). +const ( + sqlStateForeignKeyViolation = "23503" + sqlStateIssueNumberGuard = "UZ413" +) + +// TxBeginner opens a transaction; *pgxpool.Pool satisfies it. +type TxBeginner interface { + Begin(ctx context.Context) (pgx.Tx, error) +} + +// UseTransactions makes the link and reconnect rules atomic: begin opens a +// transaction and bind returns the Querier whose statements run in it +// (store.Queries.WithTx in production). A Service without it runs each unit of +// work directly on its Querier, which is atomic only if that Querier is, so +// production wiring (cmd/server) always sets it. It returns s for chaining and +// must be called before the Service is shared. +func (s *Service) UseTransactions(begin TxBeginner, bind func(pgx.Tx) Querier) *Service { + s.begin, s.bind = begin, bind + return s +} + +// inTx runs fn in one transaction (READ COMMITTED, the pool default), rolling +// back when fn fails and committing otherwise. +func (s *Service) inTx(ctx context.Context, fn func(Querier) error) error { + if s.begin == nil || s.bind == nil { + return fn(s.q) + } + tx, err := s.begin.Begin(ctx) + if err != nil { + return err + } + defer tx.Rollback(ctx) //nolint:errcheck // a no-op after a successful Commit + if err := fn(s.bind(tx)); err != nil { + return err + } + return tx.Commit(ctx) +} + +// txError turns an inTx failure into the typed error a caller returns: a typed +// *Error passes through, the issue-number guard's UZ413 and a foreign key +// violation are conflicts, and anything else is logged and becomes Internal. +func (s *Service) txError(what string, err error) *Error { + if te := (*Error)(nil); errors.As(err, &te) { + return te + } + var pe *pgconn.PgError + if errors.As(err, &pe) && (pe.Code == sqlStateIssueNumberGuard || pe.Code == sqlStateForeignKeyViolation) { + return conflict("the repo or connection changed while this was being done; retry") + } + s.log.Error(what, "error", err) + return internal() +} + +func isForeignKeyViolation(err error) bool { + var pe *pgconn.PgError + return errors.As(err, &pe) && pe.Code == sqlStateForeignKeyViolation +} + +var errConnectionLinked = conflict("this connection is linked to a repo and cannot be deleted while it is linked; a link cannot be removed") diff --git a/api/internal/poller/linked_repo_livedb_test.go b/api/internal/poller/linked_repo_livedb_test.go new file mode 100644 index 0000000000..d2ef6b5511 --- /dev/null +++ b/api/internal/poller/linked_repo_livedb_test.go @@ -0,0 +1,245 @@ +package poller + +import ( + "bytes" + "context" + "errors" + "fmt" + "log/slog" + "net/http" + "os" + "strings" + "testing" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgxpool" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/forgesvc" + "github.com/vtmocanu/uzi/api/internal/issuetracker" + "github.com/vtmocanu/uzi/api/internal/issuetracker/youtracktest" + "github.com/vtmocanu/uzi/api/internal/issuetrackersvc" + "github.com/vtmocanu/uzi/api/internal/secretbox" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// PRD #4173 M4 through the poller: a repo linked to an issue tracker still runs every +// code-host step when the issue side is unavailable, never runs autopilot, and records +// an issue run's MR state through the board-free lane with no cached card. + +// trustFake lets the real YouTrack driver (which takes no TLS option by design) trust +// the youtracktest certificate for this test only. +func trustFake(t *testing.T, srv *youtracktest.Server) { + t.Helper() + def, ok := http.DefaultTransport.(*http.Transport) + if !ok { + t.Fatal("http.DefaultTransport is not an *http.Transport") + } + saved := def.TLSClientConfig + def.TLSClientConfig = srv.Client().Transport.(*http.Transport).TLSClientConfig.Clone() + t.Cleanup(func() { def.TLSClientConfig = saved }) +} + +type refusingDrivers struct{ calls int } + +func (r *refusingDrivers) DriverFor(context.Context, uuid.UUID, uuid.UUID, issuetracker.Binding, string, string, string) (issuetracker.Driver, error) { + r.calls++ + return nil, errors.New("base URL is not in the allowed issue tracker list") +} + +type oneRepoStore struct { + q *store.Queries + repo uuid.UUID +} + +func (r oneRepoStore) ListEnabledReposWithConnections(ctx context.Context) ([]store.ListEnabledReposWithConnectionsRow, error) { + rows, err := r.q.ListEnabledReposWithConnections(ctx) + var out []store.ListEnabledReposWithConnectionsRow + for _, row := range rows { + if row.ID == r.repo { + out = append(out, row) + } + } + return out, err +} + +type linkedEnv struct { + ctx context.Context + pool *pgxpool.Pool + q *store.Queries + svc *forgesvc.Service + rec *forgetest.Recorder + engine *Engine + starter *apRuns + user uuid.UUID + repo uuid.UUID + run7 uuid.UUID +} + +// newLinkedEnv seeds one enabled repo linked to a real issuetrackersvc connection to +// srv, with an autopilot-labelled card cached (issue 5) and a completed issue run +// (issue 7, MR 13 recorded opened, forge reports it closed) with NO cached card. +func newLinkedEnv(t *testing.T, srv *youtracktest.Server, drivers forgesvc.IssueTrackerDrivers) *linkedEnv { + t.Helper() + dsn := os.Getenv("UZI_TEST_DATABASE_URL") + if dsn == "" { + t.Skip("UZI_TEST_DATABASE_URL unset; run e2e/run-store-it.sh") + } + ctx := context.Background() + if err := store.Migrate(ctx, dsn); err != nil { + t.Fatal(err) + } + pool, err := store.OpenPool(ctx, dsn) + if err != nil { + t.Fatal(err) + } + t.Cleanup(pool.Close) + q := store.New(pool) + box, err := secretbox.New([]byte("0123456789abcdef0123456789abcdef")) + if err != nil { + t.Fatal(err) + } + rec := forgetest.NewRecorder(&recordedPollForge{}) + svc := forgesvc.NewWithForgeBuilder(q, box, time.Second, nil, func(forge.Type, string, string, time.Duration) (forge.Forge, error) { + return rec, nil + }) + sealed, err := svc.EncryptToken("fixture") + if err != nil { + t.Fatal(err) + } + env := &linkedEnv{ctx: ctx, pool: pool, q: q, svc: svc, rec: rec, user: uuid.New(), repo: uuid.New(), run7: uuid.New()} + exec := func(sql string, args ...any) { + t.Helper() + if _, err := pool.Exec(ctx, sql, args...); err != nil { + t.Fatal(err) + } + } + exec("INSERT INTO users(id,email,password_hash,autopilot_enabled) VALUES($1,$2,'x',true)", env.user, fmt.Sprintf("%s@linked.test", env.user)) + conn := uuid.New() + exec("INSERT INTO forge_connections(id,user_id,forge_type,base_url,bot_username,bot_forge_user_id,human_username,token_ciphertext) VALUES($1,$2,'gitlab',$3,'bot',1,'alice',$4)", conn, env.user, "https://"+uuid.NewString()+".forge.test", sealed) + exec("INSERT INTO user_secrets(user_id,kind,label,is_default,ciphertext,sealed_with) VALUES($1,'anthropic_token','default',true,$2,'master')", env.user, []byte{2}) + + itConn := uuid.New() + if srv != nil { + itSvc := issuetrackersvc.New(q, box, func(u string) bool { return u == srv.URL }, issuetracker.New) + c, err := itSvc.Connect(ctx, env.user, "youtrack", srv.URL, srv.Token) + if err != nil { + t.Fatal(err) + } + itConn = c.ID + if drivers == nil { + drivers = itSvc + } + } else { + exec(`INSERT INTO issue_tracker_connections (id, user_id, kind, base_url, account_id, account_login, token_ciphertext) + VALUES ($1, $2, 'youtrack', $3, '1-1', 'bot', $4)`, itConn, env.user, "https://"+uuid.NewString()+".yt.test", []byte{1}) + } + svc.SetIssueTrackerDrivers(drivers) + exec(`INSERT INTO repos(id,connection_id,forge_project_id,path_with_namespace,web_url,default_branch,enabled, + issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at) + VALUES($1,$2,42,$3,'https://forge.test/g/r','main',true,$4,$5,$6,$7,$8,now())`, + env.repo, conn, env.repo.String(), itConn, youtracktest.ProjectID, youtracktest.ProjectShortName, youtracktest.EligibilityTagID, youtracktest.EligibilityTagName) + exec("INSERT INTO board_columns(repo_id,label_name,position) VALUES($1,'In Progress',0),($1,'Human Review',1)", env.repo) + // A cached card carrying the autopilot label: an unlinked repo would run the detector on it. + exec(`INSERT INTO issues (repo_id, forge_issue_iid, title, state, labels, web_url, forge_updated_at, synced_at) + VALUES ($1, 5, 'autopilot', 'opened', '["uzi","autopilot"]', 'https://forge.test/i/5', now(), now())`, env.repo) + exec("INSERT INTO runs(id,user_id,repo_id,kind,issue_iid,issue_title,issue_description,branch,mr_iid,mr_state,status,created_at) VALUES($1,$2,$3,'issue',7,'t','d',$4,13,'opened','completed','2020-01-01')", + env.run7, env.user, env.repo, "agent/issue-7-"+env.repo.String()) + + // A merged run that declared a moved PRD: SyncPRDLinkPatches has a candidate and + // reads the issue side for it, which would panic on a nil tracker. + exec("INSERT INTO runs(id,user_id,repo_id,kind,issue_iid,issue_title,issue_description,branch,mr_iid,mr_state,prd_done_path,status,created_at) VALUES($1,$2,$3,'issue',8,'t','Implements prds/72-x.md end to end.',$4,31,'merged','prds/done/72-x.md','completed','2020-01-01')", + uuid.New(), env.user, env.repo, "agent/issue-8-"+env.repo.String()) + + env.engine = New(svc, oneRepoStore{q, env.repo}, time.Minute, 3) + env.engine.SetPipelineWatch(time.Hour, 5) + env.starter = &apRuns{} + env.engine.SetAutopilot(NewAutopilot(q, env.starter, nil)) + return env +} + +// poll runs one tick with the default logger captured and fails if the repo's sync +// panicked (guardPanic would then silently skip every later code-host step). +func (e *linkedEnv) poll(t *testing.T) { + t.Helper() + var logs bytes.Buffer + prev := slog.Default() + slog.SetDefault(slog.New(slog.NewTextHandler(&logs, nil))) + defer slog.SetDefault(prev) + e.engine.PollOnce(e.ctx) + if strings.Contains(logs.String(), "repo sync panicked") { + t.Fatalf("the repo sync panicked: %s", logs.String()) + } +} + +func (e *linkedEnv) assertCodeHostOnly(t *testing.T) { + t.Helper() + var state *string + if err := e.pool.QueryRow(e.ctx, `SELECT mr_state FROM runs WHERE id = $1`, e.run7).Scan(&state); err != nil { + t.Fatal(err) + } + if state == nil || *state != forge.MRStateClosed { + t.Fatalf("issue run's mr_state = %v, want closed: the board-free lane must record a linked repo's issue run with no cached card", state) + } + calls := e.rec.Calls() + if !containsPrefix(calls, "GetMergeRequest(") { + t.Fatalf("the code-host steps did not run: %v", calls) + } + // The last code-host step of the pipeline watch ran: a panic in an earlier issue-side + // step would have skipped it. + if !containsPrefix(calls, "LatestPipeline(") { + t.Fatalf("SyncPipelines did not run after the issue-side steps: %v", calls) + } + for _, c := range calls { + for _, issueSide := range []string{"ListIssues(", "GetIssue(", "ListIssueLabelEvents(", "EnsureLabels(", "UpdateIssueLabels(", "ListLabels("} { + if strings.HasPrefix(c, issueSide) { + t.Errorf("the forge saw an issue-side call %q for a linked repo", c) + } + } + } + if len(e.starter.calls) != 0 { + t.Fatalf("autopilot ran on a linked repo: %+v", e.starter.calls) + } +} + +func containsPrefix(ss []string, prefix string) bool { + for _, s := range ss { + if strings.HasPrefix(s, prefix) { + return true + } + } + return false +} + +func TestLinkedRepoCodeHostStepsRunWhenTheTrackerIsUnreachableLiveDB(t *testing.T) { + srv := youtracktest.NewServer(t) + trustFake(t, srv) + env := newLinkedEnv(t, srv, nil) + srv.Close() // the tracker is gone from here on + + env.poll(t) + env.assertCodeHostOnly(t) +} + +func TestLinkedRepoCodeHostStepsRunWhenTheResolverRefusesLiveDB(t *testing.T) { + drivers := &refusingDrivers{} + env := newLinkedEnv(t, nil, drivers) + + env.poll(t) + if drivers.calls == 0 { + t.Fatal("the resolver was never asked for the linked repo's issue side") + } + env.assertCodeHostOnly(t) +} + +// With no driver source wired a linked repo fails closed on its issue side and never +// falls back to the forge, yet its code-host steps still run. +func TestLinkedRepoWithNoDriverSourceNeverFallsBackToTheForgeLiveDB(t *testing.T) { + env := newLinkedEnv(t, nil, nil) + env.svc.SetIssueTrackerDrivers(nil) + + env.poll(t) + env.assertCodeHostOnly(t) +} diff --git a/api/internal/poller/poller.go b/api/internal/poller/poller.go index 101db4a87b..844e2f5622 100644 --- a/api/internal/poller/poller.go +++ b/api/internal/poller/poller.go @@ -11,6 +11,7 @@ package poller import ( "context" + "errors" "log/slog" "runtime/debug" "sync" @@ -127,6 +128,8 @@ type Engine struct { type repoState struct { marks forgesvc.Marks pollCount int + // autopilotSkipLogged: the linked-repo autopilot skip is logged once per repo. + autopilotSkipLogged bool } // New constructs an Engine. reconcileEvery < 1 is clamped to 1 (every poll is a @@ -406,38 +409,72 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne return } + // The issue side. An unlinked repo's issue side IS the forge driver (the same value + // as the code-host side, so every forge call stays byte-identical). A linked repo + // (PRD #4173 M4) goes through the resolver, which binds repo, owner and connection + // and wraps the driver in the write guard. + linked := r.IssueTrackerConnectionID.Valid + var tr forge.IssueTracker = f + var trErr error + if linked { + tr, trErr = e.svc.IssueTrackerForRepo(ctx, r.UserID, r.ID) + if trErr != nil { + // Never hand a nil tracker to the issue-side steps below: they read through + // it per candidate and would panic, and guardPanic would then skip every + // remaining code-host step on every tick. + tr = forgesvc.UnavailableIssueTracker(trErr) + } + } + st.pollCount++ - if reconcileDue(st.pollCount, e.reconcileEvery) { - observed, err := e.svc.FullSync(ctx, r.ID, r.ForgeProjectID, f) - if err != nil { - slog.Error("poller: full sync", "repo", r.PathWithNamespace, "error", err) - report(false, forge.Class(err)) - return + if linked { + // A linked repo's issue sync failing (tracker unreachable, link unresolvable, or + // the linked sync not built yet) must never skip the code-host steps below: its + // MRs, pipelines and CI do not depend on the tracker. It also says nothing about + // the forge connection, whose client was built above, so the attempt reports ok. + report(true, forge.ErrorClassOther) + if trErr != nil { + slog.Error("poller: build linked issue tracker", "repo", r.PathWithNamespace, "error", trErr) + } else if err := e.syncLinkedIssues(ctx, r, tr, st); err != nil { + if errors.Is(err, forgesvc.ErrLinkedSyncNotReady) { + slog.Debug("poller: linked issue sync not available yet", "repo", r.PathWithNamespace) + } else { + slog.Error("poller: linked issue sync", "repo", r.PathWithNamespace, "error", err) + } } - // FullSync is unbounded and reports what it observed, not an advance; the - // clamp is the caller's. Field-wise, so a fetch that legitimately came back - // empty leaves its own mark alone rather than dragging the other's down. - st.marks = st.marks.Advance(observed) } else { - next, err := e.svc.IncrementalSync(ctx, r.ID, r.ForgeProjectID, f, st.marks) - if err != nil { - slog.Error("poller: incremental sync", "repo", r.PathWithNamespace, "error", err) - report(false, forge.Class(err)) - return + if reconcileDue(st.pollCount, e.reconcileEvery) { + observed, err := e.svc.FullSync(ctx, r.ID, r.ForgeProjectID, f) + if err != nil { + slog.Error("poller: full sync", "repo", r.PathWithNamespace, "error", err) + report(false, forge.Class(err)) + return + } + // FullSync is unbounded and reports what it observed, not an advance; the + // clamp is the caller's. Field-wise, so a fetch that legitimately came back + // empty leaves its own mark alone rather than dragging the other's down. + st.marks = st.marks.Advance(observed) + } else { + next, err := e.svc.IncrementalSync(ctx, r.ID, r.ForgeProjectID, f, st.marks) + if err != nil { + slog.Error("poller: incremental sync", "repo", r.PathWithNamespace, "error", err) + report(false, forge.Class(err)) + return + } + // Already clamped inside, and held at the caller's pair on any fetch error. + st.marks = next } - // Already clamped inside, and held at the caller's pair on any fetch error. - st.marks = next + report(true, forge.ErrorClassOther) } - report(true, forge.ErrorClassOther) - // MR-close watcher (PRD #24): after the issue cache is fresh, check each // watched card's MR for an opened↔closed edge and move the card accordingly. - // Runs only after a successful issue sync (an early return above skips it when - // the forge is unreachable). Per-candidate errors are log-and-skipped inside; + // Runs after the issue sync: on an unlinked repo only after a successful one (an early + // return above skips it when the forge is unreachable); on a linked repo regardless, + // since an issue-side failure says nothing about the code host. Per-candidate errors are log-and-skipped inside; // only a candidate-enumeration failure surfaces here. - if err := e.svc.SyncMRStates(ctx, r.ID, r.ForgeProjectID, f, f); err != nil { + if err := e.svc.SyncMRStates(ctx, r.ID, r.ForgeProjectID, f, tr); err != nil { slog.Error("poller: sync MR states", "repo", r.PathWithNamespace, "error", err) } @@ -445,7 +482,8 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne // SyncMRStates for every issue-less MR-bearing run (prompt/self_improve plus issue-less // helper runs like mr_rework/ci_fix/chat/task), so their runs.mr_state is fresh for the // mr_rework detector reading candidates later this same tick. Reads live forge state - // (needs the forge reachable, guaranteed by the early returns above); records mr_state + // (needs the forge reachable: guaranteed by the early returns above on an unlinked repo, + // and by the forge client having been built on a linked one); records mr_state // and cancels an in-flight rework on close/merge, but moves NO board card. Per-candidate // errors are log-and-skipped inside; only a candidate-enumeration failure surfaces here. if err := e.svc.SyncBoardFreeMRStates(ctx, r.ID, r.ForgeProjectID, f); err != nil { @@ -456,10 +494,11 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne // own `prds/*.md` link to the path the run moved the file to. Placed HERE — after // SyncMRStates, before the filed→close sync — because it needs a live forge // client and must not run when the forge is unreachable, which the early returns - // above already guarantee. It makes NO use of the issue cache (it reads the + // above guarantee for an unlinked repo (a linked repo runs it regardless of its + // issue side). It makes NO use of the issue cache (it reads the // description live via GetIssue, since the cache has no description column), so // unlike SyncFiledIssueCloses it does not depend on the sync ordering. - if err := e.svc.SyncPRDLinkPatches(ctx, r.ID, r.ForgeProjectID, f, f); err != nil { + if err := e.svc.SyncPRDLinkPatches(ctx, r.ID, r.ForgeProjectID, f, tr); err != nil { slog.Error("poller: sync PRD link patches", "repo", r.PathWithNamespace, "error", err) } @@ -468,7 +507,8 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne // once, on the open→closed edge, never overwriting a human's own verdict. Reads the // cache the sync above just wrote and makes NO forge call, so it costs nothing on the // wire and, like the MR watcher, is skipped entirely by the early returns when the - // forge is unreachable (a stale cache must not manufacture edges). + // forge is unreachable on an unlinked repo (a stale cache must not manufacture edges; + // a linked repo's cache is empty until the linked intake lands). if err := e.svc.SyncFiledIssueCloses(ctx, r.ID); err != nil { slog.Error("poller: sync filed-issue closes", "repo", r.PathWithNamespace, "error", err) } @@ -477,7 +517,7 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne // on the SAME fresh issue cache and with no forge call of its own. A filed incidental finding // whose issue was just observed CLOSED moves to Done, once, on the open→closed edge, and reopens // if the bug reappears. Placed immediately after the judge sync so both consume the same tick's - // cache; skipped by the same early returns when the forge is unreachable. + // cache; skipped by the same early returns when the forge is unreachable (unlinked repos). if err := e.svc.SyncFindingIssueCloses(ctx, r.ID); err != nil { slog.Error("poller: sync finding-issue closes", "repo", r.PathWithNamespace, "error", err) } @@ -506,8 +546,16 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne // application on a PRD issue into an auto_approve run for the mapped consenting // user (or one explanatory issue comment). All per-issue errors are handled // inside; nothing surfaces here. + // Skipped on a linked repo (PRD #4173 D12): the autopilot label is a forge label. if e.autopilot != nil { - e.autopilot.detect(ctx, r, f, f) + if linked { + if !st.autopilotSkipLogged { + st.autopilotSkipLogged = true + slog.Info("poller: autopilot detection skipped for a repo linked to an issue tracker", "repo", r.PathWithNamespace) + } + } else { + e.autopilot.detect(ctx, r, f, f) + } } // CI-autofix detection (PRD #71 M6): also post-sync, a sibling of the autopilot @@ -518,7 +566,7 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne // MR branch into an automatic ci_fix run (or one halt comment). All per-candidate // errors are handled inside; nothing surfaces here. if e.pipelineMaxRefs > 0 && e.ciAutoFix != nil { - e.ciAutoFix.detect(ctx, r, f, f) + e.ciAutoFix.detect(ctx, r, f, tr) } // MR-review-rework detection (PRD #700 M3): also post-sync, a sibling of the @@ -530,7 +578,7 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne // being wired (the instance kill-switch); the admin kill-switch + per-user opt-in + // per-MR cap gate activation inside it. All per-candidate errors are handled inside. if e.pipelineMaxRefs > 0 && e.mrReviewWatch != nil { - e.mrReviewWatch.detect(ctx, r, f, f) + e.mrReviewWatch.detect(ctx, r, f, tr) } // Reverse Status→label sync (PRD #364 M6): a genuine per-tick sibling (NOT gated @@ -541,13 +589,34 @@ func (e *Engine) syncRepo(ctx context.Context, r store.ListEnabledReposWithConne // overlap (tick does wg.Wait()), but the forward path in the HTTP handler CAN // interleave with this read — the stored-marker no-op tolerates it (worst case a // redundant same-value label write). - if e.reverseSyncEligible(r.ForgeType) { + // Skipped on a linked repo: a project's items are forge issue numbers (PRD #4173 M4). + if e.reverseSyncEligible(r.ForgeType) && !linked { if err := e.projectReverse.ReverseSync(ctx, r.ID); err != nil { slog.Error("poller: reverse project sync", "repo", r.PathWithNamespace, "error", err) } } } +// syncLinkedIssues runs the linked repo's issue sync through the forgesvc +// dispatchers on the same full/incremental cadence as an unlinked repo, folding the +// marks only on success. +func (e *Engine) syncLinkedIssues(ctx context.Context, r store.ListEnabledReposWithConnectionsRow, tr forge.IssueTracker, st *repoState) error { + if reconcileDue(st.pollCount, e.reconcileEvery) { + observed, err := e.svc.SyncIssues(ctx, r.ID, r.ForgeProjectID, tr) + if err != nil { + return err + } + st.marks = st.marks.Advance(observed) + return nil + } + next, err := e.svc.SyncIssuesIncremental(ctx, r.ID, r.ForgeProjectID, tr, st.marks) + if err != nil { + return err + } + st.marks = next + return nil +} + // reverseSyncEligible reports whether the per-tick reverse (Status → label) sibling // should fire for a repo of forgeType: only GitHub repos, and only when a reverse // syncer is wired (the instance kill-switch by wiring). Extracted so the fire/skip diff --git a/api/internal/poller/recorded_calls_livedb_test.go b/api/internal/poller/recorded_calls_livedb_test.go new file mode 100644 index 0000000000..d5a09a939d --- /dev/null +++ b/api/internal/poller/recorded_calls_livedb_test.go @@ -0,0 +1,155 @@ +package poller + +import ( + "context" + "fmt" + "os" + "testing" + "time" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/forgesvc" + "github.com/vtmocanu/uzi/api/internal/secretbox" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// Recorded-call test (PRD 4173 M4): for an enabled repo with no issue-tracker +// link, pin every forge call a poller tick makes, for a full reconcile tick and +// then an incremental tick carrying the marks the first one produced. The repo +// is seeded so every forge-touching step of syncRepo has work to do and so +// removing any of them changes the golden: +// +// - issue 7: an open card in Human Review whose run (MR 13) is recorded +// opened while the forge reports it closed, so SyncMRStates reads the MR +// and moves the card (EnsureLabels, UpdateIssueLabels); +// - a prompt run with MR 21 and no issue, read by SyncBoardFreeMRStates; +// - issue 8: a run whose merged MR 31 declared a moved PRD, so +// SyncPRDLinkPatches reads the live description and rewrites it; +// - issue 5: carries the autopilot label, so the autopilot detector reads its +// label events and captures its issue input. The detector is the real +// poller.Autopilot over the real store; only the run starter is a fake, +// because the run it creates makes no forge call of its own. +// +// The CI-autofix, MR-review watch and project reverse-sync detectors stay +// unwired, as in the other poller live-DB tests; the pipeline watch is on. The +// golden was captured before the tracker-link rerouting; regenerate only +// deliberately, with UZI_UPDATE_GOLDEN=1. + +// recordedPollForge answers the reads and accepts the writes the seeded repo +// makes. The MR states are keyed by MR iid: 13 closed, 21 opened, 31 merged. +type recordedPollForge struct{ forgetest.BaseFake } + +var recordedPollAt = time.Date(2026, 5, 6, 7, 8, 9, 0, time.UTC) + +func (*recordedPollForge) ListIssues(_ context.Context, _ int64, opts forge.ListIssuesOptions) ([]forge.Issue, error) { + if len(opts.Labels) == 1 && opts.Labels[0] == "agent-found" { + // One agent-found issue so the finding mark is non-zero and tick 2's + // incremental finding fetch carries its UpdatedAfter bound. + return []forge.Issue{{IID: 11, Title: "finding", State: "opened", Labels: []string{"agent-found"}, UpdatedAt: recordedPollAt}}, nil + } + return []forge.Issue{ + {IID: 5, Title: "autopilot", State: "opened", Labels: []string{"uzi", "autopilot"}, UpdatedAt: recordedPollAt}, + {IID: 7, Title: "card", State: "opened", Labels: []string{"uzi", "Human Review"}, UpdatedAt: recordedPollAt}, + }, nil +} +func (*recordedPollForge) GetMergeRequest(_ context.Context, _, iid int64) (forge.MergeRequest, error) { + state := map[int64]string{13: forge.MRStateClosed, 21: forge.MRStateOpened, 31: forge.MRStateMerged}[iid] + return forge.MergeRequest{IID: iid, State: state}, nil +} +func (*recordedPollForge) GetIssue(_ context.Context, _, iid int64) (forge.Issue, error) { + desc := "see the issue" + if iid == 8 { + desc = "Implements prds/72-x.md end to end." + } + return forge.Issue{IID: iid, Title: "t", State: "opened", Description: desc, Labels: []string{"uzi", "autopilot"}, AuthorForgeUserID: 2, UpdatedAt: recordedPollAt}, nil +} +func (*recordedPollForge) UpdateIssueDescription(context.Context, int64, int64, string) error { + return nil +} +func (*recordedPollForge) EnsureLabels(context.Context, int64, []forge.Label) error { return nil } +func (*recordedPollForge) UpdateIssueLabels(context.Context, int64, int64, []string, []string) error { + return nil +} +func (*recordedPollForge) ListIssueLabelEvents(context.Context, int64, int64) ([]forge.LabelEvent, error) { + return []forge.LabelEvent{{ID: 100, Action: "add", LabelName: "autopilot", Username: "alice"}}, nil +} +func (*recordedPollForge) ListIssueComments(context.Context, int64, int64) ([]forge.IssueComment, error) { + return nil, nil +} +func (*recordedPollForge) RepositoryAuthorEligibility(context.Context, int64, int64) (forge.AuthorEligibility, error) { + return forge.AuthorEligible, nil +} + +func TestRecordedCallsPollerTickLiveDB(t *testing.T) { + dsn := os.Getenv("UZI_TEST_DATABASE_URL") + if dsn == "" { + t.Skip("UZI_TEST_DATABASE_URL unset; run e2e/run-store-it.sh") + } + ctx := context.Background() + if err := store.Migrate(ctx, dsn); err != nil { + t.Fatal(err) + } + pool, err := store.OpenPool(ctx, dsn) + if err != nil { + t.Fatal(err) + } + t.Cleanup(pool.Close) + q := store.New(pool) + box, err := secretbox.New([]byte("0123456789abcdef0123456789abcdef")) + if err != nil { + t.Fatal(err) + } + rec := forgetest.NewRecorder(&recordedPollForge{}) + svc := forgesvc.NewWithForgeBuilder(q, box, time.Second, nil, func(forge.Type, string, string, time.Duration) (forge.Forge, error) { + return rec, nil + }) + sealed, err := svc.EncryptToken("fixture") + if err != nil { + t.Fatal(err) + } + user, conn, repo := uuid.New(), uuid.New(), uuid.New() + exec := func(sql string, args ...any) { + t.Helper() + if _, err := pool.Exec(ctx, sql, args...); err != nil { + t.Fatal(err) + } + } + exec("INSERT INTO users(id,email,password_hash,autopilot_enabled) VALUES($1,$2,'x',true)", user, fmt.Sprintf("%s@recorded.test", user)) + exec("INSERT INTO forge_connections(id,user_id,forge_type,base_url,bot_username,bot_forge_user_id,human_username,token_ciphertext) VALUES($1,$2,'gitlab',$3,'bot',1,'alice',$4)", conn, user, "https://"+uuid.NewString()+".forge.test", sealed) + exec("INSERT INTO user_secrets(user_id,kind,label,is_default,ciphertext,sealed_with) VALUES($1,'anthropic_token','default',true,$2,'master')", user, []byte{2}) + exec("INSERT INTO repos(id,connection_id,forge_project_id,path_with_namespace,web_url,default_branch,enabled) VALUES($1,$2,42,$3,'https://forge.test/g/r','main',true)", repo, conn, repo.String()) + exec("INSERT INTO board_columns(repo_id,label_name,position) VALUES($1,'In Progress',0),($1,'Human Review',1)", repo) + const runCols = "INSERT INTO runs(id,user_id,repo_id,kind,issue_iid,issue_title,issue_description,branch,mr_iid,mr_state,prd_done_path,status,created_at) " + // Board lane: issue 7's card is in Human Review, MR 13 recorded opened. + exec(runCols+"VALUES($1,$2,$3,'issue',7,'t','d',$4,13,'opened',NULL,'completed','2020-01-01')", uuid.New(), user, repo, "agent/issue-7-"+repo.String()) + // Board-free lane: an issue-less prompt run whose MR 21 was never observed. + exec(runCols+"VALUES($1,$2,$3,'prompt',NULL,'t','d',$4,21,NULL,NULL,'completed','2020-01-01')", uuid.New(), user, repo, "uzi/prompt-"+repo.String()) + // PRD link patch: issue 8's run merged MR 31 after moving its PRD. + exec(runCols+"VALUES($1,$2,$3,'issue',8,'t','Implements prds/72-x.md end to end.',$4,31,'merged','prds/done/72-x.md','completed','2020-01-01')", uuid.New(), user, repo, "agent/issue-8-"+repo.String()) + + // reconcileEvery 3: tick 1 is the first poll (full reconcile), tick 2 is + // incremental. + engine := New(svc, mergedRepoStore{q, repo}, time.Minute, 3) + engine.SetPipelineWatch(time.Hour, 5) + starter := &apRuns{} + engine.SetAutopilot(NewAutopilot(q, starter, nil)) + + var lines []string + for i, label := range []string{"tick 1 (full reconcile)", "tick 2 (incremental)"} { + before := len(rec.Calls()) + engine.PollOnce(ctx) + calls := rec.Calls()[before:] + if len(calls) == 0 { + t.Fatalf("tick %d made no forge call", i+1) + } + lines = append(lines, "# "+label) + lines = append(lines, calls...) + } + if len(starter.calls) != 1 || starter.calls[0].iid != 5 { + t.Fatalf("autopilot run starter calls = %+v, want exactly one, for issue 5", starter.calls) + } + forgetest.AssertGolden(t, "recorded_calls_poller_tick.golden", lines) +} diff --git a/api/internal/poller/testdata/recorded_calls_poller_tick.golden b/api/internal/poller/testdata/recorded_calls_poller_tick.golden new file mode 100644 index 0000000000..a9ea22d5a3 --- /dev/null +++ b/api/internal/poller/testdata/recorded_calls_poller_tick.golden @@ -0,0 +1,24 @@ +# tick 1 (full reconcile) +ListIssues(42, {Labels:["uzi"] UpdatedAfter: State:"" Limit:0}) +ListIssues(42, {Labels:nil UpdatedAfter: State:"opened" Limit:0}) +ListIssues(42, {Labels:["agent-found"] UpdatedAfter: State:"" Limit:0}) +GetMergeRequest(42, 13) +EnsureLabels(42, [{Name:"In Progress" Color:"#1f75cb"}]) +UpdateIssueLabels(42, 7, ["In Progress"], ["Human Review"]) +GetMergeRequest(42, 21) +GetMergeRequest(42, 31) +GetIssue(42, 8) +UpdateIssueDescription(42, 8, "Implements prds/done/72-x.md end to end.") +LatestPipeline(42, "main") +ListIssueLabelEvents(42, 5) +GetIssue(42, 5) +RepositoryAuthorEligibility(42, 2) +ListIssueComments(42, 5) +# tick 2 (incremental) +ListIssues(42, {Labels:["uzi"] UpdatedAfter:2026-05-06T07:08:09Z State:"" Limit:0}) +ListIssues(42, {Labels:nil UpdatedAfter:2026-05-06T07:08:09Z State:"opened" Limit:0}) +ListIssues(42, {Labels:["agent-found"] UpdatedAfter:2026-05-06T07:08:09Z State:"" Limit:0}) +GetMergeRequest(42, 13) +GetMergeRequest(42, 21) +LatestPipeline(42, "main") +ListIssueLabelEvents(42, 5) diff --git a/api/internal/provisionsvc/provisionsvc.go b/api/internal/provisionsvc/provisionsvc.go index 4448cfa02f..a8bd6dfddf 100644 --- a/api/internal/provisionsvc/provisionsvc.go +++ b/api/internal/provisionsvc/provisionsvc.go @@ -310,9 +310,15 @@ func (s *Service) EnableRepo(ctx context.Context, userID, repoID uuid.UUID) (sto s.logger().Error("build forge for connection", "error", err) return store.Repo{}, internal() } - // PRD #4173 D3: board columns are labels, on the issue-tracker half. - var tr forge.IssueTracker = f - if err := forgesvc.EnsureBoardColumns(ctx, s.q, tr, row.ID, row.ForgeProjectID); err != nil { + // PRD #4173 D3: board columns are labels, on the issue-tracker half. A repo linked to an + // external issue tracker records the column rows only: uzi creates no labels or tags + // there (writes arrive in M5), and the forge's project is not where its issues live. + if row.IssueTrackerConnectionID.Valid { + if err := forgesvc.EnsureBoardColumnRows(ctx, s.q, row.ID); err != nil { + s.logger().Error("ensure board column rows", "error", err) + return store.Repo{}, internal() + } + } else if err := forgesvc.EnsureBoardColumns(ctx, s.q, forge.IssueTracker(f), row.ID, row.ForgeProjectID); err != nil { var seedErr *forgesvc.LabelSeedError if errors.As(err, &seedErr) { return store.Repo{}, &Error{Kind: KindBadGateway, Message: "could not seed board labels on the forge: " + seedErr.Error()} diff --git a/api/internal/provisionsvc/provisionsvc_test.go b/api/internal/provisionsvc/provisionsvc_test.go index 98acc82724..78a628b371 100644 --- a/api/internal/provisionsvc/provisionsvc_test.go +++ b/api/internal/provisionsvc/provisionsvc_test.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgtype" "github.com/vtmocanu/uzi/api/internal/forge" "github.com/vtmocanu/uzi/api/internal/forge/forgetest" @@ -381,3 +382,27 @@ func TestEnableRepoUnknownRepoIsNotFound(t *testing.T) { t.Fatalf("error = %+v", pe) } } + +// PRD #4173 M4: enabling a repo linked to an external issue tracker records the default +// column rows and creates no labels: the forge's project is not where its issues live. +func TestEnableLinkedRepoSeedsColumnRowsWithoutForgeLabels(t *testing.T) { + row := repoRow() + row.IssueTrackerConnectionID = pgtype.UUID{Bytes: uuid.New(), Valid: true} + st := &fakeStore{repoRow: row} + ff := &fakeForge{labelsErr: errors.New("the forge must not be asked for labels")} + svc := New(st, fakeForges{ff}, &fakeGuard{}, allowAll) + + repo, err := svc.EnableRepo(context.Background(), uuid.New(), row.ID) + if err != nil { + t.Fatalf("EnableRepo: %v", err) + } + if !repo.Enabled || !st.enabled { + t.Fatal("repo must be enabled") + } + if want := defaultColumnNames(); !slices.Equal(st.columns, want) { + t.Fatalf("board columns = %v, want %v", st.columns, want) + } + if ff.labelCall != 0 { + t.Fatalf("EnsureLabels calls = %d, want 0 for a linked repo", ff.labelCall) + } +} diff --git a/api/internal/provisionsvc/recorded_calls_test.go b/api/internal/provisionsvc/recorded_calls_test.go new file mode 100644 index 0000000000..a167293a49 --- /dev/null +++ b/api/internal/provisionsvc/recorded_calls_test.go @@ -0,0 +1,66 @@ +package provisionsvc + +import ( + "context" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgtype" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/privcheck" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// Recorded-call test (PRD 4173 M4): for a repo with no issue-tracker link, +// pin every forge call EnableRepo makes, including the live write guard, which +// is the REAL privcheck.Service here rather than the package's fakeGuard. The +// golden was captured before the tracker-link rerouting; regenerate only +// deliberately, with UZI_UPDATE_GOLDEN=1. + +// guardedForge answers the guard's reads with a clean, write-role bot on a +// protected default branch, and accepts the column label seed. +type guardedForge struct{ forgetest.BaseFake } + +func (*guardedForge) VerifyToken(context.Context) (forge.BotIdentity, error) { + return forge.BotIdentity{ForgeUserID: 9, Username: "bot"}, nil +} +func (*guardedForge) ProjectRole(context.Context, int64, int64) (forge.Role, bool, error) { + return forge.RoleWrite, true, nil +} +func (*guardedForge) DefaultBranchProtection(context.Context, int64, string, int64) (forge.BranchProtection, error) { + return forge.BranchProtection{Protected: true}, nil +} +func (*guardedForge) EnsureLabels(context.Context, int64, []forge.Label) error { return nil } + +// recordedForges hands the one Recorder-wrapped forge to every caller, the +// service and the guard alike. +type recordedForges struct{ rec *forgetest.Recorder } + +func (recordedForges) EncryptToken(pat string) ([]byte, error) { return []byte("sealed:" + pat), nil } +func (r recordedForges) ForgeForToken(forge.Type, string, string) (forge.Forge, error) { + return r.rec, nil +} +func (r recordedForges) ForgeForConnection(string, string, []byte) (forge.Forge, error) { + return r.rec, nil +} + +func TestRecordedCallsEnableRepo(t *testing.T) { + rec := forgetest.NewRecorder(&guardedForge{}) + st := &fakeStore{repoRow: store.GetRepoForUserRow{ + ID: uuid.New(), ForgeType: "gitlab", BaseUrl: "https://forge.example", + ForgeProjectID: 11, PathWithNamespace: "g/a", DefaultBranch: pgtype.Text{String: "main", Valid: true}, + }} + guard := privcheck.NewService(nil, recordedForges{rec}) + svc := New(st, recordedForges{rec}, guard, allowAll) + + repo, err := svc.EnableRepo(context.Background(), uuid.New(), st.repoRow.ID) + if err != nil { + t.Fatalf("EnableRepo: %v", err) + } + if !repo.Enabled { + t.Fatal("repo must be enabled") + } + forgetest.AssertGolden(t, "recorded_calls_provisionsvc_enablerepo.golden", rec.Calls()) +} diff --git a/api/internal/provisionsvc/testdata/recorded_calls_provisionsvc_enablerepo.golden b/api/internal/provisionsvc/testdata/recorded_calls_provisionsvc_enablerepo.golden new file mode 100644 index 0000000000..a69cfc65cf --- /dev/null +++ b/api/internal/provisionsvc/testdata/recorded_calls_provisionsvc_enablerepo.golden @@ -0,0 +1,4 @@ +VerifyToken() +ProjectRole(11, 9) +DefaultBranchProtection(11, "main", 9) +EnsureLabels(11, [{Name:"Planned" Color:"#6699cc"} {Name:"In Progress" Color:"#1f75cb"} {Name:"Human Review" Color:"#6e49cb"} {Name:"Later" Color:"#999999"}]) diff --git a/api/internal/runlifecycle/lifecycle.go b/api/internal/runlifecycle/lifecycle.go index b13b15c4a4..6a3b804261 100644 --- a/api/internal/runlifecycle/lifecycle.go +++ b/api/internal/runlifecycle/lifecycle.go @@ -33,6 +33,8 @@ import ( "github.com/vtmocanu/uzi/api/internal/board" "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forgesvc" + "github.com/vtmocanu/uzi/api/internal/issuesource" "github.com/vtmocanu/uzi/api/internal/store" ) @@ -96,6 +98,11 @@ type Lifecycle struct { // Projects v2 Status (PRD #364 M5). Optional; nil skips the projection. projector Projector + // issueSources resolves the issue side of a repo linked to an external issue + // tracker (PRD #4173 DL-5); SetIssueSources. Nil ⇒ a linked repo fails closed + // (issuesource.ErrNotWired) and an unlinked one keeps using the mover's forge. + issueSources issuesource.Sources + // frontendOrigin is the user-facing origin (config.FrontendOrigin) the // terminal-comment hook builds run links from. Empty omits the link. frontendOrigin string @@ -138,6 +145,9 @@ func New(q Store, mover Mover, frontendOrigin string) *Lifecycle { // then performs no Status projection. func (l *Lifecycle) SetProjector(p Projector) { l.projector = p } +// SetIssueSources wires the resolver of a linked repo's issue side (PRD #4173 DL-5). +func (l *Lifecycle) SetIssueSources(is issuesource.Sources) { l.issueSources = is } + // SetBeat wires the admin-health loop-beat callback (PRD #1484 M2). Call once at startup, // before RunReconciler. A nil beat (the default) disables it, so tests that never wire one // behave exactly as before. The callback fires once per reconcile tick. @@ -151,6 +161,8 @@ func (l *Lifecycle) Interval() time.Duration { return l.interval } // by the notifier (GetRunMoveContext) and the reconciler (re-read per run). type moveContext struct { repoID uuid.UUID + userID uuid.UUID + linked bool // the repo's issue_tracker_connection_id is set (PRD #4173) issueIID int64 forgeProjectID int64 forgeType string @@ -309,6 +321,8 @@ func contextFromRow(mc store.GetRunMoveContextRow) moveContext { // GetRunMoveContext INNER-JOINs repos, so a row is returned only for a run // with a non-NULL repo_id (a chat run yields ErrNoRows and never reaches here). repoID: uuid.UUID(mc.RepoID.Bytes), + userID: mc.UserID, + linked: mc.IssueTrackerConnectionID.Valid, issueIID: mc.IssueIid.Int64, // valid: notifyOnce and reconcileOne skip card-less runs (NULL issue_iid) forgeProjectID: mc.ForgeProjectID, forgeType: mc.ForgeType, @@ -339,6 +353,12 @@ func (l *Lifecycle) apply(ctx context.Context, runID uuid.UUID, mc moveContext, if err != nil { if !errors.Is(err, pgx.ErrNoRows) { slog.Warn("run lifecycle: load issue for move", "run", runID, "error", err) + } else if mc.linked { + // PRD #4173 D10: a linked issue that moved to another project is evicted from + // the cache; it will never be found again, so the move is a permanent skip. + slog.Info("run lifecycle: linked issue is no longer cached (moved or removed), skipping move", "run", runID, "issue", mc.issueIID) + l.clearPending(ctx, runID) + return } // No cached card to move (evicted/de-labeled forge-side): leave the marker; // the reconcile loop gives up after the window. @@ -390,7 +410,23 @@ func (l *Lifecycle) apply(ctx context.Context, runID uuid.UUID, mc moveContext, slog.Warn("run lifecycle: build forge client for move", "run", runID, "error", err) return } - if _, err := l.mover.AutoMove(ctx, f, mc.forgeProjectID, issue, cols, dec.target); err != nil { + tr, err := issuesource.For(ctx, l.issueSources, mc.linked, mc.userID, mc.repoID, f) + if err != nil { + // A linked repo with no resolver, or one that cannot be built: never the forge. + // Left for the reconcile loop like any other outage. + slog.Warn("run lifecycle: resolve issue tracker for move", "run", runID, "error", err) + return + } + if _, err := l.mover.AutoMove(ctx, tr, mc.forgeProjectID, issue, cols, dec.target); err != nil { + if forgesvc.IssueWriteSkippable(err) { + // PRD #4173 D10: the write is not offered on a linked repo, or the linked issue + // is gone from its binding. Retrying cannot help, so clear the marker (permanent + // skip, logged once) instead of retrying to the give-up window. The run's + // code-host side is unaffected. + slog.Info("run lifecycle: issue move skipped on linked repo", "run", runID, "target", dec.target, "reason", err) + l.clearPending(ctx, runID) + return + } // Forge error: the transition already succeeded; leave the marker so the // reconcile loop retries once the forge is reachable again. slog.Warn("run lifecycle: forge move failed, will retry", "run", runID, "target", dec.target, "error", err) @@ -482,11 +518,21 @@ func (l *Lifecycle) maybeTerminalComment(ctx context.Context, runID uuid.UUID, s slog.Warn("run lifecycle: build forge for terminal comment", "run", runID, "error", err) return } - // Only the issue side of the driver is used here. - var tracker forge.IssueTracker = f + // Only the issue side of the driver is used here. PRD #4173: a linked repo's + // comment goes through the resolver, whose write guard refuses it (writes arrive + // in M5); like any terminal-comment failure it is logged and the lifecycle continues. + tracker, err := issuesource.For(ctx, l.issueSources, mc.IssueTrackerConnectionID.Valid, mc.UserID, uuid.UUID(mc.RepoID.Bytes), f) + if err != nil { + slog.Warn("run lifecycle: resolve issue tracker for terminal comment", "run", runID, "error", err) + return + } body := l.terminalCommentBody(status, runID, mc) if _, err := tracker.CreateIssueNote(ctx, mc.ForgeProjectID, mc.IssueIid.Int64, body); err != nil { // Already PAT-redacted by the driver. Recorded but lost (never retried). + if forgesvc.IssueWriteSkippable(err) { + slog.Info("run lifecycle: autopilot terminal comment skipped on linked repo", "run", runID, "reason", err) + return + } slog.Warn("run lifecycle: post autopilot terminal comment", "run", runID, "error", err) } } diff --git a/api/internal/runlifecycle/linked_test.go b/api/internal/runlifecycle/linked_test.go new file mode 100644 index 0000000000..c23baa0ca4 --- /dev/null +++ b/api/internal/runlifecycle/linked_test.go @@ -0,0 +1,207 @@ +package runlifecycle + +import ( + "context" + "errors" + "fmt" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgtype" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forgesvc" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// PRD #4173 M4 (DL-5, D10): a linked repo's issue side comes from the resolver, never +// the forge, and a write the resolver skips clears the move marker instead of retrying. + +// resolvedTracker marks the issue side the resolver returned. +type resolvedTracker struct{ forge.IssueTracker } + +type fakeSources struct { + calls []struct{ user, repo uuid.UUID } + tr forge.IssueTracker + err error + called int +} + +func (f *fakeSources) IssueTrackerForRepo(_ context.Context, userID, repoID uuid.UUID) (forge.IssueTracker, error) { + f.called++ + f.calls = append(f.calls, struct{ user, repo uuid.UUID }{userID, repoID}) + return f.tr, f.err +} + +// seenMover records which issue tracker AutoMove was handed. +type seenMover struct { + *fakeMover + seen []forge.IssueTracker +} + +func (m *seenMover) AutoMove(ctx context.Context, f forge.IssueTracker, p int64, issue store.Issue, cols []store.BoardColumn, target string) (store.Issue, error) { + m.seen = append(m.seen, f) + return m.fakeMover.AutoMove(ctx, f, p, issue, cols, target) +} + +func linkedMoveCtx(repoID, userID uuid.UUID, status string) store.GetRunMoveContextRow { + mc := moveCtx(repoID, 4, status, txt("Later"), nullText()) + mc.IssueTrackerConnectionID = pgtype.UUID{Bytes: uuid.New(), Valid: true} + mc.UserID = userID + return mc +} + +func linkedFixture(status string, autoMoveErr error) (*fakeStore, *seenMover, *fakeSources, *Lifecycle, uuid.UUID, uuid.UUID, uuid.UUID) { + runID, repoID, userID := uuid.New(), uuid.New(), uuid.New() + fs := &fakeStore{ + moveCtx: linkedMoveCtx(repoID, userID, status), + issue: issueWith(repoID, 4, "opened", "Later"), + columns: defaultCols(), + } + sm := &seenMover{fakeMover: &fakeMover{autoMoveErr: autoMoveErr}} + src := &fakeSources{tr: &resolvedTracker{}} + l := New(fs, sm, testOrigin) + l.now = newTestLifecycle(fs, sm.fakeMover).now + l.SetIssueSources(src) + return fs, sm, src, l, runID, repoID, userID +} + +func TestLinkedMoveUsesResolverNotForge(t *testing.T) { + fs, sm, src, l, runID, repoID, userID := linkedFixture("queued", nil) + + l.notifyOnce(context.Background(), runID, "queued") + + if len(sm.seen) != 1 || sm.seen[0] != src.tr { + t.Fatalf("AutoMove must be handed the resolver's tracker, got %v", sm.seen) + } + if len(src.calls) != 1 || src.calls[0].user != userID || src.calls[0].repo != repoID { + t.Fatalf("resolver must be asked for the run's owner and repo row, got %+v", src.calls) + } + if len(fs.recorded) != 1 { + t.Fatalf("a successful move records board_column, got %v", fs.recorded) + } +} + +func TestLinkedMoveWithoutSourcesFailsClosed(t *testing.T) { + fs, sm, _, l, runID, _, _ := linkedFixture("queued", nil) + l.issueSources = nil + + l.notifyOnce(context.Background(), runID, "queued") + + if len(sm.moves) != 0 { + t.Fatalf("a linked repo with no resolver must never reach AutoMove (forge fallback), got %v", sm.moves) + } + if len(fs.cleared) != 0 { + t.Fatal("an unresolvable issue side is an outage: the marker is left for the reconcile loop") + } +} + +func TestLinkedMoveSkippableWriteClearsMarker(t *testing.T) { + for name, err := range map[string]error{ + "not offered": fmt.Errorf("update issue labels: %w", forgesvc.ErrIssueWriteNotOffered), + } { + t.Run(name, func(t *testing.T) { + fs, sm, _, l, runID, _, _ := linkedFixture("queued", err) + + l.notifyOnce(context.Background(), runID, "queued") + + if len(sm.moves) != 1 { + t.Fatalf("expected one move attempt, got %v", sm.moves) + } + if len(fs.cleared) != 1 || fs.cleared[0] != runID { + t.Fatalf("a skippable write clears the marker once (no retry to the give-up window), got %v", fs.cleared) + } + if len(fs.recorded) != 0 { + t.Fatal("board_column must not be recorded for a skipped move") + } + }) + } +} + +func TestLinkedMoveOtherErrorStillRetries(t *testing.T) { + fs, _, _, l, runID, _, _ := linkedFixture("queued", errors.New("youtrack unreachable")) + + l.notifyOnce(context.Background(), runID, "queued") + + if len(fs.cleared) != 0 { + t.Fatal("a transient resolver/tracker error leaves the marker") + } +} + +func TestLinkedMoveIssueNoLongerCachedClearsMarker(t *testing.T) { + fs, sm, _, l, runID, _, _ := linkedFixture("queued", nil) + fs.issueErr = pgx.ErrNoRows // the moved issue was evicted from the cache + + l.notifyOnce(context.Background(), runID, "queued") + + if len(fs.cleared) != 1 || len(sm.moves) != 0 { + t.Fatalf("an evicted linked issue is a permanent skip: cleared=%v moves=%v", fs.cleared, sm.moves) + } +} + +func TestUnlinkedNoLongerCachedKeepsMarker(t *testing.T) { + runID, repoID := uuid.New(), uuid.New() + fs := &fakeStore{moveCtx: moveCtx(repoID, 4, "queued", txt("Later"), nullText()), issueErr: pgx.ErrNoRows, columns: defaultCols()} + fm := &fakeMover{} + l := newTestLifecycle(fs, fm) + src := &fakeSources{} + l.SetIssueSources(src) + + l.notifyOnce(context.Background(), runID, "queued") + + if len(fs.cleared) != 0 { + t.Fatal("the unlinked path is unchanged: no cached card leaves the marker") + } + if src.called != 0 { + t.Fatal("an unlinked repo must never consult the resolver") + } +} + +func TestUnlinkedMoveNeverConsultsResolver(t *testing.T) { + runID, repoID := uuid.New(), uuid.New() + fs := &fakeStore{ + moveCtx: moveCtx(repoID, 4, "queued", txt("Later"), nullText()), + issue: issueWith(repoID, 4, "opened", "Later"), + columns: defaultCols(), + } + fm := &fakeMover{} + sm := &seenMover{fakeMover: fm} + l := New(fs, sm, testOrigin) + src := &fakeSources{} + l.SetIssueSources(src) + + l.notifyOnce(context.Background(), runID, "queued") + + if src.called != 0 || len(sm.seen) != 1 || sm.seen[0] != forge.IssueTracker(fm.forge) { + t.Fatalf("an unlinked move uses the mover's forge: resolver calls=%d seen=%v", src.called, sm.seen) + } +} + +func TestLinkedTerminalCommentUsesResolverAndLogsSkip(t *testing.T) { + fs, _, src, l, runID, _, _ := linkedFixture("failed", nil) + fs.moveCtx.AutoApprove = true + note := &fakeForge{noteErr: fmt.Errorf("create issue note: %w", forgesvc.ErrIssueWriteNotOffered)} + src.tr = note + + l.notifyOnce(context.Background(), runID, "failed") + + if len(note.notes) != 1 { + t.Fatalf("the comment goes to the resolver's tracker, got %d notes", len(note.notes)) + } + if fs.claimCalls != 1 { + t.Fatalf("record-then-comment is unchanged, claims=%d", fs.claimCalls) + } +} + +func TestLinkedTerminalCommentWithoutSourcesPostsNothing(t *testing.T) { + fs, sm, _, l, runID, _, _ := linkedFixture("failed", nil) + fs.moveCtx.AutoApprove = true + l.issueSources = nil + + l.notifyOnce(context.Background(), runID, "failed") + + if sm.forge != nil && len(sm.forge.notes) != 0 { + t.Fatal("a linked repo with no resolver must not comment through the forge") + } +} diff --git a/api/internal/runlifecycle/moved_issue_livedb_test.go b/api/internal/runlifecycle/moved_issue_livedb_test.go new file mode 100644 index 0000000000..3a682d3f7a --- /dev/null +++ b/api/internal/runlifecycle/moved_issue_livedb_test.go @@ -0,0 +1,172 @@ +package runlifecycle + +import ( + "context" + "crypto/sha256" + "fmt" + "net/http" + "os" + "testing" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgtype" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/forgesvc" + "github.com/vtmocanu/uzi/api/internal/issuetracker" + "github.com/vtmocanu/uzi/api/internal/issuetracker/youtracktest" + "github.com/vtmocanu/uzi/api/internal/issuetrackersvc" + "github.com/vtmocanu/uzi/api/internal/secretbox" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// mrForge is the code host of the repo: it reports the run's MR as opened. +type mrForge struct{ forgetest.BaseFake } + +func (*mrForge) GetMergeRequest(_ context.Context, _, iid int64) (forge.MergeRequest, error) { + return forge.MergeRequest{IID: iid, State: forge.MRStateOpened}, nil +} + +// PRD #4173 M4 acceptance, last clause (D10), the move BEFORE the write: a run on a +// repo linked to YouTrack whose issue was moved to another project completes. The +// lifecycle's issue write is skipped (no write request reaches YouTrack), the move +// marker is cleared instead of being retried to the give-up window, and the code-host +// side (the MR the run opened, recorded by the board-free watch) still completes. +// A move between the check and the write is not tested: YouTrack has no conditional +// write, so that window is accepted (PRD #4173). +// +// Skipped unless UZI_TEST_DATABASE_URL points at a throwaway Postgres. +func TestMovedLinkedIssueSkipsTheWriteAndTheCodeHostSideCompletesLiveDB(t *testing.T) { + dsn := os.Getenv("UZI_TEST_DATABASE_URL") + if dsn == "" { + t.Skip("UZI_TEST_DATABASE_URL not set; run via e2e/run-store-it.sh for live-DB coverage") + } + ctx := context.Background() + if err := store.Migrate(ctx, dsn); err != nil { + t.Fatalf("migrate: %v", err) + } + pool, err := store.OpenPool(ctx, dsn) + if err != nil { + t.Fatalf("open pool: %v", err) + } + t.Cleanup(pool.Close) + q := store.New(pool) + key := make([]byte, secretbox.KeySize) + for i := range key { + key[i] = byte(i + 1) + } + box, err := secretbox.New(key) + if err != nil { + t.Fatal(err) + } + + srv := youtracktest.NewServer(t) + def, ok := http.DefaultTransport.(*http.Transport) + if !ok { + t.Fatal("http.DefaultTransport is not an *http.Transport") + } + saved := def.TLSClientConfig + def.TLSClientConfig = srv.Client().Transport.(*http.Transport).TLSClientConfig.Clone() + t.Cleanup(func() { def.TLSClientConfig = saved }) + + rec := forgetest.NewRecorder(&mrForge{}) + svc := forgesvc.NewWithForgeBuilder(q, box, 5*time.Second, nil, func(forge.Type, string, string, time.Duration) (forge.Forge, error) { return rec, nil }) + itSvc := issuetrackersvc.New(q, box, func(u string) bool { return u == srv.URL }, issuetracker.New) + svc.SetIssueTrackerDrivers(itSvc) + sealed, err := svc.EncryptToken("forge-token") + if err != nil { + t.Fatal(err) + } + + exec := func(sql string, args ...any) { + t.Helper() + if _, err := pool.Exec(ctx, sql, args...); err != nil { + t.Fatalf("exec %q: %v", sql, err) + } + } + user, conn, repo, worker, runID := uuid.New(), uuid.New(), uuid.New(), uuid.New(), uuid.New() + exec(`INSERT INTO users (id, email, password_hash) VALUES ($1, $2, 'x')`, user, fmt.Sprintf("moved-%s@e2e", user)) + exec(`INSERT INTO forge_connections (id, user_id, forge_type, base_url, bot_username, bot_forge_user_id, token_ciphertext) + VALUES ($1, $2, 'gitlab', $3, 'bot', 1, $4)`, conn, user, "https://"+uuid.NewString()+".forge.test", sealed) + yt, err := itSvc.Connect(ctx, user, "youtrack", srv.URL, srv.Token) + if err != nil { + t.Fatal(err) + } + exec(`INSERT INTO repos (id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, + issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at) + VALUES ($1, $2, 77, $3, 'https://forge.test/g/l', 'main', true, $4, $5, $6, $7, $8, now())`, + repo, conn, "g/moved-"+repo.String(), yt.ID, youtracktest.ProjectID, youtracktest.ProjectShortName, youtracktest.EligibilityTagID, youtracktest.EligibilityTagName) + if err := forgesvc.EnsureBoardColumnRows(ctx, q, repo); err != nil { + t.Fatal(err) + } + issueN := srv.SeedIssue("a youtrack card", "body", youtracktest.EligibilityTagID) + exec(`INSERT INTO issues (repo_id, forge_issue_iid, title, state, labels, web_url, forge_updated_at, synced_at) + VALUES ($1, $2, 't', 'opened', '[]', 'https://yt.test/i', now(), now())`, repo, issueN) + sum := sha256.Sum256([]byte("moved-worker-" + worker.String())) + exec(`INSERT INTO workers(id,user_id,name,token_hash,status) VALUES($1,$2,'moved-worker',$3,'online')`, worker, user, sum[:]) + exec(`INSERT INTO runs (id, user_id, repo_id, kind, issue_iid, issue_title, issue_description, branch, status, auto_approve, + origin_column, worker_id, claim_generation) + VALUES ($1, $2, $3, 'issue', $4, 't', 'd', $5, 'running', false, '', $6, 1)`, runID, user, repo, issueN, "agent/issue-"+runID.String(), worker) + + // The issue moves to another project BEFORE the run completes. + srv.MoveIssue(issueN, youtracktest.OtherProjectID) + srv.ResetRequests() + tr, err := svc.IssueTrackerForRepo(ctx, user, repo) + if err != nil { + t.Fatal(err) + } + if _, err := tr.GetIssue(ctx, 77, issueN); !forgesvc.IssueWriteSkippable(err) { + t.Fatalf("a read of the moved issue = %v, want a skippable not-found", err) + } + srv.ResetRequests() + + // The run completes with its MR: the completion write stamps the move marker. + rows, err := q.SetRunCompleted(ctx, store.SetRunCompletedParams{ + MrIid: pgtype.Int8{Int64: 13, Valid: true}, ID: runID, WorkerID: pgtype.UUID{Bytes: worker, Valid: true}, + }) + if err != nil || rows != 1 { + t.Fatalf("SetRunCompleted rows=%d err=%v", rows, err) + } + marker := func() (pending bool, boardColumn pgtype.Text, mrState pgtype.Text) { + t.Helper() + var since pgtype.Timestamptz + if err := pool.QueryRow(ctx, `SELECT move_pending_since, board_column, mr_state FROM runs WHERE id = $1`, runID).Scan(&since, &boardColumn, &mrState); err != nil { + t.Fatal(err) + } + return since.Valid, boardColumn, mrState + } + if pending, _, _ := marker(); !pending { + t.Fatal("the completion did not stamp the move marker") + } + + l := New(q, svc, "https://uzi.test") + l.SetIssueSources(svc) + l.notifyOnce(ctx, runID, "completed") + + for _, r := range srv.Requests() { + if r.Method != http.MethodGet { + t.Fatalf("an issue write reached YouTrack for a moved issue: %+v", r) + } + } + pending, boardColumn, _ := marker() + if pending { + t.Fatal("the move marker was left set: it would be retried to the give-up window") + } + if boardColumn.Valid { + t.Fatalf("board_column recorded as %q for a skipped move", boardColumn.String) + } + + // The code-host side is independent of the issue: the board-free lane records the MR. + if err := svc.SyncBoardFreeMRStates(ctx, repo, 77, rec); err != nil { + t.Fatal(err) + } + if _, _, mrState := marker(); !mrState.Valid || mrState.String != forge.MRStateOpened { + t.Fatalf("mr_state = %+v, want the MR recorded as opened", mrState) + } + var status string + if err := pool.QueryRow(ctx, `SELECT status FROM runs WHERE id = $1`, runID).Scan(&status); err != nil || status != "completed" { + t.Fatalf("status = %q, %v", status, err) + } +} diff --git a/api/internal/runlifecycle/recorded_calls_test.go b/api/internal/runlifecycle/recorded_calls_test.go new file mode 100644 index 0000000000..d4209fdca3 --- /dev/null +++ b/api/internal/runlifecycle/recorded_calls_test.go @@ -0,0 +1,98 @@ +package runlifecycle + +import ( + "context" + "testing" + "time" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/forgesvc" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// Recorded-call tests (PRD 4173 M4): for a repo with no issue-tracker link, +// pin every forge call the run lifecycle makes. The goldens were captured +// before the tracker-link rerouting; regenerate only deliberately, with +// UZI_UPDATE_GOLDEN=1. + +// moveTracker is the forge the lifecycle sees: it accepts the label and note +// writes a card move and a terminal comment make and returns success. +type moveTracker struct{ forgetest.BaseFake } + +func (*moveTracker) EnsureLabels(context.Context, int64, []forge.Label) error { return nil } +func (*moveTracker) UpdateIssueLabels(context.Context, int64, int64, []string, []string) error { + return nil +} +func (*moveTracker) CreateIssueNote(context.Context, int64, int64, string) (forge.IssueNote, error) { + return forge.IssueNote{}, nil +} + +// cacheStore is the slice of the issue cache the real forgesvc.AutoMove writes. +type cacheStore struct{ forgesvc.IssueStore } + +func (cacheStore) UpsertIssueLabels(_ context.Context, arg store.UpsertIssueLabelsParams) (store.Issue, error) { + return store.Issue{RepoID: arg.RepoID, ForgeIssueIid: arg.ForgeIssueIid, State: arg.State, Labels: arg.Labels}, nil +} + +// recordingMover runs the REAL forgesvc.Service.AutoMove against a recorded +// forge, so the card-move calls are the production ones. +type recordingMover struct { + svc *forgesvc.Service + rec *forgetest.Recorder +} + +func newRecordingMover() *recordingMover { + return &recordingMover{ + svc: forgesvc.New(cacheStore{}, nil, 0, nil), + rec: forgetest.NewRecorder(&moveTracker{}), + } +} + +func (m *recordingMover) ForgeForConnection(string, string, []byte) (forge.Forge, error) { + return m.rec, nil +} + +func (m *recordingMover) AutoMove(ctx context.Context, f forge.IssueTracker, projectID int64, issue store.Issue, cols []store.BoardColumn, target string) (store.Issue, error) { + return m.svc.AutoMove(ctx, f, projectID, issue, cols, target) +} + +func TestRecordedCallsCardMove(t *testing.T) { + runID, repoID := uuid.New(), uuid.New() + fs := &fakeStore{ + moveCtx: moveCtx(repoID, 4, "queued", txt("Later"), nullText()), + issue: issueWith(repoID, 4, "opened", "PRD", "Later"), + columns: defaultCols(), + } + mv := newRecordingMover() + l := New(fs, mv, testOrigin) + l.now = func() time.Time { return testNow } + + l.notifyOnce(context.Background(), runID, "queued") + + if len(fs.recorded) != 1 { + t.Fatalf("the move must reach the recorded write; recorded = %+v", fs.recorded) + } + forgetest.AssertGolden(t, "recorded_calls_runlifecycle_cardmove.golden", mv.rec.Calls()) +} + +func TestRecordedCallsAutopilotTerminalComment(t *testing.T) { + runID, repoID := uuid.New(), uuid.New() + fs := &fakeStore{ + moveCtx: autopilotMoveCtx(repoID, 4, "completed", 42, "https://gitlab.example.com/group/proj"), + issue: issueWith(repoID, 4, "opened", "PRD", "Later"), + columns: defaultCols(), + } + mv := newRecordingMover() + l := New(fs, mv, testOrigin) + l.now = func() time.Time { return testNow } + + l.notifyOnce(context.Background(), runID, "completed") + + if fs.claimCalls != 1 { + t.Fatalf("the terminal comment must be claimed once; claims = %d", fs.claimCalls) + } + forgetest.AssertGolden(t, "recorded_calls_runlifecycle_terminalcomment.golden", mv.rec.Calls()) +} diff --git a/api/internal/runlifecycle/testdata/recorded_calls_runlifecycle_cardmove.golden b/api/internal/runlifecycle/testdata/recorded_calls_runlifecycle_cardmove.golden new file mode 100644 index 0000000000..e5ef3bc8c4 --- /dev/null +++ b/api/internal/runlifecycle/testdata/recorded_calls_runlifecycle_cardmove.golden @@ -0,0 +1,2 @@ +EnsureLabels(77, [{Name:"In Progress" Color:"#1f75cb"}]) +UpdateIssueLabels(77, 4, ["In Progress"], ["Later"]) diff --git a/api/internal/runlifecycle/testdata/recorded_calls_runlifecycle_terminalcomment.golden b/api/internal/runlifecycle/testdata/recorded_calls_runlifecycle_terminalcomment.golden new file mode 100644 index 0000000000..f2bf7ba54d --- /dev/null +++ b/api/internal/runlifecycle/testdata/recorded_calls_runlifecycle_terminalcomment.golden @@ -0,0 +1,3 @@ +EnsureLabels(77, [{Name:"Human Review" Color:"#6e49cb"}]) +UpdateIssueLabels(77, 4, ["Human Review"], ["Later"]) +CreateIssueNote(77, 4, "**Autopilot opened a merge request.**\n\nAutopilot finished this issue and opened merge request !42 for your review: https://gitlab.example.com/group/proj/-/merge_requests/42") diff --git a/api/internal/schedsvc/linked_test.go b/api/internal/schedsvc/linked_test.go new file mode 100644 index 0000000000..03ea99536d --- /dev/null +++ b/api/internal/schedsvc/linked_test.go @@ -0,0 +1,131 @@ +package schedsvc + +import ( + "context" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgtype" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// PRD #4173 M4 (DL-5): a schedule on a repo linked to an external issue tracker reads +// issues through the resolver; the forge is never used for them, and a scheduler with +// no resolver fails closed for a linked repo. + +type fakeSources struct { + tr forge.IssueTracker + err error + users []uuid.UUID + repos []uuid.UUID +} + +func (f *fakeSources) IssueTrackerForRepo(_ context.Context, userID, repoID uuid.UUID) (forge.IssueTracker, error) { + f.users = append(f.users, userID) + f.repos = append(f.repos, repoID) + return f.tr, f.err +} + +// linkedHarness is newHarness with the repo linked and a resolver whose tracker is a +// separate fake from the forge, so a read shows which side served it. +func linkedHarness(withSources bool) (*harness, *fakeForge, *fakeSources) { + h := newHarness() + h.st.repoRow.IssueTrackerConnectionID = pgtype.UUID{Bytes: uuid.New(), Valid: true} + tr := &fakeForge{issue: forge.Issue{IID: 7, Description: "tracker body", Labels: []string{"PRD"}}, createdIID: 7} + src := &fakeSources{tr: tr} + if withSources { + h.sched.SetIssueSources(src) + } + return h, tr, src +} + +func TestLinkedFireIssueReadsThroughResolver(t *testing.T) { + h, tr, src := linkedHarness(true) + out, err := h.sched.RunNow(context.Background(), h.issueSchedule()) + if err != nil || len(out.Started) != 1 { + t.Fatalf("fire did not start a run: out=%+v err=%v", out, err) + } + if len(tr.getIID) != 1 || len(h.fb.f.getIID) != 0 { + t.Fatalf("the issue was read from the forge (%v) instead of the resolver (%v)", h.fb.f.getIID, tr.getIID) + } + if len(src.users) != 1 || src.users[0] != h.owner || src.repos[0] != h.repoID { + t.Fatalf("the resolver is bound by owner and repo row, got %v %v", src.users, src.repos) + } +} + +func TestLinkedFireSweepReadsThroughResolver(t *testing.T) { + h, tr, _ := linkedHarness(true) + h.st.sweepRows = []store.ListSweepCandidateIssuesRow{{ForgeIssueIid: 1}, {ForgeIssueIid: 2}} + out, err := h.sched.RunNow(context.Background(), h.sweepSchedule(pgtype.Int4{Int32: 2, Valid: true})) + if err != nil || len(out.Started) != 2 { + t.Fatalf("sweep did not start two runs: out=%+v err=%v", out, err) + } + if len(tr.getIID) != 2 || len(h.fb.f.getIID) != 0 { + t.Fatalf("sweep read forge=%v resolver=%v", h.fb.f.getIID, tr.getIID) + } +} + +func TestLinkedFireWithoutSourcesFailsClosed(t *testing.T) { + for name, sched := range map[string]func(*harness) store.RunSchedule{ + "issue": func(h *harness) store.RunSchedule { return h.issueSchedule() }, + "sweep": func(h *harness) store.RunSchedule { return h.sweepSchedule(pgtype.Int4{}) }, + } { + t.Run(name, func(t *testing.T) { + h, _, _ := linkedHarness(false) + h.st.sweepRows = []store.ListSweepCandidateIssuesRow{{ForgeIssueIid: 1}} + _, err := h.sched.fireOne(context.Background(), sched(h)) + if err == nil { + t.Fatal("a linked repo with no resolver fired") + } + if len(h.fb.f.getIID) != 0 || h.runsCreated() != 0 { + t.Fatalf("fell back to the forge: GetIssue=%v runs=%d", h.fb.f.getIID, h.runsCreated()) + } + }) + } +} + +func TestLinkedProposalDigestReadsThroughResolver(t *testing.T) { + h, tr, _ := linkedHarness(true) + tr.listResult = []forge.Issue{{IID: 3, Title: "an earlier proposal", State: "opened"}} + h.fb.f.listResult = []forge.Issue{{IID: 9, Title: "forge issue", State: "opened"}} + got := h.sched.proposalDigestSection(context.Background(), h.issueSchedule()) + if tr.listCount != 1 || h.fb.f.listCount != 0 || got == "" { + t.Fatalf("resolver lists=%d forge lists=%d digest=%q", tr.listCount, h.fb.f.listCount, got) + } +} + +func TestLinkedProposalDigestWithoutSourcesIsSkipped(t *testing.T) { + h, _, _ := linkedHarness(false) + h.fb.f.listResult = []forge.Issue{{IID: 9, Title: "forge issue", State: "opened"}} + if got := h.sched.proposalDigestSection(context.Background(), h.issueSchedule()); got != "" || h.fb.f.listCount != 0 { + t.Fatalf("digest=%q forge lists=%d: a linked repo must not list the forge", got, h.fb.f.listCount) + } +} + +func TestLinkedSelfImproveIsSkippedWithoutAnyIssueWrite(t *testing.T) { + h, tr, _ := linkedHarness(true) + out, err := h.sched.fireOne(context.Background(), h.selfImproveSchedule()) + if err != nil { + t.Fatal(err) + } + if len(out.Skips) != 1 || out.Skips[0].Reason != SkipConfigNotSupported { + t.Fatalf("out = %+v, want one config_not_supported skip", out) + } + if h.fb.f.createCount != 0 || h.fb.f.listCount != 0 || tr.createCount != 0 || tr.listCount != 0 || len(h.runs.selfImprove) != 0 { + t.Fatal("self_improve touched an issue tracker or started a run on a linked repo") + } +} + +func TestUnlinkedFireNeverConsultsResolver(t *testing.T) { + h := newHarness() + src := &fakeSources{tr: &fakeForge{}} + h.sched.SetIssueSources(src) + if _, err := h.sched.RunNow(context.Background(), h.issueSchedule()); err != nil { + t.Fatal(err) + } + if len(src.users) != 0 || len(h.fb.f.getIID) != 1 { + t.Fatalf("resolver calls=%d forge GetIssue=%v", len(src.users), h.fb.f.getIID) + } +} diff --git a/api/internal/schedsvc/never_enabled_test.go b/api/internal/schedsvc/never_enabled_test.go new file mode 100644 index 0000000000..f32a38e268 --- /dev/null +++ b/api/internal/schedsvc/never_enabled_test.go @@ -0,0 +1,61 @@ +package schedsvc + +import ( + "context" + "errors" + "testing" + + "github.com/jackc/pgx/v5/pgtype" + + "github.com/vtmocanu/uzi/api/internal/store" + "github.com/vtmocanu/uzi/api/internal/workersvc" +) + +// PRD 4173 D13: a schedule on a repo that has never been enabled fires nothing (issue, sweep, +// prompt and self_improve targets). The +// refusal comes before any forge driver is built: the builder fails the build with a +// different error, so a build would show as the wrong error, and the forge fake's call +// counters stay at zero. advance treats the error as transient (retried, never parked). +func TestFireOnNeverEnabledRepoMakesNoForgeCall(t *testing.T) { + for _, tc := range []struct { + name string + sched func(h *harness) store.RunSchedule + }{ + {"issue", func(h *harness) store.RunSchedule { return h.issueSchedule() }}, + {"sweep", func(h *harness) store.RunSchedule { return h.sweepSchedule(pgtype.Int4{}) }}, + {"prompt", func(h *harness) store.RunSchedule { + s := h.issueSchedule() + s.Target = "prompt" + s.IssueIid = pgtype.Int8{} + s.Prompt = pgtype.Text{String: "do a thing", Valid: true} + return s + }}, + {"self_improve", func(h *harness) store.RunSchedule { return h.selfImproveSchedule() }}, + } { + t.Run(tc.name, func(t *testing.T) { + h := newHarness() + h.st.repoRow.AwaitingFirstEnable = true + h.fb.err = errors.New("the forge driver must not be built for a never-enabled repo") + sched := tc.sched(h) + + _, err := h.sched.fireOne(context.Background(), sched) + if !errors.Is(err, workersvc.ErrRepoNeverEnabled) { + t.Fatalf("fireOne error = %v, want ErrRepoNeverEnabled", err) + } + f := h.fb.f + if len(f.getIID) != 0 || f.listCount != 0 || f.createCount != 0 || len(f.getMRIID) != 0 { + t.Errorf("forge calls: GetIssue=%v ListIssues=%d CreateIssue=%d GetMergeRequest=%v; want none", + f.getIID, f.listCount, f.createCount, f.getMRIID) + } + if h.runsCreated() != 0 || len(h.runs.selfImprove) != 0 { + t.Errorf("runs created = %d, self-improve runs = %d; want none", h.runsCreated(), len(h.runs.selfImprove)) + } + + // The advance rule: transient, so the schedule is not parked and not advanced. + h.sched.advance(context.Background(), sched, FireOutcome{}, err) + if len(h.st.statusCalls) != 0 { + t.Errorf("schedule status changed (%+v); a never-enabled repo must stay transient", h.st.statusCalls) + } + }) + } +} diff --git a/api/internal/schedsvc/recorded_calls_test.go b/api/internal/schedsvc/recorded_calls_test.go new file mode 100644 index 0000000000..e14378794f --- /dev/null +++ b/api/internal/schedsvc/recorded_calls_test.go @@ -0,0 +1,57 @@ +package schedsvc + +import ( + "context" + "testing" + "time" + + "github.com/jackc/pgx/v5/pgtype" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// Recorded-call tests (PRD 4173 M4): for a repo with no issue-tracker link, +// pin every forge call a schedule fire makes. The goldens were captured before +// the tracker-link rerouting; regenerate only deliberately, with +// UZI_UPDATE_GOLDEN=1. + +// recordingBuilder hands the scheduler a Recorder around the harness forge. +type recordingBuilder struct { + *fakeBuilder + rec *forgetest.Recorder +} + +func (b recordingBuilder) ForgeForConnection(string, string, []byte) (forge.Forge, error) { + return b.rec, nil +} + +// newRecordedHarness is newHarness with the scheduler's forge wrapped in a +// Recorder. The wrapped fakeForge is unchanged. +func newRecordedHarness() (*harness, *forgetest.Recorder) { + h := newHarness() + rec := forgetest.NewRecorder(h.fb.f) + h.sched = New(h.st, h.runs, recordingBuilder{h.fb, rec}, h.set, h.notif, h.vault, time.Minute, nil) + h.sched.now = func() time.Time { return h.now } + return h, rec +} + +func TestRecordedCallsFireIssue(t *testing.T) { + h, rec := newRecordedHarness() + out, err := h.sched.RunNow(context.Background(), h.issueSchedule()) + if err != nil || len(out.Started) != 1 { + t.Fatalf("fire did not start a run: out=%+v err=%v", out, err) + } + forgetest.AssertGolden(t, "recorded_calls_schedsvc_fireissue.golden", rec.Calls()) +} + +func TestRecordedCallsFireSweep(t *testing.T) { + h, rec := newRecordedHarness() + h.st.sweepRows = []store.ListSweepCandidateIssuesRow{{ForgeIssueIid: 1}, {ForgeIssueIid: 2}} + out, err := h.sched.RunNow(context.Background(), h.sweepSchedule(pgtype.Int4{Int32: 2, Valid: true})) + if err != nil || len(out.Started) != 2 { + t.Fatalf("sweep did not start two runs: out=%+v err=%v", out, err) + } + forgetest.AssertGolden(t, "recorded_calls_schedsvc_firesweep.golden", rec.Calls()) +} diff --git a/api/internal/schedsvc/scheduler.go b/api/internal/schedsvc/scheduler.go index c3799b7846..be1e3ab317 100644 --- a/api/internal/schedsvc/scheduler.go +++ b/api/internal/schedsvc/scheduler.go @@ -30,6 +30,7 @@ import ( "github.com/vtmocanu/uzi/api/internal/forge" "github.com/vtmocanu/uzi/api/internal/issueinput" + "github.com/vtmocanu/uzi/api/internal/issuesource" "github.com/vtmocanu/uzi/api/internal/notifysvc" "github.com/vtmocanu/uzi/api/internal/pgconv" "github.com/vtmocanu/uzi/api/internal/schedtmpl" @@ -215,6 +216,10 @@ type Scheduler struct { forge ForgeBuilder settings SettingsReader notifier Notifier + // issueSources resolves the issue side of a repo linked to an external issue + // tracker (PRD #4173 DL-5); SetIssueSources. Nil ⇒ a linked repo fails closed + // (issuesource.ErrNotWired); an unlinked repo keeps using the forge driver. + issueSources issuesource.Sources // vault gates the self_improve fire path (PRD #590 M1): a locked owner vault yields a // benign skip so the run never tries to spend a token it cannot unwrap. nil ⇒ always // unlocked. @@ -257,6 +262,17 @@ func New(st Store, runs RunCreator, fb ForgeBuilder, set SettingsReader, notifie } } +// SetIssueSources wires the resolver of a linked repo's issue side (PRD #4173 DL-5). +// Call once at startup with the *forgesvc.Service the ForgeBuilder is. +func (e *Scheduler) SetIssueSources(is issuesource.Sources) { e.issueSources = is } + +// issueSide is the issue tracker for repo: the forge driver f for an unlinked repo +// (as before M4), the resolver's for a linked one, an error for a linked repo when no +// resolver is wired. +func (e *Scheduler) issueSide(ctx context.Context, repo store.GetRepoForUserRow, f forge.Forge) (forge.IssueTracker, error) { + return issuesource.For(ctx, e.issueSources, repo.IssueTrackerConnectionID.Valid, repo.UserID, repo.ID, f) +} + // SetBeat wires the admin-health loop-beat callback (PRD #1484 M2). Call once at startup, // before Run. A nil beat (the default) disables it, so tests and the run-now-only Handler // scheduler that never wire one behave exactly as before. The callback fires once per Run @@ -591,7 +607,11 @@ func (e *Scheduler) fireIssue(ctx context.Context, sched store.RunSchedule) (Fir return FireOutcome{Matched: 1, Skips: []Skip{{IssueIID: &iidCopy, Reason: SkipAlreadyRunning}}}, nil } - capture, err := issueinput.Fetch(ctx, f, f, repo.ForgeProjectID, iid, repo.BotForgeUserID) + tr, err := e.issueSide(ctx, repo, f) + if err != nil { + return FireOutcome{}, fmt.Errorf("resolve issue tracker: %w", err) // transient: retry next tick + } + capture, err := issueinput.Fetch(ctx, f, tr, repo.ForgeProjectID, iid, repo.BotForgeUserID) if err != nil { if forge.IsIssueResponseTooLarge(err) { iidCopy := iid @@ -711,6 +731,10 @@ func (e *Scheduler) fireSweep(ctx context.Context, sched store.RunSchedule) (Fir if err != nil { return FireOutcome{}, err } + tr, err := e.issueSide(ctx, repo, f) + if err != nil { + return FireOutcome{}, fmt.Errorf("resolve issue tracker: %w", err) // transient: retry next tick + } // Resolve the candidate-query selector params by kind. The assigned kind must NOT go // through resolveSweepLabels — that defaults an empty selector to the uzi label (the @@ -803,7 +827,7 @@ func (e *Scheduler) fireSweep(ctx context.Context, sched store.RunSchedule) (Fir out.Skips = append(out.Skips, Skip{IssueIID: &iidCopy, Reason: SkipAlreadyRunning}) continue } - capture, err := issueinput.Fetch(ctx, f, f, repo.ForgeProjectID, iid, repo.BotForgeUserID) + capture, err := issueinput.Fetch(ctx, f, tr, repo.ForgeProjectID, iid, repo.BotForgeUserID) if err != nil { e.logger.Warn("scheduler: sweep fetch issue", "schedule", sched.ID.String(), "issue", iid, "error", err) reason := SkipFetchFailed @@ -866,7 +890,7 @@ func (e *Scheduler) fireSweep(ctx context.Context, sched store.RunSchedule) (Fir var cached store.Issue cached, removeErr = e.store.GetIssueByIID(ctx, store.GetIssueByIIDParams{RepoID: repo.ID, ForgeIssueIid: iid}) if removeErr == nil { - _, removeErr = e.forge.SetIssueLabel(ctx, f, repo.ForgeProjectID, cached, selectorLabel, "", false) + _, removeErr = e.forge.SetIssueLabel(ctx, tr, repo.ForgeProjectID, cached, selectorLabel, "", false) } } for i := range res.Started { @@ -908,6 +932,14 @@ func (e *Scheduler) firePrompt(ctx context.Context, sched store.RunSchedule) (Fi // column) — the resolver engages ONLY for origin='default', so existing schedules are // byte-for-byte unchanged. Resolution runs BEFORE deriving the title so the run-view // header labels the resolved prompt, not the (NULL) column. + // PRD 4173 D13: no run starts on a never-enabled repo. Checked first, before the + // active-run lookup and any forge read; transient to advance, like resolveRepoForge. + // A repo read that fails is left to the create seam below, which reports it. + if repo, rerr := e.store.GetRepoForUser(ctx, store.GetRepoForUserParams{ID: sched.RepoID, UserID: sched.UserID}); rerr == nil { + if err := store.RefuseNeverEnabled(repo.AwaitingFirstEnable); err != nil { + return FireOutcome{}, err + } + } prompt := sched.Prompt.String if sched.Origin == "default" { job, ok := e.catalog(sched.CatalogSlug.String) @@ -1158,6 +1190,14 @@ func (e *Scheduler) resolveRepoForge(ctx context.Context, sched store.RunSchedul } return store.GetRepoForUserRow{}, nil, err } + // PRD 4173 D13: a never-enabled repo is refused here, before any forge driver is built, + // for every fire that resolves its forge through this function. firePrompt checks the + // marker itself (it may need no forge at all). The error is transient to advance + // (logged, retried next tick, never parked), so the schedule fires once the repo is + // enabled. + if err := store.RefuseNeverEnabled(repo.AwaitingFirstEnable); err != nil { + return store.GetRepoForUserRow{}, nil, err + } f, err := e.forge.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { return store.GetRepoForUserRow{}, nil, fmt.Errorf("build forge driver: %w", err) // transient @@ -1419,7 +1459,11 @@ func (e *Scheduler) proposalDigestSection(ctx context.Context, sched store.RunSc e.logger.Warn("scheduler: proposal digest skipped, cannot resolve repo/forge", "schedule", sched.ID.String(), "error", err) return "" } - var tr forge.IssueTracker = f // only the issue-tracker half is needed here + tr, err := e.issueSide(ctx, repo, f) // only the issue-tracker half is needed here + if err != nil { + e.logger.Warn("scheduler: proposal digest skipped, cannot resolve issue tracker", "schedule", sched.ID.String(), "error", err) + return "" + } // The SAME label workersvc files a proposal under (single source of truth), so the // digest lists exactly this job's own proposal history. label := workersvc.ProposalLabel(catalogSlugOf(sched)) diff --git a/api/internal/schedsvc/self_improve.go b/api/internal/schedsvc/self_improve.go index 95522c77dd..9bda3b6373 100644 --- a/api/internal/schedsvc/self_improve.go +++ b/api/internal/schedsvc/self_improve.go @@ -85,6 +85,20 @@ func (e *Scheduler) fireSelfImprove(ctx context.Context, sched store.RunSchedule } return FireOutcome{}, err // transient DB error: retry next tick } + // PRD 4173 D13: a never-enabled repo is skipped (transient to advance: logged, retried + // next tick, never parked) before any forge call or tracking-issue write. + if err := store.RefuseNeverEnabled(repo.AwaitingFirstEnable); err != nil { + return FireOutcome{}, err + } + + // PRD #4173 M4: self-improve files a tracking issue on the repo's issue tracker, and a + // linked repo has none to write to until M5 (the write guard would refuse it anyway). + // Skipped as a benign, logged no-op before any forge call; never falls back to the forge. + if repo.IssueTrackerConnectionID.Valid { + e.logger.Info("scheduler: self_improve skipped, issue creation is not offered on a repo linked to an issue tracker", + "schedule", sched.ID.String(), "repo", repo.ID.String()) + return FireOutcome{Matched: 1, Skips: []Skip{{Reason: SkipConfigNotSupported}}}, nil + } // 2. Active-run pre-check, per repo. On a DB error retry next tick (transient). If a run // is already active for this repo, benign skip — no notification (mirrors firePrompt's diff --git a/api/internal/schedsvc/testdata/recorded_calls_schedsvc_fireissue.golden b/api/internal/schedsvc/testdata/recorded_calls_schedsvc_fireissue.golden new file mode 100644 index 0000000000..f7ffdfb025 --- /dev/null +++ b/api/internal/schedsvc/testdata/recorded_calls_schedsvc_fireissue.golden @@ -0,0 +1,3 @@ +GetIssue(42, 7) +RepositoryAuthorEligibility(42, 2) +ListIssueComments(42, 7) diff --git a/api/internal/schedsvc/testdata/recorded_calls_schedsvc_firesweep.golden b/api/internal/schedsvc/testdata/recorded_calls_schedsvc_firesweep.golden new file mode 100644 index 0000000000..2bfa51e942 --- /dev/null +++ b/api/internal/schedsvc/testdata/recorded_calls_schedsvc_firesweep.golden @@ -0,0 +1,6 @@ +GetIssue(42, 1) +RepositoryAuthorEligibility(42, 2) +ListIssueComments(42, 1) +GetIssue(42, 2) +RepositoryAuthorEligibility(42, 2) +ListIssueComments(42, 2) diff --git a/api/internal/slacksvc/chatactions.go b/api/internal/slacksvc/chatactions.go index 1a42ba4a18..ef6635813d 100644 --- a/api/internal/slacksvc/chatactions.go +++ b/api/internal/slacksvc/chatactions.go @@ -68,6 +68,9 @@ var ( ErrChatProposalGone = errors.New("slack: proposal not found or not yours") ErrChatProposalHandled = errors.New("slack: proposal already resolved") ErrChatProposalForge = errors.New("slack: forge rejected the issue") + // ErrChatProposalRepoNeverEnabled: the proposal's repo has never been enabled (PRD 4173 + // D13), so nothing is filed until the user enables it. + ErrChatProposalRepoNeverEnabled = errors.New("slack: the proposal's repo has never been enabled") // Lifecycle sentinels (PRD #191 M6). ErrChatGone = errors.New("slack: chat not found or not yours") ErrChatNotEndedYet = errors.New("slack: chat is still active") @@ -398,6 +401,8 @@ func (c *ChatActions) createProposal(ctx context.Context, a BlockAction, userID, c.editCard(ctx, a, "This proposal was already handled.", "") case errors.Is(err, ErrChatProposalGone): c.ephemeral(ctx, a, "That proposal isn't yours, or it no longer exists.") + case errors.Is(err, ErrChatProposalRepoNeverEnabled): + c.ephemeral(ctx, a, "That repo has never been enabled in uzi, so no issue was filed. Enable the repo first, then press Create again.") case errors.Is(err, ErrChatProposalForge): c.ephemeral(ctx, a, "Couldn't file the issue on the forge — the proposal is back to pending, so press Create again or open it in uzi.") default: diff --git a/api/internal/store/forge.sql.go b/api/internal/store/forge.sql.go index 9838e459e9..fbc84eedcf 100644 --- a/api/internal/store/forge.sql.go +++ b/api/internal/store/forge.sql.go @@ -174,7 +174,7 @@ SET guardrail_override_reason = NULL, guardrail_override_by = NULL, guardrail_override_at = NULL WHERE id = $1 -RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` // PRD #66 M8 (D8): revoke the admin per-repo override, re-arming the guardrail @@ -204,6 +204,13 @@ func (q *Queries) ClearRepoGuardrailOverride(ctx context.Context, id uuid.UUID) &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -505,7 +512,7 @@ func (q *Queries) GetForgeConnectionForUser(ctx context.Context, arg GetForgeCon } const getIssueByIID = `-- name: GetIssueByIID :one -SELECT id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids FROM issues WHERE repo_id = $1 AND forge_issue_iid = $2 +SELECT id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids, issue_key FROM issues WHERE repo_id = $1 AND forge_issue_iid = $2 ` type GetIssueByIIDParams struct { @@ -530,6 +537,7 @@ func (q *Queries) GetIssueByIID(ctx context.Context, arg GetIssueByIIDParams) (I &i.SyncedAt, &i.BoardPosition, &i.AssigneeIds, + &i.IssueKey, ) return i, err } @@ -672,6 +680,8 @@ func (q *Queries) GetLatestRunForIssue(ctx context.Context, arg GetLatestRunForI const getRepoByID = `-- name: GetRepoByID :one SELECT r.id, r.connection_id, r.forge_project_id, r.path_with_namespace, r.web_url, r.default_branch, r.enabled, + r.issue_tracker_connection_id, r.issue_tracker_project_id, r.issue_tracker_project_short, + r.eligibility_tag_id, r.eligibility_tag_name, r.issue_tracker_linked_at, r.awaiting_first_enable, c.forge_type, c.base_url, c.token_ciphertext, c.user_id FROM repos r JOIN forge_connections c ON c.id = r.connection_id @@ -679,17 +689,24 @@ WHERE r.id = $1 ` type GetRepoByIDRow struct { - ID uuid.UUID `json:"id"` - ConnectionID uuid.UUID `json:"connection_id"` - ForgeProjectID int64 `json:"forge_project_id"` - PathWithNamespace string `json:"path_with_namespace"` - WebUrl string `json:"web_url"` - DefaultBranch pgtype.Text `json:"default_branch"` - Enabled bool `json:"enabled"` - ForgeType string `json:"forge_type"` - BaseUrl string `json:"base_url"` - TokenCiphertext []byte `json:"token_ciphertext"` - UserID uuid.UUID `json:"user_id"` + ID uuid.UUID `json:"id"` + ConnectionID uuid.UUID `json:"connection_id"` + ForgeProjectID int64 `json:"forge_project_id"` + PathWithNamespace string `json:"path_with_namespace"` + WebUrl string `json:"web_url"` + DefaultBranch pgtype.Text `json:"default_branch"` + Enabled bool `json:"enabled"` + IssueTrackerConnectionID pgtype.UUID `json:"issue_tracker_connection_id"` + IssueTrackerProjectID pgtype.Text `json:"issue_tracker_project_id"` + IssueTrackerProjectShort pgtype.Text `json:"issue_tracker_project_short"` + EligibilityTagID pgtype.Text `json:"eligibility_tag_id"` + EligibilityTagName pgtype.Text `json:"eligibility_tag_name"` + IssueTrackerLinkedAt pgtype.Timestamptz `json:"issue_tracker_linked_at"` + AwaitingFirstEnable bool `json:"awaiting_first_enable"` + ForgeType string `json:"forge_type"` + BaseUrl string `json:"base_url"` + TokenCiphertext []byte `json:"token_ciphertext"` + UserID uuid.UUID `json:"user_id"` } // One repo plus the connection fields needed to build a forge client, UNSCOPED by @@ -715,6 +732,13 @@ func (q *Queries) GetRepoByID(ctx context.Context, id uuid.UUID) (GetRepoByIDRow &i.WebUrl, &i.DefaultBranch, &i.Enabled, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, + &i.AwaitingFirstEnable, &i.ForgeType, &i.BaseUrl, &i.TokenCiphertext, @@ -727,6 +751,8 @@ const getRepoForUser = `-- name: GetRepoForUser :one SELECT r.id, r.connection_id, r.forge_project_id, r.path_with_namespace, r.web_url, r.default_branch, r.enabled, r.fold_improve_uzi_backlog, r.guardrail_override_reason, r.guardrail_override_by, r.guardrail_override_at, + r.issue_tracker_connection_id, r.issue_tracker_project_id, r.issue_tracker_project_short, + r.eligibility_tag_id, r.eligibility_tag_name, r.issue_tracker_linked_at, r.awaiting_first_enable, c.forge_type, c.base_url, c.token_ciphertext, c.user_id, c.bot_forge_user_id FROM repos r JOIN forge_connections c ON c.id = r.connection_id @@ -739,22 +765,29 @@ type GetRepoForUserParams struct { } type GetRepoForUserRow struct { - ID uuid.UUID `json:"id"` - ConnectionID uuid.UUID `json:"connection_id"` - ForgeProjectID int64 `json:"forge_project_id"` - PathWithNamespace string `json:"path_with_namespace"` - WebUrl string `json:"web_url"` - DefaultBranch pgtype.Text `json:"default_branch"` - Enabled bool `json:"enabled"` - FoldImproveUziBacklog bool `json:"fold_improve_uzi_backlog"` - GuardrailOverrideReason pgtype.Text `json:"guardrail_override_reason"` - GuardrailOverrideBy pgtype.UUID `json:"guardrail_override_by"` - GuardrailOverrideAt pgtype.Timestamptz `json:"guardrail_override_at"` - ForgeType string `json:"forge_type"` - BaseUrl string `json:"base_url"` - TokenCiphertext []byte `json:"token_ciphertext"` - UserID uuid.UUID `json:"user_id"` - BotForgeUserID int64 `json:"bot_forge_user_id"` + ID uuid.UUID `json:"id"` + ConnectionID uuid.UUID `json:"connection_id"` + ForgeProjectID int64 `json:"forge_project_id"` + PathWithNamespace string `json:"path_with_namespace"` + WebUrl string `json:"web_url"` + DefaultBranch pgtype.Text `json:"default_branch"` + Enabled bool `json:"enabled"` + FoldImproveUziBacklog bool `json:"fold_improve_uzi_backlog"` + GuardrailOverrideReason pgtype.Text `json:"guardrail_override_reason"` + GuardrailOverrideBy pgtype.UUID `json:"guardrail_override_by"` + GuardrailOverrideAt pgtype.Timestamptz `json:"guardrail_override_at"` + IssueTrackerConnectionID pgtype.UUID `json:"issue_tracker_connection_id"` + IssueTrackerProjectID pgtype.Text `json:"issue_tracker_project_id"` + IssueTrackerProjectShort pgtype.Text `json:"issue_tracker_project_short"` + EligibilityTagID pgtype.Text `json:"eligibility_tag_id"` + EligibilityTagName pgtype.Text `json:"eligibility_tag_name"` + IssueTrackerLinkedAt pgtype.Timestamptz `json:"issue_tracker_linked_at"` + AwaitingFirstEnable bool `json:"awaiting_first_enable"` + ForgeType string `json:"forge_type"` + BaseUrl string `json:"base_url"` + TokenCiphertext []byte `json:"token_ciphertext"` + UserID uuid.UUID `json:"user_id"` + BotForgeUserID int64 `json:"bot_forge_user_id"` } // One repo plus the connection fields needed to build a forge client, scoped to @@ -777,6 +810,13 @@ func (q *Queries) GetRepoForUser(ctx context.Context, arg GetRepoForUserParams) &i.GuardrailOverrideReason, &i.GuardrailOverrideBy, &i.GuardrailOverrideAt, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, + &i.AwaitingFirstEnable, &i.ForgeType, &i.BaseUrl, &i.TokenCiphertext, @@ -905,7 +945,8 @@ const listBoardFreeMRStateWatchCandidates = `-- name: ListBoardFreeMRStateWatchC SELECT id, branch, mr_iid, mr_state FROM runs WHERE repo_id = $1::uuid - AND (issue_iid IS NULL OR kind IN ('prompt', 'self_improve')) + AND (issue_iid IS NULL OR kind IN ('prompt', 'self_improve') + OR EXISTS (SELECT 1 FROM repos lr WHERE lr.id = $1::uuid AND lr.issue_tracker_connection_id IS NOT NULL)) AND status = 'completed' AND mr_iid IS NOT NULL AND (mr_state IS NULL OR mr_state IN ('opened', 'locked')) @@ -934,7 +975,10 @@ type ListBoardFreeMRStateWatchCandidatesRow struct { // gate, the mr_rework ledger eviction, and stop-on-close cancellation all work for these runs. // Self-bounding like Lane B: poll a run only until its mr_state reaches a terminal value // (merged/closed), then it drops out of this set. LIMIT 100 is a hardcoded burst bound (mirrors -// ListMRWatchCandidates), not a sqlc param, so zero Go signature change. Keep this rationale +// ListMRWatchCandidates), not a sqlc param, so zero Go signature change. A repo linked to an +// external issue tracker (PRD #4173 M4) is widened IN whole: every issue-lane run of such a repo +// belongs here (the board lane excludes the repo), so linked runs record mr_state and cancel +// rework with no board card; an unlinked repo matches exactly as before. Keep this rationale // ABOVE the statement — a comment trailing after the `;` is grabbed by sqlc as the NEXT query's doc. func (q *Queries) ListBoardFreeMRStateWatchCandidates(ctx context.Context, repoID uuid.UUID) ([]ListBoardFreeMRStateWatchCandidatesRow, error) { rows, err := q.db.Query(ctx, listBoardFreeMRStateWatchCandidates, repoID) @@ -986,7 +1030,7 @@ func (q *Queries) ListEnabledRepoIDs(ctx context.Context) ([]uuid.UUID, error) { } const listEnabledReposByConnection = `-- name: ListEnabledReposByConnection :many -SELECT id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default FROM repos WHERE connection_id = $1 AND enabled = true +SELECT id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at FROM repos WHERE connection_id = $1 AND enabled = true ORDER BY path_with_namespace ASC ` @@ -1022,6 +1066,13 @@ func (q *Queries) ListEnabledReposByConnection(ctx context.Context, connectionID &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ); err != nil { return nil, err } @@ -1034,7 +1085,7 @@ func (q *Queries) ListEnabledReposByConnection(ctx context.Context, connectionID } const listEnabledReposForUser = `-- name: ListEnabledReposForUser :many -SELECT r.id, r.connection_id, r.forge_project_id, r.path_with_namespace, r.web_url, r.default_branch, r.enabled, r.repo_skills_enabled, r.repo_devbox_opt_in, r.repo_claudemd_enabled, r.guardrail_override_reason, r.guardrail_override_by, r.guardrail_override_at, r.required_capabilities, r.fold_improve_uzi_backlog, r.code_cross_check_guidance, r.cross_check_repo_instructions_enabled, r.ignored_inferred_capabilities, r.agent_source_default FROM repos r +SELECT r.id, r.connection_id, r.forge_project_id, r.path_with_namespace, r.web_url, r.default_branch, r.enabled, r.repo_skills_enabled, r.repo_devbox_opt_in, r.repo_claudemd_enabled, r.guardrail_override_reason, r.guardrail_override_by, r.guardrail_override_at, r.required_capabilities, r.fold_improve_uzi_backlog, r.code_cross_check_guidance, r.cross_check_repo_instructions_enabled, r.ignored_inferred_capabilities, r.agent_source_default, r.awaiting_first_enable, r.issue_tracker_connection_id, r.issue_tracker_project_id, r.issue_tracker_project_short, r.eligibility_tag_id, r.eligibility_tag_name, r.issue_tracker_linked_at FROM repos r JOIN forge_connections c ON c.id = r.connection_id WHERE c.user_id = $1 AND r.enabled = true ORDER BY r.path_with_namespace ASC @@ -1070,6 +1121,13 @@ func (q *Queries) ListEnabledReposForUser(ctx context.Context, userID uuid.UUID) &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ); err != nil { return nil, err } @@ -1084,6 +1142,8 @@ func (q *Queries) ListEnabledReposForUser(ctx context.Context, userID uuid.UUID) const listEnabledReposWithConnections = `-- name: ListEnabledReposWithConnections :many SELECT r.id, r.connection_id, r.forge_project_id, r.path_with_namespace, r.web_url, r.default_branch, r.enabled, + r.issue_tracker_connection_id, r.issue_tracker_project_id, r.issue_tracker_project_short, + r.eligibility_tag_id, r.eligibility_tag_name, r.issue_tracker_linked_at, r.awaiting_first_enable, c.forge_type, c.base_url, c.token_ciphertext, c.user_id FROM repos r JOIN forge_connections c ON c.id = r.connection_id @@ -1091,17 +1151,24 @@ WHERE r.enabled = true ` type ListEnabledReposWithConnectionsRow struct { - ID uuid.UUID `json:"id"` - ConnectionID uuid.UUID `json:"connection_id"` - ForgeProjectID int64 `json:"forge_project_id"` - PathWithNamespace string `json:"path_with_namespace"` - WebUrl string `json:"web_url"` - DefaultBranch pgtype.Text `json:"default_branch"` - Enabled bool `json:"enabled"` - ForgeType string `json:"forge_type"` - BaseUrl string `json:"base_url"` - TokenCiphertext []byte `json:"token_ciphertext"` - UserID uuid.UUID `json:"user_id"` + ID uuid.UUID `json:"id"` + ConnectionID uuid.UUID `json:"connection_id"` + ForgeProjectID int64 `json:"forge_project_id"` + PathWithNamespace string `json:"path_with_namespace"` + WebUrl string `json:"web_url"` + DefaultBranch pgtype.Text `json:"default_branch"` + Enabled bool `json:"enabled"` + IssueTrackerConnectionID pgtype.UUID `json:"issue_tracker_connection_id"` + IssueTrackerProjectID pgtype.Text `json:"issue_tracker_project_id"` + IssueTrackerProjectShort pgtype.Text `json:"issue_tracker_project_short"` + EligibilityTagID pgtype.Text `json:"eligibility_tag_id"` + EligibilityTagName pgtype.Text `json:"eligibility_tag_name"` + IssueTrackerLinkedAt pgtype.Timestamptz `json:"issue_tracker_linked_at"` + AwaitingFirstEnable bool `json:"awaiting_first_enable"` + ForgeType string `json:"forge_type"` + BaseUrl string `json:"base_url"` + TokenCiphertext []byte `json:"token_ciphertext"` + UserID uuid.UUID `json:"user_id"` } // Every enabled repo across all users, with its connection, for the sync @@ -1123,6 +1190,13 @@ func (q *Queries) ListEnabledReposWithConnections(ctx context.Context) ([]ListEn &i.WebUrl, &i.DefaultBranch, &i.Enabled, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, + &i.AwaitingFirstEnable, &i.ForgeType, &i.BaseUrl, &i.TokenCiphertext, @@ -1177,7 +1251,7 @@ func (q *Queries) ListForgeConnectionsByUser(ctx context.Context, userID uuid.UU } const listIssuesByRepo = `-- name: ListIssuesByRepo :many -SELECT id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids FROM issues WHERE repo_id = $1 +SELECT id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids, issue_key FROM issues WHERE repo_id = $1 ORDER BY board_position ASC NULLS LAST, forge_issue_iid ASC ` @@ -1223,6 +1297,7 @@ func (q *Queries) ListIssuesByRepo(ctx context.Context, repoID uuid.UUID) ([]Iss &i.SyncedAt, &i.BoardPosition, &i.AssigneeIds, + &i.IssueKey, ); err != nil { return nil, err } @@ -1417,6 +1492,11 @@ FROM latest l JOIN issues i ON i.repo_id = $1 AND i.forge_issue_iid = l.issue_iid WHERE l.status = 'completed' AND l.mr_iid IS NOT NULL + -- A repo linked to an external issue tracker (PRD #4173 M4) is owned by + -- ListBoardFreeMRStateWatchCandidates entirely: uzi moves no card there (writes arrive + -- in M5), so its MR state must not depend on the issue cache or a column label. Excluding + -- the repo here keeps the two lanes disjoint. + AND NOT EXISTS (SELECT 1 FROM repos lr WHERE lr.id = $1 AND lr.issue_tracker_connection_id IS NOT NULL) AND ( -- Lane A (UNCHANGED): open-issue board-move watch — card in Human Review -- (close-edge) or run already recorded closed (reopen-edge, Decision 10). @@ -1640,7 +1720,7 @@ func (q *Queries) ListPRDLinkPatchCandidates(ctx context.Context, arg ListPRDLin } const listReposByConnectionForUser = `-- name: ListReposByConnectionForUser :many -SELECT r.id, r.connection_id, r.forge_project_id, r.path_with_namespace, r.web_url, r.default_branch, r.enabled, r.repo_skills_enabled, r.repo_devbox_opt_in, r.repo_claudemd_enabled, r.guardrail_override_reason, r.guardrail_override_by, r.guardrail_override_at, r.required_capabilities, r.fold_improve_uzi_backlog, r.code_cross_check_guidance, r.cross_check_repo_instructions_enabled, r.ignored_inferred_capabilities, r.agent_source_default FROM repos r +SELECT r.id, r.connection_id, r.forge_project_id, r.path_with_namespace, r.web_url, r.default_branch, r.enabled, r.repo_skills_enabled, r.repo_devbox_opt_in, r.repo_claudemd_enabled, r.guardrail_override_reason, r.guardrail_override_by, r.guardrail_override_at, r.required_capabilities, r.fold_improve_uzi_backlog, r.code_cross_check_guidance, r.cross_check_repo_instructions_enabled, r.ignored_inferred_capabilities, r.agent_source_default, r.awaiting_first_enable, r.issue_tracker_connection_id, r.issue_tracker_project_id, r.issue_tracker_project_short, r.eligibility_tag_id, r.eligibility_tag_name, r.issue_tracker_linked_at FROM repos r JOIN forge_connections c ON c.id = r.connection_id WHERE r.connection_id = $1 AND c.user_id = $2 ORDER BY r.path_with_namespace ASC @@ -1681,6 +1761,13 @@ func (q *Queries) ListReposByConnectionForUser(ctx context.Context, arg ListRepo &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ); err != nil { return nil, err } @@ -1698,7 +1785,7 @@ SET labels = COALESCE((SELECT jsonb_agg(value ORDER BY ordinal) FROM jsonb_array_elements(labels) WITH ORDINALITY AS item(value, ordinal) WHERE value <> to_jsonb($1::text)), '[]'::jsonb) WHERE repo_id = $2 AND forge_issue_iid = $3 -RETURNING id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids +RETURNING id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids, issue_key ` type RemoveCachedIssueLabelParams struct { @@ -1726,6 +1813,7 @@ func (q *Queries) RemoveCachedIssueLabel(ctx context.Context, arg RemoveCachedIs &i.SyncedAt, &i.BoardPosition, &i.AssigneeIds, + &i.IssueKey, ) return i, err } @@ -1734,7 +1822,7 @@ const reopenIssueState = `-- name: ReopenIssueState :one UPDATE issues SET state = 'opened', board_position = NULL, synced_at = now() WHERE repo_id = $1 AND forge_issue_iid = $2 -RETURNING id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids +RETURNING id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids, issue_key ` type ReopenIssueStateParams struct { @@ -1767,6 +1855,7 @@ func (q *Queries) ReopenIssueState(ctx context.Context, arg ReopenIssueStatePara &i.SyncedAt, &i.BoardPosition, &i.AssigneeIds, + &i.IssueKey, ) return i, err } @@ -1861,7 +1950,7 @@ const setRepoAgentSourceDefault = `-- name: SetRepoAgentSourceDefault :one UPDATE repos SET agent_source_default = $1::text WHERE repos.id = $2 AND $1::text IS DISTINCT FROM 'repo' -RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoAgentSourceDefaultParams struct { @@ -1892,6 +1981,13 @@ func (q *Queries) SetRepoAgentSourceDefault(ctx context.Context, arg SetRepoAgen &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -1900,7 +1996,7 @@ const setRepoAgentSourceDefaultForUser = `-- name: SetRepoAgentSourceDefaultForU UPDATE repos SET agent_source_default = $1::text WHERE repos.id = $2 AND repos.connection_id IN (SELECT forge_connections.id FROM forge_connections WHERE forge_connections.user_id = $3) -RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoAgentSourceDefaultForUserParams struct { @@ -1932,12 +2028,19 @@ func (q *Queries) SetRepoAgentSourceDefaultForUser(ctx context.Context, arg SetR &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } const setRepoCapabilityPolicy = `-- name: SetRepoCapabilityPolicy :one -UPDATE repos SET required_capabilities = $2, ignored_inferred_capabilities = $3 WHERE repos.id = $1 RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +UPDATE repos SET required_capabilities = $2, ignored_inferred_capabilities = $3 WHERE repos.id = $1 RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoCapabilityPolicyParams struct { @@ -1969,6 +2072,13 @@ func (q *Queries) SetRepoCapabilityPolicy(ctx context.Context, arg SetRepoCapabi &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -1977,7 +2087,7 @@ const setRepoCapabilityPolicyForUser = `-- name: SetRepoCapabilityPolicyForUser UPDATE repos SET required_capabilities = $2, ignored_inferred_capabilities = $3 WHERE repos.id = $1 AND repos.connection_id IN (SELECT forge_connections.id FROM forge_connections WHERE forge_connections.user_id = $4) -RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoCapabilityPolicyForUserParams struct { @@ -2015,6 +2125,13 @@ func (q *Queries) SetRepoCapabilityPolicyForUser(ctx context.Context, arg SetRep &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -2023,7 +2140,7 @@ const setRepoCodeCrossCheckInputs = `-- name: SetRepoCodeCrossCheckInputs :one UPDATE repos SET code_cross_check_guidance = CASE WHEN $1::boolean THEN $2::text ELSE code_cross_check_guidance END, cross_check_repo_instructions_enabled = COALESCE($3::boolean, cross_check_repo_instructions_enabled) -WHERE id = $4 RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +WHERE id = $4 RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoCodeCrossCheckInputsParams struct { @@ -2061,6 +2178,13 @@ func (q *Queries) SetRepoCodeCrossCheckInputs(ctx context.Context, arg SetRepoCo &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -2070,7 +2194,7 @@ UPDATE repos SET code_cross_check_guidance = CASE WHEN $1::boolean THEN $2::text ELSE code_cross_check_guidance END, cross_check_repo_instructions_enabled = COALESCE($3::boolean, cross_check_repo_instructions_enabled) WHERE repos.id = $4 AND repos.connection_id IN (SELECT fc.id FROM forge_connections fc WHERE fc.user_id = $5) -RETURNING repos.id, repos.connection_id, repos.forge_project_id, repos.path_with_namespace, repos.web_url, repos.default_branch, repos.enabled, repos.repo_skills_enabled, repos.repo_devbox_opt_in, repos.repo_claudemd_enabled, repos.guardrail_override_reason, repos.guardrail_override_by, repos.guardrail_override_at, repos.required_capabilities, repos.fold_improve_uzi_backlog, repos.code_cross_check_guidance, repos.cross_check_repo_instructions_enabled, repos.ignored_inferred_capabilities, repos.agent_source_default +RETURNING repos.id, repos.connection_id, repos.forge_project_id, repos.path_with_namespace, repos.web_url, repos.default_branch, repos.enabled, repos.repo_skills_enabled, repos.repo_devbox_opt_in, repos.repo_claudemd_enabled, repos.guardrail_override_reason, repos.guardrail_override_by, repos.guardrail_override_at, repos.required_capabilities, repos.fold_improve_uzi_backlog, repos.code_cross_check_guidance, repos.cross_check_repo_instructions_enabled, repos.ignored_inferred_capabilities, repos.agent_source_default, repos.awaiting_first_enable, repos.issue_tracker_connection_id, repos.issue_tracker_project_id, repos.issue_tracker_project_short, repos.eligibility_tag_id, repos.eligibility_tag_name, repos.issue_tracker_linked_at ` type SetRepoCodeCrossCheckInputsForUserParams struct { @@ -2110,12 +2234,19 @@ func (q *Queries) SetRepoCodeCrossCheckInputsForUser(ctx context.Context, arg Se &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } const setRepoDevboxOptIn = `-- name: SetRepoDevboxOptIn :one -UPDATE repos SET repo_devbox_opt_in = $2 WHERE repos.id = $1 RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +UPDATE repos SET repo_devbox_opt_in = $2 WHERE repos.id = $1 RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoDevboxOptInParams struct { @@ -2148,6 +2279,13 @@ func (q *Queries) SetRepoDevboxOptIn(ctx context.Context, arg SetRepoDevboxOptIn &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -2156,7 +2294,7 @@ const setRepoDevboxOptInForUser = `-- name: SetRepoDevboxOptInForUser :one UPDATE repos SET repo_devbox_opt_in = $2 WHERE repos.id = $1 AND repos.connection_id IN (SELECT forge_connections.id FROM forge_connections WHERE forge_connections.user_id = $3) -RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoDevboxOptInForUserParams struct { @@ -2190,6 +2328,13 @@ func (q *Queries) SetRepoDevboxOptInForUser(ctx context.Context, arg SetRepoDevb &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -2198,7 +2343,7 @@ const setRepoEnabledForUser = `-- name: SetRepoEnabledForUser :one UPDATE repos SET enabled = $2 WHERE repos.id = $1 AND repos.connection_id IN (SELECT forge_connections.id FROM forge_connections WHERE forge_connections.user_id = $3) -RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoEnabledForUserParams struct { @@ -2230,12 +2375,19 @@ func (q *Queries) SetRepoEnabledForUser(ctx context.Context, arg SetRepoEnabledF &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } const setRepoFoldImproveUziBacklog = `-- name: SetRepoFoldImproveUziBacklog :one -UPDATE repos SET fold_improve_uzi_backlog = $2 WHERE repos.id = $1 RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +UPDATE repos SET fold_improve_uzi_backlog = $2 WHERE repos.id = $1 RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoFoldImproveUziBacklogParams struct { @@ -2268,6 +2420,13 @@ func (q *Queries) SetRepoFoldImproveUziBacklog(ctx context.Context, arg SetRepoF &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -2276,7 +2435,7 @@ const setRepoFoldImproveUziBacklogForUser = `-- name: SetRepoFoldImproveUziBackl UPDATE repos SET fold_improve_uzi_backlog = $2 WHERE repos.id = $1 AND repos.connection_id IN (SELECT forge_connections.id FROM forge_connections WHERE forge_connections.user_id = $3) -RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoFoldImproveUziBacklogForUserParams struct { @@ -2311,6 +2470,13 @@ func (q *Queries) SetRepoFoldImproveUziBacklogForUser(ctx context.Context, arg S &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -2321,7 +2487,7 @@ SET guardrail_override_reason = $2, guardrail_override_by = $3, guardrail_override_at = $4 WHERE id = $1 -RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoGuardrailOverrideParams struct { @@ -2365,12 +2531,19 @@ func (q *Queries) SetRepoGuardrailOverride(ctx context.Context, arg SetRepoGuard &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } const setRepoIgnoredInferredCapabilities = `-- name: SetRepoIgnoredInferredCapabilities :one -UPDATE repos SET ignored_inferred_capabilities = $2 WHERE repos.id = $1 RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +UPDATE repos SET ignored_inferred_capabilities = $2 WHERE repos.id = $1 RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoIgnoredInferredCapabilitiesParams struct { @@ -2401,6 +2574,13 @@ func (q *Queries) SetRepoIgnoredInferredCapabilities(ctx context.Context, arg Se &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -2409,7 +2589,7 @@ const setRepoIgnoredInferredCapabilitiesForUser = `-- name: SetRepoIgnoredInferr UPDATE repos SET ignored_inferred_capabilities = $2 WHERE repos.id = $1 AND repos.connection_id IN (SELECT forge_connections.id FROM forge_connections WHERE forge_connections.user_id = $3) -RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoIgnoredInferredCapabilitiesForUserParams struct { @@ -2441,12 +2621,19 @@ func (q *Queries) SetRepoIgnoredInferredCapabilitiesForUser(ctx context.Context, &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } const setRepoRequiredCapabilities = `-- name: SetRepoRequiredCapabilities :one -UPDATE repos SET required_capabilities = $2 WHERE repos.id = $1 RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +UPDATE repos SET required_capabilities = $2 WHERE repos.id = $1 RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoRequiredCapabilitiesParams struct { @@ -2480,6 +2667,13 @@ func (q *Queries) SetRepoRequiredCapabilities(ctx context.Context, arg SetRepoRe &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -2488,7 +2682,7 @@ const setRepoRequiredCapabilitiesForUser = `-- name: SetRepoRequiredCapabilities UPDATE repos SET required_capabilities = $2 WHERE repos.id = $1 AND repos.connection_id IN (SELECT forge_connections.id FROM forge_connections WHERE forge_connections.user_id = $3) -RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoRequiredCapabilitiesForUserParams struct { @@ -2524,6 +2718,13 @@ func (q *Queries) SetRepoRequiredCapabilitiesForUser(ctx context.Context, arg Se &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -2533,7 +2734,7 @@ UPDATE repos SET repo_skills_enabled = COALESCE($1, repo_skills_enabled), repo_claudemd_enabled = COALESCE($2, repo_claudemd_enabled) WHERE repos.id = $3 -RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoTrustFlagsParams struct { @@ -2569,6 +2770,13 @@ func (q *Queries) SetRepoTrustFlags(ctx context.Context, arg SetRepoTrustFlagsPa &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -2579,7 +2787,7 @@ UPDATE repos SET repo_claudemd_enabled = COALESCE($2, repo_claudemd_enabled) WHERE repos.id = $3 AND repos.connection_id IN (SELECT forge_connections.id FROM forge_connections WHERE forge_connections.user_id = $4) -RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type SetRepoTrustFlagsForUserParams struct { @@ -2619,6 +2827,13 @@ func (q *Queries) SetRepoTrustFlagsForUser(ctx context.Context, arg SetRepoTrust &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } @@ -2693,7 +2908,7 @@ const updateIssueState = `-- name: UpdateIssueState :one UPDATE issues SET state = $3, synced_at = now() WHERE repo_id = $1 AND forge_issue_iid = $2 -RETURNING id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids +RETURNING id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids, issue_key ` type UpdateIssueStateParams struct { @@ -2728,6 +2943,7 @@ func (q *Queries) UpdateIssueState(ctx context.Context, arg UpdateIssueStatePara &i.SyncedAt, &i.BoardPosition, &i.AssigneeIds, + &i.IssueKey, ) return i, err } @@ -2832,7 +3048,7 @@ SET title = EXCLUDED.title, has_prd_link = EXCLUDED.has_prd_link, forge_updated_at = EXCLUDED.forge_updated_at, synced_at = now() -RETURNING id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids +RETURNING id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids, issue_key ` type UpsertIssueParams struct { @@ -2887,6 +3103,7 @@ func (q *Queries) UpsertIssue(ctx context.Context, arg UpsertIssueParams) (Issue &i.SyncedAt, &i.BoardPosition, &i.AssigneeIds, + &i.IssueKey, ) return i, err } @@ -2922,7 +3139,7 @@ SET title = EXCLUDED.title, has_prd_link = EXCLUDED.has_prd_link, forge_updated_at = EXCLUDED.forge_updated_at, synced_at = now() -RETURNING id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids +RETURNING id, repo_id, forge_issue_iid, title, state, labels, web_url, author, has_prd_link, forge_updated_at, synced_at, board_position, assignee_ids, issue_key ` type UpsertIssueLabelsParams struct { @@ -2988,19 +3205,20 @@ func (q *Queries) UpsertIssueLabels(ctx context.Context, arg UpsertIssueLabelsPa &i.SyncedAt, &i.BoardPosition, &i.AssigneeIds, + &i.IssueKey, ) return i, err } const upsertRepo = `-- name: UpsertRepo :one -INSERT INTO repos (connection_id, forge_project_id, path_with_namespace, web_url, default_branch) -VALUES ($1, $2, $3, $4, $5) +INSERT INTO repos (connection_id, forge_project_id, path_with_namespace, web_url, default_branch, awaiting_first_enable) +VALUES ($1, $2, $3, $4, $5, true) ON CONFLICT (connection_id, forge_project_id) DO UPDATE SET path_with_namespace = EXCLUDED.path_with_namespace, web_url = EXCLUDED.web_url, default_branch = EXCLUDED.default_branch -RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default +RETURNING id, connection_id, forge_project_id, path_with_namespace, web_url, default_branch, enabled, repo_skills_enabled, repo_devbox_opt_in, repo_claudemd_enabled, guardrail_override_reason, guardrail_override_by, guardrail_override_at, required_capabilities, fold_improve_uzi_backlog, code_cross_check_guidance, cross_check_repo_instructions_enabled, ignored_inferred_capabilities, agent_source_default, awaiting_first_enable, issue_tracker_connection_id, issue_tracker_project_id, issue_tracker_project_short, eligibility_tag_id, eligibility_tag_name, issue_tracker_linked_at ` type UpsertRepoParams struct { @@ -3013,7 +3231,9 @@ type UpsertRepoParams struct { // Repos -------------------------------------------------------------------- // Upserted whenever the membership list is fetched. Never touches `enabled`, so -// a re-listing does not un-enable a repo the user already turned on. +// a re-listing does not un-enable a repo the user already turned on. The insert +// branch marks a newly discovered repo awaiting_first_enable (PRD #4173 D13); the +// conflict branch never touches it, and the repos trigger clears it on first enable. func (q *Queries) UpsertRepo(ctx context.Context, arg UpsertRepoParams) (Repo, error) { row := q.db.QueryRow(ctx, upsertRepo, arg.ConnectionID, @@ -3043,6 +3263,13 @@ func (q *Queries) UpsertRepo(ctx context.Context, arg UpsertRepoParams) (Repo, e &i.CrossCheckRepoInstructionsEnabled, &i.IgnoredInferredCapabilities, &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, ) return i, err } diff --git a/api/internal/store/issue_tracker_connections.sql.go b/api/internal/store/issue_tracker_connections.sql.go index ce742513dc..e4028c2ad7 100644 --- a/api/internal/store/issue_tracker_connections.sql.go +++ b/api/internal/store/issue_tracker_connections.sql.go @@ -54,6 +54,50 @@ func (q *Queries) GetIssueTrackerConnectionForUser(ctx context.Context, arg GetI return i, err } +const insertIssueTrackerConnectionIfAbsent = `-- name: InsertIssueTrackerConnectionIfAbsent :one +INSERT INTO issue_tracker_connections ( + user_id, kind, base_url, account_id, account_login, token_ciphertext, last_verified_at +) VALUES ($1, $2, $3, $4, $5, $6, now()) +ON CONFLICT (user_id, kind, base_url) DO NOTHING +RETURNING id, user_id, kind, base_url, account_id, account_login, token_ciphertext, created_at, last_verified_at +` + +type InsertIssueTrackerConnectionIfAbsentParams struct { + UserID uuid.UUID `json:"user_id"` + Kind string `json:"kind"` + BaseUrl string `json:"base_url"` + AccountID string `json:"account_id"` + AccountLogin string `json:"account_login"` + TokenCiphertext []byte `json:"token_ciphertext"` +} + +// The first-connect insert: never updates, so it cannot overwrite a row (and its account) +// that another connect created. No row returned means the identity exists already, and the +// caller re-enters the locked reconnect path. +func (q *Queries) InsertIssueTrackerConnectionIfAbsent(ctx context.Context, arg InsertIssueTrackerConnectionIfAbsentParams) (IssueTrackerConnection, error) { + row := q.db.QueryRow(ctx, insertIssueTrackerConnectionIfAbsent, + arg.UserID, + arg.Kind, + arg.BaseUrl, + arg.AccountID, + arg.AccountLogin, + arg.TokenCiphertext, + ) + var i IssueTrackerConnection + err := row.Scan( + &i.ID, + &i.UserID, + &i.Kind, + &i.BaseUrl, + &i.AccountID, + &i.AccountLogin, + &i.TokenCiphertext, + &i.CreatedAt, + &i.LastVerifiedAt, + ) + return i, err +} + const listIssueTrackerConnectionsByUser = `-- name: ListIssueTrackerConnectionsByUser :many SELECT id, user_id, kind, base_url, account_id, account_login, token_ciphertext, created_at, last_verified_at FROM issue_tracker_connections WHERE user_id = $1 ORDER BY created_at ASC, id ASC ` @@ -88,6 +132,69 @@ func (q *Queries) ListIssueTrackerConnectionsByUser(ctx context.Context, userID return items, nil } +const lockIssueTrackerConnectionByIdentityForUpdate = `-- name: LockIssueTrackerConnectionByIdentityForUpdate :one +SELECT id, user_id, kind, base_url, account_id, account_login, token_ciphertext, created_at, last_verified_at FROM issue_tracker_connections +WHERE user_id = $1 AND kind = $2 AND base_url = $3 +FOR UPDATE +` + +type LockIssueTrackerConnectionByIdentityForUpdateParams struct { + UserID uuid.UUID `json:"user_id"` + Kind string `json:"kind"` + BaseUrl string `json:"base_url"` +} + +// Reconnect step 1 (PRD #4173 M4 owner guidance): read the existing row of the +// (user, kind, base_url) identity and hold FOR UPDATE, so a link (which holds the +// row FOR SHARE) and a reconnect that changes the account cannot interleave. No row +// means a first connect, which no repo can link to yet. +func (q *Queries) LockIssueTrackerConnectionByIdentityForUpdate(ctx context.Context, arg LockIssueTrackerConnectionByIdentityForUpdateParams) (IssueTrackerConnection, error) { + row := q.db.QueryRow(ctx, lockIssueTrackerConnectionByIdentityForUpdate, arg.UserID, arg.Kind, arg.BaseUrl) + var i IssueTrackerConnection + err := row.Scan( + &i.ID, + &i.UserID, + &i.Kind, + &i.BaseUrl, + &i.AccountID, + &i.AccountLogin, + &i.TokenCiphertext, + &i.CreatedAt, + &i.LastVerifiedAt, + ) + return i, err +} + +const lockIssueTrackerConnectionForShareForUser = `-- name: LockIssueTrackerConnectionForShareForUser :one +SELECT id, user_id, kind, base_url, account_id, account_login, token_ciphertext, created_at, last_verified_at FROM issue_tracker_connections WHERE id = $1 AND user_id = $2 +FOR SHARE +` + +type LockIssueTrackerConnectionForShareForUserParams struct { + ID uuid.UUID `json:"id"` + UserID uuid.UUID `json:"user_id"` +} + +// The link's re-read of the connection inside its transaction. FOR SHARE conflicts +// with the reconnect's FOR UPDATE, which the repos foreign key's own key-share lock +// does not when the upsert changes only non-key columns. +func (q *Queries) LockIssueTrackerConnectionForShareForUser(ctx context.Context, arg LockIssueTrackerConnectionForShareForUserParams) (IssueTrackerConnection, error) { + row := q.db.QueryRow(ctx, lockIssueTrackerConnectionForShareForUser, arg.ID, arg.UserID) + var i IssueTrackerConnection + err := row.Scan( + &i.ID, + &i.UserID, + &i.Kind, + &i.BaseUrl, + &i.AccountID, + &i.AccountLogin, + &i.TokenCiphertext, + &i.CreatedAt, + &i.LastVerifiedAt, + ) + return i, err +} + const upsertIssueTrackerConnection = `-- name: UpsertIssueTrackerConnection :one INSERT INTO issue_tracker_connections ( @@ -98,6 +205,8 @@ SET account_id = EXCLUDED.account_id, account_login = EXCLUDED.account_login, token_ciphertext = EXCLUDED.token_ciphertext, last_verified_at = now() +WHERE issue_tracker_connections.account_id = EXCLUDED.account_id + OR NOT EXISTS (SELECT 1 FROM repos WHERE repos.issue_tracker_connection_id = issue_tracker_connections.id) RETURNING id, user_id, kind, base_url, account_id, account_login, token_ciphertext, created_at, last_verified_at ` @@ -114,6 +223,12 @@ type UpsertIssueTrackerConnectionParams struct { // Connect or reconnect/rotate: one connection per (user, kind, base_url). // Reconnecting with a fresh token updates the ciphertext and account identity // in place, keeps the row id, and stamps last_verified_at. +// Defence in depth for the reconnect rule (PRD #4173 M4 owner guidance): a conflicting row +// whose account would change is left alone while a repo links to it, and the statement then +// returns no row. NOT a complete guard: under READ COMMITTED the subquery reads the snapshot +// of the statement, taken before it waits on a concurrent link's row lock, so it cannot see +// a link that commits during that wait. The service takes the row lock and counts in a +// separate statement first (LockIssueTrackerConnectionByIdentityForUpdate). func (q *Queries) UpsertIssueTrackerConnection(ctx context.Context, arg UpsertIssueTrackerConnectionParams) (IssueTrackerConnection, error) { row := q.db.QueryRow(ctx, upsertIssueTrackerConnection, arg.UserID, diff --git a/api/internal/store/issue_tracker_links.sql.go b/api/internal/store/issue_tracker_links.sql.go new file mode 100644 index 0000000000..e84e5b27ea --- /dev/null +++ b/api/internal/store/issue_tracker_links.sql.go @@ -0,0 +1,213 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 +// source: issue_tracker_links.sql + +package store + +import ( + "context" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgtype" +) + +const countReposLinkedToIssueTrackerConnection = `-- name: CountReposLinkedToIssueTrackerConnection :one +SELECT count(*) FROM repos WHERE issue_tracker_connection_id = $1::uuid +` + +// How many repos name this issue tracker connection; deleting it is refused while any do. +func (q *Queries) CountReposLinkedToIssueTrackerConnection(ctx context.Context, connectionID uuid.UUID) (int64, error) { + row := q.db.QueryRow(ctx, countReposLinkedToIssueTrackerConnection, connectionID) + var count int64 + err := row.Scan(&count) + return count, err +} + +const getRepoIssueSourceForUser = `-- name: GetRepoIssueSourceForUser :one + +SELECT r.id, r.forge_project_id, r.awaiting_first_enable, + r.issue_tracker_connection_id, r.issue_tracker_project_id, r.issue_tracker_project_short, + r.eligibility_tag_id, r.eligibility_tag_name, r.issue_tracker_linked_at, + c.forge_type, c.base_url, c.token_ciphertext, + itc.id AS linked_connection_id +FROM repos r +JOIN forge_connections c ON c.id = r.connection_id +LEFT JOIN issue_tracker_connections itc + ON itc.id = r.issue_tracker_connection_id AND itc.user_id = c.user_id +WHERE r.id = $1 AND c.user_id = $2 +` + +type GetRepoIssueSourceForUserParams struct { + ID uuid.UUID `json:"id"` + UserID uuid.UUID `json:"user_id"` +} + +type GetRepoIssueSourceForUserRow struct { + ID uuid.UUID `json:"id"` + ForgeProjectID int64 `json:"forge_project_id"` + AwaitingFirstEnable bool `json:"awaiting_first_enable"` + IssueTrackerConnectionID pgtype.UUID `json:"issue_tracker_connection_id"` + IssueTrackerProjectID pgtype.Text `json:"issue_tracker_project_id"` + IssueTrackerProjectShort pgtype.Text `json:"issue_tracker_project_short"` + EligibilityTagID pgtype.Text `json:"eligibility_tag_id"` + EligibilityTagName pgtype.Text `json:"eligibility_tag_name"` + IssueTrackerLinkedAt pgtype.Timestamptz `json:"issue_tracker_linked_at"` + ForgeType string `json:"forge_type"` + BaseUrl string `json:"base_url"` + TokenCiphertext []byte `json:"token_ciphertext"` + LinkedConnectionID pgtype.UUID `json:"linked_connection_id"` +} + +// Repo to issue tracker project links (PRD #4173 M4, DL-3..DL-5). +// The one owner-scoped row the per-repo resolvers read (DL-5): the repo, its forge +// connection and its issue tracker link. The binding is keyed by repo row id and the +// owning user, never by forge project number. linked_connection_id is the link's +// connection ONLY when it belongs to the same user as the forge connection; a linked +// repo whose connection is missing for that owner reads NULL here and the resolver +// fails closed. +func (q *Queries) GetRepoIssueSourceForUser(ctx context.Context, arg GetRepoIssueSourceForUserParams) (GetRepoIssueSourceForUserRow, error) { + row := q.db.QueryRow(ctx, getRepoIssueSourceForUser, arg.ID, arg.UserID) + var i GetRepoIssueSourceForUserRow + err := row.Scan( + &i.ID, + &i.ForgeProjectID, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, + &i.ForgeType, + &i.BaseUrl, + &i.TokenCiphertext, + &i.LinkedConnectionID, + ) + return i, err +} + +const lockRepoForLinkForUser = `-- name: LockRepoForLinkForUser :one +SELECT r.id, r.awaiting_first_enable, r.issue_tracker_connection_id +FROM repos r +JOIN forge_connections c ON c.id = r.connection_id +WHERE r.id = $1 AND c.user_id = $2 +FOR UPDATE OF r +` + +type LockRepoForLinkForUserParams struct { + ID uuid.UUID `json:"id"` + UserID uuid.UUID `json:"user_id"` +} + +type LockRepoForLinkForUserRow struct { + ID uuid.UUID `json:"id"` + AwaitingFirstEnable bool `json:"awaiting_first_enable"` + IssueTrackerConnectionID pgtype.UUID `json:"issue_tracker_connection_id"` +} + +// Takes the repo row lock that the link and the first enable share (DL-4), scoped to +// the owning user. Call inside a transaction; the returned markers are read after the +// lock, so an enable that committed first is visible. +func (q *Queries) LockRepoForLinkForUser(ctx context.Context, arg LockRepoForLinkForUserParams) (LockRepoForLinkForUserRow, error) { + row := q.db.QueryRow(ctx, lockRepoForLinkForUser, arg.ID, arg.UserID) + var i LockRepoForLinkForUserRow + err := row.Scan(&i.ID, &i.AwaitingFirstEnable, &i.IssueTrackerConnectionID) + return i, err +} + +const repoStoresIssueNumber = `-- name: RepoStoresIssueNumber :one +SELECT ( + EXISTS (SELECT 1 FROM issues WHERE issues.repo_id = $1 AND issues.forge_issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM runs WHERE runs.repo_id = $1 AND (runs.issue_iid IS NOT NULL OR runs.continued_issue_iid IS NOT NULL)) + OR EXISTS (SELECT 1 FROM run_schedules WHERE run_schedules.repo_id = $1 AND run_schedules.issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM autopilot_triggers WHERE autopilot_triggers.repo_id = $1 AND autopilot_triggers.issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM github_project_items WHERE github_project_items.repo_id = $1 AND github_project_items.forge_issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM issue_proposals WHERE issue_proposals.repo_id = $1 AND issue_proposals.created_issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM recommendation_filed_issues WHERE recommendation_filed_issues.filed_repo_id = $1 AND recommendation_filed_issues.filed_issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM finding_dispositions WHERE finding_dispositions.repo_id = $1 AND finding_dispositions.filed_issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM finding_group_operations WHERE finding_group_operations.repo_id = $1 AND finding_group_operations.issue_iid IS NOT NULL) +)::boolean AS stores_issue_number +` + +// True when any table that stores an issue number for the repo has such a row. The +// guard triggers (migration 00425) make this false for an awaiting repo; the link +// checks it anyway as defence in depth. Keep in step with the trigger list there. +func (q *Queries) RepoStoresIssueNumber(ctx context.Context, repoID uuid.UUID) (bool, error) { + row := q.db.QueryRow(ctx, repoStoresIssueNumber, repoID) + var stores_issue_number bool + err := row.Scan(&stores_issue_number) + return stores_issue_number, err +} + +const setRepoIssueTrackerLink = `-- name: SetRepoIssueTrackerLink :one +UPDATE repos r +SET issue_tracker_connection_id = $1::uuid, + issue_tracker_project_id = $2::text, + issue_tracker_project_short = $3::text, + eligibility_tag_id = $4::text, + eligibility_tag_name = $5::text, + issue_tracker_linked_at = now() +FROM forge_connections c, issue_tracker_connections itc +WHERE r.id = $6 + AND c.id = r.connection_id AND c.user_id = $7 + AND itc.id = $1::uuid AND itc.user_id = $7 + AND r.awaiting_first_enable AND r.issue_tracker_connection_id IS NULL +RETURNING r.id, r.connection_id, r.forge_project_id, r.path_with_namespace, r.web_url, r.default_branch, r.enabled, r.repo_skills_enabled, r.repo_devbox_opt_in, r.repo_claudemd_enabled, r.guardrail_override_reason, r.guardrail_override_by, r.guardrail_override_at, r.required_capabilities, r.fold_improve_uzi_backlog, r.code_cross_check_guidance, r.cross_check_repo_instructions_enabled, r.ignored_inferred_capabilities, r.agent_source_default, r.awaiting_first_enable, r.issue_tracker_connection_id, r.issue_tracker_project_id, r.issue_tracker_project_short, r.eligibility_tag_id, r.eligibility_tag_name, r.issue_tracker_linked_at +` + +type SetRepoIssueTrackerLinkParams struct { + ConnectionID uuid.UUID `json:"connection_id"` + ProjectID string `json:"project_id"` + ProjectShort string `json:"project_short"` + TagID string `json:"tag_id"` + TagName string `json:"tag_name"` + RepoID uuid.UUID `json:"repo_id"` + UserID uuid.UUID `json:"user_id"` +} + +// Writes the link, owner-scoped in the one statement: the repo must belong to @user_id +// through its forge connection and the issue tracker connection must belong to the same +// user. Only a repo that has never been enabled and is not yet linked matches; no row +// means refused. Call after LockRepoForLinkForUser in the same transaction. +func (q *Queries) SetRepoIssueTrackerLink(ctx context.Context, arg SetRepoIssueTrackerLinkParams) (Repo, error) { + row := q.db.QueryRow(ctx, setRepoIssueTrackerLink, + arg.ConnectionID, + arg.ProjectID, + arg.ProjectShort, + arg.TagID, + arg.TagName, + arg.RepoID, + arg.UserID, + ) + var i Repo + err := row.Scan( + &i.ID, + &i.ConnectionID, + &i.ForgeProjectID, + &i.PathWithNamespace, + &i.WebUrl, + &i.DefaultBranch, + &i.Enabled, + &i.RepoSkillsEnabled, + &i.RepoDevboxOptIn, + &i.RepoClaudemdEnabled, + &i.GuardrailOverrideReason, + &i.GuardrailOverrideBy, + &i.GuardrailOverrideAt, + &i.RequiredCapabilities, + &i.FoldImproveUziBacklog, + &i.CodeCrossCheckGuidance, + &i.CrossCheckRepoInstructionsEnabled, + &i.IgnoredInferredCapabilities, + &i.AgentSourceDefault, + &i.AwaitingFirstEnable, + &i.IssueTrackerConnectionID, + &i.IssueTrackerProjectID, + &i.IssueTrackerProjectShort, + &i.EligibilityTagID, + &i.EligibilityTagName, + &i.IssueTrackerLinkedAt, + ) + return i, err +} diff --git a/api/internal/store/issue_tracker_repo_link_livedb_test.go b/api/internal/store/issue_tracker_repo_link_livedb_test.go new file mode 100644 index 0000000000..a853404b62 --- /dev/null +++ b/api/internal/store/issue_tracker_repo_link_livedb_test.go @@ -0,0 +1,441 @@ +package store_test + +import ( + "context" + "errors" + "strings" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgxpool" + + "github.com/vtmocanu/uzi/api/internal/store" +) + +// issue_tracker_repo_link_livedb_test.go proves migration 00425 (PRD #4173 M4) against a +// real Postgres on an isolated database: the never-enabled marker and its trigger, the +// issue-number guard on every table that stores an issue number, the issue_key and link +// CHECKs, the connection FK, and the link queries. Skipped without UZI_TEST_DATABASE_URL. + +const issueNumberGuardSQLState = "UZ413" + +func sqlState(err error) string { + var pe *pgconn.PgError + if errors.As(err, &pe) { + return pe.Code + } + return "" +} + +func itlExec(t *testing.T, pool *pgxpool.Pool, sql string, args ...any) { + t.Helper() + if _, err := pool.Exec(context.Background(), sql, args...); err != nil { + t.Fatalf("exec %q: %v", sql, err) + } +} + +func itlLinked(t *testing.T, pool *pgxpool.Pool, repo uuid.UUID) bool { + t.Helper() + var b bool + if err := pool.QueryRow(context.Background(), `SELECT issue_tracker_connection_id IS NOT NULL FROM repos WHERE id=$1`, repo).Scan(&b); err != nil { + t.Fatal(err) + } + return b +} + +func itlAwaiting(t *testing.T, pool *pgxpool.Pool, repo uuid.UUID) bool { + t.Helper() + var b bool + if err := pool.QueryRow(context.Background(), `SELECT awaiting_first_enable FROM repos WHERE id=$1`, repo).Scan(&b); err != nil { + t.Fatal(err) + } + return b +} + +// itlFixture is a user with a forge connection, an issue tracker connection, a legacy repo +// (inserted before 00425) and the ids a test needs. +type itlFixture struct { + user, forgeConn, itc, legacy uuid.UUID +} + +func itlMigrateWithLegacyRepo(t *testing.T) (context.Context, string, *pgxpool.Pool, itlFixture) { + t.Helper() + ctx, dsn := standIsolatedDB(t, "issue_tracker_link_") + if err := store.MigrateTo(ctx, dsn, 424); err != nil { + t.Fatalf("migrate to 424: %v", err) + } + pool, err := store.OpenPool(ctx, dsn) + if err != nil { + t.Fatal(err) + } + t.Cleanup(pool.Close) + f := itlFixture{user: uuid.New(), forgeConn: uuid.New(), itc: uuid.New(), legacy: uuid.New()} + itlExec(t, pool, `INSERT INTO users (id,email,password_hash) VALUES ($1,$2,'x')`, f.user, f.user.String()+"@example.test") + itlExec(t, pool, `INSERT INTO forge_connections (id,user_id,forge_type,base_url,bot_username,bot_forge_user_id,token_ciphertext) + VALUES ($1,$2,'github','https://example.com','bot',1,'x')`, f.forgeConn, f.user) + itlExec(t, pool, `INSERT INTO issue_tracker_connections (id,user_id,kind,base_url,account_id,account_login,token_ciphertext) + VALUES ($1,$2,'youtrack','https://yt.example.com','a','al','x')`, f.itc, f.user) + itlExec(t, pool, `INSERT INTO repos (id,connection_id,forge_project_id,path_with_namespace,web_url,enabled) + VALUES ($1,$2,1,'o/legacy','https://example.com/o/legacy',false)`, f.legacy, f.forgeConn) + if err := store.Migrate(ctx, dsn); err != nil { + t.Fatalf("migrate to head: %v", err) + } + return ctx, dsn, pool, f +} + +func TestIssueTrackerRepoLinkMigrationLiveDB(t *testing.T) { + ctx, _, pool, f := itlMigrateWithLegacyRepo(t) + q := store.New(pool) + + t.Run("marker", func(t *testing.T) { + if itlAwaiting(t, pool, f.legacy) { + t.Fatal("a repo that existed before the migration must count as previously enabled") + } + repo, err := q.UpsertRepo(ctx, store.UpsertRepoParams{ConnectionID: f.forgeConn, ForgeProjectID: 2, PathWithNamespace: "o/new", WebUrl: "https://example.com/o/new"}) + if err != nil { + t.Fatal(err) + } + if !repo.AwaitingFirstEnable || !itlAwaiting(t, pool, repo.ID) { + t.Fatal("UpsertRepo must mark a newly discovered repo awaiting_first_enable") + } + again, err := q.UpsertRepo(ctx, store.UpsertRepoParams{ConnectionID: f.forgeConn, ForgeProjectID: 2, PathWithNamespace: "o/new2", WebUrl: "https://example.com/o/new"}) + if err != nil || again.ID != repo.ID { + t.Fatalf("conflict upsert: %v", err) + } + if !itlAwaiting(t, pool, repo.ID) { + t.Fatal("the ON CONFLICT branch must not touch awaiting_first_enable") + } + itlExec(t, pool, `UPDATE repos SET enabled=true WHERE id=$1`, repo.ID) + if itlAwaiting(t, pool, repo.ID) { + t.Fatal("enabling must clear the marker in the same statement") + } + again, err = q.UpsertRepo(ctx, store.UpsertRepoParams{ConnectionID: f.forgeConn, ForgeProjectID: 2, PathWithNamespace: "o/new3", WebUrl: "https://example.com/o/new"}) + if err != nil || again.AwaitingFirstEnable { + t.Fatalf("a re-listing must not re-mark an enabled repo: %v", err) + } + itlExec(t, pool, `UPDATE repos SET enabled=false WHERE id=$1`, repo.ID) + if itlAwaiting(t, pool, repo.ID) { + t.Fatal("disabling must not re-mark") + } + _, err = pool.Exec(ctx, `UPDATE repos SET awaiting_first_enable=true WHERE id=$1`, repo.ID) + var pe *pgconn.PgError + if !errors.As(err, &pe) || !strings.Contains(pe.Message, "is never set back once cleared") { + t.Fatalf("setting the marker back must raise the trigger's refusal, got %v", err) + } + // Enabling and setting it in one statement still ends cleared, never raises. + itlExec(t, pool, `UPDATE repos SET enabled=true, awaiting_first_enable=true WHERE id=$1`, repo.ID) + if itlAwaiting(t, pool, repo.ID) { + t.Fatal("an enabled row must never keep the marker") + } + // A plain insert with the marker and enabled both true is cleared too. + var ins bool + if err := pool.QueryRow(ctx, `INSERT INTO repos (connection_id,forge_project_id,path_with_namespace,web_url,enabled,awaiting_first_enable) + VALUES ($1,99,'o/x','https://example.com/o/x',true,true) RETURNING awaiting_first_enable`, f.forgeConn).Scan(&ins); err != nil || ins { + t.Fatalf("insert enabled+awaiting: awaiting=%v err=%v", ins, err) + } + }) + + t.Run("guard", func(t *testing.T) { + aw, err := q.UpsertRepo(ctx, store.UpsertRepoParams{ConnectionID: f.forgeConn, ForgeProjectID: 10, PathWithNamespace: "o/aw", WebUrl: "https://example.com/o/aw"}) + if err != nil { + t.Fatal(err) + } + // A run on the (enabled-once) legacy repo supplies the FK parents. + runID, reviewID := uuid.New(), uuid.New() + itlExec(t, pool, `INSERT INTO runs (id,user_id,repo_id,issue_iid,issue_title,issue_description) VALUES ($1,$2,$3,1,'t','d')`, runID, f.user, f.legacy) + itlExec(t, pool, `INSERT INTO run_reviews (id,target_run_id,user_id,verdict) VALUES ($1,$2,$3,'issues')`, reviewID, runID, f.user) + + inserts := []struct { + table string + sql string + args func(repo uuid.UUID) []any + }{ + {"issues", `INSERT INTO issues (repo_id,forge_issue_iid,title,state,web_url,forge_updated_at,synced_at) VALUES ($1,5,'t','opened','u',now(),now())`, func(r uuid.UUID) []any { return []any{r} }}, + {"runs", `INSERT INTO runs (user_id,repo_id,issue_iid,issue_title,issue_description) VALUES ($1,$2,5,'t','d')`, func(r uuid.UUID) []any { return []any{f.user, r} }}, + {"run_schedules", `INSERT INTO run_schedules (user_id,repo_id,target,issue_iid,timing,run_at) VALUES ($1,$2,'issue',5,'once',now())`, func(r uuid.UUID) []any { return []any{f.user, r} }}, + {"autopilot_triggers", `INSERT INTO autopilot_triggers (repo_id,issue_iid,last_event_id) VALUES ($1,5,1)`, func(r uuid.UUID) []any { return []any{r} }}, + {"github_project_items", `INSERT INTO github_project_items (repo_id,forge_issue_iid,item_node_id) VALUES ($1,5,'PVTI_x')`, func(r uuid.UUID) []any { return []any{r} }}, + {"issue_proposals", `INSERT INTO issue_proposals (run_id,repo_id,title,description,created_issue_iid) VALUES ($1,$2,'t','d',5)`, func(r uuid.UUID) []any { return []any{runID, r} }}, + {"recommendation_filed_issues", `INSERT INTO recommendation_filed_issues (review_id,category,filed_repo_id,filed_issue_iid) VALUES ($1,'c',$2,5)`, func(r uuid.UUID) []any { return []any{reviewID, r} }}, + {"finding_dispositions", `INSERT INTO finding_dispositions (user_id,repo_id,location,filed_issue_iid) VALUES ($1,$2,'loc',5)`, func(r uuid.UUID) []any { return []any{f.user, r} }}, + {"finding_group_operations", `INSERT INTO finding_group_operations (user_id,repo_id,deadline_at,issue_iid,issue_url) VALUES ($1,$2,now(),5,'u')`, func(r uuid.UUID) []any { return []any{f.user, r} }}, + } + for _, in := range inserts { + if _, err := pool.Exec(ctx, in.sql, in.args(aw.ID)...); sqlState(err) != issueNumberGuardSQLState { + t.Errorf("%s: insert on an awaiting repo: want SQLSTATE %s, got %v", in.table, issueNumberGuardSQLState, err) + } + } + // A row with no issue number is not guarded: a prompt schedule and a NULL-iid finding. + itlExec(t, pool, `INSERT INTO run_schedules (user_id,repo_id,target,prompt,timing,run_at) VALUES ($1,$2,'prompt','p','once',now())`, f.user, aw.ID) + itlExec(t, pool, `INSERT INTO finding_dispositions (user_id,repo_id,location) VALUES ($1,$2,'nonum')`, f.user, aw.ID) + // A NULL filed_repo_id is skipped. + itlExec(t, pool, `INSERT INTO recommendation_filed_issues (review_id,category,filed_issue_iid) VALUES ($1,'norepo',5)`, reviewID) + + stores, err := q.RepoStoresIssueNumber(ctx, aw.ID) + if err != nil || stores { + t.Fatalf("RepoStoresIssueNumber on an awaiting repo: %v %v", stores, err) + } + + // UPDATE OF the repo id column: moving an issue onto the awaiting repo is refused. + itlExec(t, pool, `INSERT INTO issues (repo_id,forge_issue_iid,title,state,web_url,forge_updated_at,synced_at) VALUES ($1,7,'t','opened','u',now(),now())`, f.legacy) + if _, err := pool.Exec(ctx, `UPDATE issues SET repo_id=$1 WHERE repo_id=$2 AND forge_issue_iid=7`, aw.ID, f.legacy); sqlState(err) != issueNumberGuardSQLState { + t.Errorf("moving an issue to an awaiting repo: want %s, got %v", issueNumberGuardSQLState, err) + } + // UPDATE OF repo_id on a run that carries an issue number. + if _, err := pool.Exec(ctx, `UPDATE runs SET repo_id=$1 WHERE id=$2`, aw.ID, runID); sqlState(err) != issueNumberGuardSQLState { + t.Errorf("moving a run to an awaiting repo: want %s, got %v", issueNumberGuardSQLState, err) + } + + itlExec(t, pool, `UPDATE repos SET enabled=true WHERE id=$1`, aw.ID) + for _, in := range inserts { + if _, err := pool.Exec(ctx, in.sql, in.args(aw.ID)...); err != nil { + t.Errorf("%s: insert after the repo was enabled: %v", in.table, err) + } + } + stores, err = q.RepoStoresIssueNumber(ctx, aw.ID) + if err != nil || !stores { + t.Fatalf("RepoStoresIssueNumber after inserts: %v %v", stores, err) + } + }) + + t.Run("continued_issue_iid", func(t *testing.T) { + // A task run continues an issue through continued_issue_iid while issue_iid stays + // NULL (runs_continued_issue_shape), so this column alone carries the number. + repo, err := q.UpsertRepo(ctx, store.UpsertRepoParams{ConnectionID: f.forgeConn, ForgeProjectID: 40, PathWithNamespace: "o/cont", WebUrl: "https://example.com/o/cont"}) + if err != nil { + t.Fatal(err) + } + const ins = `INSERT INTO runs (user_id,repo_id,kind,branch,continued_issue_iid,issue_title,issue_description) VALUES ($1,$2,'task','b',5,'t','d')` + if _, err := pool.Exec(ctx, ins, f.user, repo.ID); sqlState(err) != issueNumberGuardSQLState { + t.Fatalf("a task run continuing an issue on an awaiting repo: want %s, got %v", issueNumberGuardSQLState, err) + } + itlExec(t, pool, `UPDATE repos SET enabled=true WHERE id=$1`, repo.ID) + itlExec(t, pool, ins, f.user, repo.ID) + }) + + t.Run("number_set_by_update", func(t *testing.T) { + // Insert with a NULL number (allowed), then UPDATE the number onto the awaiting repo. + aw, err := q.UpsertRepo(ctx, store.UpsertRepoParams{ConnectionID: f.forgeConn, ForgeProjectID: 41, PathWithNamespace: "o/upd", WebUrl: "https://example.com/o/upd"}) + if err != nil { + t.Fatal(err) + } + runID := uuid.New() + itlExec(t, pool, `INSERT INTO runs (id,user_id,repo_id,issue_iid,issue_title,issue_description) VALUES ($1,$2,$3,2,'t','d')`, runID, f.user, f.legacy) + reviewID := uuid.New() + itlExec(t, pool, `INSERT INTO run_reviews (id,target_run_id,user_id,verdict) VALUES ($1,$2,$3,'issues')`, reviewID, runID, f.user) + for _, c := range []struct { + table, insert, update string + args []any + }{ + {"issue_proposals", `INSERT INTO issue_proposals (id,run_id,repo_id,title,description) VALUES ($1,$2,$3,'t','d')`, `UPDATE issue_proposals SET created_issue_iid=5 WHERE id=$1`, []any{runID, aw.ID}}, + {"finding_dispositions", `INSERT INTO finding_dispositions (id,user_id,repo_id,location) VALUES ($1,$2,$3,'upd')`, `UPDATE finding_dispositions SET filed_issue_iid=5 WHERE id=$1`, []any{f.user, aw.ID}}, + {"finding_group_operations", `INSERT INTO finding_group_operations (id,user_id,repo_id,deadline_at) VALUES ($1,$2,$3,now())`, `UPDATE finding_group_operations SET issue_iid=5, issue_url='u' WHERE id=$1`, []any{f.user, aw.ID}}, + {"recommendation_filed_issues", `INSERT INTO recommendation_filed_issues (id,review_id,category,filed_repo_id) VALUES ($1,$2,'upd',$3)`, `UPDATE recommendation_filed_issues SET filed_issue_iid=5 WHERE id=$1`, []any{reviewID, aw.ID}}, + } { + id := uuid.New() + itlExec(t, pool, c.insert, append([]any{id}, c.args...)...) + if _, err := pool.Exec(ctx, c.update, id); sqlState(err) != issueNumberGuardSQLState { + t.Errorf("%s: UPDATE setting a number on an awaiting repo: want %s, got %v", c.table, issueNumberGuardSQLState, err) + } + } + }) + + t.Run("guard_fails_closed_on_missing_column", func(t *testing.T) { + // A trigger argument naming no column of the row must raise, never pass silently. + itlExec(t, pool, `CREATE TABLE itl_guard_probe (id int, repo_id uuid, num bigint)`) + itlExec(t, pool, `CREATE TRIGGER itl_probe_bad_num BEFORE INSERT ON itl_guard_probe + FOR EACH ROW EXECUTE FUNCTION issue_number_awaiting_first_enable_guard('repo_id', 'no_such_num')`) + _, err := pool.Exec(ctx, `INSERT INTO itl_guard_probe (id) VALUES (1)`) + var pe *pgconn.PgError + if !errors.As(err, &pe) || !strings.Contains(pe.Message, "no column no_such_num") { + t.Errorf("missing number column: want the guard's refusal, got %v", err) + } + itlExec(t, pool, `DROP TRIGGER itl_probe_bad_num ON itl_guard_probe`) + itlExec(t, pool, `CREATE TRIGGER itl_probe_bad_repo BEFORE INSERT ON itl_guard_probe + FOR EACH ROW EXECUTE FUNCTION issue_number_awaiting_first_enable_guard('no_such_repo', 'num')`) + _, err = pool.Exec(ctx, `INSERT INTO itl_guard_probe (id) VALUES (1)`) + if !errors.As(err, &pe) || !strings.Contains(pe.Message, "no column no_such_repo") { + t.Errorf("missing repo column: want the guard's refusal, got %v", err) + } + itlExec(t, pool, `DROP TABLE itl_guard_probe`) + }) + + t.Run("issue_key", func(t *testing.T) { + repo := f.legacy + ins := func(iid int64, key any) error { + _, err := pool.Exec(ctx, `INSERT INTO issues (repo_id,forge_issue_iid,issue_key,title,state,web_url,forge_updated_at,synced_at) VALUES ($1,$2,$3,'t','opened','u',now(),now())`, repo, iid, key) + return err + } + if err := ins(2, "ABC-12"); sqlState(err) != "23514" { + t.Errorf("ABC-12 on iid 2 must be refused by the CHECK: %v", err) + } + if err := ins(12, "ABC-12"); err != nil { + t.Errorf("ABC-12 on iid 12: %v", err) + } + if err := ins(13, nil); err != nil { + t.Errorf("NULL key: %v", err) + } + for i, bad := range []string{"abc12", "ABC-0", "ABC-012", "1BC-14", "ABC-14 ", "A-B-14"} { + if err := ins(14, bad); sqlState(err) != "23514" { + t.Errorf("key %q (%d) must be refused: %v", bad, i, err) + } + } + if err := ins(20, "A_1-20"); err != nil { + t.Errorf("A_1-20: %v", err) + } + }) + + t.Run("link_shape_and_fk", func(t *testing.T) { + repo, err := q.UpsertRepo(ctx, store.UpsertRepoParams{ConnectionID: f.forgeConn, ForgeProjectID: 30, PathWithNamespace: "o/lk", WebUrl: "https://example.com/o/lk"}) + if err != nil { + t.Fatal(err) + } + // Partial link refused. + if _, err := pool.Exec(ctx, `UPDATE repos SET issue_tracker_connection_id=$2 WHERE id=$1`, repo.ID, f.itc); sqlState(err) != "23514" { + t.Errorf("partial link must violate the all-or-none CHECK: %v", err) + } + full := `UPDATE repos SET issue_tracker_connection_id=$2, issue_tracker_project_id=$3, issue_tracker_project_short=$4, + eligibility_tag_id=$5, eligibility_tag_name=$6, issue_tracker_linked_at=now() WHERE id=$1` + for name, args := range map[string][]any{ + "control char in tag name": {repo.ID, f.itc, "0-1", "ABC", "9-1", "bad\x01name"}, + "bad short": {repo.ID, f.itc, "0-1", "1ABC", "9-1", "tag"}, + "short with dash": {repo.ID, f.itc, "0-1", "A-B", "9-1", "tag"}, + "empty project id": {repo.ID, f.itc, "", "ABC", "9-1", "tag"}, + "long tag name": {repo.ID, f.itc, "0-1", "ABC", "9-1", strings.Repeat("x", 257)}, + "long project id": {repo.ID, f.itc, strings.Repeat("x", 129), "ABC", "9-1", "tag"}, + } { + if _, err := pool.Exec(ctx, full, args...); sqlState(err) != "23514" { + t.Errorf("%s must be refused by the CHECK: %v", name, err) + } + } + + // issue_key is 64 wide (checked on the legacy repo, which the guard lets store a number): a 44-character short plus "-" plus 19 digits is the widest key. + bound := func(short string) error { + _, err := pool.Exec(ctx, full, repo.ID, f.itc, "0-1", short, "9-1", "tag") + return err + } + if err := bound("A" + strings.Repeat("b", 45)); sqlState(err) != "23514" { + t.Errorf("a 46-character short must be refused: %v", err) + } + if err := bound("A" + strings.Repeat("b", 44)); sqlState(err) != "23514" { + t.Errorf("a 45-character short must be refused: %v", err) + } + long := "A" + strings.Repeat("b", 43) + if err := bound(long); err != nil { + t.Fatalf("a 44-character short must link: %v", err) + } + itlExec(t, pool, `INSERT INTO issues (repo_id,forge_issue_iid,issue_key,title,state,web_url,forge_updated_at,synced_at) VALUES ($1,9223372036854775807,$2,'t','opened','u',now(),now())`, f.legacy, long+"-9223372036854775807") + itlExec(t, pool, `DELETE FROM issues WHERE issue_key=$1`, long+"-9223372036854775807") + itlExec(t, pool, `UPDATE repos SET issue_tracker_connection_id=NULL, issue_tracker_project_id=NULL, issue_tracker_project_short=NULL, + eligibility_tag_id=NULL, eligibility_tag_name=NULL, issue_tracker_linked_at=NULL WHERE id=$1`, repo.ID) + + // Link queries. + src, err := q.GetRepoIssueSourceForUser(ctx, store.GetRepoIssueSourceForUserParams{ID: repo.ID, UserID: f.user}) + if err != nil || src.LinkedConnectionID.Valid { + t.Fatalf("source before link: %+v %v", src, err) + } + other := uuid.New() + itlExec(t, pool, `INSERT INTO users (id,email,password_hash) VALUES ($1,$2,'x')`, other, other.String()+"@example.test") + if _, err := q.GetRepoIssueSourceForUser(ctx, store.GetRepoIssueSourceForUserParams{ID: repo.ID, UserID: other}); !errors.Is(err, pgx.ErrNoRows) { + t.Errorf("GetRepoIssueSourceForUser must be owner-scoped: %v", err) + } + if _, err := q.LockRepoForLinkForUser(ctx, store.LockRepoForLinkForUserParams{ID: repo.ID, UserID: other}); !errors.Is(err, pgx.ErrNoRows) { + t.Errorf("LockRepoForLinkForUser must be owner-scoped: %v", err) + } + // A cross-user connection or a foreign repo yields no row and writes nothing. + otherITC := uuid.New() + itlExec(t, pool, `INSERT INTO issue_tracker_connections (id,user_id,kind,base_url,account_id,account_login,token_ciphertext) + VALUES ($1,$2,'youtrack','https://yt2.example.com','b','bl','x')`, otherITC, other) + if _, err := q.SetRepoIssueTrackerLink(ctx, store.SetRepoIssueTrackerLinkParams{ + ConnectionID: otherITC, ProjectID: "0-1", ProjectShort: "ABC", TagID: "9-1", TagName: "uzi", RepoID: repo.ID, UserID: f.user}); !errors.Is(err, pgx.ErrNoRows) { + t.Errorf("linking to another user's connection must be refused: %v", err) + } + if _, err := q.SetRepoIssueTrackerLink(ctx, store.SetRepoIssueTrackerLinkParams{ + ConnectionID: otherITC, ProjectID: "0-1", ProjectShort: "ABC", TagID: "9-1", TagName: "uzi", RepoID: repo.ID, UserID: other}); !errors.Is(err, pgx.ErrNoRows) { + t.Errorf("another user linking a repo they do not own must be refused: %v", err) + } + if _, err := q.SetRepoIssueTrackerLink(ctx, store.SetRepoIssueTrackerLinkParams{ + ConnectionID: f.itc, ProjectID: "0-1", ProjectShort: "ABC", TagID: "9-1", TagName: "uzi", RepoID: repo.ID, UserID: other}); !errors.Is(err, pgx.ErrNoRows) { + t.Errorf("a foreign repo with the foreign user's own connection id must be refused: %v", err) + } + if itlLinked(t, pool, repo.ID) { + t.Fatal("a refused link must write nothing") + } + tx, err := pool.Begin(ctx) + if err != nil { + t.Fatal(err) + } + qt := q.WithTx(tx) + lock, err := qt.LockRepoForLinkForUser(ctx, store.LockRepoForLinkForUserParams{ID: repo.ID, UserID: f.user}) + if err != nil || !lock.AwaitingFirstEnable || lock.IssueTrackerConnectionID.Valid { + t.Fatalf("lock: %+v %v", lock, err) + } + linked, err := qt.SetRepoIssueTrackerLink(ctx, store.SetRepoIssueTrackerLinkParams{ + ConnectionID: f.itc, ProjectID: "0-1", ProjectShort: "ABC", TagID: "9-1", TagName: "uzi", RepoID: repo.ID, UserID: f.user}) + if err != nil || !linked.IssueTrackerLinkedAt.Valid { + t.Fatalf("SetRepoIssueTrackerLink: %+v %v", linked, err) + } + if err := tx.Commit(ctx); err != nil { + t.Fatal(err) + } + src, err = q.GetRepoIssueSourceForUser(ctx, store.GetRepoIssueSourceForUserParams{ID: repo.ID, UserID: f.user}) + if err != nil || !src.LinkedConnectionID.Valid || src.LinkedConnectionID.Bytes != f.itc || src.IssueTrackerProjectShort.String != "ABC" { + t.Fatalf("source after link: %+v %v", src, err) + } + n, err := q.CountReposLinkedToIssueTrackerConnection(ctx, f.itc) + if err != nil || n != 1 { + t.Fatalf("count linked: %d %v", n, err) + } + // Already linked: refused (no row). + if _, err := q.SetRepoIssueTrackerLink(ctx, store.SetRepoIssueTrackerLinkParams{ + ConnectionID: f.itc, ProjectID: "0-2", ProjectShort: "XYZ", TagID: "9-2", TagName: "t2", RepoID: repo.ID, UserID: f.user}); !errors.Is(err, pgx.ErrNoRows) { + t.Errorf("relinking must be refused: %v", err) + } + // FK: deleting a referenced connection fails. + if _, err := pool.Exec(ctx, `DELETE FROM issue_tracker_connections WHERE id=$1`, f.itc); sqlState(err) != "23503" { + t.Errorf("deleting a linked connection must fail on the FK: %v", err) + } + // Owner binding of the read: a link to another user's connection, forced past the + // query by a raw UPDATE, must not read back as a linked connection. + itlExec(t, pool, `UPDATE repos SET issue_tracker_connection_id=$2 WHERE id=$1`, repo.ID, otherITC) + src, err = q.GetRepoIssueSourceForUser(ctx, store.GetRepoIssueSourceForUserParams{ID: repo.ID, UserID: f.user}) + if err != nil || src.LinkedConnectionID.Valid { + t.Errorf("a connection of another user must not read as linked: %+v %v", src, err) + } + itlExec(t, pool, `UPDATE repos SET issue_tracker_connection_id=$2 WHERE id=$1`, repo.ID, f.itc) + // A repo that existed before the migration counts as previously enabled and refuses the link. + if _, err := q.SetRepoIssueTrackerLink(ctx, store.SetRepoIssueTrackerLinkParams{ + ConnectionID: f.itc, ProjectID: "0-1", ProjectShort: "ABC", TagID: "9-1", TagName: "uzi", RepoID: f.legacy, UserID: f.user}); !errors.Is(err, pgx.ErrNoRows) { + t.Errorf("an enabled-once repo must refuse the link: %v", err) + } + // Unlinking (all six to NULL) satisfies the CHECK. + itlExec(t, pool, `UPDATE repos SET issue_tracker_connection_id=NULL, issue_tracker_project_id=NULL, issue_tracker_project_short=NULL, + eligibility_tag_id=NULL, eligibility_tag_name=NULL, issue_tracker_linked_at=NULL WHERE id=$1`, repo.ID) + if n, _ := q.CountReposLinkedToIssueTrackerConnection(ctx, f.itc); n != 0 { + t.Errorf("count after unlink: %d", n) + } + }) +} + +// TestIssueTrackerRepoLinkDownUpLiveDB runs 00425's Down and Up again. +func TestIssueTrackerRepoLinkDownUpLiveDB(t *testing.T) { + ctx, dsn, pool, f := itlMigrateWithLegacyRepo(t) + if err := store.MigrateDownTo(ctx, dsn, 424); err != nil { + t.Fatalf("down: %v", err) + } + var n int + if err := pool.QueryRow(ctx, `SELECT count(*) FROM information_schema.columns WHERE table_name='repos' AND column_name IN ('awaiting_first_enable','issue_tracker_connection_id','eligibility_tag_name')`).Scan(&n); err != nil || n != 0 { + t.Fatalf("columns left after Down: %d %v", n, err) + } + if err := pool.QueryRow(ctx, `SELECT (SELECT count(*) FROM pg_trigger WHERE tgname LIKE '%awaiting_first_enable_guard') + (SELECT count(*) FROM pg_proc WHERE proname LIKE '%awaiting_first_enable_guard') + (SELECT count(*) FROM information_schema.columns WHERE table_name='issues' AND column_name='issue_key')`).Scan(&n); err != nil || n != 0 { + t.Fatalf("objects left after Down: %d %v", n, err) + } + if err := store.Migrate(ctx, dsn); err != nil { + t.Fatalf("up again: %v", err) + } + if itlAwaiting(t, pool, f.legacy) { + t.Fatal("legacy repo must be previously enabled after a re-Up") + } +} diff --git a/api/internal/store/migrations/00425_issue_tracker_repo_link.sql b/api/internal/store/migrations/00425_issue_tracker_repo_link.sql new file mode 100644 index 0000000000..8e1220831b --- /dev/null +++ b/api/internal/store/migrations/00425_issue_tracker_repo_link.sql @@ -0,0 +1,190 @@ +-- +goose Up + +-- This migration takes ACCESS EXCLUSIVE on repos and issues (new columns and CHECKs, which +-- scan every row while the lock is held) and SHARE ROW EXCLUSIVE on the other guarded tables +-- (CREATE TRIGGER). lock_timeout bounds only the wait for a lock, not how long it is held: fail +-- fast instead of queueing behind a long transaction and stalling every reader (the 00402 pattern). +SET LOCAL lock_timeout = '5s'; + +-- PRD #4173 M4: link a repo to an external issue tracker project, and keep that +-- link from ever being made once the repo has issue numbers of its own (D13). + +-- The never-enabled marker (DL-3). A repo is "awaiting its first enable" from the +-- moment the membership listing discovers it (UpsertRepo sets it) until it is first +-- enabled. The column default is false, so every row that exists when this migration +-- runs counts as previously enabled and can never be linked. +ALTER TABLE repos ADD COLUMN awaiting_first_enable boolean NOT NULL DEFAULT false; + +-- Clears the marker in the very statement that first enables the repo, on every +-- enable path (a raw UPDATE included), and refuses to set it back. No column list on +-- the trigger: it must see every UPDATE, because the refusal guards a write that +-- touches only awaiting_first_enable. +-- +goose StatementBegin +CREATE FUNCTION repos_awaiting_first_enable_guard() RETURNS trigger AS $$ +BEGIN + IF NEW.enabled THEN + NEW.awaiting_first_enable := false; + END IF; + IF TG_OP = 'UPDATE' AND OLD.awaiting_first_enable = false AND NEW.awaiting_first_enable = true THEN + RAISE EXCEPTION 'repos.awaiting_first_enable is never set back once cleared (repo %)', NEW.id; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; +-- +goose StatementEnd + +CREATE TRIGGER repos_awaiting_first_enable_guard + BEFORE INSERT OR UPDATE ON repos + FOR EACH ROW EXECUTE FUNCTION repos_awaiting_first_enable_guard(); + +-- The short name is capped at 44 characters so "-" always fits the 64 +-- characters of issues.issue_key (64 - 1 for the dash - 19 digits of a bigint). +-- The link from a repo to one project of one issue tracker connection, plus the +-- eligibility tag that selects which issues the sync reads. All six columns are set +-- together or not at all. The connection FK keeps the default NO ACTION (checked at +-- the end of the statement) so deleting a connection that a repo still names is +-- refused, while a user-delete cascade is not refused midway. +ALTER TABLE repos + ADD COLUMN issue_tracker_connection_id uuid REFERENCES issue_tracker_connections, + ADD COLUMN issue_tracker_project_id text, + ADD COLUMN issue_tracker_project_short text, + ADD COLUMN eligibility_tag_id text, + ADD COLUMN eligibility_tag_name text, + ADD COLUMN issue_tracker_linked_at timestamptz; + +ALTER TABLE repos ADD CONSTRAINT repos_issue_tracker_link_shape CHECK ( + (issue_tracker_connection_id IS NULL + AND issue_tracker_project_id IS NULL + AND issue_tracker_project_short IS NULL + AND eligibility_tag_id IS NULL + AND eligibility_tag_name IS NULL + AND issue_tracker_linked_at IS NULL) + OR + (issue_tracker_connection_id IS NOT NULL + AND issue_tracker_project_id IS NOT NULL + AND issue_tracker_project_short IS NOT NULL + AND eligibility_tag_id IS NOT NULL + AND eligibility_tag_name IS NOT NULL + AND issue_tracker_linked_at IS NOT NULL + AND char_length(issue_tracker_project_id) BETWEEN 1 AND 128 + AND char_length(issue_tracker_project_short) BETWEEN 1 AND 44 + AND char_length(eligibility_tag_id) BETWEEN 1 AND 128 + AND char_length(eligibility_tag_name) BETWEEN 1 AND 256 + AND issue_tracker_project_id !~ '[[:cntrl:]]' + AND issue_tracker_project_short !~ '[[:cntrl:]]' + AND eligibility_tag_id !~ '[[:cntrl:]]' + AND eligibility_tag_name !~ '[[:cntrl:]]' + AND issue_tracker_project_short ~ '^[A-Za-z][A-Za-z0-9_]*$') +); + +-- The readable id of an issue that came from an issue tracker (DL-1), for example +-- ABC-12. NULL for forge issues. The suffix must equal the row's own number, so a +-- key cannot name a different issue than the row it sits on. +ALTER TABLE issues ADD COLUMN issue_key text; +ALTER TABLE issues ADD CONSTRAINT issues_issue_key_shape CHECK ( + issue_key IS NULL OR ( + char_length(issue_key) <= 64 + AND issue_key ~ '^[A-Za-z][A-Za-z0-9_]*-[1-9][0-9]*$' + AND issue_key LIKE '%-' || forge_issue_iid::text) +); + +-- The issue-number guard (DL-4). One function for every table that stores an issue +-- number of a repo: a row with a non-NULL issue number whose repo is still awaiting +-- its first enable is refused, so no issue number can exist on a repo that could +-- still be linked. The link takes its own row lock; the marker only goes from true to +-- false, so a write that passed this guard proves the repo can never be linked later. +-- +-- Trigger arguments: the repo id column name, then one or more issue number column +-- names. A NULL repo id (recommendation_filed_issues.filed_repo_id) is skipped. +-- SQLSTATE UZ413 is this guard's own code (class UZ is not a PostgreSQL class); the +-- store maps it, nothing else raises it. +-- +goose StatementBegin +CREATE FUNCTION issue_number_awaiting_first_enable_guard() RETURNS trigger AS $$ +DECLARE + j jsonb := to_jsonb(NEW); + repo_val text := j ->> TG_ARGV[0]; + has_num boolean := false; + i int; +BEGIN + -- Fail closed: a trigger argument that names no column of the row (a rename or a + -- typo) must not turn the guard into a silent pass. + IF NOT (j ? TG_ARGV[0]) THEN + RAISE EXCEPTION 'issue number guard on %: no column %', TG_TABLE_NAME, TG_ARGV[0]; + END IF; + FOR i IN 1 .. TG_NARGS - 1 LOOP + IF NOT (j ? TG_ARGV[i]) THEN + RAISE EXCEPTION 'issue number guard on %: no column %', TG_TABLE_NAME, TG_ARGV[i]; + END IF; + END LOOP; + IF repo_val IS NULL THEN + RETURN NEW; + END IF; + FOR i IN 1 .. TG_NARGS - 1 LOOP + IF j ->> TG_ARGV[i] IS NOT NULL THEN + has_num := true; + END IF; + END LOOP; + IF NOT has_num THEN + RETURN NEW; + END IF; + IF EXISTS (SELECT 1 FROM repos WHERE id = repo_val::uuid AND awaiting_first_enable) THEN + RAISE EXCEPTION 'repo % has never been enabled: % may not store an issue number', repo_val, TG_TABLE_NAME + USING ERRCODE = 'UZ413'; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; +-- +goose StatementEnd + +CREATE TRIGGER issues_awaiting_first_enable_guard + BEFORE INSERT OR UPDATE OF repo_id, forge_issue_iid ON issues + FOR EACH ROW EXECUTE FUNCTION issue_number_awaiting_first_enable_guard('repo_id', 'forge_issue_iid'); +CREATE TRIGGER runs_awaiting_first_enable_guard + BEFORE INSERT OR UPDATE OF repo_id, issue_iid, continued_issue_iid ON runs + FOR EACH ROW EXECUTE FUNCTION issue_number_awaiting_first_enable_guard('repo_id', 'issue_iid', 'continued_issue_iid'); +CREATE TRIGGER run_schedules_awaiting_first_enable_guard + BEFORE INSERT OR UPDATE OF repo_id, issue_iid ON run_schedules + FOR EACH ROW EXECUTE FUNCTION issue_number_awaiting_first_enable_guard('repo_id', 'issue_iid'); +CREATE TRIGGER autopilot_triggers_awaiting_first_enable_guard + BEFORE INSERT OR UPDATE OF repo_id, issue_iid ON autopilot_triggers + FOR EACH ROW EXECUTE FUNCTION issue_number_awaiting_first_enable_guard('repo_id', 'issue_iid'); +CREATE TRIGGER github_project_items_awaiting_first_enable_guard + BEFORE INSERT OR UPDATE OF repo_id, forge_issue_iid ON github_project_items + FOR EACH ROW EXECUTE FUNCTION issue_number_awaiting_first_enable_guard('repo_id', 'forge_issue_iid'); +CREATE TRIGGER issue_proposals_awaiting_first_enable_guard + BEFORE INSERT OR UPDATE OF repo_id, created_issue_iid ON issue_proposals + FOR EACH ROW EXECUTE FUNCTION issue_number_awaiting_first_enable_guard('repo_id', 'created_issue_iid'); +CREATE TRIGGER recommendation_filed_issues_awaiting_first_enable_guard + BEFORE INSERT OR UPDATE OF filed_repo_id, filed_issue_iid ON recommendation_filed_issues + FOR EACH ROW EXECUTE FUNCTION issue_number_awaiting_first_enable_guard('filed_repo_id', 'filed_issue_iid'); +CREATE TRIGGER finding_dispositions_awaiting_first_enable_guard + BEFORE INSERT OR UPDATE OF repo_id, filed_issue_iid ON finding_dispositions + FOR EACH ROW EXECUTE FUNCTION issue_number_awaiting_first_enable_guard('repo_id', 'filed_issue_iid'); +CREATE TRIGGER finding_group_operations_awaiting_first_enable_guard + BEFORE INSERT OR UPDATE OF repo_id, issue_iid ON finding_group_operations + FOR EACH ROW EXECUTE FUNCTION issue_number_awaiting_first_enable_guard('repo_id', 'issue_iid'); + +-- +goose Down +DROP TRIGGER finding_group_operations_awaiting_first_enable_guard ON finding_group_operations; +DROP TRIGGER finding_dispositions_awaiting_first_enable_guard ON finding_dispositions; +DROP TRIGGER recommendation_filed_issues_awaiting_first_enable_guard ON recommendation_filed_issues; +DROP TRIGGER issue_proposals_awaiting_first_enable_guard ON issue_proposals; +DROP TRIGGER github_project_items_awaiting_first_enable_guard ON github_project_items; +DROP TRIGGER autopilot_triggers_awaiting_first_enable_guard ON autopilot_triggers; +DROP TRIGGER run_schedules_awaiting_first_enable_guard ON run_schedules; +DROP TRIGGER runs_awaiting_first_enable_guard ON runs; +DROP TRIGGER issues_awaiting_first_enable_guard ON issues; +DROP FUNCTION issue_number_awaiting_first_enable_guard(); +ALTER TABLE issues DROP CONSTRAINT issues_issue_key_shape; +ALTER TABLE issues DROP COLUMN issue_key; +ALTER TABLE repos DROP CONSTRAINT repos_issue_tracker_link_shape; +ALTER TABLE repos + DROP COLUMN issue_tracker_linked_at, + DROP COLUMN eligibility_tag_name, + DROP COLUMN eligibility_tag_id, + DROP COLUMN issue_tracker_project_short, + DROP COLUMN issue_tracker_project_id, + DROP COLUMN issue_tracker_connection_id; +DROP TRIGGER repos_awaiting_first_enable_guard ON repos; +DROP FUNCTION repos_awaiting_first_enable_guard(); +ALTER TABLE repos DROP COLUMN awaiting_first_enable; diff --git a/api/internal/store/models.go b/api/internal/store/models.go index 15c5f9d29d..47359e4618 100644 --- a/api/internal/store/models.go +++ b/api/internal/store/models.go @@ -583,6 +583,7 @@ type Issue struct { SyncedAt pgtype.Timestamptz `json:"synced_at"` BoardPosition pgtype.Int8 `json:"board_position"` AssigneeIds []byte `json:"assignee_ids"` + IssueKey pgtype.Text `json:"issue_key"` } type IssueProposal struct { @@ -1055,6 +1056,13 @@ type Repo struct { CrossCheckRepoInstructionsEnabled bool `json:"cross_check_repo_instructions_enabled"` IgnoredInferredCapabilities []string `json:"ignored_inferred_capabilities"` AgentSourceDefault pgtype.Text `json:"agent_source_default"` + AwaitingFirstEnable bool `json:"awaiting_first_enable"` + IssueTrackerConnectionID pgtype.UUID `json:"issue_tracker_connection_id"` + IssueTrackerProjectID pgtype.Text `json:"issue_tracker_project_id"` + IssueTrackerProjectShort pgtype.Text `json:"issue_tracker_project_short"` + EligibilityTagID pgtype.Text `json:"eligibility_tag_id"` + EligibilityTagName pgtype.Text `json:"eligibility_tag_name"` + IssueTrackerLinkedAt pgtype.Timestamptz `json:"issue_tracker_linked_at"` } type RepoToolProfile struct { diff --git a/api/internal/store/never_enabled.go b/api/internal/store/never_enabled.go new file mode 100644 index 0000000000..b07e4bfa37 --- /dev/null +++ b/api/internal/store/never_enabled.go @@ -0,0 +1,39 @@ +package store + +import ( + "errors" + + "github.com/jackc/pgx/v5/pgconn" +) + +// ErrRepoNeverEnabled is the typed refusal for an operation that would sync a +// repo's issues, start a run, or store an issue number on a repo that has never +// been enabled (repos.awaiting_first_enable, PRD 4173 D13). Callers check the +// repo row's AwaitingFirstEnable with RefuseNeverEnabled BEFORE any forge or +// issue tracker call, and map a UZ413 from the database through +// MapNeverEnabled; handlers answer both with 409. +var ErrRepoNeverEnabled = errors.New("the repo has never been enabled; enable it first") + +// neverEnabledSQLState is the SQLSTATE the issue-number guard trigger +// (issue_number_awaiting_first_enable_guard) raises. Class UZ is not a +// PostgreSQL class. +const neverEnabledSQLState = "UZ413" + +// RefuseNeverEnabled returns ErrRepoNeverEnabled when the repo row's +// awaiting_first_enable marker is set, else nil. +func RefuseNeverEnabled(awaitingFirstEnable bool) error { + if awaitingFirstEnable { + return ErrRepoNeverEnabled + } + return nil +} + +// MapNeverEnabled returns ErrRepoNeverEnabled (wrapping err) when err carries the +// guard trigger's SQLSTATE, and err unchanged otherwise (nil stays nil). +func MapNeverEnabled(err error) error { + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) && pgErr.Code == neverEnabledSQLState { + return errors.Join(ErrRepoNeverEnabled, err) + } + return err +} diff --git a/api/internal/store/queries/forge.sql b/api/internal/store/queries/forge.sql index 7ace732cb8..8856c40873 100644 --- a/api/internal/store/queries/forge.sql +++ b/api/internal/store/queries/forge.sql @@ -107,9 +107,11 @@ WHERE id = $1; -- name: UpsertRepo :one -- Upserted whenever the membership list is fetched. Never touches `enabled`, so --- a re-listing does not un-enable a repo the user already turned on. -INSERT INTO repos (connection_id, forge_project_id, path_with_namespace, web_url, default_branch) -VALUES ($1, $2, $3, $4, $5) +-- a re-listing does not un-enable a repo the user already turned on. The insert +-- branch marks a newly discovered repo awaiting_first_enable (PRD #4173 D13); the +-- conflict branch never touches it, and the repos trigger clears it on first enable. +INSERT INTO repos (connection_id, forge_project_id, path_with_namespace, web_url, default_branch, awaiting_first_enable) +VALUES ($1, $2, $3, $4, $5, true) ON CONFLICT (connection_id, forge_project_id) DO UPDATE SET path_with_namespace = EXCLUDED.path_with_namespace, web_url = EXCLUDED.web_url, @@ -139,6 +141,8 @@ ORDER BY r.path_with_namespace ASC; SELECT r.id, r.connection_id, r.forge_project_id, r.path_with_namespace, r.web_url, r.default_branch, r.enabled, r.fold_improve_uzi_backlog, r.guardrail_override_reason, r.guardrail_override_by, r.guardrail_override_at, + r.issue_tracker_connection_id, r.issue_tracker_project_id, r.issue_tracker_project_short, + r.eligibility_tag_id, r.eligibility_tag_name, r.issue_tracker_linked_at, r.awaiting_first_enable, c.forge_type, c.base_url, c.token_ciphertext, c.user_id, c.bot_forge_user_id FROM repos r JOIN forge_connections c ON c.id = r.connection_id @@ -159,6 +163,8 @@ WHERE r.id = $1 AND c.user_id = $2; -- plus forge_project_id for slug/issue resolution. SELECT r.id, r.connection_id, r.forge_project_id, r.path_with_namespace, r.web_url, r.default_branch, r.enabled, + r.issue_tracker_connection_id, r.issue_tracker_project_id, r.issue_tracker_project_short, + r.eligibility_tag_id, r.eligibility_tag_name, r.issue_tracker_linked_at, r.awaiting_first_enable, c.forge_type, c.base_url, c.token_ciphertext, c.user_id FROM repos r JOIN forge_connections c ON c.id = r.connection_id @@ -386,6 +392,8 @@ RETURNING *; -- engine's poller set. SELECT r.id, r.connection_id, r.forge_project_id, r.path_with_namespace, r.web_url, r.default_branch, r.enabled, + r.issue_tracker_connection_id, r.issue_tracker_project_id, r.issue_tracker_project_short, + r.eligibility_tag_id, r.eligibility_tag_name, r.issue_tracker_linked_at, r.awaiting_first_enable, c.forge_type, c.base_url, c.token_ciphertext, c.user_id FROM repos r JOIN forge_connections c ON c.id = r.connection_id @@ -790,6 +798,11 @@ FROM latest l JOIN issues i ON i.repo_id = @repo_id AND i.forge_issue_iid = l.issue_iid WHERE l.status = 'completed' AND l.mr_iid IS NOT NULL + -- A repo linked to an external issue tracker (PRD #4173 M4) is owned by + -- ListBoardFreeMRStateWatchCandidates entirely: uzi moves no card there (writes arrive + -- in M5), so its MR state must not depend on the issue cache or a column label. Excluding + -- the repo here keeps the two lanes disjoint. + AND NOT EXISTS (SELECT 1 FROM repos lr WHERE lr.id = @repo_id AND lr.issue_tracker_connection_id IS NOT NULL) AND ( -- Lane A (UNCHANGED): open-issue board-move watch — card in Human Review -- (close-edge) or run already recorded closed (reopen-edge, Decision 10). @@ -829,12 +842,16 @@ LIMIT 100; -- gate, the mr_rework ledger eviction, and stop-on-close cancellation all work for these runs. -- Self-bounding like Lane B: poll a run only until its mr_state reaches a terminal value -- (merged/closed), then it drops out of this set. LIMIT 100 is a hardcoded burst bound (mirrors --- ListMRWatchCandidates), not a sqlc param, so zero Go signature change. Keep this rationale +-- ListMRWatchCandidates), not a sqlc param, so zero Go signature change. A repo linked to an +-- external issue tracker (PRD #4173 M4) is widened IN whole: every issue-lane run of such a repo +-- belongs here (the board lane excludes the repo), so linked runs record mr_state and cancel +-- rework with no board card; an unlinked repo matches exactly as before. Keep this rationale -- ABOVE the statement — a comment trailing after the `;` is grabbed by sqlc as the NEXT query's doc. SELECT id, branch, mr_iid, mr_state FROM runs WHERE repo_id = @repo_id::uuid - AND (issue_iid IS NULL OR kind IN ('prompt', 'self_improve')) + AND (issue_iid IS NULL OR kind IN ('prompt', 'self_improve') + OR EXISTS (SELECT 1 FROM repos lr WHERE lr.id = @repo_id::uuid AND lr.issue_tracker_connection_id IS NOT NULL)) AND status = 'completed' AND mr_iid IS NOT NULL AND (mr_state IS NULL OR mr_state IN ('opened', 'locked')) diff --git a/api/internal/store/queries/issue_tracker_connections.sql b/api/internal/store/queries/issue_tracker_connections.sql index 625b01d4e6..6de99cf6bb 100644 --- a/api/internal/store/queries/issue_tracker_connections.sql +++ b/api/internal/store/queries/issue_tracker_connections.sql @@ -12,6 +12,14 @@ SET account_id = EXCLUDED.account_id, account_login = EXCLUDED.account_login, token_ciphertext = EXCLUDED.token_ciphertext, last_verified_at = now() +-- Defence in depth for the reconnect rule (PRD #4173 M4 owner guidance): a conflicting row +-- whose account would change is left alone while a repo links to it, and the statement then +-- returns no row. NOT a complete guard: under READ COMMITTED the subquery reads the snapshot +-- of the statement, taken before it waits on a concurrent link's row lock, so it cannot see +-- a link that commits during that wait. The service takes the row lock and counts in a +-- separate statement first (LockIssueTrackerConnectionByIdentityForUpdate). +WHERE issue_tracker_connections.account_id = EXCLUDED.account_id + OR NOT EXISTS (SELECT 1 FROM repos WHERE repos.issue_tracker_connection_id = issue_tracker_connections.id) RETURNING *; -- name: ListIssueTrackerConnectionsByUser :many @@ -22,3 +30,29 @@ SELECT * FROM issue_tracker_connections WHERE id = $1 AND user_id = $2; -- name: DeleteIssueTrackerConnectionForUser :execrows DELETE FROM issue_tracker_connections WHERE id = $1 AND user_id = $2; + +-- name: LockIssueTrackerConnectionByIdentityForUpdate :one +-- Reconnect step 1 (PRD #4173 M4 owner guidance): read the existing row of the +-- (user, kind, base_url) identity and hold FOR UPDATE, so a link (which holds the +-- row FOR SHARE) and a reconnect that changes the account cannot interleave. No row +-- means a first connect, which no repo can link to yet. +SELECT * FROM issue_tracker_connections +WHERE user_id = $1 AND kind = $2 AND base_url = $3 +FOR UPDATE; + +-- name: LockIssueTrackerConnectionForShareForUser :one +-- The link's re-read of the connection inside its transaction. FOR SHARE conflicts +-- with the reconnect's FOR UPDATE, which the repos foreign key's own key-share lock +-- does not when the upsert changes only non-key columns. +SELECT * FROM issue_tracker_connections WHERE id = $1 AND user_id = $2 +FOR SHARE; + +-- name: InsertIssueTrackerConnectionIfAbsent :one +-- The first-connect insert: never updates, so it cannot overwrite a row (and its account) +-- that another connect created. No row returned means the identity exists already, and the +-- caller re-enters the locked reconnect path. +INSERT INTO issue_tracker_connections ( + user_id, kind, base_url, account_id, account_login, token_ciphertext, last_verified_at +) VALUES ($1, $2, $3, $4, $5, $6, now()) +ON CONFLICT (user_id, kind, base_url) DO NOTHING +RETURNING *; diff --git a/api/internal/store/queries/issue_tracker_links.sql b/api/internal/store/queries/issue_tracker_links.sql new file mode 100644 index 0000000000..080c435f11 --- /dev/null +++ b/api/internal/store/queries/issue_tracker_links.sql @@ -0,0 +1,68 @@ +-- Repo to issue tracker project links (PRD #4173 M4, DL-3..DL-5). + +-- name: GetRepoIssueSourceForUser :one +-- The one owner-scoped row the per-repo resolvers read (DL-5): the repo, its forge +-- connection and its issue tracker link. The binding is keyed by repo row id and the +-- owning user, never by forge project number. linked_connection_id is the link's +-- connection ONLY when it belongs to the same user as the forge connection; a linked +-- repo whose connection is missing for that owner reads NULL here and the resolver +-- fails closed. +SELECT r.id, r.forge_project_id, r.awaiting_first_enable, + r.issue_tracker_connection_id, r.issue_tracker_project_id, r.issue_tracker_project_short, + r.eligibility_tag_id, r.eligibility_tag_name, r.issue_tracker_linked_at, + c.forge_type, c.base_url, c.token_ciphertext, + itc.id AS linked_connection_id +FROM repos r +JOIN forge_connections c ON c.id = r.connection_id +LEFT JOIN issue_tracker_connections itc + ON itc.id = r.issue_tracker_connection_id AND itc.user_id = c.user_id +WHERE r.id = $1 AND c.user_id = $2; + +-- name: LockRepoForLinkForUser :one +-- Takes the repo row lock that the link and the first enable share (DL-4), scoped to +-- the owning user. Call inside a transaction; the returned markers are read after the +-- lock, so an enable that committed first is visible. +SELECT r.id, r.awaiting_first_enable, r.issue_tracker_connection_id +FROM repos r +JOIN forge_connections c ON c.id = r.connection_id +WHERE r.id = $1 AND c.user_id = $2 +FOR UPDATE OF r; + +-- name: RepoStoresIssueNumber :one +-- True when any table that stores an issue number for the repo has such a row. The +-- guard triggers (migration 00425) make this false for an awaiting repo; the link +-- checks it anyway as defence in depth. Keep in step with the trigger list there. +SELECT ( + EXISTS (SELECT 1 FROM issues WHERE issues.repo_id = $1 AND issues.forge_issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM runs WHERE runs.repo_id = $1 AND (runs.issue_iid IS NOT NULL OR runs.continued_issue_iid IS NOT NULL)) + OR EXISTS (SELECT 1 FROM run_schedules WHERE run_schedules.repo_id = $1 AND run_schedules.issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM autopilot_triggers WHERE autopilot_triggers.repo_id = $1 AND autopilot_triggers.issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM github_project_items WHERE github_project_items.repo_id = $1 AND github_project_items.forge_issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM issue_proposals WHERE issue_proposals.repo_id = $1 AND issue_proposals.created_issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM recommendation_filed_issues WHERE recommendation_filed_issues.filed_repo_id = $1 AND recommendation_filed_issues.filed_issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM finding_dispositions WHERE finding_dispositions.repo_id = $1 AND finding_dispositions.filed_issue_iid IS NOT NULL) + OR EXISTS (SELECT 1 FROM finding_group_operations WHERE finding_group_operations.repo_id = $1 AND finding_group_operations.issue_iid IS NOT NULL) +)::boolean AS stores_issue_number; + +-- name: SetRepoIssueTrackerLink :one +-- Writes the link, owner-scoped in the one statement: the repo must belong to @user_id +-- through its forge connection and the issue tracker connection must belong to the same +-- user. Only a repo that has never been enabled and is not yet linked matches; no row +-- means refused. Call after LockRepoForLinkForUser in the same transaction. +UPDATE repos r +SET issue_tracker_connection_id = @connection_id::uuid, + issue_tracker_project_id = @project_id::text, + issue_tracker_project_short = @project_short::text, + eligibility_tag_id = @tag_id::text, + eligibility_tag_name = @tag_name::text, + issue_tracker_linked_at = now() +FROM forge_connections c, issue_tracker_connections itc +WHERE r.id = @repo_id + AND c.id = r.connection_id AND c.user_id = @user_id + AND itc.id = @connection_id::uuid AND itc.user_id = @user_id + AND r.awaiting_first_enable AND r.issue_tracker_connection_id IS NULL +RETURNING r.*; + +-- name: CountReposLinkedToIssueTrackerConnection :one +-- How many repos name this issue tracker connection; deleting it is refused while any do. +SELECT count(*) FROM repos WHERE issue_tracker_connection_id = @connection_id::uuid; diff --git a/api/internal/store/queries/runtime.sql b/api/internal/store/queries/runtime.sql index 48fad79f04..a4340d8961 100644 --- a/api/internal/store/queries/runtime.sql +++ b/api/internal/store/queries/runtime.sql @@ -10728,7 +10728,9 @@ ORDER BY id DESC; SELECT r.status, r.issue_iid, r.repo_id, r.origin_column, r.board_column, r.move_pending_since, r.auto_approve, r.mr_iid, rp.forge_project_id, rp.web_url AS repo_web_url, - c.forge_type, c.base_url, c.token_ciphertext + rp.issue_tracker_connection_id, rp.awaiting_first_enable, + c.forge_type, c.base_url, c.token_ciphertext, + r.user_id FROM runs r JOIN repos rp ON rp.id = r.repo_id JOIN forge_connections c ON c.id = rp.connection_id AND c.user_id = r.user_id -- #1688: owner-scoped token @@ -10743,8 +10745,11 @@ WHERE r.id = @run_id; -- not currently hold return no row, so a cross-tenant read is a 404, not a leak. A -- repo-less run has no repos row and returns no row too; the service checks repo_id -- off the owned run FIRST so it can tell that apart and answer 409. -SELECT rp.forge_project_id, - c.forge_type, c.base_url, c.token_ciphertext, c.bot_forge_user_id +-- PRD #4173 M4 adds repo_id, the connection owner's user_id and the repo's issue +-- tracker link, so the worker proxy can resolve the issue source per repo and owner. +SELECT rp.id AS repo_id, rp.forge_project_id, + rp.issue_tracker_connection_id, + c.user_id, c.forge_type, c.base_url, c.token_ciphertext, c.bot_forge_user_id FROM runs r JOIN repos rp ON rp.id = r.repo_id JOIN forge_connections c ON c.id = rp.connection_id AND c.user_id = r.user_id -- #1688: owner-scoped token diff --git a/api/internal/store/runtime.sql.go b/api/internal/store/runtime.sql.go index 12cfae5684..80d4336937 100644 --- a/api/internal/store/runtime.sql.go +++ b/api/internal/store/runtime.sql.go @@ -7659,8 +7659,9 @@ func (q *Queries) GetRunForWorkerUser(ctx context.Context, arg GetRunForWorkerUs } const getRunForgeConnForWorker = `-- name: GetRunForgeConnForWorker :one -SELECT rp.forge_project_id, - c.forge_type, c.base_url, c.token_ciphertext, c.bot_forge_user_id +SELECT rp.id AS repo_id, rp.forge_project_id, + rp.issue_tracker_connection_id, + c.user_id, c.forge_type, c.base_url, c.token_ciphertext, c.bot_forge_user_id FROM runs r JOIN repos rp ON rp.id = r.repo_id JOIN forge_connections c ON c.id = rp.connection_id AND c.user_id = r.user_id -- #1688: owner-scoped token @@ -7673,11 +7674,14 @@ type GetRunForgeConnForWorkerParams struct { } type GetRunForgeConnForWorkerRow struct { - ForgeProjectID int64 `json:"forge_project_id"` - ForgeType string `json:"forge_type"` - BaseUrl string `json:"base_url"` - TokenCiphertext []byte `json:"token_ciphertext"` - BotForgeUserID int64 `json:"bot_forge_user_id"` + RepoID uuid.UUID `json:"repo_id"` + ForgeProjectID int64 `json:"forge_project_id"` + IssueTrackerConnectionID pgtype.UUID `json:"issue_tracker_connection_id"` + UserID uuid.UUID `json:"user_id"` + ForgeType string `json:"forge_type"` + BaseUrl string `json:"base_url"` + TokenCiphertext []byte `json:"token_ciphertext"` + BotForgeUserID int64 `json:"bot_forge_user_id"` } // The forge connection facts a WORKER-authenticated run needs to build a driver and @@ -7688,11 +7692,16 @@ type GetRunForgeConnForWorkerRow struct { // not currently hold return no row, so a cross-tenant read is a 404, not a leak. A // repo-less run has no repos row and returns no row too; the service checks repo_id // off the owned run FIRST so it can tell that apart and answer 409. +// PRD #4173 M4 adds repo_id, the connection owner's user_id and the repo's issue +// tracker link, so the worker proxy can resolve the issue source per repo and owner. func (q *Queries) GetRunForgeConnForWorker(ctx context.Context, arg GetRunForgeConnForWorkerParams) (GetRunForgeConnForWorkerRow, error) { row := q.db.QueryRow(ctx, getRunForgeConnForWorker, arg.RunID, arg.WorkerID) var i GetRunForgeConnForWorkerRow err := row.Scan( + &i.RepoID, &i.ForgeProjectID, + &i.IssueTrackerConnectionID, + &i.UserID, &i.ForgeType, &i.BaseUrl, &i.TokenCiphertext, @@ -7735,7 +7744,9 @@ const getRunMoveContext = `-- name: GetRunMoveContext :one SELECT r.status, r.issue_iid, r.repo_id, r.origin_column, r.board_column, r.move_pending_since, r.auto_approve, r.mr_iid, rp.forge_project_id, rp.web_url AS repo_web_url, - c.forge_type, c.base_url, c.token_ciphertext + rp.issue_tracker_connection_id, rp.awaiting_first_enable, + c.forge_type, c.base_url, c.token_ciphertext, + r.user_id FROM runs r JOIN repos rp ON rp.id = r.repo_id JOIN forge_connections c ON c.id = rp.connection_id AND c.user_id = r.user_id -- #1688: owner-scoped token @@ -7743,19 +7754,22 @@ WHERE r.id = $1 ` type GetRunMoveContextRow struct { - Status string `json:"status"` - IssueIid pgtype.Int8 `json:"issue_iid"` - RepoID pgtype.UUID `json:"repo_id"` - OriginColumn pgtype.Text `json:"origin_column"` - BoardColumn pgtype.Text `json:"board_column"` - MovePendingSince pgtype.Timestamptz `json:"move_pending_since"` - AutoApprove bool `json:"auto_approve"` - MrIid pgtype.Int8 `json:"mr_iid"` - ForgeProjectID int64 `json:"forge_project_id"` - RepoWebUrl string `json:"repo_web_url"` - ForgeType string `json:"forge_type"` - BaseUrl string `json:"base_url"` - TokenCiphertext []byte `json:"token_ciphertext"` + Status string `json:"status"` + IssueIid pgtype.Int8 `json:"issue_iid"` + RepoID pgtype.UUID `json:"repo_id"` + OriginColumn pgtype.Text `json:"origin_column"` + BoardColumn pgtype.Text `json:"board_column"` + MovePendingSince pgtype.Timestamptz `json:"move_pending_since"` + AutoApprove bool `json:"auto_approve"` + MrIid pgtype.Int8 `json:"mr_iid"` + ForgeProjectID int64 `json:"forge_project_id"` + RepoWebUrl string `json:"repo_web_url"` + IssueTrackerConnectionID pgtype.UUID `json:"issue_tracker_connection_id"` + AwaitingFirstEnable bool `json:"awaiting_first_enable"` + ForgeType string `json:"forge_type"` + BaseUrl string `json:"base_url"` + TokenCiphertext []byte `json:"token_ciphertext"` + UserID uuid.UUID `json:"user_id"` } // Column automation (PRD #12 M1) ------------------------------------------ @@ -7785,9 +7799,12 @@ func (q *Queries) GetRunMoveContext(ctx context.Context, runID uuid.UUID) (GetRu &i.MrIid, &i.ForgeProjectID, &i.RepoWebUrl, + &i.IssueTrackerConnectionID, + &i.AwaitingFirstEnable, &i.ForgeType, &i.BaseUrl, &i.TokenCiphertext, + &i.UserID, ) return i, err } diff --git a/api/internal/workersvc/chat.go b/api/internal/workersvc/chat.go index c12b95315d..ab3b37dbec 100644 --- a/api/internal/workersvc/chat.go +++ b/api/internal/workersvc/chat.go @@ -755,12 +755,17 @@ func (s *Service) CreateProposal(ctx context.Context, wkr store.Worker, runID, r } // The target issue repo must belong to the same user (never trust a worker- // supplied repo_id blindly). - if _, err := s.q.GetRepoForUser(ctx, store.GetRepoForUserParams{ID: repoID, UserID: wkr.UserID}); err != nil { + prow, err := s.q.GetRepoForUser(ctx, store.GetRepoForUserParams{ID: repoID, UserID: wkr.UserID}) + if err != nil { if errors.Is(err, pgx.ErrNoRows) { return store.IssueProposal{}, ErrRepoNotFound } return store.IssueProposal{}, err } + // PRD 4173 D13: a proposal for a never-enabled repo could never be confirmed. + if err := store.RefuseNeverEnabled(prow.AwaitingFirstEnable); err != nil { + return store.IssueProposal{}, err + } count, err := s.q.CountPendingProposalsForRun(ctx, runID) if err != nil { return store.IssueProposal{}, err diff --git a/api/internal/workersvc/ci_fix.go b/api/internal/workersvc/ci_fix.go index 538452a2a7..2db5544d34 100644 --- a/api/internal/workersvc/ci_fix.go +++ b/api/internal/workersvc/ci_fix.go @@ -183,6 +183,10 @@ func (s *Service) createCIFixRun(ctx context.Context, userID, repoID uuid.UUID, } return store.Run{}, err } + // PRD 4173 D13: no run of any kind on a never-enabled repo; refused before any forge call. + if err := store.RefuseNeverEnabled(row.AwaitingFirstEnable); err != nil { + return store.Run{}, err + } // #66 D1 layer 2: the shared service-layer guardrail. Covers both the manual // Fix-CI button and the automatic ci-autofix poller (createCIFixRun is shared). if err := s.guardDefaultBranch(ctx, row); err != nil { diff --git a/api/internal/workersvc/composite.go b/api/internal/workersvc/composite.go index 11b11d1c2c..faffdfb6d5 100644 --- a/api/internal/workersvc/composite.go +++ b/api/internal/workersvc/composite.go @@ -12,6 +12,7 @@ import ( "github.com/vtmocanu/uzi/api/internal/forge" "github.com/vtmocanu/uzi/api/internal/issueinput" + "github.com/vtmocanu/uzi/api/internal/issuesource" "github.com/vtmocanu/uzi/api/internal/privcheck" "github.com/vtmocanu/uzi/api/internal/store" ) @@ -133,6 +134,12 @@ func (s *Service) ConfirmProposalForUser(ctx context.Context, userID, runID, pro } return CreatedIssue{}, err } + // PRD 4173 D13: no issue is created on a never-enabled repo. The claim reverts so the + // proposal stays pending; nothing has touched the forge. + if err := store.RefuseNeverEnabled(repo.AwaitingFirstEnable); err != nil { + s.revertProposal(ctx, propID) + return CreatedIssue{}, err + } f, err := s.forges.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { @@ -147,12 +154,19 @@ func (s *Service) ConfirmProposalForUser(ctx context.Context, userID, runID, pro } } - var tr forge.IssueTracker = f + // PRD #4173 DL-5: a linked repo's resolver wraps the write guard, which refuses + // CreateIssue before any request (writes arrive in M5). The claim reverts below, so + // the proposal stays pending and nothing was created. + tr, err := issuesource.For(ctx, s.issueSources, repo.IssueTrackerConnectionID.Valid, userID, repo.ID, f) + if err != nil { + s.revertProposal(ctx, propID) + return CreatedIssue{}, fmt.Errorf("%w: %v", ErrForgeBuild, err) + } created, err := tr.CreateIssue(ctx, repo.ForgeProjectID, claim.Title, claim.Description, labels) if err != nil { s.revertProposal(ctx, propID) // err is already PAT-redacted by the driver. - return CreatedIssue{}, fmt.Errorf("%w: %v", ErrForgeIssueWrite, err) + return CreatedIssue{}, fmt.Errorf("%w: %w", ErrForgeIssueWrite, err) } // Settle confirming -> confirmed with the created iid. This row is ours (we hold @@ -206,11 +220,19 @@ func (s *Service) StartRunForUser(ctx context.Context, userID, repoID uuid.UUID, } return store.Run{}, err } + // PRD 4173 D13: refused before the forge read below; CreateRun repeats the gate. + if err := store.RefuseNeverEnabled(repo.AwaitingFirstEnable); err != nil { + return store.Run{}, err + } f, err := s.forges.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { return store.Run{}, fmt.Errorf("%w: %v", ErrForgeBuild, err) } - capture, err := issueinput.FetchWithTimeout(ctx, f, f, repo.ForgeProjectID, issueIID, repo.BotForgeUserID, issueinput.InteractiveAssessmentTimeout) + tr, err := issuesource.For(ctx, s.issueSources, repo.IssueTrackerConnectionID.Valid, userID, repo.ID, f) + if err != nil { + return store.Run{}, fmt.Errorf("%w: %v", ErrForgeBuild, err) + } + capture, err := issueinput.FetchWithTimeout(ctx, f, tr, repo.ForgeProjectID, issueIID, repo.BotForgeUserID, issueinput.InteractiveAssessmentTimeout) if err != nil { if forge.IsIssueResponseTooLarge(err) { return store.Run{}, fmt.Errorf("%w: %w", ErrDescriptionTooLarge, err) diff --git a/api/internal/workersvc/issue_sources_test.go b/api/internal/workersvc/issue_sources_test.go new file mode 100644 index 0000000000..a93a50db0c --- /dev/null +++ b/api/internal/workersvc/issue_sources_test.go @@ -0,0 +1,281 @@ +package workersvc + +import ( + "context" + "errors" + "strings" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgtype" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/runkind" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// PRD #4173 M4 (DL-5): on a repo linked to an external issue tracker every issue-side +// call of this package goes through the resolver; the code host (author eligibility) +// stays on the forge; a service with no resolver fails closed instead of using the forge. + +// ytTracker stands in for a linked repo's issue tracker: its issues and comments carry +// no forge user ids, as the YouTrack driver's do. +type ytTracker struct { + forgetest.BaseFake + createErr error +} + +func (*ytTracker) GetIssue(_ context.Context, _, iid int64) (forge.Issue, error) { + return forge.Issue{IID: iid, Title: "yt title", Description: "yt body", State: "opened"}, nil +} +func (*ytTracker) ListIssueComments(context.Context, int64, int64) ([]forge.IssueComment, error) { + return []forge.IssueComment{{Body: "yt comment"}}, nil +} +func (y *ytTracker) CreateIssue(context.Context, int64, string, string, []string) (forge.Issue, error) { + return forge.Issue{}, y.createErr +} + +type fakeSources struct { + tr forge.IssueTracker + err error + calls []struct{ user, repo uuid.UUID } +} + +func (f *fakeSources) IssueTrackerForRepo(_ context.Context, userID, repoID uuid.UUID) (forge.IssueTracker, error) { + f.calls = append(f.calls, struct{ user, repo uuid.UUID }{userID, repoID}) + return f.tr, f.err +} + +func linkedRepoRow() store.GetRepoForUserRow { + return store.GetRepoForUserRow{ + ID: uuid.New(), ForgeProjectID: 7, BotForgeUserID: 1, + ForgeType: "gitlab", BaseUrl: "https://gitlab.example", TokenCiphertext: []byte("sealed"), + IssueTrackerConnectionID: pgtype.UUID{Bytes: uuid.New(), Valid: true}, + } +} + +func hasCall(calls []string, prefix string) bool { + for _, c := range calls { + if strings.HasPrefix(c, prefix) { + return true + } + } + return false +} + +// linkedFixture builds a Service whose repo row is linked, with a recorded forge (the +// code host) and a recorded issue tracker behind the resolver. +func linkedRunFixture(t *testing.T, withSources bool) (*Service, *fakeStore, *forgetest.Recorder, *forgetest.Recorder, *fakeSources) { + t.Helper() + fs := &fakeStore{repoRow: linkedRepoRow(), issueByID: store.Issue{Labels: uziLabels()}, createRunResult: store.Run{ID: uuid.New()}} + forgeRec := forgetest.NewRecorder(&m2Forge{disposition: forge.AuthorEligible}) + trRec := forgetest.NewRecorder(&ytTracker{}) + svc := New(fs, newBox(t), testParams()) + svc.SetForges(m2Builder{forgeRec}) + src := &fakeSources{tr: trRec} + if withSources { + svc.SetIssueSources(src) + } + return svc, fs, forgeRec, trRec, src +} + +func TestLinkedRunCaptureUsesResolverAndNeverAsksForgeAboutTrackerAuthors(t *testing.T) { + sites := map[string]func(*Service, uuid.UUID, uuid.UUID) error{ + "CreateRun": func(s *Service, user, repo uuid.UUID) error { + _, err := s.CreateRun(context.Background(), user, repo, 4, "caller body", nil, nil, false, nil, nil, nil, nil) + return err + }, + "StartRunForUser": func(s *Service, user, repo uuid.UUID) error { + _, err := s.StartRunForUser(context.Background(), user, repo, 4, nil, nil, false, nil, nil, nil, nil) + return err + }, + } + for name, run := range sites { + t.Run(name, func(t *testing.T) { + svc, _, forgeRec, trRec, src := linkedRunFixture(t, true) + user, repo := uuid.New(), uuid.New() + if err := run(svc, user, repo); err != nil { + t.Fatal(err) + } + if !hasCall(trRec.Calls(), "GetIssue(") || !hasCall(trRec.Calls(), "ListIssueComments(") { + t.Fatalf("the resolver's tracker was not read: %v", trRec.Calls()) + } + if hasCall(forgeRec.Calls(), "GetIssue(") || hasCall(forgeRec.Calls(), "ListIssueComments(") { + t.Fatalf("the forge was read for a linked repo's issue: %v", forgeRec.Calls()) + } + if hasCall(forgeRec.Calls(), "RepositoryAuthorEligibility(") { + t.Fatalf("the forge was asked about an issue tracker author: %v", forgeRec.Calls()) + } + if len(src.calls) != 1 || src.calls[0].user != user { + t.Fatalf("the resolver must be asked once for the owner, got %+v", src.calls) + } + }) + } +} + +func TestLinkedRunCaptureWithoutSourcesFailsClosed(t *testing.T) { + for name, run := range map[string]func(*Service) error{ + "CreateRun": func(s *Service) error { + _, err := s.CreateRun(context.Background(), uuid.New(), uuid.New(), 4, "b", nil, nil, false, nil, nil, nil, nil) + return err + }, + "StartRunForUser": func(s *Service) error { + _, err := s.StartRunForUser(context.Background(), uuid.New(), uuid.New(), 4, nil, nil, false, nil, nil, nil, nil) + return err + }, + } { + t.Run(name, func(t *testing.T) { + svc, fs, forgeRec, _, _ := linkedRunFixture(t, false) + err := run(svc) + if !errors.Is(err, ErrForgeBuild) { + t.Fatalf("err = %v, want ErrForgeBuild", err) + } + if fs.createRunParams != nil { + t.Fatal("a run was created for an unresolvable linked repo") + } + if hasCall(forgeRec.Calls(), "GetIssue(") { + t.Fatalf("fell back to the forge: %v", forgeRec.Calls()) + } + }) + } +} + +func TestUnlinkedRunCaptureNeverConsultsResolver(t *testing.T) { + svc, _, _, _, src := linkedRunFixture(t, true) + svc.q.(*fakeStore).repoRow.IssueTrackerConnectionID = pgtype.UUID{} + if _, err := svc.StartRunForUser(context.Background(), uuid.New(), uuid.New(), 4, nil, nil, false, nil, nil, nil, nil); err != nil { + t.Fatal(err) + } + if len(src.calls) != 0 { + t.Fatalf("an unlinked repo consulted the resolver: %+v", src.calls) + } +} + +func TestLinkedConfirmProposalIsRefusedAndReverts(t *testing.T) { + svc, fs, fb := confirmFixture(t) + fs.repoRow.IssueTrackerConnectionID = pgtype.UUID{Bytes: uuid.New(), Valid: true} + refused := errors.New("create issue: issue writes on a repo linked to an issue tracker are not offered yet") + src := &fakeSources{tr: &ytTracker{createErr: refused}} + svc.SetIssueSources(src) + + _, err := svc.ConfirmProposalForUser(context.Background(), uuid.New(), uuid.New(), uuid.New()) + if !errors.Is(err, ErrForgeIssueWrite) || !strings.Contains(err.Error(), "not offered") { + t.Fatalf("err = %v, want the guard's refusal behind ErrForgeIssueWrite", err) + } + if fb.f.createCalls != 0 { + t.Fatal("the forge's CreateIssue was called for a linked repo") + } + if len(fs.revertedProposals) != 1 || fs.markedProposalConfirm != nil { + t.Fatalf("the claim must revert to pending: reverts=%d", len(fs.revertedProposals)) + } + if len(src.calls) != 1 { + t.Fatalf("resolver calls = %d, want 1", len(src.calls)) + } +} + +func TestLinkedConfirmProposalWithoutSourcesFailsClosed(t *testing.T) { + svc, fs, fb := confirmFixture(t) + fs.repoRow.IssueTrackerConnectionID = pgtype.UUID{Bytes: uuid.New(), Valid: true} + + _, err := svc.ConfirmProposalForUser(context.Background(), uuid.New(), uuid.New(), uuid.New()) + if !errors.Is(err, ErrForgeBuild) { + t.Fatalf("err = %v, want ErrForgeBuild", err) + } + if fb.f.createCalls != 0 || len(fs.revertedProposals) != 1 { + t.Fatalf("forge creates=%d reverts=%d, want 0 and 1", fb.f.createCalls, len(fs.revertedProposals)) + } +} + +func TestLinkedRepoFilesNoScheduledProposal(t *testing.T) { + svc, fs, ff, run := proposalFixture(t, "feature-bingo") + fs.repoRow.IssueTrackerConnectionID = pgtype.UUID{Bytes: uuid.New(), Valid: true} + src := &fakeSources{tr: &ytTracker{}} + svc.SetIssueSources(src) + + svc.maybeFileProposal(context.Background(), run, aProposal()) + + if ff.createCalls != 0 { + t.Fatal("a proposal was filed on the forge for a linked repo") + } + if fs.createdProposal != nil { + t.Fatal("a pending audit row was left behind for a skipped filing") + } +} + +func TestLinkedContinuedIssueUsesResolverOrFailsClosed(t *testing.T) { + iid := int64(17) + newSvc := func(withSources bool) (*Service, *linkedTaskForge, *linkedTaskForge, *fakeStore) { + row := aValidRepoRow() + row.ForgeProjectID = 621 + row.IssueTrackerConnectionID = pgtype.UUID{Bytes: uuid.New(), Valid: true} + fs := &fakeStore{repoRow: row} + svc := New(fs, newBox(t), testParams()) + forgeSide := &linkedTaskForge{issue: forge.Issue{IID: iid, State: "opened"}} + trSide := &linkedTaskForge{issue: forge.Issue{IID: iid, State: "opened"}} + svc.SetForges(m2Builder{forgeSide}) + if withSources { + svc.SetIssueSources(&fakeSources{tr: trSide}) + } + return svc, forgeSide, trSide, fs + } + t.Run("resolver", func(t *testing.T) { + svc, forgeSide, trSide, fs := newSvc(true) + if _, err := svc.CreateTaskRun(context.Background(), uuid.New(), uuid.New(), "task", "", false, false, false, false, &iid); err != nil { + t.Fatal(err) + } + if trSide.calls != 1 || forgeSide.calls != 0 || fs.taskRunParams == nil { + t.Fatalf("resolver calls=%d forge calls=%d persisted=%v", trSide.calls, forgeSide.calls, fs.taskRunParams != nil) + } + }) + t.Run("no resolver", func(t *testing.T) { + svc, forgeSide, _, fs := newSvc(false) + _, err := svc.CreateTaskRun(context.Background(), uuid.New(), uuid.New(), "task", "", false, false, false, false, &iid) + if !errors.Is(err, ErrForgeBuild) || forgeSide.calls != 0 || fs.taskRunParams != nil { + t.Fatalf("err=%v forge calls=%d persisted=%v", err, forgeSide.calls, fs.taskRunParams != nil) + } + }) +} + +func TestLinkedSemanticIssueReadUsesResolverOrFailsClosed(t *testing.T) { + iid := int64(4) + build := func(withSources bool) (*Service, *driftFakeForge, *driftFakeForge) { + run := store.Run{ID: uuid.New(), UserID: uuid.New(), RepoID: pgtype.UUID{Bytes: uuid.New(), Valid: true}, Kind: runkind.Issue, IssueIid: pgtype.Int8{Int64: iid, Valid: true}} + fs := &forgeConnStore{ownedRun: run, connRow: store.GetRunForgeConnForWorkerRow{ + RepoID: uuid.UUID(run.RepoID.Bytes), UserID: run.UserID, ForgeProjectID: 9, ForgeType: "gitlab", BaseUrl: "https://gl.test", + IssueTrackerConnectionID: pgtype.UUID{Bytes: uuid.New(), Valid: true}, + }} + svc := New(fs, newBox(t), Params{}) + forgeSide := &driftFakeForge{issue: forge.Issue{Description: "forge text"}} + trSide := &driftFakeForge{issue: forge.Issue{Description: "tracker text"}} + svc.SetForges(m2Builder{forgeSide}) + if withSources { + svc.SetIssueSources(&fakeSources{tr: trSide}) + } + return svc, forgeSide, trSide + } + svc, forgeSide, trSide := build(true) + got, err := svc.currentSemanticIssue(context.Background(), worker(), uuid.New()) + if err != nil || got.Text != "tracker text" || trSide.calls != 1 || forgeSide.calls != 0 { + t.Fatalf("got=%+v err=%v tracker calls=%d forge calls=%d", got, err, trSide.calls, forgeSide.calls) + } + svc, forgeSide, _ = build(false) + got, err = svc.currentSemanticIssue(context.Background(), worker(), uuid.New()) + if err != nil || got.Reason != "unreadable" || forgeSide.calls != 0 { + t.Fatalf("no resolver: got=%+v err=%v forge calls=%d", got, err, forgeSide.calls) + } +} + +func TestForgeConnForRunCarriesTheRunsIssueBinding(t *testing.T) { + run := runWithRepoID() + link := pgtype.UUID{Bytes: uuid.New(), Valid: true} + repoID, owner := uuid.New(), uuid.New() + fs := &forgeConnStore{ownedRun: run, connRow: store.GetRunForgeConnForWorkerRow{RepoID: repoID, UserID: owner, IssueTrackerConnectionID: link}} + conn, err := New(fs, newBox(t), Params{}).ForgeConnForRun(context.Background(), worker(), uuid.New()) + if err != nil { + t.Fatal(err) + } + if conn.RepoID != repoID || conn.UserID != owner || conn.IssueTrackerConnectionID != link { + t.Fatalf("conn = %+v", conn) + } +} diff --git a/api/internal/workersvc/issue_tracker_bounds_test.go b/api/internal/workersvc/issue_tracker_bounds_test.go new file mode 100644 index 0000000000..17f07ef9fd --- /dev/null +++ b/api/internal/workersvc/issue_tracker_bounds_test.go @@ -0,0 +1,126 @@ +package workersvc + +import ( + "context" + "net/http" + "strings" + "testing" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/issueinput" + "github.com/vtmocanu/uzi/api/internal/issuetracker" + "github.com/vtmocanu/uzi/api/internal/issuetracker/youtracktest" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// PRD #4173 M4 acceptance (S4): issue tracker titles, descriptions and comments reach a +// run through the same issueinput bounds and fencing as forge issue text. These tests +// drive the real YouTrack driver against the YouTrack fake and compare with the forge +// equivalent. + +// youTrackDriver builds the real driver against the fake, trusting its certificate. +func youTrackDriver(t *testing.T, srv *youtracktest.Server) issuetracker.Driver { + t.Helper() + def, ok := http.DefaultTransport.(*http.Transport) + if !ok { + t.Fatal("http.DefaultTransport is not an *http.Transport") + } + saved := def.TLSClientConfig + def.TLSClientConfig = srv.Client().Transport.(*http.Transport).TLSClientConfig.Clone() + t.Cleanup(func() { def.TLSClientConfig = saved }) + d, err := issuetracker.New(issuetracker.KindYouTrack, issuetracker.Options{ + BaseURL: srv.URL, + Token: srv.Token, + Project: issuetracker.ProjectRef{ID: youtracktest.ProjectID, ShortName: youtracktest.ProjectShortName}, + ProjectID: 77, + EligibilityTag: issuetracker.TagRef{ID: youtracktest.EligibilityTagID, Name: youtracktest.EligibilityTagName}, + }) + if err != nil { + t.Fatal(err) + } + return d +} + +// An oversized YouTrack description is classified exactly like an oversized forge issue +// response: the run is refused as too large and none is created. +func TestOversizedIssueDescriptionIsRefusedLikeTheForgeEquivalent(t *testing.T) { + srv := youtracktest.NewServer(t) + n := srv.SeedIssue("big", strings.Repeat("a", 3<<20), youtracktest.EligibilityTagID) + yt := youTrackDriver(t, srv) + + trackers := map[string]forge.IssueTracker{ + "youtrack": yt, + "forge": &tooLargeIssueTracker{}, + } + for name, tr := range trackers { + t.Run(name, func(t *testing.T) { + row := linkedRepoRow() + row.ForgeProjectID = 77 // the driver refuses any other project id + fs := &fakeStore{repoRow: row, issueByID: store.Issue{Labels: uziLabels()}, createRunResult: store.Run{ID: uuid.New()}} + svc := New(fs, newBox(t), testParams()) + svc.SetForges(m2Builder{&m2Forge{}}) + svc.SetIssueSources(&fakeSources{tr: tr}) + iid := n + if name == "forge" { + iid = 4 + } + _, err := svc.CreateRun(context.Background(), uuid.New(), uuid.New(), iid, "caller body", nil, nil, false, nil, nil, nil, nil) + if err == nil || !strings.Contains(err.Error(), ErrDescriptionTooLarge.Error()) { + t.Fatalf("err = %v, want ErrDescriptionTooLarge", err) + } + if fs.createRunParams != nil { + t.Fatal("a run was created from an oversized issue") + } + }) + } +} + +type tooLargeIssueTracker struct{ forgetest.BaseFake } + +func (*tooLargeIssueTracker) GetIssue(context.Context, int64, int64) (forge.Issue, error) { + return forge.Issue{}, &forge.ResponseTooLargeError{Provenance: forge.ResponseIssueSuccess} +} + +// An oversized YouTrack comment never reaches a run unbounded: the captured thread obeys +// the same byte bound as a forge thread, and a comment of a YouTrack author (never an +// assessed forge account) is withheld behind the fixed placeholder. +func TestOversizedIssueCommentIsBoundedLikeTheForgeEquivalent(t *testing.T) { + const bound = 32768 + big := strings.Repeat("c", 3<<20) + + srv := youtracktest.NewServer(t) + n := srv.SeedIssue("card", "body", youtracktest.EligibilityTagID) + srv.SeedComment(n, "someone", big) + yt := youTrackDriver(t, srv) + got, err := issueinput.Fetch(context.Background(), &m2Forge{disposition: forge.AuthorEligible}, yt, 77, n, 1) + if err != nil { + t.Fatalf("YouTrack capture: %v", err) + } + total := 0 + for _, c := range got.Thread.Comments { + total += len(c.Body) + if strings.Contains(c.Body, "cccc") { + t.Fatal("an unassessed YouTrack comment body reached the capture") + } + } + if total > bound || !got.Unknown { + t.Fatalf("YouTrack thread bytes=%d unknown=%v, want <= %d and permission_unknown", total, got.Unknown, bound) + } + + // The forge equivalent: an assessed author's oversized comment is cut to the same bound. + f := &m2Forge{disposition: forge.AuthorEligible, comments: []forge.IssueComment{{AuthorForgeUserID: 5, Body: big}}} + want, err := issueinput.Fetch(context.Background(), f, f, 7, 4, 1) + if err != nil { + t.Fatalf("forge capture: %v", err) + } + ftotal := 0 + for _, c := range want.Thread.Comments { + ftotal += len(c.Body) + } + if ftotal != bound || !want.Thread.Truncated { + t.Fatalf("forge thread bytes=%d truncated=%v, want exactly %d and truncated", ftotal, want.Thread.Truncated, bound) + } +} diff --git a/api/internal/workersvc/linked_privilege_guard_test.go b/api/internal/workersvc/linked_privilege_guard_test.go new file mode 100644 index 0000000000..71dcf7fe8f --- /dev/null +++ b/api/internal/workersvc/linked_privilege_guard_test.go @@ -0,0 +1,66 @@ +package workersvc + +import ( + "context" + "errors" + "testing" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/issuetracker/youtracktest" + "github.com/vtmocanu/uzi/api/internal/privcheck" +) + +// guardForge is a forge whose branch protection reads clean, or fails when verifyErr is set. +type guardForge struct { + forgetest.BaseFake + verifyErr error +} + +func (g *guardForge) VerifyToken(context.Context) (forge.BotIdentity, error) { + return forge.BotIdentity{ForgeUserID: 42}, g.verifyErr +} +func (*guardForge) ProjectRole(context.Context, int64, int64) (forge.Role, bool, error) { + return forge.RoleWrite, true, nil +} +func (*guardForge) DefaultBranchProtection(context.Context, int64, string, int64) (forge.BranchProtection, error) { + return forge.BranchProtection{Protected: true}, nil +} + +// PRD #4173 M4 acceptance: on a repo linked to an issue tracker the default-branch +// guardrail (the same privcheck.GuardRepo the claim backstop calls) still runs against +// the forge only. It passes on a healthy forge without one issue tracker request, even +// with the issue tracker unreachable, and fails closed when the forge cannot be read. +func TestLinkedRepoGuardrailRunsAgainstTheForgeOnlyAndFailsClosed(t *testing.T) { + for name, tc := range map[string]struct { + verifyErr error + wantBlock bool + }{ + "healthy forge passes": {}, + "failing forge blocks": {verifyErr: errors.New("401 token revoked"), wantBlock: true}, + } { + t.Run(name, func(t *testing.T) { + srv := youtracktest.NewServer(t) + yt := youTrackDriver(t, srv) + srv.Close() // the issue tracker is unreachable; any request to it would fail or be recorded + srv.ResetRequests() + + g := &guardForge{verifyErr: tc.verifyErr} + fs := &fakeStore{} + svc := New(fs, newBox(t), testParams()) + svc.SetRepoGuard(privcheck.NewService(nil, m2Builder{g})) + svc.SetIssueSources(&fakeSources{tr: yt}) + + row := linkedRepoRow() + row.DefaultBranch.String, row.DefaultBranch.Valid = "main", true + err := svc.guardDefaultBranch(context.Background(), row) + var blocked *GuardrailBlockedError + if got := errors.As(err, &blocked); got != tc.wantBlock { + t.Fatalf("guardDefaultBranch = %v, want blocked=%v", err, tc.wantBlock) + } + if n := len(srv.Requests()); n != 0 { + t.Fatalf("the guardrail made %d issue tracker requests", n) + } + }) + } +} diff --git a/api/internal/workersvc/mr_rework.go b/api/internal/workersvc/mr_rework.go index 24b0a0951e..bc018f2008 100644 --- a/api/internal/workersvc/mr_rework.go +++ b/api/internal/workersvc/mr_rework.go @@ -156,12 +156,17 @@ func (s *Service) CreateManualMRReworkRun(ctx context.Context, userID, repoID uu func (s *Service) createMRReworkRun(ctx context.Context, userID, repoID uuid.UUID, ref string, mrIID int64, sourceRunID uuid.UUID, title, description string, snapshot *ReviewCommentsSnapshot, triggerSource string, manual, advance *ReviewPlan, recheck *reworkRecheck) (store.Run, error) { // Repo-ownership / existence check, mirroring createCIFixRun so an unknown repo is // a clean ErrRepoNotFound rather than an FK error at INSERT. - if _, err := s.q.GetRepoForUser(ctx, store.GetRepoForUserParams{ID: repoID, UserID: userID}); err != nil { + row, err := s.q.GetRepoForUser(ctx, store.GetRepoForUserParams{ID: repoID, UserID: userID}) + if err != nil { if errors.Is(err, pgx.ErrNoRows) { return store.Run{}, ErrRepoNotFound } return store.Run{}, err } + // PRD 4173 D13: no run of any kind on a never-enabled repo; refused before any forge call. + if err := store.RefuseNeverEnabled(row.AwaitingFirstEnable); err != nil { + return store.Run{}, err + } // The MR review snapshot rides the create path explicitly (Decision 8). A nil // snapshot stores NULL (sqlc.narg): the detector only proceeds with a non-nil diff --git a/api/internal/workersvc/never_enabled_test.go b/api/internal/workersvc/never_enabled_test.go new file mode 100644 index 0000000000..e7f9780e36 --- /dev/null +++ b/api/internal/workersvc/never_enabled_test.go @@ -0,0 +1,183 @@ +package workersvc + +import ( + "context" + "errors" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgconn" + + "github.com/vtmocanu/uzi/api/internal/runkind" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// PRD 4173 D13: every service entry point that would start a run, store an issue +// number or file an issue refuses a repo whose awaiting_first_enable marker is set, +// before any forge driver is built or called. The fakes fail the test on a forge call +// by count: buildCalls counts driver builds, createCalls counts CreateIssue. + +func TestNeverEnabledRepoRefusedBeforeForge(t *testing.T) { + ctx := context.Background() + user, repo := uuid.New(), uuid.New() + + t.Run("CreateRun", func(t *testing.T) { + fs := &fakeStore{repoRow: store.GetRepoForUserRow{ID: repo, AwaitingFirstEnable: true}} + fb := &fakeForges{f: &fakeForge{}} + svc := New(fs, newBox(t), testParams()) + svc.SetForges(fb) + _, err := svc.CreateRun(ctx, user, repo, 7, "desc", nil, nil, false, nil, nil, nil, nil) + if !errors.Is(err, ErrRepoNeverEnabled) { + t.Fatalf("err = %v, want ErrRepoNeverEnabled", err) + } + if fb.buildCalls != 0 || fs.createRunParams != nil { + t.Errorf("forge builds = %d, run created = %v; want none", fb.buildCalls, fs.createRunParams != nil) + } + }) + + t.Run("StartRunForUser", func(t *testing.T) { + fs := &fakeStore{repoRow: store.GetRepoForUserRow{ID: repo, AwaitingFirstEnable: true}} + fb := &fakeForges{f: &fakeForge{}} + svc := New(fs, newBox(t), testParams()) + svc.SetForges(fb) + _, err := svc.StartRunForUser(ctx, user, repo, 7, nil, nil, false, nil, nil, nil, nil) + if !errors.Is(err, ErrRepoNeverEnabled) { + t.Fatalf("err = %v, want ErrRepoNeverEnabled", err) + } + if fb.buildCalls != 0 || fs.createRunParams != nil { + t.Errorf("forge builds = %d, run created = %v; want none", fb.buildCalls, fs.createRunParams != nil) + } + }) + + t.Run("CreateTaskRun continuing an issue", func(t *testing.T) { + row := aValidRepoRow() + row.AwaitingFirstEnable = true + fs := &fakeStore{repoRow: row} + fb := &fakeForges{f: &fakeForge{}} + svc := New(fs, newBox(t), testParams()) + svc.SetForges(fb) + iid := int64(17) + _, err := svc.CreateTaskRun(ctx, user, repo, "task", "", false, false, false, false, &iid) + if !errors.Is(err, ErrRepoNeverEnabled) { + t.Fatalf("err = %v, want ErrRepoNeverEnabled", err) + } + if fb.buildCalls != 0 || fs.taskRunParams != nil { + t.Errorf("forge builds = %d, run created = %v; want none", fb.buildCalls, fs.taskRunParams != nil) + } + }) + + t.Run("CreateSelfImproveRun", func(t *testing.T) { + fs := &fakeStore{repoRow: store.GetRepoForUserRow{ID: repo, AwaitingFirstEnable: true}} + fb := &fakeForges{f: &fakeForge{}} + svc := New(fs, newBox(t), testParams()) + svc.SetForges(fb) + _, err := svc.CreateSelfImproveRun(ctx, user, repo, 7, "t", "d", nil, nil, false, nil) + if !errors.Is(err, ErrRepoNeverEnabled) { + t.Fatalf("err = %v, want ErrRepoNeverEnabled", err) + } + if fb.buildCalls != 0 || fs.selfImproveParams != nil { + t.Errorf("forge builds = %d, run created = %v; want none", fb.buildCalls, fs.selfImproveParams != nil) + } + }) + + t.Run("CreateTaskRun without an issue", func(t *testing.T) { + row := aValidRepoRow() + row.AwaitingFirstEnable = true + fs := &fakeStore{repoRow: row} + fb := &fakeForges{f: &fakeForge{}} + svc := New(fs, newBox(t), testParams()) + svc.SetForges(fb) + _, err := svc.CreateTaskRun(ctx, user, repo, "task", "", false, false, false, false, nil) + if !errors.Is(err, ErrRepoNeverEnabled) { + t.Fatalf("err = %v, want ErrRepoNeverEnabled", err) + } + if fb.buildCalls != 0 || fs.taskRunParams != nil { + t.Errorf("forge builds = %d, run created = %v; want none", fb.buildCalls, fs.taskRunParams != nil) + } + }) + + t.Run("CreatePromptRun", func(t *testing.T) { + fs := &fakeStore{repoRow: store.GetRepoForUserRow{ID: repo, AwaitingFirstEnable: true}} + fb := &fakeForges{f: &fakeForge{}} + svc := New(fs, newBox(t), testParams()) + svc.SetForges(fb) + _, err := svc.CreatePromptRun(ctx, user, repo, uuid.New(), "t", "p", false, false, nil, nil, false, nil, nil) + if !errors.Is(err, ErrRepoNeverEnabled) { + t.Fatalf("err = %v, want ErrRepoNeverEnabled", err) + } + if fb.buildCalls != 0 { + t.Errorf("forge builds = %d, want 0", fb.buildCalls) + } + }) + + t.Run("CreateCIFixRun", func(t *testing.T) { + fs := &fakeStore{repoRow: store.GetRepoForUserRow{ID: repo, AwaitingFirstEnable: true}} + fb := &fakeForges{f: &fakeForge{}} + svc := New(fs, newBox(t), testParams()) + svc.SetForges(fb) + _, err := svc.CreateCIFixRun(ctx, user, repo, "main", "t", "d", FailureSnapshot{}, nil) + if !errors.Is(err, ErrRepoNeverEnabled) { + t.Fatalf("err = %v, want ErrRepoNeverEnabled", err) + } + if fb.buildCalls != 0 { + t.Errorf("forge builds = %d, want 0", fb.buildCalls) + } + }) + + t.Run("createMRReworkRun", func(t *testing.T) { + fs := &fakeStore{repoRow: store.GetRepoForUserRow{ID: repo, AwaitingFirstEnable: true}} + svc := New(fs, newBox(t), testParams()) + _, err := svc.createMRReworkRun(ctx, user, repo, "ref", 1, uuid.New(), "t", "d", nil, "", nil, nil, nil) + if !errors.Is(err, ErrRepoNeverEnabled) { + t.Fatalf("err = %v, want ErrRepoNeverEnabled", err) + } + }) + + t.Run("CreateProposal", func(t *testing.T) { + fs := &fakeStore{repoRow: store.GetRepoForUserRow{ID: repo, AwaitingFirstEnable: true}, runByID: store.Run{Kind: runkind.Chat, Status: "running"}} + svc := New(fs, newBox(t), testParams()) + _, err := svc.CreateProposal(ctx, store.Worker{UserID: user}, uuid.New(), repo, "t", "d", []byte(`[]`)) + if !errors.Is(err, ErrRepoNeverEnabled) { + t.Fatalf("err = %v, want ErrRepoNeverEnabled", err) + } + if fs.createdProposal != nil { + t.Error("a proposal was stored for a never-enabled repo") + } + }) + + t.Run("ConfirmProposalForUser reverts the claim", func(t *testing.T) { + svc, fs, fb := confirmFixture(t) + fs.repoRow.AwaitingFirstEnable = true + _, err := svc.ConfirmProposalForUser(ctx, user, uuid.New(), uuid.New()) + if !errors.Is(err, ErrRepoNeverEnabled) { + t.Fatalf("err = %v, want ErrRepoNeverEnabled", err) + } + if fb.buildCalls != 0 || fb.f.createCalls != 0 { + t.Errorf("forge builds = %d, CreateIssue calls = %d; want none", fb.buildCalls, fb.f.createCalls) + } + if len(fs.revertedProposals) != 1 { + t.Errorf("reverted proposals = %d, want the claim returned to pending", len(fs.revertedProposals)) + } + }) + + t.Run("maybeFileProposal", func(t *testing.T) { + svc, fs, ff, run := proposalFixture(t, "feature-bingo") + fs.repoRow.AwaitingFirstEnable = true + svc.maybeFileProposal(ctx, run, aProposal()) + if ff.createCalls != 0 || fs.createdProposal != nil { + t.Errorf("CreateIssue calls = %d, audit row = %v; want nothing filed", ff.createCalls, fs.createdProposal != nil) + } + }) +} + +// A UZ413 from the database guard maps to the same typed error. +func TestMapNeverEnabledGuardError(t *testing.T) { + pgErr := &pgconn.PgError{Code: "UZ413", Message: "repo has never been enabled"} + if err := store.MapNeverEnabled(pgErr); !errors.Is(err, ErrRepoNeverEnabled) { + t.Fatalf("MapNeverEnabled(UZ413) = %v, want ErrRepoNeverEnabled", err) + } + other := &pgconn.PgError{Code: "23505"} + if err := store.MapNeverEnabled(other); errors.Is(err, ErrRepoNeverEnabled) { + t.Fatal("a unique violation was mapped to ErrRepoNeverEnabled") + } +} diff --git a/api/internal/workersvc/prompt.go b/api/internal/workersvc/prompt.go index f27061c042..ad28ac9bff 100644 --- a/api/internal/workersvc/prompt.go +++ b/api/internal/workersvc/prompt.go @@ -81,6 +81,10 @@ func (s *Service) createPromptRunShared(ctx context.Context, in promptRunInput, } return store.Run{}, err } + // PRD 4173 D13: no run of any kind on a never-enabled repo; refused before any forge call. + if err := store.RefuseNeverEnabled(row.AwaitingFirstEnable); err != nil { + return store.Run{}, err + } if err := s.guardDefaultBranch(ctx, row); err != nil { return store.Run{}, err } diff --git a/api/internal/workersvc/proposal_filing.go b/api/internal/workersvc/proposal_filing.go index 272b6fb0cf..81d7798ff3 100644 --- a/api/internal/workersvc/proposal_filing.go +++ b/api/internal/workersvc/proposal_filing.go @@ -145,11 +145,25 @@ func (s *Service) maybeFileProposal(ctx context.Context, run store.Run, proposal slog.Warn("file proposal: load repo", "run_id", run.ID, "error", err) return } + // PRD 4173 D13: nothing is filed on a never-enabled repo; skipped before the audit + // row and the forge write. + if err := store.RefuseNeverEnabled(repo.AwaitingFirstEnable); err != nil { + slog.Warn("file proposal: skipped", "run_id", run.ID, "error", err) + return + } f, err := s.forges.ForgeForConnection(repo.ForgeType, repo.BaseUrl, repo.TokenCiphertext) if err != nil { slog.Warn("file proposal: build forge", "run_id", run.ID, "error", err) return } + // PRD #4173 M4: filing a proposal creates an issue, which a linked repo does not + // offer until M5, so it is skipped before the audit row (no pending row is left + // behind) and never falls back to the forge, whose project number means nothing there. + if repo.IssueTrackerConnectionID.Valid { + slog.Warn("file proposal: skipped, issue creation is not offered on a repo linked to an issue tracker", + "run_id", run.ID, "repo_id", repo.ID) + return + } // Audit row first (pending), then the forge write, then stamp confirmed with the // iid — mirroring the issue_proposals shape ConfirmProposalForUser uses (PRD #929 diff --git a/api/internal/workersvc/recorded_calls_test.go b/api/internal/workersvc/recorded_calls_test.go new file mode 100644 index 0000000000..05bfbcda40 --- /dev/null +++ b/api/internal/workersvc/recorded_calls_test.go @@ -0,0 +1,65 @@ +package workersvc + +import ( + "context" + "testing" + + "github.com/google/uuid" + + "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/forge/forgetest" + "github.com/vtmocanu/uzi/api/internal/store" +) + +// Recorded-call tests (PRD 4173 M4): for a repo with no issue-tracker link, a +// manual issue run captures its input (GetIssue, ListIssueComments, and one +// RepositoryAuthorEligibility check per distinct author, the issue author +// included) and makes no other forge call. The goldens were captured before the tracker-link rerouting; +// regenerate only deliberately, with UZI_UPDATE_GOLDEN=1. + +func recordedRunFixture(t *testing.T) (*Service, *forgetest.Recorder, *fakeStore) { + t.Helper() + fs := &fakeStore{ + repoRow: store.GetRepoForUserRow{ForgeProjectID: 7, BotForgeUserID: 1}, + issueByID: store.Issue{Labels: uziLabels()}, + createRunResult: store.Run{ID: uuid.New()}, + } + f := &m2Forge{ + disposition: forge.AuthorEligible, + comments: []forge.IssueComment{ + {AuthorForgeUserID: 2, Body: "first"}, + {AuthorForgeUserID: 5, Body: "second"}, + {AuthorForgeUserID: 5, Body: "third"}, + }, + } + rec := forgetest.NewRecorder(f) + svc := New(fs, newBox(t), testParams()) + svc.SetForges(m2Builder{rec}) + return svc, rec, fs +} + +// TestRecordedCallsCreateRunManual drives CreateRun directly, so the input +// capture runs inside it (no capture handed in through the context). +func TestRecordedCallsCreateRunManual(t *testing.T) { + svc, rec, fs := recordedRunFixture(t) + if _, err := svc.CreateRun(context.Background(), uuid.New(), uuid.New(), 4, "caller raw body", nil, nil, false, nil, nil, nil, nil); err != nil { + t.Fatal(err) + } + if fs.createRunParams == nil { + t.Fatal("CreateRun did not persist a run") + } + forgetest.AssertGolden(t, "recorded_calls_workersvc_createrun.golden", rec.Calls()) +} + +// TestRecordedCallsStartRunForUser drives the manual start composite, which +// captures the issue input once and hands it to CreateRun. +func TestRecordedCallsStartRunForUser(t *testing.T) { + svc, rec, fs := recordedRunFixture(t) + if _, err := svc.StartRunForUser(context.Background(), uuid.New(), uuid.New(), 4, nil, nil, false, nil, nil, nil, nil); err != nil { + t.Fatal(err) + } + if fs.createRunParams == nil { + t.Fatal("StartRunForUser did not persist a run") + } + forgetest.AssertGolden(t, "recorded_calls_workersvc_startrunforuser.golden", rec.Calls()) +} diff --git a/api/internal/workersvc/self_improve.go b/api/internal/workersvc/self_improve.go index 58669d8ea0..6348dd7fc7 100644 --- a/api/internal/workersvc/self_improve.go +++ b/api/internal/workersvc/self_improve.go @@ -46,6 +46,11 @@ func (s *Service) CreateSelfImproveRun(ctx context.Context, userID, repoID uuid. } return store.Run{}, err } + // PRD 4173 D13: self-improve stores a tracking issue number, so a never-enabled repo + // is refused before the guard below can reach the forge. + if err := store.RefuseNeverEnabled(row.AwaitingFirstEnable); err != nil { + return store.Run{}, err + } // #66 D1 layer 2: the shared service-layer guardrail. The self-improve engine tick // has no user in the loop, so a refusal here just skips the cycle (logged upstream), // which is the desired outcome — never a run against an unguarded default branch. diff --git a/api/internal/workersvc/semantic_drift.go b/api/internal/workersvc/semantic_drift.go index 21b7e55ccf..880954f1f0 100644 --- a/api/internal/workersvc/semantic_drift.go +++ b/api/internal/workersvc/semantic_drift.go @@ -13,6 +13,7 @@ import ( "github.com/jackc/pgx/v5" "github.com/vtmocanu/uzi/api/internal/apitypes" "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/issuesource" "github.com/vtmocanu/uzi/api/internal/store" ) @@ -101,7 +102,11 @@ func (s *Service) currentSemanticIssue(ctx context.Context, wkr store.Worker, ru if err != nil { return currentIssue{Reason: "unreadable"}, nil } - cur := readCurrentSemanticIssue(ctx, f, conn.ForgeProjectID, *conn.IssueIID) + tr, err := issuesource.For(ctx, s.issueSources, conn.IssueTrackerConnectionID.Valid, conn.UserID, conn.RepoID, f) + if err != nil { + return currentIssue{Reason: "unreadable"}, nil + } + cur := readCurrentSemanticIssue(ctx, tr, conn.ForgeProjectID, *conn.IssueIID) cur.Saved, cur.SavedValid = conn.SavedBody, conn.SavedBodyValid return cur, nil } diff --git a/api/internal/workersvc/service.go b/api/internal/workersvc/service.go index 6e3235fbfb..01732be5af 100644 --- a/api/internal/workersvc/service.go +++ b/api/internal/workersvc/service.go @@ -40,6 +40,7 @@ import ( "github.com/vtmocanu/uzi/api/internal/config" "github.com/vtmocanu/uzi/api/internal/forge" "github.com/vtmocanu/uzi/api/internal/issueinput" + "github.com/vtmocanu/uzi/api/internal/issuesource" "github.com/vtmocanu/uzi/api/internal/jointoken" "github.com/vtmocanu/uzi/api/internal/pgconv" "github.com/vtmocanu/uzi/api/internal/planpolicy" @@ -94,7 +95,11 @@ var ( // ErrRunWallExpired refuses an ordinary pause or guarded question at the run deadline. ErrRunWallExpired = errors.New("run wall deadline expired") ErrRepoNotFound = errors.New("repo not found") - ErrIssueNotFound = errors.New("issue not found") + // ErrRepoNeverEnabled refuses a run, issue filing or sync on a repo that has never been + // enabled (PRD 4173 D13). It is store.ErrRepoNeverEnabled, so the store guard's UZ413 + // (store.MapNeverEnabled) and the service gates answer with one error. + ErrRepoNeverEnabled = store.ErrRepoNeverEnabled + ErrIssueNotFound = errors.New("issue not found") // ErrNotPRDIssue rejects a run on an issue that does not carry the configured // run-eligibility label (PRD #764 M1: the uzi_label) → 422. This is now the SINGLE // eligibility gate — an issue without the uzi_label is not uzi's to run, PRD link @@ -2121,6 +2126,10 @@ type Service struct { // composite ops return ErrForgesUnavailable rather than panic (the pre-#191 // deployments and every test that never wires it are unaffected). forges ForgeBuilder + // issueSources resolves the issue side of a repo linked to an external issue + // tracker (PRD #4173 DL-5); set via SetIssueSources. Nil ⇒ a linked repo fails + // closed (issuesource.ErrNotWired) and an unlinked repo keeps using forges. + issueSources issuesource.Sources // guard is the #66 default-branch guardrail (D1 layer 2), a narrow RepoGuard // interface *privcheck.Service satisfies; set via SetRepoGuard. Nil ⇒ the // service-layer gate is skipped (guardDefaultBranch is a no-op) and layer 3 (the @@ -2228,6 +2237,10 @@ func (s *Service) SetSettings(sr SettingsReader) { s.settings = sr } // builder leaves the composite ops returning ErrForgesUnavailable. func (s *Service) SetForges(fb ForgeBuilder) { s.forges = fb } +// SetIssueSources wires the resolver of a linked repo's issue side (PRD #4173 DL-5). +// Call once at startup with the same *forgesvc.Service SetForges gets. +func (s *Service) SetIssueSources(is issuesource.Sources) { s.issueSources = is } + // SetRepoGuard wires the #66 default-branch guardrail (D1 layer 2). Late-injected // in main.go because the privcheck.Service is built after this Service. A nil guard // leaves guardDefaultBranch a no-op — the claim backstop (M6, layer 3) is the net. @@ -6911,9 +6924,16 @@ type ForgeConn struct { // unmarshal it into a ReviewCommentsSnapshot and reject any reply/resolve id not // present in it (the Decision-11 server-side scope check). ReviewComments []byte - IssueIID *int64 - SavedTitle string - SavedBody string + // RepoID, UserID and IssueTrackerConnectionID bind the run's issue side (PRD + // #4173 DL-5): the issue routes resolve a linked repo's issue tracker by repo row + // and owner, never by the forge project id. IssueTrackerConnectionID.Valid is the + // repo's link. + RepoID uuid.UUID + UserID uuid.UUID + IssueTrackerConnectionID pgtype.UUID + IssueIID *int64 + SavedTitle string + SavedBody string // SavedBodyValid is true when the run captured a raw body (issue_saved_body not NULL); SavedBody // otherwise falls back to the run's issue description. SavedBodyValid bool @@ -6969,14 +6989,17 @@ func (s *Service) ForgeConnForRun(ctx context.Context, wkr store.Worker, runID u mrIID = &v } return ForgeConn{ - ForgeType: row.ForgeType, - BaseUrl: row.BaseUrl, - TokenCiphertext: row.TokenCiphertext, - ForgeProjectID: row.ForgeProjectID, - BotForgeUserID: row.BotForgeUserID, - MRIID: mrIID, - ReviewComments: run.ReviewComments, - IssueIID: issueIID, SavedTitle: run.IssueTitle, SavedBody: body, SavedBodyValid: run.IssueSavedBody.Valid, RawDigest: digest, InputReason: run.IssueInputReason.String, + ForgeType: row.ForgeType, + BaseUrl: row.BaseUrl, + TokenCiphertext: row.TokenCiphertext, + ForgeProjectID: row.ForgeProjectID, + BotForgeUserID: row.BotForgeUserID, + RepoID: row.RepoID, + UserID: row.UserID, + IssueTrackerConnectionID: row.IssueTrackerConnectionID, + MRIID: mrIID, + ReviewComments: run.ReviewComments, + IssueIID: issueIID, SavedTitle: run.IssueTitle, SavedBody: body, SavedBodyValid: run.IssueSavedBody.Valid, RawDigest: digest, InputReason: run.IssueInputReason.String, }, nil } @@ -7966,6 +7989,11 @@ func (s *Service) createRun(ctx context.Context, userID, repoID uuid.UUID, issue } return store.Run{}, err } + // PRD 4173 D13: a never-enabled repo starts no run, whoever asks (manual, schedule, + // autopilot, composite). Before guardDefaultBranch, which may call the forge. + if err := store.RefuseNeverEnabled(row.AwaitingFirstEnable); err != nil { + return store.Run{}, err + } // #66 D1 layer 2: the service-layer guardrail, shared across the PAT-bearing // inserts (issue lane, CI-fix, self-improve, scheduled prompt). Reached by the UI // AND autopilot, so gating here (not the handler) is what covers the unattended path. @@ -8071,7 +8099,13 @@ func (s *Service) createRun(ctx context.Context, userID, repoID uuid.UUID, issue if ferr != nil { return store.Run{}, ErrForgeBuild } - capture, err = issueinput.Fetch(ctx, f, f, row.ForgeProjectID, issueIID, row.BotForgeUserID) + // PRD #4173 DL-5: a linked repo's issue is read through the resolver, never the + // forge; the code-host half of the fetch (author eligibility) stays on the forge. + tr, terr := issuesource.For(ctx, s.issueSources, row.IssueTrackerConnectionID.Valid, userID, repoID, f) + if terr != nil { + return store.Run{}, fmt.Errorf("%w: %v", ErrForgeBuild, terr) + } + capture, err = issueinput.Fetch(ctx, f, tr, row.ForgeProjectID, issueIID, row.BotForgeUserID) if err != nil { if forge.IsIssueResponseTooLarge(err) { return store.Run{}, fmt.Errorf("%w: %w", ErrDescriptionTooLarge, err) diff --git a/api/internal/workersvc/task.go b/api/internal/workersvc/task.go index 2a1ae03e1b..da18a691d6 100644 --- a/api/internal/workersvc/task.go +++ b/api/internal/workersvc/task.go @@ -13,6 +13,7 @@ import ( "github.com/jackc/pgx/v5/pgtype" "github.com/vtmocanu/uzi/api/internal/forge" + "github.com/vtmocanu/uzi/api/internal/issuesource" "github.com/vtmocanu/uzi/api/internal/pgconv" "github.com/vtmocanu/uzi/api/internal/store" ) @@ -81,6 +82,10 @@ func (s *Service) CreateTaskRun(ctx context.Context, userID, repoID uuid.UUID, i } return store.Run{}, err } + // PRD 4173 D13: no run of any kind on a never-enabled repo; refused before any forge call. + if err := store.RefuseNeverEnabled(row.AwaitingFirstEnable); err != nil { + return store.Run{}, err + } // Linked handoffs validate directly against the owner's forge project. They do not // depend on cached board membership, labels, issue state, or a PRD link. @@ -96,7 +101,10 @@ func (s *Service) CreateTaskRun(ctx context.Context, userID, repoID uuid.UUID, i if err != nil { return store.Run{}, ErrForgeBuild } - var tr forge.IssueTracker = f + tr, err := issuesource.For(ctx, s.issueSources, row.IssueTrackerConnectionID.Valid, userID, repoID, f) + if err != nil { + return store.Run{}, ErrForgeBuild + } fetchCtx, cancel := context.WithTimeout(ctx, 15*time.Second) issue, err := forge.GetIssueBounded(fetchCtx, tr, row.ForgeProjectID, *continuedIssueIID) cancel() diff --git a/api/internal/workersvc/testdata/recorded_calls_workersvc_createrun.golden b/api/internal/workersvc/testdata/recorded_calls_workersvc_createrun.golden new file mode 100644 index 0000000000..1e0feaaed5 --- /dev/null +++ b/api/internal/workersvc/testdata/recorded_calls_workersvc_createrun.golden @@ -0,0 +1,4 @@ +GetIssue(7, 4) +RepositoryAuthorEligibility(7, 2) +ListIssueComments(7, 4) +RepositoryAuthorEligibility(7, 5) diff --git a/api/internal/workersvc/testdata/recorded_calls_workersvc_startrunforuser.golden b/api/internal/workersvc/testdata/recorded_calls_workersvc_startrunforuser.golden new file mode 100644 index 0000000000..1e0feaaed5 --- /dev/null +++ b/api/internal/workersvc/testdata/recorded_calls_workersvc_startrunforuser.golden @@ -0,0 +1,4 @@ +GetIssue(7, 4) +RepositoryAuthorEligibility(7, 2) +ListIssueComments(7, 4) +RepositoryAuthorEligibility(7, 5) diff --git a/e2e/run-store-it.sh b/e2e/run-store-it.sh index d57a1e986b..5b06a8df26 100755 --- a/e2e/run-store-it.sh +++ b/e2e/run-store-it.sh @@ -150,6 +150,7 @@ PKGS=( ./internal/workersvc/... ./internal/recovery/... ./internal/poller/... + ./internal/issuetrackersvc/... ) # EXPLICIT XXXXXX template, NOT `mktemp -t `: GNU coreutils rejects a `-t` # template with too few X's (it needs >=3 consecutive; this one had none) with "too diff --git a/prds/4173-external-issue-trackers.md b/prds/4173-external-issue-trackers.md index 65090fef46..26c5820fc3 100644 --- a/prds/4173-external-issue-trackers.md +++ b/prds/4173-external-issue-trackers.md @@ -176,7 +176,9 @@ D5, D10 (storage), D11, D13, D14 (choice and verification of the tag) and S2. Th - the privilege check and claim still run against the forge and fail closed with the issue tracker unreachable; - a repo with no link is byte-for-byte unchanged in its forge calls (recorded-call test); - linking is refused for every repo that existed before the migration and for a repo enabled since, including one that is disabled now; it is refused for a tag the connection's account cannot apply or that nobody else can apply, and the check modifies no issue; a link racing the first enable ends in exactly one of the two outcomes of D13; a connection with a linked repo cannot be deleted; - - when a run's issue is moved to another YouTrack project, its issue writes are skipped and its merge request still completes (D10). + - when a run's issue is moved to another YouTrack project, its issue writes are skipped and its merge request still completes (D10); the test moves the issue before the write, and an issue moved between the driver's check and its write can still be written once (YouTrack has no conditional write), a window that is not tested; + - issue tracker titles, descriptions and comments reach a run through the same `issueinput` bounds and fencing as forge issue text (S4): an oversized description and an oversized comment from YouTrack are each bounded exactly as their forge equivalents, in tests; + - a reconnect never silently changes, for a connection with a linked repo, whose issues and comments count as mapped (D11) or which account owns the tags uzi creates (D14): a reconnect that would change the account id of such a connection is refused, and a link racing a reconnect binds to the account its tag check ran against, in tests. ### M5: the board and issue writes on YouTrack @@ -244,3 +246,17 @@ Settings → Issue trackers, the CLI commands, a setup page under `docs/`. - **2026-11-11: a hostile or compromised allowlisted YouTrack cannot make the driver allocate far beyond what it sent.** Responses are capped (8 MiB a list page, 2 MiB an object, 4 KiB an error body, 64 MiB a listing); a page holds at most the requested 100 items; nested tags are capped at 1000 per item and count toward the listing's item ceiling; a byte scan bounds the JSON values before decoding; list pages decode one element at a time and stop at the first error. The worst case found is about 34 MiB for an 8 MiB page, against about 13 MiB for the largest legitimate one (measured in the security review). - **2026-11-11: left for M6's live pass** (taken from YouTrack's documentation, checked only against the fake): the `TagsCategory` activity category, the `updated:` query date format, that tag names can repeat across owners, whether YouTrack accepts tags on issue create (the driver adds them after create, so it does not depend on it), whether YouTrack accepts sharing settings in the `POST /api/tags` body, whether an authenticated token receives `usersCount` on a tag's sharing groups, and whether `tag: {name}` in a query matches every visible tag of that name (the client-side id filter depends on the server returning a superset). - **2026-11-11: migration `00424_issue_tracker_connections`** is the next free number on `prd/ws-2`; it is renumbered at upstream landing. Rolling it back deletes every stored issue tracker token. +- **2026-11-11: M4 gates sync, run creation, schedules and issue creation on the never-enabled marker (D13, owner answer).** D13 rested on "a never-enabled repo has had no sync and no run", which did not hold in code: board seed and refresh, `importAfterEnable` and `createRun` never checked `enabled`. Anything that would store an issue number now refuses a repo that was never enabled. Every repo that exists at the migration counts as enabled, so only repos created after it and not yet enabled change. +- **2026-11-11: a linked run's issue references use the issue key, never a forge closing keyword (owner answer).** The claim carries `issue_key` for a linked repo, and the agent renders every reference with it (`Related to ABC-12.`), with no closing keyword, an empty closing list and no closing gate; `Closes #N` would close an unrelated forge issue with the same number. The branch stays `agent/issue-`. A claim without the key renders exactly as before. +- **2026-11-11: `issues.issue_key` (D10).** A nullable text column with a CHECK: at most 64 characters, `-`, and the suffix must equal the row's `forge_issue_iid`, so the key cannot name another issue than the row and uniqueness follows from `UNIQUE (repo_id, forge_issue_iid)`. Only the linked sync writes it, as the link's stored project short name plus `numberInProject`; the forge sync leaves it NULL (a code rule tested on both paths, since a cross-table CHECK is impossible and a trigger was one more moving part). `runs` gets no column: a run reaches the key through `(repo_id, issue_iid)` and the claim. Rejected: a neutral `forge.Issue.Key` (D10 keeps the neutral types), parsing the key from `web_url` (fragile), a copy on `runs` (kept in step by hand). +- **2026-11-11: author mapping (D11) is stored nowhere and proven by the token.** A YouTrack account maps to a uzi user exactly when that user owns the repo's linked issue tracker connection and the account is that connection's own: the login `users/me` returns for the token, read once per driver, compared exactly with the author login the driver reports, both non-empty; a failed `users/me` means no mapping. Only the user can create it, by connecting their own token; nobody can assert a mapping for someone else, and repo access is owner-only with the link requiring the repo owner to own the connection. Everyone else is unmapped: comments withheld, issue author `permission_unknown`. The forge's `RepositoryAuthorEligibility` is never called for a linked repo; its author lookup answers unknown without a request. Rejected: an admin-filled table (an assertion is not proof), e-mail match (YouTrack e-mails need not be verified), Hub OAuth (a new auth flow), the stored `account_login` (a renamed or reused login maps to a stale owner). Residual: with a shared bot connection nobody is mapped, so every scheduled start waits for a person (fail closed). +- **2026-11-11: the never-enabled marker is `repos.awaiting_first_enable` (D13), inverted on purpose.** `boolean NOT NULL DEFAULT false`; only `UpsertRepo`'s insert sets it true (its conflict branch leaves it). A `BEFORE INSERT OR UPDATE` row trigger clears it whenever the row is enabled, so it clears in the statement that first enables the repo on every path, and raises if an update would set it back. The migration needs no backfill: the default makes every existing repo "previously enabled". Inverted because Go's zero value and raw test inserts then mean "previously enabled", so fakes and fixtures keep today's behaviour. Rejected: `ever_enabled` defaulting to false (every fixture would read as never enabled), setting it in Go (`seed` and raw SQL bypass Go), `first_enabled_at` (a boolean is enough). +- **2026-11-11: the link and the first enable share the repo row lock, and the database refuses an issue number on a never-enabled repo (D13).** The enable is an `UPDATE` (row lock); the link is one transaction that reads the owner-scoped repo `FOR UPDATE`, refuses unless it is awaiting and unlinked, refuses if any table stores an issue number for it, then writes the link. Under READ COMMITTED whichever commits first wins, so exactly one D13 outcome follows. One shared plpgsql guard trigger on every table that stores an issue number for a repo refuses a non-NULL number while the repo is awaiting; since the marker only goes true to false, a write that passed the guard means the repo can never be linked, which closes the race a review found in the link's `EXISTS` check (kept as defence in depth). Application gates in front of the guard return a clean 409 before any forge or issue tracker write, so no created issue is stranded: board seed and refresh, `importAfterEnable`, `createRun`, task runs, schedule create, patch, run-now and catalog enable, self-improve, and every forge-first issue creator; the guard's SQLSTATE maps to the same error. No relink and no unlink. Remaining window: `EnableRepo` seeds board labels before its `UPDATE`, so it re-reads the link first; a link landing between that read and the update can still leave forge labels on a linked repo, while the database reaches one outcome. Rejected: an advisory lock (D13 names the row lock), Go-only gates (racy). +- **2026-11-11: the resolvers bind repo, owner and issue tracker connection, and fail closed (D5, D10).** `IssueTrackerForRepo(ctx, userID, repoID)` and `CodeHostForRepo(ctx, userID, repoID)` read one owner-scoped row joining the repo, its forge connection and its issue tracker connection for the same owner, keyed by repo id and owner, never by forge project number. A linked repo gets the YouTrack driver bound to the link's project, the repo's forge project id and the eligibility tag; a link whose connection is missing for that owner is an error, never a forge fallback. `CodeHostForRepo` never reads the link (S2). Services left on their own factories by M2 get the resolver by setter, and the repo rows they already read carry the link id; without a resolver they may use the forge factory only for an unlinked row, so a missing wire fails closed with no boot check and existing fakes stay unchanged. Rejected: a method on every `ForgeBuilder` (about 28 fake edits), resolving by `(connection_id, forge_project_id)` (the collision itself), a boot check (unneeded under fail closed). +- **2026-11-11: a renamed eligibility tag is handled in the driver (D14), with an id-only fallback; the mechanism matches the upstream M3 driver helpers (owner guidance, 2026-11-11).** Asked for the eligibility label, `ListIssues` reads the tag by id (`GET tags/{id}`, fields `id,name`) and queries with its live name; the client-side filter keeps only the stored id. A tag that cannot be read is an error and nothing is listed: the sync fails, logged, nothing is evicted. A live name that `labelNameProblem` refuses gets no tag term at all: the query is the project term only, plus the existing client-side id filter, under the existing listing cap. Upstream names the helpers `queryTagNames` and `eligibilityLiveName` in `youtrack_labels.go` and `issueQuery(shortName, tagNames, opts)`; M4 keeps its change to those driver functions minimal so the upstream merge brings no second design. Callers keep naming the stored name, and the stored name is not rewritten in M4. Rejected: always id-only (pages the whole project every tick), refusing a renamed tag at sync (with no relink the repo would break for good), a poller-side rename (more moving parts). The forge subjects skip the new contract case, as forge labels have no owners. +- **2026-11-11: on a linked repo M4 writes nothing to the issue tracker and skips the forge-only features.** The resolver wraps the driver in a write guard: label, description, state, create and comment calls return `ErrIssueWriteNotOffered` without a request (writes are M5). That error, and a not-found the linked wrapper marks (a moved issue, D10), are a permanent skip, so the run lifecycle, the MR state record, the PRD link patch and comments never wait on the issue side. Autopilot (D12), GitHub Projects v2 sync (its numbers are forge issue numbers; a project link on a linked repo is refused) and self-improve (it creates an issue) are skipped. A linked run's MR-state watch moves to the board-free lane, which records the MR edge and cancels rework without the issue cache or a card label; the board lane excludes linked repos, so the lanes stay disjoint and unlinked repos match as before. +- **2026-11-11: migration `00425_issue_tracker_repo_link`** is the next free number on this branch; it is renumbered at upstream landing. +- **2026-11-11: issue tracker text goes through `issueinput` unchanged (S4, owner guidance, 2026-11-11).** YouTrack titles, descriptions and comments reach a run through the same `issueinput` bounds and fencing as forge issue text; tests compare an oversized YouTrack description and comment with their forge equivalents. +- **2026-11-11: a reconnect cannot change the account of a connection with a linked repo (D11, D14, owner guidance, 2026-11-11).** A reconnect (`Connect` upsert on the same user, kind and base URL) that would change the connection's account id is refused while any repo links to it, so mapping (D11) and the owner of the tags uzi creates (D14) never change silently. The link binds to the account its tag check ran against: the link transaction reads the connection row `FOR SHARE` and requires its account id to equal the checked account. The reconnect locks the connection row `FOR UPDATE` and checks for linked repos in a separate statement in the same transaction, so the two cannot race. Tested. +- **2026-11-11: the moved-issue acceptance tests a move before the write only (D10, owner guidance, 2026-11-11).** An issue moved between the driver's check and its write can still be written once, because YouTrack has no conditional write; that window is not tested. +- **2026-11-11: the M4 link messages do not claim edit rights (D14, owner guidance, 2026-11-11).** Tag sharing does not prove a person can edit issues in the linked project, so the link step's messages speak only of who can see and apply the tag.