Skip to content

Commit 7745cb7

Browse files
committed
refac
1 parent 9451874 commit 7745cb7

10 files changed

Lines changed: 2231 additions & 102 deletions

File tree

‎cptr/frontend/src/lib/apis/git.ts‎

Lines changed: 178 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,61 @@ export interface GitOperationResult {
5656
message: string;
5757
}
5858

59+
export interface GitPrAuthor {
60+
login?: string;
61+
name?: string;
62+
}
63+
64+
export interface GitPr {
65+
number: number;
66+
title: string;
67+
url: string;
68+
state?: string;
69+
isDraft?: boolean;
70+
reviewDecision?: string;
71+
statusCheckRollup?: unknown[];
72+
headRefName?: string;
73+
baseRefName?: string;
74+
author?: GitPrAuthor;
75+
assignees?: GitPrAuthor[];
76+
labels?: { name?: string; color?: string; description?: string }[];
77+
milestone?: { title?: string } | null;
78+
reviewRequests?: unknown[];
79+
latestReviews?: unknown[];
80+
mergeable?: string;
81+
mergeStateStatus?: string;
82+
maintainerCanModify?: boolean;
83+
createdAt?: string;
84+
updatedAt?: string;
85+
additions?: number;
86+
deletions?: number;
87+
changedFiles?: number;
88+
body?: string;
89+
comments?: unknown[];
90+
reviews?: unknown[];
91+
commits?: unknown[];
92+
files?: unknown[];
93+
}
94+
95+
export interface GitPrCapabilities {
96+
is_github: boolean;
97+
gh_installed: boolean;
98+
authenticated: boolean;
99+
default_branch?: string;
100+
message?: string;
101+
}
102+
103+
export interface GitPrCheck {
104+
name?: string;
105+
workflow?: string;
106+
state?: string;
107+
bucket?: 'pass' | 'fail' | 'pending' | 'skipping' | 'cancel' | string;
108+
description?: string;
109+
link?: string;
110+
startedAt?: string;
111+
completedAt?: string;
112+
}
113+
59114
// Deduplicate concurrent getGitStatus calls.
60115
// Multiple components (layout, GitBar, FileEditor × N) all fetch on mount;
61116
// this ensures they share a single in-flight request and a brief result cache.
@@ -110,9 +165,7 @@ export const getGitStashes = (root: string) =>
110165
fetchJSON(`/api/git/stashes?root=${encodeURIComponent(root)}`);
111166

112167
export const getGitConfig = (root?: string) =>
113-
fetchJSON<GitSettingsConfig>(
114-
`/api/git/config${root ? `?root=${encodeURIComponent(root)}` : ''}`
115-
);
168+
fetchJSON<GitSettingsConfig>(`/api/git/config${root ? `?root=${encodeURIComponent(root)}` : ''}`);
116169

117170
export const startGhLogin = (hostname = 'github.com', git_protocol = 'https') =>
118171
fetchJSON<GhLoginStatus>('/api/git/gh/login/start', jsonBody({ hostname, git_protocol }));
@@ -132,6 +185,126 @@ export const ghSwitch = (hostname: string, user: string) =>
132185
export const ghSetupGit = (hostname = 'github.com') =>
133186
fetchJSON('/api/git/gh/setup-git', jsonBody({ hostname }));
134187

