|
| 1 | +import hashlib |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import sys |
| 6 | +import uuid |
| 7 | + |
| 8 | +from gitopscli.git.create_git import create_git |
| 9 | +from gitopscli.yaml.gitops_config import GitOpsConfig |
| 10 | + |
| 11 | + |
| 12 | +def delete_preview_command( |
| 13 | + command, |
| 14 | + branch, |
| 15 | + username, |
| 16 | + password, |
| 17 | + git_user, |
| 18 | + git_email, |
| 19 | + create_pr, |
| 20 | + auto_merge, |
| 21 | + organisation, |
| 22 | + repository_name, |
| 23 | + git_provider, |
| 24 | + git_provider_url, |
| 25 | +): |
| 26 | + assert command == "delete-preview" |
| 27 | + |
| 28 | + apps_tmp_dir = __create_tmp_dir() |
| 29 | + root_tmp_dir = __create_tmp_dir() |
| 30 | + |
| 31 | + try: |
| 32 | + apps_git = create_git( |
| 33 | + username, |
| 34 | + password, |
| 35 | + git_user, |
| 36 | + git_email, |
| 37 | + organisation, |
| 38 | + repository_name, |
| 39 | + git_provider, |
| 40 | + git_provider_url, |
| 41 | + apps_tmp_dir, |
| 42 | + ) |
| 43 | + |
| 44 | + apps_git.checkout(branch) |
| 45 | + logging.info("App repo branch %s checkout successful", branch) |
| 46 | + shortened_branch_hash = hashlib.sha256(branch.encode("utf-8")).hexdigest()[:8] |
| 47 | + logging.info("Hashed branch %s to hash: %s", branch, shortened_branch_hash) |
| 48 | + gitops_config = GitOpsConfig(apps_git.get_full_file_path(".gitops.config.yaml")) |
| 49 | + logging.info("Read GitOpsConfig: %s", gitops_config) |
| 50 | + |
| 51 | + root_git = create_git( |
| 52 | + username, |
| 53 | + password, |
| 54 | + git_user, |
| 55 | + git_email, |
| 56 | + gitops_config.team_config_org, |
| 57 | + gitops_config.team_config_repo, |
| 58 | + git_provider, |
| 59 | + git_provider_url, |
| 60 | + root_tmp_dir, |
| 61 | + ) |
| 62 | + root_git.checkout("master") |
| 63 | + logging.info("Config repo branch master checkout successful") |
| 64 | + root_git.new_branch(branch) |
| 65 | + logging.info("Created branch %s in config repo", branch) |
| 66 | + new_preview_folder_name = gitops_config.application_name + "-" + shortened_branch_hash + "-preview" |
| 67 | + logging.info("New folder for preview: %s", new_preview_folder_name) |
| 68 | + branch_preview_env_exists = os.path.exists(root_git.get_full_file_path(new_preview_folder_name)) |
| 69 | + logging.info("Is preview env already existing for branch? %s", branch_preview_env_exists) |
| 70 | + if branch_preview_env_exists: |
| 71 | + shutil.rmtree(root_git.get_full_file_path(new_preview_folder_name), ignore_errors=True) |
| 72 | + else: |
| 73 | + logging.info("There was no preview with name: %s", new_preview_folder_name) |
| 74 | + sys.exit(0) |
| 75 | + root_git.commit(f"Deleted preview environment for {gitops_config.application_name} and branch {branch}.") |
| 76 | + root_git.push(branch) |
| 77 | + logging.info("Pushed branch %s", branch) |
| 78 | + |
| 79 | + finally: |
| 80 | + shutil.rmtree(apps_tmp_dir, ignore_errors=True) |
| 81 | + shutil.rmtree(root_tmp_dir, ignore_errors=True) |
| 82 | + if create_pr and branch != "master": |
| 83 | + pull_request = __create_pullrequest(branch, gitops_config, root_git) |
| 84 | + if auto_merge: |
| 85 | + __merge_pullrequest(branch, pull_request, root_git) |
| 86 | + |
| 87 | + |
| 88 | +def __create_pullrequest(branch, gitops_config, root_git): |
| 89 | + title = "Updated preview environemnt for " + gitops_config.application_name |
| 90 | + description = f""" |
| 91 | +This Pull Request is automatically created through [gitopscli](https://github.com/baloise-incubator/gitopscli). |
| 92 | +""" |
| 93 | + pull_request = root_git.create_pull_request(branch, "master", title, description) |
| 94 | + logging.info("Pull request created: %s", {root_git.get_pull_request_url(pull_request)}) |
| 95 | + return pull_request |
| 96 | + |
| 97 | + |
| 98 | +def __merge_pullrequest(branch, pull_request, root_git): |
| 99 | + root_git.merge_pull_request(pull_request) |
| 100 | + logging.info("Pull request merged") |
| 101 | + root_git.delete_branch(branch) |
| 102 | + logging.info("Branch '%s' deleted", branch) |
| 103 | + |
| 104 | + |
| 105 | +def __create_tmp_dir(): |
| 106 | + tmp_dir = f"/tmp/gitopscli/{uuid.uuid4()}" |
| 107 | + os.makedirs(tmp_dir) |
| 108 | + logging.info("Created directory %s", tmp_dir) |
| 109 | + return tmp_dir |
0 commit comments