Automate Jira Ticket with python
Content
๐ Automate Jira Ticket Creation from GitHub using Webhook, Flask & Ngrok
๐ฏ Objective
Automatically create a Jira issue whenever someone comments on a GitHub issue or pull request using a specific keyword like /jira
.
๐งฑ Components Used
Component | Role |
---|---|
Jira (Self-Hosted) | Issue tracking system |
GitHub | Source code hosting & issue tracking |
Flask (Python) | Lightweight web server to receive webhook requests |
Ngrok | Exposes local Flask server to the public web |
GitHub Webhook | Triggers events on GitHub (here: issue_comment) |
requests (Python) | Makes REST API calls to Jira |
Basic Auth (Jira) | Authenticates requests to Jira’s REST API |
๐ผ๏ธ Architecture
+------------------+ +-----------+ +-----------+ +-------------+
| GitHub Webhook +----->+ Ngrok +-----> + Flask +-----> | Jira API |
| (issue_comment) | | (HTTPS) | | Webhook | | (localhost) |
+------------------+ +-----------+ +-----------+ +-------------+
โ๏ธ Step-by-Step Implementation
- Start Jira (locally or VM)
Make sure Jira is running and accessible, e.g., via http://192.168.11.50:8080
, and has:
- A project with key
PYT
- An issue type with ID like
10001
- Create Flask Webhook Server
from flask import Flask, request, jsonify
import requests
from requests.auth import HTTPBasicAuth
import json
app = Flask(__name__)
@app.route('/createjira', methods=['POST'])
def create_jira_ticket():
webhook = request.get_json()
if webhook and 'comment' in webhook and '/jira' in webhook['comment'].get('body', ''):
jira_url = "http://localhost:8080/rest/api/2/issue"
auth = HTTPBasicAuth("your_username", "your_password")
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
payload = json.dumps({
"fields": {
"project": {"key": "PYT"},
"summary": "GitHub โ Jira: " + webhook['comment']['body'],
"description": f"Comment: {webhook['comment']['body']}\nUser: {webhook['comment']['user']['login']}",
"issuetype": {"id": "10001"}
}
})
response = requests.post(jira_url, headers=headers, data=payload, auth=auth)
return jsonify(response.json()), response.status_code
return jsonify({"message": "No /jira found in comment."}), 200
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)
- Run Flask App
python3 app.py
- Expose Flask with Ngrok
ngrok http 5000
Copy the HTTPS URL from Ngrok (e.g., https://abc123.ngrok-free.app
).
- Configure GitHub Webhook
- Go to Settings โ Webhooks on your repo
- Click Add Webhook
- Payload URL:
https://abc123.ngrok-free.app/createjira
- Content Type:
application/json
- Events: Select Let me select individual events โ Check Issue comments
๐งช Try it Yourself!
Post a comment on a GitHub issue with:
/jira Please track this.
Or use curl
to simulate it:
curl -X POST https://abc123.ngrok-free.app/createjira \
-H "Content-Type: application/json" \
-d '{"comment": {"body": "/jira this is urgent", "user": {"login": "devopsyourlife"}}}'
Result:
- Flask prints the webhook content
- Jira creates a new issue under the
PYT
project ๐
โ Add a Diagram Image Visuals help comprehension. Tools: diagrams.net, Lucidchart.
โ Reply to GitHub with the Jira link After creating a ticket, use GitHub API to post a reply comment with the Jira link.
๐ Security Tips
- Never expose Flask dev server in production
- Use Jira API token instead of a password
- Validate the
X-Hub-Signature-256
GitHub header - Deploy Flask behind a WSGI server (e.g., Gunicorn, Uvicorn)
โจ Improvements to Consider
- Track issue URLs and GitHub usernames in Jira ticket
- Log errors and successes to a file
- Add rate limiting or token verification
- Use Docker to containerize the whole system
๐ Stack Summary
- Python 3.10+
- Flask
- Ngrok
- GitHub Webhooks
- Jira REST API v2
๐ก Final Thoughts
This integration bridges GitHub collaboration with Jira ticketing, providing:
- Real-time issue tracking from GitHub comments
- No context-switching for developers
- Improved traceability in your DevOps workflows
Perfect for DevOps, SREs, and software engineers looking to automate and integrate their ecosystem.
๐ Read this post in French ๐ซ๐ท here (add link if needed)
Happy automating! ๐ค๐ผ