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

Commit

Permalink
Adding PULL_REQUEST_FROM_BRANCH and PULL_REQUEST_TO_BRANCH variables …
Browse files Browse the repository at this point in the history
…to make branch names available
  • Loading branch information
tomasbjerre committed Apr 15, 2015
1 parent f5c817e commit 2b6d1c0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Changelog of Pull Request Notifier for Stash.

## 1.10
* Adding PULL_REQUEST_FROM_BRANCH and PULL_REQUEST_TO_BRANCH variables to make branch names available

## 1.9
* Adding support for PUT and DELETE

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The filter text as well as the URL support variables. These are:
* ${PULL_REQUEST_AUTHOR_SLUG} Example: admin
* ${PULL_REQUEST_FROM_HASH} Example: 6053a1eaa1c009dd11092d09a72f3c41af1b59ad
* ${PULL_REQUEST_FROM_ID} Example: refs/heads/branchmodmerge
* ${PULL_REQUEST_FROM_BRANCH} Example: branchmodmerge
* ${PULL_REQUEST_FROM_REPO_ID} Example: 1
* ${PULL_REQUEST_FROM_REPO_NAME} Example: rep_1
* ${PULL_REQUEST_FROM_REPO_PROJECT_ID} Example: 1
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static se.bjurr.prnfs.listener.PrnfsPullRequestAction.fromPullRequestEvent;

import com.atlassian.stash.event.pull.PullRequestEvent;
import com.atlassian.stash.pull.PullRequestRef;

public class PrnfsRenderer {

Expand All @@ -17,6 +18,11 @@ public String resolve(PullRequestEvent pullRequestEvent) {
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getFromRef().getId();
}
}), PULL_REQUEST_FROM_BRANCH(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return branchNameFromId(pullRequestEvent.getPullRequest().getFromRef());
}
}), PULL_REQUEST_FROM_REPO_ID(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
Expand Down Expand Up @@ -87,6 +93,11 @@ public String resolve(PullRequestEvent pullRequestEvent) {
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getToRef().getId();
}
}), PULL_REQUEST_TO_BRANCH(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return branchNameFromId(pullRequestEvent.getPullRequest().getToRef());
}
}), PULL_REQUEST_TO_REPO_ID(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
Expand Down Expand Up @@ -116,6 +127,12 @@ public String resolve(PullRequestEvent pullRequestEvent) {

private Resolver resolver;

private static String branchNameFromId(PullRequestRef pullRequestRef) {
String branchId = pullRequestRef.getId();
int lastSlash = branchId.lastIndexOf('/');
return branchId.substring(lastSlash + 1);
}

private PrnfsVariable(Resolver resolver) {
this.resolver = resolver;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/admin.vm
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
<li><b>${PULL_REQUEST_AUTHOR_SLUG}</b> Example: admin</li>
<li><b>${PULL_REQUEST_FROM_HASH}</b> Example: 6053a1eaa1c009dd11092d09a72f3c41af1b59ad</li>
<li><b>${PULL_REQUEST_FROM_ID}</b> Example: refs/heads/branch_mod_merge</li>
<li><b>${PULL_REQUEST_FROM_BRANCH}</b> Example: branch_mod_merge</li>
<li><b>${PULL_REQUEST_FROM_REPO_ID}</b> Example: 1</li>
<li><b>${PULL_REQUEST_FROM_REPO_NAME}</b> Example: rep_1</li>
<li><b>${PULL_REQUEST_FROM_REPO_PROJECT_ID}</b> Example: 1</li>
<li><b>${PULL_REQUEST_FROM_REPO_PROJECT_KEY}</b> Example: PROJECT_1</li>
<li><b>${PULL_REQUEST_FROM_REPO_SLUG}</b> Example: rep_1</li>
<li><b>${PULL_REQUEST_TO_HASH}</b> Example: d6edcbf924697ab811a867421dab60d954ccad99</li>
<li><b>${PULL_REQUEST_TO_ID}</b> Example: refs/heads/basic_branching</li>
<li><b>${PULL_REQUEST_TO_BRANCH}</b> Example: basic_branching</li>
<li><b>${PULL_REQUEST_TO_REPO_ID}</b> Example: 1</li>
<li><b>${PULL_REQUEST_TO_REPO_NAME}</b> Example: rep_1</li>
<li><b>${PULL_REQUEST_TO_REPO_PROJECT_ID}</b> Example: 1</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,34 @@ public void testThatAUrlWithVariablesToCanBeInvoked() {
}
}

@Test
public void testThatAUrlWithVariableFromBranchCanBeInvokedWhenBranchIdContainsSlashes() {
prnfsTestBuilder()
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder()
.withFieldValue(AdminFormValues.FIELDS.url,
"http://bjurr.se/${" + PrnfsVariable.PULL_REQUEST_FROM_BRANCH.name() + "}")
.withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()).build()).store()
.trigger(pullRequestEventBuilder() //
.withFromRef(pullRequestRefBuilder().withId("refs/heads/branchmodmerge")) //
.withId(10L).withPullRequestAction(OPENED).build()).invokedUrl("http://bjurr.se/branchmodmerge");
}

@Test
public void testThatAUrlWithVariableFromBranchCanBeInvokedWhenBranchIdContainsOnlyName() {
prnfsTestBuilder()
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder()
.withFieldValue(AdminFormValues.FIELDS.url,
"http://bjurr.se/${" + PrnfsVariable.PULL_REQUEST_FROM_BRANCH.name() + "}")
.withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()).build()).store()
.trigger(pullRequestEventBuilder() //
.withFromRef(pullRequestRefBuilder().withId("branchmodmerge")) //
.withId(10L).withPullRequestAction(OPENED).build()).invokedUrl("http://bjurr.se/branchmodmerge");
}

@Test
public void testThatAUrlWithVariablesExceptFromAndToCanBeInvoked() {
prnfsTestBuilder()
Expand Down

0 comments on commit 2b6d1c0

Please sign in to comment.