-
Notifications
You must be signed in to change notification settings - Fork 16
/
jenkins-git-rally-notify-issue
executable file
·131 lines (106 loc) · 4.27 KB
/
jenkins-git-rally-notify-issue
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
#!/usr/bin/env python
#
# see: https://developer.help.rallydev.com/python-toolkit-rally-rest-api
#
import ConfigParser
from io import StringIO
import json
import os
import re
import requests
import sys
from pyral import Rally
action = os.getenv('ACTION')
title = os.getenv('TITLE')
if action == "opened":
body = os.getenv('BODY', '')
elif action == "created":
body = os.getenv('COMMENT_BODY', '')
issue_url = os.getenv('ISSUE_URL')
repo_name = os.getenv('REPO_NAME')
issue_api_url = os.getenv('ISSUE_API_URL')
issue_num = issue_url.split('/')[-1]
comment_user = os.getenv('COMMENT_USER', '')
# truncate the github issue body to fit in rally description max
# length of 32768
if len(body) > 32767:
msg = '...\r\n\r\ndescription truncated. See %s for full details' % issue_url
body = ''.join((body[:32600], msg))
body = body.replace('\r\n', '<br>')
rally_conf_file = ".rcbjenkins-rally-creds"
file_path = os.path.abspath(os.path.join(os.getenv("HOME"), rally_conf_file))
rally_config = StringIO(u'[default]\n%s' % open(file_path).read())
parser = ConfigParser.ConfigParser()
parser.readfp(rally_config)
rally_server = parser.get('default', 'server')
rally_user = parser.get('default', 'user')
rally_password = parser.get('default', 'password')
rally_project = parser.get('default', 'project')
# create rally object to interact with rally api
rally = Rally(rally_server, rally_user, rally_password, project=rally_project)
proj = rally.getProject()
def create_defect():
print "Filing defect against Project: %s" % rally_project
notes = '<a href="%s" target="_blank">Github issue #%s</a>' % (issue_url, issue_num)
defect_data = {'Name': '%s: %s' % (repo_name, title),
'State': 'Open',
'ScheduleState': 'Defined',
'Project': proj.ref,
'Notes': notes,
'Description': body}
try:
defect = rally.create('Defect', defect_data)
except Exception, details:
sys.stderr.write('ERROR: %s \n' % details)
sys.exit(1)
print "Defect created, ObjectID: %s FormattedID: %s" % (defect.oid,
defect.FormattedID)
# now update github issue by adding rally defect ID to the title
config_file = ".rcbjenkins-git-creds"
file_path = os.path.abspath(os.path.join(os.getenv("HOME"), config_file))
config = StringIO(u'[default]\n%s' % open(file_path).read())
parser = ConfigParser.ConfigParser()
parser.readfp(config)
github_user, github_pass = parser.get('default', 'user').split(':')
payload = {'title': '[%s]: %s' % (defect.FormattedID, title)}
try:
response = requests.patch(issue_api_url,
data=json.dumps(payload),
auth=(github_user, github_pass))
return_json = json.loads(response.content)
except Exception, details:
sys.stderr.write('ERROR: %s \n' % details)
sys.exit(1)
if response.ok:
print "updated github issue title successfully"
print "new title: %s" % json.dumps(return_json['title'])
else:
print "something went wrong when updating title"
print return_json
def create_discussion_item():
# get the defectID from the issue title
try:
defectID = re.search(r'\[([A-Z]+\d+)\]', title).group(1)
except AttributeError:
print "could not get defect ID from github issue title"
sys.exit(1)
# prepend some text to the body
global body
prepend = "<b>%s</b> said this on github:" % comment_user
body = prepend + '<br><br>' + '"' + body + '"'
print "Adding a new discussion item to defect [%s]" % defectID
# get the defect object so we can extract the oid
defect = rally.get('Defect', query='FormattedID = %s' % defectID, instance=True)
# build the discussion data dict
discussion_data = {"Artifact": defect.oid, "Text": body}
# create the discussion entry (or 'ConversationPost' in rally api parlance)
try:
discussion = rally.create('ConversationPost', discussion_data)
except Exception, details:
sys.stderr.write('ERROR: %s \n' % details)
sys.exit(1)
print "Defect updated with a new discussion entry"
if action == "opened":
create_defect()
elif action == "created":
create_discussion_item()