| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# A small script to automatically reject idle Diffs |
| 4 |
# |
| 5 |
# you need to set the PHABBOT_USER and PHABBOT_TOKEN environment variable for authentication |
| 6 |
|
| 7 |
import datetime |
| 8 |
import os |
| 9 |
import sys |
| 10 |
|
| 11 |
import phabricator |
| 12 |
|
| 13 |
MESSAGE = """There seems to have been no activities on this Diff for the past 3 Months. |
| 14 |
|
| 15 |
By policy, we are automatically moving it out of the `need-review` state. |
| 16 |
|
| 17 |
Please, move it back to `need-review` without hesitation if this diff should still be discussed. |
| 18 |
|
| 19 |
:baymax:need-review-idle: |
| 20 |
""" |
| 21 |
|
| 22 |
|
| 23 |
PHAB_URL = "https://phab.mercurial-scm.org/api/" |
| 24 |
USER = os.environ.get("PHABBOT_USER", "baymax") |
| 25 |
TOKEN = os.environ.get("PHABBOT_TOKEN") |
| 26 |
|
| 27 |
|
| 28 |
NOW = datetime.datetime.now() |
| 29 |
|
| 30 |
# 3 months in seconds |
| 31 |
DELAY = 60 * 60 * 24 * 30 * 3 |
| 32 |
|
| 33 |
|
| 34 |
def get_all_diff(phab): |
| 35 |
"""Fetch all the diff that the need review""" |
| 36 |
return phab.differential.query( |
| 37 |
status="status-needs-review", |
| 38 |
order="order-modified", |
| 39 |
paths=[('HG', None)], |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
def filter_diffs(diffs, older_than): |
| 44 |
"""filter diffs to only keep the one unmodified sin <older_than> seconds""" |
| 45 |
olds = [] |
| 46 |
for d in diffs: |
| 47 |
modified = int(d['dateModified']) |
| 48 |
modified = datetime.datetime.fromtimestamp(modified) |
| 49 |
d["idleFor"] = idle_for = NOW - modified |
| 50 |
if idle_for.total_seconds() > older_than: |
| 51 |
olds.append(d) |
| 52 |
return olds |
| 53 |
|
| 54 |
|
| 55 |
def nudge_diff(phab, diff): |
| 56 |
"""Comment on the idle diff and reject it""" |
| 57 |
diff_id = int(d['id']) |
| 58 |
phab.differential.createcomment( |
| 59 |
revision_id=diff_id, message=MESSAGE, action="reject" |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
if not USER: |
| 64 |
print( |
| 65 |
"not user specified please set PHABBOT_USER and PHABBOT_TOKEN", |
| 66 |
file=sys.stderr, |
| 67 |
) |
| 68 |
elif not TOKEN: |
| 69 |
print( |
| 70 |
"not api-token specified please set PHABBOT_USER and PHABBOT_TOKEN", |
| 71 |
file=sys.stderr, |
| 72 |
) |
| 73 |
sys.exit(1) |
| 74 |
|
| 75 |
phab = phabricator.Phabricator(USER, host=PHAB_URL, token=TOKEN) |
| 76 |
phab.connect() |
| 77 |
phab.update_interfaces() |
| 78 |
print('Hello "%s".' % phab.user.whoami()['realName']) |
| 79 |
|
| 80 |
diffs = get_all_diff(phab) |
| 81 |
print("Found %d Diffs" % len(diffs)) |
| 82 |
olds = filter_diffs(diffs, DELAY) |
| 83 |
print("Found %d old Diffs" % len(olds)) |
| 84 |
for d in olds: |
| 85 |
diff_id = d['id'] |
| 86 |
status = d['statusName'] |
| 87 |
modified = int(d['dateModified']) |
| 88 |
idle_for = d["idleFor"] |
| 89 |
msg = 'nudging D%s in "%s" state for %s' |
| 90 |
print(msg % (diff_id, status, idle_for)) |
| 91 |
# uncomment to actually affect phab |
| 92 |
nudge_diff(phab, d) |