Skip to content

Instantly share code, notes, and snippets.

@charlesreid1
Last active July 16, 2018 16:02
Show Gist options
  • Save charlesreid1/bfae7d3804d2a8dd88d35d88c7512818 to your computer and use it in GitHub Desktop.
Save charlesreid1/bfae7d3804d2a8dd88d35d88c7512818 to your computer and use it in GitHub Desktop.
Get a list of pending invitations to a github organization or team by calling the Github API
import requests
import os
import json
# Credentials
api_token = os.environ['GITHUB_TOKEN']
user = os.environ['GITHUB_USER']
def main():
list_pending_org_invites()
def list_pending_org_invites():
"""
List pending organization invites
"""
this_org = 'dcppc'
# Get the pending invitations for this org
url = 'https://api.github.com/orgs/%s/invitations'%(this_org)
invites = requests.get(url, auth=(user,api_token)).json()
print_invites(invites)
def list_pending_team_invites():
"""
List pending team invites
"""
this_team = 'everyone'
# Get the team ID for the "everyone" team
url = 'https://api.github.com/orgs/dcppc/teams'
teams = requests.get(url, auth=(user,api_token)).json()
this_team_id = ''
for team in teams:
if team['name']==this_team:
this_team_id = team['id']
# Get the pending invitations for this team
url = 'https://api.github.com/teams/%s/invitations'%(this_team_id)
invites = requests.get(url, auth=(user,api_token)).json()
print_invites(invites)
def print_invites(invites):
"""
Print the details returned by the invite API call
Example of full record returned by Github:
{
"id": 6616308,
"node_id": "MDIyOk9yZ2FuaXphdGlvbkludml0YXRpb242NjE2MzA4",
"login": "nikolamir",
"email": null,
"role": "direct_member",
"created_at": "2018-07-10T07:21:09.000-07:00",
"inviter": {
"login": "raynamharris",
"id": 7544197,
"node_id": "MDQ6VXNlcjc1NDQxOTc=",
"avatar_url": "https://avatars2.githubusercontent.com/u/7544197?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/raynamharris",
"html_url": "https://github.com/raynamharris",
"followers_url": "https://api.github.com/users/raynamharris/followers",
"following_url": "https://api.github.com/users/raynamharris/following{/other_user}",
"gists_url": "https://api.github.com/users/raynamharris/gists{/gist_id}",
"starred_url": "https://api.github.com/users/raynamharris/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/raynamharris/subscriptions",
"organizations_url": "https://api.github.com/users/raynamharris/orgs",
"repos_url": "https://api.github.com/users/raynamharris/repos",
"events_url": "https://api.github.com/users/raynamharris/events{/privacy}",
"received_events_url": "https://api.github.com/users/raynamharris/received_events",
"type": "User",
"site_admin": false
}
}
"""
"""
# Print full record
for invite in invites:
print(json.dumps(invite, indent=4))
# Print invited user only
for invite in invites:
print(invite['login'])
"""
# Print invited user and person who invited them
for invite in invites:
print("%s,%s"%(invite['login'],invite['inviter']['login']))
if __name__=="__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment