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

Commit

Permalink
Invoking URL
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Mar 28, 2015
1 parent b84e8c2 commit c905ca3
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 29 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Pull Request Notifier for Stash [![Build Status](https://travis-ci.org/tomasbjerre/pull-request-notifier-for-stash.svg?branch=master)](https://travis-ci.org/tomasbjerre/pull-request-notifier-for-stash)
A plugin for Atlassian Stash that can notfy other systems on events regarding pull requests. It can, for example, trigger a build in Jenkins.
A plugin for Atlassian Stash that can notfy other systems on events regarding pull requests.

It can, for example, trigger a build in Jenkins. Parameterized Jenkins jobs can be triggered remotely via:
```
http://server/job/theJob/buildWithParameters?token=TOKEN&PARAMETER=Value
```

## Features
The Pull Request Notifier for Stash can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public void anEvent(PullRequestEvent o) {
try {
final PrnfsSettings settings = getPrnfsSettings(pluginSettingsFactory.createGlobalSettings());
for (final PrnfsNotification n : settings.getNotifications()) {
urlInvoker.ivoke(new PrnfsRenderer(o).render(n.getUrl()), n.getUser(), n.getPassword());
if (n.getTriggers().contains(o.getAction())) {
urlInvoker.ivoke(new PrnfsRenderer(o).render(n.getUrl()), n.getUser(), n.getPassword());
}
}
} catch (final ValidationException e) {
logger.error("", e);
Expand Down
34 changes: 32 additions & 2 deletions src/main/java/se/bjurr/prnfs/listener/UrlInvoker.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
package se.bjurr.prnfs.listener;

import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Joiner.on;
import static com.google.common.io.CharStreams.readLines;
import static java.lang.Boolean.TRUE;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Optional;
import com.google.common.io.Closeables;

public class UrlInvoker {
private static final Logger logger = LoggerFactory.getLogger(UrlInvoker.class);

public void ivoke(String url, Optional<String> user, Optional<String> password) {
logger.info("Url: \"" + url + "\" user: \"" + user.or("") + "\" password: \"" + password.or("") + "\"");
public void ivoke(String urlParam, Optional<String> user, Optional<String> password) {
InputStreamReader ir = null;
try {
logger.info("Url: \"" + urlParam + "\" user: \"" + user.or("") + "\" password: \"" + password.or("") + "\"");
final URL url = new URL(urlParam);
final URLConnection uc = url.openConnection();
if (user.isPresent() && password.isPresent()) {
final String userpass = user.get() + ":" + password.get();
final String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);
}
ir = new InputStreamReader(uc.getInputStream(), UTF_8);
logger.debug(on("\n").join(readLines(ir)));
} catch (final Exception e) {
try {
Closeables.close(ir, TRUE);
} catch (final IOException e1) {
}
logger.error("", e);
}
}
}
26 changes: 13 additions & 13 deletions src/main/resources/admin.vm
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
<div class="description">
You can use variables when invoking the URL.
<ul>
<li><b>${PULL_REQUEST_ID</b></li>
<li><b>${PULL_REQUEST_FROM_ID</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_ID</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_NAME</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_SLUG</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_PROJECT_ID</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_PROJECT_KEY</b></li>
<li><b>${PULL_REQUEST_TO_ID</b></li>
<li><b>${PULL_REQUEST_TO_REPO_ID</b></li>
<li><b>${PULL_REQUEST_TO_REPO_NAME</b></li>
<li><b>${PULL_REQUEST_TO_REPO_SLUG</b></li>
<li><b>${PULL_REQUEST_TO_REPO_PROJECT_ID</b></li>
<li><b>${PULL_REQUEST_TO_REPO_PROJECT_KEY</b></li>
<li><b>${PULL_REQUEST_ID}</b></li>
<li><b>${PULL_REQUEST_FROM_ID}</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_ID}</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_NAME}</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_SLUG}</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_PROJECT_ID}</b></li>
<li><b>${PULL_REQUEST_FROM_REPO_PROJECT_KEY}</b></li>
<li><b>${PULL_REQUEST_TO_ID}</b></li>
<li><b>${PULL_REQUEST_TO_REPO_ID}</b></li>
<li><b>${PULL_REQUEST_TO_REPO_NAME}</b></li>
<li><b>${PULL_REQUEST_TO_REPO_SLUG}</b></li>
<li><b>${PULL_REQUEST_TO_REPO_PROJECT_ID}</b></li>
<li><b>${PULL_REQUEST_TO_REPO_PROJECT_KEY}</b></li>
</ul>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package se.bjurr.prnfs.admin;

import static com.atlassian.stash.pull.PullRequestAction.MERGED;
import static com.atlassian.stash.pull.PullRequestAction.OPENED;
import static se.bjurr.prnfs.admin.utils.NotificationBuilder.notificationBuilder;
import static se.bjurr.prnfs.admin.utils.PrnfsTestBuilder.prnfsTestBuilder;
Expand All @@ -21,7 +22,7 @@ public void testThatAUrlWithoutVariablesCanBeInvoked() {
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder().withFieldValue("url", "http://bjurr.se/").withFieldValue("events", OPENED.name()).build())
.store().trigger(pullRequestEventBuilder().build()).invokedUrl("http://bjurr.se/");
.store().trigger(pullRequestEventBuilder().withPullRequestAction(OPENED).build()).invokedUrl("http://bjurr.se/");
}

@Test
Expand All @@ -42,7 +43,7 @@ public void testThatAUrlWithVariablesCanBeInvokedFrom() {
.withFromRef(
pullRequestRefBuilder().withHash("10").withId("10").withProjectId(10).withProjectKey("10")
.withRepositoryId(10).withRepositoryName("10").withRepositorySlug("10")) //
.withId(10L).build()).invokedUrl("http://bjurr.se/10");
.withId(10L).withPullRequestAction(OPENED).build()).invokedUrl("http://bjurr.se/10");
}
}

Expand All @@ -63,7 +64,31 @@ public void testThatAUrlWithVariablesCanBeInvokedTo() {
.withToRef(
pullRequestRefBuilder().withHash("10").withId("10").withProjectId(10).withProjectKey("10")
.withRepositoryId(10).withRepositoryName("10").withRepositorySlug("10")) //
.withId(10L).build()).invokedUrl("http://bjurr.se/10");
.withId(10L).withPullRequestAction(OPENED).build()).invokedUrl("http://bjurr.se/10");
}
}

@Test
public void testThatAUrlIsOnlyInvokedForConfiguredEvents() {
prnfsTestBuilder()
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder().withFieldValue("url", "http://bjurr.se/").withFieldValue("events", OPENED.name()).build())
.store().trigger(pullRequestEventBuilder() //
.withToRef(pullRequestRefBuilder()) //
.withId(10L).withPullRequestAction(MERGED).build()).invokedNoUrl();
}

