This repository has been archived by the owner on May 11, 2021. It is now read-only.
/
close-old-issues.py
executable file
·70 lines (56 loc) · 3.22 KB
/
close-old-issues.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
#!/usr/bin/python
import json
from contextlib import closing
import urllib2
import base64
import re
import getpass
import datetime
close_before = datetime.datetime.today() - datetime.timedelta(days = 8);
print
print "This script will close all issues opened by KhanBugz"
print "before", close_before.date(), "with no comments"
print
github_user = raw_input("GitHub username [%s]: " % getpass.getuser())
if not github_user:
github_user = getpass.getuser()
github_pass = getpass.getpass()
basic_auth_str = base64.encodestring('%s:%s' % (github_user, github_pass))[:-1]
comment = {}
comment['body'] = "### This is an automated comment\n\n"
comment['body'] += "Thank you for reporting a problem with this exercise. This issue has been open "
comment['body'] += "for over a week and unfortunately no one has had a chance to look at it yet.\n\n"
comment['body'] += "We get close to 100 new issues reported each day, many of which are not real "
comment['body'] += "bugs, but simply students having difficulty with the math. Unfortunately this "
comment['body'] += "means that even with the assistance of our awesome volunteers, we can't always "
comment['body'] += "go through and address each issue individually. Consequently, this issue is being "
comment['body'] += "automatically closed.\n\n"
comment['body'] += "If you're still experiencing a problem, please report it again! We always "
comment['body'] += "keep an eye on the ratio users reporting a problem to users successfully using "
comment['body'] += "an exercise, so reporting a problem--even if we can't individually address "
comment['body'] += "it--really helps."
def close_issues(page):
with closing(urllib2.urlopen("https://api.github.com/repos/Khan/khan-exercises/issues?page=" + str(page) + "&per_page=100")) as issue_data:
issues = json.loads(issue_data.read())
for issue in issues:
if issue['comments'] == 0 and issue['user']['login'] == "KhanBugz":
created = datetime.datetime.strptime(issue['created_at'], "%Y-%m-%dT%H:%M:%SZ")
if created < close_before:
req = urllib2.Request("https://api.github.com/repos/Khan/khan-exercises/issues/%s/comments" % issue['number'], json.dumps(comment) )
req.add_header("Authorization", "Basic %s" % basic_auth_str)
try:
urllib2.urlopen(req)
print "added comment to issue %s" % ( issue['number'] )
except IOError, e:
print "error adding comment to %s: %s" % ( issue['number'], e )
issue['state'] = "closed"
req = urllib2.Request("https://api.github.com/repos/Khan/khan-exercises/issues/%s" % issue['number'], json.dumps(issue) )
req.add_header("Authorization", "Basic %s" % basic_auth_str)
try:
urllib2.urlopen(req)
print "closed issue %s" % ( issue['number'] )
except IOError, e:
print "error closing %s: %s" % ( issue['number'], e )
if re.findall(r'<(.*?)>; rel="(.*?)"', issue_data.info().getheader("Link"))[0][1] == "next":
close_issues( page + 1 )
close_issues(1)