Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add GitHub user to private GitHub repo utility function
const axios = require('axios')
const GITHUB_REPO = process.env.GITHUB_REPO
const GITHUB_API_TOKEN = process.env.GITHUB_API_TOKEN
const GITHUB_USERNAME = process.env.GITHUB_USERNAME
module.exports = function addUserToGithubRepo(username) {
const githubEndpoint = `https://api.github.com/repos/${GITHUB_REPO}/collaborators/${username}`
const config = {
'headers': {
'User-Agent': GITHUB_USERNAME,
'Authorization': `token ${GITHUB_API_TOKEN}`
}
}
return axios.put(githubEndpoint, {
"permission": "pull"
}, config).then(function(response) {
if (response.status === 204 || response.status === 201) {
console.log(`${username} added`)
return true
}
console.log(`${username} added ad collaborator`, response)
return false
}).catch((e) => {
console.log(e)
return e
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment