|
| 1 | +import com.intellij.openapi.actionSystem.AnAction; |
| 2 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 3 | +import com.intellij.openapi.application.ApplicationInfo; |
| 4 | +import com.intellij.openapi.project.DumbAware; |
| 5 | +import com.intellij.openapi.project.Project; |
| 6 | +import com.intellij.openapi.vcs.VcsDataKeys; |
| 7 | +import com.intellij.openapi.vcs.history.VcsFileRevision; |
| 8 | +import com.intellij.vcs.log.VcsLog; |
| 9 | +import com.intellij.vcs.log.VcsLogDataKeys; |
| 10 | +import java.awt.Desktop; |
| 11 | +import java.io.IOException; |
| 12 | +import java.net.URI; |
| 13 | +import java.util.Optional; |
| 14 | +import org.apache.log4j.LogManager; |
| 15 | +import org.apache.log4j.Logger; |
| 16 | +import org.jetbrains.annotations.NotNull; |
| 17 | + |
| 18 | +/** |
| 19 | + * Jetbrains IDE action to open a selected revision in Sourcegraph. |
| 20 | + */ |
| 21 | +public class OpenRevisionAction extends AnAction implements DumbAware { |
| 22 | + private final Logger logger = LogManager.getLogger(this.getClass()); |
| 23 | + |
| 24 | + private Optional<RevisionContext> getHistoryRevision(AnActionEvent e) { |
| 25 | + VcsFileRevision revision = e.getDataContext().getData(VcsDataKeys.VCS_FILE_REVISION); |
| 26 | + Project project = e.getProject(); |
| 27 | + |
| 28 | + if (project == null) { |
| 29 | + return Optional.empty(); |
| 30 | + } |
| 31 | + if (revision == null) { |
| 32 | + return Optional.empty(); |
| 33 | + } |
| 34 | + |
| 35 | + String rev = revision.getRevisionNumber().toString(); |
| 36 | + return Optional.of(new RevisionContext(project, rev)); |
| 37 | + } |
| 38 | + |
| 39 | + private Optional<RevisionContext> getLogRevision(AnActionEvent e) { |
| 40 | + VcsLog log = e.getDataContext().getData(VcsLogDataKeys.VCS_LOG); |
| 41 | + Project project = e.getProject(); |
| 42 | + |
| 43 | + if (project == null) { |
| 44 | + return Optional.empty(); |
| 45 | + } |
| 46 | + if (log == null || log.getSelectedCommits().isEmpty()) { |
| 47 | + return Optional.empty(); |
| 48 | + } |
| 49 | + |
| 50 | + String rev = log.getSelectedCommits().get(0).getHash().asString(); |
| 51 | + return Optional.of(new RevisionContext(project, rev)); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void actionPerformed(@NotNull AnActionEvent e) { |
| 56 | + // This action handles events for both log and history views, so attempt to load from any possible option. |
| 57 | + RevisionContext context = getHistoryRevision(e).map(Optional::of) |
| 58 | + .orElseGet(() -> getLogRevision(e)) |
| 59 | + .orElseThrow(() -> new RuntimeException("Unable to determine revision from history or log.")); |
| 60 | + |
| 61 | + try { |
| 62 | + String productName = ApplicationInfo.getInstance().getVersionName(); |
| 63 | + String productVersion = ApplicationInfo.getInstance().getFullVersion(); |
| 64 | + RepoInfo repoInfo = Util.repoInfo(context.getProject().getProjectFilePath()); |
| 65 | + |
| 66 | + CommitViewUriBuilder builder = new CommitViewUriBuilder(); |
| 67 | + URI uri = builder.build(Util.sourcegraphURL(context.getProject()), context.getRevisionNumber(), repoInfo, productName, productVersion); |
| 68 | + |
| 69 | + // Open the URL in the browser. |
| 70 | + Desktop.getDesktop().browse(uri); |
| 71 | + } catch (IOException err) { |
| 72 | + logger.debug("failed to open browser"); |
| 73 | + err.printStackTrace(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void update(@NotNull AnActionEvent e) { |
| 79 | + e.getPresentation().setEnabledAndVisible(true); |
| 80 | + } |
| 81 | +} |
0 commit comments