-
Notifications
You must be signed in to change notification settings - Fork 56
feat(tasks): search tasks by PR number in command panel #1268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8f19ca3
feat(tasks): search tasks by PR number in command panel
zeval 100af2e
fix(tasks): scope PR-number search to page 1 and unfiltered queries
zeval 21a30b8
test(github): dedup PR-lookup test setup into shared newTestStore
zeval 67d9c77
perf(tasks): batch-fetch PR-matched tasks instead of N+1 GetTask
zeval File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/jmoiron/sqlx" | ||
|
|
||
| "github.com/kandev/kandev/internal/db" | ||
| ) | ||
|
|
||
| // newTestStoreWithWorkspaces builds a store whose `tasks` table carries | ||
| // workspace_id, so the workspace-scoped PR-number lookup can be exercised. | ||
| func newTestStoreWithWorkspaces(t *testing.T) *Store { | ||
|
zeval marked this conversation as resolved.
Outdated
zeval marked this conversation as resolved.
Outdated
|
||
| t.Helper() | ||
| tmp := t.TempDir() | ||
| dbConn, err := db.OpenSQLite(filepath.Join(tmp, "github.db")) | ||
| if err != nil { | ||
| t.Fatalf("open db: %v", err) | ||
| } | ||
| sqlxDB := sqlx.NewDb(dbConn, "sqlite3") | ||
| t.Cleanup(func() { _ = sqlxDB.Close() }) | ||
| if _, err := sqlxDB.Exec(`CREATE TABLE tasks (id TEXT PRIMARY KEY, workspace_id TEXT, archived_at DATETIME)`); err != nil { | ||
| t.Fatalf("create tasks table: %v", err) | ||
| } | ||
| store, err := NewStore(sqlxDB, sqlxDB) | ||
| if err != nil { | ||
| t.Fatalf("new store: %v", err) | ||
| } | ||
| return store | ||
| } | ||
|
|
||
| func TestListTaskIDsByPRNumber(t *testing.T) { | ||
| store := newTestStoreWithWorkspaces(t) | ||
| ctx := context.Background() | ||
| now := time.Now().UTC() | ||
|
|
||
| // task-1 (ws-1) has PR #1243, task-2 (ws-1) has PR #99, | ||
| // task-3 (ws-2) also has PR #1243 — must not leak into ws-1. | ||
| exec := store.db | ||
| for _, q := range []string{ | ||
| `INSERT INTO tasks (id, workspace_id) VALUES ('task-1', 'ws-1')`, | ||
| `INSERT INTO tasks (id, workspace_id) VALUES ('task-2', 'ws-1')`, | ||
| `INSERT INTO tasks (id, workspace_id) VALUES ('task-3', 'ws-2')`, | ||
| } { | ||
| if _, err := exec.Exec(q); err != nil { | ||
| t.Fatalf("seed task: %v", err) | ||
| } | ||
| } | ||
| mkPR := func(id, taskID string, num int) *TaskPR { | ||
| return &TaskPR{ | ||
| ID: id, TaskID: taskID, RepositoryID: "repo-" + id, | ||
| Owner: "kdlbs", Repo: "kandev", PRNumber: num, | ||
| PRURL: "https://github.com/kdlbs/kandev/pull/1", PRTitle: "x", | ||
| HeadBranch: "feat/x", BaseBranch: "main", State: "open", CreatedAt: now, | ||
| } | ||
| } | ||
| for _, pr := range []*TaskPR{ | ||
| mkPR("p1", "task-1", 1243), | ||
| mkPR("p2", "task-2", 99), | ||
| mkPR("p3", "task-3", 1243), | ||
| } { | ||
| if err := store.CreateTaskPR(ctx, pr); err != nil { | ||
| t.Fatalf("create PR: %v", err) | ||
| } | ||
| } | ||
|
|
||
| got, err := store.ListTaskIDsByPRNumber(ctx, "ws-1", 1243) | ||
| if err != nil { | ||
| t.Fatalf("lookup: %v", err) | ||
| } | ||
| if len(got) != 1 || got[0] != "task-1" { | ||
| t.Errorf("expected [task-1] for ws-1 PR #1243, got %v", got) | ||
| } | ||
|
|
||
| none, err := store.ListTaskIDsByPRNumber(ctx, "ws-1", 7777) | ||
| if err != nil { | ||
| t.Fatalf("lookup unknown: %v", err) | ||
| } | ||
| if len(none) != 0 { | ||
| t.Errorf("expected no tasks for unknown PR number, got %v", none) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
apps/backend/internal/task/service/service_pr_search_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/kandev/kandev/internal/task/models" | ||
| v1 "github.com/kandev/kandev/pkg/api/v1" | ||
| ) | ||
|
|
||
| // fakePRResolver is a stand-in for the github service in PR-number search tests. | ||
| type fakePRResolver struct { | ||
| byPR map[int][]string | ||
| err error | ||
| called bool | ||
| } | ||
|
|
||
| func (f *fakePRResolver) FindTaskIDsByPRNumber(_ context.Context, _ string, prNumber int) ([]string, error) { | ||
| f.called = true | ||
| if f.err != nil { | ||
| return nil, f.err | ||
| } | ||
| return f.byPR[prNumber], nil | ||
| } | ||
|
|
||
| func seedPRSearchTasks(t *testing.T, repo interface { | ||
| CreateWorkspace(context.Context, *models.Workspace) error | ||
| CreateWorkflow(context.Context, *models.Workflow) error | ||
| CreateTask(context.Context, *models.Task) error | ||
| }) { | ||
| t.Helper() | ||
| ctx := context.Background() | ||
| if err := repo.CreateWorkspace(ctx, &models.Workspace{ID: "ws-1", Name: "WS"}); err != nil { | ||
| t.Fatalf("create workspace: %v", err) | ||
| } | ||
| if err := repo.CreateWorkflow(ctx, &models.Workflow{ID: "wf-1", WorkspaceID: "ws-1", Name: "WF"}); err != nil { | ||
| t.Fatalf("create workflow: %v", err) | ||
| } | ||
| tasks := []*models.Task{ | ||
| {ID: "task-pr", WorkspaceID: "ws-1", WorkflowID: "wf-1", WorkflowStepID: "step-1", Title: "Unrelated work", State: v1.TaskStateCreated, Priority: "medium"}, | ||
| {ID: "task-title", WorkspaceID: "ws-1", WorkflowID: "wf-1", WorkflowStepID: "step-1", Title: "Bug in PR 1243 handler", State: v1.TaskStateCreated, Priority: "medium"}, | ||
| } | ||
| for _, tk := range tasks { | ||
| if err := repo.CreateTask(ctx, tk); err != nil { | ||
| t.Fatalf("create task %s: %v", tk.ID, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func taskIDSet(tasks []*models.Task) map[string]bool { | ||
| set := make(map[string]bool, len(tasks)) | ||
| for _, t := range tasks { | ||
| set[t.ID] = true | ||
| } | ||
| return set | ||
| } | ||
|
|
||
| func TestListTasksByWorkspace_PRNumberSurfacesTask(t *testing.T) { | ||
| svc, _, repo := createTestService(t) | ||
| seedPRSearchTasks(t, repo) | ||
| svc.SetPRTaskResolver(&fakePRResolver{byPR: map[int][]string{1243: {"task-pr"}}}) | ||
| ctx := context.Background() | ||
|
|
||
| // "#1243" — task-pr has no "1243" in its title, only the PR association. | ||
| tasks, total, err := svc.ListTasksByWorkspace(ctx, "ws-1", "", "", "#1243", 1, 5, true, false, false, false) | ||
| if err != nil { | ||
| t.Fatalf("search: %v", err) | ||
| } | ||
| got := taskIDSet(tasks) | ||
| if !got["task-pr"] { | ||
| t.Errorf("expected task-pr surfaced by PR number, got %v", got) | ||
| } | ||
| if total < 1 { | ||
| t.Errorf("expected total >= 1, got %d", total) | ||
| } | ||
| } | ||
|
|
||
| func TestListTasksByWorkspace_PRNumberDedupes(t *testing.T) { | ||
| svc, _, repo := createTestService(t) | ||
| seedPRSearchTasks(t, repo) | ||
| // Resolver returns task-title, which ALSO matches the LIKE search for "1243". | ||
| svc.SetPRTaskResolver(&fakePRResolver{byPR: map[int][]string{1243: {"task-title"}}}) | ||
| ctx := context.Background() | ||
|
|
||
| tasks, _, err := svc.ListTasksByWorkspace(ctx, "ws-1", "", "", "1243", 1, 5, true, false, false, false) | ||
| if err != nil { | ||
| t.Fatalf("search: %v", err) | ||
| } | ||
| count := 0 | ||
| for _, tk := range tasks { | ||
| if tk.ID == "task-title" { | ||
| count++ | ||
| } | ||
| } | ||
| if count != 1 { | ||
| t.Errorf("expected task-title exactly once, got %d", count) | ||
| } | ||
| } | ||
|
|
||
| func TestListTasksByWorkspace_NonNumericQuerySkipsResolver(t *testing.T) { | ||
| svc, _, repo := createTestService(t) | ||
| seedPRSearchTasks(t, repo) | ||
| resolver := &fakePRResolver{byPR: map[int][]string{1243: {"task-pr"}}} | ||
| svc.SetPRTaskResolver(resolver) | ||
| ctx := context.Background() | ||
|
|
||
| tasks, _, err := svc.ListTasksByWorkspace(ctx, "ws-1", "", "", "unrelated", 1, 5, true, false, false, false) | ||
| if err != nil { | ||
| t.Fatalf("search: %v", err) | ||
| } | ||
| if resolver.called { | ||
| t.Error("resolver should not be consulted for a non-numeric query") | ||
| } | ||
| // task-pr matches the title LIKE search, task-title does not. | ||
| if got := taskIDSet(tasks); !got["task-pr"] { | ||
| t.Errorf("expected title match task-pr, got %v", got) | ||
| } | ||
| } | ||
|
|
||
| func TestListTasksByWorkspace_NilResolverNoPanic(t *testing.T) { | ||
| svc, _, repo := createTestService(t) | ||
| seedPRSearchTasks(t, repo) | ||
| ctx := context.Background() | ||
|
|
||
| // No resolver wired — PR-number search must be a safe no-op. | ||
| tasks, _, err := svc.ListTasksByWorkspace(ctx, "ws-1", "", "", "#1243", 1, 5, true, false, false, false) | ||
| if err != nil { | ||
| t.Fatalf("search: %v", err) | ||
| } | ||
| // task-pr is only reachable via the resolver, so it must be absent. | ||
| if got := taskIDSet(tasks); got["task-pr"] { | ||
| t.Errorf("did not expect task-pr without a resolver, got %v", got) | ||
| } | ||
| } | ||
|
|
||
| func TestListTasksByWorkspace_PRMatchRespectsArchivedFilter(t *testing.T) { | ||
| svc, _, repo := createTestService(t) | ||
| seedPRSearchTasks(t, repo) | ||
| if err := repo.ArchiveTask(context.Background(), "task-pr"); err != nil { | ||
| t.Fatalf("archive: %v", err) | ||
| } | ||
| svc.SetPRTaskResolver(&fakePRResolver{byPR: map[int][]string{1243: {"task-pr"}}}) | ||
| ctx := context.Background() | ||
|
|
||
| // includeArchived=false → archived PR task excluded. | ||
| excluded, _, err := svc.ListTasksByWorkspace(ctx, "ws-1", "", "", "#1243", 1, 5, false, false, false, false) | ||
| if err != nil { | ||
| t.Fatalf("search excluded: %v", err) | ||
| } | ||
| if taskIDSet(excluded)["task-pr"] { | ||
| t.Error("archived task-pr should be excluded when includeArchived=false") | ||
| } | ||
|
|
||
| // includeArchived=true → archived PR task included. | ||
| included, _, err := svc.ListTasksByWorkspace(ctx, "ws-1", "", "", "#1243", 1, 5, true, false, false, false) | ||
| if err != nil { | ||
| t.Fatalf("search included: %v", err) | ||
| } | ||
| if !taskIDSet(included)["task-pr"] { | ||
| t.Error("archived task-pr should be included when includeArchived=true") | ||
| } | ||
| } | ||
|
|
||
| func TestListTasksByWorkspace_PRMatchSkippedOnLaterPagesAndScopedSearches(t *testing.T) { | ||
| ctx := context.Background() | ||
| cases := []struct { | ||
| name string | ||
| page int | ||
| workflowID string | ||
| repoID string | ||
| }{ | ||
| {name: "page 2", page: 2}, | ||
| {name: "workflow-scoped", page: 1, workflowID: "wf-1"}, | ||
| {name: "repository-scoped", page: 1, repoID: "repo-x"}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| svc, _, repo := createTestService(t) | ||
| seedPRSearchTasks(t, repo) | ||
| resolver := &fakePRResolver{byPR: map[int][]string{1243: {"task-pr"}}} | ||
| svc.SetPRTaskResolver(resolver) | ||
|
|
||
| tasks, _, err := svc.ListTasksByWorkspace(ctx, "ws-1", tc.workflowID, tc.repoID, "#1243", tc.page, 5, true, false, false, false) | ||
| if err != nil { | ||
| t.Fatalf("search: %v", err) | ||
| } | ||
| if resolver.called { | ||
| t.Error("resolver should not be consulted for later pages or scoped searches") | ||
| } | ||
| if taskIDSet(tasks)["task-pr"] { | ||
| t.Errorf("task-pr should not be augmented for %s, got %v", tc.name, taskIDSet(tasks)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| var _ PRTaskResolver = (*fakePRResolver)(nil) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.