Skip to content

Instantly share code, notes, and snippets.

@codethejason
Last active January 7, 2016 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codethejason/c2b2d71814752eb10d4d to your computer and use it in GitHub Desktop.
Save codethejason/c2b2d71814752eb10d4d to your computer and use it in GitHub Desktop.
Check Membership of User on Github Teams
var https = require('https');
//token from your account to access the GitHub API
var token = "";
var checking = {
teamID: 1163900, //GCI Students Team for Fossasia
user: ''
}
//options for the https request
var options = {
method: 'GET',
hostname: 'api.github.com',
path: '/teams/'+checking.teamID+'/memberships/'+checking.user,
headers: {
'Authorization': 'token '+token,
'User-Agent': 'Mozilla/5.0'
}
};
//https request
var req = https.request(options, function(res) {
var body = '';
//add the small chunks that node receives to the body variable
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
//get the JSON from the returned body string
var json = JSON.parse(body);
if(res.statusCode == 200) {
if(json.state == 'active') {
console.log("This person is a member of the group.");
} else {
console.log("This person is not in the group.");
}
} else {
console.log("You don't have permissions to check for membership.")
}
});
});
req.end();
@roonyh
Copy link

roonyh commented Jan 7, 2016

This is very good. I tested it for students team and works perfectly!

Don't do if(json.message != "You must be an organization owner or team maintainer to get a team membership.")

Those messages can change without any warning.

Instead you can do if(res.statusCode==200). 200 is http status for OK as you definitely know. And its a convention that can safely be assumed won't change.

@codethejason
Copy link
Author

@roonyh Thanks for the tip!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment