From ae62e9ec82a15dbd09be86bc263a256730a07dc6 Mon Sep 17 00:00:00 2001
From: Josef Skladanka <jskladan@redhat.com>
Date: Oct 11 2017 14:32:22 +0000
Subject: Added consumer for Github's Pull Requests


---

diff --git a/conf/trigger_rules.yml.example b/conf/trigger_rules.yml.example
index 59e2e3a..c11b09f 100644
--- a/conf/trigger_rules.yml.example
+++ b/conf/trigger_rules.yml.example
@@ -58,3 +58,8 @@
     message_type: CloudCompose
   do:
     - {tasks: [fedora-cloud-tests]}
+
+- when:
+    message_type: GitHubPullRequestOpened
+  do:
+    - {tasks: [task-mtf]}
diff --git a/fedmsg.d/taskotron-trigger.py b/fedmsg.d/taskotron-trigger.py
index 89ef72a..8c2afd0 100644
--- a/fedmsg.d/taskotron-trigger.py
+++ b/fedmsg.d/taskotron-trigger.py
@@ -5,4 +5,5 @@ config = {
     'taskotron.paguregitreceivejobconsumer.enabled': True,
     'taskotron.cloudcomposecompletedconsumer.enabled': True,
     'taskotron.modulebuilddonejobconsumer.enabled': True,
+    'taskotron.githubpropenedjobconsumer.enabled': True,
 }