188+
export const getGitPrCapabilities = (root: string) =>
189+
fetchJSON<GitPrCapabilities>(`/api/git/pr/capabilities?root=${encodeURIComponent(root)}`);
190+
191+
export const getGitCurrentPr = (root: string) =>
192+
fetchJSON<{ found: boolean; pr?: GitPr | null }>(
193+
`/api/git/pr/current?root=${encodeURIComponent(root)}`
194+
);
195+
196+
export const getGitPrList = (
197+
root: string,
198+
state = 'open',
199+
scope = 'all',
200+
search = '',
201+
limit = 30
202+
) =>
203+
fetchJSON<{ items: GitPr[] }>(
204+
`/api/git/pr/list?root=${encodeURIComponent(root)}&state=${encodeURIComponent(state)}&scope=${encodeURIComponent(scope)}&search=${encodeURIComponent(search)}&limit=${limit}`
205+
);
206+
207+
export const getGitPrView = (root: string, number: number) =>
208+
fetchJSON<GitPr>(
209+
`/api/git/pr/view?root=${encodeURIComponent(root)}&number=${encodeURIComponent(number)}`
210+
);
211+
212+
export const getGitPrDiff = (root: string, number: number, ignoreWhitespace = false) =>
213+
fetchJSON(
214+
`/api/git/pr/diff?root=${encodeURIComponent(root)}&number=${encodeURIComponent(number)}&ignore_whitespace=${ignoreWhitespace}`
215+
);
216+
217+
export const getGitCompareDiff = (
218+
root: string,
219+
base: string,
220+
head: string,
221+
ignoreWhitespace = false
222+
) =>
223+
fetchJSON(
224+
`/api/git/compare/diff?root=${encodeURIComponent(root)}&base=${encodeURIComponent(base)}&head=${encodeURIComponent(head)}&ignore_whitespace=${ignoreWhitespace}`
225+
);
226+
227+
export const getGitPrChecks = (root: string, number: number) =>
228+
fetchJSON<{ items: GitPrCheck[] }>(
229+
`/api/git/pr/checks?root=${encodeURIComponent(root)}&number=${encodeURIComponent(number)}`
230+
);
231+
232+
export const gitPrCheckout = (root: string, number: number) =>
233+
fetchJSON<GitOperationResult>('/api/git/pr/checkout', jsonBody({ root, number }));
234+
235+
export const gitPrCreate = (
236+
root: string,
237+
body: {
238+
title: string;
239+
body?: string;
240+
repo?: string;
241+
base?: string;
242+
head?: string;
243+
draft?: boolean;
244+
reviewers?: string[];
245+
assignees?: string[];
246+
labels?: string[];
247+
milestone?: string;
248+
project?: string;
249+
maintainer_edit?: boolean;
250+
}
251+
) => fetchJSON<{ ok: boolean; url?: string }>('/api/git/pr/create', jsonBody({ root, ...body }));
252+
253+
export const gitPrEdit = (
254+
root: string,
255+
number: number,
256+
body: {
257+
title?: string;
258+
body?: string;
259+
base?: string;
260+
add_reviewers?: string[];
261+
remove_reviewers?: string[];
262+
add_assignees?: string[];
263+
remove_assignees?: string[];
264+
add_labels?: string[];
265+
remove_labels?: string[];
266+
milestone?: string;
267+
remove_milestone?: boolean;
268+
}
269+
) => fetchJSON<GitOperationResult>('/api/git/pr/edit', jsonBody({ root, number, ...body }));
270+
271+
export const gitPrReady = (root: string, number: number, draft = false) =>
272+
fetchJSON<GitOperationResult>('/api/git/pr/ready', jsonBody({ root, number, draft }));
273+
274+
export const gitPrClose = (
275+
root: string,
276+
number: number,
277+
{ comment = '', delete_branch = false }: { comment?: string; delete_branch?: boolean } = {}
278+
) => fetchJSON<GitOperationResult>('/api/git/pr/close', jsonBody({ root, number, comment, delete_branch }));
279+
280+
export const gitPrReopen = (root: string, number: number) =>
281+
fetchJSON<GitOperationResult>('/api/git/pr/reopen', jsonBody({ root, number }));
282+
283+
export const gitPrUpdateBranch = (root: string, number: number, rebase = false) =>
284+
fetchJSON<GitOperationResult>('/api/git/pr/update-branch', jsonBody({ root, number, rebase }));
285+
286+
export const gitPrMerge = (
287+
root: string,
288+
number: number,
289+
body: {
290+
strategy?: 'merge' | 'squash' | 'rebase';
291+
auto?: boolean;
292+
delete_branch?: boolean;
293+
subject?: string;
294+
body?: string;
295+
} = {}
296+
) => fetchJSON<GitOperationResult>('/api/git/pr/merge', jsonBody({ root, number, ...body }));
297+
298+
export const gitPrComment = (root: string, number: number, body: string) =>
299+
fetchJSON<GitOperationResult>('/api/git/pr/comment', jsonBody({ root, number, body }));
300+
301+
export const gitPrReview = (
302+
root: string,
303+
number: number,
304+
event: 'approve' | 'comment' | 'request_changes',
305+
body = ''
306+
) => fetchJSON<GitOperationResult>('/api/git/pr/review', jsonBody({ root, number, event, body }));
307+
135308
export const stageFiles = (root: string, files: string[]) =>
136309
fetchJSON('/api/git/stage', jsonBody({ root, files }));
137310

@@ -163,7 +336,8 @@ export const gitPush = (
163336
set_upstream = false,
164337
branch
165338
}: { force?: boolean; set_upstream?: boolean; branch?: string } = {}
166-
) => fetchJSON<GitOperationResult>('/api/git/push', jsonBody({ root, force, set_upstream, branch }));
339+
) =>
340+
fetchJSON<GitOperationResult>('/api/git/push', jsonBody({ root, force, set_upstream, branch }));
167341

168342
export const gitUncommit = (root: string) => fetchJSON('/api/git/uncommit', jsonBody({ root }));
169343

0 commit comments

Comments
 (0)