@@ -26,9 +26,7 @@ export async function main(
26
26
27
27
// If neither owner nor repositories are set, default to current repository
28
28
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 ( "/" ) ;
32
30
parsedOwner = owner ;
33
31
parsedRepositoryNames = [ repo ] ;
34
32
@@ -52,7 +50,9 @@ export async function main(
52
50
parsedRepositoryNames = repositories ;
53
51
54
52
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 } ")`
56
56
) ;
57
57
}
58
58
@@ -62,7 +62,9 @@ export async function main(
62
62
parsedRepositoryNames = repositories ;
63
63
64
64
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 } "`
66
68
) ;
67
69
}
68
70
@@ -76,24 +78,38 @@ export async function main(
76
78
// If at least one repository is set, get installation ID from that repository
77
79
78
80
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
+ ) ) ;
87
100
} else {
88
101
// 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
+ ) ) ;
97
113
}
98
114
99
115
// Register the token with the runner as a secret to ensure it is masked in logs
@@ -111,23 +127,13 @@ export async function main(
111
127
}
112
128
113
129
async 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 ,
117
134
request : {
118
135
hook : auth . hook ,
119
136
} ,
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
- } ) ;
131
137
} ) ;
132
138
133
139
// Get token for for all repositories of the given installation
@@ -137,12 +143,17 @@ async function getTokenFromOwner(request, auth, parsedOwner) {
137
143
} ) ;
138
144
139
145
const installationId = response . data . id ;
140
- const appSlug = response . data [ ' app_slug' ] ;
146
+ const appSlug = response . data [ " app_slug" ] ;
141
147
142
148
return { authentication, installationId, appSlug } ;
143
149
}
144
150
145
- async function getTokenFromRepository ( request , auth , parsedOwner , parsedRepositoryNames ) {
151
+ async function getTokenFromRepository (
152
+ request ,
153
+ auth ,
154
+ parsedOwner ,
155
+ parsedRepositoryNames
156
+ ) {
146
157
// https://docs.github.com/rest/apps/apps?apiVersion=2022-11-28#get-a-repository-installation-for-the-authenticated-app
147
158
const response = await request ( "GET /repos/{owner}/{repo}/installation" , {
148
159
owner : parsedOwner ,
@@ -160,7 +171,7 @@ async function getTokenFromRepository(request, auth, parsedOwner, parsedReposito
160
171
} ) ;
161
172
162
173
const installationId = response . data . id ;
163
- const appSlug = response . data [ ' app_slug' ] ;
174
+ const appSlug = response . data [ " app_slug" ] ;
164
175
165
176
return { authentication, installationId, appSlug } ;
166
177
}
0 commit comments