Skip to content

Commit

Permalink
Merge pull request #155 from socar-inc/master
Browse files Browse the repository at this point in the history
feat(gitopscli/*): add merge method parameter to deploy command
  • Loading branch information
christiansiegel authored Jul 4, 2021
2 parents 289d955 + 97f5093 commit a38a1c5
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 21 deletions.
10 changes: 8 additions & 2 deletions docs/commands/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,15 @@ gitopscli deploy \
## Usage
```
usage: gitopscli deploy [-h] --file FILE --values VALUES
[--single-commit [SINGLE_COMMIT]] --username USERNAME
[--single-commit [SINGLE_COMMIT]]
[--commit-message COMMIT_MESSAGE] --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]
[--create-pr [CREATE_PR]] [--auto-merge [AUTO_MERGE]]
[-v [VERBOSE]]
[--merge-method MERGE_METHOD] [-v [VERBOSE]]

optional arguments:
-h, --help show this help message and exit
Expand All @@ -136,6 +137,8 @@ optional arguments:
desired value as value
--single-commit [SINGLE_COMMIT]
Create only single commit for all updates
--commit-message COMMIT_MESSAGE
Specify exact commit message of deployment commit
--username USERNAME Git username (alternative: GITOPSCLI_USERNAME env
variable)
--password PASSWORD Git password or token (alternative: GITOPSCLI_PASSWORD
Expand All @@ -157,6 +160,9 @@ optional arguments:
--auto-merge [AUTO_MERGE]
Automatically merge the created PR (only valid with
--create-pr)
--merge-method MERGE_METHOD
Merge Method (e.g., 'squash', 'rebase', 'merge')
(default: merge)
-v [VERBOSE], --verbose [VERBOSE]
Verbose exception logging
```
6 changes: 6 additions & 0 deletions gitopscli/cliparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ def __create_deploy_parser() -> ArgumentParser:
const=True,
default=False,
)
parser.add_argument(
"--merge-method",
help="Merge Method (e.g., 'squash', 'rebase', 'merge') (default: merge)",
type=str,
default="merge",
)
__add_verbose_arg(parser)
return parser

Expand Down
5 changes: 3 additions & 2 deletions gitopscli/commands/deploy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import uuid
from dataclasses import dataclass
from typing import Any, Dict, Optional, Tuple
from typing import Any, Dict, Optional, Tuple, Literal
from gitopscli.git_api import GitApiConfig, GitRepo, GitRepoApi, GitRepoApiFactory
from gitopscli.io_api.yaml_util import update_yaml_file, yaml_dump, YAMLException
from gitopscli.gitops_exception import GitOpsException
Expand All @@ -25,6 +25,7 @@ class Args(GitApiConfig):

create_pr: bool
auto_merge: bool
merge_method: Literal["squash", "rebase", "merge"] = "merge"

def __init__(self, args: Args) -> None:
self.__args = args
Expand All @@ -50,7 +51,7 @@ def execute(self) -> None:
pr_id = git_repo_api.create_pull_request_to_default_branch(pr_branch, title, description).pr_id

if self.__args.auto_merge:
git_repo_api.merge_pull_request(pr_id)
git_repo_api.merge_pull_request(pr_id, self.__args.merge_method)
git_repo_api.delete_branch(pr_branch)

def __create_git_repo_api(self) -> GitRepoApi:
Expand Down
4 changes: 2 additions & 2 deletions gitopscli/git_api/bitbucket_git_repo_api_adapter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, Literal
import requests

from atlassian import Bitbucket
Expand Down Expand Up @@ -74,7 +74,7 @@ def create_pull_request(
raise GitOpsException(pull_request["errors"][0]["message"])
return GitRepoApi.PullRequestIdAndUrl(pr_id=pull_request["id"], url=pull_request["links"]["self"][0]["href"])

def merge_pull_request(self, pr_id: int) -> None:
def merge_pull_request(self, pr_id: int, merge_method: Literal["squash", "rebase", "merge"] = "merge") -> None:
pull_request = self.__bitbucket.get_pullrequest(self.__organisation, self.__repository_name, pr_id)
self.__bitbucket.merge_pull_request(
self.__organisation, self.__repository_name, pull_request["id"], pull_request["version"]
Expand Down
4 changes: 2 additions & 2 deletions gitopscli/git_api/git_repo_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABCMeta, abstractmethod
from typing import NamedTuple, Optional
from typing import NamedTuple, Optional, Literal


class GitRepoApi(metaclass=ABCMeta):
Expand Down Expand Up @@ -32,7 +32,7 @@ def create_pull_request(
...

@abstractmethod
def merge_pull_request(self, pr_id: int) -> None:
def merge_pull_request(self, pr_id: int, merge_method: Literal["squash", "rebase", "merge"] = "merge") -> None:
...

@abstractmethod
Expand Down
6 changes: 3 additions & 3 deletions gitopscli/git_api/git_repo_api_logging_proxy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Optional
from typing import Optional, Literal
from .git_repo_api import GitRepoApi


Expand Down Expand Up @@ -28,9 +28,9 @@ def create_pull_request(
logging.info("Creating pull request from '%s' to '%s' with title: %s", from_branch, to_branch, title)
return self.__api.create_pull_request(from_branch, to_branch, title, description)

def merge_pull_request(self, pr_id: int) -> None:
def merge_pull_request(self, pr_id: int, merge_method: Literal["squash", "rebase", "merge"] = "merge") -> None:
logging.info("Merging pull request %s", pr_id)
self.__api.merge_pull_request(pr_id)
self.__api.merge_pull_request(pr_id, merge_method=merge_method)

def add_pull_request_comment(self, pr_id: int, text: str, parent_id: Optional[int] = None) -> None:
if parent_id:
Expand Down
17 changes: 13 additions & 4 deletions gitopscli/git_api/github_git_repo_api_adapter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
from typing import Optional
from github import Github, UnknownObjectException, BadCredentialsException, GitRef, PullRequest, Repository
from typing import Optional, Literal

from github import (
Github,
UnknownObjectException,
BadCredentialsException,
GitRef,
PullRequest,
Repository,
)

from gitopscli.gitops_exception import GitOpsException
from .git_repo_api import GitRepoApi

Expand Down Expand Up @@ -36,9 +45,9 @@ def create_pull_request(
pull_request = repo.create_pull(title=title, body=description, head=from_branch, base=to_branch)
return GitRepoApi.PullRequestIdAndUrl(pr_id=pull_request.number, url=pull_request.html_url)

def merge_pull_request(self, pr_id: int) -> None:
def merge_pull_request(self, pr_id: int, merge_method: Literal["squash", "rebase", "merge"] = "merge") -> None:
pull_request = self.__get_pull_request(pr_id)
pull_request.merge()
pull_request.merge(merge_method=merge_method)

def add_pull_request_comment(self, pr_id: int, text: str, parent_id: Optional[int] = None) -> None:
pull_request = self.__get_pull_request(pr_id)
Expand Down
7 changes: 5 additions & 2 deletions gitopscli/git_api/gitlab_git_repo_api_adapter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, Literal
import logging
import time
import requests
Expand Down Expand Up @@ -59,12 +59,15 @@ def create_pull_request(
)
return GitRepoApi.PullRequestIdAndUrl(pr_id=merge_request.iid, url=merge_request.web_url)

def merge_pull_request(self, pr_id: int) -> None:
def merge_pull_request(self, pr_id: int, merge_method: Literal["squash", "rebase", "merge"] = "merge") -> None:
merge_request = self.__project.mergerequests.get(pr_id)

max_retries = MAX_MERGE_RETRIES
while max_retries > 0:
try:
if merge_method == "rebase":
merge_request.rebase()
return
merge_request.merge()
return
except gitlab.exceptions.GitlabMRClosedError as ex:
Expand Down
2 changes: 1 addition & 1 deletion tests/commands/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def test_create_pr_and_merge_happy_flow(self):
"Updated values in test/file.yml",
"Updated 2 values in `test/file.yml`:\n```yaml\na.b.c: foo\na.b.d: bar\n```\n",
),
call.GitRepoApi.merge_pull_request(42),
call.GitRepoApi.merge_pull_request(42, "merge"),
call.GitRepoApi.delete_branch("gitopscli-deploy-b973b5bb"),
]

Expand Down
2 changes: 1 addition & 1 deletion tests/git_api/test_git_repo_api_logging_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_create_pull_request_to_default_branch(self, logging_mock):
@patch("gitopscli.git_api.git_repo_api_logging_proxy.logging")
def test_merge_pull_request(self, logging_mock):
self.__testee.merge_pull_request(pr_id=42)
self.__mock_repo_api.merge_pull_request.assert_called_once_with(42)
self.__mock_repo_api.merge_pull_request.assert_called_once_with(42, merge_method="merge")
logging_mock.info.assert_called_once_with(
"Merging pull request %s", 42,
)
Expand Down
7 changes: 5 additions & 2 deletions tests/test_cliparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
[--git-provider GIT_PROVIDER]
[--git-provider-url GIT_PROVIDER_URL]
[--create-pr [CREATE_PR]] [--auto-merge [AUTO_MERGE]]
[-v [VERBOSE]]
[--merge-method MERGE_METHOD] [-v [VERBOSE]]
gitopscli deploy: error: the following arguments are required: --file, --values, --username, --password, --organisation, --repository-name
"""

Expand All @@ -298,7 +298,7 @@
[--git-provider GIT_PROVIDER]
[--git-provider-url GIT_PROVIDER_URL]
[--create-pr [CREATE_PR]] [--auto-merge [AUTO_MERGE]]
[-v [VERBOSE]]
[--merge-method MERGE_METHOD] [-v [VERBOSE]]
optional arguments:
-h, --help show this help message and exit
Expand Down Expand Up @@ -330,6 +330,9 @@
--auto-merge [AUTO_MERGE]
Automatically merge the created PR (only valid with
--create-pr)
--merge-method MERGE_METHOD
Merge Method (e.g., 'squash', 'rebase', 'merge')
(default: merge)
-v [VERBOSE], --verbose [VERBOSE]
Verbose exception logging
"""
Expand Down

0 comments on commit a38a1c5

Please sign in to comment.