Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 34 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,45 @@
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) {
const error = yield isExistRepositoryInGitHubApps(
installationToken,
scope
);
if (error.error !== '') {
throw new Error(error.error);
}
}
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) {
return {
error: `GitHub Apps can't accessible repository (${repository})`,
};
}
return {error: ''};
});
}
run();

/***/
Expand Down
45 changes: 40 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,48 @@ 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) {
const error = await isExistRepositoryInGitHubApps(
installationToken,
scope
);
if (error.error !== '') {
throw new Error(error.error);
}
}

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<{error: string}> {
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) {
return {error: `GitHub Apps can't accessible repository (${repository})`};
}

return {error: ''};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of returning an object, let's throw an error. This will simplify the error handling logic where we call this function. The exception will get caught by https://github.com/getsentry/action-github-app-token/pull/17/files#diff-4fab5baaca5c14d2de62d8d2fceef376ddddcc8e9509d86cfa5643f51b89ce3dR65

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i fixed in f318804 !

}

run();