-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddRepositories.py
More file actions
79 lines (68 loc) · 3.06 KB
/
addRepositories.py
File metadata and controls
79 lines (68 loc) · 3.06 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import argparse
import requests
import json
import time
def listRepositoriesfromGithub(orgname,githubToken,githubBaseURL):
page = 1
listRepos = []
hasNextPage = True
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {githubToken}'
}
while(hasNextPage):
url = f'{githubBaseURL}/orgs/{orgname}/repos?page={page}'
response = requests.get(url, headers=headers)
repos = json.loads(response.text)
if len(repos) > 0:
for repo in repos:
listRepos.append(repo['name'])
page+=1
else:
hasNextPage = False
return listRepos
def addAllRepositories(baseurl,provider, organization, token,githubToken,githubBaseURL,reponame):
repositories = listRepositoriesfromGithub(organization,githubToken,githubBaseURL)
allAboard = (reponame == None)
targetRepos = []
if not allAboard:
targetRepos = reponame.split(',')
for repo in repositories:
if allAboard or repo in targetRepos:
addRepository(baseurl, provider, organization, repo,token)
def addRepository(baseurl, provider, organization, repo,token):
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'api-token': token
}
data ={
"repositoryFullPath": f'{organization}/{repo}',
"provider": provider
}
url = f'{baseurl}/api/v3/repositories'
response = requests.post(url, headers=headers, data=json.dumps(data))
print(repo,response.status_code)
def main():
print('\nWelcome to Codacy Integration Helper - A temporary solution to Add All repositories\n')
parser = argparse.ArgumentParser(description='Codacy Integration Helper')
parser.add_argument('--token', dest='token', default=None,
help='the api-token to be used on the REST API')
parser.add_argument('--githubToken', dest='githubToken', default=None,
help='the github token to be used on the REST API of Github')
parser.add_argument('--reponame', dest='reponame', default=None,
help='comma separated list of the repositories to be added, none means all')
parser.add_argument('--provider', dest='provider',
default=None, help='git provider (gh|ghe)')
parser.add_argument('--organization', dest='organization',
default=None, help='organization name')
parser.add_argument('--baseurl', dest='baseurl', default='https://app.codacy.com',
help='codacy server address (ignore if cloud)')
parser.add_argument('--githubBaseURL', dest='githubBaseURL', default='https://api.github.com',
help='GitHub API base address (ignore if cloud)')
args = parser.parse_args()
startdate = time.time()
addAllRepositories(args.baseurl,args.provider, args.organization, args.token,args.githubToken,args.githubBaseURL,args.reponame)
enddate = time.time()
print("\nThe script took ",round(enddate-startdate,2)," seconds")
main()