Blame · Line-by-line history
pulls.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.
| 9989d86 | 1 | import { fetchJSON, postJSON } from './client'; |
| 2 | ||
| 3 | export interface PullRequest { | |
| 4 | id: number; | |
| 5 | number: number; | |
| 6 | title: string; | |
| 7 | body: string | null; | |
| 8 | state: 'open' | 'closed' | 'merged'; | |
| 9 | authorUsername: string; | |
| 10 | authorAvatarUrl: string | null; | |
| 11 | baseBranch: string; | |
| 12 | headBranch: string; | |
| 13 | baseRepo: string; | |
| 14 | headRepo: string; | |
| 15 | isDraft: boolean; | |
| 16 | commentCount: number; | |
| 17 | commitCount: number; | |
| 18 | additions: number; | |
| 19 | deletions: number; | |
| 20 | changedFiles: number; | |
| 21 | mergedAt: string | null; | |
| 22 | closedAt: string | null; | |
| 23 | createdAt: string; | |
| 24 | updatedAt: string; | |
| 25 | mergedByUsername: string | null; | |
| 26 | gateStatus: 'pending' | 'passed' | 'failed' | 'none'; | |
| 27 | } | |
| 28 | ||
| 29 | export interface PrComment { | |
| 30 | id: number; | |
| 31 | body: string; | |
| 32 | authorUsername: string; | |
| 33 | authorAvatarUrl: string | null; | |
| 34 | isAiReview: boolean; | |
| 35 | filePath: string | null; | |
| 36 | lineNumber: number | null; | |
| 37 | diffHunk: string | null; | |
| 38 | createdAt: string; | |
| 39 | updatedAt: string; | |
| 40 | } | |
| 41 | ||
| 42 | export interface AiReviewSummary { | |
| 43 | summary: string; | |
| 44 | comments: PrComment[]; | |
| 45 | severity: 'info' | 'warning' | 'error'; | |
| 46 | createdAt: string; | |
| 47 | } | |
| 48 | ||
| 49 | export interface PrDiffFile { | |
| 50 | path: string; | |
| 51 | additions: number; | |
| 52 | deletions: number; | |
| 53 | patch: string; | |
| 54 | } | |
| 55 | ||
| 56 | export interface CreatePrInput { | |
| 57 | title: string; | |
| 58 | body?: string; | |
| 59 | baseBranch: string; | |
| 60 | headBranch: string; | |
| 61 | isDraft?: boolean; | |
| 62 | } | |
| 63 | ||
| 64 | export interface CreatePrCommentInput { | |
| 65 | body: string; | |
| 66 | filePath?: string; | |
| 67 | lineNumber?: number; | |
| 68 | diffHunk?: string; | |
| 69 | } | |
| 70 | ||
| 71 | /** List pull requests for a repository. */ | |
| 72 | export async function listPullRequests( | |
| 73 | owner: string, | |
| 74 | repo: string, | |
| 75 | state: 'open' | 'closed' | 'merged' | 'all' = 'open', | |
| 76 | page = 1, | |
| 77 | ): Promise<PullRequest[]> { | |
| 78 | return fetchJSON<PullRequest[]>( | |
| 79 | `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls?state=${state}&page=${page}&json=1`, | |
| 80 | ); | |
| 81 | } | |
| 82 | ||
| 83 | /** Get a single pull request. */ | |
| 84 | export async function getPullRequest( | |
| 85 | owner: string, | |
| 86 | repo: string, | |
| 87 | number: number, | |
| 88 | ): Promise<PullRequest> { | |
| 89 | return fetchJSON<PullRequest>( | |
| 90 | `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls/${number}?json=1`, | |
| 91 | ); | |
| 92 | } | |
| 93 | ||
| 94 | /** Get comments on a pull request (including AI review comments). */ | |
| 95 | export async function getPrComments( | |
| 96 | owner: string, | |
| 97 | repo: string, | |
| 98 | number: number, | |
| 99 | ): Promise<PrComment[]> { | |
| 100 | return fetchJSON<PrComment[]>( | |
| 101 | `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls/${number}/comments?json=1`, | |
| 102 | ); | |
| 103 | } | |
| 104 | ||
| 105 | /** Get the AI review summary for a pull request. */ | |
| 106 | export async function getAiReview( | |
| 107 | owner: string, | |
| 108 | repo: string, | |
| 109 | number: number, | |
| 110 | ): Promise<AiReviewSummary | null> { | |
| 111 | try { | |
| 112 | return await fetchJSON<AiReviewSummary>( | |
| 113 | `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls/${number}/ai-review?json=1`, | |
| 114 | ); | |
| 115 | } catch { | |
| 116 | return null; | |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | /** Get diff files for a pull request. */ | |
| 121 | export async function getPrDiff( | |
| 122 | owner: string, | |
| 123 | repo: string, | |
| 124 | number: number, | |
| 125 | ): Promise<PrDiffFile[]> { | |
| 126 | return fetchJSON<PrDiffFile[]>( | |
| 127 | `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls/${number}/diff?json=1`, | |
| 128 | ); | |
| 129 | } | |
| 130 | ||
| 131 | /** Create a new pull request. */ | |
| 132 | export async function createPullRequest( | |
| 133 | owner: string, | |
| 134 | repo: string, | |
| 135 | input: CreatePrInput, | |
| 136 | ): Promise<PullRequest> { | |
| 137 | return postJSON<PullRequest>( | |
| 138 | `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls`, | |
| 139 | input, | |
| 140 | ); | |
| 141 | } | |
| 142 | ||
| 143 | /** Post a comment on a pull request. */ | |
| 144 | export async function createPrComment( | |
| 145 | owner: string, | |
| 146 | repo: string, | |
| 147 | number: number, | |
| 148 | input: CreatePrCommentInput, | |
| 149 | ): Promise<PrComment> { | |
| 150 | return postJSON<PrComment>( | |
| 151 | `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls/${number}/comments`, | |
| 152 | input, | |
| 153 | ); | |
| 154 | } | |
| 155 | ||
| 156 | /** Merge a pull request. */ | |
| 157 | export async function mergePullRequest( | |
| 158 | owner: string, | |
| 159 | repo: string, | |
| 160 | number: number, | |
| 161 | mergeMethod: 'merge' | 'squash' | 'rebase' = 'merge', | |
| 162 | ): Promise<{ merged: boolean; sha: string }> { | |
| 163 | return postJSON<{ merged: boolean; sha: string }>( | |
| 164 | `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls/${number}/merge`, | |
| 165 | { mergeMethod }, | |
| 166 | ); | |
| 167 | } | |
| 168 | ||
| 169 | /** Close a pull request without merging. */ | |
| 170 | export async function closePullRequest( | |
| 171 | owner: string, | |
| 172 | repo: string, | |
| 173 | number: number, | |
| 174 | ): Promise<PullRequest> { | |
| 175 | return postJSON<PullRequest>( | |
| 176 | `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls/${number}/close`, | |
| 177 | {}, | |
| 178 | ); | |
| 179 | } | |
| 180 | ||
| 181 | /** Trigger AI review on a pull request. */ | |
| 182 | export async function requestAiReview( | |
| 183 | owner: string, | |
| 184 | repo: string, | |
| 185 | number: number, | |
| 186 | ): Promise<{ queued: boolean }> { | |
| 187 | return postJSON<{ queued: boolean }>( | |
| 188 | `/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/pulls/${number}/request-ai-review`, | |
| 189 | {}, | |
| 190 | ); | |
| 191 | } |