@@ -5,6 +5,8 @@ import * as core from '@actions/core';
55
66type listInstallationsResponse =
77 Endpoints [ 'GET /app/installations' ] [ 'response' ] ;
8+ type listRepositoriesResponse =
9+ Endpoints [ 'GET /installation/repositories' ] [ 'response' ] ;
810
911async function run ( ) : Promise < void > {
1012 try {
@@ -24,8 +26,9 @@ async function run(): Promise<void> {
2426 await appOctokit . apps . listInstallations ( ) ;
2527 let installationId = installations . data [ 0 ] . id ;
2628 if ( scope !== '' ) {
29+ const loginName : string = scope . split ( '/' ) [ 0 ] ; // if scope set repository, loginName is username
2730 const scopedData = installations . data . find (
28- ( item ) => item . account ?. login === scope
31+ ( item ) => item . account ?. login === loginName
2932 ) ;
3033 if ( scopedData === undefined ) {
3134 throw new Error ( `set scope is ${ scope } , but installation is not found` ) ;
@@ -43,16 +46,48 @@ async function run(): Promise<void> {
4346 if ( ! resp ) {
4447 throw new Error ( 'Unable to authenticate' ) ;
4548 }
46-
47- // @ts -expect-error
48- core . setSecret ( resp . token ) ;
4949 // @ts -expect-error
50- core . setOutput ( 'token' , resp . token ) ;
50+ const installationToken = resp . token ;
51+
52+ // Need to check accessibility if scope set repository
53+ if ( scope !== '' && scope . split ( '/' ) . length === 2 ) {
54+ const error = await isExistRepositoryInGitHubApps (
55+ installationToken ,
56+ scope
57+ ) ;
58+ if ( error . error !== '' ) {
59+ throw new Error ( error . error ) ;
60+ }
61+ }
62+
63+ core . setSecret ( installationToken ) ;
64+ core . setOutput ( 'token' , installationToken ) ;
5165 } catch ( error ) {
5266 if ( error instanceof Error ) {
5367 core . setFailed ( error . message ) ;
5468 }
5569 }
5670}
5771
72+ async function isExistRepositoryInGitHubApps (
73+ installationToken : string ,
74+ repository : string
75+ ) : Promise < { error : string } > {
76+ const installationOctokit = new Octokit ( {
77+ auth : installationToken ,
78+ baseUrl : process . env . GITHUB_API_URL || 'https://api.github.com' ,
79+ } ) ;
80+ const accessibleRepositories : listRepositoriesResponse =
81+ await installationOctokit . apps . listReposAccessibleToInstallation ( ) ;
82+
83+ const repo = accessibleRepositories . data . repositories . find (
84+ ( item ) => item . full_name === repository
85+ ) ;
86+ if ( repo === undefined ) {
87+ return { error : `GitHub Apps can't accessible repository (${ repository } )` } ;
88+ }
89+
90+ return { error : '' } ;
91+ }
92+
5893run ( ) ;
0 commit comments