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

Commit 6f6964c

Browse files
trentrandtomasbjerre
authored andcommitted
Add optional Redirect URL to button configuration (#308)
1 parent 201547e commit 6f6964c

File tree

10 files changed

+86
-20
lines changed

10 files changed

+86
-20
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
Changelog of Pull Request Notifier for Bitbucket.
44

5+
## Unreleased
6+
### GitHub [#308](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/pull/308) Add optional Redirect URL to button configuration
7+
Add optional Redirect URL to button configuration ()
8+
9+
[792a9b6f754a5d0](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/792a9b6f754a5d0) Trent Rand *2018-11-13 18:11:42*
10+
511
## 3.23
612
### GitHub [#303](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/303) Post content encoding issue for single quote
713
Adding option to encode variables for JSON

src/main/java/se/bjurr/prnfb/presentation/ButtonServlet.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ public Response get(
150150
final List<PrnfbButton> buttons = buttonsService.getButtons(repositoryId, pullRequestId);
151151
final List<ButtonDTO> dtos = toButtonDtoList(buttons);
152152
Collections.sort(dtos);
153-
154-
populateButtonFormDtoList(repositoryId, pullRequestId, dtos);
155-
153+
for (final ButtonDTO dto : dtos) {
154+
renderButtonDtoList(repositoryId, pullRequestId, dto);
155+
}
156156
return ok(dtos, APPLICATION_JSON).build();
157157
}
158158

@@ -179,22 +179,26 @@ public Response press(
179179
return ok(dto, APPLICATION_JSON).build();
180180
}
181181

182-
private void populateButtonFormDtoList(
183-
Integer repositoryId, Long pullRequestId, List<ButtonDTO> dtos) {
184-
for (final ButtonDTO dto : dtos) {
185-
final PrnfbRendererWrapper renderer =
186-
buttonsService.getRenderer(repositoryId, pullRequestId, dto.getUuid());
187-
final List<ButtonFormElementDTO> buttonFormDtoList = dto.getButtonFormList();
188-
if (buttonFormDtoList != null) {
189-
for (final ButtonFormElementDTO buttonFormElementDto : buttonFormDtoList) {
190-
final String defaultValue = buttonFormElementDto.getDefaultValue();
191-
if (!isNullOrEmpty(defaultValue)) {
192-
final String defaultValueRendered = renderer.render(defaultValue, ENCODE_FOR.NONE);
193-
buttonFormElementDto.setDefaultValue(defaultValueRendered);
194-
}
182+
private void renderButtonDtoList(Integer repositoryId, Long pullRequestId, ButtonDTO dto) {
183+
final PrnfbRendererWrapper renderer =
184+
buttonsService.getRenderer(repositoryId, pullRequestId, dto.getUuid());
185+
186+
final List<ButtonFormElementDTO> buttonFormDtoList = dto.getButtonFormList();
187+
if (buttonFormDtoList != null) {
188+
for (final ButtonFormElementDTO buttonFormElementDto : buttonFormDtoList) {
189+
final String defaultValue = buttonFormElementDto.getDefaultValue();
190+
if (!isNullOrEmpty(defaultValue)) {
191+
final String defaultValueRendered = renderer.render(defaultValue, ENCODE_FOR.NONE);
192+
buttonFormElementDto.setDefaultValue(defaultValueRendered);
195193
}
196-
dto.setButtonFormList(buttonFormDtoList);
197194
}
195+
dto.setButtonFormList(buttonFormDtoList);
196+
}
197+
198+
final String redirectUrl = dto.getRedirectUrl();
199+
if (!isNullOrEmpty(redirectUrl)) {
200+
final String redirectUrlRendered = renderer.render(redirectUrl, ENCODE_FOR.HTML);
201+
dto.setRedirectUrl(redirectUrlRendered);
198202
}
199203
}
200204
}

src/main/java/se/bjurr/prnfb/presentation/dto/ButtonDTO.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class ButtonDTO implements Comparable<ButtonDTO>, Restricted {
3434
private USER_LEVEL userLevel;
3535
private UUID uuid;
3636
private String confirmationText;
37+
private String redirectUrl;
3738

3839
public void setConfirmationText(String confirmationText) {
3940
this.confirmationText = confirmationText;
@@ -115,6 +116,13 @@ public boolean equals(Object obj) {
115116
} else if (!uuid.equals(other.uuid)) {
116117
return false;
117118
}
119+
if (redirectUrl == null) {
120+
if (other.redirectUrl != null) {
121+
return false;
122+
}
123+
} else if (!redirectUrl.equals(other.redirectUrl)) {
124+
return false;
125+
}
118126
return true;
119127
}
120128

@@ -152,6 +160,10 @@ public UUID getUUID() {
152160
return this.uuid;
153161
}
154162

163+
public String getRedirectUrl() {
164+
return this.redirectUrl;
165+
}
166+
155167
@Override
156168
public int hashCode() {
157169
final int prime = 31;
@@ -165,6 +177,7 @@ public int hashCode() {
165177
result = prime * result + (repositorySlug == null ? 0 : repositorySlug.hashCode());
166178
result = prime * result + (userLevel == null ? 0 : userLevel.hashCode());
167179
result = prime * result + (uuid == null ? 0 : uuid.hashCode());
180+
result = prime * result + (redirectUrl == null ? 0 : redirectUrl.hashCode());
168181
return result;
169182
}
170183

@@ -204,6 +217,10 @@ public void setUuid(UUID uuid) {
204217
this.uuid = uuid;
205218
}
206219

220+
public void setRedirectUrl(String redirectUrl) {
221+
this.redirectUrl = redirectUrl;
222+
}
223+
207224
@Override
208225
public String toString() {
209226
return "ButtonDTO [buttonFormList="
@@ -224,6 +241,8 @@ public String toString() {
224241
+ uuid
225242
+ ", confirmationText="
226243
+ confirmationText
244+
+ ", redirectUrl="
245+
+ redirectUrl
227246
+ "]";
228247
}
229248
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class PrnfbButton implements HasUuid, Restricted {
2121
private final USER_LEVEL userLevel;
2222
private final UUID uuid;
2323
private final String confirmationText;
24+
private final String redirectUrl;
2425

2526
public PrnfbButton(
2627
UUID uuid,
@@ -30,6 +31,7 @@ public PrnfbButton(
3031
String projectKey,
3132
String repositorySlug,
3233
String confirmationText,
34+
String redirectUrl,
3335
List<PrnfbButtonFormElement> buttonFormElementList) {
3436
this.uuid = firstNonNull(uuid, randomUUID());
3537
this.name = name;
@@ -38,6 +40,7 @@ public PrnfbButton(
3840
this.repositorySlug = emptyToNull(repositorySlug);
3941
this.projectKey = emptyToNull(projectKey);
4042
this.confirmationText = emptyToNull(confirmationText);
43+
this.redirectUrl = emptyToNull(redirectUrl);
4144
this.buttonFormElementList =
4245
firstNonNull(buttonFormElementList, new ArrayList<PrnfbButtonFormElement>());
4346
}
@@ -77,6 +80,10 @@ public UUID getUuid() {
7780
return this.uuid;
7881
}
7982

83+
public String getRedirectUrl() {
84+
return redirectUrl;
85+
}
86+
8087
@Override
8188
public boolean equals(Object obj) {
8289
if (this == obj) {
@@ -137,6 +144,13 @@ public boolean equals(Object obj) {
137144
} else if (!uuid.equals(other.uuid)) {
138145
return false;
139146
}
147+
if (redirectUrl == null) {
148+
if (other.redirectUrl != null) {
149+
return false;
150+
}
151+
} else if (!redirectUrl.equals(other.redirectUrl)) {
152+
return false;
153+
}
140154
return true;
141155
}
142156

@@ -153,6 +167,7 @@ public int hashCode() {
153167
result = prime * result + (repositorySlug == null ? 0 : repositorySlug.hashCode());
154168
result = prime * result + (userLevel == null ? 0 : userLevel.hashCode());
155169
result = prime * result + (uuid == null ? 0 : uuid.hashCode());
170+
result = prime * result + (redirectUrl == null ? 0 : redirectUrl.hashCode());
156171
return result;
157172
}
158173

@@ -174,6 +189,8 @@ public String toString() {
174189
+ uuid
175190
+ ", confirmationText="
176191
+ confirmationText
192+
+ ", redirectUrl="
193+
+ redirectUrl
177194
+ "]";
178195
}
179196
}

src/main/java/se/bjurr/prnfb/transformer/ButtonTransformer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static ButtonDTO toButtonDto(PrnfbButton from) {
3434
to.setName(from.getName());
3535
to.setUserLevel(from.getUserLevel());
3636
to.setUuid(from.getUuid());
37+
to.setRedirectUrl(from.getRedirectUrl());
3738
to.setProjectKey(from.getProjectKey().orNull());
3839
to.setRepositorySlug(from.getRepositorySlug().orNull());
3940
to.setConfirmation(from.getConfirmation());
@@ -110,6 +111,7 @@ public static PrnfbButton toPrnfbButton(ButtonDTO buttonDto) {
110111
buttonDto.getProjectKey().orNull(), //
111112
buttonDto.getRepositorySlug().orNull(), //
112113
buttonDto.getConfirmationText(), //
114+
buttonDto.getRedirectUrl(), //
113115
buttonFormElement); //
114116
}
115117

src/main/resources/admin.vm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@
292292
<div class="description">Optional. If not empty, a form will be rendered from this JSON. This is documented <a href="https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket" target="_blank">here</a>.</div>
293293
</div>
294294

295+
<div class="field-group">
296+
<label>Redirect URL</label>
297+
<input class="text long-field" type="text" name="redirectUrl"></input>
298+
<div class="description">Optional. If not empty, a redirect will be performed after the notification's URL invocation has resolved.</div>
299+
</div>
300+
295301
<div class="aui-buttons">
296302
<button class="aui-button aui-button-primary">Save</button>
297303
<button class="aui-button aui-button" name="delete">Delete</button>
@@ -647,7 +653,6 @@
647653
<button class="aui-button aui-button-primary">Save</button>
648654
<button class="aui-button aui-button" name="delete">Delete</button>
649655
</div>
650-
</fieldset>
651656
</form>
652657
</div>
653658

src/main/resources/pr-triggerbutton.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,17 @@ define('plugin/prnfb/pr-triggerbutton', [
249249
});
250250
}
251251
});
252+
253+
if (item.redirectUrl) {
254+
redirect();
255+
}
252256
};
253257

258+
var redirect = function() {
259+
disableButton();
260+
window.location.replace(item.redirectUrl);
261+
}
262+
254263
if (item.confirmationText || item.buttonFormList && item.buttonFormList.length > 0) {
255264
// Create the form and dialog
256265
var confirmationText = confirmationTextTemplate(item.confirmationText);

src/test/java/se/bjurr/prnfb/presentation/ButtonServletTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private ButtonDTO createButton() {
9292
button.setConfirmation(ON_OR_OFF.off);
9393
button.setProjectKey("p1");
9494
button.setRepositorySlug("r1");
95+
button.setRedirectUrl("http://www.example.com/");
9596
return button;
9697
}
9798

@@ -105,6 +106,7 @@ private PrnfbButton createPrnfbButton(ButtonDTO button) {
105106
"p1",
106107
"r1",
107108
button.getConfirmationText(),
109+
button.getRedirectUrl(),
108110
new ArrayList<>());
109111
return prnfbButton;
110112
}

src/test/java/se/bjurr/prnfb/service/ButtonsServiceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ private void testVisibilityOnRepository(
321321
buttonProjectKey,
322322
buttonRepositorySlug,
323323
"confirmationText",
324+
null,
324325
null);
325326
when(this.repository.getProject()).thenReturn(this.project);
326327
when(this.repository.getProject().getKey()) //

src/test/java/se/bjurr/prnfb/service/SettingsServiceTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public <T> T execute(TransactionCallback<T> action) {
7171
public void testThatButtonCanBeAddedUpdatedAndDeleted() {
7272
final PrnfbButton button1 =
7373
new PrnfbButton(
74-
null, "title", EVERYONE, ON_OR_OFF.off, "p1", "r1", "confirmationText", null);
74+
null, "title", EVERYONE, ON_OR_OFF.off, "p1", "r1", "confirmationText", null, null);
7575
assertThat(this.sut.getButtons()) //
7676
.isEmpty();
7777

@@ -81,7 +81,7 @@ public void testThatButtonCanBeAddedUpdatedAndDeleted() {
8181

8282
final PrnfbButton button2 =
8383
new PrnfbButton(
84-
null, "title", EVERYONE, ON_OR_OFF.off, "p1", "r1", "confirmationText", null);
84+
null, "title", EVERYONE, ON_OR_OFF.off, "p1", "r1", "confirmationText", null, null);
8585
this.sut.addOrUpdateButton(button2);
8686
assertThat(this.sut.getButtons()) //
8787
.containsExactly(button1, button2);
@@ -95,6 +95,7 @@ public void testThatButtonCanBeAddedUpdatedAndDeleted() {
9595
"p1",
9696
"r1",
9797
"confirmationText",
98+
null,
9899
null);
99100
this.sut.addOrUpdateButton(updated);
100101
assertThat(this.sut.getButtons()) //

0 commit comments

Comments
 (0)