forked from cockpit-project/bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naughty-prune
executable file
·90 lines (69 loc) · 2.81 KB
/
naughty-prune
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
#!/usr/bin/env python3
# This file is part of Cockpit.
#
# Copyright (C) 2017 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/>.
# To use this example add a line to an issue with the "bot" label
#
# * [ ] naughty-prune
#
import subprocess
import sys
import time
import task
from lib.constants import BASE_DIR
sys.dont_write_bytecode = True
# Number of days after which a known issue is treated as stale
DAYS = 21
# In case this ever changes
SECONDS_PER_DAY = 3600 * 24
def execute(*args):
if task.verbose:
sys.stderr.write("+ " + " ".join(args) + "\n")
return subprocess.check_output(args, cwd=BASE_DIR, text=True)
def run(unused, verbose=False, **kwargs):
# Since a month ago
now = time.time()
since = now - (DAYS * SECONDS_PER_DAY)
# The head
head = execute("git", "rev-parse", "HEAD").strip()
# Get all the open known issues
issues = task.api.issues(labels=["knownissue"])
for issue in issues:
number = issue["number"]
updated = issue.get("updated_at", None)
if not updated:
updated = issue.get("created_at", None)
if not updated:
continue
# Don't touch recently updated issues
updated = time.mktime(time.strptime(updated, "%Y-%m-%dT%H:%M:%SZ"))
if updated > since:
continue
# Try to close this issue
execute("git", "checkout", "--detach", head)
execute("find", "naughty/", "-name", f"{number}-*", "-delete")
# Create a pull request from these changes
title = f"naughty: Close {number}: {issue['title']}"
days = int((now - updated) / SECONDS_PER_DAY)
body = f"Known issue which has not occurred in {days} days\n\n{issue['title']}\n\nFixes #{number}"
branch = task.branch(number, f"{title}\n\n{body}", pathspec="naughty/", **kwargs)
if branch:
kwargs["title"] = title
pull = task.pull(branch, body=body, **kwargs)
task.comment(pull, "Please comment on the upstream distro bug manually before accepting this "
"pull request.\n\nIf you wish to keep this known issue open, then update it")
if __name__ == '__main__':
task.main(function=run, title="Close known issues")