@@ -26,9 +26,7 @@ export async function main(
2626
2727 // If neither owner nor repositories are set, default to current repository
2828 if ( ! owner && repositories . length === 0 ) {
29- const [ owner , repo ] = String (
30- process . env . GITHUB_REPOSITORY
31- ) . split ( "/" ) ;
29+ const [ owner , repo ] = String ( process . env . GITHUB_REPOSITORY ) . split ( "/" ) ;
3230 parsedOwner = owner ;
3331 parsedRepositoryNames = [ repo ] ;
3432
@@ -52,7 +50,9 @@ export async function main(
5250 parsedRepositoryNames = repositories ;
5351
5452 core . info (
55- `owner not set, creating owner for given repositories "${ repositories . join ( ',' ) } " in current owner ("${ parsedOwner } ")`
53+ `owner not set, creating owner for given repositories "${ repositories . join (
54+ ","
55+ ) } " in current owner ("${ parsedOwner } ")`
5656 ) ;
5757 }
5858
@@ -62,7 +62,9 @@ export async function main(
6262 parsedRepositoryNames = repositories ;
6363
6464 core . info (
65- `owner and repositories set, creating token for repositories "${ repositories . join ( ',' ) } " owned by "${ owner } "`
65+ `owner and repositories set, creating token for repositories "${ repositories . join (
66+ ","
67+ ) } " owned by "${ owner } "`
6668 ) ;
6769 }
6870
@@ -76,24 +78,38 @@ export async function main(
7678 // If at least one repository is set, get installation ID from that repository
7779
7880 if ( parsedRepositoryNames . length > 0 ) {
79- ( { authentication, installationId, appSlug } = await pRetry ( ( ) => getTokenFromRepository ( request , auth , parsedOwner , parsedRepositoryNames ) , {
80- onFailedAttempt : ( error ) => {
81- core . info (
82- `Failed to create token for "${ parsedRepositoryNames . join ( ',' ) } " (attempt ${ error . attemptNumber } ): ${ error . message } `
83- ) ;
84- } ,
85- retries : 3 ,
86- } ) ) ;
81+ ( { authentication, installationId, appSlug } = await pRetry (
82+ ( ) =>
83+ getTokenFromRepository (
84+ request ,
85+ auth ,
86+ parsedOwner ,
87+ parsedRepositoryNames
88+ ) ,
89+ {
90+ onFailedAttempt : ( error ) => {
91+ core . info (
92+ `Failed to create token for "${ parsedRepositoryNames . join (
93+ ","
94+ ) } " (attempt ${ error . attemptNumber } ): ${ error . message } `
95+ ) ;
96+ } ,
97+ retries : 3 ,
98+ }
99+ ) ) ;
87100 } else {
88101 // Otherwise get the installation for the owner, which can either be an organization or a user account
89- ( { authentication, installationId, appSlug } = await pRetry ( ( ) => getTokenFromOwner ( request , auth , parsedOwner ) , {
90- onFailedAttempt : ( error ) => {
91- core . info (
92- `Failed to create token for "${ parsedOwner } " (attempt ${ error . attemptNumber } ): ${ error . message } `
93- ) ;
94- } ,
95- retries : 3 ,
96- } ) ) ;
102+ ( { authentication, installationId, appSlug } = await pRetry (
103+ ( ) => getTokenFromOwner ( request , auth , parsedOwner ) ,
104+ {
105+ onFailedAttempt : ( error ) => {
106+ core . info (
107+ `Failed to create token for "${ parsedOwner } " (attempt ${ error . attemptNumber } ): ${ error . message } `
108+ ) ;
109+ } ,
110+ retries : 3 ,
111+ }
112+ ) ) ;
97113 }
98114
99115 // Register the token with the runner as a secret to ensure it is masked in logs
@@ -111,23 +127,13 @@ export async function main(
111127}
112128
113129async function getTokenFromOwner ( request , auth , 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 ,
130+ // https://docs.github.com/rest/apps/apps?apiVersion=2022-11-28#get-a-user-installation-for-the-authenticated-app
131+ // This endpoint works for both users and organizations
132+ const response = await request ( "GET /users/{username}/installation" , {
133+ username : parsedOwner ,
117134 request : {
118135 hook : auth . hook ,
119136 } ,
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- request : {
128- hook : auth . hook ,
129- } ,
130- } ) ;
131137 } ) ;
132138
133139 // Get token for for all repositories of the given installation
@@ -137,12 +143,17 @@ async function getTokenFromOwner(request, auth, parsedOwner) {
137143 } ) ;
138144
139145 const installationId = response . data . id ;
140- const appSlug = response . data [ ' app_slug' ] ;
146+ const appSlug = response . data [ " app_slug" ] ;
141147
142148 return { authentication, installationId, appSlug } ;
143149}
144150
145- async function getTokenFromRepository ( request , auth , parsedOwner , parsedRepositoryNames ) {
151+ async function getTokenFromRepository (
152+ request ,
153+ auth ,
154+ parsedOwner ,
155+ parsedRepositoryNames
156+ ) {
146157 // https://docs.github.com/rest/apps/apps?apiVersion=2022-11-28#get-a-repository-installation-for-the-authenticated-app
147158 const response = await request ( "GET /repos/{owner}/{repo}/installation" , {
148159 owner : parsedOwner ,
@@ -160,7 +171,7 @@ async function getTokenFromRepository(request, auth, parsedOwner, parsedReposito
160171 } ) ;
161172
162173 const installationId = response . data . id ;
163- const appSlug = response . data [ ' app_slug' ] ;
174+ const appSlug = response . data [ " app_slug" ] ;
164175
165176 return { authentication, installationId, appSlug } ;
166177}
0 commit comments