11import { generateGitHubAppJWT } from './jwt' ;
22import type { Env , FeedbackAttachment , GitHubIssue } from '../types' ;
33
4- const GITHUB_API = 'https://api.github.com' ;
4+ export const GITHUB_API = 'https://api.github.com' ;
55
66/**
77 * Thrown when GitHub rejects an issue creation specifically due to invalid
@@ -18,7 +18,7 @@ export class GitHubLabelError extends Error {
1818 }
1919}
2020
21- const headers = ( token : string ) => ( {
21+ export const githubHeaders = ( token : string ) => ( {
2222 Authorization : `Bearer ${ token } ` ,
2323 Accept : 'application/vnd.github+json' ,
2424 'Content-Type' : 'application/json' ,
@@ -38,7 +38,7 @@ async function getInstallationId(env: Env, owner: string, repo: string): Promise
3838 const jwt = await generateGitHubAppJWT ( env . GITHUB_APP_ID , env . GITHUB_PRIVATE_KEY ) ;
3939
4040 const response = await fetch ( `${ GITHUB_API } /repos/${ owner } /${ repo } /installation` , {
41- headers : headers ( jwt ) ,
41+ headers : githubHeaders ( jwt ) ,
4242 } ) ;
4343
4444 if ( ! response . ok ) {
@@ -65,7 +65,7 @@ export async function getInstallationToken(
6565
6666 const response = await fetch ( `${ GITHUB_API } /app/installations/${ installationId } /access_tokens` , {
6767 method : 'POST' ,
68- headers : headers ( jwt ) ,
68+ headers : githubHeaders ( jwt ) ,
6969 } ) ;
7070
7171 if ( ! response . ok ) {
@@ -90,7 +90,7 @@ export async function createIssue(
9090) : Promise < GitHubIssue > {
9191 const response = await fetch ( `${ GITHUB_API } /repos/${ owner } /${ repo } /issues` , {
9292 method : 'POST' ,
93- headers : headers ( token ) ,
93+ headers : githubHeaders ( token ) ,
9494 body : JSON . stringify ( { title, body, labels } ) ,
9595 } ) ;
9696
@@ -128,7 +128,7 @@ function isLabelValidationFailure(body: string): boolean {
128128export async function isRepoPublic ( token : string , owner : string , repo : string ) : Promise < boolean > {
129129 try {
130130 const response = await fetch ( `${ GITHUB_API } /repos/${ owner } /${ repo } ` , {
131- headers : headers ( token ) ,
131+ headers : githubHeaders ( token ) ,
132132 } ) ;
133133
134134 if ( ! response . ok ) {
@@ -151,7 +151,7 @@ async function ensureScreenshotBranch(token: string, owner: string, repo: string
151151 // Check if branch already exists
152152 const check = await fetch (
153153 `${ GITHUB_API } /repos/${ owner } /${ repo } /git/ref/heads/${ SCREENSHOT_BRANCH } ` ,
154- { headers : headers ( token ) }
154+ { headers : githubHeaders ( token ) }
155155 ) ;
156156 if ( check . ok ) return ;
157157 if ( check . status !== 404 ) {
@@ -160,7 +160,7 @@ async function ensureScreenshotBranch(token: string, owner: string, repo: string
160160
161161 // Get default branch SHA
162162 const repoRes = await fetch ( `${ GITHUB_API } /repos/${ owner } /${ repo } ` , {
163- headers : headers ( token ) ,
163+ headers : githubHeaders ( token ) ,
164164 } ) ;
165165 if ( ! repoRes . ok ) {
166166 throw new Error ( `Failed to get repo info: ${ repoRes . status } ` ) ;
@@ -169,7 +169,7 @@ async function ensureScreenshotBranch(token: string, owner: string, repo: string
169169
170170 const refRes = await fetch (
171171 `${ GITHUB_API } /repos/${ owner } /${ repo } /git/ref/heads/${ repoData . default_branch } ` ,
172- { headers : headers ( token ) }
172+ { headers : githubHeaders ( token ) }
173173 ) ;
174174 if ( ! refRes . ok ) {
175175 throw new Error ( `Failed to get default branch ref: ${ refRes . status } ` ) ;
@@ -179,7 +179,7 @@ async function ensureScreenshotBranch(token: string, owner: string, repo: string
179179 // Create the screenshot branch
180180 const createRes = await fetch ( `${ GITHUB_API } /repos/${ owner } /${ repo } /git/refs` , {
181181 method : 'POST' ,
182- headers : headers ( token ) ,
182+ headers : githubHeaders ( token ) ,
183183 body : JSON . stringify ( {
184184 ref : `refs/heads/${ SCREENSHOT_BRANCH } ` ,
185185 sha : refData . object . sha ,
@@ -190,7 +190,7 @@ async function ensureScreenshotBranch(token: string, owner: string, repo: string
190190 if ( createRes . status === 422 && isExistingReferenceError ( error ) ) {
191191 const recheck = await fetch (
192192 `${ GITHUB_API } /repos/${ owner } /${ repo } /git/ref/heads/${ SCREENSHOT_BRANCH } ` ,
193- { headers : headers ( token ) }
193+ { headers : githubHeaders ( token ) }
194194 ) ;
195195 if ( recheck . ok ) return ;
196196 }
@@ -271,7 +271,7 @@ async function uploadBase64Asset(
271271
272272 const response = await fetch ( `${ GITHUB_API } /repos/${ owner } /${ repo } /contents/${ filename } ` , {
273273 method : 'PUT' ,
274- headers : headers ( token ) ,
274+ headers : githubHeaders ( token ) ,
275275 body : JSON . stringify ( {
276276 message,
277277 content : content ,
0 commit comments