Skip to content

Commit aa94164

Browse files
some cleanup + improve namings and logs
1 parent aa2a15c commit aa94164

2 files changed

Lines changed: 43 additions & 63 deletions

File tree

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import logging
2-
import os
3-
import uuid
42

53
from gitopscli.commands.create_preview import create_preview_command
64
from gitopscli.git.create_git import create_git
75
from gitopscli.io.tmp_dir import create_tmp_dir, delete_tmp_dir
86

9-
# pylint: disable=too-many-statements
10-
117

128
def create_pr_preview_command(
139
command,
@@ -25,7 +21,6 @@ def create_pr_preview_command(
2521
assert command == "create-pr-preview"
2622

2723
apps_tmp_dir = create_tmp_dir()
28-
root_tmp_dir = create_tmp_dir()
2924

3025
try:
3126
apps_git = create_git(
@@ -55,66 +50,54 @@ def create_pr_preview_command(
5550
git_provider,
5651
git_provider_url,
5752
git_hash,
58-
pr_branch,
59-
create_deployment_already_up_to_date_callback(parent_id, pr_id),
60-
create_deployment_exist_callback(parent_id, pr_id, pr_branch),
61-
create_deployment_new_callback(parent_id, pr_id, pr_branch),
53+
pr_branch, # <- preview_id
54+
__create_deployment_already_up_to_date_callback(parent_id, pr_id),
55+
__create_deployment_exist_callback(parent_id, pr_id, pr_branch),
56+
__create_deployment_new_callback(parent_id, pr_id, pr_branch),
6257
)
6358
finally:
6459
delete_tmp_dir(apps_tmp_dir)
65-
delete_tmp_dir(root_tmp_dir)
6660

6761

68-
def create_deployment_already_up_to_date_callback(parent_id, pr_id):
62+
def __create_deployment_already_up_to_date_callback(parent_id, pr_id):
6963
def deployment_already_up_to_date_callback(apps_git, new_image_tag):
70-
logging.info("The image tag %s has already been deployed. Doing nothing.", new_image_tag)
71-
pr_comment_text = f"The version `{new_image_tag}` has already been deployed. Nothing to do here."
72-
logging.info("Creating PullRequest comment for pr with id %s and content: %s", pr_id, pr_comment_text)
73-
apps_git.add_pull_request_comment(pr_id, pr_comment_text, parent_id)
64+
__add_pull_request_comment(
65+
apps_git, pr_id, parent_id, f"The version `{new_image_tag}` has already been deployed. Nothing to do here."
66+
)
7467

7568
return deployment_already_up_to_date_callback
7669

7770

78-
def create_deployment_new_callback(parent_id, pr_id, pr_branch):
71+
def __create_deployment_new_callback(parent_id, pr_id, pr_branch):
7972
def deployment_new_callback(apps_git, gitops_config, route_host):
80-
pr_comment_text = f"""\
81-
New preview environment for `{gitops_config.application_name}` and branch `{pr_branch}` created successfully. Access it here:
82-
https://{route_host}"""
83-
logging.info("Creating PullRequest comment for pr with id %s and content: %s", pr_id, pr_comment_text)
84-
apps_git.add_pull_request_comment(pr_id, pr_comment_text, parent_id)
73+
app_name = gitops_config.application_name
74+
__add_pull_request_comment(
75+
apps_git,
76+
pr_id,
77+
parent_id,
78+
f"""\
79+
New preview environment for `{app_name}` and branch `{pr_branch}` created successfully. Access it here:
80+
https://{route_host}""",
81+
)
8582

8683
return deployment_new_callback
8784

8885

89-
def create_deployment_exist_callback(parent_id, pr_id, pr_branch):
86+
def __create_deployment_exist_callback(parent_id, pr_id, pr_branch):
9087
def deployment_exist_callback(apps_git, gitops_config, route_host):
91-
pr_comment_text = f"""\
92-
Preview environment for `{gitops_config.application_name}` and branch `{pr_branch}` updated successfully. Access it here:
93-
https://{route_host}"""
94-
logging.info("Creating PullRequest comment for pr with id %s and content: %s", pr_id, pr_comment_text)
95-
apps_git.add_pull_request_comment(pr_id, pr_comment_text, parent_id)
88+
app_name = gitops_config.application_name
89+
__add_pull_request_comment(
90+
apps_git,
91+
pr_id,
92+
parent_id,
93+
f"""\
94+
Preview environment for `{app_name}` and branch `{pr_branch}` updated successfully. Access it here:
95+
https://{route_host}""",
96+
)
9697

9798
return deployment_exist_callback
9899

99100

100-
def __create_pullrequest(branch, gitops_config, root_git):
101-
title = "Updated preview environment for " + gitops_config.application_name
102-
description = f"""\
103-
This Pull Request is automatically created through [gitopscli](https://github.com/baloise/gitopscli)."""
104-
pull_request = root_git.create_pull_request(branch, "master", title, description)
105-
logging.info("Pull request created: %s", {root_git.get_pull_request_url(pull_request)})
106-
return pull_request
107-
108-
109-
def __merge_pullrequest(branch, pull_request, root_git):
110-
root_git.merge_pull_request(pull_request)
111-
logging.info("Pull request merged")
112-
root_git.delete_branch(branch)
113-
logging.info("Branch '%s' deleted", branch)
114-
115-
116-
def __create_tmp_dir():
117-
tmp_dir = f"/tmp/gitopscli/{uuid.uuid4()}"
118-
os.makedirs(tmp_dir)
119-
logging.info("Created directory %s", tmp_dir)
120-
return tmp_dir
101+
def __add_pull_request_comment(apps_git, pr_id, parent_id, pr_comment_text):
102+
logging.info("Creating PullRequest comment for pr with id %s and content: %s", pr_id, pr_comment_text)
103+
apps_git.add_pull_request_comment(pr_id, pr_comment_text, parent_id)

gitopscli/commands/create_preview.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
from gitopscli.gitops_exception import GitOpsException
1111

1212

13-
# pylint: disable=too-many-statements
14-
15-
1613
def create_preview_command(
1714
username,
1815
password,
@@ -66,7 +63,6 @@ def create_preview_command(
6663
root_git.checkout("master")
6764
logging.info("Config repo branch master checkout successful")
6865

69-
config_branch = f"master"
7066
preview_template_folder_name = ".preview-templates/" + gitops_config.application_name
7167
if os.path.isdir(root_git.get_full_file_path(preview_template_folder_name)):
7268
logging.info("Using the preview template folder: %s", preview_template_folder_name)
@@ -76,17 +72,17 @@ def create_preview_command(
7672
hashed_preview_id = hashlib.sha256(preview_id.encode("utf-8")).hexdigest()[:8]
7773
new_preview_folder_name = gitops_config.application_name + "-" + hashed_preview_id + "-preview"
7874
logging.info("New folder for preview: %s", new_preview_folder_name)
79-
branch_preview_env_already_exist = os.path.isdir(root_git.get_full_file_path(new_preview_folder_name))
80-
logging.info("Is preview env already existing for branch? %s", branch_preview_env_already_exist)
81-
if not branch_preview_env_already_exist:
75+
preview_env_already_exist = os.path.isdir(root_git.get_full_file_path(new_preview_folder_name))
76+
logging.info("Is preview env already existing? %s", preview_env_already_exist)
77+
if not preview_env_already_exist:
8278
__create_new_preview_env(
8379
git_hash,
8480
new_preview_folder_name,
8581
preview_template_folder_name,
8682
root_git,
8783
gitops_config.application_name,
8884
)
89-
logging.info("Using image tag from last app repo commit: %s", git_hash)
85+
logging.info("Using image tag from git hash: %s", git_hash)
9086
route_host = None
9187
value_replaced = False
9288
for replacement in gitops_config.replacements:
@@ -101,20 +97,21 @@ def create_preview_command(
10197
value_replaced,
10298
)
10399
if not value_replaced:
100+
logging.info("The image tag %s has already been deployed. Doing nothing.", git_hash)
104101
if deployment_already_up_to_date_callback:
105102
deployment_already_up_to_date_callback(apps_git, git_hash)
106103
return
107104

108-
if branch_preview_env_already_exist:
105+
root_git.commit(f"Update preview environment for '{gitops_config.application_name}' and git hash '{git_hash}'.")
106+
root_git.push("master")
107+
logging.info("Pushed branch master")
108+
109+
if preview_env_already_exist:
109110
if deployment_exists_callback:
110111
deployment_exists_callback(apps_git, gitops_config, route_host)
111112
else:
112113
if deployment_new_callback:
113114
deployment_new_callback(apps_git, gitops_config, route_host)
114-
115-
root_git.commit(f"Update preview environment for '{gitops_config.application_name}' and git hash '{git_hash}'.")
116-
root_git.push(config_branch)
117-
logging.info("Pushed branch %s", config_branch)
118115
finally:
119116
delete_tmp_dir(apps_tmp_dir)
120117
delete_tmp_dir(root_tmp_dir)
@@ -127,7 +124,7 @@ def __replace_value(
127124
replacement,
128125
root_git,
129126
route_host,
130-
shortened_branch_hash,
127+
hashed_preview_id,
131128
value_replaced,
132129
):
133130
replacement_value = None
@@ -137,7 +134,7 @@ def __replace_value(
137134
if replacement_variable == "GIT_COMMIT":
138135
replacement_value = new_image_tag
139136
elif replacement_variable == "ROUTE_HOST":
140-
route_host = gitops_config.route_host.replace("{SHA256_8CHAR_BRANCH_HASH}", shortened_branch_hash)
137+
route_host = gitops_config.route_host.replace("{SHA256_8CHAR_BRANCH_HASH}", hashed_preview_id)
141138
logging.info("Created route host: %s", route_host)
142139
replacement_value = route_host
143140
else:

0 commit comments

Comments
 (0)