From 80cd2216b991b7457fbafff549554dcb7623292b Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Sat, 7 Sep 2013 13:42:34 +0400 Subject: [PATCH] Github: simplify * extract variables * rename functions --- .../github/GithubCreatePullRequestAction.java | 66 +++++++++---------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubCreatePullRequestAction.java b/plugins/github/src/org/jetbrains/plugins/github/GithubCreatePullRequestAction.java index 3793215996f0..ed80ab80e09c 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubCreatePullRequestAction.java +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubCreatePullRequestAction.java @@ -126,7 +126,7 @@ public class GithubCreatePullRequestAction extends DumbAwareAction { } repository.update(); - final Pair remote = GithubUtil.findGithubRemote(repository); + Pair remote = GithubUtil.findGithubRemote(repository); if (remote == null) { GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, "Can't find GitHub remote"); return; @@ -134,11 +134,7 @@ public class GithubCreatePullRequestAction extends DumbAwareAction { final String remoteUrl = remote.getSecond(); final String remoteName = remote.getFirst().getName(); - String upstreamUrl = GithubUtil.findUpstreamRemote(repository); - final GithubFullPath upstreamUserAndRepo = - upstreamUrl == null || !GithubUrlUtil.isGithubUrl(upstreamUrl) ? null : GithubUrlUtil.getUserAndRepositoryFromRemoteUrl(upstreamUrl); - - final GithubFullPath userAndRepo = GithubUrlUtil.getUserAndRepositoryFromRemoteUrl(remoteUrl); + GithubFullPath userAndRepo = GithubUrlUtil.getUserAndRepositoryFromRemoteUrl(remoteUrl); if (userAndRepo == null) { GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, "Can't process remote: " + remoteUrl); return; @@ -150,16 +146,22 @@ public class GithubCreatePullRequestAction extends DumbAwareAction { return; } + String upstreamUrl = GithubUtil.findUpstreamRemote(repository); + GithubFullPath upstreamUserAndRepo = + upstreamUrl == null || !GithubUrlUtil.isGithubUrl(upstreamUrl) ? null : GithubUrlUtil.getUserAndRepositoryFromRemoteUrl(upstreamUrl); + final Map forks = new HashMap(); final Set branches = new HashSet(); addAvailableBranchesFromGit(repository, forks, branches); - final GithubInfo info = loadGithubInfoWithModal(project, userAndRepo, upstreamUserAndRepo, forks, branches); + GithubInfo info = loadGithubInfoAndBranchesWithModal(project, userAndRepo, upstreamUserAndRepo, forks, branches); if (info == null) { return; } + final GithubRepoDetailed repo = info.getRepo(); + final GithubAuthData auth = info.getAuthData(); - GithubRepo parent = info.getRepo().getParent(); - String suggestedBranch = + GithubRepo parent = repo.getParent(); + String defaultBranch = parent == null || parent.getDefaultBranch() == null ? null : parent.getUserName() + ":" + parent.getDefaultBranch(); Collection suggestions = ContainerUtil.map(branches, new Function() { @Override @@ -170,10 +172,10 @@ public class GithubCreatePullRequestAction extends DumbAwareAction { Consumer showDiff = new Consumer() { @Override public void consume(String ref) { - showDiffByRef(project, ref, repository, currentBranch.getName(), info.getAuthData(), forks, branches, info.getRepo().getSource()); + showDiffByRef(project, ref, repository, currentBranch.getName(), auth, forks, branches, repo.getSource()); } }; - final GithubCreatePullRequestDialog dialog = new GithubCreatePullRequestDialog(project, suggestions, suggestedBranch, showDiff); + final GithubCreatePullRequestDialog dialog = new GithubCreatePullRequestDialog(project, suggestions, defaultBranch, showDiff); DialogManager.show(dialog); if (!dialog.isOK()) { return; @@ -190,12 +192,11 @@ public class GithubCreatePullRequestAction extends DumbAwareAction { return; } - String from = info.getRepo().getUserName() + ":" + currentBranch.getName(); + String from = repo.getUserName() + ":" + currentBranch.getName(); String onto = dialog.getTargetBranch(); String targetUser = onto.substring(0, onto.indexOf(':')); - GithubAuthData auth = info.getAuthData(); - GithubFullPath targetRepo = findRepositoryByUser(project, targetUser, forks, auth, info.getRepo().getSource()); + GithubFullPath targetRepo = findRepositoryByUser(project, targetUser, forks, auth, repo.getSource()); if (targetRepo == null) { GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, "Can't find repository for specified branch: " + onto); return; @@ -216,11 +217,11 @@ public class GithubCreatePullRequestAction extends DumbAwareAction { } @Nullable - private static GithubInfo loadGithubInfoWithModal(@NotNull final Project project, - @NotNull final GithubFullPath userAndRepo, - @Nullable final GithubFullPath upstreamUserAndRepo, - @NotNull final Map forks, - @NotNull final Set branches) { + private static GithubInfo loadGithubInfoAndBranchesWithModal(@NotNull final Project project, + @NotNull final GithubFullPath userAndRepo, + @Nullable final GithubFullPath upstreamUserAndRepo, + @NotNull final Map forks, + @NotNull final Set branches) { try { return GithubUtil .computeValueInModal(project, "Access to GitHub", new ThrowableConvertor() { @@ -287,10 +288,6 @@ public class GithubCreatePullRequestAction extends DumbAwareAction { return null; } - private static boolean isRepoOwner(@NotNull String user, @NotNull GithubRepo repo) { - return StringUtil.equalsIgnoreCase(user, repo.getUserName()); - } - @Nullable private static GithubPullRequest createPullRequest(@NotNull Project project, @NotNull GithubAuthData auth, @@ -400,17 +397,17 @@ public class GithubCreatePullRequestAction extends DumbAwareAction { @Override @Nullable public DiffInfo convert(ProgressIndicator indicator) { + List list = StringUtil.split(ref, ":"); + assert list.size() == 2 : ref; + final String user = list.get(0); + final String branch = list.get(1); + TargetBranchInfo targetBranchInfo; - RemoteBranch remoteBranch = findRemoteBranch(branches, ref); + RemoteBranch remoteBranch = findRemoteBranch(branches, user, branch); if (remoteBranch != null && remoteBranch.getRemoteBranch() != null) { targetBranchInfo = getTargetBranchInfo(remoteBranch.getRemoteBranch()); } else { - List list = StringUtil.split(ref, ":"); - assert list.size() == 2 : ref; - final String user = list.get(0); - final String branch = list.get(1); - GithubFullPath forkPath = findRepositoryByUser(project, user, forks, auth, source); if (forkPath == null) { ApplicationManager.getApplication().invokeLater(new Runnable() { @@ -479,7 +476,6 @@ public class GithubCreatePullRequestAction extends DumbAwareAction { @NotNull GithubFullPath forkPath) { for (GitRemote remote : gitRepository.getRemotes()) { for (String url : remote.getUrls()) { - //noinspection ConstantConditions if (forkPath.equals(GithubUrlUtil.getUserAndRepositoryFromRemoteUrl(url))) { return new TargetBranchInfo(remote.getName(), remote.getName() + "/" + branch); } @@ -517,12 +513,10 @@ public class GithubCreatePullRequestAction extends DumbAwareAction { } @Nullable - private static RemoteBranch findRemoteBranch(@NotNull Set branches, @NotNull String ref) { - List list = StringUtil.split(ref, ":"); - assert list.size() == 2 : ref; - for (RemoteBranch branch : branches) { - if (StringUtil.equalsIgnoreCase(list.get(0), branch.getUser()) && StringUtil.equals(list.get(1), branch.getBranch())) { - return branch; + private static RemoteBranch findRemoteBranch(@NotNull Set branches, @NotNull String user, @NotNull String branch) { + for (RemoteBranch remoteBranch : branches) { + if (StringUtil.equalsIgnoreCase(user, remoteBranch.getUser()) && StringUtil.equals(branch, remoteBranch.getBranch())) { + return remoteBranch; } }