diff --git a/jobtriggers/github_pull_request_opened.py b/jobtriggers/github_pull_request_opened.py
new file mode 100644
index 0000000..250e2e8
--- /dev/null
+++ b/jobtriggers/github_pull_request_opened.py
@@ -0,0 +1,45 @@
+import koji
+import fedmsg
+import fedmsg.encoding
+import fedmsg.consumers
+
+from . import config, utils
+from .jobtrigger import JobTrigger
+from . import exceptions as exc
+
+MESSAGE_TYPE = "GitHubPullRequestOpened"
+ITEM_TYPE = "pull_request"
+
+
+class GithubPullRequestOpenedJobTrigger(JobTrigger):
+
+    def process(self, msg):
+
+        data = {
+            "_msg": msg,
+            "message_type": MESSAGE_TYPE,
+            "item": msg['msg']['pull_request']['html_url'],
+            "item_type": ITEM_TYPE,
+            "git_host": "github",
+            "repo_name": msg['msg']['repository']['full_name'],
+        }
+        return data
+
+
+class GithubPullRequestOpenedJobConsumer(fedmsg.consumers.FedmsgConsumer):
+    topic = "org.fedoraproject.%s.github.pull_request.opened" % config.deployment_type
+    jsonify = False
+    config_key = 'taskotron.githubpropenedjobconsumer.enabled'
+
+    def __init__(self, *args, **kw):
+        super(GithubPullRequestOpenedJobConsumer, self).__init__(*args, **kw)
+        self.trigger = GithubPullRequestOpenedJobTrigger(self.log)
+
+    def consume(self, message):
+        msg = fedmsg.encoding.loads(message.body)
+        try:
+            data = self.trigger.process(msg)
+            self.trigger.do_trigger(data)
+        except exc.TriggerMsgError, e:
+            self.log.debug(e)
+            return
diff --git a/setup.py b/setup.py
index 0173ae4..1e054b9 100644
--- a/setup.py
+++ b/setup.py
@@ -19,6 +19,7 @@ moksha_consumer = (
     'paguregitreceivejobconsumer = jobtriggers.pagure_git_received:PagureGitReceiveJobConsumer',
     'cloudcomposejobconsumer = jobtriggers.cloud_compose_complete_msg:CloudComposeCompletedConsumer',
     'modulebuilddonejobconsumer = jobtriggers.module_build_msg:ModuleBuildDoneJobConsumer',
+    'githubpropenedjobconsumer = jobtriggers.github_pull_request_opened:GithubPullRequestOpenedJobConsumer',
 )
 
 setup(
diff --git a/testing/test_github_pr_opened_trigger.py b/testing/test_github_pr_opened_trigger.py
new file mode 100644
index 0000000..0bdfdaa
--- /dev/null
+++ b/testing/test_github_pr_opened_trigger.py
@@ -0,0 +1,95 @@
+import pytest
+from dingus import Dingus
+from munch import Munch
+from copy import deepcopy
+import fedmsg.encoding
+import fedmsg.consumers
+
+from jobtriggers import github_pull_request_opened
+
+
+@pytest.mark.usefixtures('prepare')
+class TestKojiTagChangedJobConsumer():
+
+    @pytest.fixture
+    def prepare(self, monkeypatch):
+
+        self.ref_pr_url = "https://github.com/fedora-infra/bodhi/pull/1882"
+        self.ref_type = 'pull_request'
+        self.ref_git_host = "github"
+        self.ref_repo_name = "fedora-infra/bodhi"
+        self.ref_tasks = ['foo', 'bar']
+        self.ref_validarches = ['x86_64', 'i386']
+
+        self.ref_message = self._create_msg(self.ref_pr_url, self.ref_repo_name)
+
+        self.ref_data = {
+            "_msg": {},
+            "message_type": "GitHubPullRequestOpened",
+            "item": self.ref_pr_url,
+            "item_type": self.ref_type,
+            "git_host": self.ref_git_host,
+            "repo_name": self.ref_repo_name,
+        }
+
+        stub_hub = Munch(config=Munch(get=0))
+        self.helper = github_pull_request_opened.GithubPullRequestOpenedJobConsumer(stub_hub)
+
+        self.helper.trigger.runner = Dingus()
+        github_pull_request_opened.config.trigger_rules_template = """---
+- do:
+  - {tasks: %s}
+  when: {message_type: GitHubPullRequestOpened}
+- do:
+  - {tasks: [%s]}
+  when: {message_type: GitHubPullRequestOpened, git_host: github}
+- do:
+  - {tasks: [%s]}
+  when: {message_type: GitHubPullRequestOpened, git_host: pagure}
+""" % (self.ref_tasks, self.ref_tasks[0], self.ref_tasks[1])
+
+        github_pull_request_opened.config.valid_arches = self.ref_validarches
+        github_pull_request_opened.config.job_logging = False
+
+    def _create_msg(self, ref_pr_url, ref_repo_name):
+        return Munch(body='{"i": 1,\
+                            "msg": {\
+                                "pull_request": {"html_url": "%s"},\
+                                "repository": {"full_name": "%s"}\
+                            },\
+                            "timestamp": 1359603469.21164,\
+                            "topic": "org.fedoraproject.prod.buildsys.tag",\
+                            "username": "apache"}' %
+                        (ref_pr_url, ref_repo_name))
+
+    def test_consume(self):
+        self.helper.consume(self.ref_message)
+
+        runner_calls = self.helper.trigger.runner.trigger_job.calls()
+
+        assert len(runner_calls) == 6
+
+        assert runner_calls[0][1] == (self.ref_pr_url, self.ref_type,
+                                      self.ref_tasks[0], self.ref_validarches[0])
+        assert runner_calls[1][1] == (self.ref_pr_url, self.ref_type,
+                                      self.ref_tasks[0], self.ref_validarches[1])
+        assert runner_calls[2][1] == (self.ref_pr_url, self.ref_type,
+                                      self.ref_tasks[1], self.ref_validarches[0])
+        assert runner_calls[3][1] == (self.ref_pr_url, self.ref_type,
+                                      self.ref_tasks[1], self.ref_validarches[1])
+        assert runner_calls[4][1] == (self.ref_pr_url, self.ref_type,
+                                      self.ref_tasks[0], self.ref_validarches[0])
+        assert runner_calls[5][1] == (self.ref_pr_url, self.ref_type,
+                                      self.ref_tasks[0], self.ref_validarches[1])
+
+    def test_trigger_data(self):
+        trigger = github_pull_request_opened.GithubPullRequestOpenedJobTrigger(Dingus())
+        msg = fedmsg.encoding.loads(self.ref_message.body)
+        data = trigger.process(msg)
+
+        assert data['_msg'] == msg
+        assert data['message_type'] == github_pull_request_opened.MESSAGE_TYPE
+        assert data['item_type'] == github_pull_request_opened.ITEM_TYPE
+        assert data['item'] == self.ref_pr_url
+        assert data['git_host'] == 'github'
+        assert data['repo_name'] == self.ref_repo_name

