-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
60 lines (52 loc) · 2.1 KB
/
utils.py
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 requests
def get_repos(github_api,username,token):
"""This function is used to list github repositories for an authenticated user.
Args:
github_api (string): the github api url
username (string): your github username
token (string): your github personal access token
Returns:
dict: a collection of repositories {id:repository_name}
"""
#the api link to list the repositories
repo_url=github_api+"/user/repos"
#get the repositories of the user with its credentials
repos = requests.get(repo_url, auth=(username,token))
#initialize a dictonary to store the repositories name
repos_dict={}
for i,x in enumerate(repos.json(),1):
#store each repository name with an id number
repos_dict.update({i:x['name']})
return repos_dict
def show_repos(repos_dict):
"""A function to show the list of repositories
Args:
repos_dict (dict): a collection of repositories {id:repository_name}
"""
#print the values so that you can choose the repositories to delete
print("List of repositories:")
for key,value in repos_dict.items():
print("{}- {}".format(key,value))
def create_repo(github_api,repo_name,is_public,username,token):
"""A function to create a github repository from the local environment
Args:
github_api (string): the github api url
repo_name (string): the name of the repository you want to create
is_public (bool): the paramter to set your repository public or private
username (string): your github username
token (string): your github personal access token
Raises:
SystemExit: an exception to raise
Returns:
integer: it's a request response
"""
if is_public:
data = '{"name": "' + repo_name + '", "public": true }'
else:
data = '{"name": "' + repo_name + '", "public": false }'
try:
r = requests.post(github_api + "/user/repos", data=data, auth=(username,token))
r.raise_for_status()
except requests.exceptions.RequestException as err:
raise SystemExit(err)
return r