diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/action-github-app-token.iml b/.idea/action-github-app-token.iml new file mode 100644 index 0000000..24643cc --- /dev/null +++ b/.idea/action-github-app-token.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..89930e4 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..0a282d3 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/action.yml b/action.yml index 8da93fb..850c7dd 100644 --- a/action.yml +++ b/action.yml @@ -10,8 +10,8 @@ inputs: description: 'Private key for the GitHub App' scope: required: false - description: 'Scope of installation account' - default: '' + description: 'Scope of installation account, defaults to the owner of the repository where the workflow is executing' + default: ${{ github.repository_owner }} outputs: token: description: 'Github Token for App installation' diff --git a/src/main.ts b/src/main.ts index c555579..35aeafa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,11 +1,7 @@ import {createAppAuth} from '@octokit/auth-app'; import {Octokit} from '@octokit/rest'; -import {Endpoints} from '@octokit/types'; import * as core from '@actions/core'; -type listInstallationsResponse = - Endpoints['GET /app/installations']['response']; - async function run(): Promise { try { const privateKey: string = core.getInput('private_key'); @@ -20,21 +16,24 @@ async function run(): Promise { baseUrl: process.env.GITHUB_API_URL || 'https://api.github.com', }); - const installations: listInstallationsResponse = - await appOctokit.apps.listInstallations(); - let installationId = installations.data[0].id; - if (scope !== '') { - const scopedData = installations.data.find( - (item) => - item.account && - 'login' in item.account && - item.account?.login === scope - ); - if (scopedData === undefined) { - throw new Error(`set scope is ${scope}, but installation is not found`); - } - installationId = scopedData.id; - } + const response = await appOctokit.apps + .getOrgInstallation({org: scope}) + .catch(async (error) => { + if (error.status === 404) { + return await appOctokit.apps + .getUserInstallation({username: scope}) + .catch((nestederror) => { + if (nestederror.status === 404) { + throw new Error( + `set scope is ${scope}, but installation is not found` + ); + } + throw nestederror; + }); + } + throw error; + }); + const installationId = response.data.id; // This is untyped // See: https://github.com/octokit/core.js/blob/master/src/index.ts#L182-L183