From f10f38c6eab20dec7dd167731bf42dff10b75b47 Mon Sep 17 00:00:00 2001 From: Martin Moss Date: Wed, 26 Jun 2024 17:30:47 +0100 Subject: [PATCH] Add protection to not make Pull requests in forked repos against the original Repository --- gitfeatures/core.py | 52 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/gitfeatures/core.py b/gitfeatures/core.py index c9e6705..4fbd0dd 100644 --- a/gitfeatures/core.py +++ b/gitfeatures/core.py @@ -11,13 +11,16 @@ seperator = os.environ.get("GITFEATURES_BRANCH_SEPERATOR", "_") repo = os.environ.get("GITFEATURES_REPO", "github") merge_strategy = os.environ.get("GITFEATURES_STRATEGY", "merge") +fork_pr_strategy = os.environ.get("GITFEATURES_FORK_PR_STRATEGY", "private") def _call(args): try: return check_output(args).decode("utf-8") except CalledProcessError: - sys.exit(__name__ + ": none zero exit status executing: " + " ".join(args)) # noqa + sys.exit( + __name__ + ": none zero exit status executing: " + " ".join(args) + ) # noqa def _get_branch_name(prefix, name): @@ -42,7 +45,9 @@ def new_feature(name, prefix): new_branch = _get_branch_name(prefix, name) if _branch_exists(new_branch): - sys.exit(__name__ + ": local or remote branch already exists: " + new_branch) # noqa + sys.exit( + __name__ + ": local or remote branch already exists: " + new_branch + ) # noqa _call(["git", "checkout", "-b", new_branch]) _call(["git", "push", "-u", "origin", new_branch + ":" + new_branch]) @@ -59,11 +64,15 @@ def finish_feature(name, prefix): branch = cur_branch _call(["git", "checkout", master_branch]) else: - sys.exit(__name__ + ": please provide a branch name if on {}".format(master_branch)) + sys.exit( + __name__ + ": please provide a branch name if on {}".format(master_branch) + ) _call(["git", "remote", "update", "origin"]) - commits = _call(["git", "log", "--oneline", branch, "^origin/{}".format(master_branch)]) + commits = _call( + ["git", "log", "--oneline", branch, "^origin/{}".format(master_branch)] + ) if commits: sys.exit( __name__ @@ -136,10 +145,14 @@ def pullrequest(args): # check its up to date with remote master if not pull _call(["git", "remote", "update", "origin"]) - commits = _call(["git", "log", "--oneline", "^" + branch, "origin/{}".format(master_branch)]) + commits = _call( + ["git", "log", "--oneline", "^" + branch, "origin/{}".format(master_branch)] + ) if commits: print( - "Your branch is behind origin/{} so cannot be automatically {}d.".format(master_branch, merge_strategy) + "Your branch is behind origin/{} so cannot be automatically {}d.".format( + master_branch, merge_strategy + ) ) # noqa print(commits) print( @@ -153,9 +166,15 @@ def pullrequest(args): _call(["git", "checkout", branch]) try: print("git {} {}".format(merge_strategy, master_branch)) - output = check_output(["git", merge_strategy, master_branch]).decode("utf-8") + output = check_output(["git", merge_strategy, master_branch]).decode( + "utf-8" + ) print(output) - print("Congratulations, successfully {}d {}".format(merge_strategy, master_branch)) + print( + "Congratulations, successfully {}d {}".format( + merge_strategy, master_branch + ) + ) except CalledProcessError as e: if b"CONFLICT" in e.output: err = ( @@ -176,19 +195,30 @@ def pullrequest(args): _call(["git", "push", "origin", branch + ":" + branch]) origin = _call(["git", "config", "--get", "remote.origin.url"]) + print("origin", origin) name = origin.split(":")[1].replace(".git\n", "") + print("name", name) + print("branch", branch) url = _get_pullrequest_url(name, branch) - if (len(args) > 0 and args[0] == "--dry-run") or os.environ.get("CONSOLEONLY", False): # noqa + if (len(args) > 0 and args[0] == "--dry-run") or os.environ.get( + "CONSOLEONLY", False + ): # noqa print(url) else: webbrowser.open_new_tab(url) def _get_pullrequest_url(name, branch): + print("pullrequest", name, branch) if repo == "github": - url = "https://github.com/" + name + "/pull/new/" + branch + if fork_pr_strategy == "private": + url = f"https://github.com/{name}/compare/{master_branch}...{branch}" + else: + url = "https://github.com/" + name + "/pull/new/" + branch elif repo == "bitbucket": - url = "https://bitbucket.org/" + name + "/pull-requests/new?t=1&source=" + branch # noqa + url = ( + "https://bitbucket.org/" + name + "/pull-requests/new?t=1&source=" + branch + ) # noqa return url