Skip to content
This repository was archived by the owner on Nov 28, 2024. It is now read-only.

Commit b610c21

Browse files
committed
Add option to no-op if target branch is up-to-date.
1 parent ddda4b0 commit b610c21

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

git_pull_request/__init__.py

+25-8
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ def git_get_title_and_message(begin, end):
243243
return (len(titles), title, message)
244244

245245

246-
def git_pull_request(target_remote=None, target_branch=None,
246+
def git_pull_request(*,
247+
target_remote=None, target_branch=None,
247248
title=None, message=None,
248249
comment=None,
249250
rebase=True,
@@ -254,7 +255,9 @@ def git_pull_request(target_remote=None, target_branch=None,
254255
fork=True,
255256
setup_only=False,
256257
branch_prefix=None,
257-
dry_run=False):
258+
dry_run=False,
259+
allow_noop=False,
260+
):
258261
branch = git_get_branch_name()
259262
if not branch:
260263
LOG.critical("Unable to find current branch")
@@ -326,7 +329,7 @@ def git_pull_request(target_remote=None, target_branch=None,
326329
retcode = fork_and_push_pull_request(
327330
g, hosttype, repo, rebase, target_remote, target_branch, branch,
328331
user, title, message, comment, force_editor, tag_previous_revision,
329-
fork, setup_only, branch_prefix, dry_run,
332+
fork, setup_only, branch_prefix, dry_run, allow_noop=allow_noop,
330333
)
331334

332335
approve_login_password(host=hostname, user=user, password=password)
@@ -435,7 +438,7 @@ def fork_and_push_pull_request(g, hosttype, repo_to_fork, rebase,
435438
title, message, comment,
436439
force_editor, tag_previous_revision, fork,
437440
setup_only, branch_prefix,
438-
dry_run=False):
441+
dry_run=False, *, allow_noop):
439442

440443
g_user = g.get_user()
441444

@@ -582,10 +585,16 @@ def fork_and_push_pull_request(g, hosttype, repo_to_fork, rebase,
582585
title=title,
583586
body=message)
584587
except github.GithubException as e:
585-
LOG.critical(
586-
_format_github_exception("create pull request", e)
587-
)
588-
return 50
588+
if allow_noop and any((
589+
error.get("message", "").startswith("No commits between")
590+
for error in e.data.get("errors", [])
591+
)):
592+
LOG.info("Did not create pull-request, no new commits.")
593+
else:
594+
LOG.critical(
595+
_format_github_exception("create pull request", e)
596+
)
597+
return 50
589598
else:
590599
LOG.info("Pull-request created: %s", pull.html_url)
591600

@@ -693,6 +702,13 @@ def build_parser():
693702
default=False,
694703
help="Just setup the fork repo"
695704
)
705+
git_config_add_argument(
706+
parser,
707+
"--allow-noop",
708+
action="store_true",
709+
default=False,
710+
help="If the target branch is up-to-date, do nothing."
711+
)
696712
return parser
697713

698714

@@ -724,6 +740,7 @@ def main():
724740
setup_only=args.setup_only,
725741
branch_prefix=args.branch_prefix,
726742
dry_run=args.dry_run,
743+
allow_noop=args.allow_noop,
727744
)
728745
except Exception:
729746
LOG.error("Unable to send pull request", exc_info=True)

git_pull_request/tests/test_gpr.py

+4
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,11 @@ def test_get_remote_for_branch(self):
290290
gpr._run_shell_command(["git", "config",
291291
"git-pull-request.target-branch",
292292
"awesome_branch"])
293+
gpr._run_shell_command(["git", "config",
294+
"git-pull-request.allow_noop",
295+
"true"])
293296
args = gpr.build_parser().parse_args([])
294297
self.assertEqual(True, args.setup_only)
295298
self.assertEqual("never", args.fork)
296299
self.assertEqual("awesome_branch", args.target_branch)
300+
self.assertEqual(True, args.allow_noop)

0 commit comments

Comments
 (0)