Skip to content

Instantly share code, notes, and snippets.

@intrepidmatt
Last active February 1, 2016 22:11
Show Gist options
  • Select an option

  • Save intrepidmatt/658b3ab7f9f5cb63e514 to your computer and use it in GitHub Desktop.

Select an option

Save intrepidmatt/658b3ab7f9f5cb63e514 to your computer and use it in GitHub Desktop.
Node script to add all members of a GitHub organization to a particular team.
var request = require('request');
var async = require('async');
var ghUsername = process.env.GH_USERNAME;
var ghToken = process.env.GH_TOKEN;
var ghOrgname = process.env.GH_ORGNAME;
var ghTeamId = process.env.GH_TEAMID;
function githubRequest(apiEndpoint, method, callback) {
request({
url: 'https://api.github.com' + apiEndpoint,
method: method,
headers: {
'User-Agent': ghUsername,
'Authorization': 'token ' + ghToken
},
}, function(error, response, body) {
callback(error, response, body);
});
}
githubRequest('/orgs/' + ghOrgname + '/members?per_page=100', 'GET', function(error, response, body) {
var users = JSON.parse(body);
async.eachSeries(users, function(user, next) {
console.log('Adding user: ' + user.login);
githubRequest('/teams/' + ghTeamId + '/memberships/' + user.login, 'PUT', function(error, response, body) {
if (error) {
console.log('Error: ' + error);
} else {
console.log('Success.');
}
next();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment