← Back to team overview

launchpad-reviewers team mailing list archive

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

 

Bayard 'kit' Randel has proposed merging lp:~blr/lp-dev-utils/hide-bug-messages-by-user into lp:lp-dev-utils.

Commit message:
Add script hide-bug-comments-by-user.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~blr/lp-dev-utils/hide-bug-messages-by-user/+merge/274944

Adds the script hide-bug-comments-by-user which iterates through searchTasks, setting bug comments visibility for the given user(s).
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~blr/lp-dev-utils/hide-bug-messages-by-user into lp:lp-dev-utils.
=== added file 'hide-bug-comments-by-user.py'
--- hide-bug-comments-by-user.py	1970-01-01 00:00:00 +0000
+++ hide-bug-comments-by-user.py	2015-10-19 20:33:45 +0000
@@ -0,0 +1,71 @@
+#!/usr/bin/env python
+
+"""A utility to hide bug comments by usernames; particularly useful when
+dealing with spam.
+"""
+
+__all__ = [
+    'main',
+    ]
+
+import argparse
+from datetime import datetime
+import sys
+
+from launchpadlib.launchpad import Launchpad
+import pytz
+
+
+def hideBugComments(user, timestamp=None):
+    count = 0
+    time_threshold = None
+    tasks = user.searchTasks(omit_duplicates=False)
+    if timestamp:
+        time_threshold = datetime.utcfromtimestamp(
+            timestamp).replace(tzinfo=pytz.utc)
+    for task in tasks:
+        for msg_num, msg in enumerate(task.bug.messages):
+            if msg.owner != user:
+                continue
+            if time_threshold and msg.date_created < time_threshold:
+                continue
+            task.bug.setCommentVisibility(
+                comment_number=msg_num, visible=False)
+            count += 1
+    print('%d comments hidden.' % count)
+
+
+def main():
+    parser = argparse.ArgumentParser(
+        description='Hides bugtask comments for a given user(s).')
+    parser.add_argument('username', type=str, nargs='+',
+                        help='One or more launchpad usernames')
+    parser.add_argument('--timestamp', metavar="POSIX Timestamp",
+                        dest='timestamp', type=float,
+                        help='Comments before this timestamp will be ignored')
+    parser.add_argument(
+        '--api-url',
+        help='API url to target, defaults to https://api.launchpad.net')
+
+    arg_vals = parser.parse_args()
+    usernames = arg_vals.username
+    timestamp = arg_vals.timestamp
+
+    if arg_vals.api_url:
+        api_root = arg_vals.api_url
+    else:
+        api_root = 'https://api.launchpad.net'
+
+    launchpad = Launchpad.login_with(
+        'hidebugcomments', service_root=api_root, version='devel')
+    for username in usernames:
+        try:
+            user = launchpad.people[username]
+        except KeyError:
+            print('User %s not found.' % username)
+            sys.exit(1)
+
+        hideBugComments(user, timestamp)
+
+if __name__ == '__main__':
+    main()


Follow ups