1- import { strict as assert } from "node:assert" ;
2-
31import { request as octokitRequest } from "@octokit/request" ;
4- import type { RequestInterface } from "@octokit/types" ;
52
63const DIFF_ACCEPT_HEADER = "application/vnd.github.v3.diff" ;
74
8- function resolveGitHubToken ( ) : string | undefined {
9- return process . env . GITHUB_TOKEN ?. trim ( ) ;
10- }
11-
12- const defaultHeaders : Record < string , string > = {
13- "user-agent" : "opencode-bench" ,
14- } ;
15-
16- const token = resolveGitHubToken ( ) ;
17- assert (
18- token ,
19- "GITHUB_TOKEN is required to call the GitHub API. Set it before running the CLI." ,
20- ) ;
21- defaultHeaders . authorization = `Bearer ${ token } ` ;
5+ let client : ReturnType < typeof octokitRequest . defaults > ;
226
23- const requestClient : RequestInterface = octokitRequest . defaults ( {
24- headers : defaultHeaders ,
25- } ) ;
26-
27- function getRequestClient ( ) : RequestInterface {
28- return requestClient ;
7+ function getRequestClient ( ) {
8+ if ( client ) return client ;
9+ client = octokitRequest . defaults ( {
10+ headers : {
11+ "user-agent" : "opencode-bench" ,
12+ authorization : `Bearer ${ process . env . GITHUB_TOKEN ?. trim ( ) } ` ,
13+ } ,
14+ } ) ;
15+ return client ;
2916}
3017
3118export async function fetchComparisonDiff (
3219 owner : string ,
3320 repo : string ,
3421 from : string ,
3522 to : string ,
36- ) : Promise < string > {
23+ ) {
3724 const client = getRequestClient ( ) ;
3825
3926 const response = await client (
@@ -51,10 +38,10 @@ export async function fetchComparisonDiff(
5138
5239 const diff = String ( response . data ) ;
5340
54- assert (
55- diff . trim ( ) . length > 0 ,
56- `GitHub comparison diff for ${ owner } /${ repo } between ${ from } and ${ to } was empty.` ,
57- ) ;
41+ if ( diff . trim ( ) . length === 0 )
42+ throw new Error (
43+ `GitHub comparison diff for ${ owner } /${ repo } between ${ from } and ${ to } was empty.` ,
44+ ) ;
5845
5946 return diff ;
6047}
0 commit comments