Last active
January 7, 2016 16:08
-
-
Save codethejason/c2b2d71814752eb10d4d to your computer and use it in GitHub Desktop.
Check Membership of User on Github Teams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 forOK
as you definitely know. And its a convention that can safely be assumed won't change.