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

Commit d43d29e

Browse files
committed
Adding header support #321
Now supporting one header.
1 parent 0a432d6 commit d43d29e

File tree

7 files changed

+118
-0
lines changed

7 files changed

+118
-0
lines changed

src/main/java/se/bjurr/prnfb/listener/PrnfbPullRequestEventListener.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import se.bjurr.prnfb.service.SettingsService;
5353
import se.bjurr.prnfb.service.VariablesContext;
5454
import se.bjurr.prnfb.service.VariablesContext.VariablesContextBuilder;
55+
import se.bjurr.prnfb.settings.PrnfbHeader;
5556
import se.bjurr.prnfb.settings.PrnfbNotification;
5657
import se.bjurr.prnfb.settings.PrnfbSettingsData;
5758
import se.bjurr.prnfb.settings.TRIGGER_IF_MERGE;
@@ -311,6 +312,13 @@ public NotificationResponse notify(
311312
.withMethod(notification.getMethod()) //
312313
.withPostContent(postContent) //
313314
.appendBasicAuth(notification);
315+
for (final PrnfbHeader header : notification.getHeaders()) {
316+
urlInvoker //
317+
.withHeader(
318+
header.getName(),
319+
renderer.render(
320+
header.getValue(), ENCODE_FOR.NONE, clientKeyStore, shouldAcceptAnyCertificate));
321+
}
314322
final HttpResponse httpResponse =
315323
createInvoker()
316324
.invoke(

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
public class NotificationDTO implements Comparable<NotificationDTO>, Restricted {
1818
private String filterRegexp;
1919
private String filterString;
20+
private List<HeaderDTO> headers;
2021
private String injectionUrl;
2122
private String injectionUrlRegexp;
2223
private String variableName;
@@ -77,6 +78,13 @@ public boolean equals(final Object obj) {
7778
} else if (!filterString.equals(other.filterString)) {
7879
return false;
7980
}
81+
if (headers == null) {
82+
if (other.headers != null) {
83+
return false;
84+
}
85+
} else if (!headers.equals(other.headers)) {
86+
return false;
87+
}
8088
if (httpVersion == null) {
8189
if (other.httpVersion != null) {
8290
return false;
@@ -240,6 +248,10 @@ public String getFilterString() {
240248
return this.filterString;
241249
}
242250

251+
public List<HeaderDTO> getHeaders() {
252+
return this.headers;
253+
}
254+
243255
public String getInjectionUrl() {
244256
return this.injectionUrl;
245257
}
@@ -332,6 +344,7 @@ public int hashCode() {
332344
int result = 1;
333345
result = prime * result + (filterRegexp == null ? 0 : filterRegexp.hashCode());
334346
result = prime * result + (filterString == null ? 0 : filterString.hashCode());
347+
result = prime * result + (headers == null ? 0 : headers.hashCode());
335348
result = prime * result + (httpVersion == null ? 0 : httpVersion.hashCode());
336349
result = prime * result + (injectionUrl == null ? 0 : injectionUrl.hashCode());
337350
result = prime * result + (injectionUrlRegexp == null ? 0 : injectionUrlRegexp.hashCode());
@@ -368,6 +381,10 @@ public void setFilterString(final String filterString) {
368381
this.filterString = filterString;
369382
}
370383

384+
public void setHeaders(final List<HeaderDTO> headers) {
385+
this.headers = headers;
386+
}
387+
371388
public void setInjectionUrl(final String injectionUrl) {
372389
this.injectionUrl = injectionUrl;
373390
}
@@ -478,6 +495,8 @@ public String toString() {
478495
+ filterRegexp
479496
+ ", filterString="
480497
+ filterString
498+
+ ", headers="
499+
+ headers
481500
+ ", injectionUrl="
482501
+ injectionUrl
483502
+ ", injectionUrlRegexp="

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class PrnfbNotification implements HasUuid, Restricted {
2626
private static final String DEFAULT_NAME = "Notification";
2727
private final String filterRegexp;
2828
private final String filterString;
29+
private final List<PrnfbHeader> headers;
2930
private final String injectionUrl;
3031
private final String injectionUrlRegexp;
3132
private final String variableName;
@@ -58,6 +59,7 @@ public PrnfbNotification(final PrnfbNotificationBuilder builder) throws Validati
5859
this.proxyServer = emptyToNull(nullToEmpty(builder.getProxyServer()).trim());
5960
this.proxySchema = emptyToNull(nullToEmpty(builder.getProxySchema()).trim());
6061
this.proxyPort = builder.getProxyPort();
62+
this.headers = checkNotNull(builder.getHeaders());
6163
this.postContent = emptyToNull(nullToEmpty(builder.getPostContent()).trim());
6264
this.method = firstNonNull(builder.getMethod(), GET);
6365
this.triggerIfCanMerge = firstNonNull(builder.getTriggerIfCanMerge(), ALWAYS);
@@ -126,6 +128,13 @@ public boolean equals(final Object obj) {
126128
} else if (!filterString.equals(other.filterString)) {
127129
return false;
128130
}
131+
if (headers == null) {
132+
if (other.headers != null) {
133+
return false;
134+
}
135+
} else if (!headers.equals(other.headers)) {
136+
return false;
137+
}
129138
if (httpVersion == null) {
130139
if (other.httpVersion != null) {
131140
return false;
@@ -289,6 +298,10 @@ public Optional<String> getFilterString() {
289298
return fromNullable(this.filterString);
290299
}
291300

301+
public List<PrnfbHeader> getHeaders() {
302+
return this.headers;
303+
}
304+
292305
public Optional<String> getInjectionUrl() {
293306
return fromNullable(this.injectionUrl);
294307
}
@@ -386,6 +399,7 @@ public int hashCode() {
386399
int result = 1;
387400
result = prime * result + (filterRegexp == null ? 0 : filterRegexp.hashCode());
388401
result = prime * result + (filterString == null ? 0 : filterString.hashCode());
402+
result = prime * result + (headers == null ? 0 : headers.hashCode());
389403
result = prime * result + (httpVersion == null ? 0 : httpVersion.hashCode());
390404
result = prime * result + (injectionUrl == null ? 0 : injectionUrl.hashCode());
391405
result = prime * result + (injectionUrlRegexp == null ? 0 : injectionUrlRegexp.hashCode());
@@ -420,6 +434,8 @@ public String toString() {
420434
+ filterRegexp
421435
+ ", filterString="
422436
+ filterString
437+
+ ", headers="
438+
+ headers
423439
+ ", injectionUrl="
424440
+ injectionUrl
425441
+ ", injectionUrlRegexp="

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static PrnfbNotificationBuilder prnfbNotificationBuilder() {
2121
public PrnfbNotificationBuilder(
2222
final String filterRegexp,
2323
final String filterString,
24+
final List<PrnfbHeader> headers,
2425
final String injectionUrl,
2526
final String injectionUrlRegexp,
2627
final String variableName,
@@ -47,6 +48,7 @@ public PrnfbNotificationBuilder(
4748
final String httpVersion) {
4849
this.filterRegexp = filterRegexp;
4950
this.filterString = filterString;
51+
this.headers = headers;
5052
this.injectionUrl = injectionUrl;
5153
this.injectionUrlRegexp = injectionUrlRegexp;
5254
this.variableName = variableName;
@@ -86,6 +88,7 @@ public static PrnfbNotificationBuilder prnfbNotificationBuilder(final PrnfbNotif
8688
b.filterString = from.getFilterString().orNull();
8789
b.method = from.getMethod();
8890
b.postContent = from.getPostContent().orNull();
91+
b.headers = from.getHeaders();
8992
b.triggerIgnoreStateList = from.getTriggerIgnoreStateList();
9093
b.proxyUser = from.getProxyUser().orNull();
9194
b.proxyPassword = from.getProxyPassword().orNull();
@@ -107,6 +110,7 @@ public static PrnfbNotificationBuilder prnfbNotificationBuilder(final PrnfbNotif
107110

108111
private String filterRegexp;
109112
private String filterString;
113+
private List<PrnfbHeader> headers = newArrayList();
110114
private String injectionUrl;
111115
private String injectionUrlRegexp;
112116
private String variableName;
@@ -152,6 +156,10 @@ public String getFilterString() {
152156
return this.filterString;
153157
}
154158

159+
public List<PrnfbHeader> getHeaders() {
160+
return this.headers;
161+
}
162+
155163
public String getInjectionUrl() {
156164
return this.injectionUrl;
157165
}
@@ -244,6 +252,11 @@ public UUID getUUID() {
244252
return this.uuid;
245253
}
246254

255+
public PrnfbNotificationBuilder setHeaders(final List<PrnfbHeader> headers) {
256+
this.headers = headers;
257+
return this;
258+
}
259+
247260
public PrnfbNotificationBuilder setTriggerIgnoreState(
248261
final List<PullRequestState> triggerIgnoreStateList) {
249262
this.triggerIgnoreStateList = triggerIgnoreStateList;
@@ -270,6 +283,11 @@ public PrnfbNotificationBuilder withFilterString(final String filterString) {
270283
return this;
271284
}
272285

286+
public PrnfbNotificationBuilder withHeader(final String name, final String value) {
287+
this.headers.add(new PrnfbHeader(name, value));
288+
return this;
289+
}
290+
273291
public PrnfbNotificationBuilder withInjectionUrl(final String injectionUrl) {
274292
this.injectionUrl = emptyToNull(injectionUrl);
275293
return this;

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package se.bjurr.prnfb.transformer;
22

3+
import static com.google.common.base.Strings.isNullOrEmpty;
34
import static com.google.common.collect.Lists.newArrayList;
45
import static se.bjurr.prnfb.settings.PrnfbNotificationBuilder.prnfbNotificationBuilder;
56
import static se.bjurr.prnfb.settings.PrnfbSettings.UNCHANGED;
67

78
import com.atlassian.bitbucket.pull.PullRequestState;
89
import java.util.List;
910
import se.bjurr.prnfb.listener.PrnfbPullRequestAction;
11+
import se.bjurr.prnfb.presentation.dto.HeaderDTO;
1012
import se.bjurr.prnfb.presentation.dto.NotificationDTO;
13+
import se.bjurr.prnfb.settings.PrnfbHeader;
1114
import se.bjurr.prnfb.settings.PrnfbNotification;
1215
import se.bjurr.prnfb.settings.ValidationException;
1316

@@ -25,6 +28,7 @@ public static NotificationDTO toNotificationDto(final PrnfbNotification from) {
2528
to.setVariableRegex(from.getVariableRegex().orNull());
2629
to.setMethod(from.getMethod());
2730
to.setName(from.getName());
31+
to.setHeaders(toHeaders(from.getHeaders()));
2832
to.setPostContent(from.getPostContent().orNull());
2933
to.setPostContentEncoding(from.getPostContentEncoding());
3034
to.setProxyPort(from.getProxyPort());
@@ -60,6 +64,7 @@ public static PrnfbNotification toPrnfbNotification(final NotificationDTO from)
6064
return prnfbNotificationBuilder() //
6165
.withFilterRegexp(from.getFilterRegexp()) //
6266
.withFilterString(from.getFilterString()) //
67+
.setHeaders(toHeaders(from)) //
6368
.withInjectionUrl(from.getInjectionUrl()) //
6469
.withInjectionUrlRegexp(from.getInjectionUrlRegexp()) //
6570
.withVariableName(from.getVariableName()) //
@@ -87,6 +92,31 @@ public static PrnfbNotification toPrnfbNotification(final NotificationDTO from)
8792
.build();
8893
}
8994

95+
private static List<HeaderDTO> toHeaders(final List<PrnfbHeader> headers) {
96+
final List<HeaderDTO> to = newArrayList();
97+
if (headers != null) {
98+
for (final PrnfbHeader h : headers) {
99+
final HeaderDTO t = new HeaderDTO();
100+
t.setName(h.getName());
101+
t.setValue(h.getValue());
102+
to.add(t);
103+
}
104+
}
105+
return to;
106+
}
107+
108+
private static List<PrnfbHeader> toHeaders(final NotificationDTO from) {
109+
final List<PrnfbHeader> to = newArrayList();
110+
if (from.getHeaders() != null) {
111+
for (final HeaderDTO headerDto : from.getHeaders()) {
112+
if (!isNullOrEmpty(headerDto.getName()) && !isNullOrEmpty(headerDto.getValue())) {
113+
to.add(new PrnfbHeader(headerDto.getName(), headerDto.getValue()));
114+
}
115+
}
116+
}
117+
return to;
118+
}
119+
90120
private static List<PrnfbPullRequestAction> toPrnfbPullRequestActions(
91121
final List<String> strings) {
92122
final List<PrnfbPullRequestAction> to = newArrayList();

src/main/resources/admin.vm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,18 @@
648648
</div>
649649
</fieldset>
650650

651+
<h4>Headers</h4>
652+
<p>Optional HTTP headers to send with URL.</p>
653+
<fieldset class="group">
654+
<legend>
655+
<span>Headers</span>
656+
</legend>
657+
<div class="field-group listfield">
658+
<input class="text text-field" type="text" name="headers[][name]">
659+
<input class="text text-field" type="text" name="headers[][value]">
660+
</div>
661+
</fieldset>
662+
651663
<div class="aui-buttons">
652664
<button class="aui-button aui-button-primary">Save</button>
653665
<button class="aui-button aui-button" name="delete">Delete</button>

src/main/resources/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,21 @@ define('plugin/prnfb/utils', [
159159
}
160160
$(formSelector).populate(data);
161161

162+
$(formSelector).find('.template').each(function(index, el) {
163+
var template = $(el).data('template');
164+
var field = $(el).data('field');
165+
var target = $(el).data('target');
166+
var emptyJson = $(el).data('empty').replace(/\'/g, '"');
167+
var empty = JSON.parse(emptyJson);
168+
var rendered = "";
169+
if (data[field]) {
170+
for (var i = 0; i < data[field].length; i++) {
171+
rendered += AJS.template.load(template).fill(data[field][i]);
172+
}
173+
}
174+
rendered += AJS.template.load(template).fill(empty);
175+
$(target).html(rendered);
176+
});
162177
}
163178

164179
function clearForm(formSelector) {

0 commit comments

Comments
 (0)