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

Commit d937d3c

Browse files
committed
Adding migration code for old settings format #109
1 parent 319f5de commit d937d3c

File tree

13 files changed

+1148
-5
lines changed

13 files changed

+1148
-5
lines changed

CHANGELOG.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Changelog of Pull Request Notifier for Bitbucket.
1212

1313
* Also adding EVERYTHING_URL variable . Resolved to every other variable.
1414

15-
[f52d8ae78aaa0e7](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/f52d8ae78aaa0e7) Tomas Bjerre *2016-05-07 12:53:31*
15+
[da4e64ab06398ff](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/da4e64ab06398ff) Tomas Bjerre *2016-05-08 08:16:03*
1616

1717
Refactoring
1818

@@ -23,14 +23,14 @@ Changelog of Pull Request Notifier for Bitbucket.
2323

2424
* Also adding EVERYTHING_URL variable . Resolved to every other variable.
2525

26-
[f52d8ae78aaa0e7](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/f52d8ae78aaa0e7) Tomas Bjerre *2016-05-07 12:53:31*
26+
[da4e64ab06398ff](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/da4e64ab06398ff) Tomas Bjerre *2016-05-08 08:16:03*
2727

2828
### GitHub [#25](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/25) Enable configuration in per-repository hook screen
2929
Admin GUI
3030

3131
* Also adding EVERYTHING_URL variable . Resolved to every other variable.
3232

33-
[f52d8ae78aaa0e7](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/f52d8ae78aaa0e7) Tomas Bjerre *2016-05-07 12:53:31*
33+
[da4e64ab06398ff](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/da4e64ab06398ff) Tomas Bjerre *2016-05-08 08:16:03*
3434

3535
Adding repo and global admin pages
3636

@@ -45,9 +45,13 @@ Changelog of Pull Request Notifier for Bitbucket.
4545

4646
* Also adding EVERYTHING_URL variable . Resolved to every other variable.
4747

48-
[f52d8ae78aaa0e7](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/f52d8ae78aaa0e7) Tomas Bjerre *2016-05-07 12:53:31*
48+
[da4e64ab06398ff](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/da4e64ab06398ff) Tomas Bjerre *2016-05-08 08:16:03*
4949

5050
### No issue
51+
Adding migration code for old settings format
52+
53+
[515086b440e76de](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/515086b440e76de) Tomas Bjerre *2016-05-08 08:48:18*
54+
5155
changelog maven plugin 1.29
5256

5357
[25ccd05b465bd4a](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/25ccd05b465bd4a) Tomas Bjerre *2016-04-14 16:36:16*

src/main/java/se/bjurr/prnfb/service/SettingsService.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,31 @@
77
import static com.google.common.collect.Iterables.find;
88
import static com.google.common.collect.Iterables.tryFind;
99
import static com.google.common.collect.Lists.newArrayList;
10+
import static se.bjurr.prnfb.settings.PrnfbNotificationBuilder.prnfbNotificationBuilder;
1011
import static se.bjurr.prnfb.settings.PrnfbSettingsBuilder.prnfbSettingsBuilder;
1112
import static se.bjurr.prnfb.settings.PrnfbSettingsDataBuilder.prnfbSettingsDataBuilder;
1213

1314
import java.util.List;
1415
import java.util.UUID;
1516

17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
19+
20+
import se.bjurr.prnfb.listener.PrnfbPullRequestAction;
1621
import se.bjurr.prnfb.settings.HasUuid;
1722
import se.bjurr.prnfb.settings.PrnfbButton;
1823
import se.bjurr.prnfb.settings.PrnfbNotification;
24+
import se.bjurr.prnfb.settings.PrnfbNotificationBuilder;
1925
import se.bjurr.prnfb.settings.PrnfbSettings;
2026
import se.bjurr.prnfb.settings.PrnfbSettingsData;
27+
import se.bjurr.prnfb.settings.TRIGGER_IF_MERGE;
2128
import se.bjurr.prnfb.settings.USER_LEVEL;
2229
import se.bjurr.prnfb.settings.ValidationException;
30+
import se.bjurr.prnfb.settings.legacy.AdminFormValues.BUTTON_VISIBILITY;
31+
import se.bjurr.prnfb.settings.legacy.Header;
32+
import se.bjurr.prnfb.settings.legacy.SettingsStorage;
2333

