Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Created April 26, 2020 21:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DavidWells/ee96ba5cb5eefa9b6004ab73e9c16ed4 to your computer and use it in GitHub Desktop.
Save DavidWells/ee96ba5cb5eefa9b6004ab73e9c16ed4 to your computer and use it in GitHub Desktop.
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