Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
[Issue-28] Add new variables for SSH and HTTP clone URL source and ta…
Browse files Browse the repository at this point in the history
…rget branch
  • Loading branch information
christiangalsterer committed Apr 18, 2015
1 parent ed70d2f commit 001a9c7
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
97 changes: 66 additions & 31 deletions src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<NamedLink> 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;
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/admin.vm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<li><b>${PULL_REQUEST_AUTHOR_ID}</b> Example: 1</li>
<li><b>${PULL_REQUEST_AUTHOR_NAME}</b> Example: admin</li>
<li><b>${PULL_REQUEST_AUTHOR_SLUG}</b> Example: admin</li>
<li><b>${PULL_REQUEST_FROM_SSH_CLONE_URL}</b> Example: ssh://git@localhost:7999/project_1/rep_1</li>
<li><b>${PULL_REQUEST_FROM_HTTP_CLONE_URL}</b> Example: http://admin@localhost:7990/stash/scm/project_1/rep_1.git</li>
<li><b>${PULL_REQUEST_FROM_HASH}</b> Example: 6053a1eaa1c009dd11092d09a72f3c41af1b59ad</li>
<li><b>${PULL_REQUEST_FROM_ID}</b> Example: refs/heads/branch_mod_merge</li>
<li><b>${PULL_REQUEST_FROM_BRANCH}</b> Example: branch_mod_merge</li>
Expand All @@ -24,6 +26,8 @@
<li><b>${PULL_REQUEST_FROM_REPO_PROJECT_ID}</b> Example: 1</li>
<li><b>${PULL_REQUEST_FROM_REPO_PROJECT_KEY}</b> Example: PROJECT_1</li>
<li><b>${PULL_REQUEST_FROM_REPO_SLUG}</b> Example: rep_1</li>
<li><b>${PULL_REQUEST_TO_SSH_CLONE_URL}</b> Example: ssh://git@localhost:7999/project_1/rep_1</li>
<li><b>${PULL_REQUEST_TO_HTTP_CLONE_URL}</b> Example: http://admin@localhost:7990/stash/scm/project_1/rep_1.git</li>
<li><b>${PULL_REQUEST_TO_HASH}</b> Example: d6edcbf924697ab811a867421dab60d954ccad99</li>
<li><b>${PULL_REQUEST_TO_ID}</b> Example: refs/heads/basic_branching</li>
<li><b>${PULL_REQUEST_VERSION}</b> Example: 1</li>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/atlassian-plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<component-import key="renderer" interface="com.atlassian.templaterenderer.velocity.one.six.VelocityTemplateRenderer" />
<component-import key="pluginSettingsFactory" interface="com.atlassian.sal.api.pluginsettings.PluginSettingsFactory" />
<component-import key="transactionTemplate" interface="com.atlassian.sal.api.transaction.TransactionTemplate" />
<component-import key="repositoryService" interface="com.atlassian.stash.repository.RepositoryService" />

<rest key="rest" path="/prnfs-admin" version="1.0">
<description>Provides REST resources for the admin UI.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,6 +84,8 @@ public static PrnfsTestBuilder prnfsTestBuilder() {

private final PluginSettingsFactory pluginSettingsFactory;

private final RepositoryService repositoryService;

private HttpServletRequest request;

private TransactionTemplate transactionTemplate;
Expand All @@ -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> T execute(TransactionCallback<T> action) {
Expand All @@ -113,7 +117,7 @@ public <T> T execute(TransactionCallback<T> 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) {
Expand Down

0 comments on commit 001a9c7

Please sign in to comment.