#42 Added consumer for Github's Pull Requests
Merged 6 years ago by tflink. Opened 6 years ago by jskladan.

@@ -58,3 +58,8 @@ 

      message_type: CloudCompose

    do:

      - {tasks: [fedora-cloud-tests]}

+ 

+ - when:

+     message_type: GitHubPullRequestOpened

But... I don't get it. This will run task-mtf on all github PRs that fedmsg watches! Or am I mistaken?

We want to run mtf just for specific repositories. According to his comments, it should be projects under https://github.com/container-images/*. Shouldn't we add a regex line to match those?

+   do:

+     - {tasks: [task-mtf]}

This should be task-mtf-containers (https://pagure.io/task-mtf-containers) according to the ticket.

@@ -5,4 +5,5 @@ 

      'taskotron.paguregitreceivejobconsumer.enabled': True,

      'taskotron.cloudcomposecompletedconsumer.enabled': True,

      'taskotron.modulebuilddonejobconsumer.enabled': True,

+     'taskotron.githubpropenedjobconsumer.enabled': True,

  }

@@ -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

file modified
+1
@@ -19,6 +19,7 @@ 

      '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(

@@ -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

Pull-Request has been merged by tflink

6 years ago

But... I don't get it. This will run task-mtf on all github PRs that fedmsg watches! Or am I mistaken?

We want to run mtf just for specific repositories. According to his comments, it should be projects under https://github.com/container-images/*. Shouldn't we add a regex line to match those?

This should be task-mtf-containers (https://pagure.io/task-mtf-containers) according to the ticket.

@kparal this will, in fact, run nothing, on its own. This is Trigger's code, not deployment.

Bare in mind, that there is a difference between the trigger code, tests and examples, and the way trigger is actually configured in ansible and deployed.

The "enable this on our servers" is unrelated to the trigger_rules.yml.example file. If for nothing else, then for the fact we don't have either task-mtf-containers nor task-mtf among the mirrored tasks on the buildmasters...

I understand where you are coming from, but the comments are not really related to this particular PR.

Right, I got confused. Thanks.