Skip to content

Instantly share code, notes, and snippets.

@magicien
Created September 13, 2017 21:02
Show Gist options
  • Save magicien/ad3abe8e570fb6f8c583af9bf6c7e972 to your computer and use it in GitHub Desktop.
Save magicien/ad3abe8e570fb6f8c583af9bf6c7e972 to your computer and use it in GitHub Desktop.
const jwt = require('jsonwebtoken')
const fetch = require('node-fetch')
const appId = process.env.GITHUB_APP_ID
const privateKey = process.env.GITHUB_APP_PRIVATE_KEY
const getRepositories = (token) => {
return fetch('https://api.github.com/installation/repositories', {
headers: {
Authorization: `token ${token}`,
Accept: 'application/vnd.github.machine-man-preview+json'
}
})
.then(res => res.json())
}
const getInstallationRepos = (installationId) => {
const tokenUrl = `https://api.github.com/installations/${installationId}/access_tokens`
const token = jwt.sign({}, privateKey, {
algorithm: 'RS256',
expiresIn: (60 * 10), // 10min
issuer: appId
})
return fetch(tokenUrl, {
method: 'POST',
headers: {Authorization:`Bearer ${token}`, Accept:'application/vnd.github.machine-man-preview+json'}
})
.then(res => res.json())
.then(json => getRepositories(json.token))
.catch(err => console.error(err))
}
getInstallationRepos('123456789')
.then(repos => {
console.log(repos)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment