Repository: VirtusLab/git-machete
Pull request #1120 (2023-11-30 20:34:38)

Add `--yes` option to `github create-pr`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/VirtusLab/git-machete/pull/1120?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VirtusLab) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`cbfc68d`)](https://app.codecov.io/gh/VirtusLab/git-machete/commit/cbfc68d3f595bfd5e9f381f4e3f0684eba58b7d1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VirtusLab) 98.59% compared to head [(`203d148`)](https://app.codecov.io/gh/VirtusLab/git-machete/pull/1120?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VirtusLab) 98.59%.


<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff            @@
##           develop    #1120   +/-   ##
========================================
  Coverage    98.59%   98.59%           
========================================
  Files           12       12           
  Lines         3340     3343    +3     
  Branches       748      749    +1     
========================================
+ Hits          3293     3296    +3     
  Misses          30       30           
  Partials        17       17           
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/VirtusLab/git-machete/pull/1120?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VirtusLab).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=VirtusLab).


Patch:
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 56185b6..809094b 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -5,6 +5,7 @@
 - added: `--removed-from-remote` flag to `git machete slide-out` (contributed by @raylu)
 - added: `--title` flag to `git machete github create-pr` that allows for setting PR title explicitly (suggested by @mjgigli)
 - added: `--with-urls` flag to `git machete github anno-prs` and `machete.github.annotateWithUrls` git config key that allow for adding the URL of the PR to the annotations (contributed by @guyboltonking)
+- added: `--yes` flag to `git machete github create-pr` so that the user isn't asked whether to push the branch (suggested by @mkondratek)
 - deprecated: `git machete clean` and `git machete github sync`; use `github checkout-prs --mine`, `delete-unmanaged` and `slide-out --removed-from-remote` instead
 - fixed: PR author is now always added to annotation if different from current user (contributed by @guyboltonking)
 
diff --git a/completion/git-machete.completion.bash b/completion/git-machete.completion.bash
index 564e12c..01385ab 100644
--- a/completion/git-machete.completion.bash
+++ b/completion/git-machete.completion.bash
@@ -22,7 +22,7 @@ _git_machete() {
   local discover_opts="-C --checked-out-since= -l --list-commits -r --roots= -y --yes"
   local fork_point_opts="--inferred --override-to= --override-to-inferred --override-to-parent --unset-override"
   local github_anno_prs_opts="--with-urls"
-  local github_create_pr_opts="--draft --title="
+  local github_create_pr_opts="--draft --title= --yes"
   local github_checkout_prs_opts="--all --by= --mine"
   local github_retarget_pr_opts="-b --branch= --ignore-if-missing"
   local reapply_opts="-f --fork-point="
diff --git a/completion/git-machete.completion.zsh b/completion/git-machete.completion.zsh
index 08685e4..7ed3c32 100644
--- a/completion/git-machete.completion.zsh
+++ b/completion/git-machete.completion.zsh
@@ -273,6 +273,7 @@ __git_machete_github_subcommands() {
           _arguments \
             '(--draft)'--draft'[Create the new PR as draft]' \
             '(--title)'--title='[Set the title for new PR explicitly]' \
+            '(--yes)'--yes'[Do not ask for confirmation whether to push the branch]' \
             "${common_flags[@]}"
         ;;
 
diff --git a/completion/git-machete.fish b/completion/git-machete.fish
index 0455f56..880066b 100644
--- a/completion/git-machete.fish
+++ b/completion/git-machete.fish
@@ -103,6 +103,7 @@ complete -c git-machete -n "__fish_seen_subcommand_from github; and __fish_seen_
 complete -c git-machete -n "__fish_seen_subcommand_from github; and __fish_seen_subcommand_from checkout-prs; and not __fish_seen_subcommand_from --mine"                 -x -l mine                                                 -d 'Checkout open PRs for the current user associated with the GitHub token'
 complete -c git-machete -n "__fish_seen_subcommand_from github; and __fish_seen_subcommand_from create-pr;    and not __fish_seen_subcommand_from --draft"                -f -l draft                                                -d 'Create the new PR as a draft'
 complete -c git-machete -n "__fish_seen_subcommand_from github; and __fish_seen_subcommand_from create-pr;    and not __fish_seen_subcommand_from --title"                -x -l title                                                -d 'Set the title for new PR explicitly'
+complete -c git-machete -n "__fish_seen_subcommand_from github; and __fish_seen_subcommand_from create-pr;    and not __fish_seen_subcommand_from --yes"                  -f -l yes                                                  -d 'Do not ask for confirmation whether to push the branch'
 complete -c git-machete -n "__fish_seen_subcommand_from github; and __fish_seen_subcommand_from retarget-pr;  and not __fish_seen_subcommand_from --branch"               -x -l branch -s b       -a '(__machete_managed_branches)'  -d 'Specify the branch for which the associated PR base will be set to its upstream (parent) branch'
 complete -c git-machete -n "__fish_seen_subcommand_from github; and __fish_seen_subcommand_from retarget-pr;  and not __fish_seen_subcommand_from --ignore-if-missing"    -f -l ignore-if-missing                                    -d 'Ignore errors and quietly terminate execution if there is no PR opened for current (or specified) branch'
 
diff --git a/docs/man/git-machete.1 b/docs/man/git-machete.1
index 5f11c24..e12386e 100644
--- a/docs/man/git-machete.1
+++ b/docs/man/git-machete.1
@@ -1091,7 +1091,7 @@ Checkout open PRs for the current user associated with the GitHub token.
 .sp
 \fB<PR\-number\-1> ... <PR\-number\-N>\fP    Pull request numbers to checkout.
 .TP
-.B \fBcreate\-pr [\-\-draft] [\-\-title=<title>]\fP:
+.B \fBcreate\-pr [\-\-draft] [\-\-title=<title>] [\-\-yes]\fP:
 Creates a PR for the current branch, using the upstream (parent) branch as the PR base.
 Once the PR is successfully created, annotates the current branch with the new PR\(aqs number.
 .sp
@@ -1111,6 +1111,9 @@ Create the new PR as a draft.
 .TP
 .BI \-\-title\fB= <title>
 Set the PR title explicitly (the default is to use the first included commit\(aqs message as the title).
+.TP
+.B  \-\-yes
+Do not ask for confirmation whether to push the branch.
 .UNINDENT
 .TP
 .B \fBrestack\-pr\fP:
diff --git a/docs/source/cli/github.rst b/docs/source/cli/github.rst
index 0f0af11..4b41815 100644
--- a/docs/source/cli/github.rst
+++ b/docs/source/cli/github.rst
@@ -53,7 +53,7 @@ Creates, checks out and manages GitHub PRs while keeping them reflected in branc
 
     ``<PR-number-1> ... <PR-number-N>``    Pull request numbers to checkout.
 
-``create-pr [--draft] [--title=<title>]``:
+``create-pr [--draft] [--title=<title>] [--yes]``:
     Creates a PR for the current branch, using the upstream (parent) branch as the PR base.
     Once the PR is successfully created, annotates the current branch with the new PR's number.
 
@@ -71,6 +71,8 @@ Creates, checks out and manages GitHub PRs while keeping them reflected in branc
 
     --title=<title>    Set the PR title explicitly (the default is to use the first included commit's message as the title).
 
+    --yes              Do not ask for confirmation whether to push the branch.
+
 ``restack-pr``:
     Perform the following sequence of actions:
     1. If the PR for the current branch is ready for review, it gets converted to a draft.
diff --git a/git_machete/cli.py b/git_machete/cli.py
index 9771bf3..81fd8f3 100644
--- a/git_machete/cli.py
+++ b/git_machete/cli.py
@@ -250,6 +250,7 @@ def create_cli_parser() -> argparse.ArgumentParser:
     github_parser.add_argument('--mine', action='store_true')
     github_parser.add_argument('--title')
     github_parser.add_argument('--with-urls', action='store_true')
+    github_parser.add_argument('--yes', action='store_true')
 
     go_parser = subparsers.add_parser(
         'go',
@@ -697,6 +698,8 @@ def launch(orig_args: List[str]) -> None:
                 raise MacheteException("`--title` option is only valid with `create-pr` subcommand.")
             if 'with_urls' in parsed_cli and github_subcommand != 'anno-prs':
                 raise MacheteException("`--with-urls` option is only valid with `anno-prs` subcommand.")
+            if 'yes' in parsed_cli and github_subcommand != 'create-pr':
+                raise MacheteException("`--yes` option is only valid with `create-pr` subcommand.")
 
             if github_subcommand == "anno-prs":
                 machete_client.sync_annotations_to_github_prs(include_urls=cli_opts.opt_with_urls)
@@ -715,7 +718,8 @@ def launch(orig_args: List[str]) -> None:
                     head=current_branch,
                     opt_draft=cli_opts.opt_draft,
                     opt_onto=cli_opts.opt_onto,
-                    opt_title=cli_opts.opt_title)
+                    opt_title=cli_opts.opt_title,
+                    opt_yes=cli_opts.opt_yes)
             elif github_subcommand == "restack-pr":
                 machete_client.restack_github_pr()
             elif github_subcommand == "retarget-pr":
diff --git a/git_machete/client.py b/git_machete/client.py
index 6e47fbd..aa8a21d 100644
--- a/git_machete/client.py
+++ b/git_machete/client.py
@@ -2429,11 +2429,12 @@ class MacheteClient:
             head: LocalBranchShortName,
             opt_draft: bool,
             opt_onto: Optional[LocalBranchShortName],
-            opt_title: Optional[str]
+            opt_title: Optional[str],
+            opt_yes: bool
     ) -> None:
         # first make sure that head branch is synced with remote
         try:
-            self.__sync_before_creating_pr(opt_onto=opt_onto, opt_yes=False)
+            self.__sync_before_creating_pr(opt_onto=opt_onto, opt_yes=opt_yes)
         except InteractionStopped:
             return
 
@@ -2456,7 +2457,7 @@ class MacheteClient:
                 is_called_from_github=True,
                 opt_push_tracked=False,
                 opt_push_untracked=True,
-                opt_yes=False)
+                opt_yes=opt_yes)
         else:
             prs_for_base_branch = github_client.get_open_pull_requests_by_head(base)
             if len(prs_for_base_branch) >= 1:
diff --git a/git_machete/generated_docs.py b/git_machete/generated_docs.py
index c6bca43..b25a53d 100644
--- a/git_machete/generated_docs.py
+++ b/git_machete/generated_docs.py
@@ -612,7 +612,7 @@ long_docs: Dict[str, str] = {
 
               `<PR-number-1> ... <PR-number-N>`    Pull request numbers to checkout.
 
-           `create-pr [--draft] [--title=<title>]`:
+           `create-pr [--draft] [--title=<title>] [--yes]`:
  
               Creates a PR for the current branch, using the upstream (parent) branch as the PR base.
               Once the PR is successfully created, annotates the current branch with the new PR's number.
@@ -634,6 +634,9 @@ long_docs: Dict[str, str] = {
 
               Set the PR title explicitly (the default is to use the first included commit's message as the title).
 
+                 <b>--yes</b>
+              Do not ask for confirmation whether to push the branch.
+
            `restack-pr`:
  
               Perform the following sequence of actions:


Test patch:
diff --git a/tests/completion_e2e/test_completion_e2e.py b/tests/completion_e2e/test_completion_e2e.py
index 1c42ef0..50bf347 100644
--- a/tests/completion_e2e/test_completion_e2e.py
+++ b/tests/completion_e2e/test_completion_e2e.py
@@ -61,7 +61,7 @@ test_cases: Dict[str, str] = {
     "git machete github checkout-prs -":
         "--all --by --debug -h --help --mine -v --verbose",
     "git machete github create-pr -":
-        "--debug --draft -h --help --title -v --verbose",
+        "--debug --draft -h --help --title -v --verbose --yes",
     "git machete github retarget-pr --":
         "--branch --debug --help --ignore-if-missing --verbose",
     "git machete github retarget-pr -b ":
diff --git a/tests/test_github.py b/tests/test_github.py
index 1727e8f..977f8a1 100644
--- a/tests/test_github.py
+++ b/tests/test_github.py
@@ -341,5 +341,9 @@ class TestGitHub(BaseTest):
                        "--ignore-if-missing option is only valid with retarget-pr subcommand.")
         assert_failure(["github", "checkout-prs", "--all", "--mine"],
                        "checkout-prs subcommand must take exactly one of the following options: --all, --by=..., --mine, pr-number(s)")
+        assert_failure(["github", "checkout-prs", "--title=foo"],
+                       "--title option is only valid with create-pr subcommand.")
         assert_failure(["github", "restack-pr", "--with-urls"],
                        "--with-urls option is only valid with anno-prs subcommand.")
+        assert_failure(["github", "restack-pr", "--yes"],
+                       "--yes option is only valid with create-pr subcommand.")
diff --git a/tests/test_github_create_pr.py b/tests/test_github_create_pr.py
index aa334d1..fdd8eb2 100644
--- a/tests/test_github_create_pr.py
+++ b/tests/test_github_create_pr.py
@@ -298,7 +298,6 @@ class TestGitHubCreatePR(BaseTest):
         )
 
     def test_github_create_pr_missing_base_branch_on_remote(self, mocker: MockerFixture) -> None:
-        self.patch_symbol(mocker, 'builtins.input', mock_input_returning_y)
         self.patch_symbol(mocker, 'git_machete.github.OrganizationAndRepository.from_url', mock_from_url)
         self.patch_symbol(mocker, 'git_machete.github.GitHubToken.for_domain', mock_github_token_for_domain_none)
         self.patch_symbol(mocker, 'urllib.request.urlopen', mock_urlopen(
@@ -327,11 +326,11 @@ class TestGitHubCreatePR(BaseTest):
 
         expected_msg = ("Fetching origin...\n"
                         "Warn: Base branch for this PR (feature/api_handling) is not found on remote, pushing...\n"
-                        "Push untracked branch feature/api_handling to origin? (y, Q)\n"
+                        "Pushing untracked branch feature/api_handling to origin...\n"
                         "Creating a PR from feature/api_exception_handling to feature/api_handling... OK, see www.github.com\n")
 
         self.repo_sandbox.set_git_config_key("machete.github.annotateWithUrls", "true")
-        assert_success(['github', 'create-pr'], expected_msg)
+        assert_success(['github', 'create-pr', '--yes'], expected_msg)
         assert_success(
             ['status'],
             """