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

Commit

Permalink
Adding header support #321
Browse files Browse the repository at this point in the history
Now supporting one header.
  • Loading branch information
tomasbjerre committed May 14, 2019
1 parent 0a432d6 commit d43d29e
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import se.bjurr.prnfb.service.SettingsService;
import se.bjurr.prnfb.service.VariablesContext;
import se.bjurr.prnfb.service.VariablesContext.VariablesContextBuilder;
import se.bjurr.prnfb.settings.PrnfbHeader;
import se.bjurr.prnfb.settings.PrnfbNotification;
import se.bjurr.prnfb.settings.PrnfbSettingsData;
import se.bjurr.prnfb.settings.TRIGGER_IF_MERGE;
Expand Down Expand Up @@ -311,6 +312,13 @@ public NotificationResponse notify(
.withMethod(notification.getMethod()) //
.withPostContent(postContent) //
.appendBasicAuth(notification);
for (final PrnfbHeader header : notification.getHeaders()) {
urlInvoker //
.withHeader(
header.getName(),
renderer.render(
header.getValue(), ENCODE_FOR.NONE, clientKeyStore, shouldAcceptAnyCertificate));
}
final HttpResponse httpResponse =
createInvoker()
.invoke(
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/se/bjurr/prnfb/presentation/dto/NotificationDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class NotificationDTO implements Comparable<NotificationDTO>, Restricted {
private String filterRegexp;
private String filterString;
private List<HeaderDTO> headers;
private String injectionUrl;
private String injectionUrlRegexp;
private String variableName;
Expand Down Expand Up @@ -77,6 +78,13 @@ public boolean equals(final Object obj) {
} else if (!filterString.equals(other.filterString)) {
return false;
}
if (headers == null) {
if (other.headers != null) {
return false;
}
} else if (!headers.equals(other.headers)) {
return false;
}
if (httpVersion == null) {
if (other.httpVersion != null) {
return false;
Expand Down Expand Up @@ -240,6 +248,10 @@ public String getFilterString() {
return this.filterString;
}

public List<HeaderDTO> getHeaders() {
return this.headers;
}

public String getInjectionUrl() {
return this.injectionUrl;
}
Expand Down Expand Up @@ -332,6 +344,7 @@ public int hashCode() {
int result = 1;
result = prime * result + (filterRegexp == null ? 0 : filterRegexp.hashCode());
result = prime * result + (filterString == null ? 0 : filterString.hashCode());
result = prime * result + (headers == null ? 0 : headers.hashCode());
result = prime * result + (httpVersion == null ? 0 : httpVersion.hashCode());
result = prime * result + (injectionUrl == null ? 0 : injectionUrl.hashCode());
result = prime * result + (injectionUrlRegexp == null ? 0 : injectionUrlRegexp.hashCode());
Expand Down Expand Up @@ -368,6 +381,10 @@ public void setFilterString(final String filterString) {
this.filterString = filterString;
}

public void setHeaders(final List<HeaderDTO> headers) {
this.headers = headers;
}

public void setInjectionUrl(final String injectionUrl) {
this.injectionUrl = injectionUrl;
}
Expand Down Expand Up @@ -478,6 +495,8 @@ public String toString() {
+ filterRegexp
+ ", filterString="
+ filterString
+ ", headers="
+ headers
+ ", injectionUrl="
+ injectionUrl
+ ", injectionUrlRegexp="
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/se/bjurr/prnfb/settings/PrnfbNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class PrnfbNotification implements HasUuid, Restricted {
private static final String DEFAULT_NAME = "Notification";
private final String filterRegexp;
private final String filterString;
private final List<PrnfbHeader> headers;
private final String injectionUrl;
private final String injectionUrlRegexp;
private final String variableName;
Expand Down Expand Up @@ -58,6 +59,7 @@ public PrnfbNotification(final PrnfbNotificationBuilder builder) throws Validati
this.proxyServer = emptyToNull(nullToEmpty(builder.getProxyServer()).trim());
this.proxySchema = emptyToNull(nullToEmpty(builder.getProxySchema()).trim());
this.proxyPort = builder.getProxyPort();
this.headers = checkNotNull(builder.getHeaders());
this.postContent = emptyToNull(nullToEmpty(builder.getPostContent()).trim());
this.method = firstNonNull(builder.getMethod(), GET);
this.triggerIfCanMerge = firstNonNull(builder.getTriggerIfCanMerge(), ALWAYS);
Expand Down Expand Up @@ -126,6 +128,13 @@ public boolean equals(final Object obj) {
} else if (!filterString.equals(other.filterString)) {
return false;
}
if (headers == null) {
if (other.headers != null) {
return false;
}
} else if (!headers.equals(other.headers)) {
return false;
}
if (httpVersion == null) {
if (other.httpVersion != null) {
return false;
Expand Down Expand Up @@ -289,6 +298,10 @@ public Optional<String> getFilterString() {
return fromNullable(this.filterString);
}

public List<PrnfbHeader> getHeaders() {
return this.headers;
}

public Optional<String> getInjectionUrl() {
return fromNullable(this.injectionUrl);
}
Expand Down Expand Up @@ -386,6 +399,7 @@ public int hashCode() {
int result = 1;
result = prime * result + (filterRegexp == null ? 0 : filterRegexp.hashCode());
result = prime * result + (filterString == null ? 0 : filterString.hashCode());
result = prime * result + (headers == null ? 0 : headers.hashCode());
result = prime * result + (httpVersion == null ? 0 : httpVersion.hashCode());
result = prime * result + (injectionUrl == null ? 0 : injectionUrl.hashCode());
result = prime * result + (injectionUrlRegexp == null ? 0 : injectionUrlRegexp.hashCode());
Expand Down Expand Up @@ -420,6 +434,8 @@ public String toString() {
+ filterRegexp
+ ", filterString="
+ filterString
+ ", headers="
+ headers
+ ", injectionUrl="
+ injectionUrl
+ ", injectionUrlRegexp="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static PrnfbNotificationBuilder prnfbNotificationBuilder() {
public PrnfbNotificationBuilder(
final String filterRegexp,
final String filterString,
final List<PrnfbHeader> headers,
final String injectionUrl,
final String injectionUrlRegexp,
final String variableName,
Expand All @@ -47,6 +48,7 @@ public PrnfbNotificationBuilder(
final String httpVersion) {
this.filterRegexp = filterRegexp;
this.filterString = filterString;
this.headers = headers;
this.injectionUrl = injectionUrl;
this.injectionUrlRegexp = injectionUrlRegexp;
this.variableName = variableName;
Expand Down Expand Up @@ -86,6 +88,7 @@ public static PrnfbNotificationBuilder prnfbNotificationBuilder(final PrnfbNotif
b.filterString = from.getFilterString().orNull();
b.method = from.getMethod();
b.postContent = from.getPostContent().orNull();
b.headers = from.getHeaders();
b.triggerIgnoreStateList = from.getTriggerIgnoreStateList();
b.proxyUser = from.getProxyUser().orNull();
b.proxyPassword = from.getProxyPassword().orNull();
Expand All @@ -107,6 +110,7 @@ public static PrnfbNotificationBuilder prnfbNotificationBuilder(final PrnfbNotif

private String filterRegexp;
private String filterString;
private List<PrnfbHeader> headers = newArrayList();
private String injectionUrl;
private String injectionUrlRegexp;
private String variableName;
Expand Down Expand Up @@ -152,6 +156,10 @@ public String getFilterString() {
return this.filterString;
}

public List<PrnfbHeader> getHeaders() {
return this.headers;
}

public String getInjectionUrl() {
return this.injectionUrl;
}
Expand Down Expand Up @@ -244,6 +252,11 @@ public UUID getUUID() {
return this.uuid;
}

public PrnfbNotificationBuilder setHeaders(final List<PrnfbHeader> headers) {
this.headers = headers;
return this;
}

public PrnfbNotificationBuilder setTriggerIgnoreState(
final List<PullRequestState> triggerIgnoreStateList) {
this.triggerIgnoreStateList = triggerIgnoreStateList;
Expand All @@ -270,6 +283,11 @@ public PrnfbNotificationBuilder withFilterString(final String filterString) {
return this;
}

public PrnfbNotificationBuilder withHeader(final String name, final String value) {
this.headers.add(new PrnfbHeader(name, value));
return this;
}

public PrnfbNotificationBuilder withInjectionUrl(final String injectionUrl) {
this.injectionUrl = emptyToNull(injectionUrl);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package se.bjurr.prnfb.transformer;

import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.collect.Lists.newArrayList;
import static se.bjurr.prnfb.settings.PrnfbNotificationBuilder.prnfbNotificationBuilder;
import static se.bjurr.prnfb.settings.PrnfbSettings.UNCHANGED;

import com.atlassian.bitbucket.pull.PullRequestState;
import java.util.List;
import se.bjurr.prnfb.listener.PrnfbPullRequestAction;
import se.bjurr.prnfb.presentation.dto.HeaderDTO;
import se.bjurr.prnfb.presentation.dto.NotificationDTO;
import se.bjurr.prnfb.settings.PrnfbHeader;
import se.bjurr.prnfb.settings.PrnfbNotification;
import se.bjurr.prnfb.settings.ValidationException;

Expand All @@ -25,6 +28,7 @@ public static NotificationDTO toNotificationDto(final PrnfbNotification from) {
to.setVariableRegex(from.getVariableRegex().orNull());
to.setMethod(from.getMethod());
to.setName(from.getName());
to.setHeaders(toHeaders(from.getHeaders()));
to.setPostContent(from.getPostContent().orNull());
to.setPostContentEncoding(from.getPostContentEncoding());
to.setProxyPort(from.getProxyPort());
Expand Down Expand Up @@ -60,6 +64,7 @@ public static PrnfbNotification toPrnfbNotification(final NotificationDTO from)
return prnfbNotificationBuilder() //
.withFilterRegexp(from.getFilterRegexp()) //
.withFilterString(from.getFilterString()) //
.setHeaders(toHeaders(from)) //
.withInjectionUrl(from.getInjectionUrl()) //
.withInjectionUrlRegexp(from.getInjectionUrlRegexp()) //
.withVariableName(from.getVariableName()) //
Expand Down Expand Up @@ -87,6 +92,31 @@ public static PrnfbNotification toPrnfbNotification(final NotificationDTO from)
.build();
}

private static List<HeaderDTO> toHeaders(final List<PrnfbHeader> headers) {
final List<HeaderDTO> to = newArrayList();
if (headers != null) {
for (final PrnfbHeader h : headers) {
final HeaderDTO t = new HeaderDTO();
t.setName(h.getName());
t.setValue(h.getValue());
to.add(t);
}
}
return to;
}

private static List<PrnfbHeader> toHeaders(final NotificationDTO from) {
final List<PrnfbHeader> to = newArrayList();
if (from.getHeaders() != null) {
for (final HeaderDTO headerDto : from.getHeaders()) {
if (!isNullOrEmpty(headerDto.getName()) && !isNullOrEmpty(headerDto.getValue())) {
to.add(new PrnfbHeader(headerDto.getName(), headerDto.getValue()));
}
}
}
return to;
}

private static List<PrnfbPullRequestAction> toPrnfbPullRequestActions(
final List<String> strings) {
final List<PrnfbPullRequestAction> to = newArrayList();
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/admin.vm
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,18 @@
</div>
</fieldset>

<h4>Headers</h4>
<p>Optional HTTP headers to send with URL.</p>
<fieldset class="group">
<legend>
<span>Headers</span>
</legend>
<div class="field-group listfield">
<input class="text text-field" type="text" name="headers[][name]">
<input class="text text-field" type="text" name="headers[][value]">
</div>
</fieldset>

<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
15 changes: 15 additions & 0 deletions src/main/resources/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ define('plugin/prnfb/utils', [
}
$(formSelector).populate(data);

$(formSelector).find('.template').each(function(index, el) {
var template = $(el).data('template');
var field = $(el).data('field');
var target = $(el).data('target');
var emptyJson = $(el).data('empty').replace(/\'/g, '"');
var empty = JSON.parse(emptyJson);
var rendered = "";
if (data[field]) {
for (var i = 0; i < data[field].length; i++) {
rendered += AJS.template.load(template).fill(data[field][i]);
}
}
rendered += AJS.template.load(template).fill(empty);
$(target).html(rendered);
});
}

function clearForm(formSelector) {
Expand Down

0 comments on commit d43d29e

Please sign in to comment.