def on_buildbot_event(data, message, dry_run):
    """Act upon buildbot events."""
    # Pulse gives us a job_id and a job_guid, we need request_id.
    repo_name = data['project']
    job_id = data['job_id']
    request_id = get_request_id_from_job_id(repo_name, job_id)
    action = data['action']
    status = None

    # Re-trigger action
    if action == 'retrigger':
        make_retrigger_request(repo_name, request_id, dry_run=dry_run)
        if not dry_run:
            status = 'Retrigger request sent'
        else:
            status = 'Dry-mode, nothing was retriggered'
    # Cancel action
    elif action == 'cancel':
        make_cancel_request(repo_name, request_id, dry_run=dry_run)
        if not dry_run:
            status = 'Cancel request sent'
        else:
            status = 'Dry-run mode, nothing was cancelled'
    # Send a pulse message showing what we did
    message_sender = MessageHandler()
    pulse_message = {
        'job_id': job_id,
        'request_id': request_id,
        'action': action,
        'requester': data['requester'],
        'status': status}
    routing_key = '{}.{}'.format(repo_name, action)
    message_sender.publish_message(pulse_message, routing_key)
    # We need to ack the message to remove it from our queue
    message.ack()
    def test_call_without_dry_run(self, get_credentials, delete):
        """trigger_arbitrary_job should call requests.post."""
        buildapi.make_cancel_request("repo", "1234567", dry_run=False)

        # We expect that make_cancel_request will call requests.delete
        # once with the following arguments
        delete.assert_called_once_with('%s/%s/request/%s' %
                                       (buildapi.HOST_ROOT, "repo", "1234567"),
                                       auth=get_credentials())
    def test_call_without_dry_run(self, get_credentials, delete):
        """trigger_arbitrary_job should call requests.post."""
        buildapi.make_cancel_request("repo", "1234567", dry_run=False)

        # We expect that make_cancel_request will call requests.delete
        # once with the following arguments
        delete.assert_called_once_with(
            '%s/%s/request/%s' % (buildapi.HOST_ROOT, "repo", "1234567"),
            auth=get_credentials())
 def test_call_with_dry_run(self, get_credentials, delete):
     """make_cancel_request should return None when dry_run is True."""
     self.assertEquals(
         buildapi.make_cancel_request("repo", "1234567", dry_run=True),
         None)
     # make_cancel_request should not call requests.delete when dry_run is True
     assert delete.call_count == 0
 def cancel(self, uuid, *args, **kwargs):
     return buildapi.make_cancel_request(repo_name=kwargs['repo_name'],
                                         request_id=uuid,
                                         *args,
                                         **kwargs)
 def cancel(self, uuid, *args, **kwargs):
     return buildapi.make_cancel_request(
         repo_name=kwargs['repo_name'],
         request_id=uuid,
         *args,
         **kwargs)
 def test_call_with_dry_run(self, get_credentials, delete):
     """make_cancel_request should return None when dry_run is True."""
     self.assertEquals(
         buildapi.make_cancel_request("repo", "1234567", dry_run=True), None)
     # make_cancel_request should not call requests.delete when dry_run is True
     assert delete.call_count == 0