1+ import pRetry from "p-retry" ;
12// @ts -check
23
34/**
@@ -75,47 +76,26 @@ export async function main(
7576
7677 let authentication ;
7778 // If at least one repository is set, get installation ID from that repository
78- // https://docs.github.com/rest/apps/apps?apiVersion=2022-11-28#get-a-repository-installation-for-the-authenticated-app
79+
7980 if ( parsedRepositoryNames ) {
80- const response = await request ( "GET /repos/{owner}/{repo}/installation" , {
81- owner : parsedOwner ,
82- repo : parsedRepositoryNames . split ( "," ) [ 0 ] ,
83- headers : {
84- authorization : `bearer ${ appAuthentication . token } ` ,
81+ authentication = await pRetry ( ( ) => getTokenFromRepository ( request , auth , parsedOwner , appAuthentication , parsedRepositoryNames ) , {
82+ onFailedAttempt : ( error ) => {
83+ core . info (
84+ `Failed to create token for " ${ parsedRepositoryNames } " (attempt ${ error . attemptNumber } ): ${ error . message } `
85+ ) ;
8586 } ,
87+ retries : 3 ,
8688 } ) ;
8789
88- // Get token for given repositories
89- authentication = await auth ( {
90- type : "installation" ,
91- installationId : response . data . id ,
92- repositoryNames : parsedRepositoryNames . split ( "," ) ,
93- } ) ;
9490 } else {
9591 // Otherwise get the installation for the owner, which can either be an organization or a user account
96- // https://docs.github.com/rest/apps/apps?apiVersion=2022-11-28#get-a-repository-installation-for-the-authenticated-app
97- const response = await request ( "GET /orgs/{org}/installation" , {
98- org : parsedOwner ,
99- headers : {
100- authorization : `bearer ${ appAuthentication . token } ` ,
92+ authentication = await pRetry ( ( ) => getTokenFromOwner ( request , auth , appAuthentication , parsedOwner ) , {
93+ onFailedAttempt : ( error ) => {
94+ core . info (
95+ `Failed to create token for " ${ parsedOwner } " (attempt ${ error . attemptNumber } ): ${ error . message } `
96+ ) ;
10197 } ,
102- } ) . catch ( ( error ) => {
103- /* c8 ignore next */
104- if ( error . status !== 404 ) throw error ;
105-
106- // https://docs.github.com/rest/apps/apps?apiVersion=2022-11-28#get-a-user-installation-for-the-authenticated-app
107- return request ( "GET /users/{username}/installation" , {
108- username : parsedOwner ,
109- headers : {
110- authorization : `bearer ${ appAuthentication . token } ` ,
111- } ,
112- } ) ;
113- } ) ;
114-
115- // Get token for for all repositories of the given installation
116- authentication = await auth ( {
117- type : "installation" ,
118- installationId : response . data . id ,
98+ retries : 3 ,
11999 } ) ;
120100 }
121101
@@ -129,3 +109,51 @@ export async function main(
129109 core . saveState ( "token" , authentication . token ) ;
130110 }
131111}
112+
113+ async function getTokenFromOwner ( request , auth , appAuthentication , parsedOwner ) {
114+ // https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#get-an-organization-installation-for-the-authenticated-app
115+ const response = await request ( "GET /orgs/{org}/installation" , {
116+ org : parsedOwner ,
117+ headers : {
118+ authorization : `bearer ${ appAuthentication . token } ` ,
119+ } ,
120+ } ) . catch ( ( error ) => {
121+ /* c8 ignore next */
122+ if ( error . status !== 404 ) throw error ;
123+
124+ // https://docs.github.com/rest/apps/apps?apiVersion=2022-11-28#get-a-user-installation-for-the-authenticated-app
125+ return request ( "GET /users/{username}/installation" , {
126+ username : parsedOwner ,
127+ headers : {
128+ authorization : `bearer ${ appAuthentication . token } ` ,
129+ } ,
130+ } ) ;
131+ } ) ;
132+
133+ // Get token for for all repositories of the given installation
134+ const authentication = await auth ( {
135+ type : "installation" ,
136+ installationId : response . data . id ,
137+ } ) ;
138+ return authentication ;
139+ }
140+
141+ async function getTokenFromRepository ( request , auth , parsedOwner , appAuthentication , parsedRepositoryNames ) {
142+ // https://docs.github.com/rest/apps/apps?apiVersion=2022-11-28#get-a-repository-installation-for-the-authenticated-app
143+ const response = await request ( "GET /repos/{owner}/{repo}/installation" , {
144+ owner : parsedOwner ,
145+ repo : parsedRepositoryNames . split ( "," ) [ 0 ] ,
146+ headers : {
147+ authorization : `bearer ${ appAuthentication . token } ` ,
148+ } ,
149+ } ) ;
150+
151+ // Get token for given repositories
152+ const authentication = await auth ( {
153+ type : "installation" ,
154+ installationId : response . data . id ,
155+ repositoryNames : parsedRepositoryNames . split ( "," ) ,
156+ } ) ;
157+
158+ return authentication ;
159+ }
0 commit comments