Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesPull RequestsActionsSecurityInsightsSettings
✨ AI
More
Blame · Line-by-line history

repos.ts

Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.

repos.tsBlame150 lines · 1 contributor
5c83cccClaude1import { fetchJSON, postJSON } from './client';
9a8fbedClaude2import { useSettingsStore } from '../store/settingsStore';
5c83cccClaude3
4export interface Repository {
5 id: number;
6 name: string;
7 fullName: string;
8 description: string | null;
9 isPrivate: boolean;
10 isArchived: boolean;
11 isFork: boolean;
12 defaultBranch: string;
13 language: string | null;
14 starCount: number;
15 forkCount: number;
16 openIssueCount: number;
17 ownerUsername: string;
18 updatedAt: string;
19 createdAt: string;
20}
21
22export interface TreeEntry {
23 name: string;
24 type: 'blob' | 'tree';
25 mode: string;
26 sha: string;
27 path: string;
28}
29
30export interface FileContent {
31 name: string;
32 path: string;
33 sha: string;
34 content: string;
35 encoding: string;
36 size: number;
37}
38
39export interface CommitEntry {
40 sha: string;
41 message: string;
42 authorName: string;
43 authorEmail: string;
44 authorDate: string;
45 committerName: string;
46 committerDate: string;
47 parentShas: string[];
48}
49
50export interface BranchInfo {
51 name: string;
52 sha: string;
53 isDefault: boolean;
54 isProtected: boolean;
55}
56
57export interface SearchResult {
58 file: string;
59 line: number;
60 content: string;
61 sha: string;
62}
63
64export interface RepoStats {
65 commits: number;
66 branches: number;
67 tags: number;
68 contributors: number;
69}
70
71/** List repositories for a user. */
72export async function listUserRepos(username: string): Promise<Repository[]> {
73 return fetchJSON<Repository[]>(`/api/users/${encodeURIComponent(username)}/repos`);
74}
75
76/** List all public repos (explore). */
77export async function listPublicRepos(page = 1): Promise<Repository[]> {
78 return fetchJSON<Repository[]>(`/api/repos?page=${page}`);
79}
80
81/** Get a single repository. */
82export async function getRepo(owner: string, repo: string): Promise<Repository> {
83 return fetchJSON<Repository>(`/api/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}`);
84}
85
86/** List the file tree for a repo at a given ref and path. */
87export async function getTree(
88 owner: string,
89 repo: string,
90 ref = 'HEAD',
91 path = '',
92): Promise<TreeEntry[]> {
93 const encodedPath = path ? `/${encodeURIComponent(path)}` : '';
94 return fetchJSON<TreeEntry[]>(
95 `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/tree/${encodeURIComponent(ref)}${encodedPath}?json=1`,
96 );
97}
98
99/** Get raw file content. */
100export async function getFileContent(
101 owner: string,
102 repo: string,
103 ref: string,
104 path: string,
105): Promise<string> {
9a8fbedClaude106 const host = useSettingsStore.getState().host;
5c83cccClaude107 const response = await fetch(
9a8fbedClaude108 `${host}/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/raw/${encodeURIComponent(ref)}/${path}`,
5c83cccClaude109 {
110 headers: { Accept: 'text/plain' },
111 },
112 );
113 if (!response.ok) throw new Error(`HTTP ${response.status}`);
114 return response.text();
115}
116
117/** List commits on a branch. */
118export async function listCommits(
119 owner: string,
120 repo: string,
121 branch = 'HEAD',
122 page = 1,
123): Promise<CommitEntry[]> {
124 return fetchJSON<CommitEntry[]>(
125 `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/commits?branch=${encodeURIComponent(branch)}&page=${page}&json=1`,
126 );
127}
128
129/** List branches. */
130export async function listBranches(owner: string, repo: string): Promise<BranchInfo[]> {
131 return fetchJSON<BranchInfo[]>(
132 `/api/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/branches`,
133 );
134}
135
136/** Star a repo. */
137export async function starRepo(owner: string, repo: string): Promise<void> {
138 await postJSON(`/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/star`, {});
139}
140
141/** Search code in a repo. */
142export async function searchCode(
143 owner: string,
144 repo: string,
145 query: string,
146): Promise<SearchResult[]> {
147 return fetchJSON<SearchResult[]>(
148 `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/search?q=${encodeURIComponent(query)}&json=1`,
149 );
150}