-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost-commit
executable file
·98 lines (74 loc) · 2.88 KB
/
post-commit
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
import httplib
import unittest
import urllib
import logging
#youtrack username
username = ""
#youtrack password
password = ""
#youtrack base URL e.g.: http://youtrack.mydomain.tld:8080
youtrack_base_url = ""
# a url pattern for links to git commits
# eg: https://bitbucket.org/user/repo/commits/%s
# or https://github.com/user/repo/commit/%s
git_url_pattern = ""
logging.basicConfig(filename='post-commit.log',level=logging.DEBUG)
class YouTrackConnection(object):
def __init__(self, username, password, url):
self.http = httplib.HTTPConnection(url)
self.url = url
self.base_url = self.url + "/rest"
if self._login(username, password):
self.connected = True
def _login(self, username, password):
login_url = self.base_url + "/user/login"
body = "login=%s&password=%s" % (username, password)
headers = {"Content-Length": str(len(body)), "Content-Type":
"application/x-www-form-urlencoded"}
#self.http.set_debuglevel(4)
self.http.request('POST', "http://"+login_url, body, headers)
response = self.http.getresponse()
data = response.read()
self.headers = {'Cookie': response.getheader('set-cookie'), 'Cache-Control': 'no-cache'}
if response.status != 200:
return False
return True
def _req(self, method, uri, body=None):
headers = self.headers
if method == "PUT" or method == "POST":
headers = headers.copy()
headers['Content-Type'] = 'application/xml; charset=UTF-8'
headers['Content-Length'] = str(len(body))
self.http.request(method, "http://"+self.base_url + uri, body, headers)
response = self.http.getresponse()
print response.status
print response.reason
data = response.read()
def execute(self, issueId, command, comment=None, group=None):
params = {'command': command}
if comment is not None:
params['comment'] = comment
if group is not None:
params['group'] = group
self._req('POST', '/issue/' + issueId + "/execute?" + urllib.urlencode(params), body='')
return True
if __name__ == "__main__":
import sys
import subprocess
import re
message = subprocess.Popen(['git', 'log', '-1', '--pretty=%B'], stdout=subprocess.PIPE).communicate()[0]
commit_hash = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE).communicate()[0]
issue_re = re.compile(ur'([A-Z][A-Z]-[0-9]*).*')
command_re = re.compile(ur'.*#([a-zA-Z]*)')
issue_id = re.findall(issue_re, message)
command = re.findall(command_re, message)
print message, commit_hash, issue_id, command
if issue_id:
c = YouTrackConnection(username, password, youtrack_base_url)
if c.connected:
if command and command[0] == "fixes":
print "Fixing %s" % issue_id[0]
c.execute(issue_id[0], "State Fixed", message + "\n\n [" + git_url_pattern % commit_hash.strip() +" commit]")
if issue_id and not command:
c.execute(issue_id[0], "tag WIP", message + "\n\n [" + git_url_pattern % commit_hash.strip() +" commit]")