diff --git a/CHANGELOG.md b/CHANGELOG.md index 75f4a0d..74a9dbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Changelog of Pull Request Notifier for Stash. +## 1.15 +* Removing RESCOPED event, its confusing when to use it together with _FROM and _TO. RESCOPED was triggered when both _FROM and _TO changed at the exact same time. Now, just check _FROM if you only want to trigger when source branch changes, _TO if only target and both if you want to trigger for both. +* Adding logging to make it easier to debug what events are triggered. + ## 1.14 * New variables with information about the user who issued the event * ${PULL_REQUEST_USER_DISPLAY_NAME} Example: Some User diff --git a/README.md b/README.md index f6fa8d7..436adf8 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ The Pull Request Notifier for Stash can: * Send custom HTTP headers * Can optionally use proxy to connect -If you only want to trigger on RESCOPED_FROM, or RESCOPED_TO, you will also need to trigger on RESCOPED. Stash will fire an event, RESCOPED, if target and/or source branch is changed. The plugin has its own implementation to create the RESCOPED_FROM and RESCOPED_TO events. RESCOPED is transformed to RESCOPED_FROM if only source branch changed, RESCOPED_TO if only target branch changed and kept as RESCOPED if both changed. +The plugin has its own implementation to create the RESCOPED_FROM and RESCOPED_TO events. RESCOPED is transformed to RESCOPED_TO if target branch changed, RESCOPED_FROM if source branch, or both, changed. The filter text as well as the URL support variables. These are: diff --git a/src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestAction.java b/src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestAction.java index 8a096dd..6690ad5 100644 --- a/src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestAction.java +++ b/src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestAction.java @@ -10,10 +10,13 @@ import static com.atlassian.stash.pull.PullRequestAction.UNAPPROVED; import static com.atlassian.stash.pull.PullRequestAction.UPDATED; import static com.google.common.collect.Lists.newArrayList; +import static java.lang.Boolean.FALSE; import java.util.List; import java.util.Map; +import se.bjurr.prnfs.settings.PrnfsNotification; + import com.atlassian.stash.event.pull.PullRequestEvent; import com.atlassian.stash.event.pull.PullRequestRescopedEvent; import com.google.common.collect.ImmutableMap; @@ -30,7 +33,7 @@ public class PrnfsPullRequestAction { .put(MERGED.name(), new PrnfsPullRequestAction(MERGED.name())) // .put(OPENED.name(), new PrnfsPullRequestAction(OPENED.name())) // .put(REOPENED.name(), new PrnfsPullRequestAction(REOPENED.name())) // - .put(RESCOPED.name(), new PrnfsPullRequestAction(RESCOPED.name())) // + .put(RESCOPED.name(), new PrnfsPullRequestAction(RESCOPED_FROM)) // .put(RESCOPED_FROM, new PrnfsPullRequestAction(RESCOPED_FROM)) // .put(RESCOPED_TO, new PrnfsPullRequestAction(RESCOPED_TO)) // .put(UNAPPROVED.name(), new PrnfsPullRequestAction(UNAPPROVED.name())) // @@ -62,8 +65,7 @@ public static List values() { return newArrayList(values.values()); } - @SuppressWarnings("deprecation") - public static PrnfsPullRequestAction fromPullRequestEvent(PullRequestEvent event) { + public static PrnfsPullRequestAction fromPullRequestEvent(PullRequestEvent event, PrnfsNotification notification) { if (event instanceof PullRequestRescopedEvent) { PullRequestRescopedEvent rescopedEvent = (PullRequestRescopedEvent) event; boolean toChanged = !rescopedEvent.getPreviousToHash().equals( @@ -74,8 +76,22 @@ public static PrnfsPullRequestAction fromPullRequestEvent(PullRequestEvent event return PrnfsPullRequestAction.valueOf(RESCOPED_FROM); } else if (toChanged && !fromChanged) { return PrnfsPullRequestAction.valueOf(RESCOPED_TO); + } else { + if (notification.getTriggers().contains(values.get(RESCOPED_FROM))) { + return PrnfsPullRequestAction.valueOf(RESCOPED_FROM); + } else if (notification.getTriggers().contains(values.get(RESCOPED_TO))) { + return PrnfsPullRequestAction.valueOf(RESCOPED_TO); + } } } return PrnfsPullRequestAction.valueOf(event.getAction().name()); } + + @Override + public boolean equals(Object obj) { + if (obj instanceof PrnfsPullRequestAction) { + return getName().equals(((PrnfsPullRequestAction) obj).getName()); + } + return FALSE; + } } diff --git a/src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestEventListener.java b/src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestEventListener.java index a88f1fa..7e6a8ff 100644 --- a/src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestEventListener.java +++ b/src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestEventListener.java @@ -29,6 +29,7 @@ import com.atlassian.stash.event.pull.PullRequestRescopedEvent; import com.atlassian.stash.event.pull.PullRequestUnapprovedEvent; import com.atlassian.stash.event.pull.PullRequestUpdatedEvent; +import com.atlassian.stash.pull.PullRequest; import com.atlassian.stash.repository.RepositoryService; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; @@ -62,55 +63,57 @@ public PrnfsPullRequestEventListener(PluginSettingsFactory pluginSettingsFactory @EventListener public void onEvent(PullRequestApprovedEvent e) { - handleEvent(e, fromPullRequestEvent(e)); + handleEvent(e); } @EventListener public void onEvent(PullRequestCommentAddedEvent e) { - handleEvent(e, fromPullRequestEvent(e)); + handleEvent(e); } @EventListener public void onEvent(PullRequestDeclinedEvent e) { - handleEvent(e, fromPullRequestEvent(e)); + handleEvent(e); } @EventListener public void onEvent(PullRequestMergedEvent e) { - handleEvent(e, fromPullRequestEvent(e)); + handleEvent(e); } @EventListener public void onEvent(PullRequestOpenedEvent e) { - handleEvent(e, fromPullRequestEvent(e)); + handleEvent(e); } @EventListener public void onEvent(PullRequestReopenedEvent e) { - handleEvent(e, fromPullRequestEvent(e)); + handleEvent(e); } @EventListener public void onEvent(final PullRequestRescopedEvent e) { - handleEvent(e, fromPullRequestEvent(e)); + handleEvent(e); } @EventListener public void onEvent(PullRequestUnapprovedEvent e) { - handleEvent(e, fromPullRequestEvent(e)); + handleEvent(e); } @EventListener public void onEvent(PullRequestUpdatedEvent e) { - handleEvent(e, fromPullRequestEvent(e)); + handleEvent(e); } @VisibleForTesting - public void handleEvent(PullRequestEvent pullRequestEvent, PrnfsPullRequestAction action) { - final PrnfsRenderer renderer = new PrnfsRenderer(pullRequestEvent, repositoryService); + public void handleEvent(PullRequestEvent pullRequestEvent) { try { final PrnfsSettings settings = getPrnfsSettings(pluginSettingsFactory.createGlobalSettings()); for (final PrnfsNotification notification : settings.getNotifications()) { + final PrnfsRenderer renderer = new PrnfsRenderer(pullRequestEvent, repositoryService, notification); + PrnfsPullRequestAction action = fromPullRequestEvent(pullRequestEvent, notification); + PullRequest pr = pullRequestEvent.getPullRequest(); if (notification.getFilterRegexp().isPresent() && notification.getFilterString().isPresent() && !compile(notification.getFilterRegexp().get()).matcher(renderer.render(notification.getFilterString().get())) @@ -122,8 +125,13 @@ public void handleEvent(PullRequestEvent pullRequestEvent, PrnfsPullRequestActio if (notification.getPostContent().isPresent()) { postContent = Optional.of(renderer.render(notification.getPostContent().get())); } - UrlInvoker urlInvoker = urlInvoker().withUrlParam(renderer.render(notification.getUrl())) - .withMethod(notification.getMethod()).withPostContent(postContent); + String renderedUrl = renderer.render(notification.getUrl()); + logger.info(action.getName() + " "// + + pr.getFromRef().getId() + "(" + pr.getFromRef().getLatestChangeset() + ") -> " // + + pr.getToRef().getId() + "(" + pr.getToRef().getLatestChangeset() + ")" + " " // + + renderedUrl); + UrlInvoker urlInvoker = urlInvoker().withUrlParam(renderedUrl).withMethod(notification.getMethod()) + .withPostContent(postContent); if (notification.getUser().isPresent() && notification.getPassword().isPresent()) { final String userpass = notification.getUser().get() + ":" + notification.getPassword().get(); final String basicAuth = "Basic " + new String(printBase64Binary(userpass.getBytes(UTF_8))); diff --git a/src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java b/src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java index 9bdfa5f..9179c60 100644 --- a/src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java +++ b/src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java @@ -6,6 +6,8 @@ import java.util.Set; +import se.bjurr.prnfs.settings.PrnfsNotification; + import com.atlassian.stash.event.pull.PullRequestCommentAddedEvent; import com.atlassian.stash.event.pull.PullRequestEvent; import com.atlassian.stash.repository.Repository; @@ -23,149 +25,178 @@ public enum PrnfsVariable { PULL_REQUEST_FROM_HASH(new Resolver() { @SuppressWarnings("deprecation") @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getFromRef().getLatestChangeset(); } }), PULL_REQUEST_FROM_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getFromRef().getId(); } }), PULL_REQUEST_FROM_BRANCH(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getFromRef().getDisplayId(); } }), PULL_REQUEST_FROM_REPO_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getFromRef().getRepository().getId() + ""; } }), PULL_REQUEST_FROM_REPO_NAME(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getFromRef().getRepository().getName() + ""; } }), PULL_REQUEST_FROM_REPO_PROJECT_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getFromRef().getRepository().getProject().getId() + ""; } }), PULL_REQUEST_FROM_REPO_PROJECT_KEY(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getFromRef().getRepository().getProject().getKey(); } }), PULL_REQUEST_FROM_REPO_SLUG(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getFromRef().getRepository().getSlug() + ""; } }), PULL_REQUEST_FROM_SSH_CLONE_URL(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return cloneUrlFromRepository(ssh, pullRequestEvent.getPullRequest().getFromRef().getRepository(), repositoryService); } }), PULL_REQUEST_FROM_HTTP_CLONE_URL(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return cloneUrlFromRepository(http, pullRequestEvent.getPullRequest().getFromRef().getRepository(), repositoryService); } }), PULL_REQUEST_ACTION(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { - return fromPullRequestEvent(pullRequestEvent).getName(); + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { + return fromPullRequestEvent(pullRequestEvent, prnfsNotification).getName(); } }), PULL_REQUEST_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getId() + ""; } }), PULL_REQUEST_VERSION(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getVersion() + ""; } }), PULL_REQUEST_AUTHOR_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getAuthor().getUser().getId() + ""; } }), PULL_REQUEST_AUTHOR_DISPLAY_NAME(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getAuthor().getUser().getDisplayName(); } }), PULL_REQUEST_AUTHOR_NAME(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getAuthor().getUser().getName(); } }), PULL_REQUEST_AUTHOR_EMAIL(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getAuthor().getUser().getEmailAddress(); } }), PULL_REQUEST_AUTHOR_SLUG(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getAuthor().getUser().getSlug(); } }), PULL_REQUEST_TO_HASH(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getToRef().getLatestChangeset(); } }), PULL_REQUEST_TO_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getToRef().getId(); } }), PULL_REQUEST_TO_BRANCH(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getToRef().getDisplayId(); } }), PULL_REQUEST_TO_REPO_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getToRef().getRepository().getId() + ""; } }), PULL_REQUEST_TO_REPO_NAME(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getToRef().getRepository().getName() + ""; } }), PULL_REQUEST_TO_REPO_PROJECT_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getToRef().getRepository().getProject().getId() + ""; } }), PULL_REQUEST_TO_REPO_PROJECT_KEY(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getToRef().getRepository().getProject().getKey(); } }), PULL_REQUEST_TO_REPO_SLUG(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getPullRequest().getToRef().getRepository().getSlug() + ""; } }), PULL_REQUEST_TO_SSH_CLONE_URL(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return cloneUrlFromRepository(ssh, pullRequestEvent.getPullRequest().getToRef().getRepository(), repositoryService); } }), PULL_REQUEST_TO_HTTP_CLONE_URL(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return cloneUrlFromRepository(http, pullRequestEvent.getPullRequest().getToRef().getRepository(), repositoryService); } }), PULL_REQUEST_COMMENT_TEXT(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { if (pullRequestEvent instanceof PullRequestCommentAddedEvent) { return ((PullRequestCommentAddedEvent) pullRequestEvent).getComment().getText(); } else { @@ -174,27 +205,32 @@ public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repos } }), PULL_REQUEST_USER_DISPLAY_NAME(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getUser().getDisplayName(); } }), PULL_REQUEST_USER_EMAIL_ADDRESS(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getUser().getEmailAddress(); } }), PULL_REQUEST_USER_ID(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getUser().getId() + ""; } }), PULL_REQUEST_USER_NAME(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getUser().getName(); } }), PULL_REQUEST_USER_SLUG(new Resolver() { @Override - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { return pullRequestEvent.getUser().getSlug(); } }); @@ -213,28 +249,33 @@ private static String cloneUrlFromRepository(REPO_PROTOCOL protocol, Repository this.resolver = resolver; } - public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { - return resolver.resolve(pullRequestEvent, repositoryService); + public String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { + return resolver.resolve(pullRequestEvent, repositoryService, prnfsNotification); } } public interface Resolver { - String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService); + String resolve(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification); } private final PullRequestEvent pullRequestEvent; private final RepositoryService repositoryService; + private final PrnfsNotification prnfsNotification; - public PrnfsRenderer(PullRequestEvent pullRequestEvent, RepositoryService repositoryService) { + public PrnfsRenderer(PullRequestEvent pullRequestEvent, RepositoryService repositoryService, + PrnfsNotification prnfsNotification) { this.pullRequestEvent = pullRequestEvent; this.repositoryService = repositoryService; + this.prnfsNotification = prnfsNotification; } 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, repositoryService)); + string = string.replaceAll(regExpStr, variable.resolve(pullRequestEvent, repositoryService, prnfsNotification)); } } return string; diff --git a/src/main/resources/admin.vm b/src/main/resources/admin.vm index c9b980e..9284b1e 100644 --- a/src/main/resources/admin.vm +++ b/src/main/resources/admin.vm @@ -79,12 +79,10 @@

