Update Github module to pass access token in request header (#777)

* Update Github module to pass access token in request header

Also update the status colors to match the current Github Status webpage
CSS.

* Fix indentation
This commit is contained in:
Erik Johnson 2020-04-12 10:50:08 -05:00 committed by GitHub
parent 25f43621ed
commit e0234c1223
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,8 +19,7 @@ except ImportError:
API_METHODS_URL = 'https://www.githubstatus.com/api/v2/summary.json' API_METHODS_URL = 'https://www.githubstatus.com/api/v2/summary.json'
STATUS_URL = 'https://www.githubstatus.com' STATUS_URL = 'https://www.githubstatus.com'
NOTIFICATIONS_URL = 'https://github.com/notifications' NOTIFICATIONS_URL = 'https://github.com/notifications'
ACCESS_TOKEN_AUTH_URL = 'https://api.github.com/notifications?access_token=%s' AUTH_URL = 'https://api.github.com/notifications'
BASIC_AUTH_URL = 'https://api.github.com/notifications'
class Github(IntervalModule): class Github(IntervalModule):
@ -230,10 +229,10 @@ class Github(IntervalModule):
'critical': 'GitHub', 'critical': 'GitHub',
} }
_default_colors = { _default_colors = {
'none': '#2ecc71', 'none': '#28a745',
'minor': '#f1c40f', 'minor': '#dbab09',
'major': '#e67e22', 'major': '#e36209',
'critical': '#e74c3c', 'critical': '#dc3545',
} }
# Module configurables # Module configurables
@ -512,14 +511,21 @@ class Github(IntervalModule):
'access token' if self.access_token else 'username/password' 'access token' if self.access_token else 'username/password'
) )
old_unread_url = None
if self.access_token: if self.access_token:
unread_url = ACCESS_TOKEN_AUTH_URL % self.access_token request_kwargs = {
'headers': {
'Authorization': 'token {}'.format(self.access_token),
},
}
else: else:
unread_url = BASIC_AUTH_URL request_kwargs = {
'auth': (self.username, self.password),
}
self.current_unread = set() self.current_unread = set()
page_num = 0 page_num = 0
old_unread_url = None
unread_url = AUTH_URL
while old_unread_url != unread_url: while old_unread_url != unread_url:
old_unread_url = unread_url old_unread_url = unread_url
page_num += 1 page_num += 1
@ -528,13 +534,7 @@ class Github(IntervalModule):
page_num, unread_url page_num, unread_url
) )
try: try:
if self.access_token: response = requests.get(unread_url, **request_kwargs)
response = requests.get(unread_url)
else:
response = requests.get(
unread_url,
auth=(self.username, self.password)
)
self.logger.log( self.logger.log(
5, 5,
'Raw return from GitHub notification check: %s', 'Raw return from GitHub notification check: %s',