This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcp-api.py
206 lines (190 loc) · 5.54 KB
/
mcp-api.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import os
import sys
import json
try:
import requests
except:
# error message
print("Error, 'requests' is not installed.\nTry again after disabling active 'env', or type: `pip install requests`.")
# exit script
sys.exit(1)
#--------------#
# DEF SELECTOR #
#--------------#
def selector(arg):
switch = {
'createRepo': createRepo,
'delRepo': delRepo,
'descRepo': descRepo,
'repoList': repoList,
'gitVisibility': gitVisibility,
}
# get the case
case = switch.get(arg, 'error')
if case == 'error':
sys.stdout.write('error, invalid case\n')
else:
# run the case
case()
#------------#
# HELPER DEF #
#------------#
# get repo list
def _getRepo():
# get user auth
auth = (arg_list[1],arg_list[2])
# modify url
url = base+'user/repos'
# get repo from api
r = requests.get(url, auth=auth)
# check response
if r.ok:
return r
else:
sys.stdout.write('%s\n' %r.json()['message'])
sys.exit(1)
# confirm that given repo exist
def _confirmRepo():
# get repos
data = _getRepo()
# look for match
for repo in data.json():
if arg_list[3].lower() == repo['name'].lower():
# repo found
return True
# no matching repo names
return False
#--------#
# GITHUB #
#--------#
# create a new repo on github
def createRepo():
sys.stdout.write('creating new repo...\n')
# test for existing repo
data = _getRepo()
for repo in data.json():
if repo['name'].lower() == arg_list[3].lower():
sys.stdout.write('error, repo already exist.\n')
sys.exit(1)
# get user auth
auth = (arg_list[1], arg_list[2])
# modify base url
url = base+'user/repos'
# set true/false
arg_list[5] = True if arg_list[5] == 'true' else False
# new repo data
data = json.dumps({"name": arg_list[3], "description": arg_list[4], "private": arg_list[5]})
# post new repo to api
r = requests.post(url, auth=auth, data=data)
# check responcse
if r.ok:
sys.stdout.write('repo created\n')
sys.exit(0)
else:
sys.stdout.write('%s\n' %r.json()['message'])
sys.exit(1)
# delete repo from github
def delRepo():
sys.stdout.write('checking existing repos...\n')
# confirm repo name
if _confirmRepo():
sys.stdout.write('found repo, deleting...\n')
# get user auth
auth = (arg_list[1], arg_list[2])
# modify base url
url = base+'repos/'+arg_list[1]+'/'+arg_list[3]
# send delete request to api
r = requests.delete(url, auth=auth)
# check responcse
if r.ok:
sys.stdout.write('repo deleted\n')
sys.exit(0)
else:
sys.stdout.write('%s\n' %r.json()['message'])
sys.exit(1)
else:
sys.stdout.write('no repo exist with the name: %s\n' %arg_list[3])
# return invalid repo
sys.exit(1)
# add/change repo description
def descRepo():
sys.stdout.write('changing repo description...\n')
# confirm repo name
if _confirmRepo():
sys.stdout.write('found repo, updating...\n')
# get user auth
auth = (arg_list[1], arg_list[2])
# modify base url
url = base+'repos/'+arg_list[1]+'/'+arg_list[3]
# updated repo data
data = json.dumps({"description":arg_list[4]})
# send delete request to api
r = requests.patch(url, auth=auth, data=data)
# check responcse
if r.ok:
sys.stdout.write('description changed\n')
sys.exit(0)
else:
sys.stdout.write('%s\n' %r.json()['message'])
sys.exit(1)
else:
sys.stdout.write('no repo exist with the name: %s\n' %arg_list[3])
# return invalid repo
sys.exit(1)
# get repos from github
def repoList():
# get repo list
data = _getRepo()
# display info
for repo in data.json():
sys.stdout.write(' name: %s\n' %repo['name'])
sys.stdout.write(' description: %s\n' %repo['description'])
sys.stdout.write(' private: %s\n' %repo['private'])
sys.stdout.write('-----------------------------------\n')
sys.exit(0)
# change repo visibility
def gitVisibility():
sys.stdout.write('changing repo visibility...\n')
# get user auth
auth = (arg_list[1],arg_list[2])
# modify url
url = base+'repos/'+arg_list[1]+'/'+arg_list[3]
# default state
private = True
# check repo name
if _confirmRepo():
# get repo visibility state
data = _getRepo()
for repo in data.json():
if repo['name'].lower() == arg_list[3].lower():
if repo['private']:
private = False
else:
private = True
# patch information
data = json.dumps({'private': private})
# send updated info
r = requests.patch(url, auth=auth, data=data)
# check response
if r.ok:
sys.stdout.write('done, private = %s\n' %private)
sys.exit(0)
else:
sys.stdout.write('%s\n' %r.json()['message'])
sys.exit(1)
else:
sys.stdout.write('no repo exist with the name: %s\n' %arg_list[3])
# return invalid repo
sys.exit(1)
#---------------#
# GET FROM BASH #
#---------------#
# base url
base = 'https://api.github.com/'
# argument list
arg_list = []
# get arguments from bash
for arg in sys.argv[1:]:
arg_list.append(arg)
# run selector
selector(arg_list[0])