forked from Kenmakhanu/100daysofdevops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_del_list_arg.py
More file actions
47 lines (34 loc) · 1.53 KB
/
Copy pathcreate_del_list_arg.py
File metadata and controls
47 lines (34 loc) · 1.53 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
import requests
from pprint import pprint
import os
import json
import argparse
VALID_TYPES = ['all', 'owner', 'public', 'private', 'member']
my_parser = argparse.ArgumentParser()
my_parser.add_argument("--reponame","--r", dest="reponame",
help='Please enter the repository name')
my_parser.add_argument("--deleterepo","--d",dest="deleterepo",
help='To delete a repository')
my_parser.add_argument("--listrepo","--l",dest="listrepo",
help='To list a repository owned by "all, owner, public, private, member" default value is all', default='all', choices = VALID_TYPES)
args = my_parser.parse_args()
token = os.environ.get("GITHUB_TOKEN")
# https://advanced-python.readthedocs.io/en/latest/rest/list_repos.html
reponame = args.reponame
deleterepo = args.deleterepo
listrepo = args.listrepo
GITHUB_API_URL = "https://api.github.com/"
headers = {"Authorization": "token {}".format(token)}
data = {"name": "{}".format(reponame)}
if reponame:
r = requests.post(GITHUB_API_URL +"user/repos" + "", data=json.dumps(data), headers=headers)
if deleterepo:
username = input("Please enter your GitHub username: ")
r = requests.delete("https://api.github.com/repos/{}/{}".format(username,deleterepo), headers=headers)
print(r)
if listrepo:
username = input("Please enter your GitHub username: ")
output = requests.get("https://api.github.com/users/{}/repos".format(username))
output = json.loads(output.text)
for repo in output:
pprint(repo["name"])