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

Commit 6378e65

Browse files
committed
Blocking KEEP_THIS_TO_LEAVE_UNCHANGED from being accidently saved
1 parent fd58b25 commit 6378e65

File tree

4 files changed

+105
-85
lines changed

4 files changed

+105
-85
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
Changelog of Pull Request Notifier for Bitbucket.
44

55
## Unreleased
6+
### No issue
7+
Blocking KEEP_THIS_TO_LEAVE_UNCHANGED from being accidently saved
8+
9+
[d3b0537c21dded1](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/d3b0537c21dded1) Tomas Bjerre *2017-07-31 05:41:23*
10+
11+
## 3.8
612
### No issue
713
doc
814

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

+73-62
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public PrnfbNotification addOrUpdateNotification(PrnfbNotification prnfbNotifica
8282
public PrnfbNotification doInTransaction() {
8383
try {
8484
return doAddOrUpdateNotification(prnfbNotification);
85-
} catch (ValidationException e) {
85+
} catch (final ValidationException e) {
8686
propagate(e);
8787
}
8888
return null;
@@ -121,7 +121,7 @@ public Optional<PrnfbNotification> findNotification(UUID notificationUuid) {
121121
}
122122

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

135135
public List<PrnfbButton> getButtons(String projectKey) {
136-
List<PrnfbButton> found = newArrayList();
137-
for (PrnfbButton candidate : getPrnfbSettings().getButtons()) {
136+
final List<PrnfbButton> found = newArrayList();
137+
for (final PrnfbButton candidate : getPrnfbSettings().getButtons()) {
138138
if (candidate.getProjectKey().isPresent()
139139
&& candidate.getProjectKey().get().equals(projectKey)) {
140140
found.add(candidate);
@@ -144,8 +144,8 @@ public List<PrnfbButton> getButtons(String projectKey) {
144144
}
145145

146146
public List<PrnfbButton> getButtons(String projectKey, String repositorySlug) {
147-
List<PrnfbButton> found = newArrayList();
148-
for (PrnfbButton candidate : getPrnfbSettings().getButtons()) {
147+
final List<PrnfbButton> found = newArrayList();
148+
for (final PrnfbButton candidate : getPrnfbSettings().getButtons()) {
149149
if (candidate.getProjectKey().isPresent()
150150
&& candidate.getProjectKey().get().equals(projectKey) //
151151
&& candidate.getRepositorySlug().isPresent()
@@ -165,8 +165,8 @@ public List<PrnfbNotification> getNotifications() {
165165
}
166166

167167
public List<PrnfbNotification> getNotifications(String projectKey) {
168-
List<PrnfbNotification> found = newArrayList();
169-
for (PrnfbNotification candidate : getPrnfbSettings().getNotifications()) {
168+
final List<PrnfbNotification> found = newArrayList();
169+
for (final PrnfbNotification candidate : getPrnfbSettings().getNotifications()) {
170170
if (candidate.getProjectKey().isPresent()
171171
&& candidate.getProjectKey().get().equals(projectKey)) {
172172
found.add(candidate);
@@ -176,8 +176,8 @@ public List<PrnfbNotification> getNotifications(String projectKey) {
176176
}
177177

178178
public List<PrnfbNotification> getNotifications(String projectKey, String repositorySlug) {
179-
List<PrnfbNotification> found = newArrayList();
180-
for (PrnfbNotification candidate : getPrnfbSettings().getNotifications()) {
179+
final List<PrnfbNotification> found = newArrayList();
180+
for (final PrnfbNotification candidate : getPrnfbSettings().getNotifications()) {
181181
if (candidate.getProjectKey().isPresent()
182182
&& candidate.getProjectKey().get().equals(projectKey) //
183183
&& candidate.getRepositorySlug().isPresent()
@@ -208,8 +208,8 @@ public void setPrnfbSettingsData(PrnfbSettingsData prnfbSettingsData) {
208208
new TransactionCallback<Void>() {
209209
@Override
210210
public Void doInTransaction() {
211-
PrnfbSettings oldSettings = doGetPrnfbSettings();
212-
PrnfbSettings newPrnfbSettings =
211+
final PrnfbSettings oldSettings = doGetPrnfbSettings();
212+
final PrnfbSettings newPrnfbSettings =
213213
prnfbSettingsBuilder(oldSettings) //
214214
.setPrnfbSettingsData(prnfbSettingsData) //
215215
.build();
@@ -224,8 +224,8 @@ private PrnfbButton doAddOrUpdateButton(PrnfbButton prnfbButton) {
224224
doDeleteButton(prnfbButton.getUuid());
225225
}
226226

227-
PrnfbSettings originalSettings = doGetPrnfbSettings();
228-
PrnfbSettings updated =
227+
final PrnfbSettings originalSettings = doGetPrnfbSettings();
228+
final PrnfbSettings updated =
229229
prnfbSettingsBuilder(originalSettings) //
230230
.withButton(prnfbButton) //
231231
.build();
@@ -236,28 +236,39 @@ private PrnfbButton doAddOrUpdateButton(PrnfbButton prnfbButton) {
236236

237237
private PrnfbNotification doAddOrUpdateNotification(PrnfbNotification newNotification)
238238
throws ValidationException {
239-
Optional<PrnfbNotification> oldNotification = findNotification(newNotification.getUuid());
239+
final UUID notificationUuid = newNotification.getUuid();
240+
241+
Optional<String> oldUser = Optional.absent();
242+
Optional<String> oldPassword = Optional.absent();
243+
Optional<String> oldProxyUser = Optional.absent();
244+
Optional<String> oldProxyPassword = Optional.absent();
245+
final Optional<PrnfbNotification> oldNotification = findNotification(notificationUuid);
246+
if (oldNotification.isPresent()) {
247+
oldUser = oldNotification.get().getUser();
248+
oldPassword = oldNotification.get().getPassword();
249+
oldProxyUser = oldNotification.get().getProxyUser();
250+
oldProxyPassword = oldNotification.get().getProxyPassword();
251+
}
252+
253+
final String user = keepIfUnchanged(newNotification.getUser(), oldUser);
254+
final String password = keepIfUnchanged(newNotification.getPassword(), oldPassword);
255+
final String proxyUser = keepIfUnchanged(newNotification.getProxyUser(), oldProxyUser);
256+
final String proxyPassword =
257+
keepIfUnchanged(newNotification.getProxyPassword(), oldProxyPassword);
258+
newNotification =
259+
prnfbNotificationBuilder(newNotification) //
260+
.withUser(user) //
261+
.withPassword(password) //
262+
.withProxyUser(proxyUser) //
263+
.withProxyPassword(proxyPassword) //
264+
.build();
265+
240266
if (oldNotification.isPresent()) {
241-
String user = keepIfUnchanged(newNotification.getUser(), oldNotification.get().getUser());
242-
String password =
243-
keepIfUnchanged(newNotification.getPassword(), oldNotification.get().getPassword());
244-
String proxyUser =
245-
keepIfUnchanged(newNotification.getProxyUser(), oldNotification.get().getProxyUser());
246-
String proxyPassword =
247-
keepIfUnchanged(
248-
newNotification.getProxyPassword(), oldNotification.get().getProxyPassword());
249-
newNotification =
250-
prnfbNotificationBuilder(newNotification) //
251-
.withUser(user) //
252-
.withPassword(password) //
253-
.withProxyUser(proxyUser) //
254-
.withProxyPassword(proxyPassword) //
255-
.build();
256-
doDeleteNotification(newNotification.getUuid());
267+
doDeleteNotification(notificationUuid);
257268
}
258269

259-
PrnfbSettings originalSettings = doGetPrnfbSettings();
260-
PrnfbSettings updated =
270+
final PrnfbSettings originalSettings = doGetPrnfbSettings();
271+
final PrnfbSettings updated =
261272
prnfbSettingsBuilder(originalSettings) //
262273
.withNotification(newNotification) //
263274
.build();
@@ -267,29 +278,29 @@ private PrnfbNotification doAddOrUpdateNotification(PrnfbNotification newNotific
267278
}
268279

269280
private String keepIfUnchanged(Optional<String> newValue, Optional<String> oldValue) {
270-
boolean isUnchanged = newValue.isPresent() && newValue.get().equals(UNCHANGED);
281+
final boolean isUnchanged = newValue.isPresent() && newValue.get().equals(UNCHANGED);
271282
if (isUnchanged) {
272283
return oldValue.orNull();
273284
}
274285
return newValue.orNull();
275286
}
276287

277288
private void doDeleteButton(UUID uuid) {
278-
PrnfbSettings originalSettings = doGetPrnfbSettings();
279-
List<PrnfbButton> keep =
289+
final PrnfbSettings originalSettings = doGetPrnfbSettings();
290+
final List<PrnfbButton> keep =
280291
newArrayList(filter(originalSettings.getButtons(), not(withUuid(uuid))));
281-
PrnfbSettings withoutDeleted =
292+
final PrnfbSettings withoutDeleted =
282293
prnfbSettingsBuilder(originalSettings) //
283294
.setButtons(keep) //
284295
.build();
285296
doSetPrnfbSettings(withoutDeleted);
286297
}
287298

288299
private void doDeleteNotification(UUID uuid) {
289-
PrnfbSettings originalSettings = doGetPrnfbSettings();
290-
List<PrnfbNotification> keep =
300+
final PrnfbSettings originalSettings = doGetPrnfbSettings();
301+
final List<PrnfbNotification> keep =
291302
newArrayList(filter(originalSettings.getNotifications(), not(withUuid(uuid))));
292-
PrnfbSettings withoutDeleted =
303+
final PrnfbSettings withoutDeleted =
293304
prnfbSettingsBuilder(originalSettings) //
294305
.setNotifications(keep) //
295306
.build();
@@ -307,12 +318,12 @@ private PrnfbSettings doGetPrnfbSettings() {
307318
!= null) {
308319
try {
309320
this.logger.info("Using legacy settings.");
310-
se.bjurr.prnfb.settings.legacy.PrnfbSettings legacySettings =
321+
final se.bjurr.prnfb.settings.legacy.PrnfbSettings legacySettings =
311322
SettingsStorage.getPrnfbSettings(this.pluginSettings);
312-
PrnfbSettings fromLegacy = settingsFromLegacy(legacySettings);
323+
final PrnfbSettings fromLegacy = settingsFromLegacy(legacySettings);
313324
doSetPrnfbSettings(fromLegacy);
314325
storedSettings = this.pluginSettings.get(STORAGE_KEY);
315-
} catch (Exception e) {
326+
} catch (final Exception e) {
316327
this.logger.error("", e);
317328
}
318329
} else {
@@ -329,23 +340,23 @@ private PrnfbSettings doGetPrnfbSettings() {
329340
}
330341

331342
private void doSetPrnfbSettings(PrnfbSettings newSettings) {
332-
PrnfbSettingsData oldSettingsData = doGetPrnfbSettings().getPrnfbSettingsData();
333-
PrnfbSettingsData newSettingsData = newSettings.getPrnfbSettingsData();
334-
String keyStorePassword =
343+
final PrnfbSettingsData oldSettingsData = doGetPrnfbSettings().getPrnfbSettingsData();
344+
final PrnfbSettingsData newSettingsData = newSettings.getPrnfbSettingsData();
345+
final String keyStorePassword =
335346
keepIfUnchanged(
336347
newSettingsData.getKeyStorePassword(), oldSettingsData.getKeyStorePassword());
337348

338-
PrnfbSettingsData adjustedSettingsData =
349+
final PrnfbSettingsData adjustedSettingsData =
339350
prnfbSettingsDataBuilder(newSettingsData) //
340351
.setKeyStorePassword(keyStorePassword) //
341352
.build();
342353

343-
PrnfbSettings adjustedSettings =
354+
final PrnfbSettings adjustedSettings =
344355
prnfbSettingsBuilder(newSettings) //
345356
.setPrnfbSettingsData(adjustedSettingsData) //
346357
.build();
347358

348-
String data = gson.toJson(adjustedSettings);
359+
final String data = gson.toJson(adjustedSettings);
349360
this.pluginSettings.put(STORAGE_KEY, data);
350361
}
351362

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

364375
private PrnfbSettings settingsFromLegacy(
365376
se.bjurr.prnfb.settings.legacy.PrnfbSettings oldSettings) {
366-
String ks = oldSettings.getKeyStore().orNull();
367-
String ksp = oldSettings.getKeyStorePassword().orNull();
368-
String kst = oldSettings.getKeyStoreType();
377+
final String ks = oldSettings.getKeyStore().orNull();
378+
final String ksp = oldSettings.getKeyStorePassword().orNull();
379+
final String kst = oldSettings.getKeyStoreType();
369380
USER_LEVEL adminRestr = USER_LEVEL.SYSTEM_ADMIN;
370381
if (oldSettings.isAdminsAllowed()) {
371382
adminRestr = USER_LEVEL.ADMIN;
@@ -374,10 +385,10 @@ private PrnfbSettings settingsFromLegacy(
374385
adminRestr = USER_LEVEL.EVERYONE;
375386
}
376387

377-
boolean shouldAcceptAnyCertificate = false;
388+
final boolean shouldAcceptAnyCertificate = false;
378389

379-
List<PrnfbButton> newButtons = newArrayList();
380-
for (se.bjurr.prnfb.settings.legacy.PrnfbButton oldButton : oldSettings.getButtons()) {
390+
final List<PrnfbButton> newButtons = newArrayList();
391+
for (final se.bjurr.prnfb.settings.legacy.PrnfbButton oldButton : oldSettings.getButtons()) {
381392
USER_LEVEL userLevel = USER_LEVEL.SYSTEM_ADMIN;
382393
if (oldButton.getVisibility() == BUTTON_VISIBILITY.ADMIN) {
383394
userLevel = USER_LEVEL.ADMIN;
@@ -397,11 +408,11 @@ private PrnfbSettings settingsFromLegacy(
397408
null));
398409
}
399410

400-
List<PrnfbNotification> newNotifications = newArrayList();
401-
for (se.bjurr.prnfb.settings.legacy.PrnfbNotification oldNotification :
411+
final List<PrnfbNotification> newNotifications = newArrayList();
412+
for (final se.bjurr.prnfb.settings.legacy.PrnfbNotification oldNotification :
402413
oldSettings.getNotifications()) {
403414
try {
404-
PrnfbNotificationBuilder builder =
415+
final PrnfbNotificationBuilder builder =
405416
prnfbNotificationBuilder() //
406417
.withFilterRegexp(oldNotification.getFilterRegexp().orNull()) //
407418
.withFilterString(oldNotification.getFilterString().orNull()) //
@@ -420,20 +431,20 @@ private PrnfbSettings settingsFromLegacy(
420431
.withUrl(oldNotification.getUrl()) //
421432
.withUser(oldNotification.getUser().orNull());
422433

423-
for (Header h : oldNotification.getHeaders()) {
434+
for (final Header h : oldNotification.getHeaders()) {
424435
builder.withHeader(h.getName(), h.getValue());
425436
}
426437

427-
for (PullRequestState t : oldNotification.getTriggerIgnoreStateList()) {
438+
for (final PullRequestState t : oldNotification.getTriggerIgnoreStateList()) {
428439
builder.withTriggerIgnoreState(t);
429440
}
430441

431-
for (PrnfbPullRequestAction t : oldNotification.getTriggers()) {
442+
for (final PrnfbPullRequestAction t : oldNotification.getTriggers()) {
432443
builder.withTrigger(t);
433444
}
434445

435446
newNotifications.add(builder.build());
436-
} catch (ValidationException e) {
447+
} catch (final ValidationException e) {
437448
this.logger.error("", e);
438449
}
439450
}

src/main/java/se/bjurr/prnfb/settings/PrnfbNotification.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static java.util.UUID.randomUUID;
99
import static java.util.regex.Pattern.compile;
1010
import static se.bjurr.prnfb.http.UrlInvoker.HTTP_METHOD.GET;
11+
import static se.bjurr.prnfb.service.PrnfbRenderer.ENCODE_FOR.NONE;
1112
import static se.bjurr.prnfb.settings.TRIGGER_IF_MERGE.ALWAYS;
1213

1314
import java.net.URL;
@@ -92,7 +93,7 @@ public PrnfbNotification(PrnfbNotificationBuilder builder) throws ValidationExce
9293
this.injectionUrl = emptyToNull(nullToEmpty(builder.getInjectionUrl()).trim());
9394
this.injectionUrlRegexp = emptyToNull(nullToEmpty(builder.getInjectionUrlRegexp()).trim());
9495
this.triggerIgnoreStateList = builder.getTriggerIgnoreStateList();
95-
this.postContentEncoding = builder.getPostContentEncoding();
96+
this.postContentEncoding = firstNonNull(builder.getPostContentEncoding(), NONE);
9697
}
9798

9899
@Override

0 commit comments

Comments
 (0)