diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..6e35679 --- /dev/null +++ b/.env.example @@ -0,0 +1,9 @@ +GH_ORG=cyprus-developer-community +GH_GRAPHQL_URL=https://api.github.com/graphql + +# https://github.com/settings/tokens?type=beta +# Create a fine-grained token with the following permissions: +# Resource owner: cyprus-developer-community +# Repository Access: Public Repositories (read only) +# Organization permissions: Members (read only) +GH_PAT= diff --git a/.gitignore b/.gitignore index 893d066..7d772eb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,6 @@ node_modules /functions/\[\[path\]\].js /public/build .env -.env.* keyfile.pem build .pnpm-debug.log diff --git a/src/lib/octokit.server.js b/src/lib/octokit.server.js index b8c7fdf..1e32e01 100644 --- a/src/lib/octokit.server.js +++ b/src/lib/octokit.server.js @@ -1,17 +1,32 @@ import { Octokit } from '@octokit/core' import { createAppAuth } from '@octokit/auth-app' -const appOctokit = new Octokit({ - authStrategy: createAppAuth, - auth: { - appId: process.env.GH_APP_ID, - privateKey: atob(process.env.GH_PRIVATE_KEY) - } -}) +let token = '', + octokit = {} -const { token } = await appOctokit.auth({ - type: 'installation', - installationId: process.env.GH_APP_INSTALLATION_ID -}) +if (process.env.GH_PAT) { + // with personal access token (local development) + const patOctokit = new Octokit({ auth: process.env.GH_PAT }) -export { token, appOctokit as octokit } + token = process.env.GH_PAT + octokit = patOctokit +} else { + // with GitHub App (production) + const appOctokit = new Octokit({ + authStrategy: createAppAuth, + auth: { + appId: process.env.GH_APP_ID, + privateKey: atob(process.env.GH_PRIVATE_KEY) + } + }) + + const { token: appToken } = await appOctokit.auth({ + type: 'installation', + installationId: process.env.GH_APP_INSTALLATION_ID + }) + + token = appToken + octokit = appOctokit +} + +export { token, octokit }