Skip to content

Instantly share code, notes, and snippets.

@svandragt
Created April 23, 2025 12:56
Show Gist options
  • Save svandragt/7e02fcdc6f5eddd73417c41761e2ce4a to your computer and use it in GitHub Desktop.
Save svandragt/7e02fcdc6f5eddd73417c41761e2ce4a to your computer and use it in GitHub Desktop.
JIRA Ticket Checker Script
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "pyperclip",
# "requests",
# ]
# ///
import os
import requests
import subprocess
import re
import pyperclip
# Configuration from environment variables
JIRA_URL = os.getenv('JIRA_URL')
JIRA_EMAIL = os.getenv('JIRA_EMAIL')
JIRA_API_TOKEN = os.getenv('JIRA_API_TOKEN')
JIRA_QUERY = os.getenv('JIRA_QUERY')
def get_jira_tickets():
# Set up the request
auth = (JIRA_EMAIL, JIRA_API_TOKEN)
headers = {
"Content-Type": "application/json"
}
params = {
'jql': JIRA_QUERY,
'fields': 'key', # Only fetch the ticket key
'maxResults': 100 # Adjust as needed
}
# Make the request to JIRA
response = requests.get(JIRA_URL, headers=headers, auth=auth, params=params)
response.raise_for_status() # Raise an error for bad responses
# Extract ticket keys
tickets = [issue['key'] for issue in response.json().get('issues', [])]
return tickets
def get_current_branch():
# Get the current Git branch name
branch_name = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip().decode('utf-8')
return branch_name
def get_latest_commit_message():
# Get the latest Git commit message
commit_message = subprocess.check_output(['git', 'log', '-1', '--pretty=%B']).strip().decode('utf-8')
return commit_message
def find_matching_tickets(tickets, branch_name, commit_message):
# Regex to match JIRA ticket format
ticket_pattern = re.compile(r'[A-Z]+-\d+')
# Find matches in branch name and commit message
matches = set()
for ticket in tickets:
if ticket in branch_name or ticket in commit_message:
matches.add(ticket)
return matches
def main():
# Check if environment variables are set
if not all([JIRA_URL, JIRA_EMAIL, JIRA_API_TOKEN]):
print("Please set the JIRA_URL, JIRA_EMAIL, and JIRA_API_TOKEN environment variables.")
return
# Get JIRA tickets assigned to the user
tickets = get_jira_tickets()
# Get current branch name and latest commit message
branch_name = get_current_branch()
commit_message = get_latest_commit_message()
# Find matching tickets
matching_tickets = find_matching_tickets(tickets, branch_name, commit_message)
# Copy matching tickets to clipboard
if matching_tickets:
ticket_list = "\n".join(matching_tickets)
pyperclip.copy(ticket_list)
print(f"Copied to clipboard:\n{ticket_list}")
else:
print("No matching tickets found.")
if __name__ == "__main__":
main()
@svandragt
Copy link
Author

The JIRA Ticket Checker Script is a practical tool designed for developers and project managers. It automatically retrieves JIRA tickets assigned to you and checks them against your current Git branch and latest commit message. If there are any matches, it copies the relevant ticket numbers to your clipboard for easy access.

This script simplifies the process of tracking tasks and ensures you stay organized without the need for manual searching. With secure environment variable configuration, it keeps your JIRA credentials safe. It's a straightforward solution to help you manage your workflow more efficiently. #chatgpt

@svandragt
Copy link
Author

svandragt commented Apr 23, 2025

Requires only UV https://docs.astral.sh/uv/ uv run jira-ticket.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment