/
ticket.py
81 lines (63 loc) · 2.58 KB
/
ticket.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
from atlassian import Jira
import json
import os
import sys
GET_TICKETS_TO_DEPLOY = os.getenv('TICKETS_TO_DEPLOY', False)
class DeploymentTicket:
def __init__(self):
__ATLASSSIAN_BASE_URI = os.getenv('ATLASSSIAN_BASE_URI', False)
__ATLASSIAN_USER = os.getenv('ATLASSIAN_USER', False)
__ATLASSIAN_API_KEY = os.getenv('ATLASSIAN_API_KEY', False)
if any([__ATLASSSIAN_BASE_URI is False, __ATLASSIAN_USER is False, __ATLASSIAN_API_KEY is False]):
raise Exception('One or more environment variables are not set')
self.jira = Jira(
url=__ATLASSSIAN_BASE_URI,
username=__ATLASSIAN_USER,
password=__ATLASSIAN_API_KEY)
def getTickets(self):
JQL = GET_TICKETS_TO_DEPLOY
data = self.jira.jql(JQL)
if 'total' not in data:
sys.exit(data)
if data['total'] > 0:
print(self.getHeader(data))
print(self.getBody(data['issues']))
print(self.getFooter())
else:
print("No issues ready to deploy at this moment")
def getAssigne(self, value, defaultValue):
if value is None:
return defaultValue
else:
return value['key']
def getHeader(self, data):
total = data['total']
tickets = ''
for record in data['issues']:
tickets += '{},'.format(record['key'])
header = 'Total Ticket Count: *{}* [Filter|{}/issues/?jql=Key%20in%20({})] \n\n'.format(
total, self.jira.url, tickets)
header += 'h2. Migrations \n'
header += 'Please review the PRs for each ticket to see if there are any migrations and put in this section \n\n'
header += 'h2. Tickets By Engineer: \n'
return header
def getBody(self, issues):
body = ''
assignee = ''
for record in issues:
currentAssignee = self.getAssigne(
record['fields']['assignee'], 'Undefined')
if assignee != currentAssignee:
body += '[~{}] \n'.format(currentAssignee)
assignee = currentAssignee
body += '# {} {} \n'.format(record['key'],
record['fields']['summary'].encode('utf-8', errors='ignore'))
return body
def getFooter(self):
footer = 'commit: \n'
footer += 'pipeline: \n'
footer += 'Description was autogenerated by [this tool|https://github.com/Andresjz/ticketdeploy]'
return footer
if __name__ == '__main__':
deploymentTicket = DeploymentTicket()
deploymentTicket.getTickets()