Skip to content

Commit 4e210a5

Browse files
authored
Merge pull request #10 from infinityworksltd/private_repos
Added pagination support
2 parents ca9f066 + c7f6c72 commit 4e210a5

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

github_exporter.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,36 @@ def _collect_org_metrics(self, gauges, metrics):
5858
for repo in response_json:
5959
self._add_metrics(gauges, metrics, repo)
6060

61-
def _get_json(self, url):
61+
def _get_json(self, url: str):
62+
"""
63+
using github core api
64+
rate limit 5000 per hours
65+
"""
6266
print("Getting JSON Payload for " + url)
6367
if os.getenv('GITHUB_TOKEN'):
6468
payload = {"access_token":os.environ["GITHUB_TOKEN"]}
65-
response_json = json.loads(requests.get(url,params=payload).content.decode('UTF-8'))
69+
r = requests.get(url,params=payload)
6670
else:
67-
response_json = json.loads(requests.get(url).content.decode('UTF-8'))
68-
return response_json
71+
r = requests.get(url)
72+
result = json.loads(r.content.decode('UTF-8'))
73+
if result is None:
74+
raise ValueError("Github API is broken, try again")
75+
return self._pagination(r, result)
76+
77+
def _pagination(self, response: requests.Response, result):
78+
if "Link" not in response.headers:
79+
return result
80+
links = dict()
81+
for i in response.headers["Link"].split(","):
82+
url, rel = i.split(";")
83+
rel = rel[6:-1]
84+
url = url[1:-1]
85+
links[rel] = url
86+
if "next" in links:
87+
assert type(result) is list
88+
return result + self._get_json(links["next"])
89+
else:
90+
return result
6991

7092
def _check_api_limit(self):
7193
rate_limit_url = "https://api.github.com/rate_limit"
@@ -91,6 +113,7 @@ def sigterm_handler(_signo, _stack_frame):
91113

92114
if __name__ == '__main__':
93115
# Ensure we have something to export
116+
print("Starting Exporter")
94117
if not (os.getenv('REPOS') or os.getenv('ORGS')):
95118
print("No repositories or organizations specified, exiting")
96119
exit(1)

0 commit comments

Comments
 (0)