-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjira_issues.py
218 lines (183 loc) · 6.47 KB
/
jira_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
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
from jira.client import JIRA
import logging
import time
import os
import json
bitbar_header = ['BB', '---']
# Use your email address as the username
USER="[email protected]"
# Use the token value as the password
# Create an API token here: https://id.atlassian.com/manage/api-tokens
PASSW="dummypassword"
SERVER="https://example.atlassian.net"
QUERY="assignee = currentUser() ORDER BY updatedDate DESC"
TOPRECENT=10
# Adjust title length of a ticket in status
STATUSLENGTH=80
# Adjust title length of a ticket in dropdown menu
TICKETLENGTH=80
NONSPRINT='[Non sprint]'
CACHE_FILE="jira_noti.cache"
CONFIG_FILE="jirabar.json"
TIME_OUT=3
def parseConfigFile():
print("parseConfigFile")
config = {}
global USER
global PASSW
global SERVER
global QUERY
global TOPRECENT
global STATUSLENGTH
global TICKETLENGTH
global CACHE_FILE
global TIME_OUT
if(not os.path.isfile(CONFIG_FILE)):
print("Create config file")
config = {
'jiraUser': USER,
'jiraApiKey': PASSW,
'server': SERVER,
'query': QUERY,
'toprecent': TOPRECENT,
'statuslength': STATUSLENGTH,
'ticketlength': TICKETLENGTH,
'cacheFile': CACHE_FILE,
'timeOut': TIME_OUT
}
with open(CONFIG_FILE, 'w') as configFile:
json.dump(config, configFile, sort_keys=True, indent=4)
else:
print("Parse config file")
with open(CONFIG_FILE, 'r') as configFile:
config = json.load(configFile)
USER = config['jiraUser']
PASSW = config['jiraApiKey']
SERVER = config['server']
QUERY = config['query']
TOPRECENT = config['toprecent']
STATUSLENGTH = config['statuslength']
TICKETLENGTH = config['ticketlength']
CACHE_FILE = config['cacheFile']
TIME_OUT = config['timeOut']
# Defines a function for connecting to Jira
def connect_jira(log, jira_server, jira_user, jira_password):
try:
log.info("Connecting to JIRA: %s" % jira_server)
jira_options = {'server': jira_server}
jira = JIRA(timeout=TIME_OUT, options=jira_options, basic_auth=(jira_user, jira_password), max_retries=0)
return jira
except Exception as e:
log.error("Failed to connect to JIRA: %s" % e)
return None
# Get bitbar status
def get_in_progress_item(issues):
myIssues=[]
mySprints={}
mySprints[NONSPRINT] = []
bitbar_header = ['']
###############################################################################
## header rows
####################
## drop down started
bitbar_header.append("%s" % ("---"))
## refer link to the in progress element
dashboard = 'LINK_TO_TOP'
bitbar_header.append("%s" % (dashboard))
## dashboard link
dashboard = 'Dashboard | href=' + SERVER + '/secure/Dashboard.jspa'
bitbar_header.append("%s" % (dashboard))
## add more custom link
###############################################################################
# filter out Closed or Blocked items
for issue in issues:
if (str(issue.fields.status) not in ('Closed', 'Blocked', 'New', 'Rejected', 'Test On Hold' , 'Stuck')):
myIssues.append(issue);
sprintName = ''
if(issue.fields.customfield_10007):
fieldIndex = 0
if(len(issue.fields.customfield_10007) > 1):
fieldIndex = 1
# get sprint name
if (issue.raw['fields']["customfield_10007"] and issue.raw['fields']["customfield_10007"][0]['name']):
sprintName = issue.raw['fields']["customfield_10007"][0]['name']
if(sprintName != ''):
mySprints[sprintName] = []
myIssues.sort(key=lambda x: x.fields.updated, reverse=True)
bitbar_header.append("%s" % ("---"))
recent = 'Recent(' + str(TOPRECENT) + '):'
bitbar_header.append("%s" % (recent))
###############################################################################
## TOP RECENT
####################
i = 0
for element in myIssues:
status=""
sprintName = ''
if(element.fields.customfield_10007):
fieldIndex = 0
if(len(element.fields.customfield_10007) > 1):
fieldIndex = 1
try:
sprintName = element.raw['fields']["customfield_10007"][0]['name']
except AttributeError:
sprintName = ''
# Create ticket with sprint name if it exsist
# <ID>(<status>) :: <Title>
issue_status = str(element.fields.status)
issue_status = issue_status.replace("&", "and")
status = status + str(element.key) + "(" + issue_status + ") :: " + str(element.fields.summary)
if(len(status) > TICKETLENGTH):
status = status[0:TICKETLENGTH] + '..'
else:
status = status[0:TICKETLENGTH]
status = status + " | href=" + SERVER + "/browse/" + str(element.key)
# adding ticket to sprint
if(sprintName):
status = sprintName + " # " + status
mySprints[sprintName].append("%s" % (status))
else:
mySprints[NONSPRINT].append("%s" % (status))
# just show top TOPRECENT tickets
if(i < TOPRECENT):
bitbar_header.append("%s" % (status))
if (issue_status not in ('Open', 'Prepare Testing')):
top_status_bar = str(element.key) + "(" + issue_status + ") :: " + str(element.fields.summary)
if(len(top_status_bar) > STATUSLENGTH):
top_status_bar = top_status_bar[0:STATUSLENGTH] + '..'
###############################################################################
## header element
####################
if(bitbar_header[0] == ''):
bitbar_header[0] = str("%s" % (top_status_bar))
## link to that elem
bitbar_header[2] = str("%s" % (status))
###############################################################################
i = i + 1
if(bitbar_header[0] == ''):
bitbar_header[0] = '(-) :: No "In progress" ticket'
###############################################################################
content = '\n'.join(bitbar_header)
# print(content)
with open(CACHE_FILE, "w+") as f:
f.write(content)
f.write("\n---\n")
f.write("cached\n")
f.write(time.ctime())
f.close()
return (content)
def getParsedIssues():
log = logging.getLogger(__name__)
parseConfigFile()
if(USER == "[email protected]" or PASSW == "dummypassword"):
return "Missing Credentials for JIRA! Please update the config file! Restart after edit!"
jira = connect_jira(log, SERVER, USER, PASSW)
log = ""
if ( jira is not None):
issues = jira.search_issues(QUERY)
if(len(issues) > 0):
log = get_in_progress_item(issues)
else:
bitbar_header = ['No jira issue', '---', 'Connection error?']
log = ('\n'.join(bitbar_header))
return log