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

Commit

Permalink
Adding author related variables
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Apr 7, 2015
1 parent 280abe7 commit 475f25d
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

Changelog of Pull Request Notifier for Stash.

## 1.5
* Using password type on password-field in admin GUI
* Some new variables added
* ${PULL_REQUEST_ACTION} Example: OPENED
* ${PULL_REQUEST_AUTHOR_DISPLAY_NAME} Example: Administrator
* ${PULL_REQUEST_AUTHOR_EMAIL} Example: [email protected]
* ${PULL_REQUEST_AUTHOR_ID} Example: 1
* ${PULL_REQUEST_AUTHOR_NAME} Example: admin
* ${PULL_REQUEST_AUTHOR_SLUG} Example: admin

## 1.4
* Bugfix: Avoiding multiple notifications being sent from same event.

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ The Pull Request Notifier for Stash can:
The filter text as well as the URL support variables. These are:

* ${PULL_REQUEST_ID} Example: 1
* ${PULL_REQUEST_ACTION} Example: OPENED
* ${PULL_REQUEST_AUTHOR_DISPLAY_NAME} Example: Administrator
* ${PULL_REQUEST_AUTHOR_EMAIL} Example: [email protected]
* ${PULL_REQUEST_AUTHOR_ID} Example: 1
* ${PULL_REQUEST_AUTHOR_NAME} Example: admin
* ${PULL_REQUEST_AUTHOR_SLUG} Example: admin
* ${PULL_REQUEST_FROM_HASH} Example: 6053a1eaa1c009dd11092d09a72f3c41af1b59ad
* ${PULL_REQUEST_FROM_ID} Example: refs/heads/branchmodmerge
* ${PULL_REQUEST_FROM_REPO_ID} Example: 1
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package se.bjurr.prnfs.listener;

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