- -
- -
+ +

diff --git a/src/test/java/se/bjurr/prnfs/admin/PrnfsPullRequestEventListenerTest.java b/src/test/java/se/bjurr/prnfs/admin/PrnfsPullRequestEventListenerTest.java index 23c33a9..f4a6942 100644 --- a/src/test/java/se/bjurr/prnfs/admin/PrnfsPullRequestEventListenerTest.java +++ b/src/test/java/se/bjurr/prnfs/admin/PrnfsPullRequestEventListenerTest.java @@ -15,9 +15,12 @@ import static se.bjurr.prnfs.admin.utils.NotificationBuilder.notificationBuilder; import static se.bjurr.prnfs.admin.utils.PrnfsParticipantBuilder.prnfsParticipantBuilder; import static se.bjurr.prnfs.admin.utils.PrnfsTestBuilder.prnfsTestBuilder; +import static se.bjurr.prnfs.admin.utils.PullRequestEventBuilder.PREVIOUS_FROM_HASH; +import static se.bjurr.prnfs.admin.utils.PullRequestEventBuilder.PREVIOUS_TO_HASH; import static se.bjurr.prnfs.admin.utils.PullRequestEventBuilder.pullRequestEventBuilder; import static se.bjurr.prnfs.admin.utils.PullRequestRefBuilder.pullRequestRefBuilder; import static se.bjurr.prnfs.listener.PrnfsPullRequestAction.RESCOPED_FROM; +import static se.bjurr.prnfs.listener.PrnfsPullRequestAction.RESCOPED_TO; import java.io.IOException; import java.net.URL; @@ -57,10 +60,10 @@ public void testThatAUrlCanHaveSeveralVariables() { "http://bjurr.se/?PULL_REQUEST_FROM_HASH=${PULL_REQUEST_FROM_HASH}&PULL_REQUEST_TO_HASH=${PULL_REQUEST_TO_HASH}&PULL_REQUEST_FROM_REPO_SLUG=${PULL_REQUEST_FROM_REPO_SLUG}&PULL_REQUEST_TO_REPO_SLUG=${PULL_REQUEST_TO_REPO_SLUG}") .withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()).build()) .store() - .trigger(pullRequestEventBuilder() // - .withFromRef(pullRequestRefBuilder().withHash("cde456").withRepositorySlug("fromslug")) // - .withToRef(pullRequestRefBuilder().withHash("asd123").withRepositorySlug("toslug")) // - .withId(10L).withPullRequestAction(OPENED).build()) + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withHash("cde456").withRepositorySlug("fromslug")) + .withToRef(pullRequestRefBuilder().withHash("asd123").withRepositorySlug("toslug")).withId(10L) + .withPullRequestAction(OPENED).build()) .invokedUrl( 0, "http://bjurr.se/?PULL_REQUEST_FROM_HASH=cde456&PULL_REQUEST_TO_HASH=asd123&PULL_REQUEST_FROM_REPO_SLUG=fromslug&PULL_REQUEST_TO_REPO_SLUG=toslug") @@ -112,8 +115,8 @@ public void testThatAUrlWithVariablesFromCanBeInvoked() { refBuilder.withHash("10").withId("10").withProjectId(10).withProjectKey("10").withRepositoryId(10) .withRepositoryName("10").withRepositorySlug("10").withCloneUrl(PrnfsRenderer.REPO_PROTOCOL.http, "10") - .withCloneUrl(PrnfsRenderer.REPO_PROTOCOL.ssh, "10").withDisplayId("10").build() // - .withId(10L).withPullRequestAction(OPENED).triggerEvent().invokedUrl(0, "http://bjurr.se/10"); + .withCloneUrl(PrnfsRenderer.REPO_PROTOCOL.ssh, "10").withDisplayId("10").build().withId(10L) + .withPullRequestAction(OPENED).triggerEvent().invokedUrl(0, "http://bjurr.se/10"); } } @@ -156,9 +159,9 @@ public void testThatAUrlWithVariableFromBranchCanBeInvokedWhenBranchIdContainsSl .withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()).build()) .store() .trigger( - pullRequestEventBuilder() // + pullRequestEventBuilder() .withFromRef( - pullRequestRefBuilder().withId("refs/heads/feature/branchmodmerge").withDisplayId("feature/branchmodmerge")) // + pullRequestRefBuilder().withId("refs/heads/feature/branchmodmerge").withDisplayId("feature/branchmodmerge")) .withId(10L).withPullRequestAction(OPENED).build()).invokedUrl(0, "http://bjurr.se/feature/branchmodmerge"); } @@ -170,10 +173,12 @@ public void testThatAUrlWithVariableFromBranchCanBeInvokedWhenBranchIdContainsOn notificationBuilder() .withFieldValue(AdminFormValues.FIELDS.url, "http://bjurr.se/${" + PrnfsVariable.PULL_REQUEST_FROM_BRANCH.name() + "}") - .withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()).build()).store() - .trigger(pullRequestEventBuilder() // - .withFromRef(pullRequestRefBuilder().withId("branchmodmerge").withDisplayId("branchmodmerge")) // - .withId(10L).withPullRequestAction(OPENED).build()).invokedUrl(0, "http://bjurr.se/branchmodmerge"); + .withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()).build()) + .store() + .trigger( + pullRequestEventBuilder() + .withFromRef(pullRequestRefBuilder().withId("branchmodmerge").withDisplayId("branchmodmerge")).withId(10L) + .withPullRequestAction(OPENED).build()).invokedUrl(0, "http://bjurr.se/branchmodmerge"); } @Test @@ -391,7 +396,6 @@ public void testThatBasicAuthenticationHeaderIsNotSentIfThereIsNoPassword() { .withFieldValue(AdminFormValues.FIELDS.user, "theuser").withFieldValue(AdminFormValues.FIELDS.password, "") .build()).store().trigger(pullRequestEventBuilder().withPullRequestAction(OPENED).build()) .invokedUrl(0, "http://bjurr.se/").didNotSendHeaders(); - ; } @Test @@ -436,9 +440,11 @@ public void testThatFilterCanBeUsedToIgnoreEventsThatAreOnAnotherProject() { notificationBuilder().withFieldValue(AdminFormValues.FIELDS.url, "http://bjurr.se/") .withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()) .withFieldValue(FIELDS.filter_string, "${PULL_REQUEST_FROM_REPO_PROJECT_KEY}") - .withFieldValue(FIELDS.filter_regexp, "EXP").build()).store().trigger(pullRequestEventBuilder() // - .withFromRef(pullRequestRefBuilder().withProjectKey("ABC")) // - .withId(10L).withPullRequestAction(OPENED).build()).invokedNoUrl(); + .withFieldValue(FIELDS.filter_regexp, "EXP").build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withProjectKey("ABC")).withId(10L) + .withPullRequestAction(OPENED).build()).invokedNoUrl(); } @Test @@ -449,10 +455,12 @@ public void testThatFilterCanIncludeRescopedFrom() { notificationBuilder().withFieldValue(AdminFormValues.FIELDS.url, "http://bjurr.se/") .withFieldValue(AdminFormValues.FIELDS.events, RESCOPED_FROM) .withFieldValue(FIELDS.filter_string, "${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_ACTION.name() + "}") - .withFieldValue(FIELDS.filter_regexp, RESCOPED_FROM).build()).store().trigger(pullRequestEventBuilder() // - .withFromRef(pullRequestRefBuilder().withHash("from")) // - .withToRef(pullRequestRefBuilder().withHash("previousToHash")) // - .withId(10L).withPullRequestAction(RESCOPED).build()).invokedUrl(0, "http://bjurr.se/"); + .withFieldValue(FIELDS.filter_regexp, RESCOPED_FROM).build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withHash("from")) + .withToRef(pullRequestRefBuilder().withHash(PREVIOUS_TO_HASH)).withId(10L).withPullRequestAction(RESCOPED) + .build()).invokedUrl(0, "http://bjurr.se/"); } @Test @@ -470,10 +478,12 @@ public void testThatFilterCanBeUsedWithComments() { FIELDS.filter_string, "${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_TO_ID.name() + "}:${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_COMMENT_TEXT.name() + "}:") - .withFieldValue(FIELDS.filter_regexp, ".*:.*?keyword.*?:.*").build()).store().trigger(pullRequestEventBuilder() // - .withFromRef(pullRequestRefBuilder().withHash("from")) // - .withToRef(pullRequestRefBuilder().withId("123")) // - .withCommentText("keyword A nice comment").withId(10L).withPullRequestAction(COMMENTED).build()) + .withFieldValue(FIELDS.filter_regexp, ".*:.*?keyword.*?:.*").build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withHash("from")) + .withToRef(pullRequestRefBuilder().withId("123")).withCommentText("keyword A nice comment").withId(10L) + .withPullRequestAction(COMMENTED).build()) .invokedUrl(0, "http://bjurr.se/?comment=keyword%20A%20nice%20comment&version=0"); } @@ -492,11 +502,12 @@ public void testThatFilterCanBeUsedWithCommentsAndSpecialEscapedChars() { FIELDS.filter_string, "${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_TO_ID.name() + "}:${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_COMMENT_TEXT.name() + "}:") - .withFieldValue(FIELDS.filter_regexp, ".*:\\skeyword\\s:.*").build()).store().trigger(pullRequestEventBuilder() // - .withFromRef(pullRequestRefBuilder().withHash("from")) // - .withToRef(pullRequestRefBuilder().withId("123")) // - .withCommentText(" keyword ").withId(10L).withPullRequestAction(COMMENTED).build()) - .invokedUrl(0, "http://bjurr.se/?comment=%20keyword%20&version=0"); + .withFieldValue(FIELDS.filter_regexp, ".*:\\skeyword\\s:.*").build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withHash("from")) + .withToRef(pullRequestRefBuilder().withId("123")).withCommentText(" keyword ").withId(10L) + .withPullRequestAction(COMMENTED).build()).invokedUrl(0, "http://bjurr.se/?comment=%20keyword%20&version=0"); } @Test @@ -514,10 +525,12 @@ public void testThatFilterCanBeUsedWithCommentsIgnore() { FIELDS.filter_string, "${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_TO_ID.name() + "}:${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_COMMENT_TEXT.name() + "}:") - .withFieldValue(FIELDS.filter_regexp, ".*:.*?keyword.*?:.*").build()).store().trigger(pullRequestEventBuilder() // - .withFromRef(pullRequestRefBuilder().withHash("from")) // - .withToRef(pullRequestRefBuilder().withId("123")) // - .withCommentText("notkw A nice comment").withId(10L).withPullRequestAction(COMMENTED).build()).invokedNoUrl(); + .withFieldValue(FIELDS.filter_regexp, ".*:.*?keyword.*?:.*").build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withHash("from")) + .withToRef(pullRequestRefBuilder().withId("123")).withCommentText("notkw A nice comment").withId(10L) + .withPullRequestAction(COMMENTED).build()).invokedNoUrl(); } @Test @@ -541,11 +554,107 @@ public void testThatURLCanIncludeRescopedFrom() { notificationBuilder() .withFieldValue(AdminFormValues.FIELDS.url, "http://bjurr.se/${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_ACTION.name() + "}") - .withFieldValue(AdminFormValues.FIELDS.events, RESCOPED_FROM).build()).store() - .trigger(pullRequestEventBuilder() // - .withFromRef(pullRequestRefBuilder().withHash("from")) // - .withToRef(pullRequestRefBuilder().withHash("previousToHash")) // - .withPullRequestAction(RESCOPED).build()).invokedUrl(0, "http://bjurr.se/RESCOPED_FROM"); + .withFieldValue(AdminFormValues.FIELDS.events, RESCOPED_FROM).build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withHash("from")) + .withToRef(pullRequestRefBuilder().withHash(PREVIOUS_TO_HASH)) // + .withPullRequestAction(RESCOPED).build()).invokedUrl(0, "http://bjurr.se/RESCOPED_FROM"); + } + + @Test + public void testThatURLCanIncludeRescopedTo() { + prnfsTestBuilder() + .isLoggedInAsAdmin() + .withNotification( + notificationBuilder() + .withFieldValue(AdminFormValues.FIELDS.url, + "http://bjurr.se/${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_ACTION.name() + "}") + .withFieldValue(AdminFormValues.FIELDS.events, RESCOPED_TO).build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withHash(PREVIOUS_FROM_HASH)) + .withToRef(pullRequestRefBuilder().withHash("toHash")).withPullRequestAction(RESCOPED).build()) + .invokedOnlyUrl("http://bjurr.se/RESCOPED_TO"); + } + + @Test + public void testThatURLCanIncludeRescopedFromWhenBothFromAndToChanges() { + prnfsTestBuilder() + .isLoggedInAsAdmin() + .withNotification( + notificationBuilder() + .withFieldValue(AdminFormValues.FIELDS.url, + "http://bjurr.se/${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_ACTION.name() + "}") + .withFieldValue(AdminFormValues.FIELDS.events, RESCOPED_FROM).build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withHash("fromHash")) + .withToRef(pullRequestRefBuilder().withHash("toHash")).withPullRequestAction(RESCOPED).build()) + .invokedOnlyUrl("http://bjurr.se/RESCOPED_FROM"); + } + + @Test + public void testThatAllURLsMatchingEventsAreTriggered() { + prnfsTestBuilder() + .isLoggedInAsAdmin() + .withNotification( + notificationBuilder() + .withFieldValue(AdminFormValues.FIELDS.url, + "http://bjurr.se/${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_ACTION.name() + "}") + .withFieldValue(AdminFormValues.FIELDS.events, RESCOPED_FROM).build()) + .store() + .withNotification( + notificationBuilder() + .withFieldValue(AdminFormValues.FIELDS.url, + "http://bjurr.se/${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_ACTION.name() + "}") + .withFieldValue(AdminFormValues.FIELDS.events, RESCOPED_TO).build()) + .store() + .withNotification( + notificationBuilder() + .withFieldValue(AdminFormValues.FIELDS.url, + "http://bjurr.se/${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_ACTION.name() + "}") + .withFieldValue(AdminFormValues.FIELDS.events, RESCOPED_FROM) + .withFieldValue(AdminFormValues.FIELDS.events, RESCOPED_TO).build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withHash("fromHash")) + .withToRef(pullRequestRefBuilder().withHash("toHash")) // + .withPullRequestAction(RESCOPED).build()).invokedUrl(0, "http://bjurr.se/RESCOPED_FROM") + .invokedUrl(1, "http://bjurr.se/RESCOPED_TO").invokedUrl(2, "http://bjurr.se/RESCOPED_FROM"); + } + + @Test + public void testThatURLCanIncludeRescopedFromWhenBothFromAndToChangesAndBothFromAndToAreConfigured() { + prnfsTestBuilder() + .isLoggedInAsAdmin() + .withNotification( + notificationBuilder() + .withFieldValue(AdminFormValues.FIELDS.url, + "http://bjurr.se/${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_ACTION.name() + "}") + .withFieldValue(AdminFormValues.FIELDS.events, RESCOPED_FROM) + .withFieldValue(AdminFormValues.FIELDS.events, RESCOPED_TO).build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withHash("fromHash")) + .withToRef(pullRequestRefBuilder().withHash("toHash")).withPullRequestAction(RESCOPED).build()) + .invokedUrl(0, "http://bjurr.se/RESCOPED_FROM"); + } + + @Test + public void testThatURLCanIncludeRescopedToWhenBothFromAndToChanges() { + prnfsTestBuilder() + .isLoggedInAsAdmin() + .withNotification( + notificationBuilder() + .withFieldValue(AdminFormValues.FIELDS.url, + "http://bjurr.se/${" + PrnfsRenderer.PrnfsVariable.PULL_REQUEST_ACTION.name() + "}") + .withFieldValue(AdminFormValues.FIELDS.events, RESCOPED_TO).build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withHash("fromHash")) + .withToRef(pullRequestRefBuilder().withHash("toHash")).withPullRequestAction(RESCOPED).build()) + .invokedUrl(0, "http://bjurr.se/RESCOPED_TO"); } @Test @@ -556,9 +665,11 @@ public void testThatFilterCanBeUsedToIgnoreEventsThatAreOnAnotherProjectAnBranch notificationBuilder().withFieldValue(AdminFormValues.FIELDS.url, "http://bjurr.se/") .withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()) .withFieldValue(FIELDS.filter_string, "${PULL_REQUEST_FROM_REPO_PROJECT_KEY} ${PULL_REQUEST_FROM_ID}") - .withFieldValue(FIELDS.filter_regexp, "EXP my_branch").build()).store().trigger(pullRequestEventBuilder() // - .withFromRef(pullRequestRefBuilder().withProjectKey("ABC").withId("my_therbranch")) // - .withId(10L).withPullRequestAction(OPENED).build()).invokedNoUrl(); + .withFieldValue(FIELDS.filter_regexp, "EXP my_branch").build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withProjectKey("ABC").withId("my_therbranch")) + .withId(10L).withPullRequestAction(OPENED).build()).invokedNoUrl(); } @Test @@ -569,9 +680,11 @@ public void testThatFilterCanBeUsedToTriggerEventsThatAreOnAnotherProject() { notificationBuilder().withFieldValue(AdminFormValues.FIELDS.url, "http://bjurr.se/") .withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()) .withFieldValue(FIELDS.filter_string, "${PULL_REQUEST_FROM_REPO_PROJECT_KEY}") - .withFieldValue(FIELDS.filter_regexp, "EXP").build()).store().trigger(pullRequestEventBuilder() // - .withFromRef(pullRequestRefBuilder().withProjectKey("EXP")) // - .withId(10L).withPullRequestAction(OPENED).build()).invokedUrl(0, "http://bjurr.se/"); + .withFieldValue(FIELDS.filter_regexp, "EXP").build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withProjectKey("EXP")).withId(10L) + .withPullRequestAction(OPENED).build()).invokedUrl(0, "http://bjurr.se/"); } @Test @@ -582,9 +695,11 @@ public void testThatFilterCanBeUsedToTriggerOnEventsThatAreOnAnotherProjectAnBra notificationBuilder().withFieldValue(AdminFormValues.FIELDS.url, "http://bjurr.se/") .withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()) .withFieldValue(FIELDS.filter_string, "${PULL_REQUEST_FROM_REPO_PROJECT_KEY} ${PULL_REQUEST_FROM_ID}") - .withFieldValue(FIELDS.filter_regexp, "EXP my_branch").build()).store().trigger(pullRequestEventBuilder() // - .withFromRef(pullRequestRefBuilder().withProjectKey("EXP").withId("my_branch")) // - .withId(10L).withPullRequestAction(OPENED).build()).invokedUrl(0, "http://bjurr.se/"); + .withFieldValue(FIELDS.filter_regexp, "EXP my_branch").build()) + .store() + .trigger( + pullRequestEventBuilder().withFromRef(pullRequestRefBuilder().withProjectKey("EXP").withId("my_branch")) + .withId(10L).withPullRequestAction(OPENED).build()).invokedUrl(0, "http://bjurr.se/"); } @Test @@ -598,7 +713,6 @@ public void testThatMultipleUrlsCanBeInvoked() { notificationBuilder().withFieldValue(AdminFormValues.FIELDS.url, "http://opened.se/") .withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()).build()).store() .trigger(pullRequestEventBuilder() // - .withToRef(pullRequestRefBuilder()) // .withId(10L).withPullRequestAction(MERGED).build()).invokedOnlyUrl("http://merged.se/").didNotSendHeaders(); } @@ -730,5 +844,4 @@ public void testThatEventsAreMentionedInAdminGUI() throws IOException { assertTrue(prnfsAction.getName() + " in " + resource.toString(), adminVmContent.contains(prnfsAction.getName())); } } - } 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 1a38ae3..fe3f8d8 100644 --- a/src/test/java/se/bjurr/prnfs/admin/utils/PrnfsTestBuilder.java +++ b/src/test/java/se/bjurr/prnfs/admin/utils/PrnfsTestBuilder.java @@ -16,7 +16,6 @@ import static se.bjurr.prnfs.admin.AdminFormValues.NAME; import static se.bjurr.prnfs.admin.AdminFormValues.VALUE; import static se.bjurr.prnfs.admin.utils.PullRequestEventBuilder.pullRequestEventBuilder; -import static se.bjurr.prnfs.listener.PrnfsPullRequestAction.fromPullRequestEvent; import static se.bjurr.prnfs.listener.PrnfsPullRequestEventListener.setInvoker; import static se.bjurr.prnfs.listener.UrlInvoker.getHeaderValue; import static se.bjurr.prnfs.settings.PrnfsPredicates.predicate; @@ -265,7 +264,7 @@ public void invoke(UrlInvoker urlInvoker) { urlInvokers.add(urlInvoker); } }); - listener.handleEvent(event, fromPullRequestEvent(event)); + listener.handleEvent(event); return this; } diff --git a/src/test/java/se/bjurr/prnfs/admin/utils/PullRequestEventBuilder.java b/src/test/java/se/bjurr/prnfs/admin/utils/PullRequestEventBuilder.java index a91ee69..0095692 100644 --- a/src/test/java/se/bjurr/prnfs/admin/utils/PullRequestEventBuilder.java +++ b/src/test/java/se/bjurr/prnfs/admin/utils/PullRequestEventBuilder.java @@ -15,9 +15,11 @@ import com.atlassian.stash.pull.PullRequestParticipant; public class PullRequestEventBuilder { + public static final String PREVIOUS_TO_HASH = "previousToHash"; + public static final String PREVIOUS_FROM_HASH = "previousFromHash"; private PullRequestAction pullRequestAction; - private PullRequestRefBuilder toRef; - private PullRequestRefBuilder fromRef; + private PullRequestRefBuilder toRef = pullRequestRefBuilder(); + private PullRequestRefBuilder fromRef = pullRequestRefBuilder(); private Long id; private PullRequestParticipant author; private String commentText; @@ -85,8 +87,8 @@ public PullRequestEvent build() { PullRequestEvent pullRequestEvent = mock(PullRequestEvent.class); if (pullRequestAction == RESCOPED) { PullRequestRescopedEvent event = mock(PullRequestRescopedEvent.class); - when(event.getPreviousFromHash()).thenReturn("previousFromHash"); - when(event.getPreviousToHash()).thenReturn("previousToHash"); + when(event.getPreviousFromHash()).thenReturn(PREVIOUS_FROM_HASH); + when(event.getPreviousToHash()).thenReturn(PREVIOUS_TO_HASH); pullRequestEvent = event; } else if (pullRequestAction == COMMENTED) { PullRequestCommentAddedEvent event = mock(PullRequestCommentAddedEvent.class); diff --git a/src/test/java/se/bjurr/prnfs/listener/PrnfsPullRequestActionTest.java b/src/test/java/se/bjurr/prnfs/listener/PrnfsPullRequestActionTest.java deleted file mode 100644 index d96b5e9..0000000 --- a/src/test/java/se/bjurr/prnfs/listener/PrnfsPullRequestActionTest.java +++ /dev/null @@ -1,81 +0,0 @@ -package se.bjurr.prnfs.listener; - -import static com.atlassian.stash.pull.PullRequestAction.OPENED; -import static com.atlassian.stash.pull.PullRequestAction.RESCOPED; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static se.bjurr.prnfs.listener.PrnfsPullRequestAction.RESCOPED_FROM; -import static se.bjurr.prnfs.listener.PrnfsPullRequestAction.RESCOPED_TO; -import static se.bjurr.prnfs.listener.PrnfsPullRequestAction.fromPullRequestEvent; - -import org.junit.Before; -import org.junit.Test; - -import com.atlassian.stash.event.pull.PullRequestEvent; -import com.atlassian.stash.event.pull.PullRequestRescopedEvent; -import com.atlassian.stash.pull.PullRequest; -import com.atlassian.stash.pull.PullRequestAction; -import com.atlassian.stash.pull.PullRequestRef; - -public class PrnfsPullRequestActionTest { - private PullRequestRescopedEvent event; - private PullRequest pullRequest; - private PullRequestRef fromRef; - private PullRequestRef toRef; - - @Before - public void before() { - event = mock(PullRequestRescopedEvent.class); - pullRequest = mock(PullRequest.class); - fromRef = mock(PullRequestRef.class); - toRef = mock(PullRequestRef.class); - when(event.getAction()).thenReturn(RESCOPED); - when(event.getPullRequest()).thenReturn(pullRequest); - when(event.getPullRequest().getFromRef()).thenReturn(fromRef); - when(event.getPullRequest().getToRef()).thenReturn(toRef); - } - - @Test - public void testThatAllPullRequestActionEventsAreMapped() { - for (PullRequestAction a : PullRequestAction.values()) { - assertNotNull(a.name(), PrnfsPullRequestAction.valueOf(a.name())); - assertEquals(a.name(), a.name(), PrnfsPullRequestAction.valueOf(a.name()).getName()); - } - } - - @Test - public void testThatRescopedEventsAreCalculatedCorrectlyWhenFromAndToChanges() { - when(event.getPreviousFromHash()).thenReturn("FROM"); - when(event.getPreviousToHash()).thenReturn("TO"); - when(event.getPullRequest().getFromRef().getLatestChangeset()).thenReturn("FROM2"); - when(event.getPullRequest().getToRef().getLatestChangeset()).thenReturn("TO2"); - assertEquals(RESCOPED.name(), fromPullRequestEvent(event).getName()); - } - - @Test - public void testThatRescopedEventsAreCalculatedCorrectlyWhenOnlyFromChanges() { - when(event.getPreviousFromHash()).thenReturn("FROM"); - when(event.getPreviousToHash()).thenReturn("TO"); - when(event.getPullRequest().getFromRef().getLatestChangeset()).thenReturn("FROM2"); - when(event.getPullRequest().getToRef().getLatestChangeset()).thenReturn("TO"); - assertEquals(RESCOPED_FROM, fromPullRequestEvent(event).getName()); - } - - @Test - public void testThatRescopedEventsAreCalculatedCorrectlyWhenOnlyToChanges() { - when(event.getPreviousFromHash()).thenReturn("FROM"); - when(event.getPreviousToHash()).thenReturn("TO"); - when(event.getPullRequest().getFromRef().getLatestChangeset()).thenReturn("FROM"); - when(event.getPullRequest().getToRef().getLatestChangeset()).thenReturn("TO2"); - assertEquals(RESCOPED_TO, fromPullRequestEvent(event).getName()); - } - - @Test - public void testThatNoneRescopedEventsAreCalculatedCorrectly() { - PullRequestEvent superEvent = mock(PullRequestEvent.class); - when(superEvent.getAction()).thenReturn(OPENED); - assertEquals(OPENED.name(), fromPullRequestEvent(superEvent).getName()); - } -}