File tree Expand file tree Collapse file tree 2 files changed +27
-7
lines changed
Expand file tree Collapse file tree 2 files changed +27
-7
lines changed Original file line number Diff line number Diff line change @@ -88,10 +88,19 @@ export const dispatchControllerFactory: () => Promise<RequestHandler> =
8888 // If it's not present, we need to resolve the default branch.
8989 let enrichedTargetRef = body . target . ref ;
9090 if ( ! enrichedTargetRef ) {
91- enrichedTargetRef = await getRepositoryDefaultBranch ( {
92- owner : body . target . owner ,
93- repo : body . target . repo ,
94- } ) ;
91+ try {
92+ enrichedTargetRef = await getRepositoryDefaultBranch ( {
93+ owner : body . target . owner ,
94+ repo : body . target . repo ,
95+ } ) ;
96+ } catch ( e ) {
97+ _reqLogger . warn ( { error : e } , "Failed to resolve default branch" ) ;
98+
99+ return res
100+ . status ( 400 )
101+ . header ( "content-type" , responseContentType )
102+ . json ( { error : "Failed to resolve default branch" } ) ;
103+ }
95104 }
96105
97106 // Map the body to the policy input.
Original file line number Diff line number Diff line change @@ -116,10 +116,21 @@ async function resolveAccessToken(id: RepositoryIdentity): Promise<string> {
116116export async function getRepositoryDefaultBranch (
117117 id : RepositoryIdentity ,
118118) : Promise < string > {
119+ // We need to fetch the metadata of the repository, which might be private.
120+ // Therefore, we also need the access token.
121+ const token = await resolveAccessToken ( {
122+ owner : id . owner ,
123+ repo : id . repo ,
124+ } ) ;
125+
119126 try {
120- const { data } = await _baseOctokit . rest . repos . get ( {
121- owner : id . owner ,
122- repo : id . repo ,
127+ const data = await runOctokit ( token , async ( octokit ) => {
128+ const { data } = await octokit . rest . repos . get ( {
129+ owner : id . owner ,
130+ repo : id . repo ,
131+ } ) ;
132+
133+ return data ;
123134 } ) ;
124135
125136 return data . default_branch ;
You can’t perform that action at this time.
0 commit comments