Skip to content

Commit

Permalink
Merge pull request #110 from baloise/feat/delete-preview-expect-previ…
Browse files Browse the repository at this point in the history
…ew-exists-flag

feat(delete-preview,delete-pr-preview): add  flag --expect-preview-exists
  • Loading branch information
christiansiegel committed Oct 1, 2020
2 parents 751dfd0 + 17be32c commit 24eb4aa
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 27 deletions.
19 changes: 12 additions & 7 deletions docs/commands/delete-pr-preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ gitopscli delete-pr-preview \

## Usage
```
usage: gitopscli delete-pr-preview [-h] --username USERNAME --password PASSWORD
[--git-user GIT_USER] [--git-email GIT_EMAIL]
--organisation ORGANISATION --repository-name
REPOSITORY_NAME [--git-provider GIT_PROVIDER]
[--git-provider-url GIT_PROVIDER_URL] --branch
BRANCH
[-v [VERBOSE]]
usage: gitopscli delete-pr-preview [-h] --username USERNAME --password
PASSWORD [--git-user GIT_USER]
[--git-email GIT_EMAIL] --organisation
ORGANISATION --repository-name
REPOSITORY_NAME
[--git-provider GIT_PROVIDER]
[--git-provider-url GIT_PROVIDER_URL]
--branch BRANCH
[--expect-preview-exists [EXPECT_PREVIEW_EXISTS]]
[-v [VERBOSE]]
optional arguments:
-h, --help show this help message and exit
Expand All @@ -43,6 +46,8 @@ optional arguments:
Git provider base API URL (e.g.
https://bitbucket.example.tld)
--branch BRANCH The branch for which the preview was created for
--expect-preview-exists [EXPECT_PREVIEW_EXISTS]
Fail if preview does not exist
-v [VERBOSE], --verbose [VERBOSE]
Verbose exception logging
```
10 changes: 7 additions & 3 deletions docs/commands/delete-preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ usage: gitopscli delete-preview [-h] --username USERNAME --password PASSWORD
[--git-user GIT_USER] [--git-email GIT_EMAIL]
--organisation ORGANISATION --repository-name
REPOSITORY_NAME [--git-provider GIT_PROVIDER]
[--git-provider-url GIT_PROVIDER_URL] --preview-id
PREVIEW_ID
[--git-provider-url GIT_PROVIDER_URL]
--preview-id PREVIEW_ID
[--expect-preview-exists [EXPECT_PREVIEW_EXISTS]]
[-v [VERBOSE]]
optional arguments:
Expand All @@ -42,7 +43,10 @@ optional arguments:
--git-provider-url GIT_PROVIDER_URL
Git provider base API URL (e.g.
https://bitbucket.example.tld)
--preview-id PREVIEW_ID The preview id for which the preview was created for
--preview-id PREVIEW_ID
The preview-id for which the preview was created for
--expect-preview-exists [EXPECT_PREVIEW_EXISTS]
Fail if preview does not exist
-v [VERBOSE], --verbose [VERBOSE]
Verbose exception logging
```
13 changes: 13 additions & 0 deletions gitopscli/cliparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def __add_delete_preview_command_parser(subparsers):
add_delete_preview_p.add_argument(
"--preview-id", help="The preview-id for which the preview was created for", required=True
)
__add_expect_preview_exists_parser(add_delete_preview_p)
__add_verbose_parser(add_delete_preview_p)


Expand All @@ -101,6 +102,7 @@ def __add_delete_pr_preview_command_parser(subparsers):
add_delete_preview_p.add_argument(
"--branch", help="The branch for which the preview was created for", required=True
)
__add_expect_preview_exists_parser(add_delete_preview_p)
__add_verbose_parser(add_delete_preview_p)


Expand Down Expand Up @@ -146,6 +148,17 @@ def __add_create_prid_parser(subparsers):
subparsers.add_argument("--parent-id", help="the id of the parent comment, in case of a reply", type=int)


def __add_expect_preview_exists_parser(subparsers):
subparsers.add_argument(
"--expect-preview-exists",
help="Fail if preview does not exist",
type=__str2bool,
nargs="?",
const=True,
default=False,
)


def __add_verbose_parser(subparsers):
subparsers.add_argument(
"-v", "--verbose", help="Verbose exception logging", type=__str2bool, nargs="?", const=True, default=False,
Expand Down
2 changes: 2 additions & 0 deletions gitopscli/commands/delete_pr_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def delete_pr_preview_command(
repository_name,
git_provider,
git_provider_url,
expect_preview_exists,
):
assert command == "delete-pr-preview"

Expand Down Expand Up @@ -73,6 +74,7 @@ def delete_pr_preview_command(
git_provider,
git_provider_url,
branch,
expect_preview_exists,
)

finally:
Expand Down
38 changes: 25 additions & 13 deletions gitopscli/commands/delete_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def delete_preview_command(
git_provider,
git_provider_url,
preview_id,
expect_preview_exists,
):

assert command is not None
Expand All @@ -41,12 +42,12 @@ def delete_preview_command(
)

apps_git.checkout("master")
logging.info("App repo branch master checkout successful")
logging.info("App repo '%s/%s' branch 'master' checkout successful", organisation, repository_name)
try:
gitops_config = GitOpsConfig(apps_git.get_full_file_path(".gitops.config.yaml"))
except FileNotFoundError as ex:
raise GitOpsException(f"Couldn't find .gitops.config.yaml") from ex
logging.info("Read GitOpsConfig: %s", gitops_config)
logging.info("Read .gitops.config.yaml")

root_git = create_git(
username,
Expand All @@ -60,22 +61,33 @@ def delete_preview_command(
root_tmp_dir,
)
root_git.checkout("master")
logging.info("Config repo branch master checkout successful")
config_branch = "master"
logging.info(
"Config repo '%s/%s' branch 'master' checkout successful",
gitops_config.team_config_org,
gitops_config.team_config_repo,
)
hashed_preview_id = hashlib.sha256(preview_id.encode("utf-8")).hexdigest()[:8]
preview_folder_name = gitops_config.application_name + "-" + hashed_preview_id + "-preview"
logging.info("Preview folder name: %s", preview_folder_name)
branch_preview_env_exists = os.path.exists(root_git.get_full_file_path(preview_folder_name))
logging.info("Is preview env already existing for branch? %s", branch_preview_env_exists)
preview_folder_full_path = root_git.get_full_file_path(preview_folder_name)
branch_preview_env_exists = os.path.exists(preview_folder_full_path)

if expect_preview_exists and not branch_preview_env_exists:
raise GitOpsException(f"There was no preview with name: {preview_folder_name}")

if branch_preview_env_exists:
shutil.rmtree(root_git.get_full_file_path(preview_folder_name), ignore_errors=True)
shutil.rmtree(preview_folder_full_path, ignore_errors=True)
root_git.commit(
f"Delete preview environment for '{gitops_config.application_name}' and preview id '{preview_id}'."
)
root_git.push("master")
logging.info("Pushed branch 'master'")
else:
raise GitOpsException(f"There was no preview with name: {preview_folder_name}")
root_git.commit(
f"Delete preview environment for '{gitops_config.application_name}' and preview id '{preview_id}'."
)
root_git.push(config_branch)
logging.info("Pushed branch %s", config_branch)
logging.info(
"No preview environment for '%s' and preview id '%s'. Nothing to do..",
gitops_config.application_name,
preview_id,
)

finally:
delete_tmp_dir(apps_tmp_dir)
Expand Down
Loading

0 comments on commit 24eb4aa

Please sign in to comment.