forked from evmar/git-cl
-
Notifications
You must be signed in to change notification settings - Fork 3
/
allura_issues.py
145 lines (123 loc) · 4.48 KB
/
allura_issues.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
# Integrates git-cl with the Allura issue tracking tool
# Phil Holmes
import urllib
import cl_settings
import sys
settings = cl_settings.Settings()
def create_issue(subject, description, code_review_url=None):
BEARER_TOKEN = settings.GetToken()
allura_server = settings.GetTrackerServer()
allura_api = add_api_string(allura_server)
if code_review_url:
if description:
description += '\n\n'
description += code_review_url
data = {
'access_token': BEARER_TOKEN,
'ticket_form.summary': subject,
'ticket_form.description': description,
'ticket_form.status': 'Started',
'ticket_form.custom_fields._patch': 'new',
'ticket_form.custom_fields._type': 'Enhancement',
}
data_encoded = urllib.urlencode(data)
allura_result = urllib.urlopen (allura_api + "/new", data_encoded)
if allura_result.getcode() == 200:
print 'Ticket created at: %s' % allura_result.geturl().replace("/rest","")
else:
print 'Error code %s' % (allura_result.getcode())
print 'Failed URL was %s' % allura_api + "/new"
print 'Failed data was %s' % data_encoded
sys.exit(1)
issue_id = get_issue_number(allura_result.geturl())
# Now get text of issue back, to locate the originator
filehandle = urllib.urlopen (allura_api + issue_id)
if filehandle.getcode() != 200:
print "Problem getting originator for Allura issue"
sys.exit(1)
issue_str = filehandle.read()
originator = get_reporter(issue_str)
# Now set the owner to the originator
data = {
'access_token': BEARER_TOKEN,
'ticket_form.assigned_to': originator,
}
data_encoded = urllib.urlencode(data)
filehandle = urllib.urlopen (allura_api + issue_id + "/save", data_encoded)
if filehandle.getcode() != 200:
print "Problem setting originator for Allura issue"
sys.exit(1)
return issue_id
def update_issue(allura_issue_id, comment, code_review_url=None):
BEARER_TOKEN = settings.GetToken()
allura_server = settings.GetTrackerServer()
allura_api = add_api_string(allura_server)
# Set patch status to new
## TODO: Sometimes a new code review is created for an issue that
## already had one. It would be helpful to update the description
## field to refer to the latest code review instead of the old.
data = {
'access_token': BEARER_TOKEN,
'ticket_form.status': 'Started',
'ticket_form.custom_fields._patch': 'new',
}
data_encoded = urllib.urlencode(data)
filehandle = urllib.urlopen (allura_api + str(allura_issue_id) + "/save", data_encoded)
if filehandle.getcode() != 200:
print "Problem setting patch status for Allura issue"
sys.exit(1)
if code_review_url:
if comment:
comment += '\n\n'
comment += code_review_url
# Now get the thread ID so we can add a note to the correct thread
filehandle = urllib.urlopen (allura_api + str(allura_issue_id))
issue_data = filehandle.read()
thread_id = get_thread_ID(issue_data)
data = {
'access_token': BEARER_TOKEN,
'text': comment,
}
issue_id = get_issue_number(filehandle.geturl())
data_encoded = urllib.urlencode(data)
allura_url = allura_api + "/_discuss/thread/"
allura_url += thread_id + "/new"
allura_result = urllib.urlopen (allura_url, data_encoded)
if allura_result.getcode() != 200:
print 'Received error code %s when attempting to update %s' % (
allura_result.getcode(), allura_url)
sys.exit(1)
return issue_id
def get_issue_number(url):
trim_url = url[0:len(url)-1]
slash_pos = trim_url.rfind('/')
issue = url[slash_pos+1: len(url)]
if issue[len(issue)-1] == '/':
issue = issue [0:len(issue)-1]
return issue
def add_api_string(allura_server):
p_pos = allura_server.find('/p/')
if p_pos < 1:
print 'Allura server has unxepected format: expect it to contain /p/ in the URL'
print 'Please run git cl config'
sys.exit(1)
api_server = allura_server[0:p_pos]
api_server += '/rest'
api_server += allura_server [p_pos:]
return api_server
def get_reporter(issue_text):
reporter_pos = issue_text.index('"reported_by": "')
reporter_str = issue_text[reporter_pos+16:]
quote_pos = reporter_str.index('"')
reporter_str = reporter_str[0:quote_pos]
return reporter_str
def get_thread_ID(issue_data):
discussion_pos = issue_data.index('"discussion_thread": ')
discussion_data = issue_data[discussion_pos:]
id_key = '"_id": "'
id_key_pos = discussion_data.index(id_key)
id_str = discussion_data[id_key_pos + len(id_key):]
quote_pos = id_str.index('"')
thread_id = id_str[0:quote_pos]
return thread_id