Skip to content

Instantly share code, notes, and snippets.

@correl
Created August 6, 2018 14:05
Show Gist options
  • Save correl/07f2bd66e784f16e7a8b6067c0c10f40 to your computer and use it in GitHub Desktop.
Save correl/07f2bd66e784f16e7a8b6067c0c10f40 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.7
""" Outputs JIRA issue information for each issue id
"""
from urllib import urlencode
import argparse
import base64
import json
import os
import sys
import urllib2
from clint.textui import puts, colored, columns
JIRA_URL = os.environ['JIRA_URL']
JIRA_USER = os.environ['JIRA_USER']
JIRA_PASS = os.environ['JIRA_PASS']
def rest_call(path, method="GET", **kwargs):
path = "{0}rest/api/latest/{1}".format(JIRA_URL, path)
if method != "GET":
data = urlencode(kwargs)
else:
data = None
req = urllib2.Request(path)
base64string = base64.encodestring('%s:%s' % (JIRA_USER, JIRA_PASS))[:-1]
req.add_header("Authorization", "Basic %s" % base64string)
f = urllib2.urlopen(req, data)
data = f.read()
try:
return json.loads(data)
except json.JSONDecodeError:
return None
parser = argparse.ArgumentParser(description="Lookup JIRA issues")
parser.add_argument('issues', metavar='ISSUE-ID', type=str, nargs='*')
parser.add_argument('-i', '--stdin', action='store_true',
help='Read standard input and scan it for issue ids')
parser.add_argument('-m', action='store_true', help='Machine-readable output')
args = parser.parse_args()
issues = [i.upper() for i in args.issues]
if args.stdin:
import re
text = sys.stdin.read()
matches = re.findall('[A-Z]+-\d+', text)
issues.extend(matches)
for issue_id in set(issues):
try:
issue = rest_call('issue/{0}'.format(issue_id))
except urllib2.HTTPError:
continue
fixVersions = " ".join([v['name'] for v in issue['fields']['fixVersions']])
storyPoints = issue['fields']['customfield_10004']
storyPoints = str(int(storyPoints)) if storyPoints else ''
if args.m:
puts('\t'.join([
issue['key'],
storyPoints,
fixVersions,
issue['fields']['status']['name'],
issue['fields']['summary'],
]))
else:
output = columns(
[colored.white(issue['key']), 15],
[colored.white(storyPoints), 5],
[colored.green(fixVersions), 15],
[colored.red(issue['fields']['status']['name']), 15],
[issue['fields']['summary'], None],
)
puts(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment