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

Commit

Permalink
Implementing variables suport
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Mar 28, 2015
1 parent 9e5b6db commit b84e8c2
Show file tree
Hide file tree
Showing 12 changed files with 546 additions and 45 deletions.
11 changes: 2 additions & 9 deletions src/main/java/se/bjurr/prnfs/admin/AdminFormValues.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package se.bjurr.prnfs.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import com.google.common.annotations.VisibleForTesting;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public final class AdminFormValues extends ArrayList<Map<String, String>> {
public static final String NAME = "name";
private static final long serialVersionUID = 9084184120202816120L;
public static final String VALUE = "value";

@VisibleForTesting
public void setSetting(String name, String value) {
Map<String, String> map = new HashMap<String, String>();
map.put(NAME, name);
map.put(VALUE, value);
add(map);
public enum FIELDS {
user, password, events, FORM_IDENTIFIER, url
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
package se.bjurr.prnfs.listener;

import static se.bjurr.prnfs.settings.SettingsStorage.getPrnfsSettings;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import se.bjurr.prnfs.settings.PrnfsNotification;
import se.bjurr.prnfs.settings.PrnfsSettings;
import se.bjurr.prnfs.settings.ValidationException;

import com.atlassian.event.api.EventListener;
import com.atlassian.sal.api.ApplicationProperties;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import com.atlassian.stash.event.pull.PullRequestEvent;
import com.google.common.annotations.VisibleForTesting;

public class PrnfsPullRequestEventListener {

private UrlInvoker urlInvoker = new UrlInvoker();
private final PluginSettingsFactory pluginSettingsFactory;
private static final Logger logger = LoggerFactory.getLogger(PrnfsPullRequestEventListener.class);

public PrnfsPullRequestEventListener(ApplicationProperties applicationProperties) {
public PrnfsPullRequestEventListener(PluginSettingsFactory pluginSettingsFactory) {
this.pluginSettingsFactory = pluginSettingsFactory;
}

@VisibleForTesting
public void setUrlInvoker(UrlInvoker urlInvoker) {
this.urlInvoker = urlInvoker;
}

@EventListener
public void anEvent(PullRequestEvent o) {
logger.info(o.getAction().name());
logger.info(o.getPullRequest().getTitle());
try {
final PrnfsSettings settings = getPrnfsSettings(pluginSettingsFactory.createGlobalSettings());
for (final PrnfsNotification n : settings.getNotifications()) {
urlInvoker.ivoke(new PrnfsRenderer(o).render(n.getUrl()), n.getUser(), n.getPassword());
}
} catch (final ValidationException e) {
logger.error("", e);
}
}
}
110 changes: 110 additions & 0 deletions src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package se.bjurr.prnfs.listener;

import com.atlassian.stash.event.pull.PullRequestEvent;

public class PrnfsRenderer {

public interface Resolver {
public String resolve(PullRequestEvent pullRequestEvent);
}

public enum PrnfsVariable {
PULL_REQUEST_ID(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getId() + "";
}
}), PULL_REQUEST_FROM_HASH(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getFromRef().getLatestCommit();
}
}), PULL_REQUEST_FROM_ID(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getFromRef().getId();
}
}), PULL_REQUEST_FROM_REPO_PROJECT_ID(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getFromRef().getRepository().getProject().getId() + "";
}
}), PULL_REQUEST_TO_REPO_PROJECT_ID(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getToRef().getRepository().getProject().getId() + "";
}
}), PULL_REQUEST_FROM_REPO_ID(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getFromRef().getRepository().getId() + "";
}
}), PULL_REQUEST_TO_REPO_ID(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getToRef().getRepository().getId() + "";
}
}), PULL_REQUEST_FROM_REPO_NAME(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getFromRef().getRepository().getName() + "";
}
}), PULL_REQUEST_TO_REPO_NAME(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getToRef().getRepository().getName() + "";
}
}), PULL_REQUEST_FROM_REPO_SLUG(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getFromRef().getRepository().getSlug() + "";
}
}), PULL_REQUEST_TO_REPO_SLUG(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getToRef().getRepository().getSlug() + "";
}
}), PULL_REQUEST_FROM_REPO_PROJECT_KEY(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getFromRef().getRepository().getProject().getKey();
}
}), PULL_REQUEST_TO_REPO_PROJECT_KEY(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getToRef().getRepository().getProject().getKey();
}
}), PULL_REQUEST_TO_ID(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getToRef().getId();
}
});

