diff --git a/src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestEventListener.java b/src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestEventListener.java index f3a39fa0..e9a2561c 100644 --- a/src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestEventListener.java +++ b/src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestEventListener.java @@ -9,6 +9,7 @@ import static se.bjurr.prnfs.listener.UrlInvoker.urlInvoker; import static se.bjurr.prnfs.settings.SettingsStorage.getPrnfsSettings; +import com.atlassian.stash.repository.RepositoryService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,6 +40,7 @@ public interface Invoker { } private final PluginSettingsFactory pluginSettingsFactory; + private final RepositoryService repositoryService; private static final Logger logger = LoggerFactory.getLogger(PrnfsPullRequestEventListener.class); private static Invoker invoker = new Invoker() { @@ -53,8 +55,9 @@ public static void setInvoker(Invoker invoker) { PrnfsPullRequestEventListener.invoker = invoker; } - public PrnfsPullRequestEventListener(PluginSettingsFactory pluginSettingsFactory) { + public PrnfsPullRequestEventListener(PluginSettingsFactory pluginSettingsFactory, RepositoryService repositoryService) { this.pluginSettingsFactory = pluginSettingsFactory; + this.repositoryService = repositoryService; } @EventListener @@ -104,7 +107,7 @@ public void onEvent(PullRequestUpdatedEvent e) { @VisibleForTesting public void handleEvent(PullRequestEvent o, PrnfsPullRequestAction action) { - final PrnfsRenderer renderer = new PrnfsRenderer(o); + final PrnfsRenderer renderer = new PrnfsRenderer(o, repositoryService); try { final PrnfsSettings settings = getPrnfsSettings(pluginSettingsFactory.createGlobalSettings()); for (final PrnfsNotification n : settings.getNotifications()) { diff --git a/src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java b/src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java index 700271aa..2e684460 100644 --- a/src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java +++ b/src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java @@ -5,133 +5,159 @@ import com.atlassian.stash.event.pull.PullRequestCommentAddedEvent; import com.atlassian.stash.event.pull.PullRequestEvent; import com.atlassian.stash.pull.PullRequestRef; +import com.atlassian.stash.repository.Repository; +import com.atlassian.stash.repository.RepositoryCloneLinksRequest; +import com.atlassian.stash.repository.RepositoryService; +import com.atlassian.stash.util.NamedLink; + +import java.util.Set; public class PrnfsRenderer { public enum PrnfsVariable { PULL_REQUEST_FROM_HASH(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getFromRef().getLatestChangeset(); } }), PULL_REQUEST_FROM_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getFromRef().getId(); } }), PULL_REQUEST_FROM_BRANCH(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return branchNameFromId(pullRequestEvent.getPullRequest().getFromRef()); } }), PULL_REQUEST_FROM_REPO_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getFromRef().getRepository().getId() + ""; } }), PULL_REQUEST_FROM_REPO_NAME(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getFromRef().getRepository().getName() + ""; } }), PULL_REQUEST_FROM_REPO_PROJECT_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getFromRef().getRepository().getProject().getId() + ""; } }), PULL_REQUEST_FROM_REPO_PROJECT_KEY(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getFromRef().getRepository().getProject().getKey(); } }), PULL_REQUEST_FROM_REPO_SLUG(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getFromRef().getRepository().getSlug() + ""; } + }), PULL_REQUEST_FROM_SSH_CLONE_URL(new Resolver() { + @Override + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + return cloneUrlFromRepository("ssh", pullRequestEvent.getPullRequest().getFromRef().getRepository(), repositoryService); + } + }), PULL_REQUEST_FROM_HTTP_CLONE_URL(new Resolver() { + @Override + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + return cloneUrlFromRepository("http", pullRequestEvent.getPullRequest().getFromRef().getRepository(), repositoryService); + } }), PULL_REQUEST_ACTION(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return fromPullRequestEvent(pullRequestEvent).getName(); } }), PULL_REQUEST_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getId() + ""; } }), PULL_REQUEST_VERSION(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getVersion() + ""; } }), PULL_REQUEST_AUTHOR_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getAuthor().getUser().getId() + ""; } }), PULL_REQUEST_AUTHOR_DISPLAY_NAME(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getAuthor().getUser().getDisplayName(); } }), PULL_REQUEST_AUTHOR_NAME(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getAuthor().getUser().getName(); } }), PULL_REQUEST_AUTHOR_EMAIL(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getAuthor().getUser().getEmailAddress(); } }), PULL_REQUEST_AUTHOR_SLUG(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getAuthor().getUser().getSlug(); } }), PULL_REQUEST_TO_HASH(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getToRef().getLatestChangeset(); } }), PULL_REQUEST_TO_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getToRef().getId(); } }), PULL_REQUEST_TO_BRANCH(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return branchNameFromId(pullRequestEvent.getPullRequest().getToRef()); } }), PULL_REQUEST_TO_REPO_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getToRef().getRepository().getId() + ""; } }), PULL_REQUEST_TO_REPO_NAME(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getToRef().getRepository().getName() + ""; } }), PULL_REQUEST_TO_REPO_PROJECT_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getToRef().getRepository().getProject().getId() + ""; } }), PULL_REQUEST_TO_REPO_PROJECT_KEY(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getToRef().getRepository().getProject().getKey(); } }), PULL_REQUEST_TO_REPO_SLUG(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { return pullRequestEvent.getPullRequest().getToRef().getRepository().getSlug() + ""; } + }), PULL_REQUEST_TO_SSH_CLONE_URL(new Resolver() { + @Override + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + return cloneUrlFromRepository("ssh", pullRequestEvent.getPullRequest().getToRef().getRepository(), repositoryService); + } + }) , PULL_REQUEST_TO_HTTP_CLONE_URL(new Resolver() { + @Override + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + return cloneUrlFromRepository("http", pullRequestEvent.getPullRequest().getToRef().getRepository(), repositoryService); + } }), PULL_REQUEST_COMMENT_TEXT(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { if (pullRequestEvent instanceof PullRequestCommentAddedEvent) { return ((PullRequestCommentAddedEvent) pullRequestEvent).getComment().getText(); } else { @@ -148,30 +174,39 @@ private static String branchNameFromId(PullRequestRef pullRequestRef) { return branchId.substring(lastSlash + 1); } - private PrnfsVariable(Resolver resolver) { + private static String cloneUrlFromRepository(String protocol, Repository repository, RepositoryService repositoryService) { + RepositoryCloneLinksRequest request = new RepositoryCloneLinksRequest.Builder().protocol(protocol).repository(repository).build(); + final Set cloneLinks = repositoryService.getCloneLinks(request); + return cloneLinks.iterator().hasNext() ? cloneLinks.iterator().next().getHref() : ""; + + } + + PrnfsVariable(Resolver resolver) { this.resolver = resolver; } - public String resolve(PullRequestEvent pullRequestEvent) { - return resolver.resolve(pullRequestEvent); + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + return resolver.resolve(pullRequestEvent, repositoryService); } } public interface Resolver { - public String resolve(PullRequestEvent pullRequestEvent); + String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService); } private final PullRequestEvent pullRequestEvent; + private final RepositoryService repositoryService; - public PrnfsRenderer(PullRequestEvent pullRequestEvent) { + public PrnfsRenderer(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { this.pullRequestEvent = pullRequestEvent; + this.repositoryService = repositoryService; } public String render(String string) { for (final PrnfsVariable variable : PrnfsVariable.values()) { final String regExpStr = "\\$\\{" + variable.name() + "\\}"; if (string.contains(regExpStr.replaceAll("\\\\", ""))) { - string = string.replaceAll(regExpStr, variable.resolve(pullRequestEvent)); + string = string.replaceAll(regExpStr, variable.resolve(pullRequestEvent, repositoryService)); } } return string; diff --git a/src/main/resources/admin.vm b/src/main/resources/admin.vm index ebc76df1..a3b77302 100644 --- a/src/main/resources/admin.vm +++ b/src/main/resources/admin.vm @@ -16,6 +16,8 @@
  • ${PULL_REQUEST_AUTHOR_ID} Example: 1
  • ${PULL_REQUEST_AUTHOR_NAME} Example: admin
  • ${PULL_REQUEST_AUTHOR_SLUG} Example: admin
  • +
  • ${PULL_REQUEST_FROM_SSH_CLONE_URL} Example: ssh://git@localhost:7999/project_1/rep_1
  • +
  • ${PULL_REQUEST_FROM_HTTP_CLONE_URL} Example: http://admin@localhost:7990/stash/scm/project_1/rep_1.git
  • ${PULL_REQUEST_FROM_HASH} Example: 6053a1eaa1c009dd11092d09a72f3c41af1b59ad
  • ${PULL_REQUEST_FROM_ID} Example: refs/heads/branch_mod_merge
  • ${PULL_REQUEST_FROM_BRANCH} Example: branch_mod_merge
  • @@ -24,6 +26,8 @@
  • ${PULL_REQUEST_FROM_REPO_PROJECT_ID} Example: 1
  • ${PULL_REQUEST_FROM_REPO_PROJECT_KEY} Example: PROJECT_1
  • ${PULL_REQUEST_FROM_REPO_SLUG} Example: rep_1
  • +
  • ${PULL_REQUEST_TO_SSH_CLONE_URL} Example: ssh://git@localhost:7999/project_1/rep_1
  • +
  • ${PULL_REQUEST_TO_HTTP_CLONE_URL} Example: http://admin@localhost:7990/stash/scm/project_1/rep_1.git
  • ${PULL_REQUEST_TO_HASH} Example: d6edcbf924697ab811a867421dab60d954ccad99
  • ${PULL_REQUEST_TO_ID} Example: refs/heads/basic_branching
  • ${PULL_REQUEST_VERSION} Example: 1
  • diff --git a/src/main/resources/atlassian-plugin.xml b/src/main/resources/atlassian-plugin.xml index b4792699..8e67e6ca 100644 --- a/src/main/resources/atlassian-plugin.xml +++ b/src/main/resources/atlassian-plugin.xml @@ -16,6 +16,7 @@ + Provides REST resources for the admin UI. diff --git a/src/test/java/se/bjurr/prnfs/admin/PrnfsPullRequestEventListenerTest.java b/src/test/java/se/bjurr/prnfs/admin/PrnfsPullRequestEventListenerTest.java index d37fa1c6..3b095b76 100644 --- a/src/test/java/se/bjurr/prnfs/admin/PrnfsPullRequestEventListenerTest.java +++ b/src/test/java/se/bjurr/prnfs/admin/PrnfsPullRequestEventListenerTest.java @@ -90,7 +90,7 @@ public void testThatAUrlWithoutVariablesCanBeInvoked() { @Test public void testThatAUrlWithVariablesFromCanBeInvoked() { for (final PrnfsVariable prnfsVariable : PrnfsVariable.values()) { - if (!prnfsVariable.name().contains("_FROM_")) { + if (!prnfsVariable.name().contains("_FROM_") || prnfsVariable.name().equals(PrnfsVariable.PULL_REQUEST_FROM_SSH_CLONE_URL.name()) || prnfsVariable.name().equals(PrnfsVariable.PULL_REQUEST_FROM_HTTP_CLONE_URL.name())) { continue; } prnfsTestBuilder() @@ -112,7 +112,7 @@ public void testThatAUrlWithVariablesFromCanBeInvoked() { @Test public void testThatAUrlWithVariablesToCanBeInvoked() { for (final PrnfsVariable prnfsVariable : PrnfsVariable.values()) { - if (!prnfsVariable.name().contains("_TO_")) { + if (!prnfsVariable.name().contains("_TO_") || prnfsVariable.name().equals(PrnfsVariable.PULL_REQUEST_TO_SSH_CLONE_URL.name()) || prnfsVariable.name().equals(PrnfsVariable.PULL_REQUEST_TO_HTTP_CLONE_URL.name())) { continue; } prnfsTestBuilder() diff --git a/src/test/java/se/bjurr/prnfs/admin/utils/PrnfsTestBuilder.java b/src/test/java/se/bjurr/prnfs/admin/utils/PrnfsTestBuilder.java index 73651ce0..49528c17 100644 --- a/src/test/java/se/bjurr/prnfs/admin/utils/PrnfsTestBuilder.java +++ b/src/test/java/se/bjurr/prnfs/admin/utils/PrnfsTestBuilder.java @@ -28,6 +28,7 @@ import javax.servlet.http.HttpServletRequest; +import com.atlassian.stash.repository.RepositoryService; import org.mockito.Matchers; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -83,6 +84,8 @@ public static PrnfsTestBuilder prnfsTestBuilder() { private final PluginSettingsFactory pluginSettingsFactory; + private final RepositoryService repositoryService; + private HttpServletRequest request; private TransactionTemplate transactionTemplate; @@ -105,6 +108,7 @@ private PrnfsTestBuilder() { userKey = new UserKey("asd"); userManager = mock(UserManager.class); pluginSettingsFactory = mock(PluginSettingsFactory.class); + repositoryService = mock(RepositoryService.class); transactionTemplate = new TransactionTemplate() { @Override public T execute(TransactionCallback action) { @@ -113,7 +117,7 @@ public T execute(TransactionCallback action) { }; when(pluginSettingsFactory.createGlobalSettings()).thenReturn(pluginSettings); configResource = new ConfigResource(userManager, pluginSettingsFactory, transactionTemplate); - listener = new PrnfsPullRequestEventListener(pluginSettingsFactory); + listener = new PrnfsPullRequestEventListener(pluginSettingsFactory, repositoryService); } public PrnfsTestBuilder delete(String id) {