@Test
public void testThatMultipleUrlsCanBeInvoked() {
prnfsTestBuilder()
.isLoggedInAsAdmin()
.withNotification(
notificationBuilder().withFieldValue("url", "http://merged.se/").withFieldValue("events", MERGED.name()).build())
.withNotification(
notificationBuilder().withFieldValue("url", "http://opened.se/").withFieldValue("events", OPENED.name()).build())
.store().trigger(pullRequestEventBuilder() //
.withToRef(pullRequestRefBuilder()) //
.withId(10L).withPullRequestAction(MERGED).build()).invokedOnlyUrl("http://merged.se/");
}
}
29 changes: 20 additions & 9 deletions src/test/java/se/bjurr/prnfs/admin/utils/PrnfsTestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static com.google.common.collect.Maps.uniqueIndex;
import static java.lang.Boolean.TRUE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -197,34 +198,44 @@ public PrnfsTestBuilder withNotification(AdminFormValues adminFormValues) {
}

public PrnfsTestBuilder invokedUrl(String url) {
assertEquals(gson.toJson(pluginSettings), url, invokedUrl);
assertTrue(gson.toJson(pluginSettings), invokedUrl.contains(url));
return this;
}

public PrnfsTestBuilder invokedUser(String user) {
assertEquals(user, this.usedUser.get());
assertTrue(this.usedUser.contains(user));
return this;
}

public PrnfsTestBuilder invokedPassword(String password) {
assertEquals(password, this.usedPassword.get());
assertTrue(this.usedPassword.contains(password));
return this;
}

private String invokedUrl;
private Optional<String> usedUser;
private Optional<String> usedPassword;
private final List<String> invokedUrl = newArrayList();
private final List<String> usedUser = newArrayList();
private final List<String> usedPassword = newArrayList();

public PrnfsTestBuilder trigger(PullRequestEvent event) {
listener.setUrlInvoker(new UrlInvoker() {
@Override
public void ivoke(String url, Optional<String> userParam, Optional<String> passwordParam) {
invokedUrl = url;
usedUser = userParam;
usedPassword = passwordParam;
invokedUrl.add(url);
usedUser.add(userParam.or(""));
usedPassword.add(passwordParam.or(""));
}
});
listener.anEvent(event);
return this;
}

public PrnfsTestBuilder invokedNoUrl() {
assertEquals(0, invokedUrl.size());
return this;
}

public void invokedOnlyUrl(String url) {
assertEquals(1, invokedUrl.size());
assertTrue(invokedUrl.get(0).equals(url));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static PullRequestEventBuilder pullRequestEventBuilder() {
return new PullRequestEventBuilder();
}

public PullRequestEventBuilder withPullRequestAction(PullRequestAction pullRequestAction) {
this.pullRequestAction = pullRequestAction;
return this;
}

public PullRequestEvent build() {
final PullRequestEvent pullRequestEvent = mock(PullRequestEvent.class);
final PullRequest pullRequest = mock(PullRequest.class);
Expand Down

0 comments on commit c905ca3

Please sign in to comment.