From c6aaba574fedb9884b1390d183ec734c6254bc6d Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Jul 26 2016 12:59:49 +0000
Subject: [PATCH 1/6] Add a widget to list tickets on a fedorahosted.org trac instance


---

diff --git a/hubs/widgets/__init__.py b/hubs/widgets/__init__.py
index a365c3d..323aa70 100755
--- a/hubs/widgets/__init__.py
+++ b/hubs/widgets/__init__.py
@@ -15,6 +15,7 @@ from hubs.widgets import github_pr
 from hubs.widgets import pagureissues
 from hubs.widgets import githubissues
 from hubs.widgets import bugzilla
+from hubs.widgets import fhosted
 
 from hubs.widgets.workflow import pendingacls
 from hubs.widgets.workflow import updates2stable
@@ -39,6 +40,7 @@ registry = {
     'pagureissues': pagureissues,
     'githubissues': githubissues,
     'bugzilla': bugzilla,
+    'fedorahosted': fhosted,
 
     'workflow.pendingacls': pendingacls,
     'workflow.updates2stable': updates2stable,
diff --git a/hubs/widgets/fhosted.py b/hubs/widgets/fhosted.py
new file mode 100644
index 0000000..4f99f71
--- /dev/null
+++ b/hubs/widgets/fhosted.py
@@ -0,0 +1,56 @@
+from hubs.widgets.chrome import panel
+from hubs.hinting import hint, prefixed as _
+from hubs.widgets.base import argument
+from hubs.widgets import templating
+import hubs.validators as validators
+import requests
+
+chrome = panel("Fedorahosted: Open Tickets")
+template = templating.environment.get_template('templates/fedorahosted.html')
+position = 'right'
+
+from xmlrpclib import ServerProxy
+
+
+@argument(name="project",
+          default=None,
+          validator=validators.github_organization,
+          help="Name of the trac instance on fedorahosted.org")
+@argument(name="n_tickets",
+          default=4,
+          validator=validators.integer,
+          help="The number of tickets to display.")
+def data(session, widget, project, n_tickets=4):
+    n_tickets = int(n_tickets)
+    url = 'https://fedorahosted.org/%s/rpc' % project
+    filters = 'status=accepted&status=assigned&status=new&status=reopened'\
+        '&col=id&col=summary&col=status&col=owner&col=type&col=priority'\
+        '&col=milestone&col=changetime&order=changetime'
+    server = ServerProxy(url)
+    tickets = server.ticket.query(filters)
+    output = []
+    total_tickets = len(tickets)
+    for idx, ticket in enumerate(tickets):
+        ticket = server.ticket.get(ticket)
+        data = ticket[3]
+        data['id'] = ticket[0]
+        data['short_summary'] = data['summary'][:45]
+        output.append(data)
+        if idx + 1 >= n_tickets:
+            break
+
+    return dict(
+        project=project,
+        tickets=output,
+        total_tickets=total_tickets,
+    )
+
+
+@hint(topics=[_('trac.ticket.update'), _('trac.ticket.new')])
+def should_invalidate(message, session, widget):
+    project = widget.config.get('project', '')
+    url = 'https://fedorahosted.org/%s/' % project
+    if '.trac.ticket' in message['topic']:
+        if message['msg']['instance']['base_url'] == url:
+            return True
+    return False
diff --git a/hubs/widgets/templates/fedorahosted.html b/hubs/widgets/templates/fedorahosted.html
new file mode 100644
index 0000000..4908362
--- /dev/null
+++ b/hubs/widgets/templates/fedorahosted.html
@@ -0,0 +1,41 @@
+<a class="btn btn-success" target="_blank"
+    href="https://fedorahosted.org/{{ project }}/report/1">
+  All Issues
+</a>
+<hr/>
+
+<ul class="media-list">
+  {% for issue in tickets %}
+    <li class="media">
+      <div class="media-left">
+        <a  href="https://fedorahosted.org/{{ project }}/ticket/{{ issue['id'] }}"
+            target="_blank">
+          {{ project }}
+        <span class="label label-default">#{{ issue['id'] }}</span>
+        </a>
+      </div>
+      <div class="media-body">
+        <h4 class="media-heading">
+          <span title="{{ issue['summary'] }}">
+            {{ issue['short_summary'] }} {% if
+              issue['summary'] | length > 45 %} ... {% endif %}
+          </span>
+        </h4>
+        Opened by:
+            {{ issue['reporter'] }}
+        --
+        {% if issue['owner'] %}
+          Assigned to:
+            {{issue['owner'] }}
+        {% else %}
+          <span class="text-muted">Unassigned</span>
+        {% endif %}
+      </div>
+    </li>
+  {% endfor %}
+  {% if total_tickets > tickets | length %}
+  <li>
+    And {{ total_tickets - tickets | length }} more ...
+  </li>
+  {% endif %}
+</ul>

From 5e1c8fb716d79119f63946707807d4ed398d8512 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Jul 26 2016 13:00:14 +0000
Subject: [PATCH 2/6] Small fix to ensure the number of meetings provided is a integer


---

diff --git a/hubs/widgets/meetings.py b/hubs/widgets/meetings.py
index 071496a..8c05744 100755
--- a/hubs/widgets/meetings.py
+++ b/hubs/widgets/meetings.py
@@ -69,6 +69,7 @@ position = 'both'
           validator=validators.integer,
           help="The number of meetings to display.")
 def data(session, widget, calendar, n_meetings=4):
