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