Skip to content

Commit 75666bb

Browse files
leorollandryshu
authored andcommitted
feat(push): #9 - add push and get_repo_ssh_url
1 parent 44f9c68 commit 75666bb

File tree

5 files changed

+75
-4
lines changed

5 files changed

+75
-4
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import click
2+
3+
from gitflow_toolbox.common.gitlab import CurrentGitlab, RemoteGitlab
4+
from gitflow_toolbox.common.is_main_call import is_main_call
5+
6+
7+
@click.command()
8+
@click.option("--remote/--current", default=False)
9+
@click.pass_context
10+
def get_project_ssh_url(ctx: click.Context, remote: bool):
11+
"""Get Gitlab project SSH URL (for cloning)
12+
13+
Args:
14+
remote (bool): whether to check on the current gitlab or remote gitlab (True=remote)
15+
"""
16+
project = RemoteGitlab().project if remote else CurrentGitlab().project
17+
ssh_url = project.attributes.get("ssh_url_to_repo")
18+
if is_main_call(ctx):
19+
click.echo(ssh_url)
20+
return ssh_url

gitflow_toolbox/push.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
import shutil
3+
import uuid
4+
5+
import click
6+
import git
7+
8+
from gitflow_toolbox.get_repo_ssh_url import get_project_ssh_url
9+
10+
11+
@click.command()
12+
@click.option("--from-gitlab", type=click.Choice(["current", "remote"], case_sensitive=False))
13+
@click.argument("source_branch", type=str)
14+
@click.option("--to-gitlab", type=click.Choice(["current", "remote"], case_sensitive=False))
15+
@click.argument("target_branch", type=str)
16+
@click.option("--force", is_flag=True, type=bool)
17+
@click.pass_context
18+
def push(
19+
ctx: click.Context,
20+
from_gitlab: tuple[str],
21+
source_branch: str,
22+
to_gitlab: tuple[str],
23+
target_branch: str,
24+
force: bool,
25+
):
26+
27+
project_from_ssh_url = ctx.invoke(get_project_ssh_url, remote=(from_gitlab == "remote"))
28+
project_from_clone_dir = os.path.join("/tmp", f"gf_{uuid.uuid4()}")
29+
30+
# Clone
31+
click.echo(f"Cloning {project_from_ssh_url} into {project_from_clone_dir}")
32+
repo_from = git.Repo.clone_from(project_from_ssh_url, project_from_clone_dir, branch=source_branch)
33+
34+
# Add remote
35+
project_to_ssh_url = ctx.invoke(get_project_ssh_url, remote=(to_gitlab == "remote"))
36+
click.echo(f"Adding remote to {project_to_ssh_url}")
37+
repo_from.create_remote("target", project_to_ssh_url)
38+
39+
# Push
40+
click.echo(f"Pushing {from_gitlab} {source_branch} into {to_gitlab} {target_branch} (force={force})")
41+
repo_from.remotes.target.push(refspec=f"{source_branch}:{target_branch}", force=force)
42+
43+
# Clean
44+
click.echo(f"Removing cache {project_from_clone_dir}")
45+
shutil.rmtree(project_from_clone_dir)
46+
click.echo(f"✨ Successfully pushed {from_gitlab} {source_branch} into {to_gitlab} {target_branch}")

main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from gitflow_toolbox.check_mr_exists import check_mr_exists
66
from gitflow_toolbox.ensure_branch import ensure_branch
77
from gitflow_toolbox.ensure_mr import ensure_mr
8+
from gitflow_toolbox.get_repo_ssh_url import get_project_ssh_url
9+
from gitflow_toolbox.push import push
810

911
load_dotenv()
1012

@@ -18,6 +20,8 @@ def cli():
1820
cli.add_command(ensure_branch)
1921
cli.add_command(check_mr_exists)
2022
cli.add_command(ensure_mr)
23+
cli.add_command(get_project_ssh_url)
24+
cli.add_command(push)
2125

2226
if __name__ == "__main__":
2327
cli()

poetry.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ python = ">=3.9.0,<4"
1616
click = "^8.1.3"
1717
python-gitlab = "^3.6.0"
1818
python-dotenv = "^0.20.0"
19+
GitPython = "^3.1.27"
1920

2021
[tool.poetry.dev-dependencies]
2122
bandit = "^1.7"

0 commit comments

Comments
 (0)