Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
create tsv of tickets from jira
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import argparse
import pytz
import pprint
from jira import JIRA
from datetime import datetime
def valid_date(s):
try:
return datetime.strptime(s, '%Y-%m-%d')
except ValueError:
msg = "Not a valid date: '{0}'.".format(s)
raise argparse.ArgumentTypeError(msg)
def get_ticket_info(ticket):
line = ticket.id
line += u'\t' + ticket.fields.summary
if ticket.fields.reporter != None:
line += u'\t' + ticket.fields.reporter.name
if ticket.fields.assignee != None:
line += u'\t' + ticket.fields.assignee.name
return line
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='get tickets as tsv')
parser.add_argument('-j', '--jira-url',
help='JIRA url',
default=os.environ.get('JIRA_URL', None),
type=str)
parser.add_argument('-ju', '--jira-user',
help='JIRA user',
default=os.environ.get('JIRA_USER', None),
type=str)
parser.add_argument('-jp', '--jira-passwd',
help='JIRA password',
default=os.environ.get('JIRA_PASSWORD', None),
type=str)
parser.add_argument('-jql', '--jira-ql',
help='JIRA QL to filter tickets',
default=os.environ.get('JIRA_QL', None),
type=str)
parser.add_argument('-f', '--frm',
help='Parse tickets from this date. Please specify in %%Y-%%m-%%d format.',
type=valid_date, required=True)
parser.add_argument('-t', '--to',
help='Parse tickets until this date. Please specify in %%Y-%%m-%%d format.',
type=valid_date, required=True)
parser.add_argument('-tz', '--timezone',
help='Timezone to find jira tickets',
default='Asia/Tokyo',
type=str)
parser.add_argument('-o', '--output-path',
help='output file path',
default=os.path.expanduser('./jira_tickets.tsv'),
type=str)
args = parser.parse_args()
pp = pprint.PrettyPrinter(indent=2)
jc = JIRA(server=args.jira_url, auth=(args.jira_user, args.jira_passwd))
tz = pytz.timezone(args.timezone)
from_dt = tz.localize(args.frm)
to_dt = tz.localize(args.to)
tickets = jc.search_issues(
'{} AND updatedDate >= "{}" AND updatedDate <= "{}"'
.format(args.jira_ql, from_dt.strftime('%Y/%m/%d'), to_dt.strftime('%Y/%m/%d')),
maxResults=False)
print('number of tickets : %d\n', len(tickets))
with open(args.output_path, 'w') as f:
f.write('id\ttitle\treporter\tassignee\n')
for ticket in tickets:
#print(ticket.fields.issuetype.name, ticket.fields.summary)
line = get_ticket_info(ticket)
f.write(line.encode('utf-8') + os.linesep)
@go-zen-chu

This comment has been minimized.

Copy link
Owner Author

go-zen-chu commented Jul 15, 2019

prerequisite

pip install jira

I recommend to you to customize https://gist.github.com/go-zen-chu/afdbd84d903ad4020d988237bf3fd822#file-get_jira_tsv-py-L19-L25 for your usage.

Also using .envrc below helps you configure running program above.

export JIRA_URL="https://your_jira"
export JIRA_USER=`whoami`
export JIRA_PASSWORD='xxxxx'
export JIRA_QL="project = xxxxx"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.