-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathatlassian.py
More file actions
65 lines (49 loc) · 2.04 KB
/
atlassian.py
File metadata and controls
65 lines (49 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python26
import urllib
import urllib2
import json
import subprocess
import base64
import re
"""
This uses the JIRA rest api to close you some sweet jira tickets.
It reads your git commits looking for some common phrasing.
eg:
This super sweet commit was sassy and closes #RAD-1
It will take your commit message, add it to the ticket and then close the ticket.
It uses basic auth { :( } but, you can simply put your information in your config file
and parse it. It should be as follows:
[jira]
username = sexlexia
password = kifgetmypants1
url = blahasde.com
From there you can rock and roll, never having to see Jira again!
"""
config = subprocess.Popen(['git', 'config', '--list'], stdout=subprocess.PIPE).communicate()[0]
jira_creds = re.compile("jira.*=*")
credentials = re.findall(jira_creds, config)
if len(credentials) < 3:
print "You don't have your jira credentials set in your config"
exit(0)
username, password, url = [x.split("=")[-1] for x in credentials]
line = subprocess.Popen(['git', 'log', '-1', 'HEAD', '--pretty=%B'], stdout=subprocess.PIPE).communicate()[0].rstrip("\n")
ticket_regex = re.compile("[closes|fixes|finishes|completes]+ #[[A-Z]+-[0-9]+]*")
ticket = re.findall(ticket_regex, line)
if ticket:
ticket = ticket[0].split("#")[-1]
print ticket
headers = {"Authorization": "Basic %s" % base64.encodestring('%s:%s' % (username, password)).replace("\n",""), "Content-Type" : "application/json"}
try:
#Add the comment
data = { "body" : line}
request = urllib2.Request("https://%s/rest/api/latest/issue/%s/comment" % (url, ticket), json.dumps(data), headers)
result = urllib2.urlopen(request)
result.close()
data = {"transition": { "id" : 5 } }
request = urllib2.Request("https://%s/rest/api/latest/issue/%s/transitions" % (url, ticket), json.dumps(data), headers)
result = urllib2.urlopen(request)
result.close()
except urllib2.HTTPError as e:
print "Bad request :("
print e.read()
e.close()