Skip to content

Fix bug when multiple installations are available and scope is not specified #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
37 changes: 18 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
try {
const privateKey: string = core.getInput('private_key');
Expand All @@ -20,21 +16,24 @@ async function run(): Promise<void> {
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
Expand Down