Skip to content

Commit

Permalink
Merge pull request #739 from randy3k/pull_request
Browse files Browse the repository at this point in the history
Enhancement: offer push to remote rather than configuring remote
  • Loading branch information
stoivo committed Aug 19, 2017
2 parents 25dd004 + 4c5717d commit 7632549
Showing 1 changed file with 22 additions and 67 deletions.
89 changes: 22 additions & 67 deletions github/commands/pull_request.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sublime
from sublime_plugin import TextCommand
from sublime_plugin import WindowCommand
from webbrowser import open as open_in_browser

from ...core.git_command import GitCommand
Expand All @@ -8,20 +8,22 @@
from .. import git_mixins
from ...common import interwebs
from ...common import util
from ...core.commands.push import GsPushToBranchNameCommand

SET_UPSTREAM_PROMPT = ("You have not set an upstream for the active branch. "
"Would you like to set one?")

PUSH_PROMPT = ("You have not set an upstream for the active branch. "
"Would you like to push to a remote?")

class GsPullRequestCommand(TextCommand, GitCommand, git_mixins.GithubRemotesMixin):

class GsPullRequestCommand(WindowCommand, GitCommand, git_mixins.GithubRemotesMixin):

"""
Display open pull requests on the base repo. When a pull request is selected,
allow the user to 1) checkout the PR as detached HEAD, 2) checkout the PR as
a local branch, 3) view the PR's diff, or 4) open the PR in the browser.
"""

def run(self, edit):
def run(self):
sublime.set_timeout_async(self.run_async, 0)

def run_async(self):
Expand Down Expand Up @@ -57,7 +59,7 @@ def on_select_pr(self, pr):
return

self.pr = pr
self.view.window().show_quick_panel(
self.window.show_quick_panel(
["Checkout as detached HEAD.",
"Checkout as local branch.",
"Create local branch, but do not checkout.",
Expand All @@ -73,15 +75,15 @@ def on_select_action(self, idx):
if idx == 0:
self.fetch_and_checkout_pr()
elif idx == 1:
self.view.window().show_input_panel(
self.window.show_input_panel(
"Enter branch name for PR {}:".format(self.pr["number"]),
"pull-request-{}".format(self.pr["number"]),
self.fetch_and_checkout_pr,
None,
None
)
elif idx == 2:
self.view.window().show_input_panel(
self.window.show_input_panel(
"Enter branch name for PR {}:".format(self.pr["number"]),
"pull-request-{}".format(self.pr["number"]),
self.create_branch_for_pr,
Expand Down Expand Up @@ -129,14 +131,12 @@ def create_branch_for_pr(self, branch_name):

def view_diff_for_pr(self):
response = interwebs.get_url(self.pr["diff_url"])
print("getting", self.pr["diff_url"])
print(repr(response.payload.decode("utf-8")))

diff_view = util.view.get_scratch_view(self, "pr_diff", read_only=True)
diff_view.set_name("PR #{}".format(self.pr["number"]))
diff_view.set_syntax_file("Packages/GitSavvy/syntax/diff.sublime-syntax")

self.view.window().focus_view(diff_view)
self.window.focus_view(diff_view)
diff_view.sel().clear()
diff_view.run_command("gs_replace_view_text", {
"text": response.payload.decode("utf-8")
Expand All @@ -146,28 +146,20 @@ def open_pr_in_browser(self):
open_in_browser(self.pr["html_url"])


class GsCreatePullRequestCommand(TextCommand, GitCommand, git_mixins.GithubRemotesMixin):
class GsCreatePullRequestCommand(WindowCommand, GitCommand, git_mixins.GithubRemotesMixin):
"""
Create pull request of the current commit on the current repo.
"""

def run(self, edit):
def run(self):
sublime.set_timeout_async(self.run_async, 0)

def run_async(self):
savvy_settings = sublime.load_settings("GitSavvy.sublime-settings")
if savvy_settings.get("prompt_for_tracking_branch") and not self.get_upstream_for_active_branch():
if sublime.ok_cancel_dialog(SET_UPSTREAM_PROMPT):
self.remotes = list(self.get_remotes().keys())

if not self.remotes:
self.view.window().show_quick_panel(["There are no remotes available."], None)
else:
self.view.window().show_quick_panel(
self.remotes,
self.on_select_remote,
flags=sublime.MONOSPACE_FONT
)
if not self.get_upstream_for_active_branch():
if sublime.ok_cancel_dialog(PUSH_PROMPT):
self.window.run_command(
"gs_push_and_create_pull_request",
{"set_upstream": True})

else:
remote_branch = self.get_active_remote_branch()
Expand Down Expand Up @@ -199,46 +191,9 @@ def open_comparision_in_browser(self, owner, branch):
branch
))

def on_select_remote(self, remote_index):
"""
After the user selects a remote, display a panel of branches that are
present on that remote, then proceed to `on_select_branch`.
"""
# If the user pressed `esc` or otherwise cancelled.
if remote_index == -1:
return

self.selected_remote = self.remotes[remote_index]
self.branches_on_selected_remote = self.list_remote_branches(self.selected_remote)

self.current_local_branch = self.get_current_branch_name()

try:
pre_selected_index = self.branches_on_selected_remote.index(
self.selected_remote + "/" + self.current_local_branch)
except ValueError:
pre_selected_index = 0
class GsPushAndCreatePullRequestCommand(GsPushToBranchNameCommand):

self.view.window().show_quick_panel(
self.branches_on_selected_remote,
self.on_select_branch,
flags=sublime.MONOSPACE_FONT,
selected_index=pre_selected_index
)

def on_select_branch(self, branch_index):
"""
Determine the actual branch name of the user's selection, and proceed
to `do_pull`.
"""
# If the user pressed `esc` or otherwise cancelled.
if branch_index == -1:
return
selected_remote_branch = self.branches_on_selected_remote[branch_index].split("/", 1)[1]
remote_ref = self.selected_remote + "/" + selected_remote_branch

self.current_local_branch = self.get_current_branch_name()

self.git("branch", "-u", remote_ref, self.current_local_branch)

sublime.set_timeout_async(self.run_async, 0)
def do_push(self, *args, **kwargs):
super().do_push(*args, **kwargs)
self.window.run_command("gs_create_pull_request")

0 comments on commit 7632549

Please sign in to comment.