Streamline your workflow by automatically generating JIRA tickets from GitHub issue comments using Python and Flask
Welcome to the world of DevOps! Today, we are diving into an exciting project that bridges Jira and GitHub for seamless integration. The goal of this project is to automate the creation of Jira tickets directly from GitHub issue comments, saving time and reducing manual effort for developers.
Here’s how we’ll tackle this project:
Once everything is set up, our Flask app will act as a webhook API for GitHub. Anytime a developer comments /jira on a GitHub issue, the program will automatically create a corresponding Jira ticket, visible on the Jira dashboard. Exciting, right? Let’s get started!
Before diving into the project, make sure you have the following ready:
pip install flask
With these prerequisites in place, you're all set to kickstart this project!
Let’s begin the project by creating and setting up an EC2 instance for hosting our Flask application. Follow these steps:
Step 1: Create the EC2 Instance
Step 2: SSH into the Instance
Use the downloaded key-pair file to SSH into the instance:
pip install flask
Step 3: Set Up Python Environment
Run the following commands to install Python and Flask:
ssh -i your-key.pem ubuntu@<instance-public-ip>
This will set up all the necessary dependencies for the project.
Step 4: Create the Flask Application
sudo apt update sudo apt install python3-pip python3-venv python3 -m venv myvenv source myvenv/bin/activate # Activate the virtual environment pip3 install flask # Install Flask in the virtual environment
Add the following content to the file:
nano github_jira.py
Before running the github_jira.py script, we need two critical pieces of information:
Steps to Generate the Atlassian API Token:
Navigate to Account Settings:
Create a New API Token:
Copy the API Token:
import requests from requests.auth import HTTPBasicAuth import json from flask import Flask, request app = Flask(__name__) # Define a route that handles POST requests @app.route('/createJira', methods=['POST']) def createJira(): # The comment's body field in the GitHub payload comment_data = request.json.get("comment", {}) comment_body = comment_data.get("body", "") # Check if the body field of the comment is "/jira" if comment_body == "/jira": print("Condition met. Proceeding with POST request...") # Jira API details url = "https://<Your-Atlassian-domain>/rest/api/3/issue" API_TOKEN = "<YOUR_API_TOKEN>" auth = HTTPBasicAuth("<YOUR_EMAIL_ADDRESSS_CONNECTED_TO_THE_ACCOUNT>", 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": "<YOUR_KEY>" }, "issuetype": { "id": "<YOUR_ISSUE_ID>" }, "summary": "Main order flow broken", }, "update": {} }) # POST request to create an issue in Jira response = requests.post(url, data=payload, headers=headers, auth=auth) print("POST request response:", response.status_code, response.text) # Return the response back return json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")) else: print("No matching comment found. POST request will not be made.") return json.dumps({"error": "No matching comment found. POST request was not made."}, sort_keys=True, indent=4, separators=(",", ": ")) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
Add Your Atlassian Domain:
Replace
Before running the script, you need to update a few important fields in the github_jira.py file to ensure the integration works seamlessly with your Jira account.
1. HTTP Basic Authentication (Email Address)
Replace the first parameter in the HTTPBasicAuth with the email address linked to your Jira account.
API_TOKEN = "<Your-Generated-API-Token>"
2. Project Key
Replace the "key" field under fields in the script:
pip install flask
3. Issue Type ID
Replace the "id" field under issuetype in the script:
ssh -i your-key.pem ubuntu@<instance-public-ip>
Example of Updated Fields in the Script:
sudo apt update sudo apt install python3-pip python3-venv python3 -m venv myvenv source myvenv/bin/activate # Activate the virtual environment pip3 install flask # Install Flask in the virtual environment
Final Step: Run the Script
Once these fields are updated, run the script using:
nano github_jira.py
Your script is now fully configured and ready to integrate GitHub comments with Jira ticket creation!
Now that our script is ready, the final step is to configure a webhook in your GitHub repository. This webhook will listen for specific events (in this case, issue comments) and trigger the Flask application.
Steps to Add the Webhook:
Add a New Webhook:
pip install flask
Content Type:
Select application/json from the dropdown menu.
Triggers:
Select the option "Let me select individual events".
Check the box for Issue comments only.
Save the Webhook:
Testing the Integration
Observe the Magic:
Verify on the Jira Dashboard:
Congratulations! ? You've successfully completed a hands-on project integrating GitHub and Jira. By leveraging a Flask application as an intermediary, we automated the process of creating Jira tickets directly from GitHub issue comments.
In this project, we covered:
This integration simplifies collaboration between developers and project managers by reducing manual effort and ensuring that important tasks don't slip through the cracks. It’s a practical demonstration of how automation can enhance productivity in a DevOps workflow.
Feel free to build upon this foundation to customize the integration further or explore additional use cases, such as automating GitHub Pull Request tracking in Jira or integrating other tools into your workflow.
We hope you found this project informative and engaging. ? For more informative blog, Follow me on Hashnode, X(Twitter) and LinkedIn.
Happy coding and automating! ?
The above is the detailed content of Automating JIRA Ticket Creation with a Flask API: A GitHub Webhook Integration Guide. For more information, please follow other related articles on the PHP Chinese website!