-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeclimate_comp_metric.py
executable file
·63 lines (57 loc) · 2.51 KB
/
codeclimate_comp_metric.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
import requests
import json
import re
import os
import subprocess
from secrets import *
cc_headers = {"Authorization": "Token token=" + codeclimate_api_key , "Accept": "application/vnd.api+json"}
def get_all_codeclimate_data(owner_and_repo):
matchObj = re.match(r'.*\/(.*)', owner_and_repo)
short_name = matchObj.group(1)
os.chdir(os.path.expanduser("~/repos"))
os.chdir("./{0}".format(short_name))
# r = requests.get("https://api.codeclimate.com/v1/repos?github_slug={0}".format(owner_and_repo), headers=cc_headers)
# response = json.loads(r.text)
# repo_id = response["data"][0]["id"]
repo_id = "59ee8a344eb04002790006bc"
r = requests.get("https://api.codeclimate.com/v1/repos/{0}/ref_points?page[size]=100&filter[analyzed]=True&filter[branch]=master".format(repo_id), headers=cc_headers)
response = json.loads(r.text)
commit_to_snapshot = {}
has_next = True
while (has_next):
for datapt in response["data"]:
try:
print(datapt)
commit_to_snapshot[datapt["attributes"]["commit_sha"]] = datapt["relationships"]["snapshot"]["data"]["id"]
except:
print("ERROR?: ")
print(datapt)
has_next = response["links"] != {} and "next" in response["links"]
if has_next:
r = requests.get(response["links"]["next"])
response = json.loads(r.text)
print(commit_to_snapshot)
for commit, snapshot in commit_to_snapshot.items():
file_dict = {}
r = requests.get("https://api.codeclimate.com/v1/repos/{0}/snapshots/{1}".format(repo_id, snapshot), headers=cc_headers)
response = json.loads(r.text)
has_next = True
print(response)
while(has_next):
for file in response["data"]:
try:
file_dict[file["attributes"]["path"]] = file["attributes"]["rating"]
except:
print("ERROR?: ")
print(file)
has_next = response["links"] != {} and "next" in response["links"]
if has_next:
r = requests.get(response["links"]["next"])
response = json.loads(r.text)
save_to_file(commit, file_dict)
def save_to_file(commit_hash, file_dict):
if (not os.path.isdir("./codeclimate")):
os.system("mkdir codeclimate")
with open("codeclimate/" + commit_hash + ".json", 'w') as file:
file.write(json.dumps(file_dict))
#get_all_codeclimate_data("saasbook/CS169_Great_Course_Guide")