summaryrefslogtreecommitdiffstats
path: root/contrib/abandon_stale.py
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/abandon_stale.py')
-rwxr-xr-xcontrib/abandon_stale.py51
1 files changed, 36 insertions, 15 deletions
diff --git a/contrib/abandon_stale.py b/contrib/abandon_stale.py
index 99022aa6af..2e01131d5c 100755
--- a/contrib/abandon_stale.py
+++ b/contrib/abandon_stale.py
@@ -71,6 +71,9 @@ def _main():
action='store_true',
help='enable dry-run mode: show stale changes but do '
'not abandon them')
+ parser.add_option('-t', '--test', dest='testmode', action='store_true',
+ help='test mode: query changes with the `test-abandon` '
+ 'topic and ignore age option')
parser.add_option('-a', '--age', dest='age',
metavar='AGE',
default="6months",
@@ -100,6 +103,9 @@ def _main():
default=None,
action='store',
help='only abandon changes owned by the given user')
+ parser.add_option('--exclude-wip', dest='exclude_wip',
+ action='store_true',
+ help='Exclude changes that are Work-in-Progress')
parser.add_option('-v', '--verbose', dest='verbose',
action='store_true',
help='enable verbose (debug) logging')
@@ -114,13 +120,16 @@ def _main():
logging.error("Gerrit URL is required")
return 1
- pattern = re.compile(r"^([\d]+)(month[s]?|year[s]?|week[s]?)")
- match = pattern.match(options.age)
- if not match:
- logging.error("Invalid age: %s", options.age)
- return 1
- message = "Abandoning after %s %s or more of inactivity." % \
- (match.group(1), match.group(2))
+ if options.testmode:
+ message = "Abandoning in test mode"
+ else:
+ pattern = re.compile(r"^([\d]+)(month[s]?|year[s]?|week[s]?)")
+ match = pattern.match(options.age)
+ if not match:
+ logging.error("Invalid age: %s", options.age)
+ return 1
+ message = "Abandoning after %s %s or more of inactivity." % \
+ (match.group(1), match.group(2))
if options.digest_auth:
auth_type = HTTPDigestAuthFromNetrc
@@ -139,7 +148,12 @@ def _main():
stale_changes = []
offset = 0
step = 500
- query_terms = ["status:new", "age:%s" % options.age]
+ if options.testmode:
+ query_terms = ["status:new", "owner:self", "topic:test-abandon"]
+ else:
+ query_terms = ["status:new", "age:%s" % options.age]
+ if options.exclude_wip:
+ query_terms += ["-is:wip"]
if options.branches:
query_terms += ["branch:%s" % b for b in options.branches]
elif options.exclude_branches:
@@ -148,7 +162,7 @@ def _main():
query_terms += ["project:%s" % p for p in options.projects]
elif options.exclude_projects:
query_terms = ["-project:%s" % p for p in options.exclude_projects]
- if options.owner:
+ if options.owner and not options.testmode:
query_terms += ["owner:%s" % options.owner]
query = "%20".join(query_terms)
while True:
@@ -177,21 +191,27 @@ def _main():
abandon_message += "\n\n" + options.message
for change in stale_changes:
number = change["_number"]
- try:
- owner = change["owner"]["name"]
- except:
- owner = "Unknown"
+ project = ""
+ if len(options.projects) != 1:
+ project = "%s: " % change["project"]
+ owner = ""
+ if options.verbose:
+ try:
+ o = change["owner"]["name"]
+ except KeyError:
+ o = "Unknown"
+ owner = " (%s)" % o
subject = change["subject"]
if len(subject) > 70:
subject = subject[:65] + " [...]"
change_id = change["id"]
- logging.info("%s (%s): %s", number, owner, subject)
+ logging.info("%s%s: %s%s", number, owner, project, subject)
if options.dry_run:
continue
try:
gerrit.post("/changes/" + change_id + "/abandon",
- json={"message" : "%s" % abandon_message})
+ json={"message": "%s" % abandon_message})
abandoned += 1
except Exception as e:
errors += 1
@@ -200,5 +220,6 @@ def _main():
if not options.dry_run:
logging.info("Abandoned %d changes. %d errors.", abandoned, errors)
+
if __name__ == "__main__":
sys.exit(_main())