-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.py
136 lines (113 loc) · 4.97 KB
/
environment.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
import sys
import yaml
import requests
from requests.exceptions import HTTPError
from termcolor import colored
from datetime import date
from utils import Utils
class Environment(object):
""" Environment implementation. One for all """
_config = []
_instance = None
_url = None
_headers = None
_ticket_list = []
_utils = None
_auth = None
def __init__(self, env):
self._instance = env
self._config = self._init_config()
self._url = str(self._config['redmine_settings']['url'])
self._headers = {
'X-Redmine-API-Key': str(self._config['redmine_settings']['api_key'])}
self._utils = Utils()
self._auth = (self._config['http_auth']['username'],
self._config['http_auth']['pass'])
def check_connection(self):
""" Checking connection - retreiving list of tickets """
try:
res = requests.get(self._url + 'issues.' +
self._config['api_format'], headers=self._headers, auth=self._auth if self._config['http_auth']['enabled'] else '')
if res.status_code == 200:
print(colored('Connection is: 200. OK.', 'green'))
return True
print(colored('Connection is: ' + str(res.status_code) + '. KO', 'red'))
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
raise http_err
except:
print('Something went wrong!')
return False
return False
def group_ticket_list(self):
try:
full_url = self._url + 'issues.' + \
self._config['api_format'] + '?assigned_to_id=' + \
str(self._config['deployment_groups'][self._instance])
res = requests.get(full_url, headers=self._headers,
auth=self._auth if self._config['http_auth']['enabled'] else '')
# Prints list of tickets which will be added to a deployment.
self._create_tickets_list(res)
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
return False
except ConnectionError as ex:
print(ex.with_traceback)
return False
return True
def create_deployment_ticket(self):
"""
Create deployment ticket in 2 cases -
if we have anything to deploy and if user will confirm it.
"""
if len(self._ticket_list) <= 0:
return False
if self._utils.prompt('Do you want to create a ticket with this list?'):
try:
ticket_subject = self._config['issue_params']['subject'].format(
env=self._instance, date=date.today())
full_url = self._url + 'issues.' + self._config['api_format']
body = {'issue': {
'project_id': self._config['issue_params']['project_id'],
'priority_id': self._config['issue_params']['priority_id'],
'subject': ticket_subject,
'description': '\n'.join(map(str, self._ticket_list))
}}
res = requests.post(full_url, headers=self._headers, json=body,
auth=self._auth if self._config['http_auth']['enabled'] else '')
if res.status_code == 200 or res.status_code == 201:
result = self._utils.parse_response(
res, self._config['api_format'])
print(colored('Done!', 'green'))
print(colored(self._url + 'issues/' + str(result['issue']['id'])))
return True
print(res.content)
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except ConnectionError as ex:
print(ex.with_traceback)
@staticmethod
def get_config():
""" Load configuration """
with open('./config/config.yml', 'r') as stream:
try:
return yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
return False
# Protected section.
@classmethod
def _init_config(cls):
""" Load configuration """
return cls.get_config()
def _create_tickets_list(self, r: requests.Response):
""" Create a pretty formatter list from a redmine response """
print(colored('Issues list:', 'yellow'))
result = self._utils.parse_response(r, self._config['api_format'])
for value in self._utils.get_issues_list(result, self._config['api_format']):
issue_link = self._config['redmine_settings']['url'] + \
'issues/' + str(value['id'])
issue_title = value['subject']
self._ticket_list.append(issue_link + ' ' + issue_title)
print(colored(issue_link + ' ' + issue_title,
'yellow', attrs=['underline']))