11import logging
2- import os
3- import uuid
42
53from gitopscli .commands .create_preview import create_preview_command
64from gitopscli .git .create_git import create_git
75from gitopscli .io .tmp_dir import create_tmp_dir , delete_tmp_dir
86
9- # pylint: disable=too-many-statements
10-
117
128def 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 )
0 commit comments