forked from cockpit-project/bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-info
executable file
·74 lines (56 loc) · 2.53 KB
/
github-info
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
#!/usr/bin/env python3
# This file is part of Cockpit.
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
# Shared GitHub code. When run as a script, we print out info about
# our GitHub interacition.
import argparse
import datetime
import sys
from task import github
sys.dont_write_bytecode = True
def httpdate(dt):
"""Return a string representation of a date according to RFC 1123
(HTTP/1.1).
The supplied date must be in UTC.
From: http://stackoverflow.com/a/225106
"""
weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()]
month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"][dt.month - 1]
return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month,
dt.year, dt.hour, dt.minute, dt.second)
def main():
parser = argparse.ArgumentParser(description='Test GitHub rate limits')
parser.parse_args()
# in order for the limit not to be affected by the call itself,
# use a conditional request with a timestamp in the future
future_timestamp = datetime.datetime.utcnow() + datetime.timedelta(seconds=3600)
api = github.GitHub()
headers = {'If-Modified-Since': httpdate(future_timestamp)}
response = api.request("GET", "git/refs/heads/main", "", headers)
sys.stdout.write("Rate limits:\n")
for entry in ["X-RateLimit-Limit", "X-RateLimit-Remaining", "X-RateLimit-Reset"]:
entries = [t for t in response['headers'].items() if t[0].lower() == entry.lower()]
if entries:
if entry == "X-RateLimit-Reset":
readable = datetime.datetime.utcfromtimestamp(float(entries[0][1])).isoformat()
sys.stdout.write(f"{entry}: {entries[0][1]} ({readable})\n")
else:
sys.stdout.write(f"{entry}: {entries[0][1]}\n")
return 0
if __name__ == '__main__':
sys.exit(main())