-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_api_ops.py
More file actions
60 lines (54 loc) · 1.79 KB
/
github_api_ops.py
File metadata and controls
60 lines (54 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import requests
import json
from config import GITHUB_TOKEN_URL, GITHUB_API, ORG_LOGIN
def get_token(code):
body = json.dumps({
"client_id": os.environ["CLIENT_ID"],
"client_secret": os.environ["CLIENT_SECRET"],
"code": code,
"redirect_uri": os.environ["AUTH_CALLBACK_URL"]
})
headers = {
"Content-Type": "application/json",
"Accept": "application/json"
}
response = requests.request("POST", GITHUB_TOKEN_URL, headers=headers, data=body)
return response.json()
def get_user_info(token):
url = f"{GITHUB_API}/user"
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/vnd.github.v3+json"
}
response = requests.request("GET", url, headers=headers)
return response.json()
def get_user_emails(token):
url = f"{GITHUB_API}/user/emails"
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/vnd.github.v3+json"
}
response = requests.request("GET", url, headers=headers)
return response.json()
def get_team_id(pat, team_slug):
url = f"{GITHUB_API}/orgs/{ORG_LOGIN}/teams/{team_slug}"
headers = {
"Authorization": f"Bearer {pat}",
"Accept": "application/vnd.github.v3+json"
}
response = requests.request("GET", url, headers=headers)
return response.json()['id']
def invite_user(pat, user_id, teams_ids):
url = f"{GITHUB_API}/orgs/{ORG_LOGIN}/invitations"
body = json.dumps({
"invitee_id": user_id,
"team_ids": teams_ids
})
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {pat}",
"Accept": "application/vnd.github.v3+json"
}
response = requests.request("POST", url, headers=headers, data=body)
return response.json()