Skip to content

Instantly share code, notes, and snippets.

@drewbrokke
Last active July 28, 2016 21:54
Show Gist options
  • Save drewbrokke/bcc69084948bf3066d7e7ca98c1458c9 to your computer and use it in GitHub Desktop.
Save drewbrokke/bcc69084948bf3066d7e7ca98c1458c9 to your computer and use it in GitHub Desktop.
Get Latest PR from Liferay JIRA Ticket
"""
Opens the latest pull request registered to a JIRA ticket.
You can call it from the Liferay repo to check your current branch,
or you can pass in a ticket number as an argument.
"""
import json
import sys
import urllib2
import webbrowser
if len(sys.argv) > 1:
TICKET = sys.argv[1]
else:
import re
import subprocess
try:
BRANCH_ALL = subprocess.Popen(('git', 'branch'), stdout=subprocess.PIPE)
BRANCH = subprocess.check_output(('grep', r'^\* '), stdin=BRANCH_ALL.stdout)
TICKET = re.sub(r'.*([A-Z]{3,}-\d+).*', r'\1', BRANCH)
except subprocess.CalledProcessError:
print """
Could not parse git branch.
Either call this script with a ticket number or from a git repo.
"""
sys.exit()
TICKET_URL = "https://issues.liferay.com/rest/api/latest/issue/" + TICKET
try:
RESPONSE_TEXT = urllib2.urlopen(TICKET_URL).read()
RESPONSE_DICT = json.loads(RESPONSE_TEXT)
PULL_REQUEST_URL = RESPONSE_DICT["fields"]["customfield_10421"]
webbrowser.open(PULL_REQUEST_URL)
except AttributeError:
print "No pull request found for JIRA ticket number '" + TICKET + "'"
sys.exit()
except urllib2.HTTPError:
print "No JIRA ticket found for ticket number '" + TICKET +"'"
sys.exit()
except urllib2.URLError:
print "No internet connection available."
sys.exit()
finally:
sys.exit()
@natecavanaugh
Copy link

Heya Drew,
I would change the regex for extracting the JIRA ticket to something like:

re.sub('.*([A-Z]{3,}-\d+).*', r'\1', branch)

Basically, this will grab only the JIRA ticket (since I believe all JIRA projects are at least 3 uppercase letters, a dash, and any number of digits).

This should work for cases like pull-request-1234-LPS-1234.

@drewbrokke
Copy link
Author

Updated. Thanks Nate!

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