Skip to content

Commit 05e6e3f

Browse files
authored
Allow to create PR for a reference that is in a different repository (#9)
Signed-off-by: Matthieu Patou <[email protected]>
1 parent c137580 commit 05e6e3f

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/stacky/stacky.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,20 @@ def find_reviewers(b) -> Optional[List[str]]:
662662
return
663663

664664

665-
def create_gh_pr(b):
665+
def create_gh_pr(b, prefix):
666666
cout("Creating PR for {}\n", b.name, fg="green")
667-
cmd = ["gh", "pr", "create", "--head", b.name, "--base", b.parent.name]
667+
parent_prefix = ""
668+
if b.parent.name not in STACK_BOTTOMS:
669+
parent_prefix = prefix
670+
cmd = [
671+
"gh",
672+
"pr",
673+
"create",
674+
"--head",
675+
f"{prefix}{b.name}",
676+
"--base",
677+
f"{parent_prefix}{b.parent.name}",
678+
]
668679
match = re.match(r"([A-Z]{3,}-\d{1,})($|-.*)", b.name)
669680
reviewers = find_reviewers(b)
670681
if match:
@@ -714,7 +725,7 @@ def create_gh_pr(b):
714725
)
715726

716727

717-
def do_push(forest, *, force=False, pr=False):
728+
def do_push(forest, *, force=False, pr=False, remote_name="origin"):
718729
if pr:
719730
load_pr_info_for_forest(forest)
720731
print_forest(forest)
@@ -781,6 +792,18 @@ def do_push(forest, *, force=False, pr=False):
781792
if actions and not force:
782793
confirm()
783794

795+
# Figure out if we need to add a prefix to the branch
796+
# ie. user:foo
797+
# We should call gh repo set-default before doing that
798+
val = run(["git", "config", f"remote.{remote_name}.gh-resolved"], check=False)
799+
if val is not None and "/" in val:
800+
# If there is a "/" in the gh-resolved it means that the repo where
801+
# the should be created is not the same as the one where the push will
802+
# be made, we need to add a prefix to the branch in the gh pr command
803+
val = run(["git", "config", f"remote.{remote_name}.url"])
804+
prefix = f'{val.split(":")[1].split("/")[0]}:'
805+
else:
806+
prefix = ""
784807
for b, push, pr_action in actions:
785808
if push:
786809
cout("Pushing {}\n", b.name, fg="green")
@@ -808,7 +831,7 @@ def do_push(forest, *, force=False, pr=False):
808831
out=True,
809832
)
810833
elif pr_action == PR_CREATE:
811-
create_gh_pr(b)
834+
create_gh_pr(b, prefix)
812835

813836

814837
def cmd_stack_push(stack, args):
@@ -1254,6 +1277,12 @@ def main():
12541277
choices=["always", "auto", "never"],
12551278
help="Colorize output and error",
12561279
)
1280+
parser.add_argument(
1281+
"--remote-name",
1282+
"-r",
1283+
default="origin",
1284+
help="name of the git remote where branches will be pushed",
1285+
)
12571286

12581287
subparsers = parser.add_subparsers(required=True, dest="command")
12591288

0 commit comments

Comments
 (0)