annotate contrib/phab-clean.py @ 50487:7f0f3b274d1e

ui: keep the progress bar around when writing if stdout is not a tty
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Tue, 21 Mar 2023 14:29:35 +0000
parents 6000f5b25c9b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45849
c102b704edb5 global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44291
diff changeset
1 #!/usr/bin/env python3
44291
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
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
7 import datetime
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
8 import os
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
9 import sys
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
10
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
11 import phabricator
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
12
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
13 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
14
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
15 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
16
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
17 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
18
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
19 :baymax:need-review-idle:
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
20 """
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 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
24 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
25 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
26
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 NOW = datetime.datetime.now()
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
29
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
30 # 3 months in seconds
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
31 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
32
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 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
35 """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
36 return phab.differential.query(
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
37 status="status-needs-review",
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
38 order="order-modified",
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
39 paths=[('HG', None)],
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
40 )
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 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
44 """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
45 olds = []
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
46 for d in diffs:
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
47 modified = int(d['dateModified'])
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
48 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
49 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
50 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
51 olds.append(d)
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
52 return olds
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
53
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 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
56 """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
57 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
58 phab.differential.createcomment(
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
59 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
60 )
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 if not USER:
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
64 print(
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
65 "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
66 file=sys.stderr,
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
67 )
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
68 elif not TOKEN:
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
69 print(
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
70 "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
71 file=sys.stderr,
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
72 )
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
73 sys.exit(1)
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
74
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
75 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
76 phab.connect()
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
77 phab.update_interfaces()
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
78 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
79
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
80 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
81 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
82 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
83 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
84 for d in olds:
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
85 diff_id = d['id']
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
86 status = d['statusName']
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
87 modified = int(d['dateModified'])
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
88 idle_for = d["idleFor"]
064eb65d040f contrib: a small script to nudge lingering diff
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
89 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
90 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
91 # 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
92 nudge_diff(phab, d)