#!/usr/bin/env python

"""Print the last time a reviewer(bot) left a comment."""

import argparse
import copy
import json
import time

import requests


TIME_FORMAT = "%Y-%m-%d %H:%M:%S"


class Comment(object):
    date = None
    change_id = None
    subject = None

    def __init__(self, date, change_id, subject):
        super(Comment, self).__init__()
        self.date = date
        self.change_id = change_id
        self.subject = subject

    def __str__(self):
        return ("%s '%s' https://review.openstack.org/#q,%s,n,z" % (
            time.strftime(TIME_FORMAT, self.date),
            self.subject, self.change_id))

    def __le__(self, other):
        # self < other
        return self.date < other.date


def main():
    parser = argparse.ArgumentParser(description='list most recent comment by '
                                     'reviewer')
    parser.add_argument('-n', '--name',
                        default="Elastic Recheck",
                        help='unique gerrit name of the reviewer')
    # name = "VMware NSX CI"
    args = parser.parse_args()
    # Include review messages in query
    query = ("https://review.openstack.org/changes/?q=reviewer:\"%s\"&"
             "o=MESSAGES" % (args.name))
    r = requests.get(query)
    changes = json.loads(r.text[4:])

    comment = None
    for change in changes:
        date = last_comment(change, args.name)
        current_comment = Comment(date, change['change_id'],
                                  change['subject'])
        if not comment or comment < current_comment:
            comment = copy.copy(current_comment)
    print "last comment from '%s'" % args.name
    print comment


def last_comment(change, name):
    """Return most recent timestamp for comment by name."""
    last_date = None
    for message in change['messages']:
        if 'author' in message and message['author']['name'] == name:
            date = message['date']
            # https://review.openstack.org/Documentation/rest-api.html#timestamp
            # drop nanoseconds
            date = date.split('.')[0]
            date = time.strptime(date, TIME_FORMAT)
            if date > last_date:
                last_date = date
    return last_date


if __name__ == "__main__":
    main()

@@ -95,20 +95,8 @@
 rse%0A
-import copy%0A
 impo
@@ -213,25 +213,22 @@
 one%0A    
-change_id
+number
  = None%0A
@@ -276,25 +276,22 @@
 , date, 
-change_id
+number
 , subjec
@@ -376,29 +376,23 @@
 elf.
-change_id = change_id
+number = number
 %0A   
@@ -462,21 +462,16 @@
 urn (%22%25s
- '%25s'
  https:/
@@ -496,17 +496,16 @@
 org/
-#q,%25s,n,z
+%25s '%25s' 
 %22 %25 
@@ -578,31 +578,28 @@
 elf.
-subject, self.change_id
+number, self.subject
 ))%0A%0A
@@ -689,16 +689,109 @@
 r.date%0A%0A
+    def __repr__(self):%0A        # for sorting%0A        return repr((self.date, self.number))%0A%0A
 %0Adef mai
@@ -1371,23 +1371,22 @@
  comment
+s
  = 
-None
+%5B%5D
 %0A    for
@@ -1465,25 +1465,23 @@
    c
-urrent_comment = 
+omments.append(
 Comm
@@ -1502,23 +1502,19 @@
 ge%5B'
-change_id
+_number
 '%5D,%0A
-  
     
@@ -1563,61 +1563,134 @@
 t'%5D)
+)%0A
 %0A    
-    if not comment or comment %3C current_
+COUNT = 10%0A    print %22last %25s comments from '%25s'%22 %25 (COUNT, args.name)%0A    for i, comment in enumerate(sorted(
 comment
-:
+s,
 %0A   
@@ -1702,109 +1702,170 @@
     
+                           key=lambda 
 comment
- = copy.copy(current_comment)%0A    print %22last comment from '%25s'%22 %25 args.name%0A
+: comment.date,%0A                                       reverse=True)%5B0:COUNT%5D):%0A   
     
+ 
 print
+ %22%5B%25d%5D %25s%22 %25 (i,
  comment
 %0A%0A%0Ad
@@ -1860,16 +1860,17 @@
  comment
+)
 %0A%0A%0Adef l

Print last 10 comments