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

Commit

Permalink
Falling back on 1.x settings if no 2.x settings
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Sep 26, 2015
1 parent 7e95606 commit e8ecb22
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 16 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 Bitbucket.

## 2.4
* The storage key was accidently changed when migrating to Bitbucket 4. This includes a fix that will load 1.x settings if they exist, and if no 2.x settings are available.
* Adding "Triggers do not apply to" option with values DECLINED and MERGED.
* Changing wording of trigger conditions in admin GUI.

Expand Down
18 changes: 11 additions & 7 deletions src/main/java/se/bjurr/prnfb/admin/ConfigResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.UNAUTHORIZED;
import static se.bjurr.prnfb.admin.AdminFormValues.FORM_TYPE.BUTTON_CONFIG_FORM;
import static se.bjurr.prnfb.admin.AdminFormValues.FORM_TYPE.TRIGGER_CONFIG_FORM;
import static se.bjurr.prnfb.settings.PrnfbNotification.isOfType;
import static se.bjurr.prnfb.settings.SettingsStorage.checkFieldsRecognized;
import static se.bjurr.prnfb.settings.SettingsStorage.deleteSettings;
Expand Down Expand Up @@ -103,11 +105,13 @@ static boolean isAdminAllowed(UserManager userManager, HttpServletRequest reques
if (user == null) {
return false;
}
PrnfbSettings settings = securityService.withPermission(ADMIN, "Getting config").call(
() -> getPrnfbSettings(pluginSettingsFactory.createGlobalSettings()));
return userManager.isSystemAdmin(user.getUserKey()) //
|| settings.isUsersAllowed() //
|| settings.isAdminsAllowed() && userManager.isAdmin(user.getUserKey());
PrnfbSettings settings = securityService//
.withPermission(ADMIN, "Getting config")//
.call(() -> getPrnfbSettings(pluginSettingsFactory.createGlobalSettings()));
return //
userManager.isSystemAdmin(user.getUserKey()) || //
settings.isUsersAllowed() || //
settings.isAdminsAllowed() && userManager.isAdmin(user.getUserKey());
}

