forked from cockpit-project/bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naughty-prune
executable file
·95 lines (73 loc) · 3.01 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
91
92
93
94
95
#!/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 sys
import subprocess
import time
sys.dont_write_bytecode = True
import task
from machine.machine_core.directories import BASE_DIR
# 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, universal_newlines=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()
# Don't replace this issue with the pull request(s)
if "issue" in kwargs:
del kwargs["issue"]
# 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", "{0}-*".format(number), "-delete")
# Create a pull request from these changes
title = "naughty: Close {0}: {1}".format(number, issue["title"])
days = int((now - updated) / SECONDS_PER_DAY)
body = "Known issue which has not occurred in {0} days\n\n{1}\n\nFixes #{2}".format(
days, issue["title"], number)
branch = task.branch(number, "{0}\n\n{1}".format(title, 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")