34+
import com.atlassian.bitbucket.pull.PullRequestState;
2435
import com.atlassian.bitbucket.user.SecurityService;
2536
import com.atlassian.bitbucket.util.Operation;
2637
import com.atlassian.sal.api.pluginsettings.PluginSettings;
@@ -36,6 +47,7 @@ public class SettingsService {
3647

3748
public static final String STORAGE_KEY = "se.bjurr.prnfb.pull-request-notifier-for-bitbucket-3";
3849
private static Gson gson = new Gson();
50+
private final Logger logger = LoggerFactory.getLogger(SettingsService.class);
3951
private final PluginSettings pluginSettings;
4052
private final SecurityService securityService;
4153
private final TransactionTemplate transactionTemplate;
@@ -233,6 +245,88 @@ private void doDeleteNotification(UUID uuid) {
233245
private PrnfbSettings doGetPrnfbSettings() {
234246
Object storedSettings = this.pluginSettings.get(STORAGE_KEY);
235247
if (storedSettings == null) {
248+
try {
249+
se.bjurr.prnfb.settings.legacy.PrnfbSettings oldSettings = SettingsStorage.getPrnfbSettings(this.pluginSettings);
250+
251+
String ks = oldSettings.getKeyStore().orNull();
252+
String ksp = oldSettings.getKeyStorePassword().orNull();
253+
String kst = oldSettings.getKeyStoreType();
254+
USER_LEVEL adminRestr = USER_LEVEL.SYSTEM_ADMIN;
255+
if (oldSettings.isAdminsAllowed()) {
256+
adminRestr = USER_LEVEL.ADMIN;
257+
}
258+
if (oldSettings.isUsersAllowed()) {
259+
adminRestr = USER_LEVEL.EVERYONE;
260+
}
261+
262+
boolean shouldAcceptAnyCertificate = false;
263+
264+
List<PrnfbButton> newButtons = newArrayList();
265+
for (se.bjurr.prnfb.settings.legacy.PrnfbButton oldButton : oldSettings.getButtons()) {
266+
USER_LEVEL userLevel = USER_LEVEL.SYSTEM_ADMIN;
267+
if (oldButton.getVisibility() == BUTTON_VISIBILITY.ADMIN) {
268+
userLevel = USER_LEVEL.ADMIN;
269+
}
270+
if (oldButton.getVisibility() == BUTTON_VISIBILITY.EVERYONE) {
271+
userLevel = USER_LEVEL.EVERYONE;
272+
}
273+
newButtons.add(new PrnfbButton(UUID.randomUUID(), oldButton.getTitle(), userLevel, null, null));
274+
}
275+
276+
List<PrnfbNotification> newNotifications = newArrayList();
277+
for (se.bjurr.prnfb.settings.legacy.PrnfbNotification oldNotification : oldSettings.getNotifications()) {
278+
try {
279+
PrnfbNotificationBuilder builder = prnfbNotificationBuilder()//
280+
.withFilterRegexp(oldNotification.getFilterRegexp().orNull())//
281+
.withFilterString(oldNotification.getFilterString().orNull())//
282+
.withInjectionUrl(oldNotification.getInjectionUrl().orNull())//
283+
.withInjectionUrlRegexp(oldNotification.getInjectionUrlRegexp().orNull())//
284+
.withMethod(oldNotification.getMethod())//
285+
.withName(oldNotification.getName())//
286+
.withPassword(oldNotification.getPassword().orNull())//
287+
.withPostContent(oldNotification.getPostContent().orNull())//
288+
.withProxyPassword(oldNotification.getProxyPassword().orNull())//
289+
.withProxyPort(oldNotification.getProxyPort())//
290+
.withProxyServer(oldNotification.getProxyServer().orNull())//
291+
.withProxyUser(oldNotification.getProxyUser().orNull())//
292+
.withTriggerIfCanMerge(TRIGGER_IF_MERGE.valueOf(oldNotification.getTriggerIfCanMerge().name()))//
293+
.withUrl(oldNotification.getUrl())//
294+
.withUser(oldNotification.getUser().orNull());
295+
296+
for (Header h : oldNotification.getHeaders()) {
297+
builder.withHeader(h.getName(), h.getValue());
298+
}
299+
300+
for (PullRequestState t : oldNotification.getTriggerIgnoreStateList()) {
301+
builder.withTriggerIgnoreState(t);
302+
}
303+
304+
for (PrnfbPullRequestAction t : oldNotification.getTriggers()) {
305+
builder.withTrigger(t);
306+
}
307+
308+
newNotifications.add(builder.build());
309+
} catch (ValidationException e) {
310+
this.logger.error("", e);
311+
}
312+
}
313+
314+
return prnfbSettingsBuilder()//
315+
.setPrnfbSettingsData(//
316+
prnfbSettingsDataBuilder()//
317+
.setAdminRestriction(adminRestr)//
318+
.setKeyStore(ks)//
319+
.setKeyStorePassword(ksp)//
320+
.setKeyStoreType(kst)//
321+
.setShouldAcceptAnyCertificate(shouldAcceptAnyCertificate)//
322+
.build())//
323+
.setButtons(newButtons)//
324+
.setNotifications(newNotifications)//
325+
.build();
326+
327+
} catch (se.bjurr.prnfb.settings.legacy.ValidationException e) {
328+
this.logger.error("", e);
329+
}
236330
return prnfbSettingsBuilder()//
237331
.setPrnfbSettingsData(//
238332
prnfbSettingsDataBuilder()//
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package se.bjurr.prnfb.settings.legacy;
2+
3+
import java.util.ArrayList;
4+
import java.util.Map;
5+
6+
import javax.xml.bind.annotation.XmlAccessType;
7+
import javax.xml.bind.annotation.XmlAccessorType;
8+
import javax.xml.bind.annotation.XmlRootElement;
9+
10+
@XmlRootElement
11+
@XmlAccessorType(XmlAccessType.FIELD)
12+
@Deprecated
13+
public final class AdminFormValues extends ArrayList<Map<String, String>> {
14+
public enum BUTTON_VISIBILITY {
15+
ADMIN, EVERYONE, NONE, SYSTEM_ADMIN
16+
}
17+
18+
public enum FIELDS {
19+
accept_any_certificate, //
20+
admin_allowed, //
21+
button_title, //
22+
button_visibility, //
23+
events, //
24+
filter_regexp, //
25+
filter_string, //
26+
FORM_IDENTIFIER, //
27+
FORM_TYPE, //
28+
header_name, //
29+
header_value, //
30+
injection_url, //
31+
injection_url_regexp, //
32+
key_store, //
33+
key_store_password, //
34+
key_store_type, //
35+
method, //
36+
name, //
37+
password, //
38+
post_content, //
39+
proxy_password, //
40+
proxy_port, //
41+
proxy_server, //
42+
proxy_user, //
43+
trigger_if_isconflicting, //
44+
trigger_ignore_state, //
45+
url, //
46+
user, //
47+
user_allowed;
48+
}
49+
50+
public enum FORM_TYPE {
51+
BUTTON_CONFIG_FORM, GLOBAL_SETTINGS, TRIGGER_CONFIG_FORM
52+
}
53+
54+
public enum TRIGGER_IF_MERGE {
55+
ALWAYS, CONFLICTING, NOT_CONFLICTING
56+
}
57+
58+
public static final String DEFAULT_NAME = "Unnamed trigger";;
59+
60+
public static final String NAME = "name";;
61+
62+
public static final String VALUE = "value";;
63+
64+
private static final long serialVersionUID = 9084184120202816120L;
65+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package se.bjurr.prnfb.settings.legacy;
2+
3+
import static com.google.common.base.Preconditions.checkNotNull;
4+
import static com.google.common.base.Strings.emptyToNull;
5+
import static com.google.common.base.Strings.nullToEmpty;
6+
7+
@Deprecated
8+
public class Header {
9+
10+
private final String name;
11+
private final String value;
12+
13+
public Header(String name, String value) {
14+
this.name = checkNotNull(emptyToNull(nullToEmpty(name).trim()));
15+
this.value = checkNotNull(emptyToNull(nullToEmpty(value).trim()));
16+
}
17+
18+
public String getName() {
19+
return this.name;
20+
}
21+
22+
public String getValue() {
23+
return this.value;
24+
}
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package se.bjurr.prnfb.settings.legacy;
2+
3+
import se.bjurr.prnfb.settings.legacy.AdminFormValues.BUTTON_VISIBILITY;
4+
5+
@Deprecated
6+
public class PrnfbButton {
7+
8+
private final String formIdentifier;
9+
private final String title;
10+
private final BUTTON_VISIBILITY visibility;
11+
12+
public PrnfbButton(String title, BUTTON_VISIBILITY visibility, String formIdentifier) {
13+
this.title = title;
14+
this.visibility = visibility;
15+
this.formIdentifier = formIdentifier;
16+
}
17+
18+
public String getFormIdentifier() {
19+
return this.formIdentifier;
20+
}
21+
22+
public String getTitle() {
23+
return this.title;
24+
}
25+
26+
public BUTTON_VISIBILITY getVisibility() {
27+
return this.visibility;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return "Title: " + this.title;
33+
}
34+
}

0 commit comments

Comments
 (0)