Skip to content

Commit

Permalink
feat: allow branch overwrite
Browse files Browse the repository at this point in the history
In some cases you want to overwrite the target branch. By supplying the `--branch my-branch` option you are now able to do just that.

Issue: #23
  • Loading branch information
Joris Conijn committed Feb 10, 2022
1 parent 56c5d6d commit 6dd7445
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ From this point you are ready for the next change.

When a pull requests exists a proposal is made to update the existing pull request.

### Overwrite target branch

When you want to overwrite the target branch you need to supply the `--branch <name>` option:

```bash
pull-request-codecommit --branch my-target-branch
```

## Testing locally

```bash
Expand Down
13 changes: 9 additions & 4 deletions pull_request_codecommit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

@click.command()
@click.option("-r", "--repository-path", default=None)
@click.option("-b", "--branch", default=None)
@click.option("--auto-merge/--no-auto-merge", default=False)
def main(repository_path: Optional[str], auto_merge: bool) -> None:
def main(
repository_path: Optional[str], branch: Optional[str], auto_merge: bool
) -> None:
"""
pull-request-codecommit
"""
repo = __load_repository(repository_path)
repo = __load_repository(repository_path=repository_path, target_branch=branch)
__display_repository_information(repo)
pr = __create_pull_request(repo)

Expand All @@ -22,8 +25,10 @@ def main(repository_path: Optional[str], auto_merge: bool) -> None:
click.echo(f"Auto merging resulted in: {status}")


def __load_repository(repository_path: Optional[str]) -> Repository:
repo = Repository(repository_path)
def __load_repository(
repository_path: Optional[str], target_branch: Optional[str]
) -> Repository:
repo = Repository(path=repository_path, target_branch=target_branch)

if not repo.remote.supported:
raise click.ClickException("The repository is not compatible with this tool!")
Expand Down
2 changes: 1 addition & 1 deletion pull_request_codecommit/aws/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, profile: Optional[str], region: Optional[str]) -> None:

@property
def base_command(self) -> List[str]:
base_command = ["aws"]
base_command = ["aws", "--output", "json"]

if self.__profile:
base_command.extend(["--profile", self.__profile])
Expand Down
20 changes: 12 additions & 8 deletions pull_request_codecommit/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ class Repository:
Understands CodeCommit repositories
"""

def __init__(self, path: Optional[str] = None) -> None:
self.__remote: Optional[Remote] = None
self.__branch: str = ""

def __init__(
self, path: Optional[str] = None, target_branch: Optional[str] = None
) -> None:
if not path:
path = os.getcwd()

self.__git = GitClient(path)
self.__remote: Optional[Remote] = None
self.__branch: str = ""
self.__target_branch: str = (
target_branch if target_branch else self.__config("destination_branch")
)

def __config(self, method: str) -> Optional[str]:
return getattr(Config, method)(self.remote.profile)
def __config(self, method: str) -> str:
item = getattr(Config, method)(self.remote.profile)
return item if item else ""

@property
def remote(self) -> Remote:
Expand All @@ -38,8 +43,7 @@ def branch(self) -> str:

@property
def destination(self) -> str:
destination = self.__config("destination_branch")
return destination if destination else ""
return self.__target_branch

def commits(self) -> Commits:
return self.__git.get_commit_messages(destination_branch=self.destination)
Expand Down
9 changes: 8 additions & 1 deletion tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ def generate_invoke_parameters(

@pytest.mark.parametrize(
"remote, region, profile, config, commits, parameters",
generate_invoke_parameters([[], ["--auto-merge"]]),
generate_invoke_parameters(
[
[],
["--auto-merge"],
["-b", "my-target-branch"],
["--branch", "my-target-branch"],
]
),
)
@patch("pull_request_codecommit.aws.client.subprocess.run")
@patch("pull_request_codecommit.repository.GitClient")
Expand Down

0 comments on commit 6dd7445

Please sign in to comment.