how to integrate Jira webhook in flask for getting created issues?

  flask, jira, python, python-jira, webhooks

I am new to Jira and webhooks. I am trying to write a python script that should invoke the Jira webhook when a new issue is created and it should return me the issue key, description, summary, etc. I have created the webhook in Jira system settings under webhook and trying to use in my code but I am not able to get any response. Below is what I have tried:

import collections

from flask import abort, request, jsonify, make_response


webhook_url = "https://xxxxxx.com/rest/webhooks/webhook1"

class JiraWebhook(object):
    def __init__(self, app, endpoint="/jira"):
        app.add_url_rule(endpoint, view_func=self.handle, methods=["POST"])
        self._hooks = collections.defaultdict(list)

    def hook(self, event_type="jira:issue_created"):
        def decorator(f):
            self._hooks[event_type].append(f)
            return f
        return decorator

    def _post_received(self):
        data = request.get_json()
        if data is None:
            abort(400)
        event_type = data.get("webhookEvent")
        if event_type is None:
            abort(400)
        for hook in self._hooks[event_type]:
            hook(data)

        return "", 200

Here:

   from flask import Flask
import app

app = Flask(__name__)
jw = JiraWebhook(app)



@app.route('/')
def webhook_service():
    return "Webhook Service"

@jw.hook("jira:issue_created")
def issue_created(payload):
    print("Issue created")
    print(payload)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port = 80)

I am not able to figure it out that:

  1. How can I connect to Jira Webhook so everytime an issue is created, it will print me the details?
  2. Where do I need to define my Jira credentails?

Can anyone guide me? Sorry, If This is a very basic question. Thanks in advance.

Source: Python Questions

LEAVE A COMMENT