private Resolver resolver;

private PrnfsVariable(Resolver resolver) {
this.resolver = resolver;
}

public String resolve(PullRequestEvent pullRequestEvent) {
return resolver.resolve(pullRequestEvent);
}
}

private final PullRequestEvent pullRequestEvent;

public PrnfsRenderer(PullRequestEvent pullRequestEvent) {
this.pullRequestEvent = pullRequestEvent;
}

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));
}
}
return string;
}
}
14 changes: 14 additions & 0 deletions src/main/java/se/bjurr/prnfs/listener/UrlInvoker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package se.bjurr.prnfs.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Optional;

public class UrlInvoker {
private static final Logger logger = LoggerFactory.getLogger(UrlInvoker.class);

public void ivoke(String url, Optional<String> user, Optional<String> password) {
logger.info("Url: \"" + url + "\" user: \"" + user.or("") + "\" password: \"" + password.or("") + "\"");
}
}
18 changes: 14 additions & 4 deletions src/main/java/se/bjurr/prnfs/settings/SettingsStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

import org.slf4j.Logger;
Expand All @@ -28,7 +29,6 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;

public class SettingsStorage {
Expand Down Expand Up @@ -74,12 +74,22 @@ private static Map<String, AdminFormValues> getNotificationsMap(PluginSettings p
}

public static PrnfsNotification getPrnfsNotification(AdminFormValues a) throws ValidationException {
final Optional<Map<String, String>> urlOpt = Iterables.tryFind(a, predicate("url"));
for (final Map<String, String> m : a) {
for (final Entry<String, String> entry : m.entrySet()) {
if (entry.getKey().equals(NAME)) {
if (AdminFormValues.FIELDS.valueOf(entry.getValue()) == null) {
throw new ValidationException(entry.getValue(), "Field not recognized!");
}
} else if (!entry.getKey().equals(VALUE)) {
throw new ValidationException(entry.getKey(), "Key not recognized!");
}
}
}
final Optional<Map<String, String>> urlOpt = tryFind(a, predicate(AdminFormValues.FIELDS.url.name()));
if (!urlOpt.isPresent()) {
throw new ValidationException("url", "URL not set");
}
final PrnfsNotificationBuilder prnfsNotificationBuilder = prnfsNotificationBuilder().withUser("").withPassword("")
.withUrl(urlOpt.get().get(VALUE));
final PrnfsNotificationBuilder prnfsNotificationBuilder = prnfsNotificationBuilder().withUrl(urlOpt.get().get(VALUE));
final Iterable<Map<String, String>> events = filter(a, predicate("events"));
for (final Map<String, String> event : events) {
prnfsNotificationBuilder.withTrigger(PullRequestAction.valueOf(event.get(VALUE)));
Expand Down
20 changes: 14 additions & 6 deletions src/main/resources/admin.vm
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@
<div class="description">
You can use variables when invoking the URL.
<ul>
<li><b>${ID}</b> - Pull Request ID</li>
<li><b>${FROM_REPO}</b> - From repository</li>
<li><b>${FROM_COMMIT}</b> - From commit</li>
<li><b>${TO_REPO}</b> - To repository</li>
<li><b>${TO_COMMIT}</b> - To commit</li>
<li><b>${PULL_REQUEST_ID</b></li>
<li><b>${PULL_REQUEST_FROM_ID</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_ID</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_NAME</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_SLUG</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_PROJECT_ID</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_PROJECT_KEY</b></li>
<li><b>${PULL_REQUEST_TO_ID</b></li>
<li><b>${PULL_REQUEST_TO_REPO_ID</b></li>
<li><b>${PULL_REQUEST_TO_REPO_NAME</b></li>
<li><b>${PULL_REQUEST_TO_REPO_SLUG</b></li>
<li><b>${PULL_REQUEST_TO_REPO_PROJECT_ID</b></li>
<li><b>${PULL_REQUEST_TO_REPO_PROJECT_KEY</b></li>
</ul>
</div>
</div>
Expand Down Expand Up @@ -63,7 +71,7 @@
<div>
<fieldset>
<legend>Invoke URL</legend>
<label for="user">URL</label>
<label for="url">URL</label>
<input type="text" name="url">
<div class="error url"></div>
</fieldset>
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/se/bjurr/prnfs/admin/NotificationsStorageTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package se.bjurr.prnfs.admin;

import static com.atlassian.stash.pull.PullRequestAction.OPENED;
import static se.bjurr.prnfs.admin.utils.AdminRequestBuilder.adminRequestBuilder;
import static se.bjurr.prnfs.admin.utils.PrnfsTestBuilder.prnfsTestBuilder;
import static se.bjurr.prnfs.admin.utils.NotificationBuilder.notificationBuilder;
import static se.bjurr.prnfs.settings.SettingsStorage.FORM_IDENTIFIER_NAME;

Expand All @@ -10,7 +10,7 @@
public class NotificationsStorageTest {
@Test
public void testThatANewNotificationCanBeStored() {
adminRequestBuilder()
prnfsTestBuilder()
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder().withFieldValue("url", "http://bjurr.se/").withFieldValue("events", OPENED.name()).build())
Expand All @@ -21,7 +21,7 @@ public void testThatANewNotificationCanBeStored() {

@Test
public void testThatANewNotificationCanBeStoredWithWhiteSpaceInFormIdentifier() {
adminRequestBuilder()
prnfsTestBuilder()
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder().withFieldValue("url", "http://bjurr.se/").withFieldValue("events", OPENED.name())
Expand All @@ -32,7 +32,7 @@ public void testThatANewNotificationCanBeStoredWithWhiteSpaceInFormIdentifier()

@Test
public void testThatTwoNewNotificationsCanBeStored() {
adminRequestBuilder()
prnfsTestBuilder()
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder().withFieldValue("url", "http://bjurr.se/").withFieldValue("events", OPENED.name()).build())
Expand All @@ -46,7 +46,7 @@ public void testThatTwoNewNotificationsCanBeStored() {

@Test
public void testThatTwoNewNotificationsCanBeStoredAndThenOneDeleted() {
adminRequestBuilder()
prnfsTestBuilder()
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder().withFieldValue("url", "http://bjurr.se/").withFieldValue("events", OPENED.name()).build())
Expand All @@ -65,7 +65,7 @@ public void testThatTwoNewNotificationsCanBeStoredAndThenOneDeleted() {

@Test
public void testThatTwoNewNotificationsCanBeStoredAndThenOneUpdated() {
adminRequestBuilder()
prnfsTestBuilder()
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder().withFieldValue("url", "http://bjurr.se/").withFieldValue("events", OPENED.name()).build())
Expand Down Expand Up @@ -94,14 +94,14 @@ public void testThatTwoNewNotificationsCanBeStoredAndThenOneUpdated() {

@Test
public void testThatUrlMustBeSet() {
adminRequestBuilder().isLoggedInAsAdmin()
prnfsTestBuilder().isLoggedInAsAdmin()
.withNotification(notificationBuilder().withFieldValue("events", OPENED.name()).build()).store()
.hasValidationError("url", "URL not set");
}

@Test
public void testThatUrlMustBeValid() {
adminRequestBuilder()
prnfsTestBuilder()
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder().withFieldValue("events", OPENED.name()).withFieldValue("url", "notcorrect").build())
Expand All @@ -110,7 +110,7 @@ public void testThatUrlMustBeValid() {

@Test
public void testThatValuesMustBeSet() {
adminRequestBuilder().isLoggedInAsAdmin().withNotification(notificationBuilder().build()).store()
prnfsTestBuilder().isLoggedInAsAdmin().withNotification(notificationBuilder().build()).store()
.hasValidationError("url", "URL not set");
}
}
Loading

0 comments on commit b84e8c2

Please sign in to comment.