Skip to content
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

Fix bug when scope is repository #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ You will need to provide the GitHub App ID and private key. The action will then
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.APP_PRIVATE_KEY }}
scope: "octocat" # Optional, you can set "org" or "org/repo"

- name: Checkout private repo
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ inputs:
description: 'Private key for the GitHub App'
scope:
required: false
description: 'Scope of installation account'
description: 'Scope of installation account. Format: "org" or "org/repo"'
default: ''
outputs:
token:
Expand Down
31 changes: 27 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@
const installations = yield appOctokit.apps.listInstallations();
let installationId = installations.data[0].id;
if (scope !== '') {
const loginName = scope.split('/')[0]; // if scope set repository, loginName is username
const scopedData = installations.data.find((item) => {
var _a;
return (
((_a = item.account) === null || _a === void 0
? void 0
: _a.login) === scope
: _a.login) === loginName
);
});
if (scopedData === undefined) {
Expand All @@ -129,16 +130,38 @@
throw new Error('Unable to authenticate');
}
// @ts-expect-error
core.setSecret(resp.token);
// @ts-expect-error
core.setOutput('token', resp.token);
const installationToken = resp.token;
// Need to check accessibility if scope set repository
if (scope !== '' && scope.split('/').length === 2) {
yield isExistRepositoryInGitHubApps(installationToken, scope);
}
core.setSecret(installationToken);
core.setOutput('token', installationToken);
} catch (error) {
if (error instanceof Error) {
core.setFailed(error.message);
}
}
});
}
function isExistRepositoryInGitHubApps(installationToken, repository) {
return __awaiter(this, void 0, void 0, function* () {
const installationOctokit = new rest_1.Octokit({
auth: installationToken,
baseUrl: process.env.GITHUB_API_URL || 'https://api.github.com',
});
const accessibleRepositories =
yield installationOctokit.apps.listReposAccessibleToInstallation();
const repo = accessibleRepositories.data.repositories.find(
(item) => item.full_name === repository
);
if (repo === undefined) {
throw new Error(
`GitHub Apps can't accessible repository (${repository})`
);
}
});
}
run();

/***/
Expand Down
37 changes: 32 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as core from '@actions/core';

type listInstallationsResponse =
Endpoints['GET /app/installations']['response'];
type listRepositoriesResponse =
Endpoints['GET /installation/repositories']['response'];

async function run(): Promise<void> {
try {
Expand All @@ -24,8 +26,9 @@ async function run(): Promise<void> {
await appOctokit.apps.listInstallations();
let installationId = installations.data[0].id;
if (scope !== '') {
const loginName: string = scope.split('/')[0]; // if scope set repository, loginName is username
const scopedData = installations.data.find(
(item) => item.account?.login === scope
(item) => item.account?.login === loginName
);
if (scopedData === undefined) {
throw new Error(`set scope is ${scope}, but installation is not found`);
Expand All @@ -43,16 +46,40 @@ async function run(): Promise<void> {
if (!resp) {
throw new Error('Unable to authenticate');
}

// @ts-expect-error
core.setSecret(resp.token);
// @ts-expect-error
core.setOutput('token', resp.token);
const installationToken = resp.token;

// Need to check accessibility if scope set repository
if (scope !== '' && scope.split('/').length === 2) {
await isExistRepositoryInGitHubApps(installationToken, scope);
}

core.setSecret(installationToken);
core.setOutput('token', installationToken);
} catch (error) {
if (error instanceof Error) {
core.setFailed(error.message);
}
}
}

async function isExistRepositoryInGitHubApps(
installationToken: string,
repository: string
): Promise<void> {
const installationOctokit = new Octokit({
auth: installationToken,
baseUrl: process.env.GITHUB_API_URL || 'https://api.github.com',
});
const accessibleRepositories: listRepositoriesResponse =
await installationOctokit.apps.listReposAccessibleToInstallation();

const repo = accessibleRepositories.data.repositories.find(
(item) => item.full_name === repository
);
if (repo === undefined) {
throw new Error(`GitHub Apps can't accessible repository (${repository})`);
}
}

run();