feat(phase3): comments, issue compose, PR diff viewer, reviews, merge, checks
- API: comments CRUD, create/edit issue, milestones, collaborators, pull files, raw unified diff (getText), reviews, merge, combined commit status - hooks: comment thread + composer mutations, issue compose mutations, pull files/diff/reviews, create review, merge, combined status - UI: TextArea, ChipSelect, CommentItem, CommentComposer, native DiffView (parseDiff), StatusChecks - screens: IssueCompose (create/edit), PullFiles, PullDiff, PullReview; issue detail gains thread + close/reopen; PR detail gains reviews, merge, files, checks; commit detail shows checks - navigation/types + i18n (en/nl) updated; centralized safeHost helper - tests: diff parser, endpoint body shapes (merge capitalisation), client getText
This commit is contained in:
1 parent
f641238b6f
commit
ddd834b7ac
32 files changed
+2546
-68
No files matched your search
@@ -99,6 +99,33 @@ describe('GiteaClient pagination', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('GiteaClient getText', () => {
|
||||
it('returns raw text and sends the token only in the Authorization header', async () => {
|
||||
const diff = 'diff --git a/x b/x\n+added\n';
|
||||
const fetchImpl = jest.fn(async () => response(diff)) as unknown as jest.Mock;
|
||||
|
||||
const text = await client(fetchImpl).getText('/repos/o/r/pulls/1.diff');
|
||||
|
||||
expect(text).toBe(diff);
|
||||
const [url, init] = fetchImpl.mock.calls[0];
|
||||
expect(url).toBe('https://codeberg.org/api/v1/repos/o/r/pulls/1.diff');
|
||||
expect(init.headers.Authorization).toBe(`token ${TOKEN}`);
|
||||
expect(url).not.toContain(TOKEN);
|
||||
});
|
||||
|
||||
it('maps a non-2xx response to an ApiError without leaking the token', async () => {
|
||||
const fetchImpl = jest.fn(async () => response('nope', 404)) as unknown as jest.Mock;
|
||||
const error = await client(fetchImpl)
|
||||
.getText('/repos/o/r/pulls/1.diff')
|
||||
.then(() => null)
|
||||
.catch((e: unknown) => e as ApiError);
|
||||
|
||||
expect(error).toBeInstanceOf(ApiError);
|
||||
expect(error?.kind).toBe('not-found');
|
||||
expect(String(error?.message)).not.toContain(TOKEN);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GiteaClient error mapping', () => {
|
||||
it('maps 401 to an unauthorized ApiError', async () => {
|
||||
const fetchImpl = jest.fn(async () =>
|
||||
|
||||
@@ -117,6 +117,66 @@ export class GiteaClient {
|
||||
return raw.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a plain-text response (e.g. a pull request `.diff`).
|
||||
*
|
||||
* Shares the timeout/abort/error-normalization contract of {@link raw}, but
|
||||
* deliberately does not parse JSON. A non-2xx response is still mapped to an
|
||||
* {@link ApiError} that never contains the URL or the token.
|
||||
*/
|
||||
async getText(path: string, options: Omit<RequestOptions, 'method' | 'body'> = {}): Promise<string> {
|
||||
const url = `${this.apiRoot}${path}${buildQuery(options.query ?? {})}`;
|
||||
const headers: Record<string, string> = { Accept: 'text/plain' };
|
||||
if (this.token) headers.Authorization = `token ${this.token}`;
|
||||
|
||||
const controller = new AbortController();
|
||||
let timedOut = false;
|
||||
const timeoutMs = options.timeoutMs ?? this.timeoutMs;
|
||||
const timer = setTimeout(() => {
|
||||
timedOut = true;
|
||||
controller.abort();
|
||||
}, timeoutMs);
|
||||
const onCallerAbort = () => controller.abort();
|
||||
if (options.signal) {
|
||||
if (options.signal.aborted) {
|
||||
clearTimeout(timer);
|
||||
throw new ApiError({ kind: 'aborted', status: 0, message: 'Request cancelled.' });
|
||||
}
|
||||
options.signal.addEventListener('abort', onCallerAbort);
|
||||
}
|
||||
|
||||
let response: Response;
|
||||
try {
|
||||
response = await this.fetchImpl(url, { method: 'GET', headers, signal: controller.signal });
|
||||
} catch (error) {
|
||||
if (timedOut) {
|
||||
throw new ApiError({
|
||||
kind: 'timeout',
|
||||
status: 0,
|
||||
message: 'The server took too long to respond.',
|
||||
});
|
||||
}
|
||||
if (options.signal?.aborted) {
|
||||
throw new ApiError({ kind: 'aborted', status: 0, message: 'Request cancelled.' });
|
||||
}
|
||||
throw toApiError(error);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
options.signal?.removeEventListener('abort', onCallerAbort);
|
||||
}
|
||||
|
||||
const text = await response.text();
|
||||
if (!response.ok) {
|
||||
const kind = ApiError.kindForStatus(response.status);
|
||||
throw new ApiError({
|
||||
kind,
|
||||
status: response.status,
|
||||
message: messageForStatus(kind, response.status, null),
|
||||
});
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a request and returns data plus pagination metadata.
|
||||
* `hasMore` uses `X-Total-Count` when present, otherwise a length heuristic
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Endpoint body-shape guards.
|
||||
*
|
||||
* Gitea's merge API is a known exception to its lowercase JSON conventions
|
||||
* (`Do`, `MergeTitleField`, … are capitalised), so it is worth pinning down.
|
||||
*/
|
||||
|
||||
import { GiteaClient } from './client';
|
||||
import { createPullReview, mergePull } from './endpoints';
|
||||
|
||||
function response(body = '{}', status = 200): Response {
|
||||
return {
|
||||
ok: status >= 200 && status < 300,
|
||||
status,
|
||||
headers: { get: () => null },
|
||||
text: async () => body,
|
||||
} as unknown as Response;
|
||||
}
|
||||
|
||||
function capture() {
|
||||
const fetchImpl = jest.fn(async () => response()) as unknown as jest.Mock;
|
||||
const client = new GiteaClient({ baseUrl: 'https://codeberg.org', token: 'tok', fetchImpl });
|
||||
return { fetchImpl, client };
|
||||
}
|
||||
|
||||
function postedBody(fetchImpl: jest.Mock): Record<string, unknown> {
|
||||
return JSON.parse(fetchImpl.mock.calls[0][1].body as string) as Record<string, unknown>;
|
||||
}
|
||||
|
||||
describe('endpoint request bodies', () => {
|
||||
it('uses Gitea\'s capitalised merge field names', async () => {
|
||||
const { fetchImpl, client } = capture();
|
||||
await mergePull(client, 'octo', 'repo', 7, { style: 'squash', title: 'T', message: 'M' });
|
||||
const body = postedBody(fetchImpl);
|
||||
|
||||
expect(fetchImpl.mock.calls[0][0]).toBe('https://codeberg.org/api/v1/repos/octo/repo/pulls/7/merge');
|
||||
expect(body.Do).toBe('squash');
|
||||
expect(body.MergeTitleField).toBe('T');
|
||||
expect(body.MergeMessageField).toBe('M');
|
||||
});
|
||||
|
||||
it('submits a review with an event and body', async () => {
|
||||
const { fetchImpl, client } = capture();
|
||||
await createPullReview(client, 'octo', 'repo', 7, { event: 'REQUEST_CHANGES', body: 'please' });
|
||||
const body = postedBody(fetchImpl);
|
||||
|
||||
expect(body.event).toBe('REQUEST_CHANGES');
|
||||
expect(body.body).toBe('please');
|
||||
});
|
||||
});
|
||||
@@ -13,14 +13,20 @@ import type { GiteaClient, RequestOptions } from './client';
|
||||
import type {
|
||||
Activity,
|
||||
Branch,
|
||||
ChangedFile,
|
||||
CombinedStatus,
|
||||
Comment,
|
||||
Commit,
|
||||
ContentsEntry,
|
||||
Issue,
|
||||
Label,
|
||||
Milestone,
|
||||
NotificationCount,
|
||||
NotificationThread,
|
||||
PullRequest,
|
||||
PullReview,
|
||||
Repository,
|
||||
ReviewEvent,
|
||||
ServerVersion,
|
||||
Tag,
|
||||
User,
|
||||
@@ -388,6 +394,240 @@ export function fetchPull(
|
||||
return client.get<PullRequest>(`${repoPath(owner, repo)}/pulls/${index}`, options);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Issues, comments, labels, milestones, collaborators (write paths)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface CreateIssueInput {
|
||||
title: string;
|
||||
body?: string;
|
||||
labels?: number[];
|
||||
assignees?: string[];
|
||||
milestone?: number;
|
||||
closed?: boolean;
|
||||
}
|
||||
|
||||
export function createIssue(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
input: CreateIssueInput,
|
||||
options?: RequestOptions,
|
||||
): Promise<Issue> {
|
||||
return client.post<Issue>(`${repoPath(owner, repo)}/issues`, input, options);
|
||||
}
|
||||
|
||||
export interface EditIssueInput {
|
||||
title?: string;
|
||||
body?: string;
|
||||
state?: 'open' | 'closed';
|
||||
labels?: number[];
|
||||
assignees?: string[];
|
||||
milestone?: number | null;
|
||||
}
|
||||
|
||||
export function updateIssue(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
index: number,
|
||||
input: EditIssueInput,
|
||||
options?: RequestOptions,
|
||||
): Promise<Issue> {
|
||||
return client.patch<Issue>(`${repoPath(owner, repo)}/issues/${index}`, input, options);
|
||||
}
|
||||
|
||||
/** Issue and pull-request comments share this endpoint (PRs are issues in Gitea). */
|
||||
export function listIssueComments(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
index: number,
|
||||
params: PageParams = {},
|
||||
options?: RequestOptions,
|
||||
) {
|
||||
return client.raw<Comment[]>(`${repoPath(owner, repo)}/issues/${index}/comments`, {
|
||||
...options,
|
||||
query: { page: params.page ?? 1, limit: params.limit ?? 30 },
|
||||
});
|
||||
}
|
||||
|
||||
export function createIssueComment(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
index: number,
|
||||
body: string,
|
||||
options?: RequestOptions,
|
||||
): Promise<Comment> {
|
||||
return client.post<Comment>(
|
||||
`${repoPath(owner, repo)}/issues/${index}/comments`,
|
||||
{ body },
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
export function updateIssueComment(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
commentId: number,
|
||||
body: string,
|
||||
options?: RequestOptions,
|
||||
): Promise<Comment> {
|
||||
return client.patch<Comment>(
|
||||
`${repoPath(owner, repo)}/issues/comments/${commentId}`,
|
||||
{ body },
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
export function deleteIssueComment(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
commentId: number,
|
||||
options?: RequestOptions,
|
||||
): Promise<void> {
|
||||
return client.delete<void>(`${repoPath(owner, repo)}/issues/comments/${commentId}`, options);
|
||||
}
|
||||
|
||||
export function listMilestones(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
params: PageParams & { state?: 'open' | 'closed' | 'all' } = {},
|
||||
options?: RequestOptions,
|
||||
) {
|
||||
return client.raw<Milestone[]>(`${repoPath(owner, repo)}/milestones`, {
|
||||
...options,
|
||||
query: { page: params.page ?? 1, limit: params.limit ?? 50, state: params.state ?? 'open' },
|
||||
});
|
||||
}
|
||||
|
||||
export function listCollaborators(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
params: PageParams = {},
|
||||
options?: RequestOptions,
|
||||
) {
|
||||
return client.raw<User[]>(`${repoPath(owner, repo)}/collaborators`, {
|
||||
...options,
|
||||
query: { page: params.page ?? 1, limit: params.limit ?? 50 },
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Pull request files, diffs, reviews, merge
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export function listPullFiles(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
index: number,
|
||||
params: PageParams = {},
|
||||
options?: RequestOptions,
|
||||
): Promise<ChangedFile[]> {
|
||||
return client.get<ChangedFile[]>(`${repoPath(owner, repo)}/pulls/${index}/files`, {
|
||||
...options,
|
||||
query: { page: params.page ?? 1, limit: params.limit ?? 50 },
|
||||
});
|
||||
}
|
||||
|
||||
/** Raw unified diff for a pull request (`Accept: text/plain`). */
|
||||
export function fetchPullDiff(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
index: number,
|
||||
options?: RequestOptions,
|
||||
): Promise<string> {
|
||||
return client.getText(`${repoPath(owner, repo)}/pulls/${index}.diff`, options);
|
||||
}
|
||||
|
||||
export function listPullReviews(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
index: number,
|
||||
params: PageParams = {},
|
||||
options?: RequestOptions,
|
||||
) {
|
||||
return client.raw<PullReview[]>(`${repoPath(owner, repo)}/pulls/${index}/reviews`, {
|
||||
...options,
|
||||
query: { page: params.page ?? 1, limit: params.limit ?? 50 },
|
||||
});
|
||||
}
|
||||
|
||||
export interface CreateReviewInput {
|
||||
event: ReviewEvent;
|
||||
body?: string;
|
||||
commitId?: string;
|
||||
}
|
||||
|
||||
export function createPullReview(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
index: number,
|
||||
input: CreateReviewInput,
|
||||
options?: RequestOptions,
|
||||
): Promise<PullReview> {
|
||||
return client.post<PullReview>(
|
||||
`${repoPath(owner, repo)}/pulls/${index}/reviews`,
|
||||
{ event: input.event, body: input.body ?? '', commit_id: input.commitId },
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
export type MergeStyle = 'merge' | 'squash' | 'rebase' | 'rebase-merge' | 'fast-forward-only';
|
||||
|
||||
export interface MergePullInput {
|
||||
style: MergeStyle;
|
||||
title?: string;
|
||||
message?: string;
|
||||
deleteBranchAfterMerge?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges a pull request. A 409 means the branch moved (stale head SHA) and a
|
||||
* 405 means the request is not in a mergeable state.
|
||||
*/
|
||||
export function mergePull(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
index: number,
|
||||
input: MergePullInput,
|
||||
options?: RequestOptions,
|
||||
): Promise<void> {
|
||||
return client.post<void>(
|
||||
`${repoPath(owner, repo)}/pulls/${index}/merge`,
|
||||
{
|
||||
Do: input.style,
|
||||
MergeTitleField: input.title,
|
||||
MergeMessageField: input.message,
|
||||
delete_branch_after_merge: input.deleteBranchAfterMerge,
|
||||
},
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
export function fetchCombinedStatus(
|
||||
client: GiteaClient,
|
||||
owner: string,
|
||||
repo: string,
|
||||
ref: string,
|
||||
options?: RequestOptions,
|
||||
): Promise<CombinedStatus> {
|
||||
return client.get<CombinedStatus>(
|
||||
`${repoPath(owner, repo)}/commits/${enc(ref)}/status`,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Notifications
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -90,3 +90,13 @@ export function instanceHostLabel(instanceUrl: string): string {
|
||||
return instanceUrl;
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns the host (including port) of a URL, or null when it cannot be parsed. */
|
||||
export function safeHost(value: string | null | undefined): string | null {
|
||||
if (!value) return null;
|
||||
try {
|
||||
return new URL(value).host;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -69,28 +69,69 @@ export const queryKeys = {
|
||||
[...queryKeys.repo(accountId, owner, name), 'commit', sha] as const,
|
||||
|
||||
// --- issues & pull requests ------------------------------------------------
|
||||
issueSearchRoot: (accountId: string) =>
|
||||
[...queryKeys.account(accountId), 'issues', 'search'] as const,
|
||||
|
||||
issueSearch: (accountId: string, params: Record<string, unknown>) =>
|
||||
[...queryKeys.account(accountId), 'issues', 'search', params] as const,
|
||||
[...queryKeys.issueSearchRoot(accountId), params] as const,
|
||||
|
||||
repoIssuesRoot: (accountId: string, owner: string, name: string) =>
|
||||
[...queryKeys.repo(accountId, owner, name), 'issues'] as const,
|
||||
|
||||
repoIssues: (
|
||||
accountId: string,
|
||||
owner: string,
|
||||
name: string,
|
||||
params: Record<string, unknown>,
|
||||
) => [...queryKeys.repo(accountId, owner, name), 'issues', params] as const,
|
||||
) => [...queryKeys.repoIssuesRoot(accountId, owner, name), params] as const,
|
||||
|
||||
issue: (accountId: string, owner: string, name: string, index: number) =>
|
||||
[...queryKeys.repo(accountId, owner, name), 'issue', index] as const,
|
||||
|
||||
issueCommentsRoot: (accountId: string, owner: string, name: string, index: number) =>
|
||||
[...queryKeys.issue(accountId, owner, name, index), 'comments'] as const,
|
||||
|
||||
issueComments: (
|
||||
accountId: string,
|
||||
owner: string,
|
||||
name: string,
|
||||
index: number,
|
||||
params: Record<string, unknown>,
|
||||
) => [...queryKeys.issueCommentsRoot(accountId, owner, name, index), params] as const,
|
||||
|
||||
repoMilestones: (accountId: string, owner: string, name: string) =>
|
||||
[...queryKeys.repo(accountId, owner, name), 'milestones'] as const,
|
||||
|
||||
repoCollaborators: (accountId: string, owner: string, name: string) =>
|
||||
[...queryKeys.repo(accountId, owner, name), 'collaborators'] as const,
|
||||
|
||||
repoLabels: (accountId: string, owner: string, name: string) =>
|
||||
[...queryKeys.repo(accountId, owner, name), 'labels'] as const,
|
||||
|
||||
pullsRoot: (accountId: string, owner: string, name: string) =>
|
||||
[...queryKeys.repo(accountId, owner, name), 'pulls'] as const,
|
||||
|
||||
pulls: (accountId: string, owner: string, name: string, params: Record<string, unknown>) =>
|
||||
[...queryKeys.repo(accountId, owner, name), 'pulls', params] as const,
|
||||
[...queryKeys.pullsRoot(accountId, owner, name), params] as const,
|
||||
|
||||
pull: (accountId: string, owner: string, name: string, index: number) =>
|
||||
[...queryKeys.repo(accountId, owner, name), 'pull', index] as const,
|
||||
|
||||
pullFiles: (accountId: string, owner: string, name: string, index: number) =>
|
||||
[...queryKeys.pull(accountId, owner, name, index), 'files'] as const,
|
||||
|
||||
pullDiff: (accountId: string, owner: string, name: string, index: number) =>
|
||||
[...queryKeys.pull(accountId, owner, name, index), 'diff'] as const,
|
||||
|
||||
pullReviews: (accountId: string, owner: string, name: string, index: number) =>
|
||||
[...queryKeys.pull(accountId, owner, name, index), 'reviews'] as const,
|
||||
|
||||
combinedStatus: (accountId: string, owner: string, name: string, ref: string) =>
|
||||
[...queryKeys.repo(accountId, owner, name), 'status', ref] as const,
|
||||
|
||||
commitsRoot: (accountId: string, owner: string, name: string) =>
|
||||
[...queryKeys.repo(accountId, owner, name), 'commits'] as const,
|
||||
|
||||
// --- notifications ---------------------------------------------------------
|
||||
notificationsRoot: (accountId: string) =>
|
||||
[...queryKeys.account(accountId), 'notifications'] as const,
|
||||
|
||||
@@ -375,6 +375,63 @@ export interface Comment {
|
||||
updated_at?: string;
|
||||
}
|
||||
|
||||
/** A changed file in a pull request (GET /repos/{owner}/{repo}/pulls/{index}/files). */
|
||||
export interface ChangedFile {
|
||||
filename?: string;
|
||||
previous_filename?: string;
|
||||
status?: 'added' | 'removed' | 'modified' | 'renamed' | 'copied' | 'changed' | 'unchanged';
|
||||
additions?: number;
|
||||
deletions?: number;
|
||||
changes?: number;
|
||||
html_url?: string;
|
||||
raw_url?: string;
|
||||
contents_url?: string;
|
||||
}
|
||||
|
||||
/** A review left on a pull request. */
|
||||
export type ReviewState = 'APPROVED' | 'PENDING' | 'COMMENT' | 'REQUEST_CHANGES';
|
||||
export type ReviewEvent = 'APPROVE' | 'REQUEST_CHANGES' | 'COMMENT';
|
||||
|
||||
export interface PullReview {
|
||||
id?: number;
|
||||
user?: User;
|
||||
state?: ReviewState;
|
||||
body?: string;
|
||||
commit_id?: string;
|
||||
submitted_at?: string;
|
||||
updated_at?: string;
|
||||
stale?: boolean;
|
||||
dismissed?: boolean;
|
||||
official?: boolean;
|
||||
comments_count?: number;
|
||||
html_url?: string;
|
||||
}
|
||||
|
||||
export type CommitStatusState = 'pending' | 'success' | 'error' | 'failure' | 'warning' | 'skipped';
|
||||
|
||||
/** A single CI status (Gitea Actions or an external checker). */
|
||||
export interface CommitStatus {
|
||||
id?: number;
|
||||
status?: CommitStatusState;
|
||||
context?: string;
|
||||
description?: string;
|
||||
target_url?: string;
|
||||
url?: string;
|
||||
created_at?: string;
|
||||
updated_at?: string;
|
||||
creator?: User;
|
||||
}
|
||||
|
||||
/** Aggregated status of all checks for one commit. */
|
||||
export interface CombinedStatus {
|
||||
state?: CommitStatusState | 'no_status';
|
||||
sha?: string;
|
||||
total_count?: number;
|
||||
statuses?: CommitStatus[];
|
||||
commit_url?: string;
|
||||
url?: string;
|
||||
}
|
||||
|
||||
/** Generic pagination metadata parsed from response headers. */
|
||||
export interface PageMeta {
|
||||
page: number;
|
||||
|
||||
Reference in new issue
Block a user