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

Commit

Permalink
Blocking KEEP_THIS_TO_LEAVE_UNCHANGED from being accidently saved
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Jul 31, 2017
1 parent 1fabd8c commit ce5274f
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 86 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
Changelog of Pull Request Notifier for Bitbucket.

## Unreleased
### No issue
Blocking KEEP_THIS_TO_LEAVE_UNCHANGED from being accidently saved

[d3b0537c21dded1](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/d3b0537c21dded1) Tomas Bjerre *2017-07-31 05:41:23*

## 3.8
### No issue
doc

[ec92968f70f4ddf](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/ec92968f70f4ddf) Tomas Bjerre *2017-07-30 19:27:54*
[a9fbe75c3fcca7d](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/a9fbe75c3fcca7d) Tomas Bjerre *2017-07-30 19:28:12*

## 3.7
### GitHub [#238](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/238) Project and Repo not shown correctly in dropboxes in global admin GUI
Expand Down
135 changes: 73 additions & 62 deletions src/main/java/se/bjurr/prnfb/service/SettingsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public PrnfbNotification addOrUpdateNotification(PrnfbNotification prnfbNotifica
public PrnfbNotification doInTransaction() {
try {
return doAddOrUpdateNotification(prnfbNotification);
} catch (ValidationException e) {
} catch (final ValidationException e) {
propagate(e);
}
return null;
Expand Down Expand Up @@ -121,7 +121,7 @@ public Optional<PrnfbNotification> findNotification(UUID notificationUuid) {
}

public PrnfbButton getButton(UUID buttionUuid) {
Optional<PrnfbButton> foundOpt = findButton(buttionUuid);
final Optional<PrnfbButton> foundOpt = findButton(buttionUuid);
if (!foundOpt.isPresent()) {
throw new RuntimeException(buttionUuid + " not fond in:\n" + on('\n').join(getButtons()));
}
Expand All @@ -133,8 +133,8 @@ public List<PrnfbButton> getButtons() {
}

public List<PrnfbButton> getButtons(String projectKey) {
List<PrnfbButton> found = newArrayList();
for (PrnfbButton candidate : getPrnfbSettings().getButtons()) {
final List<PrnfbButton> found = newArrayList();
for (final PrnfbButton candidate : getPrnfbSettings().getButtons()) {
if (candidate.getProjectKey().isPresent()
&& candidate.getProjectKey().get().equals(projectKey)) {
found.add(candidate);
Expand All @@ -144,8 +144,8 @@ public List<PrnfbButton> getButtons(String projectKey) {
}

public List<PrnfbButton> getButtons(String projectKey, String repositorySlug) {
List<PrnfbButton> found = newArrayList();
for (PrnfbButton candidate : getPrnfbSettings().getButtons()) {
final List<PrnfbButton> found = newArrayList();
for (final PrnfbButton candidate : getPrnfbSettings().getButtons()) {
if (candidate.getProjectKey().isPresent()
&& candidate.getProjectKey().get().equals(projectKey) //
&& candidate.getRepositorySlug().isPresent()
Expand All @@ -165,8 +165,8 @@ public List<PrnfbNotification> getNotifications() {
}

public List<PrnfbNotification> getNotifications(String projectKey) {
List<PrnfbNotification> found = newArrayList();
for (PrnfbNotification candidate : getPrnfbSettings().getNotifications()) {
final List<PrnfbNotification> found = newArrayList();
for (final PrnfbNotification candidate : getPrnfbSettings().getNotifications()) {
if (candidate.getProjectKey().isPresent()
&& candidate.getProjectKey().get().equals(projectKey)) {
found.add(candidate);
Expand All @@ -176,8 +176,8 @@ public List<PrnfbNotification> getNotifications(String projectKey) {
}

public List<PrnfbNotification> getNotifications(String projectKey, String repositorySlug) {
List<PrnfbNotification> found = newArrayList();
for (PrnfbNotification candidate : getPrnfbSettings().getNotifications()) {
final List<PrnfbNotification> found = newArrayList();
for (final PrnfbNotification candidate : getPrnfbSettings().getNotifications()) {
if (candidate.getProjectKey().isPresent()
&& candidate.getProjectKey().get().equals(projectKey) //
&& candidate.getRepositorySlug().isPresent()
Expand Down Expand Up @@ -208,8 +208,8 @@ public void setPrnfbSettingsData(PrnfbSettingsData prnfbSettingsData) {
new TransactionCallback<Void>() {
@Override
public Void doInTransaction() {
PrnfbSettings oldSettings = doGetPrnfbSettings();
PrnfbSettings newPrnfbSettings =
final PrnfbSettings oldSettings = doGetPrnfbSettings();
final PrnfbSettings newPrnfbSettings =
prnfbSettingsBuilder(oldSettings) //
.setPrnfbSettingsData(prnfbSettingsData) //
.build();
Expand All @@ -224,8 +224,8 @@ private PrnfbButton doAddOrUpdateButton(PrnfbButton prnfbButton) {
doDeleteButton(prnfbButton.getUuid());
}

PrnfbSettings originalSettings = doGetPrnfbSettings();
PrnfbSettings updated =
final PrnfbSettings originalSettings = doGetPrnfbSettings();
final PrnfbSettings updated =
prnfbSettingsBuilder(originalSettings) //
.withButton(prnfbButton) //
.build();
Expand All @@ -236,28 +236,39 @@ private PrnfbButton doAddOrUpdateButton(PrnfbButton prnfbButton) {

private PrnfbNotification doAddOrUpdateNotification(PrnfbNotification newNotification)
throws ValidationException {
Optional<PrnfbNotification> oldNotification = findNotification(newNotification.getUuid());
final UUID notificationUuid = newNotification.getUuid();

Optional<String> oldUser = Optional.absent();
Optional<String> oldPassword = Optional.absent();
Optional<String> oldProxyUser = Optional.absent();
Optional<String> oldProxyPassword = Optional.absent();
final Optional<PrnfbNotification> oldNotification = findNotification(notificationUuid);
if (oldNotification.isPresent()) {
oldUser = oldNotification.get().getUser();
oldPassword = oldNotification.get().getPassword();
oldProxyUser = oldNotification.get().getProxyUser();
oldProxyPassword = oldNotification.get().getProxyPassword();
}

final String user = keepIfUnchanged(newNotification.getUser(), oldUser);
final String password = keepIfUnchanged(newNotification.getPassword(), oldPassword);
final String proxyUser = keepIfUnchanged(newNotification.getProxyUser(), oldProxyUser);
final String proxyPassword =
keepIfUnchanged(newNotification.getProxyPassword(), oldProxyPassword);
newNotification =
prnfbNotificationBuilder(newNotification) //
.withUser(user) //
.withPassword(password) //
.withProxyUser(proxyUser) //
.withProxyPassword(proxyPassword) //
.build();

if (oldNotification.isPresent()) {
String user = keepIfUnchanged(newNotification.getUser(), oldNotification.get().getUser());
String password =
keepIfUnchanged(newNotification.getPassword(), oldNotification.get().getPassword());
String proxyUser =
keepIfUnchanged(newNotification.getProxyUser(), oldNotification.get().getProxyUser());
String proxyPassword =
keepIfUnchanged(
newNotification.getProxyPassword(), oldNotification.get().getProxyPassword());
newNotification =
prnfbNotificationBuilder(newNotification) //
.withUser(user) //
.withPassword(password) //
.withProxyUser(proxyUser) //
.withProxyPassword(proxyPassword) //
.build();
doDeleteNotification(newNotification.getUuid());
doDeleteNotification(notificationUuid);
}

PrnfbSettings originalSettings = doGetPrnfbSettings();
PrnfbSettings updated =
final PrnfbSettings originalSettings = doGetPrnfbSettings();
final PrnfbSettings updated =
prnfbSettingsBuilder(originalSettings) //
.withNotification(newNotification) //
.build();
Expand All @@ -267,29 +278,29 @@ private PrnfbNotification doAddOrUpdateNotification(PrnfbNotification newNotific
}

private String keepIfUnchanged(Optional<String> newValue, Optional<String> oldValue) {
boolean isUnchanged = newValue.isPresent() && newValue.get().equals(UNCHANGED);
final boolean isUnchanged = newValue.isPresent() && newValue.get().equals(UNCHANGED);
if (isUnchanged) {
return oldValue.orNull();
}
return newValue.orNull();
}

private void doDeleteButton(UUID uuid) {
PrnfbSettings originalSettings = doGetPrnfbSettings();
List<PrnfbButton> keep =
final PrnfbSettings originalSettings = doGetPrnfbSettings();
final List<PrnfbButton> keep =
newArrayList(filter(originalSettings.getButtons(), not(withUuid(uuid))));
PrnfbSettings withoutDeleted =
final PrnfbSettings withoutDeleted =
prnfbSettingsBuilder(originalSettings) //
.setButtons(keep) //
.build();
doSetPrnfbSettings(withoutDeleted);
}

private void doDeleteNotification(UUID uuid) {
PrnfbSettings originalSettings = doGetPrnfbSettings();
List<PrnfbNotification> keep =
final PrnfbSettings originalSettings = doGetPrnfbSettings();
final List<PrnfbNotification> keep =
newArrayList(filter(originalSettings.getNotifications(), not(withUuid(uuid))));
PrnfbSettings withoutDeleted =
final PrnfbSettings withoutDeleted =
prnfbSettingsBuilder(originalSettings) //
.setNotifications(keep) //
.build();
Expand All @@ -307,12 +318,12 @@ private PrnfbSettings doGetPrnfbSettings() {
!= null) {
try {
this.logger.info("Using legacy settings.");
se.bjurr.prnfb.settings.legacy.PrnfbSettings legacySettings =
final se.bjurr.prnfb.settings.legacy.PrnfbSettings legacySettings =
SettingsStorage.getPrnfbSettings(this.pluginSettings);
PrnfbSettings fromLegacy = settingsFromLegacy(legacySettings);
final PrnfbSettings fromLegacy = settingsFromLegacy(legacySettings);
doSetPrnfbSettings(fromLegacy);
storedSettings = this.pluginSettings.get(STORAGE_KEY);
} catch (Exception e) {
} catch (final Exception e) {
this.logger.error("", e);
}
} else {
Expand All @@ -329,23 +340,23 @@ private PrnfbSettings doGetPrnfbSettings() {
}

private void doSetPrnfbSettings(PrnfbSettings newSettings) {
PrnfbSettingsData oldSettingsData = doGetPrnfbSettings().getPrnfbSettingsData();
PrnfbSettingsData newSettingsData = newSettings.getPrnfbSettingsData();
String keyStorePassword =
final PrnfbSettingsData oldSettingsData = doGetPrnfbSettings().getPrnfbSettingsData();
final PrnfbSettingsData newSettingsData = newSettings.getPrnfbSettingsData();
final String keyStorePassword =
keepIfUnchanged(
newSettingsData.getKeyStorePassword(), oldSettingsData.getKeyStorePassword());

PrnfbSettingsData adjustedSettingsData =
final PrnfbSettingsData adjustedSettingsData =
prnfbSettingsDataBuilder(newSettingsData) //
.setKeyStorePassword(keyStorePassword) //
.build();

PrnfbSettings adjustedSettings =
final PrnfbSettings adjustedSettings =
prnfbSettingsBuilder(newSettings) //
.setPrnfbSettingsData(adjustedSettingsData) //
.build();

String data = gson.toJson(adjustedSettings);
final String data = gson.toJson(adjustedSettings);
this.pluginSettings.put(STORAGE_KEY, data);
}

Expand All @@ -363,9 +374,9 @@ public T perform() throws RuntimeException {

private PrnfbSettings settingsFromLegacy(
se.bjurr.prnfb.settings.legacy.PrnfbSettings oldSettings) {
String ks = oldSettings.getKeyStore().orNull();
String ksp = oldSettings.getKeyStorePassword().orNull();
String kst = oldSettings.getKeyStoreType();
final String ks = oldSettings.getKeyStore().orNull();
final String ksp = oldSettings.getKeyStorePassword().orNull();
final String kst = oldSettings.getKeyStoreType();
USER_LEVEL adminRestr = USER_LEVEL.SYSTEM_ADMIN;
if (oldSettings.isAdminsAllowed()) {
adminRestr = USER_LEVEL.ADMIN;
Expand All @@ -374,10 +385,10 @@ private PrnfbSettings settingsFromLegacy(
adminRestr = USER_LEVEL.EVERYONE;
}

boolean shouldAcceptAnyCertificate = false;
final boolean shouldAcceptAnyCertificate = false;

List<PrnfbButton> newButtons = newArrayList();
for (se.bjurr.prnfb.settings.legacy.PrnfbButton oldButton : oldSettings.getButtons()) {
final List<PrnfbButton> newButtons = newArrayList();
for (final se.bjurr.prnfb.settings.legacy.PrnfbButton oldButton : oldSettings.getButtons()) {
USER_LEVEL userLevel = USER_LEVEL.SYSTEM_ADMIN;
if (oldButton.getVisibility() == BUTTON_VISIBILITY.ADMIN) {
userLevel = USER_LEVEL.ADMIN;
Expand All @@ -397,11 +408,11 @@ private PrnfbSettings settingsFromLegacy(
null));
}

List<PrnfbNotification> newNotifications = newArrayList();
for (se.bjurr.prnfb.settings.legacy.PrnfbNotification oldNotification :
final List<PrnfbNotification> newNotifications = newArrayList();
for (final se.bjurr.prnfb.settings.legacy.PrnfbNotification oldNotification :
oldSettings.getNotifications()) {
try {
PrnfbNotificationBuilder builder =
final PrnfbNotificationBuilder builder =
prnfbNotificationBuilder() //
.withFilterRegexp(oldNotification.getFilterRegexp().orNull()) //
.withFilterString(oldNotification.getFilterString().orNull()) //
Expand All @@ -420,20 +431,20 @@ private PrnfbSettings settingsFromLegacy(
.withUrl(oldNotification.getUrl()) //
.withUser(oldNotification.getUser().orNull());

for (Header h : oldNotification.getHeaders()) {
for (final Header h : oldNotification.getHeaders()) {
builder.withHeader(h.getName(), h.getValue());
}

for (PullRequestState t : oldNotification.getTriggerIgnoreStateList()) {
for (final PullRequestState t : oldNotification.getTriggerIgnoreStateList()) {
builder.withTriggerIgnoreState(t);
}

for (PrnfbPullRequestAction t : oldNotification.getTriggers()) {
for (final PrnfbPullRequestAction t : oldNotification.getTriggers()) {
builder.withTrigger(t);
}

newNotifications.add(builder.build());
} catch (ValidationException e) {
} catch (final ValidationException e) {
this.logger.error("", e);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/se/bjurr/prnfb/settings/PrnfbNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static java.util.UUID.randomUUID;
import static java.util.regex.Pattern.compile;
import static se.bjurr.prnfb.http.UrlInvoker.HTTP_METHOD.GET;
import static se.bjurr.prnfb.service.PrnfbRenderer.ENCODE_FOR.NONE;
import static se.bjurr.prnfb.settings.TRIGGER_IF_MERGE.ALWAYS;

import java.net.URL;
Expand Down Expand Up @@ -92,7 +93,7 @@ public PrnfbNotification(PrnfbNotificationBuilder builder) throws ValidationExce
this.injectionUrl = emptyToNull(nullToEmpty(builder.getInjectionUrl()).trim());
this.injectionUrlRegexp = emptyToNull(nullToEmpty(builder.getInjectionUrlRegexp()).trim());
this.triggerIgnoreStateList = builder.getTriggerIgnoreStateList();
this.postContentEncoding = builder.getPostContentEncoding();
this.postContentEncoding = firstNonNull(builder.getPostContentEncoding(), NONE);
}

@Override
Expand Down
Loading

0 comments on commit ce5274f

Please sign in to comment.