From d5aae819bfd1ca6bf1e31241b04b9ab7713b6c82 Mon Sep 17 00:00:00 2001 From: Ryan Lerch Date: Jan 24 2018 10:26:18 +0000 Subject: add repositories widget This adds a new widget called repositories. It is designed to be shown on the team hub type only, and shows a list of the repos set up in the team hubs config (in the dev platforms section) It also shows how many open bugs and open PRs there are for each of the repos set up. --- diff --git a/hubs/default_config.py b/hubs/default_config.py index 1ee9a73..6304436 100644 --- a/hubs/default_config.py +++ b/hubs/default_config.py @@ -71,6 +71,7 @@ WIDGETS = [ 'hubs.widgets.my_hubs:MyHubs', 'hubs.widgets.pagure_pr:PagurePRs', 'hubs.widgets.pagureissues:PagureIssues', + 'hubs.widgets.repositories:Repositories', 'hubs.widgets.sticky:Sticky', 'hubs.widgets.workflow.updates2stable:Updates2Stable', ] diff --git a/hubs/widgets/repositories/__init__.py b/hubs/widgets/repositories/__init__.py new file mode 100644 index 0000000..93087b3 --- /dev/null +++ b/hubs/widgets/repositories/__init__.py @@ -0,0 +1,110 @@ +from __future__ import unicode_literals + +import requests + +from hubs.utils import pagure +from hubs.widgets.base import Widget +from hubs.widgets.view import RootWidgetView +from hubs.widgets.caching import CachedFunction + + +class Repositories(Widget): + + name = "repositories" + label = "Repositories" + position = "right" + hub_types = ["team"] + + +class BaseView(RootWidgetView): + + def get_context(self, instance, *args, **kwargs): + repos = GetRepos(instance) + return dict( + repos=repos(), + ) + + +class GetRepos(CachedFunction): + + def execute(self): + repos_list = [] + + for repo in self.instance.hub.config["pagure"]: + issues_url = '/'.join([pagure.PAGURE_URL, "api", + "0", repo, "issues"]) + issues_response = requests.get(issues_url) + issues_data = issues_response.json() + + pr_url = '/'.join([pagure.PAGURE_URL, "api", + "0", repo, "pull-requests"]) + pr_response = requests.get(pr_url) + pr_data = pr_response.json() + + out = {"repo_name": repo, + "repo_url": "https://pagure.io/"+repo, + "issues_count": issues_data['total_issues'], + "pr_count": pr_data['total_requests'], + "type": "pagure", + } + repos_list.append(out) + + for repo in self.instance.hub.config["github"]: + issues_url = ':'.join( + [('https://api.github.com/search/issues?' + 'q=""+state:open+type:issue+repo'), repo]) + issues_response = requests.get(issues_url) + issues_count = issues_response.json()['total_count'] + pr_url = ':'.join( + [('https://api.github.com/search/issues?' + 'q=""+state:open+type:pr+repo'), repo]) + pr_response = requests.get(pr_url) + pr_count = pr_response.json()['total_count'] + + out = {"repo_name": repo, + "repo_url": "https://github.com/"+repo, + "issues_count": issues_count, + "pr_count": pr_count, + "type": "github" + } + repos_list.append(out) + + return repos_list + + def should_invalidate(self, message): + pagure_repos = self.instance.hub.config["pagure"] + github_repos = self.instance.hub.config["github"] + if ( + ".github.issue." in message["topic"] or + ".github.pull_request.opened" in message["topic"] or + ".github.pull_request.closed" in message["topic"]): + try: + repo = message['msg']['repository']['fullname'] + except KeyError: + return False + return (repo in github_repos) + elif (".pagure.issue." in message["topic"]): + try: + repo = message['msg']['project']['name'] + except KeyError: + return False + return (repo in pagure_repos) + elif ( + ".pagure.pull-request.new" not in message["topic"] or + ".pagure.pull-request.closed" not in message["topic"]): + try: + repo = message['msg']['pullrequest']['project']['name'] + except KeyError: + return False + return (repo in pagure_repos) + elif message["topic"].endswith('.hubs.hub.updated'): + if ( + "github" in message["msg"]["changed_keys"] or + "pagure" in message["msg"]["changed_keys"]): + hub_id = message["msg"]["hub_id"] + if hub_id == self.instance.hub.id: + return True + else: + return False + else: + return False diff --git a/hubs/widgets/repositories/templates/root.html b/hubs/widgets/repositories/templates/root.html new file mode 100644 index 0000000..0a81e9a --- /dev/null +++ b/hubs/widgets/repositories/templates/root.html @@ -0,0 +1,23 @@ + +{% for repo in repos %} + + + + + +{% endfor %} +
{{repo["repo_name"]}}{{repo["issues_count"]}}{{repo["pr_count"]}}
+ + \ No newline at end of file