Skip to content

Commit

Permalink
[ADD] webhook: Add standard webhook odoo module
Browse files Browse the repository at this point in the history
  • Loading branch information
moylop260 authored and yajo committed Sep 1, 2017
1 parent 2db4c01 commit c3436ec
Show file tree
Hide file tree
Showing 13 changed files with 648 additions and 0 deletions.
143 changes: 143 additions & 0 deletions webhook/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

=======
Webhook
=======

Module to receive .. _global webhooks: https://en.wikipedia.org/wiki/Webhook events.
This module invoke methods to process webhook events.


Configuration
=============

You will need create a new module to add your logic to process the events with methods called:
*def run_CONSUMER_EVENT\**
Example with gihub consumer and push event.

.. code-block:: python
@api.one
def run_github_push_task(self):
# You will have all request data in
# variable: self.env.request
pass
Where CONSUMER is the name of you webhook consumer. e.g. github (Extract from field *name* of *webhook* model)
Where EVENT is the name of the event from webhook *request* data.
Where *\** is your particular method to process this event.

To configure a new webhook you need add all ip or subnet address (with *ip/integer*) owned by your webhook consumer in webhook.address model as data.

Example with github:

.. code-block:: xml
<!--webhook github data of remote address-->
<record model="webhook.address" id="webhook_address_github">
<field name="name">192.30.252.0/22</field>
<field name="webhook_id" ref="webhook_github"/>
</record>
You need to add a python code to extract event name from webhook request info into `python_code_get_event` field of webhook model.
You can get all full data of request webhook from variable `request`
Example with github:

.. code-block:: xml
<!--webhook github data-->
<record model="webhook" id="webhook_github">
<field name="name">github</field>
<field name="python_code_get_event">request.httprequest.headers.get('X-Github-Event')</field>
</record>
Full example of create a new webhook configuration data.

.. code-block:: xml
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<!--webhook github data-->
<record model="webhook" id="webhook_github">
<field name="name">github</field>
<field name="python_code_get_event">request.httprequest.headers.get('X-Github-Event')</field>
</record>
<!--webhook github data of remote address-->
<record model="webhook.address" id="webhook_address_github">
<field name="name">192.30.252.0/22</field>
<field name="webhook_id" ref="webhook_github"/>
</record>
</data>
</openerp>
.. figure:: path/to/local/image.png
:alt: alternative description
:width: 600 px

Usage
=====

To use this module, you need to:

#. Go to your customer webhook configuration from 3rd-party applications
and use the odoo webhook url HOST/webhook/NAME_WEBHOOK

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/{repo_id}/{branch}

.. repo_id is available in https://github.com/OCA/maintainer-tools/blob/master/tools/repos_with_ids.txt
.. branch is "8.0" for example
Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/server-tools/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smash it by providing detailed and welcomed feedback.

Credits
=======

Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.

Contributors
------------

* Moisés López <[email protected]>

Funders
-------

The development of this module has been financially supported by:

* Vauxoo

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.
7 changes: 7 additions & 0 deletions webhook/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Vauxoo - https://www.vauxoo.com/
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
from . import controllers
from . import tests
30 changes: 30 additions & 0 deletions webhook/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Vauxoo - https://www.vauxoo.com/
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': 'Webhook',
'version': '8.0.1.0.0',
'author': 'Vauxoo, Odoo Community Association (OCA)',
'category': 'Server Tools',
'website': 'https://www.vauxoo.com',
'license': 'AGPL-3',
'depends': [
'web',
],
'external_dependencies': {
'python': [
'ipaddress',
'requests',
],
},
'data': [
'security/ir.model.access.csv',
'views/webhook_views.xml',
'data/webhook_data.xml',
],
'demo': [
'demo/webhook_demo.xml',
],
'installable': True,
'auto_install': False,
}
5 changes: 5 additions & 0 deletions webhook/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Vauxoo - https://www.vauxoo.com/
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import main
44 changes: 44 additions & 0 deletions webhook/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Vauxoo - https://www.vauxoo.com/
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import pprint

from openerp.addons.web import http
from openerp.http import request
from openerp import SUPERUSER_ID, exceptions
from openerp.tools.translate import _


class WebhookController(http.Controller):

@http.route(['/webhook/<webhook_name>'], type='json',
auth='none', method=['POST'])
def webhook(self, webhook_name, **post):
'''
:params string webhook_name: Name of webhook to use
Webhook odoo controller to receive json request and send to
driver method.
You will need create your webhook with http://0.0.0.0:0000/webhook
NOTE: Important use --db-filter params in odoo start.
'''
# Deprecated by webhook_name dynamic name
# webhook = webhook_registry.search_with_request(
# cr, SUPERUSER_ID, request, context=context)
webhook = request.env['webhook'].with_env(
request.env(user=SUPERUSER_ID)).search(
[('name', '=', webhook_name)], limit=1)
# TODO: Add security by secret string or/and ip consumer
if not webhook:
remote_addr = ''
if hasattr(request, 'httprequest'):
if hasattr(request.httprequest, 'remote_addr'):
remote_addr = request.httprequest.remote_addr
raise exceptions.ValidationError(_(
'webhook consumer [%s] from remote address [%s] '
'not found jsonrequest [%s]' % (
webhook_name,
remote_addr,
pprint.pformat(request.jsonrequest)[:450]
)))
webhook.run_webhook(request)
16 changes: 16 additions & 0 deletions webhook/data/webhook_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>

<!--webhook github data-->
<record model="webhook" id="webhook_github">
<field name="name">github</field>
<field name="python_code_get_event">request.httprequest.headers.get('X-Github-Event')</field>
</record>
<record model="webhook.address" id="webhook_address_github">
<field name="name">192.30.252.0/22</field>
<field name="webhook_id" ref="webhook_github"/>
</record>

</data>
</openerp>
17 changes: 17 additions & 0 deletions webhook/demo/webhook_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version='1.0' encoding='UTF-8'?>
<openerp>
<data noupdate="1">

<record id="webhook_test" model="webhook">
<field name="name">wehook_test</field>
<field name="python_code_get_event">request.httprequest.headers.get("X-Webhook-Test-Event")</field>
<field name="python_code_get_ip">request.httprequest.headers.get("X-Webhook-Test-Address")</field>
</record>

<record id="webhook_address_localhost" model="webhook.address">
<field name="name">127.0.0.1</field>
<field name="webhook_id" ref="webhook_test"/>
</record>

</data>
</openerp>
5 changes: 5 additions & 0 deletions webhook/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Vauxoo - https://www.vauxoo.com/
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import webhook
Loading

0 comments on commit c3436ec

Please sign in to comment.