Skip to content

Commit 00d8d13

Browse files
committed
Add Javascript code to obtain a GitHub App access token
In order to act as a GitHub App, we need to obtain a so-called "installation access token", which is like a Personal Access Token for GitHub Apps, but scoped to a specific repository. To that end, we need to figure out the "installation ID" of the GitHub App on that specific repository. Here are two support functions to make this happen, both making use of the previously-introduced function to perform GitHub API requests on behalf of a GitHub App. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 92d02f1 commit 00d8d13

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

get-app-installation-id.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = async (context, appId, privateKey, owner, repo) => {
2+
const gitHubApiRequestAsApp = require('./github-api-request-as-app')
3+
const answer = await gitHubApiRequestAsApp(
4+
context,
5+
appId,
6+
privateKey,
7+
'GET',
8+
`/repos/${owner}/${repo}/installation`)
9+
if (answer.error) throw answer.error
10+
if (answer.id) return answer.id
11+
throw new Error(`Unhandled response:\n${JSON.stringify(answer, null, 2)}`)
12+
}

get-installation-access-token.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = async (context, appId, privateKey, installation_id) => {
2+
const gitHubApiRequestAsApp = require('./github-api-request-as-app')
3+
const answer = await gitHubApiRequestAsApp(
4+
context,
5+
appId,
6+
privateKey,
7+
'POST',
8+
`/app/installations/${installation_id}/access_tokens`)
9+
if (answer.error) throw answer.error
10+
if (answer.token) return answer.token
11+
throw new Error(`Unhandled response:\n${JSON.stringify(answer, null, 2)}`)
12+
}

0 commit comments

Comments
 (0)