Skip to content

Instantly share code, notes, and snippets.

@lonniev
Created January 7, 2025 00:01
Show Gist options
  • Save lonniev/daf53d0c0a1ece700dfe96385baff84f to your computer and use it in GitHub Desktop.
Save lonniev/daf53d0c0a1ece700dfe96385baff84f to your computer and use it in GitHub Desktop.
Use python with JIRA APIs to find the Jira issues for an Organization that you touched over a time range.
from itertools import compress
from jira import JIRA
import json
import keyring
import argparse
import os
from dateutil import parser
from datetime import datetime
import pytz
from tzlocal import get_localzone
def parseToOffsetIso8601( date_str ):
local_tz = get_localzone()
local_dt = parser.parse( date_str ).astimezone( local_tz )
return local_dt
def findJiraItems( project, start_date, end_date, organization ):
def user_altered_in_period(jira, issue):
changelog = jira.issue(issue, expand='changelog').changelog
current_user = jira.current_user()
start = parseToOffsetIso8601( start_date )
end = parseToOffsetIso8601( end_date )
return any(
history.author.accountId == current_user and start <= datetime.strptime( history.created, '%Y-%m-%dT%H:%M:%S.%f%z') <= end
for history in changelog.histories
)
jira_server = keyring.get_password( "repositories", "jira_server" )
jira_user = keyring.get_password( "repositories", "jira_user" )
jira_api_token = keyring.get_password( "repositories", "jira_apitoken" )
jira = JIRA({"server": jira_server}, basic_auth=(jira_user, jira_api_token))
# JQL query
jql_query = f'''project = "{project}"
AND (assignee = currentUser() OR reporter = currentUser() OR watcher = currentUser() OR comment ~ currentUser())
AND Organizations in ("{organization}")
AND updated >= "{start_date}"'''
issues = jira.search_issues(jql_query, fields= "key,summary,assignee,reporter,customfield_11000" )
user_altered = list( compress( issues, [ user_altered_in_period( jira, issue.key ) for issue in issues] ) )
# Extract and print issue details
matches = [ {
"key": issue.key,
"url": issue.self,
"summary": issue.fields.summary,
"assignee": (
issue.fields.assignee.displayName if issue.fields.assignee else None
),
"reporter": issue.fields.reporter.displayName,
"reporter_email": issue.fields.reporter.emailAddress
} for issue in user_altered ]
return matches
def parseToJiraDate( date_str ):
local_tz = get_localzone()
local_dt = parser.parse( date_str ).astimezone( local_tz )
return local_dt.strftime('%Y/%m/%d')
def main():
arg_parser = argparse.ArgumentParser(description='Fetch Jira tickets by date range for the current user.')
arg_parser.add_argument('--start-date', required=True, help='Start date in YYYY-MM-DD format')
arg_parser.add_argument('--end-date', required=True, help='End date in YYYY-MM-DD format')
arg_parser.add_argument('--project', required=True, help='Jira Project holding the tickets')
arg_parser.add_argument('--organization', required=True, help='Organization of the reporter')
args = arg_parser.parse_args()
args.start_date = parseToJiraDate( args.start_date )
args.end_date = parseToJiraDate( args.end_date )
matches = findJiraItems( args.project, args.start_date, args.end_date, args.organization )
print( json.dumps(matches, indent=2) )
if __name__ == '__main__':
main()
@lonniev
Copy link
Author

lonniev commented Jan 7, 2025

Use like this:

python ~/supportTicketsFor.py --start-date 2024-01-01 --end-date 2024-12-31 --project 'My Help Desk' --organization 'ACME - Space & Airborne Systems'

[
  [                                                                                                                                                                                                                                                                                         
   {                                                                                                                                                                                                                                                                                       
     "key": "MHD-4750",                                                                                                                                                                                                                                                                    
     "url": "https://mocked-url.com/issue/59080",                                                                                                                                                                                                                                          
     "summary": "Request download of the API Cookbook for version 42",                                                                                                                                                                                                      
     "assignee": "Mocked Name 1",                                                                                                                                                                                                                                                          
     "reporter": "Mocked Name 2",                                                                                                                                                                                                                                                          
     "reporter_email": "[email protected]"                                                                                                                                                                                                                                         
   },  ...
]```

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