Merge lp:~blr/lp-dev-utils/hide-bug-messages-by-user into lp:lp-dev-utils

Proposed by Kit Randel
Status: Merged
Merged at revision: 126
Proposed branch: lp:~blr/lp-dev-utils/hide-bug-messages-by-user
Merge into: lp:lp-dev-utils
Diff against target: 86 lines (+82/-0)
1 file modified
hide-bug-comments-by-user.py (+82/-0)
To merge this branch: bzr merge lp:~blr/lp-dev-utils/hide-bug-messages-by-user
Reviewer Review Type Date Requested Status
William Grant code Needs Fixing
Review via email: mp+274944@code.launchpad.net

Commit message

Add script hide-bug-comments-by-user.

Description of the change

Adds the script hide-bug-comments-by-user which iterates through searchTasks, setting bug comments visibility for the given user(s). Useful for dealing with spammers.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) :
review: Needs Fixing (code)
127. By Kit Randel

* Call searchTasks with list of all bug statuses.
* Replace --timestamp with --since, allowing filtering by ISO 8601.

128. By Kit Randel

Remove unused imports.

Revision history for this message
William Grant (wgrant) :
review: Needs Fixing (code)
129. By Kit Randel

* Filter searchTasks by bug_commenter.
* Use more conventional -l/--launchpad cli argument for api url.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'hide-bug-comments-by-user.py'
2--- hide-bug-comments-by-user.py 1970-01-01 00:00:00 +0000
3+++ hide-bug-comments-by-user.py 2015-10-22 02:05:39 +0000
4@@ -0,0 +1,82 @@
5+#!/usr/bin/env python
6+
7+"""A utility to hide bug comments by usernames; particularly useful when
8+dealing with spam.
9+"""
10+
11+__all__ = [
12+ 'main',
13+ ]
14+
15+import argparse
16+import sys
17+
18+import iso8601
19+
20+from launchpadlib.launchpad import Launchpad
21+
22+
23+BUG_STATUSES = ['New',
24+ 'Incomplete (with response)',
25+ 'Incomplete (without response)',
26+ 'Opinion',
27+ 'Invalid',
28+ 'Won\'t Fix',
29+ 'Expired',
30+ 'Confirmed',
31+ 'Triaged',
32+ 'In Progress',
33+ 'Fix Committed',
34+ 'Fix Released']
35+
36+
37+def hideBugComments(user, filter_from_date=None):
38+ count = 0
39+ date_threshold = None
40+ tasks = user.searchTasks(omit_duplicates=False,
41+ bug_commenter=user,
42+ status=BUG_STATUSES)
43+ if filter_from_date:
44+ date_threshold = iso8601.parse_date(filter_from_date)
45+ for task in tasks:
46+ for msg in task.bug.messages:
47+ if msg.owner != user:
48+ continue
49+ if date_threshold and msg.date_created < date_threshold:
50+ continue
51+ comment_num = int(msg.self_link.rsplit('/', 1)[-1])
52+ print('Hiding comment: %s' % msg.web_link)
53+ task.bug.setCommentVisibility(
54+ comment_number=comment_num, visible=False)
55+ count += 1
56+ print('%d comments hidden.' % count)
57+
58+
59+def main():
60+ parser = argparse.ArgumentParser(
61+ description='Hides bugtask comments for a given user(s).')
62+ parser.add_argument('username', type=str, nargs='+',
63+ help='One or more launchpad usernames')
64+ parser.add_argument('--since', metavar="ISO 8601 Date e.g. 2015-10-20",
65+ dest='since', type=str,
66+ help='Comments before this date will be ignored')
67+ parser.add_argument(
68+ "-l", "--launchpad", dest="launchpad_instance", default="production")
69+
70+ arg_vals = parser.parse_args()
71+ usernames = arg_vals.username
72+ date = arg_vals.since
73+
74+ launchpad = Launchpad.login_with(
75+ 'hidebugcomments', arg_vals.launchpad_instance, version='devel')
76+ for username in usernames:
77+ try:
78+ user = launchpad.people[username]
79+ except KeyError:
80+ print('User %s not found.' % username)
81+ sys.exit(1)
82+
83+ hideBugComments(user, date)
84+
85+if __name__ == '__main__':
86+ main()

Subscribers

People subscribed via source and target branches