public class PrnfsRenderer {

Expand Down Expand Up @@ -40,11 +41,41 @@ public String resolve(PullRequestEvent pullRequestEvent) {
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getFromRef().getRepository().getSlug() + "";
}
}), PULL_REQUEST_ACTION(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return PullRequestAction.fromId(pullRequestEvent.getAction().getId()).name();
}
}), PULL_REQUEST_ID(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getId() + "";
}
}), PULL_REQUEST_AUTHOR_ID(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getAuthor().getUser().getId() + "";
}
}), PULL_REQUEST_AUTHOR_DISPLAY_NAME(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getAuthor().getUser().getDisplayName();
}
}), PULL_REQUEST_AUTHOR_NAME(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getAuthor().getUser().getName();
}
}), PULL_REQUEST_AUTHOR_EMAIL(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getAuthor().getUser().getEmailAddress();
}
}), PULL_REQUEST_AUTHOR_SLUG(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
return pullRequestEvent.getPullRequest().getAuthor().getUser().getSlug();
}
}), PULL_REQUEST_TO_HASH(new Resolver() {
@Override
public String resolve(PullRequestEvent pullRequestEvent) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/admin.vm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
<div class="description">
You can use variables when invoking the URL or when forming the filter string.
<ul>
http://bjurr.se/?COMMENTED&Administrator&[email protected]&1&admin&admin
<li><b>${PULL_REQUEST_ID}</b> Example: 1</li>
<li><b>${PULL_REQUEST_ACTION}</b> Example: OPENED</li>
<li><b>${PULL_REQUEST_AUTHOR_DISPLAY_NAME}</b> Example: Administrator</li>
<li><b>${PULL_REQUEST_AUTHOR_EMAIL}</b> Example: [email protected]</li>
<li><b>${PULL_REQUEST_AUTHOR_ID}</b> Example: 1</li>
<li><b>${PULL_REQUEST_AUTHOR_NAME}</b> Example: admin</li>
<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_REPO_ID}</b> Example: 1</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static se.bjurr.prnfs.admin.utils.NotificationBuilder.notificationBuilder;
import static se.bjurr.prnfs.admin.utils.PrnfsParticipantBuilder.prnfsParticipantBuilder;
import static se.bjurr.prnfs.admin.utils.PrnfsTestBuilder.prnfsTestBuilder;
import static se.bjurr.prnfs.admin.utils.PullRequestEventBuilder.pullRequestEventBuilder;
import static se.bjurr.prnfs.admin.utils.PullRequestRefBuilder.pullRequestRefBuilder;
Expand Down Expand Up @@ -79,9 +80,9 @@ public void testThatAUrlWithoutVariablesCanBeInvoked() {
}

@Test
public void testThatAUrlWithVariablesCanBeInvokedFrom() {
public void testThatAUrlWithVariablesFromCanBeInvoked() {
for (final PrnfsVariable prnfsVariable : PrnfsVariable.values()) {
if (prnfsVariable.name().contains("_TO_")) {
if (!prnfsVariable.name().contains("_FROM_")) {
continue;
}
prnfsTestBuilder()
Expand All @@ -101,9 +102,9 @@ public void testThatAUrlWithVariablesCanBeInvokedFrom() {
}

@Test
public void testThatAUrlWithVariablesCanBeInvokedTo() {
public void testThatAUrlWithVariablesToCanBeInvoked() {
for (final PrnfsVariable prnfsVariable : PrnfsVariable.values()) {
if (prnfsVariable.name().contains("_FROM_")) {
if (!prnfsVariable.name().contains("_TO_")) {
continue;
}
prnfsTestBuilder()
Expand All @@ -122,6 +123,34 @@ public void testThatAUrlWithVariablesCanBeInvokedTo() {
}
}

@Test
public void testThatAUrlWithVariablesExceptFromAndToCanBeInvoked() {
prnfsTestBuilder()
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder()
.withFieldValue(
AdminFormValues.FIELDS.url,
"http://bjurr.se/id=${" + PrnfsVariable.PULL_REQUEST_ID.name() + "}&action=${"
+ PrnfsVariable.PULL_REQUEST_ACTION.name() + "}&displayName=${"
+ PrnfsVariable.PULL_REQUEST_AUTHOR_DISPLAY_NAME.name() + "}&authorEmail=${"
+ PrnfsVariable.PULL_REQUEST_AUTHOR_EMAIL.name() + "}&authorId=${"
+ PrnfsVariable.PULL_REQUEST_AUTHOR_ID.name() + "}&authorName=${"
+ PrnfsVariable.PULL_REQUEST_AUTHOR_NAME.name() + "}&authorSlug=${"
+ PrnfsVariable.PULL_REQUEST_AUTHOR_SLUG.name() + "}")
.withFieldValue(AdminFormValues.FIELDS.events, OPENED.name()).build())
.store()
.trigger(
pullRequestEventBuilder()
.withId(10L)
.withPullRequestAction(OPENED)
.withAuthor(
prnfsParticipantBuilder().withDisplayName("authorDisplayName").withEmail("authorEmail").withId(100)
.withName("authorName").withSlug("authorSlug").build()).build())
.invokedUrl(
"http://bjurr.se/id=10&action=OPENED&displayName=authorDisplayName&authorEmail=authorEmail&authorId=100&authorName=authorName&authorSlug=authorSlug");
}

@Test
public void testThatBasicAuthenticationHeaderIsSentIfThereIsAUser() {
prnfsTestBuilder()
Expand Down
114 changes: 114 additions & 0 deletions src/test/java/se/bjurr/prnfs/admin/utils/PrnfsParticipantBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package se.bjurr.prnfs.admin.utils;

import com.atlassian.stash.pull.PullRequest;
import com.atlassian.stash.pull.PullRequestParticipant;
import com.atlassian.stash.pull.PullRequestRole;
import com.atlassian.stash.user.StashUser;
import com.atlassian.stash.user.StashUserVisitor;
import com.atlassian.stash.user.UserType;

public class PrnfsParticipantBuilder {

private String slug;
private String name;
private Integer id;
private String displayName;
private String email;

private PrnfsParticipantBuilder() {
}

public PrnfsParticipantBuilder withDisplayName(String string) {
this.displayName = string;
return this;
}

public PrnfsParticipantBuilder withEmail(String string) {
this.email = string;
return this;
}

public PrnfsParticipantBuilder withId(Integer id) {
this.id = id;
return this;
}

public PrnfsParticipantBuilder withName(String string) {
this.name = string;
return this;
}

public PrnfsParticipantBuilder withSlug(String string) {
this.slug = string;
return this;
}

public static PrnfsParticipantBuilder prnfsParticipantBuilder() {
return new PrnfsParticipantBuilder();
}

public PullRequestParticipant build() {
return new PullRequestParticipant() {

@Override
public boolean isApproved() {
return false;
}

@Override
public StashUser getUser() {
return new StashUser() {
@Override
public String getName() {
return name;
}

@Override
public String getEmailAddress() {
return email;
}

@Override
public boolean isActive() {
return false;
}

@Override
public UserType getType() {
return null;
}

@Override
public String getSlug() {
return slug;
}

@Override
public Integer getId() {
return id;
}

@Override
public String getDisplayName() {
return displayName;
}

@Override
public <T> T accept(StashUserVisitor<T> arg0) {
return null;
}
};
}

@Override
public PullRequestRole getRole() {
return null;
}

@Override
public PullRequest getPullRequest() {
return null;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ public PrnfsTestBuilder invokedPassword(String password) {
}

public PrnfsTestBuilder invokedUrl(String url) {
assertTrue(gson.toJson(pluginSettings), invokedUrl.contains(url));
if (invokedUrl.size() == 1) {
assertEquals(url, invokedUrl.get(0));
} else {
assertTrue(gson.toJson(invokedUrl), invokedUrl.contains(url));
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import com.atlassian.stash.event.pull.PullRequestEvent;
import com.atlassian.stash.pull.PullRequest;
import com.atlassian.stash.pull.PullRequestAction;
import com.atlassian.stash.pull.PullRequestParticipant;

public class PullRequestEventBuilder {
private PullRequestAction pullRequestAction;
private PullRequestRefBuilder toRef;
private PullRequestRefBuilder fromRef;
private Long id;
private PullRequestParticipant author;

private PullRequestEventBuilder() {
}
Expand All @@ -35,19 +37,25 @@ public PullRequestEventBuilder withPullRequestAction(PullRequestAction pullReque
return this;
}

public PullRequestEventBuilder withAuthor(PullRequestParticipant author) {
this.author = author;
return this;
}

public PullRequestEventBuilder withId(Long id) {
this.id = id;
return this;
}

public PullRequestEvent build() {
final PullRequestEvent pullRequestEvent = mock(PullRequestEvent.class);
final PullRequest pullRequest = mock(PullRequest.class);
when(pullRequestEvent.getAction()).thenReturn(pullRequestAction);
when(pullRequestEvent.getPullRequest()).thenReturn(pullRequest);
when(pullRequestEvent.getPullRequest().getAuthor()).thenReturn(author);
when(pullRequestEvent.getPullRequest().getId()).thenReturn(id);
when(pullRequestEvent.getPullRequest().getFromRef()).thenReturn(fromRef);
when(pullRequestEvent.getPullRequest().getToRef()).thenReturn(toRef);
return pullRequestEvent;
}

public PullRequestEventBuilder withId(Long id) {
this.id = id;
return this;
}
}

0 comments on commit 475f25d

Please sign in to comment.