DevOps
Python Scripting
Project: Jira Automation

Project: Jira Ticket Automation

Managing issues manually can be slow. In this project, you will build a Python tool that automatically creates Jira tickets via the Atlassian REST API. This is essential for automating bug reports from CI/CD pipelines or monitoring systems.

Prerequisites

  • Jira Account: A Jira Cloud instance where you have project access.
  • API Token: Generate one from your Atlassian Account Security (opens in a new tab) page.
  • Python Libraries:
    • requests: To handle HTTP requests.
    • python-dotenv: To securely load credentials from a file.
pip install requests python-dotenv

Step 1: Secure Configuration

Never hardcode your API token. Create a .env file in your project directory:

JIRA_URL=https://your-domain.atlassian.net
JIRA_EMAIL=your-email@example.com
JIRA_TOKEN=your-api-token-here
JIRA_PROJECT_KEY=DEVOPS

Step 2: The Core Script

Create create_jira_ticket.py and implement the logic to talk to the Jira REST API.

import os
import requests
import json
from requests.auth import HTTPBasicAuth
from dotenv import load_dotenv
 
# Load credentials from .env
load_dotenv()
 
URL = f"{os.getenv('JIRA_URL')}/rest/api/3/issue"
EMAIL = os.getenv('JIRA_EMAIL')
TOKEN = os.getenv('JIRA_TOKEN')
PROJECT_KEY = os.getenv('JIRA_PROJECT_KEY')
 
# Authentication object
auth = HTTPBasicAuth(EMAIL, TOKEN)
 
# Headers
headers = {
   "Accept": "application/json",
   "Content-Type": "application/json"
}
 
def create_payload(summary, description):
    # Jira uses 'Atlassian Document Format' (ADF) for descriptions
    return json.dumps({
        "fields": {
            "project": {"key": PROJECT_KEY},
            "summary": summary,
            "description": {
                "type": "doc",
                "version": 1,
                "content": [
                    {
                        "type": "paragraph",
                        "content": [{"type": "text", "text": description}]
                    }
                ]
            },
            "issuetype": {"name": "Task"}
        }
    })
 
def create_issue(summary, desc):
    payload = create_payload(summary, desc)
    response = requests.post(URL, data=payload, headers=headers, auth=auth)
    
    if response.status_code == 201:
        data = response.json()
        print(f"âś… Ticket Created! URL: {os.getenv('JIRA_URL')}/browse/{data['key']}")
    else:
        print(f"❌ Error {response.status_code}: {response.text}")
 
# Example Run
create_issue("Automated Testing Alert", "High failure rate detected in the production pipeline.")

Step 3: Understanding the Logic

  1. Authentication: We use HTTPBasicAuth with your email and the API token.
  2. Payload (ADF): Jira Cloud requires the description in Atlassian Document Format (a JSON structure), not raw text.
  3. Endpoint: We target the /rest/api/3/issue endpoint for creation.
  4. Response Handling: We look for status code 201 (Created) to confirm success.

Step 4: Run and Verify

Execute the script:

python3 create_jira_ticket.py

Check your Jira project dashboard—you should see a new task titled "Automated Testing Alert" waiting for you!


[!TIP] Issue Types You can easily change the script to create Bug, Story, or Epic by modifying the issuetype field in the payload generator. Ensure the issue type exists in your specific Jira project.