Skip to content

Commit 58c10b1

Browse files
Merge pull request #72 from baloise-incubator/feat/remove-branch-arg
Remove --branch arg
2 parents d45bce0 + 5baad8e commit 58c10b1

11 files changed

Lines changed: 85 additions & 65 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
44
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
55

66
## [Unreleased]
7+
### Changed
8+
- The `--branch` arg of the `delete-preview` command now only refers to the app repo branch. The config repo changes are pushed to `master` by default or to a randomly created branch name if `--create_pr` is given.
9+
10+
### Removed
11+
- The `--branch` arg was removed for commands `deploy` and `create-preview`. For `create-preview` the app repo branch is detected via the `--pr_id`. All changes in the config repo are pushed to `master` by default, or to a randomly created branch name if `--create_pr` is given.
712

813
## [2.1.0] - 2020-02-09
914
### Added

docs/commands/create-preview.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,18 @@ gitopscli create-preview \
7070
--organisation "my-team" \
7171
--repository-name "app-xy" \
7272
--pr-id 4711 \
73-
--branch "my-pr-branch" \
7473
--create-pr \
7574
--auto-merge
7675
```
7776

78-
!!! warning "The `--branch` parameter is currently used to locate the PR branch in the *app repository* as well as to create the change branch in the *deployment config repository*. This may lead to branch collisions in the config repository. Hence, the current behavior may be subject to change in the near future. [(Issue #40)](https://github.com/baloise-incubator/gitopscli/issues/40)"
79-
8077
## Usage
8178
```
8279
usage: gitopscli create-preview [-h] --username USERNAME --password PASSWORD
8380
[--git-user GIT_USER] [--git-email GIT_EMAIL]
8481
--organisation ORGANISATION --repository-name
8582
REPOSITORY_NAME [--git-provider GIT_PROVIDER]
8683
[--git-provider-url GIT_PROVIDER_URL]
87-
[--branch BRANCH] [--create-pr [CREATE_PR]]
84+
[--create-pr [CREATE_PR]]
8885
[--auto-merge [AUTO_MERGE]] --pr-id PR_ID
8986
[--parent-id PARENT_ID] [-v [VERBOSE]]
9087
@@ -104,10 +101,8 @@ optional arguments:
104101
--git-provider-url GIT_PROVIDER_URL
105102
Git provider base API URL (e.g.
106103
https://bitbucket.example.tld)
107-
--branch BRANCH Branch to push the changes to
108104
--create-pr [CREATE_PR]
109-
Creates a Pull Request (only when --branch is not
110-
master/default branch)
105+
Creates a Pull Request
111106
--auto-merge [AUTO_MERGE]
112107
Automatically merge the created PR (only valid with
113108
--create-pr)

docs/commands/delete-preview.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ usage: gitopscli delete-preview [-h] --username USERNAME --password PASSWORD
2424
[--git-user GIT_USER] [--git-email GIT_EMAIL]
2525
--organisation ORGANISATION --repository-name
2626
REPOSITORY_NAME [--git-provider GIT_PROVIDER]
27-
[--git-provider-url GIT_PROVIDER_URL]
28-
[--branch BRANCH] [--create-pr [CREATE_PR]]
27+
[--git-provider-url GIT_PROVIDER_URL] --branch
28+
BRANCH [--create-pr [CREATE_PR]]
2929
[--auto-merge [AUTO_MERGE]] [-v [VERBOSE]]
3030
3131
optional arguments:
@@ -44,10 +44,9 @@ optional arguments:
4444
--git-provider-url GIT_PROVIDER_URL
4545
Git provider base API URL (e.g.
4646
https://bitbucket.example.tld)
47-
--branch BRANCH Branch to push the changes to
47+
--branch BRANCH The branch for which the preview was created for
4848
--create-pr [CREATE_PR]
49-
Creates a Pull Request (only when --branch is not
50-
master/default branch)
49+
Creates a Pull Request
5150
--auto-merge [AUTO_MERGE]
5251
Automatically merge the created PR (only valid with
5352
--create-pr)

docs/commands/deploy.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ gitopscli deploy \
2727
--organisation "deployment" \
2828
--repository-name "incubator-non-prod" \
2929
--file "example/values.yaml" \
30-
--values "{frontend.tag: 1.1.0, backend.tag: 1.1.0}" \
31-
--branch "master"
30+
--values "{frontend.tag: 1.1.0, backend.tag: 1.1.0}"
3231
```
3332

3433
### Number Of Commits
@@ -66,8 +65,6 @@ Date: Thu Mar 12 15:30:00 2020 +0100
6665

6766
In some cases you might want to create a pull request for your updates. You can achieve this by adding `--create-pr` to the command. The pull request can be left open or merged directly with `--auto-merge`.
6867

69-
!!! info "We recommend creating a random branch name for automatically created pull requests to prevent collisions."
70-
7168
```bash
7269
gitopscli deploy \
7370
--git-provider-url https://bitbucket.baloise.dev \
@@ -79,7 +76,6 @@ gitopscli deploy \
7976
--repository-name "incubator-non-prod" \
8077
--file "example/values.yaml" \
8178
--values "{frontend.tag: 1.1.0, backend.tag: 1.1.0}" \
82-
--branch deploy-$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 5 | head -n 1) \
8379
--create-pr \
8480
--auto-merge
8581
```
@@ -98,8 +94,8 @@ usage: gitopscli deploy [-h] --file FILE --values VALUES
9894
--repository-name REPOSITORY_NAME
9995
[--git-provider GIT_PROVIDER]
10096
[--git-provider-url GIT_PROVIDER_URL]
101-
[--branch BRANCH] [--create-pr [CREATE_PR]]
102-
[--auto-merge [AUTO_MERGE]] [-v [VERBOSE]]
97+
[--create-pr [CREATE_PR]] [--auto-merge [AUTO_MERGE]]
98+
[-v [VERBOSE]]
10399

104100
optional arguments:
105101
-h, --help show this help message and exit
@@ -122,10 +118,8 @@ optional arguments:
122118
--git-provider-url GIT_PROVIDER_URL
123119
Git provider base API URL (e.g.
124120
https://bitbucket.example.tld)
125-
--branch BRANCH Branch to push the changes to
126121
--create-pr [CREATE_PR]
127-
Creates a Pull Request (only when --branch is not
128-
master/default branch)
122+
Creates a Pull Request
129123
--auto-merge [AUTO_MERGE]
130124
Automatically merge the created PR (only valid with
131125
--create-pr)

gitopscli/cliparser.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ def __add_create_preview_command_parser(subparsers):
7676
def __add_delete_preview_command_parser(subparsers):
7777
add_delete_preview_p = subparsers.add_parser("delete-preview", help="Delete a preview environment")
7878
__add_git_parser_args(add_delete_preview_p)
79+
add_delete_preview_p.add_argument(
80+
"--branch", help="The branch for which the preview was created for", required=True
81+
)
7982
__add_branch_pr_parser_args(add_delete_preview_p)
8083
__add_verbose_parser(add_delete_preview_p)
8184

@@ -93,14 +96,8 @@ def __add_git_parser_args(deploy_p, api_only=False):
9396

9497

9598
def __add_branch_pr_parser_args(deploy_p):
96-
deploy_p.add_argument("--branch", help="Branch to push the changes to", default="master")
9799
deploy_p.add_argument(
98-
"--create-pr",
99-
help="Creates a Pull Request (only when --branch is not master/default branch)",
100-
type=__str2bool,
101-
nargs="?",
102-
const=True,
103-
default=False,
100+
"--create-pr", help="Creates a Pull Request", type=__str2bool, nargs="?", const=True, default=False,
104101
)
105102
deploy_p.add_argument(
106103
"--auto-merge",

gitopscli/commands/create_preview.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def create_preview_command(
1414
command,
1515
pr_id,
1616
parent_id,
17-
branch,
1817
username,
1918
password,
2019
git_user,
@@ -44,10 +43,12 @@ def create_preview_command(
4443
apps_tmp_dir,
4544
)
4645

47-
apps_git.checkout(branch)
48-
logging.info("App repo branch %s checkout successful", branch)
49-
shortened_branch_hash = hashlib.sha256(branch.encode("utf-8")).hexdigest()[:8]
50-
logging.info("Hashed branch %s to hash: %s", branch, shortened_branch_hash)
46+
pr_branch = apps_git.get_pull_request_branch(pr_id)
47+
48+
apps_git.checkout(pr_branch)
49+
logging.info("App repo PR branch %s checkout successful", pr_branch)
50+
shortened_branch_hash = hashlib.sha256(pr_branch.encode("utf-8")).hexdigest()[:8]
51+
logging.info("Hashed branch %s to hash: %s", pr_branch, shortened_branch_hash)
5152
gitops_config = GitOpsConfig(apps_git.get_full_file_path(".gitops.config.yaml"))
5253
logging.info("Read GitOpsConfig: %s", gitops_config)
5354

@@ -64,8 +65,12 @@ def create_preview_command(
6465
)
6566
root_git.checkout("master")
6667
logging.info("Config repo branch master checkout successful")
67-
root_git.new_branch(branch)
68-
logging.info("Created branch %s in config repo", branch)
68+
69+
config_branch = f"gitopscli-create-preview-{str(uuid.uuid4())[:8]}" if create_pr else "master"
70+
if create_pr:
71+
root_git.new_branch(config_branch)
72+
logging.info("Created branch %s in config repo", config_branch)
73+
6974
preview_template_folder_name = ".preview-templates/" + gitops_config.application_name
7075
logging.info("Using the preview template folder: %s", preview_template_folder_name)
7176
new_preview_folder_name = gitops_config.application_name + "-" + shortened_branch_hash + "-preview"
@@ -74,7 +79,11 @@ def create_preview_command(
7479
logging.info("Is preview env already existing for branch? %s", branch_preview_env_already_exist)
7580
if not branch_preview_env_already_exist:
7681
__create_new_preview_env(
77-
branch, new_preview_folder_name, preview_template_folder_name, root_git, gitops_config.application_name
82+
config_branch,
83+
new_preview_folder_name,
84+
preview_template_folder_name,
85+
root_git,
86+
gitops_config.application_name,
7887
)
7988
new_image_tag = apps_git.get_last_commit_hash()
8089
logging.info("Using image tag from last app repo commit: %s", new_image_tag)
@@ -94,27 +103,27 @@ def create_preview_command(
94103
if not value_replaced:
95104
__no_deployment_needed(apps_git, new_image_tag, parent_id, pr_id)
96105
return
97-
root_git.commit(f"Upated preview environment for {gitops_config.application_name} and branch {branch}.")
98-
root_git.push(branch)
99-
logging.info("Pushed branch %s", branch)
106+
root_git.commit(f"Upated preview environment for {gitops_config.application_name} and branch {pr_branch}.")
107+
root_git.push(config_branch)
108+
logging.info("Pushed branch %s", config_branch)
100109
pr_comment_text = f"""
101-
New Preview Environment for {gitops_config.application_name} and branch {branch} created successfully. Access it here:
110+
New Preview Environment for {gitops_config.application_name} and branch {pr_branch} created successfully. Access it here:
102111
https://{route_host}
103112
"""
104113
if branch_preview_env_already_exist:
105114
pr_comment_text = f"""
106-
Preview Environment for {gitops_config.application_name} and branch {branch} updated successfully. Access it here:
115+
Preview Environment for {gitops_config.application_name} and branch {pr_branch} updated successfully. Access it here:
107116
https://{route_host}
108117
"""
109118
logging.info("Creating PullRequest comment for pr with id %s and content: %s", pr_id, pr_comment_text)
110119
apps_git.add_pull_request_comment(pr_id, pr_comment_text, parent_id)
111120
finally:
112121
shutil.rmtree(apps_tmp_dir, ignore_errors=True)
113122
shutil.rmtree(root_tmp_dir, ignore_errors=True)
114-
if create_pr and branch != "master":
115-
pull_request = __create_pullrequest(branch, gitops_config, root_git)
123+
if create_pr:
124+
pull_request = __create_pullrequest(config_branch, gitops_config, root_git)
116125
if auto_merge:
117-
__merge_pullrequest(branch, pull_request, root_git)
126+
__merge_pullrequest(config_branch, pull_request, root_git)
118127

119128

120129
def __replace_value(

gitopscli/commands/delete_preview.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ def delete_preview_command(
5959
)
6060
root_git.checkout("master")
6161
logging.info("Config repo branch master checkout successful")
62-
root_git.new_branch(branch)
63-
logging.info("Created branch %s in config repo", branch)
62+
63+
config_branch = f"gitopscli-delete-preview-{str(uuid.uuid4())[:8]}" if create_pr else "master"
64+
65+
root_git.new_branch(config_branch)
66+
logging.info("Created branch %s in config repo", config_branch)
6467
shortened_branch_hash = hashlib.sha256(branch.encode("utf-8")).hexdigest()[:8]
6568
logging.info("Hashed branch %s to hash: %s", branch, shortened_branch_hash)
6669
preview_folder_name = gitops_config.application_name + "-" + shortened_branch_hash + "-preview"
@@ -74,16 +77,16 @@ def delete_preview_command(
7477
root_git.commit(
7578
f"Deleted preview environment for application: {gitops_config.application_name} and branch: {branch}."
7679
)
77-
root_git.push(branch)
78-
logging.info("Pushed branch %s", branch)
80+
root_git.push(config_branch)
81+
logging.info("Pushed branch %s", config_branch)
7982

8083
finally:
8184
shutil.rmtree(apps_tmp_dir, ignore_errors=True)
8285
shutil.rmtree(root_tmp_dir, ignore_errors=True)
83-
if create_pr and branch != "master":
84-
pull_request = __create_pullrequest(branch, gitops_config, root_git)
86+
if create_pr:
87+
pull_request = __create_pullrequest(config_branch, gitops_config, root_git)
8588
if auto_merge:
86-
__merge_pullrequest(branch, pull_request, root_git)
89+
__merge_pullrequest(config_branch, pull_request, root_git)
8790

8891

8992
def __create_pullrequest(branch, gitops_config, root_git):

gitopscli/commands/deploy.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def deploy_command(
1212
command,
1313
file,
1414
values,
15-
branch,
1615
username,
1716
password,
1817
git_user,
@@ -46,22 +45,24 @@ def deploy_command(
4645
git.checkout("master")
4746
logging.info("Master checkout successful")
4847

49-
if branch != "master":
50-
git.new_branch(branch)
51-
logging.info("Created branch %s", branch)
48+
config_branch = f"gitopscli-deploy-{str(uuid.uuid4())[:8]}" if create_pr else "master"
49+
50+
if create_pr:
51+
git.new_branch(config_branch)
52+
logging.info("Created branch %s", config_branch)
5253

5354
updated_values = __update_values(git, file, values, single_commit)
5455
if not updated_values:
5556
logging.info("All values already up-to-date. I'm done here")
5657
return
5758

58-
git.push(branch)
59-
logging.info("Pushed branch %s", branch)
59+
git.push(config_branch)
60+
logging.info("Pushed branch %s", config_branch)
6061
finally:
6162
shutil.rmtree(tmp_dir, ignore_errors=True)
6263

63-
if create_pr and branch != "master":
64-
__create_pr(git, branch, file, updated_values, auto_merge)
64+
if create_pr:
65+
__create_pr(git, config_branch, file, updated_values, auto_merge)
6566

6667

6768
def __update_values(git, file, values, single_commit):

gitopscli/git/abstract_git_util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,7 @@ def add_pull_request_comment(self, pr_id, text, parent_id):
9999
@abstractmethod
100100
def delete_branch(self, branch):
101101
pass
102+
103+
@abstractmethod
104+
def get_pull_request_branch(self, pr_id):
105+
pass

gitopscli/git/bitbucket_git_util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,9 @@ def delete_branch(self, branch):
8080
)
8181
if result and "errors" in result:
8282
raise GitOpsException(result["errors"][0]["message"])
83+
84+
def get_pull_request_branch(self, pr_id):
85+
pull_request = self._bitbucket.get_pullrequest(self._organisation, self._repository_name, pr_id)
86+
if "errors" in pull_request:
87+
raise GitOpsException(pull_request["errors"][0]["message"])
88+
return pull_request["fromRef"]["displayId"]

0 commit comments

Comments
 (0)