Skip to content

Commit

Permalink
fix: enable PAT auth
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickHeneise committed Nov 11, 2023
1 parent 6247beb commit e46d0af
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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=
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ node_modules
/functions/\[\[path\]\].js
/public/build
.env
.env.*
keyfile.pem
build
.pnpm-debug.log
Expand Down
39 changes: 27 additions & 12 deletions src/lib/octokit.server.js
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit e46d0af

Please sign in to comment.