github.ts195 lines, 4.2 KB
| 1 | import { db } from "@/server/db"; |
| 2 | |
| 3 | export interface GitHubPullRequestFile { |
| 4 | sha: string; |
| 5 | filename: string; |
| 6 | status: |
| 7 | | "added" |
| 8 | | "removed" |
| 9 | | "modified" |
| 10 | | "renamed" |
| 11 | | "copied" |
| 12 | | "changed" |
| 13 | | "unchanged"; |
| 14 | additions: number; |
| 15 | deletions: number; |
| 16 | changes: number; |
| 17 | patch?: string; |
| 18 | previous_filename?: string; |
| 19 | } |
| 20 | |
| 21 | export interface GitHubUser { |
| 22 | login: string; |
| 23 | avatar_url: string; |
| 24 | } |
| 25 | |
| 26 | export interface GitHubPullRequest { |
| 27 | id: number; |
| 28 | number: number; |
| 29 | title: string; |
| 30 | state: "open" | "closed"; |
| 31 | html_url: string; |
| 32 | user: GitHubUser; |
| 33 | created_at: string; |
| 34 | updated_at: string; |
| 35 | merged_at: string | null; |
| 36 | draft: boolean; |
| 37 | head: { |
| 38 | ref: string; |
| 39 | sha: string; |
| 40 | }; |
| 41 | base: { |
| 42 | ref: string; |
| 43 | }; |
| 44 | additions: number; |
| 45 | deletions: number; |
| 46 | changed_files: number; |
| 47 | } |
| 48 | |
| 49 | export interface GitHubRepo { |
| 50 | id: number; |
| 51 | name: string; |
| 52 | full_name: string; |
| 53 | private: boolean; |
| 54 | html_url: string; |
| 55 | description: string | null; |
| 56 | language: string | null; |
| 57 | stargazers_count: number; |
| 58 | updated_at: string; |
| 59 | } |
| 60 | |
| 61 | export async function getGitHubAccessToken( |
| 62 | userId: string, |
| 63 | ): Promise<string | null> { |
| 64 | const account = await db.account.findFirst({ |
| 65 | where: { |
| 66 | userId, |
| 67 | providerId: "github", |
| 68 | }, |
| 69 | select: { |
| 70 | accessToken: true, |
| 71 | }, |
| 72 | }); |
| 73 | |
| 74 | return account?.accessToken ?? null; |
| 75 | } |
| 76 | |
| 77 | export async function fetchGitHubRepos( |
| 78 | accessToken: string, |
| 79 | ): Promise<GitHubRepo[]> { |
| 80 | const repos: GitHubRepo[] = []; |
| 81 | let page = 1; |
| 82 | const perPage = 100; |
| 83 | |
| 84 | while (true) { |
| 85 | const response = await fetch( |
| 86 | `https://api.github.com/user/repos?per_page=${perPage}&page=${page}&sort=updated`, |
| 87 | { |
| 88 | headers: { |
| 89 | Authorization: `Bearer ${accessToken}`, |
| 90 | Accept: "application/vnd.github.v3+json", |
| 91 | }, |
| 92 | }, |
| 93 | ); |
| 94 | |
| 95 | if (!response.ok) { |
| 96 | throw new Error(`Failed to fetch GitHub repos: ${response.status}`); |
| 97 | } |
| 98 | |
| 99 | const data = (await response.json()) as GitHubRepo[]; |
| 100 | repos.push(...data); |
| 101 | if (data.length < perPage) break; |
| 102 | page++; |
| 103 | } |
| 104 | |
| 105 | return repos; |
| 106 | } |
| 107 | |
| 108 | export async function fetchPullRequests( |
| 109 | accessToken: string, |
| 110 | owner: string, |
| 111 | repo: string, |
| 112 | state: "open" | "closed" | "all" = "open", |
| 113 | ): Promise<GitHubPullRequest[]> { |
| 114 | const response = await fetch( |
| 115 | `https://api.github.com/repos/${owner}/${repo}/pulls?state=${state}&per_page=30&sort=updated&direction=desc`, |
| 116 | { |
| 117 | headers: { |
| 118 | Authorization: `Bearer ${accessToken}`, |
| 119 | Accept: "application/vnd.github.v3+json", |
| 120 | }, |
| 121 | }, |
| 122 | ); |
| 123 | |
| 124 | if (!response.ok) { |
| 125 | throw new Error(`GitHub API error: ${response.status}`); |
| 126 | } |
| 127 | |
| 128 | const pulls = (await response.json()) as GitHubPullRequest[]; |
| 129 | |
| 130 | // The list endpoint doesn't include additions/deletions/changed_files, |
| 131 | // so we fetch each PR individually to get those stats. |
| 132 | const detailed = await Promise.all( |
| 133 | pulls.map((pr) => fetchPullRequest(accessToken, owner, repo, pr.number)), |
| 134 | ); |
| 135 | |
| 136 | return detailed; |
| 137 | } |
| 138 | |
| 139 | export async function fetchPullRequest( |
| 140 | accessToken: string, |
| 141 | owner: string, |
| 142 | repo: string, |
| 143 | prNumber: number, |
| 144 | ): Promise<GitHubPullRequest> { |
| 145 | const response = await fetch( |
| 146 | `https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}`, |
| 147 | { |
| 148 | headers: { |
| 149 | Authorization: `Bearer ${accessToken}`, |
| 150 | Accept: "application/vnd.github.v3+json", |
| 151 | }, |
| 152 | }, |
| 153 | ); |
| 154 | |
| 155 | if (!response.ok) { |
| 156 | throw new Error(`GitHub API error: ${response.status}`); |
| 157 | } |
| 158 | |
| 159 | return (await response.json()) as GitHubPullRequest; |
| 160 | } |
| 161 | |
| 162 | export async function fetchPullRequestFiles( |
| 163 | accessToken: string, |
| 164 | owner: string, |
| 165 | repo: string, |
| 166 | prNumber: number, |
| 167 | ): Promise<GitHubPullRequestFile[]> { |
| 168 | const files: GitHubPullRequestFile[] = []; |
| 169 | let page = 1; |
| 170 | const perPage = 100; |
| 171 | |
| 172 | while (true) { |
| 173 | const response = await fetch( |
| 174 | `https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/files?per_page=${perPage}&page=${page}`, |
| 175 | { |
| 176 | headers: { |
| 177 | Authorization: `Bearer ${accessToken}`, |
| 178 | Accept: "application/vnd.github.v3+json", |
| 179 | }, |
| 180 | }, |
| 181 | ); |
| 182 | |
| 183 | if (!response.ok) { |
| 184 | throw new Error(`GitHub API error: ${response.status}`); |
| 185 | } |
| 186 | |
| 187 | const data = (await response.json()) as GitHubPullRequestFile[]; |
| 188 | files.push(...data); |
| 189 | |
| 190 | if (data.length < perPage) break; |
| 191 | page++; |
| 192 | } |
| 193 | |
| 194 | return files; |
| 195 | } |