@@ -18,19 +18,47 @@ export async function main(
1818 createAppAuth ,
1919 request
2020) {
21+ let parsedOwner = "" ;
22+ let parsedRepositoryNames = "" ;
2123
22- let org = "" ;
23- if ( owner . length == 0 ) {
24- org = process . env . GITHUB_REPOSITORY_OWNER || "" ;
24+ // if neither owner nor repositories are set, default to current repository
25+ if ( ! owner && ! repositories ) {
26+ [ parsedOwner , parsedRepositoryNames ] = String (
27+ process . env . GITHUB_REPOSITORY
28+ ) . split ( "/" ) ;
29+
30+ core . info (
31+ `owner and repositories not set, creating token for the current repository ("${ parsedRepositoryNames } ")`
32+ ) ;
33+ }
34+
35+ // if only an owner is set, default to all repositories from that owner
36+ if ( owner && ! repositories ) {
37+ parsedOwner = owner ;
38+
39+ core . info (
40+ `repositories not set, creating token for all repositories for given owner "${ owner } "`
41+ ) ;
2542 }
2643
27- if ( owner . length == 0 && repositories . length == 0 ) {
28- repositories = process . env . GITHUB_REPOSITORY ?. split ( "/" ) [ 1 ] || "" ;
44+ // if repositories are set, but no owner, default to `GITHUB_REPOSITORY_OWNER`
45+ if ( ! owner && repositories ) {
46+ parsedOwner = String ( process . env . GITHUB_REPOSITORY_OWNER ) ;
47+ parsedRepositoryNames = repositories ;
48+
49+ core . info (
50+ `owner not set, creating owner for given repositories "${ repositories } " in current owner ("${ parsedOwner } ")`
51+ ) ;
2952 }
3053
31- let repos = [ ] ;
32- if ( repositories . trim ( ) != "" ) {
33- repos = repositories . split ( "," ) . map ( ( repo ) => repo . trim ( ) ) ;
54+ // if both owner and repositories are set, use those values
55+ if ( owner && repositories ) {
56+ parsedOwner = owner ;
57+ parsedRepositoryNames = repositories ;
58+
59+ core . info (
60+ `owner and repositories set, creating token for repositories "${ repositories } " owned by "${ owner } "`
61+ ) ;
3462 }
3563
3664 const auth = createAppAuth ( {
@@ -43,39 +71,54 @@ export async function main(
4371 type : "app" ,
4472 } ) ;
4573
46- // Get the installation ID
74+ let authentication ;
75+ // If at least one repository is set, get installation ID from that repository
4776 // https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#get-a-repository-installation-for-the-authenticated-app
48- const { data : installation } = await request (
49- "GET /orgs/{org}/ installation" ,
50- {
51- org ,
77+ if ( parsedRepositoryNames ) {
78+ const response = await request ( "GET /repos/{owner}/{repo}/ installation" , {
79+ owner : parsedOwner ,
80+ repo : parsedRepositoryNames . split ( "," ) [ 0 ] ,
5281 headers : {
5382 authorization : `bearer ${ appAuthentication . token } ` ,
5483 } ,
55- }
56- ) ;
57-
58- // Create a new installation token
59- let authentication ;
84+ } ) ;
6085
61- if ( repositories . length == 0 ) {
86+ // get token for given repositories
6287 authentication = await auth ( {
6388 type : "installation" ,
64- installationId : installation . id ,
89+ installationId : response . data . id ,
90+ repositoryNames : parsedRepositoryNames . split ( "," ) ,
6591 } ) ;
6692 } else {
93+ // otherwise get the installation for the owner which can either be an organization or a user account
94+ // https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#get-a-repository-installation-for-the-authenticated-app
95+ const response = await request ( "GET /orgs/{org}/installation" , {
96+ org : parsedOwner ,
97+ headers : {
98+ authorization : `bearer ${ appAuthentication . token } ` ,
99+ } ,
100+ } ) . catch ( ( error ) => {
101+ if ( error . status !== 404 ) throw error ;
102+
103+ // https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#get-a-user-installation-for-the-authenticated-app
104+ return request ( "GET /users/{username}/installation" , {
105+ username : parsedOwner ,
106+ headers : {
107+ authorization : `bearer ${ appAuthentication . token } ` ,
108+ } ,
109+ } ) ;
110+ } ) ;
111+
112+ // get token for for all repositories of the given installation
67113 authentication = await auth ( {
68114 type : "installation" ,
69- installationId : installation . id ,
70- repositoryNames : repos ,
115+ installationId : response . data . id ,
71116 } ) ;
72117 }
73-
74- // Register the token with the runner as a secret to ensure it is masked in logs
75- core . setSecret ( authentication . token ) ;
76118
77119 core . setOutput ( "token" , authentication . token ) ;
78-
120+ // Register the token with the runner as a secret to ensure it is masked in logs
121+ core . setSecret ( authentication . token ) ;
79122 // Make token accessible to post function (so we can invalidate it)
80123 core . saveState ( "token" , authentication . token ) ;
81124}
0 commit comments