/**
Expand All @@ -127,11 +131,11 @@ public Response post(final AdminFormValues config, @Context HttpServletRequest r
try {
injectFormIdentifierIfNotSet(config);
checkFieldsRecognized(config);
if (isOfType(config, AdminFormValues.FORM_TYPE.TRIGGER_CONFIG_FORM)) {
if (isOfType(config, TRIGGER_CONFIG_FORM)) {
// Assuming TRIGGER_CONFIG_FORM here if field not available, to be backwards
// compatible
getPrnfbNotification(config);
} else if (isOfType(config, AdminFormValues.FORM_TYPE.BUTTON_CONFIG_FORM)) {
} else if (isOfType(config, BUTTON_CONFIG_FORM)) {
getPrnfbButton(config);
}
} catch (final ValidationException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/se/bjurr/prnfb/settings/PrnfbPredicates.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static Predicate<Map<String, String>> predicate(final String name) {
return new Predicate<Map<String, String>>() {
@Override
public boolean apply(Map<String, String> input) {
return input.get(NAME).equals(name);
return input.get(NAME) != null && input.get(NAME).equals(name);
}
};
}
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/se/bjurr/prnfb/settings/SettingsStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public class SettingsStorage {

private static Random random = new Random(currentTimeMillis());

public static final String STORAGE_KEY = AdminFormValues.class.getName() + "_2";
public static final String STORAGE_KEY = "se.bjurr.prnfb.admin.AdminFormValues_2";
public static final String STORAGE_KEY_PRNFS = "se.bjurr.prnfs.admin.AdminFormValues_2";

public static void deleteSettings(PluginSettings pluginSettings, String id) {
final Map<String, AdminFormValues> map = getNotificationsMap(pluginSettings);
Expand Down Expand Up @@ -222,11 +223,19 @@ public static PrnfbSettings getPrnfbSettings(PluginSettings pluginSettings) thro
public static List<AdminFormValues> getSettingsAsFormValues(PluginSettings settings) {
final List<AdminFormValues> toReturn = newArrayList();
try {
if (!fromNullable(settings.get(STORAGE_KEY)).isPresent()) {
return toReturn;
/**
* The storage key was accidently changed when migrating to Bitbucket 4. This
* is an attempt to load 1.x settings if they exist.
*/
Optional<Object> settingsToUse = fromNullable(settings.get(STORAGE_KEY));
if (!settingsToUse.isPresent()) {
settingsToUse = fromNullable(settings.get(STORAGE_KEY_PRNFS));
if (!settingsToUse.isPresent()) {
return toReturn;
}
}
@SuppressWarnings("unchecked")
final List<String> settingsList = newArrayList((List<String>) settings.get(STORAGE_KEY));
final List<String> settingsList = newArrayList((List<String>) settingsToUse.get());
for (final String storedJson : settingsList) {
toReturn.add(injectConfigurationName(gson.fromJson(storedJson, AdminFormValues.class)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static se.bjurr.prnbs.admin.utils.PullRequestEventBuilder.pullRequestEventBuilder;
import static se.bjurr.prnfb.admin.AdminFormValues.NAME;
import static se.bjurr.prnfb.admin.AdminFormValues.VALUE;
import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.FORM_IDENTIFIER;
import static se.bjurr.prnfb.listener.PrnfbPullRequestEventListener.setInvoker;
import static se.bjurr.prnfb.listener.UrlInvoker.getHeaderValue;
import static se.bjurr.prnfb.settings.PrnfbPredicates.predicate;
Expand Down Expand Up @@ -194,8 +195,7 @@ private Map<String, Header> toMap(List<Header> headers) {
@SuppressWarnings("unchecked")
private Map<String, AdminFormValues> getAdminFormFields() throws Exception {
return uniqueIndex((List<AdminFormValues>) configResource.get(request).getEntity(),
(Function<AdminFormValues, String>) input -> find(input, predicate(AdminFormValues.FIELDS.FORM_IDENTIFIER.name()))
.get(VALUE));
(Function<AdminFormValues, String>) input -> find(input, predicate(FORM_IDENTIFIER.name())).get(VALUE));
}

public PrnfbTestBuilder hasFieldValueAt(AdminFormValues.FIELDS field, String value, String id) throws Exception {
Expand Down Expand Up @@ -343,8 +343,7 @@ public PullRequestEventBuilder triggerPullRequestEventBuilder() {
}

public PrnfbTestBuilder withNotification(AdminFormValues adminFormValues) {
final Optional<Map<String, String>> existing = tryFind(adminFormValues,
predicate(AdminFormValues.FIELDS.FORM_IDENTIFIER.name()));
final Optional<Map<String, String>> existing = tryFind(adminFormValues, predicate(FORM_IDENTIFIER.name()));
if (existing.isPresent()) {
this.adminFormValuesMap.put(existing.get().get(VALUE), adminFormValues);
} else {
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/se/bjurr/prnfb/settings/SettingsStorageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package se.bjurr.prnfb.settings;

import static com.google.common.collect.Lists.newArrayList;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static se.bjurr.prnfb.settings.SettingsStorage.STORAGE_KEY;
import static se.bjurr.prnfb.settings.SettingsStorage.STORAGE_KEY_PRNFS;
import static se.bjurr.prnfb.settings.SettingsStorage.getSettingsAsFormValues;

import org.junit.Before;
import org.junit.Test;

import se.bjurr.prnfb.admin.AdminFormValues;

import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;

public class SettingsStorageTest {

private String prnfs;
private String prnfb;
private PluginSettings settings;

@Before
public void before() {
AdminFormValues prnfs = new AdminFormValues();
prnfs.add(ImmutableMap.<String, String> builder().put("key", "PRNFS").build());
this.prnfs = new Gson().toJson(prnfs);
AdminFormValues prnfb = new AdminFormValues();
prnfb.add(ImmutableMap.<String, String> builder().put("key", "PRNFB").build());
this.prnfb = new Gson().toJson(prnfb);
settings = mock(PluginSettings.class);
}

@Test
public void testThatPrnfsSettingsAreUsedIfAvailableAnNoPrnfbSettingsAvailable() {
when(settings.get(STORAGE_KEY_PRNFS)).thenReturn(newArrayList(prnfs));
assertEquals("PRNFS", getSettingsAsFormValues(settings).get(0).get(0).get("key"));
}

@Test
public void testThatPrnfbSettingsAreUsedIfAvailable() {
when(settings.get(STORAGE_KEY)).thenReturn(newArrayList(prnfb));
assertEquals("PRNFB", getSettingsAsFormValues(settings).get(0).get(0).get("key"));
}

@Test
public void testThatPrnfbSettingsAreUsedIfAvailableEvenIfPrnfsAreAvailable() {
when(settings.get(STORAGE_KEY)).thenReturn(newArrayList(prnfb));
when(settings.get(STORAGE_KEY_PRNFS)).thenReturn(newArrayList(prnfs));
assertEquals("PRNFB", getSettingsAsFormValues(settings).get(0).get(0).get("key"));
}

}

0 comments on commit e8ecb22

Please sign in to comment.