Streamlining Collaboration: Unifying GitHub and Jira with Python Flask ๐ŸŒ๐Ÿค

Streamlining Collaboration: Unifying GitHub and Jira with Python Flask ๐ŸŒ๐Ÿค

ยท

3 min read

In this hands-on project, I explored the seamless integration of GitHub and Jira using Python Flask. The project is divided into two parts, each unraveling key aspects of the integration process.

Part One: Jira Setup and Python Script Execution

1. Create a Jira Account

2. Access Kanban Board and API Documentation

Copy the code from this documentation to create a ticket on JIRA using python

3. Generate API Token

Go to Profile > Manage my account > Security > Create an api token

4. List All Projects

Create a Python script to list all projects available in Jira.

Copy the code from below link to get or list all the projects available in your account

5. Create an Issue in Jira

Develop a Python file to create an issue on the Jira board. Obtain the issue ID through the Jira export XML option.

6. Execute Python Script

Run the Python script, and witness the creation of a ticket in the Jira board.

This concludes Part One, focusing on Jira setup, API understanding, and Python script execution.

Part Two: Flask Application and GitHub Integration

7. Launch an AWS EC2 Instance

Start by launching an AWS EC2 instance to host your Flask application.

8. Create a Flask Application

Develop a Flask application in Python. Test its functionality to ensure it's running smoothly.

9. Integrate Jira Ticket Creation Code

Edit the Flask app by incorporating the Jira ticket creation code developed in Part One.

10. Set Up GitHub Webhook

Create a webhook in GitHub, pointing to the /createJira route in your Flask app. This webhook triggers when a new issue is created.

11. Verify Integration

Upon receiving a successful webhook response, test the integration by commenting /jira on issues in GitHub. Observe the automatic creation of Jira tickets.

Once webhook is successful, lets comment in github and check if ticket is getting created in Jira

We can see that the ticket got automatically created because of the webhook and the /jira comment

12. The complete code using the conditional logic for reference

# This code sample uses the 'requests' library:
# http://docs.python-requests.org
import requests
from requests.auth import HTTPBasicAuth
import json
from flask import Flask, request

app = Flask(__name__)

# Define a route that handles GET requests
@app.route('/createJira', methods=['POST'])
def createJira():

    url = "https://bala-works.atlassian.net/rest/api/3/issue"

    API_TOKEN=""


    auth = HTTPBasicAuth("", API_TOKEN)

    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json"
    }

    payload = json.dumps( {
        "fields": {
        "description": {
            "content": [
                {
                    "content": [
                        {
                            "text": "Order entry fails when selecting supplier.",
                            "type": "text"
                        }
                    ],
                    "type": "paragraph"
                    }
                ],
            "type": "doc",
             "version": 1
        },
        "project": {
           "key": "AUT"
        },
        "issuetype": {
            "id": "10001"
        },
        "summary": "Main order flow broken",
    },
    "update": {}
    } )

    # Check if the GitHub comment contains "/jira"
    if "/jira" not in request.json["comment"]["body"]:
        return "Not a valid /jira command", 400  # Return a 400 Bad Request status

    response = requests.request(
        "POST",
        url,
        data=payload,
        headers=headers,
        auth=auth
    )

    return json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": "))

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Just add the API token and email id in the given field

13. Fine-Tune Integration

Implement conditional logic to ensure that only comments containing /jira trigger ticket creation.
We tried to comment /abc instead of /jira to check if still ticket is getting created in jira

The webhook response is shown below as it is not a valid type, as we have handled the condition in our Python code

Congratulations! You have successfully integrated Jira with GitHub using a Flask application. This step-by-step guide empowers you to replicate the integration for your projects.

ย