-
Notifications
You must be signed in to change notification settings - Fork 0
/
gittub.py
87 lines (68 loc) · 2.35 KB
/
gittub.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import logging
import requests
import json
import argparse
import pprint
def shortenMiddle(s, n):
sLen = len(s)
if sLen > n:
halfN = int(n / 2)
return s[:halfN] + '...' + s[-halfN:]
return s
logger = logging.getLogger('enable_req_status_check')
logger.setLevel(logging.DEBUG)
logger.handlers = []
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
logger.addHandler(sh)
ghRootEndpoint = 'https://api.github.com'
createRepoURL = ghRootEndpoint + "/user/repos"
deleteRepoURL = ghRootEndpoint + "/repos/tejakummarikuntla/{repo_name}"
ghAPIToken = os.environ['GITHUB_API_TOKEN']
logger.info("GITHUB_API_TOKEN='{}'".format(shortenMiddle(ghAPIToken, 6)))
headers = {'Authorization': 'token ' + ghAPIToken}
repoCreationPayload = json.dumps(
{
"name": "Hello-World",
"description": "This is your first repository",
"homepage": "https://github.com",
"private": False,
"has_issues": True,
"has_projects": True,
"has_wiki": True
})
def createRepo(repo):
load_dict = json.loads(repoCreationPayload)
load_dict["name"]=repo
# repoCreationPayload["private"] = mode
ghApiCreateResponse = requests.post(createRepoURL,
json.dumps(load_dict),
headers = headers).json()
logger.info(pprint.pprint(ghApiCreateResponse))
def deleteRepo(repo):
ghApiDeleteResponse = requests.delete(deleteRepoURL.format(repo_name=repo),
headers = headers
)
logger.info(pprint.pprint(ghApiDeleteResponse))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog= 'CLI to Create or Delete a Repo',
usage='''
Use -c to Create a repository
Use -d to Delete a repository
''',
description='''
This tool works with GitHub API Token:
Currently, Can create and Delete a Repository
''',
add_help= True
)
parser.add_argument("--create", "-c", type=str, help="Enter the name of the Repo", metavar="")
parser.add_argument("--delete", "-d", type=str, )
arg = parser.parse_args()
if arg.create is not None:
createRepo(arg.create)
else:
if arg.delete is not None:
deleteRepo(arg.delete)