forked from RedHatQE/firewatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjira_config_gen.py
90 lines (73 loc) · 2.97 KB
/
jira_config_gen.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
from pathlib import Path
import click
from jinja2 import Environment
from jinja2 import FileSystemLoader
from simple_logger.logger import get_logger
class JiraConfig:
def __init__(
self,
server_url: str,
token_path: str,
output_file: str,
template_path: str,
) -> None:
"""
Used to build the Jira configuration file for use in the report command.
Args:
server_url (str): Jira server URL, i.e "https://issues.stage.redhat.com"
token_path (str): Path to the file holding the Jira server API token
output_file (str): Where the rendered config will be stored.
template_path (str): Path to Jinja template used to generate Jira credentials. Defaults to /firewatch/cli/templates/jira.config.j2.
"""
self.logger = get_logger(__name__)
self.config_file_path = self.render_template(
server_url=server_url,
token=self.token(file_path=token_path),
output_file=output_file,
template_path=template_path,
)
def token(self, file_path: str) -> str:
"""
Reads the contents of file_path and returns it. The file_path should be the file that holds the Jira API token
Args:
file_path (str): The path to the file that holds the Jira API token
Returns:
str: A string object that represents the Jira API token
"""
try:
with open(file_path) as file:
return file.read().strip()
except Exception as ex:
self.logger.error(
f"Failed to read Jira token from {file_path}. error: {ex}",
)
raise click.Abort()
def render_template(
self,
server_url: str,
token: str,
output_file: str,
template_path: str,
) -> str:
"""
Uses Jinja to render the Jira configuration file
Args:
server_url (str): Jira server URL, i.e "https://issues.stage.redhat.com"
token (str): Jira server API token
output_file (str): Where the rendered config will be stored.
template_path (str): Path to Jinja template used to generate Jira credentials. Defaults to /firewatch/cli/templates/jira.config.j2.
Returns:
str: A string object that represents the path of the rendered template.
"""
template_dir = Path(template_path).parent
template_filename = Path(template_path).name
# Load Jinja template file
env = Environment(loader=FileSystemLoader(template_dir))
template = env.get_template(template_filename)
# Render the template
context = {"server_url": server_url, "token": token}
rendered_template = template.render(**context)
with open(output_file, "w") as file:
file.write(rendered_template)
self.logger.info(f"Config file written to {output_file}")
return output_file