Github: update action quietly

This commit is contained in:
Aleksey Pivovarov
2013-08-19 21:47:04 +04:00
parent b24def6f99
commit d83c4e8189
2 changed files with 25 additions and 15 deletions
@@ -97,14 +97,14 @@ public class GithubOpenInBrowserAction extends DumbAwareAction {
return;
}
String urlToOpen = getGithubUrl(project, virtualFile, editor);
String urlToOpen = getGithubUrl(project, virtualFile, editor, false);
if (urlToOpen != null) {
BrowserUtil.launchBrowser(urlToOpen);
}
}
@Nullable
public static String getGithubUrl(@NotNull Project project, @NotNull VirtualFile virtualFile, @Nullable Editor editor) {
public static String getGithubUrl(@NotNull Project project, @NotNull VirtualFile virtualFile, @Nullable Editor editor, boolean quiet) {
GitRepositoryManager manager = GitUtil.getRepositoryManager(project);
final GitRepository repository = manager.getRepositoryForFile(virtualFile);
@@ -113,25 +113,24 @@ public class GithubOpenInBrowserAction extends DumbAwareAction {
for (GitRepository repo : manager.getRepositories()) {
details.append(repo.getPresentableUrl()).append("; ");
}
GithubNotifications.showError(project, CANNOT_OPEN_IN_BROWSER, "Can't find git repository", details.toString());
showError(project, "Can't find git repository", details.toString(), quiet);
return null;
}
final String githubRemoteUrl = GithubUtil.findGithubRemoteUrl(repository);
if (githubRemoteUrl == null) {
GithubNotifications.showError(project, CANNOT_OPEN_IN_BROWSER, "Can't find github remote");
showError(project, "Can't find github remote", null, quiet);
return null;
}
final String rootPath = repository.getRoot().getPath();
final String path = virtualFile.getPath();
if (!path.startsWith(rootPath)) {
GithubNotifications
.showError(project, CANNOT_OPEN_IN_BROWSER, "File is not under repository root", "Root: " + rootPath + ", file: " + path);
showError(project, "File is not under repository root", "Root: " + rootPath + ", file: " + path, quiet);
return null;
}
String branch = getBranchNameOnRemote(project, repository);
String branch = getBranchNameOnRemote(project, repository, quiet);
if (branch == null) {
return null;
}
@@ -139,7 +138,7 @@ public class GithubOpenInBrowserAction extends DumbAwareAction {
String relativePath = path.substring(rootPath.length());
String urlToOpen = makeUrlToOpen(editor, relativePath, branch, githubRemoteUrl);
if (urlToOpen == null) {
GithubNotifications.showError(project, CANNOT_OPEN_IN_BROWSER, "Can't create properly url", githubRemoteUrl);
showError(project, "Can't create properly url", githubRemoteUrl, quiet);
return null;
}
@@ -174,23 +173,31 @@ public class GithubOpenInBrowserAction extends DumbAwareAction {
}
@Nullable
public static String getBranchNameOnRemote(@NotNull Project project, @NotNull GitRepository repository) {
public static String getBranchNameOnRemote(@NotNull Project project, @NotNull GitRepository repository, boolean quiet) {
GitLocalBranch currentBranch = repository.getCurrentBranch();
if (currentBranch == null) {
GithubNotifications.showError(project, CANNOT_OPEN_IN_BROWSER,
"Can't open the file on GitHub when repository is on detached HEAD. Please checkout a branch.");
showError(project, "Can't open the file on GitHub when repository is on detached HEAD. Please checkout a branch.", null, quiet);
return null;
}
GitRemoteBranch tracked = currentBranch.findTrackedBranch(repository);
if (tracked == null) {
GithubNotifications
.showError(project, CANNOT_OPEN_IN_BROWSER, "Can't open the file on GitHub when current branch doesn't have a tracked branch.",
"Current branch: " + currentBranch + ", tracked info: " + repository.getBranchTrackInfos());
showError(project, "Can't open the file on GitHub when current branch doesn't have a tracked branch.",
"Current branch: " + currentBranch + ", tracked info: " + repository.getBranchTrackInfos(), quiet);
return null;
}
return tracked.getNameForRemoteOperations();
}
private static void showError(@NotNull Project project, @NotNull String message, @Nullable String details, boolean quiet) {
if (!quiet) {
if (details == null) {
GithubNotifications.showError(project, CANNOT_OPEN_IN_BROWSER, message);
}
else {
GithubNotifications.showError(project, CANNOT_OPEN_IN_BROWSER, message, details);
}
}
}
}
@@ -32,7 +32,10 @@ public class GithubWebBrowserUrlProvider extends WebBrowserUrlProvider {
@Nullable
@Override
public Url getUrl(@NotNull PsiElement element, @NotNull PsiFile psiFile, @NotNull VirtualFile virtualFile) throws BrowserException {
String url = GithubOpenInBrowserAction.getGithubUrl(element.getProject(), virtualFile, null);
String url = GithubOpenInBrowserAction.getGithubUrl(element.getProject(), virtualFile, null, true);
if (url == null) {
return null;
}
return new UrlImpl(url, "https", null, null, null);
}