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

Commit 2b6d1c0

Browse files
committed
Adding PULL_REQUEST_FROM_BRANCH and PULL_REQUEST_TO_BRANCH variables to make branch names available
1 parent f5c817e commit 2b6d1c0

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

CHANGELOG.md

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

33
Changelog of Pull Request Notifier for Stash.
44

5+
## 1.10
6+
* Adding PULL_REQUEST_FROM_BRANCH and PULL_REQUEST_TO_BRANCH variables to make branch names available
7+
58
## 1.9
69
* Adding support for PUT and DELETE
710

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The filter text as well as the URL support variables. These are:
3535
* ${PULL_REQUEST_AUTHOR_SLUG} Example: admin
3636
* ${PULL_REQUEST_FROM_HASH} Example: 6053a1eaa1c009dd11092d09a72f3c41af1b59ad
3737
* ${PULL_REQUEST_FROM_ID} Example: refs/heads/branchmodmerge
38+
* ${PULL_REQUEST_FROM_BRANCH} Example: branchmodmerge
3839
* ${PULL_REQUEST_FROM_REPO_ID} Example: 1
3940
* ${PULL_REQUEST_FROM_REPO_NAME} Example: rep_1
4041
* ${PULL_REQUEST_FROM_REPO_PROJECT_ID} Example: 1

src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static se.bjurr.prnfs.listener.PrnfsPullRequestAction.fromPullRequestEvent;
44

55
import com.atlassian.stash.event.pull.PullRequestEvent;
6+
import com.atlassian.stash.pull.PullRequestRef;
67

78
public class PrnfsRenderer {
89

@@ -17,6 +18,11 @@ public String resolve(PullRequestEvent pullRequestEvent) {
1718
public String resolve(PullRequestEvent pullRequestEvent) {
1819
return pullRequestEvent.getPullRequest().getFromRef().getId();
1920
}
21+
}), PULL_REQUEST_FROM_BRANCH(new Resolver() {
22+
@Override
23+
public String resolve(PullRequestEvent pullRequestEvent) {
24+
return branchNameFromId(pullRequestEvent.getPullRequest().getFromRef());
25+
}
2026
}), PULL_REQUEST_FROM_REPO_ID(new Resolver() {
2127
@Override
2228
public String resolve(PullRequestEvent pullRequestEvent) {
@@ -87,6 +93,11 @@ public String resolve(PullRequestEvent pullRequestEvent) {
8793
public String resolve(PullRequestEvent pullRequestEvent) {
8894
return pullRequestEvent.getPullRequest().getToRef().getId();
8995
}
96+
}), PULL_REQUEST_TO_BRANCH(new Resolver() {
97+
@Override
98+
public String resolve(PullRequestEvent pullRequestEvent) {
99+
return branchNameFromId(pullRequestEvent.getPullRequest().getToRef());
100+
}
90101
}), PULL_REQUEST_TO_REPO_ID(new Resolver() {
91102
@Override
92103
public String resolve(PullRequestEvent pullRequestEvent) {
@@ -116,6 +127,12 @@ public String resolve(PullRequestEvent pullRequestEvent) {
116127

117128
private Resolver resolver;
118129

130+
private static String branchNameFromId(PullRequestRef pullRequestRef) {
131+
String branchId = pullRequestRef.getId();
132+
int lastSlash = branchId.lastIndexOf('/');
133+
return branchId.substring(lastSlash + 1);
134+
}
135+
119136
private PrnfsVariable(Resolver resolver) {
120137
this.resolver = resolver;
121138
}

src/main/resources/admin.vm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
<li><b>${PULL_REQUEST_AUTHOR_SLUG}</b> Example: admin</li>
1919
<li><b>${PULL_REQUEST_FROM_HASH}</b> Example: 6053a1eaa1c009dd11092d09a72f3c41af1b59ad</li>
2020
<li><b>${PULL_REQUEST_FROM_ID}</b> Example: refs/heads/branch_mod_merge</li>
21+
<li><b>${PULL_REQUEST_FROM_BRANCH}</b> Example: branch_mod_merge</li>
2122
<li><b>${PULL_REQUEST_FROM_REPO_ID}</b> Example: 1</li>
2223
<li><b>${PULL_REQUEST_FROM_REPO_NAME}</b> Example: rep_1</li>
2324
<li><b>${PULL_REQUEST_FROM_REPO_PROJECT_ID}</b> Example: 1</li>
2425
<li><b>${PULL_REQUEST_FROM_REPO_PROJECT_KEY}</b> Example: PROJECT_1</li>
2526
<li><b>${PULL_REQUEST_FROM_REPO_SLUG}</b> Example: rep_1</li>
2627
<li><b>${PULL_REQUEST_TO_HASH}</b> Example: d6edcbf924697ab811a867421dab60d954ccad99</li>
2728
<li><b>${PULL_REQUEST_TO_ID}</b> Example: refs/heads/basic_branching</li>
29+
<li><b>${PULL_REQUEST_TO_BRANCH}</b> Example: basic_branching</li>
2830
<li><b>${PULL_REQUEST_TO_REPO_ID}</b> Example: 1</li>
2931
<li><b>${PULL_REQUEST_TO_REPO_NAME}</b> Example: rep_1</li>
3032
<li><b>${PULL_REQUEST_TO_REPO_PROJECT_ID}</b> Example: 1</li>

src/test/java/se/bjurr/prnfs/admin/PrnfsPullRequestEventListenerTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,34 @@ public void testThatAUrlWithVariablesToCanBeInvoked() {
128128
}
129129
}
130130

131+
@Test
132+
public void testThatAUrlWithVariableFromBranchCanBeInvokedWhenBranchIdContainsSlashes() {
133+
prnfsTestBuilder()
134+
.isLoggedInAsAdmin()
135+
.withNotification(
136+
notificationBuilder()
137+
.withFieldValue(AdminFormValues.FIELDS.url,
138+
"http://bjurr.se/${" + PrnfsVariable.PULL_REQUEST_FROM_BRANCH.name() + "}")
139+
.withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()).build()).store()
140+
.trigger(pullRequestEventBuilder() //
141+
.withFromRef(pullRequestRefBuilder().withId("refs/heads/branchmodmerge")) //
142+
.withId(10L).withPullRequestAction(OPENED).build()).invokedUrl("http://bjurr.se/branchmodmerge");
143+
}
144+
145+
@Test
146+
public void testThatAUrlWithVariableFromBranchCanBeInvokedWhenBranchIdContainsOnlyName() {
147+
prnfsTestBuilder()
148+
.isLoggedInAsAdmin()
149+
.withNotification(
150+
notificationBuilder()
151+
.withFieldValue(AdminFormValues.FIELDS.url,
152+
"http://bjurr.se/${" + PrnfsVariable.PULL_REQUEST_FROM_BRANCH.name() + "}")
153+
.withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()).build()).store()
154+
.trigger(pullRequestEventBuilder() //
155+
.withFromRef(pullRequestRefBuilder().withId("branchmodmerge")) //
156+
.withId(10L).withPullRequestAction(OPENED).build()).invokedUrl("http://bjurr.se/branchmodmerge");
157+
}
158+
131159
@Test
132160
public void testThatAUrlWithVariablesExceptFromAndToCanBeInvoked() {
133161
prnfsTestBuilder()

0 commit comments

Comments
 (0)