forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_issue_no_activity_closer.py
125 lines (93 loc) · 3.84 KB
/
github_issue_no_activity_closer.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
import datetime
import getpass
import json
import re
import time
import requests
AUTOCLOSE_MESSAGE = """\
### This is an automated comment
This issue has been closed due to lack of activity. This doesn't mean the \
issue is invalid, it simply got no attention within the last year. Please \
reopen with missing/relevant information if still valid.
Typically, issues fall in this state for one of the following reasons:
- Hard, impossible or not enough information to reproduce
- Missing test case
- Lack of a champion with interest and/or funding to address the issue
"""
AUTOCLOSE_LABEL = "No Recent Activity"
def fetch_issues(headers):
issues = []
url = ("https://api.github.com/repos/mapserver/mapserver/issues?"
"per_page=100")
while url:
print "Fetching %s" % url
r = requests.get(url, headers=headers)
r.raise_for_status()
time.sleep(1)
issues.extend(r.json())
match = re.match(r'<(.*?)>; rel="next"', r.headers['Link'] or '')
url = match.group(1) if match else None
return issues
def close_issue(issue, headers):
"""Attempt to close an issue and return whether it succeeded."""
r = requests.post("https://api.github.com/repos/mapserver/mapserver/"
"issues/%s" % issue['number'],
data=json.dumps({'state': 'closed'}), headers=headers)
try:
r.raise_for_status()
time.sleep(1)
return True
except requests.HTTPError:
return False
def post_issue_comment(issue, comment_text, headers):
"""Attempt to post an issue comment and return whether it succeeded."""
r = requests.post("https://api.github.com/repos/mapserver/mapserver/"
"issues/%s/comments" % issue['number'],
data=json.dumps({'body': comment_text}), headers=headers)
try:
r.raise_for_status()
time.sleep(1)
return True
except requests.HTTPError:
return False
def add_issue_label(issue, label, headers):
"""Attempt to add a label to the issue and return whether it succeeded."""
r = requests.post("https://api.github.com/repos/mapserver/mapserver/"
"issues/%s/labels" % issue['number'],
data=json.dumps([label]), headers=headers)
try:
r.raise_for_status()
time.sleep(1)
return True
except requests.HTTPError:
return False
def close_old_issues(close_before, headers):
all_issues = fetch_issues(headers)
for issue in all_issues:
issue_last_activity = datetime.datetime.strptime(
issue['updated_at'], "%Y-%m-%dT%H:%M:%SZ")
print "Processing issue %s with last activity at %s" % (issue['number'], issue_last_activity)
if issue_last_activity < close_before and issue["state"] == "open":
if post_issue_comment(issue, AUTOCLOSE_MESSAGE, headers):
print " Added comment to old issue %s" % issue['number']
else:
print " Error adding comment to old issue %s" % issue['number']
continue
if add_issue_label(issue, AUTOCLOSE_LABEL, headers):
print " Added label to old issue %s" % issue['number']
else:
print " Error adding label to old issue %s" % issue['number']
if close_issue(issue, headers):
print " Closed old issue %s" % issue['number']
else:
print " Error closing old issue %s" % issue['number']
if __name__ == '__main__':
close_before = datetime.datetime.today() - datetime.timedelta(days=366)
print
print "This script closes all issues with no activity within the last year."
print
github_token = getpass.getpass("Please provide an oauth token: ")
print
headers = {"Authorization": "token %s" % github_token}
close_old_issues(close_before, headers)