1
1
import { browser } from "$app/environment" ;
2
2
import type { components } from "@octokit/openapi-types" ;
3
3
import { parseMultiFilePatch , trimCommitHash } from "$lib/util" ;
4
- import { makeImageDetails } from "$lib/diff-viewer-multi-file.svelte" ;
4
+ import { LoadingState , makeImageDetails } from "$lib/diff-viewer-multi-file.svelte" ;
5
5
import { PUBLIC_GITHUB_APP_NAME , PUBLIC_GITHUB_CLIENT_ID } from "$env/static/public" ;
6
6
7
7
export const GITHUB_USERNAME_KEY = "github_username" ;
@@ -21,7 +21,7 @@ export type GithubDiff = {
21
21
} ;
22
22
23
23
export type GithubDiffResult = {
24
- info : GithubDiff ;
24
+ info : Promise < GithubDiff > ;
25
25
response : Promise < string > ;
26
26
} ;
27
27
@@ -112,7 +112,7 @@ export async function fetchGithubPRComparison(token: string | null, owner: strin
112
112
const base = prInfo . base . sha ;
113
113
const head = prInfo . head . sha ;
114
114
const title = `${ prInfo . title } (#${ prInfo . number } )` ;
115
- return await fetchGithubComparison ( token , owner , repo , base , head , title , prInfo . html_url ) ;
115
+ return fetchGithubComparison ( token , owner , repo , base , head , title , prInfo . html_url ) ;
116
116
}
117
117
118
118
function injectOptionalToken ( token : string | null , opts : RequestInit ) {
@@ -124,7 +124,7 @@ function injectOptionalToken(token: string | null, opts: RequestInit) {
124
124
}
125
125
}
126
126
127
- export async function fetchGithubPRInfo ( token : string | null , owner : string , repo : string , prNumber : string ) : Promise < GithubPR > {
127
+ async function fetchGithubPRInfo ( token : string | null , owner : string , repo : string , prNumber : string ) : Promise < GithubPR > {
128
128
const opts : RequestInit = {
129
129
headers : {
130
130
Accept : "application/json" ,
@@ -139,8 +139,8 @@ export async function fetchGithubPRInfo(token: string | null, owner: string, rep
139
139
}
140
140
}
141
141
142
- export function parseMultiFilePatchGithub ( details : GithubDiff , patch : string ) {
143
- return parseMultiFilePatch ( patch , ( from , to , status ) => {
142
+ export function parseMultiFilePatchGithub ( details : GithubDiff , patch : string , loadingState : LoadingState ) {
143
+ return parseMultiFilePatch ( patch , loadingState , ( from , to , status ) => {
144
144
const token = getGithubToken ( ) ;
145
145
return makeImageDetails (
146
146
from ,
@@ -152,67 +152,74 @@ export function parseMultiFilePatchGithub(details: GithubDiff, patch: string) {
152
152
} ) ;
153
153
}
154
154
155
- export async function fetchGithubComparison (
155
+ export function fetchGithubComparison (
156
156
token : string | null ,
157
157
owner : string ,
158
158
repo : string ,
159
159
base : string ,
160
160
head : string ,
161
161
description ?: string ,
162
162
url ?: string ,
163
- ) : Promise < GithubDiffResult > {
164
- const opts : RequestInit = {
165
- headers : {
166
- Accept : "application/vnd.github.v3.diff" ,
167
- } ,
163
+ ) : GithubDiffResult {
164
+ return {
165
+ info : ( async ( ) => {
166
+ if ( ! url ) {
167
+ url = `https://github.com/${ owner } /${ repo } /compare/${ base } ...${ head } ` ;
168
+ }
169
+ if ( ! description ) {
170
+ description = `Comparing ${ trimCommitHash ( base ) } ...${ trimCommitHash ( head ) } ` ;
171
+ }
172
+ return { owner, repo, base, head, description, backlink : url } ;
173
+ } ) ( ) ,
174
+ response : ( async ( ) => {
175
+ const opts : RequestInit = {
176
+ headers : {
177
+ Accept : "application/vnd.github.v3.diff" ,
178
+ } ,
179
+ } ;
180
+ injectOptionalToken ( token , opts ) ;
181
+ const response = await fetch ( `https://api.github.com/repos/${ owner } /${ repo } /compare/${ base } ...${ head } ` , opts ) ;
182
+ if ( ! response . ok ) {
183
+ throw Error ( `Failed to retrieve comparison (${ response . status } ): ${ await response . text ( ) } ` ) ;
184
+ }
185
+ return await response . text ( ) ;
186
+ } ) ( ) ,
168
187
} ;
169
- injectOptionalToken ( token , opts ) ;
170
- const response = await fetch ( `https://api.github.com/repos/${ owner } /${ repo } /compare/${ base } ...${ head } ` , opts ) ;
171
- if ( response . ok ) {
172
- if ( ! description ) {
173
- description = `Comparing ${ trimCommitHash ( base ) } ...${ trimCommitHash ( head ) } ` ;
174
- }
175
- if ( ! url ) {
176
- url = `https://github.com/${ owner } /${ repo } /compare/${ base } ...${ head } ` ;
177
- }
178
- const info = { owner, repo, base, head, description, backlink : url } ;
179
- return { response : response . text ( ) , info } ;
180
- } else {
181
- throw Error ( `Failed to retrieve comparison (${ response . status } ): ${ await response . text ( ) } ` ) ;
182
- }
183
188
}
184
189
185
- export async function fetchGithubCommitDiff ( token : string | null , owner : string , repo : string , commit : string ) : Promise < GithubDiffResult > {
186
- const diffOpts : RequestInit = {
187
- headers : {
188
- Accept : "application/vnd.github.v3.diff" ,
189
- } ,
190
- } ;
191
- injectOptionalToken ( token , diffOpts ) ;
190
+ export function fetchGithubCommitDiff ( token : string | null , owner : string , repo : string , commit : string ) : GithubDiffResult {
192
191
const url = `https://api.github.com/repos/${ owner } /${ repo } /commits/${ commit } ` ;
193
- const response = await fetch ( url , diffOpts ) ;
194
- if ( response . ok ) {
195
- const metaOpts : RequestInit = {
196
- headers : {
197
- Accept : "application/vnd.github+json" ,
198
- } ,
199
- } ;
200
- injectOptionalToken ( token , metaOpts ) ;
201
- const metaResponse = await fetch ( url , metaOpts ) ;
202
- if ( ! metaResponse . ok ) {
203
- throw Error ( `Failed to retrieve commit meta (${ metaResponse . status } ): ${ await metaResponse . text ( ) } ` ) ;
204
- }
205
- const meta : GithubCommitDetails = await metaResponse . json ( ) ;
206
- const firstParent = meta . parents [ 0 ] . sha ;
207
- const description = `${ meta . commit . message . split ( "\n" ) [ 0 ] } (${ trimCommitHash ( commit ) } )` ;
208
- const info = { owner, repo, base : firstParent , head : commit , description, backlink : meta . html_url } ;
209
- return {
210
- response : response . text ( ) ,
211
- info,
212
- } ;
213
- } else {
214
- throw Error ( `Failed to retrieve commit diff (${ response . status } ): ${ await response . text ( ) } ` ) ;
215
- }
192
+ return {
193
+ info : ( async ( ) => {
194
+ const metaOpts : RequestInit = {
195
+ headers : {
196
+ Accept : "application/vnd.github+json" ,
197
+ } ,
198
+ } ;
199
+ injectOptionalToken ( token , metaOpts ) ;
200
+ const metaResponse = await fetch ( url , metaOpts ) ;
201
+ if ( ! metaResponse . ok ) {
202
+ throw Error ( `Failed to retrieve commit meta (${ metaResponse . status } ): ${ await metaResponse . text ( ) } ` ) ;
203
+ }
204
+ const meta : GithubCommitDetails = await metaResponse . json ( ) ;
205
+ const firstParent = meta . parents [ 0 ] . sha ;
206
+ const description = `${ meta . commit . message . split ( "\n" ) [ 0 ] } (${ trimCommitHash ( commit ) } )` ;
207
+ return { owner, repo, base : firstParent , head : commit , description, backlink : meta . html_url } ;
208
+ } ) ( ) ,
209
+ response : ( async ( ) => {
210
+ const diffOpts : RequestInit = {
211
+ headers : {
212
+ Accept : "application/vnd.github.v3.diff" ,
213
+ } ,
214
+ } ;
215
+ injectOptionalToken ( token , diffOpts ) ;
216
+ const response = await fetch ( url , diffOpts ) ;
217
+ if ( ! response . ok ) {
218
+ throw Error ( `Failed to retrieve commit diff (${ response . status } ): ${ await response . text ( ) } ` ) ;
219
+ }
220
+ return await response . text ( ) ;
221
+ } ) ( ) ,
222
+ } ;
216
223
}
217
224
218
225
export async function fetchGithubFile ( token : string | null , owner : string , repo : string , path : string , ref : string ) : Promise < Blob > {
0 commit comments