+    n_meetings = int(n_meetings)
     base = 'https://apps.fedoraproject.org/calendar/api/meetings/?calendar=%s'
     url = base % calendar
     response = requests.get(url).json()

From 14e16f00448e90e243da64df7a81a5a9229ad4d7 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Jul 26 2016 13:14:49 +0000
Subject: [PATCH 3/6] Drop the project name from the link


---

diff --git a/hubs/widgets/templates/fedorahosted.html b/hubs/widgets/templates/fedorahosted.html
index 4908362..7c855b9 100644
--- a/hubs/widgets/templates/fedorahosted.html
+++ b/hubs/widgets/templates/fedorahosted.html
@@ -10,7 +10,6 @@
       <div class="media-left">
         <a  href="https://fedorahosted.org/{{ project }}/ticket/{{ issue['id'] }}"
             target="_blank">
-          {{ project }}
         <span class="label label-default">#{{ issue['id'] }}</span>
         </a>
       </div>

From d18864c268d481d7089ddb8bb9bdd9ed9c3c0e8f Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Jul 26 2016 13:14:59 +0000
Subject: [PATCH 4/6] Small fix to properly show the assignee of a PR in pagure


---

diff --git a/hubs/widgets/templates/pagure_pr.html b/hubs/widgets/templates/pagure_pr.html
index 5508cb7..94b2f23 100644
--- a/hubs/widgets/templates/pagure_pr.html
+++ b/hubs/widgets/templates/pagure_pr.html
@@ -25,7 +25,7 @@
           Assigned to:
           <a href="https://pagure.io/user/{{ pr['pr_assignee'] }}"
               target="_blank">
-            {{ pr['pr_assignee'] }}
+            {{ pr['pr_assignee']['name'] }}
           </a>
         {% else %}
           <span class="text-muted">Unassigned</span>

From b3049f8dfb1565e9e07276dd2a798d8374538cbd Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Jul 26 2016 14:00:10 +0000
Subject: [PATCH 5/6] Show an error message upon error :)


---

diff --git a/hubs/widgets/fhosted.py b/hubs/widgets/fhosted.py
index 4f99f71..0c08928 100644
--- a/hubs/widgets/fhosted.py
+++ b/hubs/widgets/fhosted.py
@@ -26,8 +26,14 @@ def data(session, widget, project, n_tickets=4):
     filters = 'status=accepted&status=assigned&status=new&status=reopened'\
         '&col=id&col=summary&col=status&col=owner&col=type&col=priority'\
         '&col=milestone&col=changetime&order=changetime'
-    server = ServerProxy(url)
-    tickets = server.ticket.query(filters)
+    try:
+        server = ServerProxy(url)
+        tickets = server.ticket.query(filters)
+    except:
+        return dict(
+            error='Invalid or wrongly configured project'
+        )
+
     output = []
     total_tickets = len(tickets)
     for idx, ticket in enumerate(tickets):
diff --git a/hubs/widgets/templates/fedorahosted.html b/hubs/widgets/templates/fedorahosted.html
index 7c855b9..ad22b57 100644
--- a/hubs/widgets/templates/fedorahosted.html
+++ b/hubs/widgets/templates/fedorahosted.html
@@ -1,3 +1,6 @@
+{% if error %}
+<p>{{ error }}</p>
+{% else %}
 <a class="btn btn-success" target="_blank"
     href="https://fedorahosted.org/{{ project }}/report/1">
   All Issues
@@ -38,3 +41,4 @@
   </li>
   {% endif %}
 </ul>
+{% endif %}

From 770b3ad9da7edd0c62e348989ac09c514c0591ca Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Jul 26 2016 14:09:46 +0000
Subject: [PATCH 6/6] Fix the validator used in the fedorahosted widget


---

diff --git a/hubs/validators.py b/hubs/validators.py
index 791f3fe..c0385d3 100755
--- a/hubs/validators.py
+++ b/hubs/validators.py
@@ -27,7 +27,7 @@ def username(session, value):
 
 def github_organization(session, value):
     # TODO -- implement this.
-    return True
+    return value
 
 
 def fmn_context(session, value):
@@ -39,3 +39,7 @@ def fmn_context(session, value):
 
 def pagure_repo(session, value):
     return value
+
+def fedorahosted_project(session, value):
+    # TODO -- implement this.
+    return value
diff --git a/hubs/widgets/fhosted.py b/hubs/widgets/fhosted.py
index 0c08928..6a54827 100644
--- a/hubs/widgets/fhosted.py
+++ b/hubs/widgets/fhosted.py
@@ -14,7 +14,7 @@ from xmlrpclib import ServerProxy
 
 @argument(name="project",
           default=None,
-          validator=validators.github_organization,
+          validator=validators.fedorahosted_project,
           help="Name of the trac instance on fedorahosted.org")
 @argument(name="n_tickets",
           default=4,

