contrib/phab-clean.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Wed, 06 Apr 2022 18:39:15 +0200
branchstable
changeset 49048 020378f32d57
parent 45830 c102b704edb5
child 48875 6000f5b25c9b
permissions -rwxr-xr-x
debuglock: ignore ENOENT error when unlocking This is consistent with the main `lock.release` code. Differential Revision: https://phab.mercurial-scm.org/D12481
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45830
c102b704edb5 global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44217
diff changeset
     1
#!/usr/bin/env python3
44217
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     2
#
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     3
# A small script to automatically reject idle Diffs
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     4
#
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     5
# you need to set the PHABBOT_USER and PHABBOT_TOKEN environment variable for authentication
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     6
from __future__ import absolute_import, print_function
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     7
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     8
import datetime
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     9
import os
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    10
import sys
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    11
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    12
import phabricator
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    13
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    14
MESSAGE = """There seems to have been no activities on this Diff for the past 3 Months.
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    15
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    16
By policy, we are automatically moving it out of the `need-review` state.
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    17
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    18
Please, move it back to `need-review` without hesitation if this diff should still be discussed.
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    19
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    20
:baymax:need-review-idle:
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    21
"""
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    22
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    23
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    24
PHAB_URL = "https://phab.mercurial-scm.org/api/"
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    25
USER = os.environ.get("PHABBOT_USER", "baymax")
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    26
TOKEN = os.environ.get("PHABBOT_TOKEN")
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    27
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    28
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    29
NOW = datetime.datetime.now()
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    30
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    31
# 3 months in seconds
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    32
DELAY = 60 * 60 * 24 * 30 * 3
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    33
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    34
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    35
def get_all_diff(phab):
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    36
    """Fetch all the diff that the need review"""
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    37
    return phab.differential.query(
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    38
        status="status-needs-review",
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    39
        order="order-modified",
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    40
        paths=[('HG', None)],
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    41
    )
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    42
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    43
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    44
def filter_diffs(diffs, older_than):
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    45
    """filter diffs to only keep the one unmodified sin <older_than> seconds"""
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    46
    olds = []
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    47
    for d in diffs:
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    48
        modified = int(d['dateModified'])
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    49
        modified = datetime.datetime.fromtimestamp(modified)
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    50
        d["idleFor"] = idle_for = NOW - modified
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    51
        if idle_for.total_seconds() > older_than:
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    52
            olds.append(d)
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    53
    return olds
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    54
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    55
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    56
def nudge_diff(phab, diff):
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    57
    """Comment on the idle diff and reject it"""
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    58
    diff_id = int(d['id'])
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    59
    phab.differential.createcomment(
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    60
        revision_id=diff_id, message=MESSAGE, action="reject"
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    61
    )
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    62
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    63
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    64
if not USER:
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    65
    print(
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    66
        "not user specified please set PHABBOT_USER and PHABBOT_TOKEN",
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    67
        file=sys.stderr,
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    68
    )
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    69
elif not TOKEN:
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    70
    print(
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    71
        "not api-token specified please set PHABBOT_USER and PHABBOT_TOKEN",
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    72
        file=sys.stderr,
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    73
    )
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    74
    sys.exit(1)
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    75
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    76
phab = phabricator.Phabricator(USER, host=PHAB_URL, token=TOKEN)
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    77
phab.connect()
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    78
phab.update_interfaces()
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    79
print('Hello "%s".' % phab.user.whoami()['realName'])
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    80
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    81
diffs = get_all_diff(phab)
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    82
print("Found %d Diffs" % len(diffs))
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    83
olds = filter_diffs(diffs, DELAY)
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    84
print("Found %d old Diffs" % len(olds))
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    85
for d in olds:
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    86
    diff_id = d['id']
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    87
    status = d['statusName']
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    88
    modified = int(d['dateModified'])
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    89
    idle_for = d["idleFor"]
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    90
    msg = 'nudging D%s in "%s" state for %s'
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    91
    print(msg % (diff_id, status, idle_for))
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    92
    # uncomment to actually affect phab
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    93
    nudge_diff(phab, d)