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

Commit

Permalink
Hide buttons in PR if no notification configured #51
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Aug 18, 2015
1 parent abfd812 commit c67d1a7
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 65 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Changelog of Pull Request Notifier for Stash.

## 1.21
* Hiding buttons in pull request view, if no notification will be fired when it is clicked
* Using label without ID:s in admin GUI
* To avoid using same ID:s multiple times

Expand Down
91 changes: 63 additions & 28 deletions src/main/java/se/bjurr/prnfs/ManualResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class ManualResource {
private final PrnfsPullRequestEventListener prnfsPullRequestEventListener;
private final SecurityService securityService;
private final PluginSettingsFactory pluginSettingsFactory;
private final ApplicationPropertiesService propertiesService;
private final RepositoryService repositoryService;
private static final List<AdminFormValues.BUTTON_VISIBILITY> adminOk = newArrayList();
private static final List<AdminFormValues.BUTTON_VISIBILITY> systemAdminOk = newArrayList();
static {
Expand All @@ -80,34 +82,39 @@ public ManualResource(UserManager userManager, UserService userService, PluginSe
this.prnfsPullRequestEventListener = prnfsPullRequestEventListener;
this.securityService = securityService;
this.pluginSettingsFactory = pluginSettingsFactory;
this.propertiesService = propertiesService;
this.repositoryService = repositoryService;
}

@GET
@Produces(APPLICATION_JSON)
public Response get(@Context HttpServletRequest request) throws Exception {
public Response get(@Context HttpServletRequest request, @QueryParam("repositoryId") Integer repositoryId,
@QueryParam("pullRequestId") Long pullRequestId) throws Exception {
if (userManager.getRemoteUser(request) == null) {
return status(UNAUTHORIZED).build();
}
List<PrnfsButton> buttons = newArrayList();
for (PrnfsButton candidate : getSettings().getButtons()) {
final PrnfsSettings settings = getSettings();
for (PrnfsButton candidate : settings.getButtons()) {
UserKey userKey = userManager.getRemoteUserKey();
if (canUseButton(candidate, userManager.isAdmin(userKey), userManager.isSystemAdmin(userKey))) {
PrnfsPullRequestAction pullRequestAction = PrnfsPullRequestAction.valueOf(BUTTON_TRIGGER);
final PullRequest pullRequest = pullRequestService.getById(repositoryId, pullRequestId);
Map<PrnfsVariable, Supplier<String>> variables = getVariables(settings, candidate.getFormIdentifier());
if (allowedUseButton(candidate, userManager.isAdmin(userKey), userManager.isSystemAdmin(userKey))
&& triggeredByAction(settings, pullRequestAction, pullRequest, variables, request)) {
buttons.add(candidate);
}
}
return ok(gson.toJson(buttons), APPLICATION_JSON).build();
}

static boolean canUseButton(PrnfsButton candidate, boolean isAdmin, boolean isSystemAdmin) {
if (candidate.getVisibility().equals(EVERYONE)) {
return TRUE;
}
if (isSystemAdmin && systemAdminOk.contains(candidate.getVisibility())) {
return TRUE;
} else if (isAdmin && adminOk.contains(candidate.getVisibility())) {
return TRUE;
} else if (candidate.getVisibility().equals(EVERYONE)) {
return TRUE;
private boolean triggeredByAction(PrnfsSettings settings, PrnfsPullRequestAction pullRequestAction,
PullRequest pullRequest, Map<PrnfsVariable, Supplier<String>> variables, HttpServletRequest request) {
for (PrnfsNotification prnfsNotification : settings.getNotifications()) {
PrnfsRenderer renderer = getRenderer(pullRequest, prnfsNotification, pullRequestAction, variables, request);
if (prnfsPullRequestEventListener.notificationTriggeredByAction(prnfsNotification, pullRequestAction, renderer)) {
return TRUE;
}
}
return FALSE;
}
Expand All @@ -121,28 +128,56 @@ public Response post(@Context HttpServletRequest request, @QueryParam("repositor
return status(UNAUTHORIZED).build();
}

final PullRequest pullRequest = pullRequestService.getById(repositoryId, pullRequestId);
final PrnfsSettings settings = getSettings();
for (PrnfsNotification prnfsNotification : settings.getNotifications()) {
PrnfsPullRequestAction pullRequestAction = PrnfsPullRequestAction.valueOf(BUTTON_TRIGGER);
StashUser stashUser = userService.getUserBySlug(userManager.getRemoteUser(request).getUsername());
Map<PrnfsVariable, Supplier<String>> variables = new HashMap<PrnfsRenderer.PrnfsVariable, Supplier<String>>();
variables.put(BUTTON_TRIGGER_TITLE, new Supplier<String>() {
@Override
public String get() {
return find(settings.getButtons(), new Predicate<PrnfsButton>() {
@Override
public boolean apply(PrnfsButton input) {
return input.getFormIdentifier().equals(formIdentifier);
}
}).getTitle();
}
});
prnfsPullRequestEventListener.notify(prnfsNotification, pullRequestAction, pullRequest, stashUser, variables);
final PullRequest pullRequest = pullRequestService.getById(repositoryId, pullRequestId);
Map<PrnfsVariable, Supplier<String>> variables = getVariables(settings, formIdentifier);
PrnfsRenderer renderer = getRenderer(pullRequest, prnfsNotification, pullRequestAction, variables, request);
if (prnfsPullRequestEventListener.notificationTriggeredByAction(prnfsNotification, pullRequestAction, renderer)) {
prnfsPullRequestEventListener.notify(prnfsNotification, pullRequestAction, pullRequest, variables, renderer);
}
}
return status(OK).build();
}

private Map<PrnfsVariable, Supplier<String>> getVariables(final PrnfsSettings settings, final String formIdentifier) {
Map<PrnfsVariable, Supplier<String>> variables = new HashMap<PrnfsRenderer.PrnfsVariable, Supplier<String>>();
variables.put(BUTTON_TRIGGER_TITLE, new Supplier<String>() {
@Override
public String get() {
return find(settings.getButtons(), new Predicate<PrnfsButton>() {
@Override
public boolean apply(PrnfsButton input) {
return input.getFormIdentifier().equals(formIdentifier);
}
}).getTitle();
}
});
return variables;
}

private PrnfsRenderer getRenderer(final PullRequest pullRequest, PrnfsNotification prnfsNotification,
PrnfsPullRequestAction pullRequestAction, Map<PrnfsVariable, Supplier<String>> variables, HttpServletRequest request) {
StashUser stashUser = userService.getUserBySlug(userManager.getRemoteUser(request).getUsername());
return new PrnfsRenderer(pullRequest, pullRequestAction, stashUser, repositoryService, propertiesService,
prnfsNotification, variables);
}

static boolean allowedUseButton(PrnfsButton candidate, boolean isAdmin, boolean isSystemAdmin) {
if (candidate.getVisibility().equals(EVERYONE)) {
return TRUE;
}
if (isSystemAdmin && systemAdminOk.contains(candidate.getVisibility())) {
return TRUE;
} else if (isAdmin && adminOk.contains(candidate.getVisibility())) {
return TRUE;
} else if (candidate.getVisibility().equals(EVERYONE)) {
return TRUE;
}
return FALSE;
}

private PrnfsSettings getSettings() throws Exception {
final PrnfsSettings settings = securityService.withPermission(ADMIN, "Getting config").call(
new Operation<PrnfsSettings, Exception>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Optional.absent;
import static com.google.common.collect.Maps.newHashMap;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static java.util.regex.Pattern.compile;
import static javax.ws.rs.core.HttpHeaders.AUTHORIZATION;
import static javax.xml.bind.DatatypeConverter.printBase64Binary;
Expand Down Expand Up @@ -142,7 +144,9 @@ public String get() {
}
});
}
notify(notification, action, pullRequestEvent.getPullRequest(), pullRequestEvent.getUser(), variables);
PrnfsRenderer renderer = new PrnfsRenderer(pullRequestEvent.getPullRequest(), action, pullRequestEvent.getUser(), repositoryService,
propertiesService, notification, variables);
notify(notification, action, pullRequestEvent.getPullRequest(), variables, renderer);
}
} catch (final ValidationException e) {
logger.error("", e);
Expand All @@ -151,16 +155,8 @@ public String get() {

@SuppressWarnings("deprecation")
public void notify(final PrnfsNotification notification, PrnfsPullRequestAction pullRequestAction,
PullRequest pullRequest, StashUser stashUser, Map<PrnfsVariable, Supplier<String>> variables) {
PrnfsRenderer renderer = new PrnfsRenderer(pullRequest, pullRequestAction, stashUser, repositoryService,
propertiesService, notification, variables);
if (!notification.getTriggers().contains(pullRequestAction)) {
return;
}
if (notification.getFilterRegexp().isPresent()
&& notification.getFilterString().isPresent()
&& !compile(notification.getFilterRegexp().get()).matcher(renderer.render(notification.getFilterString().get()))
.find()) {
PullRequest pullRequest, Map<PrnfsVariable, Supplier<String>> variables, PrnfsRenderer renderer) {
if (!notificationTriggeredByAction(notification, pullRequestAction, renderer)) {
return;
}
Optional<String> postContent = absent();
Expand Down Expand Up @@ -188,4 +184,18 @@ public void notify(final PrnfsNotification notification, PrnfsPullRequestAction
urlInvoker.withProxyPassword(notification.getProxyPassword());
invoker.invoke(urlInvoker);
}

public boolean notificationTriggeredByAction(PrnfsNotification notification, PrnfsPullRequestAction pullRequestAction,
PrnfsRenderer renderer) {
if (!notification.getTriggers().contains(pullRequestAction)) {
return FALSE;
}
if (notification.getFilterRegexp().isPresent()
&& notification.getFilterString().isPresent()
&& !compile(notification.getFilterRegexp().get()).matcher(renderer.render(notification.getFilterString().get()))
.find()) {
return FALSE;
}
return TRUE;
}
}
32 changes: 16 additions & 16 deletions src/test/java/se/bjurr/prnfs/ManualResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static java.lang.Boolean.TRUE;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static se.bjurr.prnfs.ManualResource.canUseButton;
import static se.bjurr.prnfs.ManualResource.allowedUseButton;
import static se.bjurr.prnfs.admin.AdminFormValues.BUTTON_VISIBILITY.ADMIN;
import static se.bjurr.prnfs.admin.AdminFormValues.BUTTON_VISIBILITY.EVERYONE;
import static se.bjurr.prnfs.admin.AdminFormValues.BUTTON_VISIBILITY.NONE;
Expand All @@ -23,23 +23,23 @@ public void testThatButtonIsOnlyDisplayedForPrivilegedUsers() {
PrnfsButton systemAdmin = new PrnfsButton("", SYSTEM_ADMIN, "");
PrnfsButton none = new PrnfsButton("", NONE, "");

assertTrue(canUseButton(everyone, TRUE, TRUE));
assertTrue(canUseButton(everyone, TRUE, FALSE));
assertTrue(canUseButton(everyone, FALSE, TRUE));
assertTrue(allowedUseButton(everyone, TRUE, TRUE));
assertTrue(allowedUseButton(everyone, TRUE, FALSE));
assertTrue(allowedUseButton(everyone, FALSE, TRUE));

assertTrue(canUseButton(admin, TRUE, TRUE));
assertFalse(canUseButton(admin, FALSE, TRUE));
assertTrue(canUseButton(admin, TRUE, FALSE));
assertFalse(canUseButton(admin, FALSE, FALSE));
assertTrue(allowedUseButton(admin, TRUE, TRUE));
assertFalse(allowedUseButton(admin, FALSE, TRUE));
assertTrue(allowedUseButton(admin, TRUE, FALSE));
assertFalse(allowedUseButton(admin, FALSE, FALSE));

assertTrue(canUseButton(systemAdmin, TRUE, TRUE));
assertTrue(canUseButton(systemAdmin, FALSE, TRUE));
assertTrue(canUseButton(systemAdmin, TRUE, FALSE));
assertFalse(canUseButton(systemAdmin, FALSE, FALSE));
assertTrue(allowedUseButton(systemAdmin, TRUE, TRUE));
assertTrue(allowedUseButton(systemAdmin, FALSE, TRUE));
assertTrue(allowedUseButton(systemAdmin, TRUE, FALSE));
assertFalse(allowedUseButton(systemAdmin, FALSE, FALSE));

assertFalse(canUseButton(none, FALSE, FALSE));
assertFalse(canUseButton(none, FALSE, TRUE));
assertFalse(canUseButton(none, TRUE, FALSE));
assertFalse(canUseButton(none, FALSE, FALSE));
assertFalse(allowedUseButton(none, FALSE, FALSE));
assertFalse(allowedUseButton(none, FALSE, TRUE));
assertFalse(allowedUseButton(none, TRUE, FALSE));
assertFalse(allowedUseButton(none, FALSE, FALSE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,46 @@ public void testThatButtonCanBeUsedForTriggeringEvent() throws Exception {
.withFieldValue(AdminFormValues.FIELDS.button_title, "Trigger notification")
.withFieldValue(AdminFormValues.FIELDS.button_visibility, AdminFormValues.BUTTON_VISIBILITY.EVERYONE.name())
.build()).store().triggerButton("Button Form").invokedOnlyUrl("http://bjurr.se/Trigger%20notification")
.hasButtonEnabled("Button Form");
.hasButtonsEnabled("Button Form");
}

@Test
public void testThatButtonIsHiddenIfNoConfiguredNotificationForItWhenNotificationSetToOpened() throws Exception {
prnfsTestBuilder()
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder()
.withFieldValue(AdminFormValues.FIELDS.url, "http://bjurr.se/${" + BUTTON_TRIGGER_TITLE + "}")
.withFieldValue(AdminFormValues.FIELDS.events, OPENED.name())
.withFieldValue(AdminFormValues.FIELDS.FORM_TYPE, AdminFormValues.FORM_TYPE.TRIGGER_CONFIG_FORM.name()).build())
.store()
.withNotification(
notificationBuilder().withFieldValue(AdminFormValues.FIELDS.FORM_IDENTIFIER, "Button Form")
.withFieldValue(AdminFormValues.FIELDS.FORM_TYPE, AdminFormValues.FORM_TYPE.BUTTON_CONFIG_FORM.name())
.withFieldValue(AdminFormValues.FIELDS.button_title, "Trigger notification")
.withFieldValue(AdminFormValues.FIELDS.button_visibility, AdminFormValues.BUTTON_VISIBILITY.EVERYONE.name())
.build()).store().triggerButton("Button Form").hasNoButtonsEnabled();
}

@Test
public void testThatButtonIsHiddenIfNoConfiguredNotificationForItWhenNotificationSetToButtonTriggered()
throws Exception {
prnfsTestBuilder()
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder()
.withFieldValue(AdminFormValues.FIELDS.url, "http://bjurr.se/${" + BUTTON_TRIGGER_TITLE + "}")
.withFieldValue(AdminFormValues.FIELDS.events, BUTTON_TRIGGER)
.withFieldValue(AdminFormValues.FIELDS.filter_string, "${" + BUTTON_TRIGGER_TITLE + "}")
.withFieldValue(AdminFormValues.FIELDS.filter_regexp, "123")
.withFieldValue(AdminFormValues.FIELDS.FORM_TYPE, AdminFormValues.FORM_TYPE.TRIGGER_CONFIG_FORM.name()).build())
.store()
.withNotification(
notificationBuilder().withFieldValue(AdminFormValues.FIELDS.FORM_IDENTIFIER, "Button Form")
.withFieldValue(AdminFormValues.FIELDS.FORM_TYPE, AdminFormValues.FORM_TYPE.BUTTON_CONFIG_FORM.name())
.withFieldValue(AdminFormValues.FIELDS.button_title, "Trigger notification")
.withFieldValue(AdminFormValues.FIELDS.button_visibility, AdminFormValues.BUTTON_VISIBILITY.EVERYONE.name())
.build()).store().triggerButton("Button Form").hasNoButtonsEnabled();
}

@Test
Expand Down
28 changes: 19 additions & 9 deletions src/test/java/se/bjurr/prnfs/admin/utils/PrnfsTestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public static PrnfsTestBuilder prnfsTestBuilder() {
private final ConfigResource configResource;

private PrnfsPullRequestEventListener listener;

private final ManualResource manualResouce;

private final PluginSettings pluginSettings;
Expand Down Expand Up @@ -333,16 +334,25 @@ public void invoke(UrlInvoker urlInvoker) {
return this;
}

public PrnfsTestBuilder hasButtonEnabled(final String formIdentifier) throws Exception {
List<PrnfsButton> enabledButtons = new Gson().fromJson((String) manualResouce.get(request).getEntity(),
new TypeToken<List<PrnfsButton>>() {
public PrnfsTestBuilder hasNoButtonsEnabled() throws Exception {
return hasButtonsEnabled();
}

public PrnfsTestBuilder hasButtonsEnabled(final String... formIdentifiers) throws Exception {
Integer repositoryId = 0;
Long pullRequestId = 0L;
List<PrnfsButton> enabledButtons = new Gson().fromJson(
(String) manualResouce.get(request, repositoryId, pullRequestId).getEntity(), new TypeToken<List<PrnfsButton>>() {
}.getType());
assertTrue(tryFind(enabledButtons, new Predicate<PrnfsButton>() {
@Override
public boolean apply(PrnfsButton input) {
return input.getFormIdentifier().equals(formIdentifier);
}
}).isPresent());
assertEquals(formIdentifiers.length, enabledButtons.size());
for (final String formIdentifier : formIdentifiers) {
assertTrue(tryFind(enabledButtons, new Predicate<PrnfsButton>() {
@Override
public boolean apply(PrnfsButton input) {
return input.getFormIdentifier().equals(formIdentifier);
}
}).isPresent());
}
return this;
}

Expand Down

0 comments on commit c67d1a7

Please sign in to comment.