11import { browser } from "$app/environment" ;
22import type { components } from "@octokit/openapi-types" ;
33import { 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" ;
55import { PUBLIC_GITHUB_APP_NAME , PUBLIC_GITHUB_CLIENT_ID } from "$env/static/public" ;
66
77export const GITHUB_USERNAME_KEY = "github_username" ;
@@ -21,7 +21,7 @@ export type GithubDiff = {
2121} ;
2222
2323export type GithubDiffResult = {
24- info : GithubDiff ;
24+ info : Promise < GithubDiff > ;
2525 response : Promise < string > ;
2626} ;
2727
@@ -112,7 +112,7 @@ export async function fetchGithubPRComparison(token: string | null, owner: strin
112112 const base = prInfo . base . sha ;
113113 const head = prInfo . head . sha ;
114114 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 ) ;
116116}
117117
118118function injectOptionalToken ( token : string | null , opts : RequestInit ) {
@@ -124,7 +124,7 @@ function injectOptionalToken(token: string | null, opts: RequestInit) {
124124 }
125125}
126126
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 > {
128128 const opts : RequestInit = {
129129 headers : {
130130 Accept : "application/json" ,
@@ -139,8 +139,8 @@ export async function fetchGithubPRInfo(token: string | null, owner: string, rep
139139 }
140140}
141141
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 ) => {
144144 const token = getGithubToken ( ) ;
145145 return makeImageDetails (
146146 from ,
@@ -152,67 +152,74 @@ export function parseMultiFilePatchGithub(details: GithubDiff, patch: string) {
152152 } ) ;
153153}
154154
155- export async function fetchGithubComparison (
155+ export function fetchGithubComparison (
156156 token : string | null ,
157157 owner : string ,
158158 repo : string ,
159159 base : string ,
160160 head : string ,
161161 description ?: string ,
162162 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+ } ) ( ) ,
168187 } ;
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- }
183188}
184189
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 {
192191 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+ } ;
216223}
217224
218225export async function fetchGithubFile ( token : string | null , owner : string , repo : string , path : string , ref : string ) : Promise < Blob > {
0 commit comments