Skip to content

Instantly share code, notes, and snippets.

@vmalloc
Created December 23, 2012 12:16
Show Gist options
  • Save vmalloc/4363135 to your computer and use it in GitHub Desktop.
Save vmalloc/4363135 to your computer and use it in GitHub Desktop.
Small utility to commit a currently assigned Jira issue via git, using *dialog* and the Jira REST API.
#! /usr/bin/python
# -*- mode: python -*-
from cStringIO import StringIO
from collections import OrderedDict
import argparse
import requests
import subprocess
import sys
import tempfile
parser = argparse.ArgumentParser(usage="%(prog)s [options] args...")
def main(args):
issue_key, summary = _get_issue_key_and_summary()
temp_filename = tempfile.mktemp()
with open(temp_filename, "w") as temp_file:
temp_file.write("{0}: {1}".format(issue_key, summary))
p = subprocess.Popen("git commit -a -e -F {}".format(temp_filename), shell=True)
return p.wait()
def _get_issue_key_and_summary():
issues = requests.get("http://your.jira.server.com/rest/api/2/search", auth=(USERNAME, PASSWORD),
params={
"jql" : 'assignee = currentUser() AND resolution is EMPTY ORDER BY updated DESC',
}).json["issues"]
issues = OrderedDict((issue["key"], issue) for issue in issues)
if not issues:
raise ProgramError("No in progress issues")
if len(issues) > 1:
issue = _select_issue(issues)
else:
[issue] = issues.values()
return issue["key"], issue["fields"]["summary"]
def _select_issue(issues):
opts = []
for issue in issues.values():
opts.extend((issue["key"], '"{}"'.format(issue["fields"]["summary"])))
p = subprocess.Popen("dialog --menu \"select issue\" 0 0 0 {}".format(" ".join(opts)), shell=True, stderr=subprocess.PIPE)
if p.wait() != 0:
raise ProgramError("Dialog failed: {}".format(p.stderr.read()))
subprocess.call("clear", shell=True)
issue_key = p.stderr.read().strip()
return issues[issue_key]
################################## Boilerplate ################################
class ProgramError(Exception):
pass
#### For use with entry_points/console_scripts
def main_entry_point():
args = parser.parse_args()
try:
sys.exit(main(args))
except ProgramError as e:
sys.exit(str(e))
if __name__ == "__main__":
main_entry_point()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment