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

Commit c8cb7b1

Browse files
agentmerlintomasbjerre
authored andcommitted
Add new variable for storing a regex match on another variable value. #333 (#334)
1 parent ab5e796 commit c8cb7b1

File tree

9 files changed

+237
-8
lines changed

9 files changed

+237
-8
lines changed

CHANGELOG.md

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

33
Changelog of Pull Request Notifier for Bitbucket.
44

5+
## Unreleased
6+
### GitHub [#1](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/1) Finish GUI
7+
Add link to bitbucket debug logging doc () ()
8+
9+
Bitbucket makes it easy to add debug logging for specific loggers once you know what to look for.
10+
11+
[c5eac08e7f6dd49](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/c5eac08e7f6dd49) Dave Neeley *2019-03-29 17:13:31*
12+
13+
### GitHub [#316](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/pull/316) Update README_jenkins.md
14+
Update README_jenkins.md ()
15+
16+
[a66210fb34fbc46](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/a66210fb34fbc46) Anton Kazakov *2019-01-20 15:37:19*
17+
18+
### GitHub [#325](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/pull/325) Add link to bitbucket debug logging doc
19+
Add link to bitbucket debug logging doc () ()
20+
21+
Bitbucket makes it easy to add debug logging for specific loggers once you know what to look for.
22+
23+
[c5eac08e7f6dd49](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/c5eac08e7f6dd49) Dave Neeley *2019-03-29 17:13:31*
24+
25+
### GitHub [#326](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/pull/326) Add Azure Pipelines documentation
26+
Add Azure Pipelines documentation ()
27+
28+
[f6836b09b4d371b](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/f6836b09b4d371b) Dave Neeley *2019-03-29 21:17:29*
29+
30+
### GitHub [#327](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/pull/327) Update Azure Pipelines Sample to set branch of pull request
31+
Update Azure Pipelines Sample to set branch of pull request ()
32+
33+
* Trigger builds on branches
34+
35+
* triggerInfo is returned by later api calls, properties are not
36+
37+
[ab5e796b5352e5e](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/ab5e796b5352e5e) Dave Neeley *2019-03-30 05:28:11*
38+
39+
### No issue
40+
Updating build script
41+
42+
[6528d86c783c172](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/6528d86c783c172) Tomas Bjerre *2018-11-30 18:58:02*
43+
544
## 3.24
645
### GitHub [#308](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/pull/308) Add optional Redirect URL to button configuration
746
Add optional Redirect URL to button configuration ()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ The filter text as well as the URL support variables. These are:
5656
| `${BUTTON_TRIGGER_TITLE}` | Example: `Trigger Notification` |
5757
| `${BUTTON_FORM_DATA}` | The form data that was submitted |
5858
| `${INJECTION_URL_VALUE}` | Value retrieved from any URL |
59+
| `${VARIABLE_REGEX_MATCH}` | The result of custom regex applied to a variable |
5960
| `${PULL_REQUEST_URL}` | Example: `http://localhost:7990/projects/PROJECT_1/repos/rep_1/pull-requests/1` |
6061
| `${PULL_REQUEST_USER_DISPLAY_NAME}` | Example: `Some User` |
6162
| `${PULL_REQUEST_USER_EMAIL_ADDRESS}` | Example: `some.user@bitbucket.domain` |

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class NotificationDTO implements Comparable<NotificationDTO>, Restricted
2020
private List<HeaderDTO> headers;
2121
private String injectionUrl;
2222
private String injectionUrlRegexp;
23+
private String variableName;
24+
private String variableRegex;
2325
private HTTP_METHOD method;
2426
private String name;
2527
private String password;
@@ -104,6 +106,20 @@ public boolean equals(final Object obj) {
104106
} else if (!injectionUrlRegexp.equals(other.injectionUrlRegexp)) {
105107
return false;
106108
}
109+
if (variableName == null) {
110+
if (other.variableName != null) {
111+
return false;
112+
}
113+
} else if (!variableName.equals(other.variableName)) {
114+
return false;
115+
}
116+
if (variableRegex == null) {
117+
if (other.variableRegex != null) {
118+
return false;
119+
}
120+
} else if (!variableRegex.equals(other.variableRegex)) {
121+
return false;
122+
}
107123
if (method != other.method) {
108124
return false;
109125
}
@@ -244,6 +260,14 @@ public String getInjectionUrlRegexp() {
244260
return this.injectionUrlRegexp;
245261
}
246262

263+
public String getVariableName() {
264+
return this.variableName;
265+
}
266+
267+
public String getVariableRegex() {
268+
return this.variableRegex;
269+
}
270+
247271
public HTTP_METHOD getMethod() {
248272
return this.method;
249273
}
@@ -324,6 +348,8 @@ public int hashCode() {
324348
result = prime * result + (httpVersion == null ? 0 : httpVersion.hashCode());
325349
result = prime * result + (injectionUrl == null ? 0 : injectionUrl.hashCode());
326350
result = prime * result + (injectionUrlRegexp == null ? 0 : injectionUrlRegexp.hashCode());
351+
result = prime * result + (variableName == null ? 0 : variableName.hashCode());
352+
result = prime * result + (variableRegex == null ? 0 : variableRegex.hashCode());
327353
result = prime * result + (method == null ? 0 : method.hashCode());
328354
result = prime * result + (name == null ? 0 : name.hashCode());
329355
result = prime * result + (password == null ? 0 : password.hashCode());
@@ -367,6 +393,14 @@ public void setInjectionUrlRegexp(final String injectionUrlRegexp) {
367393
this.injectionUrlRegexp = injectionUrlRegexp;
368394
}
369395

396+
public void setVariableName(final String variableName) {
397+
this.variableName = variableName;
398+
}
399+
400+
public void setVariableRegex(final String variableRegex) {
401+
this.variableRegex = variableRegex;
402+
}
403+
370404
public void setMethod(final HTTP_METHOD method) {
371405
this.method = method;
372406
}
@@ -467,6 +501,10 @@ public String toString() {
467501
+ injectionUrl
468502
+ ", injectionUrlRegexp="
469503
+ injectionUrlRegexp
504+
+ ", variableName="
505+
+ variableName
506+
+ ", variableRegex="
507+
+ variableRegex
470508
+ ", method="
471509
+ method
472510
+ ", name="

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,56 @@ public String resolve(
8989
return on('&').join(parts);
9090
}
9191
}),
92+
VARIABLE_REGEX_MATCH(
93+
new PrnfbVariableResolver() {
94+
@Override
95+
public String resolve(
96+
final PullRequest pullRequest,
97+
final PrnfbPullRequestAction pullRequestAction,
98+
final ApplicationUser applicationUser,
99+
final RepositoryService repositoryService,
100+
final ApplicationPropertiesService propertiesService,
101+
final PrnfbNotification prnfbNotification,
102+
final Map<PrnfbVariable, Supplier<String>> variables,
103+
final ClientKeyStore clientKeyStore,
104+
final boolean shouldAcceptAnyCertificate,
105+
final SecurityService securityService) {
106+
if (prnfbNotification == null || !prnfbNotification.getVariableName().isPresent()) {
107+
return "";
108+
}
109+
final String variableName = prnfbNotification.getVariableName().get();
110+
String variableValue = "";
111+
for (final PrnfbVariable v : PrnfbVariable.values()) {
112+
if (v.name().equals(variableName) && v != VARIABLE_REGEX_MATCH) {
113+
variableValue =
114+
v.resolve(
115+
pullRequest,
116+
pullRequestAction,
117+
applicationUser,
118+
repositoryService,
119+
propertiesService,
120+
prnfbNotification,
121+
variables,
122+
clientKeyStore,
123+
shouldAcceptAnyCertificate,
124+
securityService);
125+
}
126+
}
127+
if (prnfbNotification.getVariableRegex().isPresent()) {
128+
final Matcher m =
129+
compile(prnfbNotification.getVariableRegex().get()).matcher(variableValue);
130+
if (!m.find()) {
131+
return "";
132+
}
133+
if (m.groupCount() == 0) {
134+
return m.group();
135+
}
136+
return m.group(1);
137+
} else {
138+
return variableValue;
139+
}
140+
}
141+
}),
92142
INJECTION_URL_VALUE(
93143
new PrnfbVariableResolver() {
94144
@Override

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class PrnfbNotification implements HasUuid, Restricted {
2929
private final List<PrnfbHeader> headers;
3030
private final String injectionUrl;
3131
private final String injectionUrlRegexp;
32+
private final String variableName;
33+
private final String variableRegex;
3234
private final HTTP_METHOD method;
3335
private final String name;
3436
private final String password;
@@ -93,6 +95,8 @@ public PrnfbNotification(final PrnfbNotificationBuilder builder) throws Validati
9395
this.name = firstNonNull(emptyToNull(nullToEmpty(builder.getName()).trim()), DEFAULT_NAME);
9496
this.injectionUrl = emptyToNull(nullToEmpty(builder.getInjectionUrl()).trim());
9597
this.injectionUrlRegexp = emptyToNull(nullToEmpty(builder.getInjectionUrlRegexp()).trim());
98+
this.variableName = emptyToNull(nullToEmpty(builder.getVariableName()).trim());
99+
this.variableRegex = emptyToNull(nullToEmpty(builder.getVariableRegex()).trim());
96100
this.triggerIgnoreStateList = builder.getTriggerIgnoreStateList();
97101
this.postContentEncoding = firstNonNull(builder.getPostContentEncoding(), NONE);
98102
this.httpVersion = builder.getHttpVersion();
@@ -152,6 +156,20 @@ public boolean equals(final Object obj) {
152156
} else if (!injectionUrlRegexp.equals(other.injectionUrlRegexp)) {
153157
return false;
154158
}
159+
if (variableName == null) {
160+
if (other.variableName != null) {
161+
return false;
162+
}
163+
} else if (!variableName.equals(other.variableName)) {
164+
return false;
165+
}
166+
if (variableRegex == null) {
167+
if (other.variableRegex != null) {
168+
return false;
169+
}
170+
} else if (!variableRegex.equals(other.variableRegex)) {
171+
return false;
172+
}
155173
if (method != other.method) {
156174
return false;
157175
}
@@ -292,6 +310,14 @@ public Optional<String> getInjectionUrlRegexp() {
292310
return fromNullable(this.injectionUrlRegexp);
293311
}
294312

313+
public Optional<String> getVariableName() {
314+
return fromNullable(this.variableName);
315+
}
316+
317+
public Optional<String> getVariableRegex() {
318+
return fromNullable(this.variableRegex);
319+
}
320+
295321
public HTTP_METHOD getMethod() {
296322
return this.method;
297323
}
@@ -377,6 +403,8 @@ public int hashCode() {
377403
result = prime * result + (httpVersion == null ? 0 : httpVersion.hashCode());
378404
result = prime * result + (injectionUrl == null ? 0 : injectionUrl.hashCode());
379405
result = prime * result + (injectionUrlRegexp == null ? 0 : injectionUrlRegexp.hashCode());
406+
result = prime * result + (variableName == null ? 0 : variableName.hashCode());
407+
result = prime * result + (variableRegex == null ? 0 : variableRegex.hashCode());
380408
result = prime * result + (method == null ? 0 : method.hashCode());
381409
result = prime * result + (name == null ? 0 : name.hashCode());
382410
result = prime * result + (password == null ? 0 : password.hashCode());
@@ -412,6 +440,10 @@ public String toString() {
412440
+ injectionUrl
413441
+ ", injectionUrlRegexp="
414442
+ injectionUrlRegexp
443+
+ ", variableName="
444+
+ variableName
445+
+ ", variableRegex="
446+
+ variableRegex
415447
+ ", method="
416448
+ method
417449
+ ", name="

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public PrnfbNotificationBuilder(
2424
final List<PrnfbHeader> headers,
2525
final String injectionUrl,
2626
final String injectionUrlRegexp,
27+
final String variableName,
28+
final String variableRegex,
2729
final HTTP_METHOD method,
2830
final String name,
2931
final String password,
@@ -49,6 +51,8 @@ public PrnfbNotificationBuilder(
4951
this.headers = headers;
5052
this.injectionUrl = injectionUrl;
5153
this.injectionUrlRegexp = injectionUrlRegexp;
54+
this.variableName = variableName;
55+
this.variableRegex = variableRegex;
5256
this.method = method;
5357
this.name = name;
5458
this.password = password;
@@ -96,6 +100,8 @@ public static PrnfbNotificationBuilder prnfbNotificationBuilder(final PrnfbNotif
96100
b.name = from.getName();
97101
b.injectionUrl = from.getInjectionUrl().orNull();
98102
b.injectionUrlRegexp = from.getInjectionUrlRegexp().orNull();
103+
b.variableName = from.getVariableName().orNull();
104+
b.variableRegex = from.getVariableRegex().orNull();
99105
b.triggerIfCanMerge = from.getTriggerIfCanMerge();
100106
b.postContentEncoding = from.getPostContentEncoding();
101107
b.httpVersion = from.getHttpVersion();
@@ -107,6 +113,8 @@ public static PrnfbNotificationBuilder prnfbNotificationBuilder(final PrnfbNotif
107113
private List<PrnfbHeader> headers = newArrayList();
108114
private String injectionUrl;
109115
private String injectionUrlRegexp;
116+
private String variableName;
117+
private String variableRegex;
110118
private HTTP_METHOD method;
111119
private String name;
112120
private String password;
@@ -160,6 +168,14 @@ public String getInjectionUrlRegexp() {
160168
return this.injectionUrlRegexp;
161169
}
162170

171+
public String getVariableName() {
172+
return this.variableName;
173+
}
174+
175+
public String getVariableRegex() {
176+
return this.variableRegex;
177+
}
178+
163179
public HTTP_METHOD getMethod() {
164180
return this.method;
165181
}
@@ -282,6 +298,16 @@ public PrnfbNotificationBuilder withInjectionUrlRegexp(final String injectionUrl
282298
return this;
283299
}
284300

301+
public PrnfbNotificationBuilder withVariableName(final String variableName) {
302+
this.variableName = emptyToNull(variableName);
303+
return this;
304+
}
305+
306+
public PrnfbNotificationBuilder withVariableRegex(final String variableRegex) {
307+
this.variableRegex = emptyToNull(variableRegex);
308+
return this;
309+
}
310+
285311
public PrnfbNotificationBuilder withMethod(final HTTP_METHOD method) {
286312
this.method = firstNonNull(method, GET);
287313
return this;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public static NotificationDTO toNotificationDto(final PrnfbNotification from) {
2424
to.setFilterString(from.getFilterString().orNull());
2525
to.setInjectionUrl(from.getInjectionUrl().orNull());
2626
to.setInjectionUrlRegexp(from.getInjectionUrlRegexp().orNull());
27+
to.setVariableName(from.getVariableName().orNull());
28+
to.setVariableRegex(from.getVariableRegex().orNull());
2729
to.setMethod(from.getMethod());
2830
to.setName(from.getName());
2931
to.setHeaders(toHeaders(from.getHeaders()));
@@ -65,6 +67,8 @@ public static PrnfbNotification toPrnfbNotification(final NotificationDTO from)
6567
.setHeaders(toHeaders(from)) //
6668
.withInjectionUrl(from.getInjectionUrl()) //
6769
.withInjectionUrlRegexp(from.getInjectionUrlRegexp()) //
70+
.withVariableName(from.getVariableName()) //
71+
.withVariableRegex(from.getVariableRegex()) //
6872
.withMethod(from.getMethod()) //
6973
.withName(from.getName()) //
7074
.withPassword(from.getPassword()) //

src/main/resources/admin.vm

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<li><b>${BUTTON_TRIGGER_TITLE}</b> Example: Trigger Notification</li>
7171
<li><b>${BUTTON_FORM_DATA}</b> The form data that was submitted</li>
7272
<li><b>${INJECTION_URL_VALUE}</b> If injection URL configured</li>
73+
<li><b>${VARIABLE_REGEX_MATCH}</b> If variable regex is configured</li>
7374
<li><b>${PULL_REQUEST_USER_DISPLAY_NAME}</b> Example: Some User</li>
7475
<li><b>${PULL_REQUEST_USER_EMAIL_ADDRESS}</b> Example: some.user@bitbucket.domain</li>
7576
<li><b>${PULL_REQUEST_USER_ID}</b> Example: 1</li>
@@ -497,6 +498,21 @@
497498
</div>
498499
</fieldset>
499500

501+
<h4>Variable Regex Match</h4>
502+
<p>Apply custom regex to a variable. Uses first capture group if one is provided.</p>
503+
<fieldset class="group">
504+
<div class="field-group">
505+
<label>Variable Name </label>
506+
<input class="text long-field" type="text" name="variableName">
507+
<div class="description"></div>
508+
</div>
509+
<div class="field-group">
510+
<label>Variable Regex </label>
511+
<input class="text long-field" type="text" name="variableRegex">
512+
<div class="description">Regular expression to match against the variable value.</div>
513+
</div>
514+
</fieldset>
515+
500516
<h4>Basic authentication</h4>
501517
<p>Optional basic authentication details.</p>
502518
<fieldset class="group">

0 commit comments

Comments
 (0)