Files
kandev/apps/web/lib/api/domains/github-api.ts
T
Luan Martins a852be3c33 feat(web): show task linkage on GitHub PR list (#952)
* feat(web): show task linkage on GitHub PR list

Add a compact indicator next to each PR showing whether a task already
exists. When tasks are linked, show a clickable button with the task
title and a count badge for multiple tasks. When no task exists, show
'No task created yet'.

- Add usePRKeyToTasks hook that maps PR keys to task associations
- Add PRRowTaskIndicator component for the per-PR task UI
- Wire the data through github-page-client into PRList
- Add tests for the indicator and hook utility

* feat(web): improve PR task indicator with task titles and multi-task dropdown

- Show actual task title instead of PR title in single-task case
- Replace count badge with dropdown menu for multiple linked tasks
- Switch icon from GitPullRequest to Checklist
- Load all workflow snapshots on GitHub page to hydrate task titles
- Fix test to use unique task_id per fixture

* feat(web): neutralize PR task indicator color and add tooltip

The checklist icon previously mirrored PR aggregate status, which duplicated
information already shown on the PR row and misleadingly suggested it
reflected task state. Drop the color and add a tooltip surfacing the actual
task title and workflow step.

Adds e2e coverage for the GitHub PR list task indicator: empty badge,
single-task button, and tooltip content.

* fix(web): navigate to task page when clicking PR task indicator

replaceTaskUrl only mutates history.state, which sets active task in the
store but does not trigger a Next.js route change. From /github, this left
the URL pointing to /t/<id> while the page still rendered the GitHub list.
Use router.push so the click actually navigates to the task.

* test(web): cover PR task indicator click navigation

Extends the GitHub PR list task indicator spec with a click assertion
verifying the URL changes to /t/<taskId> and the PR list unmounts.
Regression guard for the replaceTaskUrl → router.push fix.

* fix(github): link task to PR at creation time, not via branch poller

Tasks started from the GitHub PR list "+ Task" button were left without a
github_task_prs row when the worktree branch did not match the PR's head
branch on GitHub. This happened for PR-review-style tasks, which check out
synthetic branches (e.g. feature/review-...-h92) that don't exist as PR
heads. The poller's FindPRByBranch could never resolve them, so only the
first task whose branch happened to match got linked — every other task
created from the same PR was missing from the indicator.

The PR is already known at "+ Task" time, so persist the linkage
immediately rather than waiting on branch-based discovery:

- AssociateExistingPRByURL: new service method that creates the
  github_task_prs row from a PR URL without creating a PR watch. The
  watch is still created later at agent start via ensureSessionPRWatch.
- POST /api/v1/github/task-prs/associate: HTTP endpoint exposing it.
- QuickTaskLauncher.handleSuccess: after task creation, fires a
  best-effort associate call with the task's repository_id and PR URL.
  Failures fall back to the legacy poller path silently.

Backend test covers two distinct tasks linking to the same PR.
E2e adds a PR with two tasks and asserts the multi-task indicator
("Tasks 2") renders alongside the single-task and empty cases.

* refactor(github): make task-PR create endpoint RESTful

Rename POST /api/v1/github/task-prs/associate to POST /api/v1/github/task-prs.
The action-named "/associate" was RPC-styled; the resource collection is the
right URL for creation alongside the existing GET. Frontend client renamed
associateTaskPR → createTaskPR to match.

* fix(github): address PR review findings

- Map invalid PR URL to HTTP 400 (was 500). New ErrInvalidPRURL sentinel
  lets the controller distinguish parse failures from genuine server
  errors. Other failure paths still return 500.
- Prefer dialog-time repositoryId in pickRepositoryIdForPR. Multi-repo
  tasks with shared branch names were ambiguous under pure branch
  matching; the matched repo id captured by the launcher dialog wins
  when present.
- Cover usePRKeyToTasks (not only the prKey util) with hook tests:
  empty input, multi-task grouping, distinct keys, and the non-array
  defensive branch.
- Lock createTaskPR request semantics: method/body now win over a
  caller's options.init so the POST JSON shape can't be overridden.

* fix(web): use TaskPR row id for dropdown key

Multi-repo tasks can produce more than one TaskPR row for the same PR
(one row per repository_id), so keying the multi-task dropdown by
task_id risked a duplicate-key warning and unstable reconciliation.
Switch to the row id, which is unique per row.
2026-05-20 22:38:44 +01:00

368 lines
11 KiB
TypeScript

import { fetchJson, type ApiRequestOptions } from "../client";
import type {
GitHubStatusResponse,
GitHubOrg,
GitHubRepoInfo,
GitHubPR,
TaskPRsResponse,
TaskPR,
PRFeedback,
PRWatchesResponse,
ReviewWatch,
ReviewWatchesResponse,
CreateReviewWatchRequest,
UpdateReviewWatchRequest,
TriggerReviewResponse,
PRStatsResponse,
IssueWatch,
IssueWatchesResponse,
CreateIssueWatchRequest,
UpdateIssueWatchRequest,
TriggerIssueResponse,
SearchPRsResponse,
SearchIssuesResponse,
GitHubPRStatus,
GitHubActionPresets,
UpdateGitHubActionPresetsRequest,
} from "@/lib/types/github";
// Status
export async function fetchGitHubStatus(options?: ApiRequestOptions) {
return fetchJson<GitHubStatusResponse>("/api/v1/github/status", options);
}
// Token configuration
export async function configureGitHubToken(token: string) {
return fetchJson<{ configured: boolean }>("/api/v1/github/token", {
init: {
method: "POST",
body: JSON.stringify({ token }),
},
});
}
export async function clearGitHubToken() {
return fetchJson<{ cleared: boolean }>("/api/v1/github/token", {
init: { method: "DELETE" },
});
}
// Task PR associations
export async function listTaskPRs(taskIds: string[], options?: ApiRequestOptions) {
const query = new URLSearchParams();
query.set("task_ids", taskIds.join(","));
return fetchJson<TaskPRsResponse>(`/api/v1/github/task-prs?${query.toString()}`, options);
}
export async function listWorkspaceTaskPRs(workspaceId: string, options?: ApiRequestOptions) {
return fetchJson<TaskPRsResponse>(
`/api/v1/github/task-prs?workspace_id=${encodeURIComponent(workspaceId)}`,
options,
);
}
export async function getTaskPR(taskId: string, options?: ApiRequestOptions) {
return fetchJson<TaskPR>(`/api/v1/github/task-prs/${taskId}`, options);
}
export async function createTaskPR(
data: { task_id: string; repository_id?: string; pr_url: string },
options?: ApiRequestOptions,
) {
return fetchJson<TaskPR>(`/api/v1/github/task-prs`, {
...options,
init: {
...(options?.init ?? {}),
method: "POST",
body: JSON.stringify(data),
},
});
}
// PR feedback (live from GitHub)
export async function getPRFeedback(
owner: string,
repo: string,
number: number,
options?: ApiRequestOptions,
) {
return fetchJson<PRFeedback>(`/api/v1/github/prs/${owner}/${repo}/${number}`, options);
}
// Lightweight PR status (review + checks + mergeable), skips comments.
export async function getPRStatus(
owner: string,
repo: string,
number: number,
options?: ApiRequestOptions,
) {
return fetchJson<GitHubPRStatus>(`/api/v1/github/prs/${owner}/${repo}/${number}/status`, options);
}
export type PRStatusRef = { owner: string; repo: string; number: number };
// Batch variant of getPRStatus: one round-trip for a whole list page. The
// backend fans out concurrently and caches per-PR, so repeat calls for the
// same page are cheap. Keys in the returned map are "<owner>/<repo>#<number>".
export async function getPRStatusesBatch(refs: PRStatusRef[], options?: ApiRequestOptions) {
return fetchJson<{ statuses: Record<string, GitHubPRStatus> }>(`/api/v1/github/prs/statuses`, {
...options,
init: {
method: "POST",
body: JSON.stringify({ refs }),
...(options?.init ?? {}),
},
});
}
// Submit PR review
export async function submitPRReview(
owner: string,
repo: string,
number: number,
event: "APPROVE" | "COMMENT" | "REQUEST_CHANGES",
body?: string,
) {
return fetchJson<{ submitted: boolean }>(
`/api/v1/github/prs/${owner}/${repo}/${number}/reviews`,
{
init: {
method: "POST",
body: JSON.stringify({ event, body: body ?? "" }),
},
},
);
}
// PR watches
export async function listPRWatches(options?: ApiRequestOptions) {
return fetchJson<PRWatchesResponse>("/api/v1/github/watches/pr", options);
}
export async function deletePRWatch(id: string, options?: ApiRequestOptions) {
return fetchJson<{ success: boolean }>(`/api/v1/github/watches/pr/${id}`, {
...options,
init: { method: "DELETE", ...(options?.init ?? {}) },
});
}
// Review watches
// Omit workspaceId to fetch every watch across all workspaces.
export async function listReviewWatches(workspaceId?: string, options?: ApiRequestOptions) {
const path = workspaceId
? `/api/v1/github/watches/review?workspace_id=${encodeURIComponent(workspaceId)}`
: `/api/v1/github/watches/review`;
return fetchJson<ReviewWatchesResponse>(path, options);
}
export async function createReviewWatch(
payload: CreateReviewWatchRequest,
options?: ApiRequestOptions,
) {
return fetchJson<ReviewWatch>("/api/v1/github/watches/review", {
...options,
init: { method: "POST", body: JSON.stringify(payload), ...(options?.init ?? {}) },
});
}
export async function updateReviewWatch(
id: string,
payload: UpdateReviewWatchRequest,
options?: ApiRequestOptions,
) {
return fetchJson<ReviewWatch>(`/api/v1/github/watches/review/${id}`, {
...options,
init: { method: "PUT", body: JSON.stringify(payload), ...(options?.init ?? {}) },
});
}
export async function deleteReviewWatch(id: string, options?: ApiRequestOptions) {
return fetchJson<{ success: boolean }>(`/api/v1/github/watches/review/${id}`, {
...options,
init: { method: "DELETE", ...(options?.init ?? {}) },
});
}
export async function triggerReviewWatch(id: string, options?: ApiRequestOptions) {
return fetchJson<TriggerReviewResponse>(`/api/v1/github/watches/review/${id}/trigger`, {
...options,
init: { method: "POST", ...(options?.init ?? {}) },
});
}
export async function triggerAllReviewWatches(workspaceId: string, options?: ApiRequestOptions) {
const query = new URLSearchParams({ workspace_id: workspaceId });
return fetchJson<TriggerReviewResponse>(
`/api/v1/github/watches/review/trigger-all?${query.toString()}`,
{
...options,
init: { method: "POST", ...(options?.init ?? {}) },
},
);
}
// Orgs & repo search
export async function listUserOrgs(options?: ApiRequestOptions) {
return fetchJson<{ orgs: GitHubOrg[] }>("/api/v1/github/orgs", options);
}
export async function searchOrgRepos(org: string, query?: string, options?: ApiRequestOptions) {
const params = new URLSearchParams({ org });
if (query) params.set("q", query);
return fetchJson<{ repos: GitHubRepoInfo[] }>(
`/api/v1/github/repos/search?${params.toString()}`,
options,
);
}
// PR info (lightweight)
export async function fetchPRInfo(
owner: string,
repo: string,
number: number,
options?: ApiRequestOptions,
) {
return fetchJson<GitHubPR>(
`/api/v1/github/prs/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/${number}/info`,
options,
);
}
// Remote repo branches
export async function fetchRepoBranches(owner: string, repo: string, options?: ApiRequestOptions) {
return fetchJson<{ branches: { name: string }[] }>(
`/api/v1/github/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/branches`,
options,
);
}
// Stats
export async function fetchGitHubStats(
params?: { workspace_id?: string; start_date?: string; end_date?: string },
options?: ApiRequestOptions,
) {
const query = new URLSearchParams();
if (params?.workspace_id) query.set("workspace_id", params.workspace_id);
if (params?.start_date) query.set("start_date", params.start_date);
if (params?.end_date) query.set("end_date", params.end_date);
const suffix = query.toString();
return fetchJson<PRStatsResponse>(`/api/v1/github/stats${suffix ? `?${suffix}` : ""}`, options);
}
// Issue watches
// Omit workspaceId to fetch every watch across all workspaces.
export async function listIssueWatches(workspaceId?: string, options?: ApiRequestOptions) {
const path = workspaceId
? `/api/v1/github/watches/issue?workspace_id=${encodeURIComponent(workspaceId)}`
: `/api/v1/github/watches/issue`;
return fetchJson<IssueWatchesResponse>(path, options);
}
export async function createIssueWatch(
payload: CreateIssueWatchRequest,
options?: ApiRequestOptions,
) {
return fetchJson<IssueWatch>("/api/v1/github/watches/issue", {
...options,
init: { method: "POST", body: JSON.stringify(payload), ...(options?.init ?? {}) },
});
}
export async function updateIssueWatch(
id: string,
payload: UpdateIssueWatchRequest,
options?: ApiRequestOptions,
) {
return fetchJson<IssueWatch>(`/api/v1/github/watches/issue/${id}`, {
...options,
init: { method: "PUT", body: JSON.stringify(payload), ...(options?.init ?? {}) },
});
}
export async function deleteIssueWatch(id: string, options?: ApiRequestOptions) {
return fetchJson<{ deleted: boolean }>(`/api/v1/github/watches/issue/${id}`, {
...options,
init: { method: "DELETE", ...(options?.init ?? {}) },
});
}
export async function triggerIssueWatch(id: string, options?: ApiRequestOptions) {
return fetchJson<TriggerIssueResponse>(`/api/v1/github/watches/issue/${id}/trigger`, {
...options,
init: { method: "POST", ...(options?.init ?? {}) },
});
}
export async function triggerAllIssueWatches(workspaceId: string, options?: ApiRequestOptions) {
const query = new URLSearchParams({ workspace_id: workspaceId });
return fetchJson<TriggerIssueResponse>(
`/api/v1/github/watches/issue/trigger-all?${query.toString()}`,
{
...options,
init: { method: "POST", ...(options?.init ?? {}) },
},
);
}
// User PR / issue search (for the /github page).
// Pass `query` to use a verbatim GitHub search string, or `filter` to append to
// the default (type:pr state:open / type:issue state:open).
type SearchParams = {
query?: string;
filter?: string;
page?: number;
perPage?: number;
};
function buildSearchQuery(params: SearchParams) {
const search = new URLSearchParams();
if (params.query) search.set("query", params.query);
if (params.filter) search.set("filter", params.filter);
if (params.page && params.page > 1) search.set("page", String(params.page));
if (params.perPage) search.set("per_page", String(params.perPage));
return search.toString();
}
export async function searchUserPRs(params: SearchParams, options?: ApiRequestOptions) {
const suffix = buildSearchQuery(params);
return fetchJson<SearchPRsResponse>(
`/api/v1/github/user/prs${suffix ? `?${suffix}` : ""}`,
options,
);
}
export async function searchUserIssues(params: SearchParams, options?: ApiRequestOptions) {
const suffix = buildSearchQuery(params);
return fetchJson<SearchIssuesResponse>(
`/api/v1/github/user/issues${suffix ? `?${suffix}` : ""}`,
options,
);
}
// Action presets (quick-launch prompts on the /github page).
export async function fetchGitHubActionPresets(workspaceId: string, options?: ApiRequestOptions) {
const query = new URLSearchParams({ workspace_id: workspaceId });
return fetchJson<GitHubActionPresets>(
`/api/v1/github/action-presets?${query.toString()}`,
options,
);
}
export async function updateGitHubActionPresets(
payload: UpdateGitHubActionPresetsRequest,
options?: ApiRequestOptions,
) {
return fetchJson<GitHubActionPresets>("/api/v1/github/action-presets", {
...options,
init: { ...(options?.init ?? {}), method: "PUT", body: JSON.stringify(payload) },
});
}
export async function resetGitHubActionPresets(workspaceId: string, options?: ApiRequestOptions) {
const query = new URLSearchParams({ workspace_id: workspaceId });
return fetchJson<GitHubActionPresets>(`/api/v1/github/action-presets/reset?${query.toString()}`, {
...options,
init: { ...(options?.init ?? {}), method: "POST" },
});
}