Skip to content

Instantly share code, notes, and snippets.

@p00j4
Created May 2, 2017 07:22
Show Gist options
  • Save p00j4/18be94b7261ff564d13241d0899f7101 to your computer and use it in GitHub Desktop.
Save p00j4/18be94b7261ff564d13241d0899f7101 to your computer and use it in GitHub Desktop.
Get Users from Github and Slack team
"""
A sample to get github users and slack sers of provided team tokens
prived the detaisl where "GIVE" is written & run this file it will get you both the lists from there copy and create a dict of GITHub & Slack name sof that user manually
(Note: it would have completely automated based on mail id but there are some privacy settings which doesn't reveal all user's mail id so this is the best can do in current context, however if you are sure all your github users have public email id then you just need to write 1 more mapper to get final dict 100% automatically)
"""
import requests
import simplejson as json
GIT_TOKEN = "<GIVE_YOUR_GIT_TOKEN>"
SLACK_TOKEN = "<GIVE_YOUR_SLACK_TOKEN>"
organisation = "<GIVE_YOUR_GITHUB_ORGANISATION_NAME>"
SLACK_USER_LIST = "https://slack.com/api/users.list?token="
API_GITHUB_USERS = "https://api.github.com/users/"
API_GITHUB_MEMBERS_LIST = "https://api.github.com/orgs/{org}/members?page="
def getGithubUsers():
if git_mappings:
return git_mappings
users = []
page = 1
while True:
member_api = "%s%s" % (API_GITHUB_MEMBERS_LIST.format(org=organisation), page)
response = requests.get(member_api, headers={"Authorization": "token " + GIT_TOKEN})
if not response:
break
users += json.loads(response["content"])
page += 1
git_users = []
for item in users:
user_name = item["login"]
git_users.append(user_name)
git_mappings[user_name] = user_name #useful for 100% automatic solution
print git_users
return git_users
def getSlackUsers():
if slack_mappings:
return slack_mappings
response = requests.get(SLACK_USER_LIST + SLACK_TOKEN, headers={})
users = json.loads(response["content"])
slack_users={}
for item in users["members"]:
slack_users.append(item["name"])
slack_mappings[item["name"]] = item["name"] #useful for 100% automatic solution
print slack_users
return slack_users
print "******************* Github Users *********************"
getGithubUsers()
print "******************* Slack Users *********************"
getSlackUsers()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment