|
| 1 | +package com.chriscarini.jetbrains.github.issue.navigation; |
| 2 | + |
| 3 | +import com.chriscarini.jetbrains.github.utils.GitHubUri; |
| 4 | +import com.chriscarini.jetbrains.github.utils.GitHubUtils; |
| 5 | +import com.intellij.dvcs.repo.VcsRepositoryManager; |
| 6 | +import com.intellij.dvcs.repo.VcsRepositoryMappingListener; |
| 7 | +import com.intellij.openapi.application.ApplicationManager; |
| 8 | +import com.intellij.openapi.diagnostic.Logger; |
| 9 | +import com.intellij.openapi.project.Project; |
| 10 | +import com.intellij.openapi.vcs.IssueNavigationConfiguration; |
| 11 | +import com.intellij.openapi.vcs.IssueNavigationLink; |
| 12 | +import git4idea.repo.GitRemote; |
| 13 | +import git4idea.repo.GitRepositoryImpl; |
| 14 | +import org.jetbrains.annotations.NonNls; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | + |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.List; |
| 19 | +import java.util.stream.Collectors; |
| 20 | +import java.util.stream.Stream; |
| 21 | + |
| 22 | + |
| 23 | +public class GitHubIssueNavigationVcsRepositoryMappingListener implements VcsRepositoryMappingListener { |
| 24 | + |
| 25 | + private static final @NonNls Logger LOG = Logger.getInstance(GitHubIssueNavigationVcsRepositoryMappingListener.class); |
| 26 | + private final @NotNull Project project; |
| 27 | + |
| 28 | + public GitHubIssueNavigationVcsRepositoryMappingListener(@NotNull final Project project) { |
| 29 | + this.project = project; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void mappingChanged() { |
| 34 | + |
| 35 | + if (ApplicationManager.getApplication().isUnitTestMode()) { |
| 36 | + LOG.debug("Unit test mode; will not attempt to add GitHub Issue Navigation to project"); |
| 37 | + return; |
| 38 | + } |
| 39 | + VcsRepositoryManager.getInstance(project).getRepositories().forEach(repository -> { |
| 40 | + final Collection<GitRemote> repoRemotes = ((GitRepositoryImpl) repository).getRemotes(); |
| 41 | + repoRemotes.forEach( |
| 42 | + gitRemote -> { |
| 43 | + final String url = gitRemote.getFirstUrl(); |
| 44 | + |
| 45 | + if (url == null) { |
| 46 | + LOG.debug(String.format("Null Git remote URL: %s", gitRemote)); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (!GitHubUtils.isKnownGitHubDomain(url)) { |
| 51 | + LOG.debug(String.format( |
| 52 | + "Git remote URL (%s) not from a known GitHub domain: %s", |
| 53 | + gitRemote, |
| 54 | + GitHubUtils.knownGitHubDomains().collect(Collectors.joining(", ")) |
| 55 | + )); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + final String cleanedUrl = GitHubUri.parseUrl(url).asHttpsFormatUrl(); |
| 60 | + |
| 61 | + if (existsInIssueNavConfig(project, cleanedUrl)) { |
| 62 | + LOG.debug(String.format("%s is already registered.", cleanedUrl)); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + LOG.debug(String.format("Registering [%s].", cleanedUrl)); |
| 67 | + addNewIssueNavigationConfiguration(project, cleanedUrl); |
| 68 | + } |
| 69 | + ); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + protected static boolean existsInIssueNavConfig(@NotNull final Project project, @NotNull final String url) { |
| 74 | + final List<IssueNavigationLink> matching = IssueNavigationConfiguration.getInstance(project).getLinks() |
| 75 | + .stream() |
| 76 | + .filter(issueNavigationLink -> issueNavigationLink.getLinkRegexp().contains(url)) |
| 77 | + .toList(); |
| 78 | + |
| 79 | + return !matching.isEmpty(); |
| 80 | + } |
| 81 | + |
| 82 | + protected static void addNewIssueNavigationConfiguration(@NotNull final Project project, @NotNull final String url) { |
| 83 | + final IssueNavigationConfiguration issueNavigationConfiguration = IssueNavigationConfiguration.getInstance(project); |
| 84 | + |
| 85 | + final IssueNavigationLink newLink = new IssueNavigationLink("\\(#(\\d+)\\)", String.format("%s/issues/$1", url)); |
| 86 | + |
| 87 | + issueNavigationConfiguration.setLinks( |
| 88 | + Stream.concat( |
| 89 | + issueNavigationConfiguration.getLinks().stream(), |
| 90 | + Stream.of(newLink) |
| 91 | + ).collect(Collectors.toList())); |
| 92 | + } |
| 93 | +} |
0 commit comments