-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithubissuetracker.py
104 lines (86 loc) · 3.03 KB
/
githubissuetracker.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
"""
file: gitissuetracker.py
author: Christoffer Rosen <[email protected]>
date: December, 2013
description: Represents a Github Issue tracker object used
for getting the dates issues were opened.
12/12/13: Doesn't currently support private repos
"""
import requests, json, dateutil.parser, time
from caslogging import logging
class GithubIssueTracker:
"""
GitIssueTracker()
Represents a Github Issue Tracker Object
"""
owner = None # Owner of the github repo
repo = None # The repo name
request_repos = "https://api.github.com/repos" # Request url to get issue info
request_auth = "https://api.github.com/authorizations" # Request url for auth
def __init__(self, owner, repo):
"""
Constructor
"""
self.owner = owner
self.repo = repo
self.auth_token = None
self.authenticate() # Authenticate our app
def authenticate(self):
"""
authenticate()
Authenticates this application to github using
the cas-user git user credentials. This is temporary!
"""
s = requests.Session()
s.auth = ("cas-user", "riskykiwi1")
payload = {"scopes": ["repo"]}
r = s.get(self.request_auth, params=payload)
if r.headers.get('x-ratelimit-remaining') == '0':
logging.info("Github quota limit hit -- waiting")
# Wait up to a hour until we can continue..
while r.headers.get('x-ratelimit-remaining') == '0':
time.sleep(600) # Wait 10 minutes and try again
r = s.get(self.request_auth, params=payload)
data = r.json()
data = r.json()[0]
if r.status_code >= 400:
msg = data.get('message')
logging.error("Failed to authenticate issue tracker: \n" +msg)
return # Exit
else:
self.auth_token = data.get("token")
requests_left = r.headers.get('x-ratelimit-remaining')
logging.info("Analyzer has " + requests_left + " issue tracker calls left this hour")
def getDateOpened(self, issueNumber):
"""
getDateOpened()
Gets the date the issue number was opened in unix time
If issue cannot be found for whichever reason, returns null.
"""
header = {'Authorization': 'token ' + self.auth_token}
r = requests.get(self.request_repos + "/" + self.owner + "/" +
self.repo + "/issues/" + issueNumber, headers=header)
data = r.json()
# If forbidden
if r.status_code == 403:
# Check the api quota
if r.headers.get('x-ratelimit-remaining') == '0':
logging.info("Github quota limit hit -- waiting")
# Wait up to a hour until we can continue..
while r.headers.get('x-ratelimit-remaining') == '0':
time.sleep(600) # Wait 10 minutes and try again
r = requests.get(self.request_repos + "/" + self.owner + "/" +
self.repo + "/issues/" + issueNumber, headers=header)
data = r.json()
# Check for other error codes
elif r.status_code >= 400:
msg = data.get('message')
logging.error("ISSUE TRACKER FAILURE: \n" + msg)
return None
else:
try:
date = (dateutil.parser.parse(data.get('created_at'))).timestamp()
return date
except:
logging.error("ISSUE TRACKER FAILURE: Could not get created_at from github issues API")
return None