forked from CMSCompOps/WmAgentScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JIRAClient.py
executable file
·227 lines (194 loc) · 7.52 KB
/
JIRAClient.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from subprocess import PIPE,Popen
import sys,os,json
import time
from utils import sendLog
import socket
# JIRA requires a peculiar combination of package versions sometimes
# Github issue: https://github.com/CMSCompOps/WmAgentScripts/issues/454
try:
import jira
except ImportError as e:
cmd1 = 'sudo yum remove python-requests python-urllib3 -y'
cmd2 = 'sudo pip install --upgrade --force-reinstall requests urllib3'
print("Error importing jira: {}\nDoing the following commands: \n\t{}\n\t{}".format(str(e),cmd1, cmd2))
cmd1out, cmd1err = Popen(cmd1, shell=True, stderr=PIPE, stdout=PIPE).communicate()
print(cmd1out)
print(cmd1err)
cmd2out, cmd2err = Popen(cmd2, shell=True, stderr=PIPE, stdout=PIPE).communicate()
print(cmd2out)
print(cmd2err)
#import jira # It doesn't seem to work properly right away when installing via subprocess
# Just exit and let the next Unified cycle restart it
print("Exiting...")
print("Testing Sharad's theory of collectd errors")
sys.exit(-1)
class JIRAClient:
def __init__(self, debug=False,cookie=None):
self.server='https://its.cern.ch/jira'
cookie = os.environ.get('JIRA_SSO_COOKIE', cookie)
cookies = {}
try:
print "using cookie from", cookie
for l in open(cookie,'r').read().split('\n'):
try:
s = l.split()
if s[5] in ['JSESSIONID','atlassian.xsrf.token']:
cookies[ s[5] ] = s[6]
except:
pass
except:
print cookie,"is not a file?"
print "run cern-get-sso-cookie -u https://its.cern.ch/jira/loginCern.jsp -o %s --krb, or something like that"%cookie
#print cookies
if not cookies:
print "That ain't going to work out"
sys.exit(1)
self.client = jira.JIRA('https://its.cern.ch/jira' , options = {'cookies': cookies})
def last_time(self, j):
try:
j = self.get(j.key)
except:
j = self.get(j)
last_comment_time = self.time_to_time(j.fields.comment.comments[-1].updated) if (hasattr(j.fields, 'comment') and j.fields.comment.comments) else self.created(j)
return last_comment_time
def create_or_last(self, prepid, priority=None, label=None, reopen=False):
jiras = self.find( {'prepid' : prepid})
j = None
created = False
reopened = False
if len(jiras)==0:
c_doc = { 'summary' : prepid,
'description' : 'https://dmytro.web.cern.ch/dmytro/cmsprodmon/workflows.php?prep_id=%s \nAutomatic JIRA from unified'%( prepid )}
if priority: c_doc['priority'] = priority
if label: c_doc['label'] = label
j = self.create(c_doc)
created = True
else:
j = sorted(jiras, key= lambda o:self.created(o))[-1]
if reopen:
reopened = self.reopen( j.key )
return j,reopened,created
def create(self , indications , do = True):
fields = {
'project' : 'CMSCOMPPR',
'issuetype' : {'id' : "3"}
}
label = indications.get('label',None)
who = {
'WorkflowTrafficController' : 'haozturk',
'UnifiedOfficer' : 'snorberg',
'AgentDoc' : 'jen_a',
'Late' : None
}.get(label,None)
if label:
fields['labels'] = [ label ]
if who:
fields['assignee'] = {'name':who,'key':who}
summary = indications.get('summary',None)
if summary:
fields['summary'] = summary
description = indications.get('description', None)
if description:
fields['description'] = description
priority = indications.get('priority', None)
if type(priority) == int:
## transform to a str
#needs decision : 6
#blocker : 1
#critical : 2
#major : 3
#minor : 4
prios = {
0 : '4',
85000 : '3',
110000 : '2'
}
set_to = None
for p in sorted(prios.keys()):
if priority>=p:
set_to = prios[p]
priority = set_to
elif (priority and not priority.isdigit()):
priority = { 'decision' : '6',
'blocker' : '1'}.get(priority, None)
if priority:
fields['priority'] = {'id' : priority}
print fields
if do:
i = self.client.create_issue( fields)
return i
def find(self ,specifications):
query = 'project=CMSCOMPPR'
summary = specifications.get('prepid',specifications.get('summary',None))
if summary:
query += ' AND summary~"%s"'%(summary)
if specifications.get('status',None):
status = specifications['status']
if status.startswith('!'):
query += ' AND status != %s'%(status[1:])
else:
query += ' AND status = %s'%(status)
if specifications.get('label',None):
label = specifications['label']
query += ' AND labels = %s'%label
if specifications.get('text',None):
string = specifications['text']
query += ' AND text ~ "%s"'% string
return self._find( query )
def comment(self, key, comment):
if not comment: return
self.client.add_comment( key, comment )
def _find(self, query):
return self.client.search_issues( query ,maxResults=-1)
def get(self, jid):
return self.client.issue( jid )
def time_to_time(self, time_str):
ts,aux = time_str.split('.')
if '+' in aux:
s = int(aux.split('+')[-1])/100.
else:
s = -int(aux.split('-')[-1])/100.
t = time.mktime(time.strptime( ts, "%Y-%m-%dT%H:%M:%S")) - s*60*60
return t
def created(self,j):
return self.time_to_time( j.fields.created )
def _transition(self, status, jid):
to = {'closed' : '2',
'reopened' : '3',
'progress' : '4'
}.get( status , None)
if to:
try:
print jid,"to",status
self.client.transition_issue( jid, to)
return True
except Exception as e:
print "transition to",status,"not successful"
print str(e)
return False
else:
print "transition to",status,"not known"
return False
def progress(self, jid):
return self._transition('progress', jid)
def reopen(self, jid):
return self._transition('reopened', jid)
def close(self, jid):
return self._transition('closed', jid)
if __name__ == "__main__":
JC = JIRAClient(cookie = 'jira.txt')
i= JC.get('CMSCOMPPR-4516')
print i.fields.summary
ii = JC.find({'prepid' : 'SUS-RunIISummer16MiniAODv3-00261'})
print [io.key for io in ii]
#JC.reopen('CMSCOMPPR-4518')
#JC.progress('CMSCOMPPR-4518')
#JC.close('CMSCOMPPR-4518')
#JC.create( {
# 'priority' : 120000,
# 'summary' : 'automatic',
# 'label' : 'WorkflowTrafficController',
# 'description' : 'Automatic JIRA from unified'},
# do = False)
ii = JC.find({'summary' : 'vocms0253.cern.ch heartbeat issues'})
print [time.asctime(time.gmtime(JC.created(io))) for io in sorted(ii, key=lambda o:JC.created(o))]