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

Commit

Permalink
Add optional Redirect URL to button configuration (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
trentrand authored and tomasbjerre committed Nov 13, 2018
1 parent 201547e commit 6f6964c
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Changelog of Pull Request Notifier for Bitbucket.

## Unreleased
### GitHub [#308](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/pull/308) Add optional Redirect URL to button configuration
Add optional Redirect URL to button configuration ()

[792a9b6f754a5d0](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/792a9b6f754a5d0) Trent Rand *2018-11-13 18:11:42*

## 3.23
### GitHub [#303](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/303) Post content encoding issue for single quote
Adding option to encode variables for JSON
Expand Down
38 changes: 21 additions & 17 deletions src/main/java/se/bjurr/prnfb/presentation/ButtonServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ public Response get(
final List<PrnfbButton> buttons = buttonsService.getButtons(repositoryId, pullRequestId);
final List<ButtonDTO> dtos = toButtonDtoList(buttons);
Collections.sort(dtos);

populateButtonFormDtoList(repositoryId, pullRequestId, dtos);

for (final ButtonDTO dto : dtos) {
renderButtonDtoList(repositoryId, pullRequestId, dto);
}
return ok(dtos, APPLICATION_JSON).build();
}

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

private void populateButtonFormDtoList(
Integer repositoryId, Long pullRequestId, List<ButtonDTO> dtos) {
for (final ButtonDTO dto : dtos) {
final PrnfbRendererWrapper renderer =
buttonsService.getRenderer(repositoryId, pullRequestId, dto.getUuid());
final List<ButtonFormElementDTO> buttonFormDtoList = dto.getButtonFormList();
if (buttonFormDtoList != null) {
for (final ButtonFormElementDTO buttonFormElementDto : buttonFormDtoList) {
final String defaultValue = buttonFormElementDto.getDefaultValue();
if (!isNullOrEmpty(defaultValue)) {
final String defaultValueRendered = renderer.render(defaultValue, ENCODE_FOR.NONE);
buttonFormElementDto.setDefaultValue(defaultValueRendered);
}
private void renderButtonDtoList(Integer repositoryId, Long pullRequestId, ButtonDTO dto) {
final PrnfbRendererWrapper renderer =
buttonsService.getRenderer(repositoryId, pullRequestId, dto.getUuid());

final List<ButtonFormElementDTO> buttonFormDtoList = dto.getButtonFormList();
if (buttonFormDtoList != null) {
for (final ButtonFormElementDTO buttonFormElementDto : buttonFormDtoList) {
final String defaultValue = buttonFormElementDto.getDefaultValue();
if (!isNullOrEmpty(defaultValue)) {
final String defaultValueRendered = renderer.render(defaultValue, ENCODE_FOR.NONE);
buttonFormElementDto.setDefaultValue(defaultValueRendered);
}
dto.setButtonFormList(buttonFormDtoList);
}
dto.setButtonFormList(buttonFormDtoList);
}

final String redirectUrl = dto.getRedirectUrl();
if (!isNullOrEmpty(redirectUrl)) {
final String redirectUrlRendered = renderer.render(redirectUrl, ENCODE_FOR.HTML);
dto.setRedirectUrl(redirectUrlRendered);
}
}
}
19 changes: 19 additions & 0 deletions src/main/java/se/bjurr/prnfb/presentation/dto/ButtonDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class ButtonDTO implements Comparable<ButtonDTO>, Restricted {
private USER_LEVEL userLevel;
private UUID uuid;
private String confirmationText;
private String redirectUrl;

public void setConfirmationText(String confirmationText) {
this.confirmationText = confirmationText;
Expand Down Expand Up @@ -115,6 +116,13 @@ public boolean equals(Object obj) {
} else if (!uuid.equals(other.uuid)) {
return false;
}
if (redirectUrl == null) {
if (other.redirectUrl != null) {
return false;
}
} else if (!redirectUrl.equals(other.redirectUrl)) {
return false;
}
return true;
}

Expand Down Expand Up @@ -152,6 +160,10 @@ public UUID getUUID() {
return this.uuid;
}

public String getRedirectUrl() {
return this.redirectUrl;
}

@Override
public int hashCode() {
final int prime = 31;
Expand All @@ -165,6 +177,7 @@ public int hashCode() {
result = prime * result + (repositorySlug == null ? 0 : repositorySlug.hashCode());
result = prime * result + (userLevel == null ? 0 : userLevel.hashCode());
result = prime * result + (uuid == null ? 0 : uuid.hashCode());
result = prime * result + (redirectUrl == null ? 0 : redirectUrl.hashCode());
return result;
}

Expand Down Expand Up @@ -204,6 +217,10 @@ public void setUuid(UUID uuid) {
this.uuid = uuid;
}

public void setRedirectUrl(String redirectUrl) {
this.redirectUrl = redirectUrl;
}

@Override
public String toString() {
return "ButtonDTO [buttonFormList="
Expand All @@ -224,6 +241,8 @@ public String toString() {
+ uuid
+ ", confirmationText="
+ confirmationText
+ ", redirectUrl="
+ redirectUrl
+ "]";
}
}
17 changes: 17 additions & 0 deletions src/main/java/se/bjurr/prnfb/settings/PrnfbButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class PrnfbButton implements HasUuid, Restricted {
private final USER_LEVEL userLevel;
private final UUID uuid;
private final String confirmationText;
private final String redirectUrl;

public PrnfbButton(
UUID uuid,
Expand All @@ -30,6 +31,7 @@ public PrnfbButton(
String projectKey,
String repositorySlug,
String confirmationText,
String redirectUrl,
List<PrnfbButtonFormElement> buttonFormElementList) {
this.uuid = firstNonNull(uuid, randomUUID());
this.name = name;
Expand All @@ -38,6 +40,7 @@ public PrnfbButton(
this.repositorySlug = emptyToNull(repositorySlug);
this.projectKey = emptyToNull(projectKey);
this.confirmationText = emptyToNull(confirmationText);
this.redirectUrl = emptyToNull(redirectUrl);
this.buttonFormElementList =
firstNonNull(buttonFormElementList, new ArrayList<PrnfbButtonFormElement>());
}
Expand Down Expand Up @@ -77,6 +80,10 @@ public UUID getUuid() {
return this.uuid;
}

public String getRedirectUrl() {
return redirectUrl;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down Expand Up @@ -137,6 +144,13 @@ public boolean equals(Object obj) {
} else if (!uuid.equals(other.uuid)) {
return false;
}
if (redirectUrl == null) {
if (other.redirectUrl != null) {
return false;
}
} else if (!redirectUrl.equals(other.redirectUrl)) {
return false;
}
return true;
}

Expand All @@ -153,6 +167,7 @@ public int hashCode() {
result = prime * result + (repositorySlug == null ? 0 : repositorySlug.hashCode());
result = prime * result + (userLevel == null ? 0 : userLevel.hashCode());
result = prime * result + (uuid == null ? 0 : uuid.hashCode());
result = prime * result + (redirectUrl == null ? 0 : redirectUrl.hashCode());
return result;
}

Expand All @@ -174,6 +189,8 @@ public String toString() {
+ uuid
+ ", confirmationText="
+ confirmationText
+ ", redirectUrl="
+ redirectUrl
+ "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static ButtonDTO toButtonDto(PrnfbButton from) {
to.setName(from.getName());
to.setUserLevel(from.getUserLevel());
to.setUuid(from.getUuid());
to.setRedirectUrl(from.getRedirectUrl());
to.setProjectKey(from.getProjectKey().orNull());
to.setRepositorySlug(from.getRepositorySlug().orNull());
to.setConfirmation(from.getConfirmation());
Expand Down Expand Up @@ -110,6 +111,7 @@ public static PrnfbButton toPrnfbButton(ButtonDTO buttonDto) {
buttonDto.getProjectKey().orNull(), //
buttonDto.getRepositorySlug().orNull(), //
buttonDto.getConfirmationText(), //
buttonDto.getRedirectUrl(), //
buttonFormElement); //
}

Expand Down
7 changes: 6 additions & 1 deletion src/main/resources/admin.vm
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@
<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>
</div>

<div class="field-group">
<label>Redirect URL</label>
<input class="text long-field" type="text" name="redirectUrl"></input>
<div class="description">Optional. If not empty, a redirect will be performed after the notification's URL invocation has resolved.</div>
</div>

<div class="aui-buttons">
<button class="aui-button aui-button-primary">Save</button>
<button class="aui-button aui-button" name="delete">Delete</button>
Expand Down Expand Up @@ -647,7 +653,6 @@
<button class="aui-button aui-button-primary">Save</button>
<button class="aui-button aui-button" name="delete">Delete</button>
</div>
</fieldset>
</form>
</div>

Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/pr-triggerbutton.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,17 @@ define('plugin/prnfb/pr-triggerbutton', [
});
}
});

if (item.redirectUrl) {
redirect();
}
};

var redirect = function() {
disableButton();
window.location.replace(item.redirectUrl);
}

if (item.confirmationText || item.buttonFormList && item.buttonFormList.length > 0) {
// Create the form and dialog
var confirmationText = confirmationTextTemplate(item.confirmationText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private ButtonDTO createButton() {
button.setConfirmation(ON_OR_OFF.off);
button.setProjectKey("p1");
button.setRepositorySlug("r1");
button.setRedirectUrl("http://www.example.com/");
return button;
}

Expand All @@ -105,6 +106,7 @@ private PrnfbButton createPrnfbButton(ButtonDTO button) {
"p1",
"r1",
button.getConfirmationText(),
button.getRedirectUrl(),
new ArrayList<>());
return prnfbButton;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ private void testVisibilityOnRepository(
buttonProjectKey,
buttonRepositorySlug,
"confirmationText",
null,
null);
when(this.repository.getProject()).thenReturn(this.project);
when(this.repository.getProject().getKey()) //
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/se/bjurr/prnfb/service/SettingsServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public <T> T execute(TransactionCallback<T> action) {
public void testThatButtonCanBeAddedUpdatedAndDeleted() {
final PrnfbButton button1 =
new PrnfbButton(
null, "title", EVERYONE, ON_OR_OFF.off, "p1", "r1", "confirmationText", null);
null, "title", EVERYONE, ON_OR_OFF.off, "p1", "r1", "confirmationText", null, null);
assertThat(this.sut.getButtons()) //
.isEmpty();

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

final PrnfbButton button2 =
new PrnfbButton(
null, "title", EVERYONE, ON_OR_OFF.off, "p1", "r1", "confirmationText", null);
null, "title", EVERYONE, ON_OR_OFF.off, "p1", "r1", "confirmationText", null, null);
this.sut.addOrUpdateButton(button2);
assertThat(this.sut.getButtons()) //
.containsExactly(button1, button2);
Expand All @@ -95,6 +95,7 @@ public void testThatButtonCanBeAddedUpdatedAndDeleted() {
"p1",
"r1",
"confirmationText",
null,
null);
this.sut.addOrUpdateButton(updated);
assertThat(this.sut.getButtons()) //
Expand Down

0 comments on commit 6f6964c

Please sign in to comment.