1
+ import pRetry from "p-retry" ;
1
2
// @ts -check
2
3
3
4
/**
@@ -75,47 +76,26 @@ export async function main(
75
76
76
77
let authentication ;
77
78
// 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
+
79
80
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
+ ) ;
85
86
} ,
87
+ retries : 3 ,
86
88
} ) ;
87
89
88
- // Get token for given repositories
89
- authentication = await auth ( {
90
- type : "installation" ,
91
- installationId : response . data . id ,
92
- repositoryNames : parsedRepositoryNames . split ( "," ) ,
93
- } ) ;
94
90
} else {
95
91
// 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
+ ) ;
101
97
} ,
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 ,
119
99
} ) ;
120
100
}
121
101
@@ -129,3 +109,51 @@ export async function main(
129
109
core . saveState ( "token" , authentication . token ) ;
130
110
}
131
111
}
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