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

Commit

Permalink
Removing XPath alternative
Browse files Browse the repository at this point in the history
 * Introduced in version 1.22. It may not work in all installations. And is is not needed for Jenkins, which was the original use case.
 * Also adjusting admin GUI
  • Loading branch information
tomasbjerre committed Sep 1, 2015
1 parent 27fc68a commit defa101
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 121 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.26
* Removing XPath alternative, introduced in version 1.22. It may not work in all installations. And is is not needed for Jenkins, which was the original use case.

## 1.25
* Bugfix: Sending post content in POST and PUT notifications.

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ The Pull Request Notifier for Stash can:
* RESCOPED_FROM, when source branch change
* RESCOPED_TO, when target branch change
* BUTTON_TRIGGER, when trigger button in pull request view is pressed
* Can invoke CSRF protected systems (including Jenkins), using the ${INJECTION_URL_VALUE} variable
* Can invoke CSRF protected systems, using the ${INJECTION_URL_VALUE} variable
* With Jenkins you set the Injection URL field to: `JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)`
* Be configured to only trigger if the pull request mathches a filter. A filter text is constructed with any combination of the variables and then a regexp is constructed to match that text.
* Add buttons to pull request view in Stash. And map those buttons to URL invocations. This can be done by setting the filter string to ${BUTTON_TRIGGER_TITLE} and the filter regexp to title of button.
* Authenticate with HTTP basic authentication.
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/se/bjurr/prnfs/admin/AdminFormValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public enum FORM_TYPE {
BUTTON_CONFIG_FORM, TRIGGER_CONFIG_FORM, GLOBAL_SETTINGS
};

public enum INEJCTION_TYPE {
XPATH, RAW
};

public enum BUTTON_VISIBILITY {
NONE, SYSTEM_ADMIN, ADMIN, EVERYONE
};
Expand All @@ -50,8 +46,6 @@ public enum FIELDS {
button_visibility, //
admin_allowed, //
user_allowed, //
injection_url, //
injection_url_xpath, //
injection_url_type, //
injection_url
}
}
24 changes: 1 addition & 23 deletions src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package se.bjurr.prnfs.listener;

import static java.util.logging.Level.SEVERE;
import static java.util.logging.Logger.getLogger;
import static javax.xml.xpath.XPathConstants.STRING;
import static se.bjurr.prnfs.admin.AdminFormValues.INEJCTION_TYPE.RAW;
import static se.bjurr.prnfs.admin.AdminFormValues.INEJCTION_TYPE.XPATH;
import static se.bjurr.prnfs.listener.PrnfsRenderer.REPO_PROTOCOL.http;
import static se.bjurr.prnfs.listener.PrnfsRenderer.REPO_PROTOCOL.ssh;
import static se.bjurr.prnfs.listener.UrlInvoker.urlInvoker;
Expand All @@ -14,9 +10,6 @@
import java.util.Set;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPathFactory;

import se.bjurr.prnfs.settings.PrnfsNotification;

import com.atlassian.stash.pull.PullRequest;
Expand Down Expand Up @@ -316,22 +309,7 @@ public String resolve(PullRequest pullRequest, PrnfsPullRequestAction pullReques
.withProxyUser(prnfsNotification.getProxyUser()) //
.withProxyPassword(prnfsNotification.getProxyPassword());
PrnfsRenderer.invoker.invoke(urlInvoker);
if (prnfsNotification.getInjectionUrlType() == RAW) {
return urlInvoker.getResponseString().trim();
} else if (prnfsNotification.getInjectionUrlType() == XPATH && prnfsNotification.getInjectionUrlXPath().isPresent()) {
try {
return (String) XPathFactory
.newInstance()
.newXPath()
.compile(prnfsNotification.getInjectionUrlXPath().get())
.evaluate(
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(urlInvoker.getResponseStringStream()), STRING);
} catch (Exception e) {
logger.log(SEVERE, prnfsNotification.getInjectionUrl().get() + " "
+ prnfsNotification.getInjectionUrlXPath().get(), e);
}
}
return "";
return urlInvoker.getResponseString().trim();
}
});

Expand Down
23 changes: 2 additions & 21 deletions src/main/java/se/bjurr/prnfs/settings/PrnfsNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import se.bjurr.prnfs.admin.AdminFormValues;
import se.bjurr.prnfs.admin.AdminFormValues.FORM_TYPE;
import se.bjurr.prnfs.admin.AdminFormValues.INEJCTION_TYPE;
import se.bjurr.prnfs.listener.PrnfsPullRequestAction;
import se.bjurr.prnfs.listener.UrlInvoker.HTTP_METHOD;

Expand All @@ -40,14 +39,11 @@ public class PrnfsNotification {
private final Integer proxyPort;
private final String name;
private final String injectionUrl;
private final String injectionUrlJsonPath;
private final String injectionUrlXPath;
private final INEJCTION_TYPE injectionUrlType;

public PrnfsNotification(List<PrnfsPullRequestAction> triggers, String url, String user, String password,
String filterString, String filterRegexp, String method, String postContent, List<Header> headers, String proxyUser,
String proxyPassword, String proxyServer, String proxyPort, String name, String injectionUrl,
String injectionUrlJsonPath, String injectionUrlXPath, INEJCTION_TYPE injectionUrlType) throws ValidationException {
String proxyPassword, String proxyServer, String proxyPort, String name, String injectionUrl)
throws ValidationException {
this.proxyUser = emptyToNull(nullToEmpty(proxyUser).trim());
this.proxyPassword = emptyToNull(nullToEmpty(proxyPassword).trim());
this.proxyServer = emptyToNull(nullToEmpty(proxyServer).trim());
Expand Down Expand Up @@ -83,9 +79,6 @@ public PrnfsNotification(List<PrnfsPullRequestAction> triggers, String url, Stri
this.filterRegexp = filterRegexp;
this.name = firstNonNull(emptyToNull(nullToEmpty(name).trim()), DEFAULT_NAME);
this.injectionUrl = emptyToNull(nullToEmpty(injectionUrl).trim());
this.injectionUrlJsonPath = emptyToNull(nullToEmpty(injectionUrlJsonPath).trim());
this.injectionUrlXPath = emptyToNull(nullToEmpty(injectionUrlXPath).trim());
this.injectionUrlType = injectionUrlType;
}

public Optional<String> getFilterRegexp() {
Expand Down Expand Up @@ -153,16 +146,4 @@ public static boolean isOfType(AdminFormValues config, FORM_TYPE formType) {
public Optional<String> getInjectionUrl() {
return fromNullable(injectionUrl);
}

public Optional<String> getInjectionUrlJsonPath() {
return fromNullable(injectionUrlJsonPath);
}

public Optional<String> getInjectionUrlXPath() {
return fromNullable(injectionUrlXPath);
}

public INEJCTION_TYPE getInjectionUrlType() {
return injectionUrlType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.List;

import se.bjurr.prnfs.admin.AdminFormValues.INEJCTION_TYPE;
import se.bjurr.prnfs.listener.PrnfsPullRequestAction;

public class PrnfsNotificationBuilder {
Expand All @@ -31,39 +30,20 @@ public static PrnfsNotificationBuilder prnfsNotificationBuilder() {
private String proxyPort;
private String name;
private String injectionUrl;
private String injectionUrlJsonPath;
private String injectionUrlXPath;
private INEJCTION_TYPE injectionUrlType;

private PrnfsNotificationBuilder() {
}

public PrnfsNotification build() throws ValidationException {
return new PrnfsNotification(triggers, url, user, password, filterString, filterRegexp, method, postContent, headers,
proxyUser, proxyPassword, proxyServer, proxyPort, name, injectionUrl, injectionUrlJsonPath, injectionUrlXPath,
injectionUrlType);
proxyUser, proxyPassword, proxyServer, proxyPort, name, injectionUrl);
}

public PrnfsNotificationBuilder withInjectionUrl(String injectionUrl) {
this.injectionUrl = checkNotNull(injectionUrl);
return this;
}

public PrnfsNotificationBuilder withInjectionUrlJsonPath(String injectionUrlJsonPath) {
this.injectionUrlJsonPath = checkNotNull(injectionUrlJsonPath);
return this;
}

public PrnfsNotificationBuilder withInjectionUrlXPath(String injectionUrlXPath) {
this.injectionUrlXPath = checkNotNull(injectionUrlXPath);
return this;
}

public PrnfsNotificationBuilder withInjectionUrlType(INEJCTION_TYPE injectionUrlType) {
this.injectionUrlType = injectionUrlType;
return this;
}

public PrnfsNotificationBuilder withPassword(String password) {
this.password = checkNotNull(password);
return this;
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/se/bjurr/prnfs/settings/SettingsStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,6 @@ public static PrnfsNotification getPrnfsNotification(AdminFormValues adminFormVa
prnfsNotificationBuilder.withInjectionUrl(find(adminFormValues,
predicate(AdminFormValues.FIELDS.injection_url.name())).get(VALUE));
}
if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.injection_url_xpath.name())).isPresent()) {
prnfsNotificationBuilder.withInjectionUrlXPath(find(adminFormValues,
predicate(AdminFormValues.FIELDS.injection_url_xpath.name())).get(VALUE));
}
if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.injection_url_type.name())).isPresent()) {
prnfsNotificationBuilder.withInjectionUrlType(AdminFormValues.INEJCTION_TYPE.valueOf(find(adminFormValues,
predicate(AdminFormValues.FIELDS.injection_url_type.name())).get(VALUE)));
}
return prnfsNotificationBuilder.build();
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fieldset {

legend {
font-weight: bold;
font-size: 20px;
}

.left {
Expand Down
18 changes: 11 additions & 7 deletions src/main/resources/admin.vm
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@
<input type="hidden" name="FORM_TYPE" value="BUTTON_CONFIG_FORM">
<div>
<fieldset>
<legend>Trigger notification -button in pull request view</legend>
<legend>Trigger button</legend>
<p>
Button in pull request view.
</p>
<label>Text to display on button<input type="text" name="button_title"></label>
</fieldset>
</div>
Expand Down Expand Up @@ -130,7 +133,10 @@
</div>
<div>
<fieldset>
<legend>Filter (Optional). Variables above are available.</legend>
<legend>Filter (Optional)</legend>
<p>
Variables above are available.
</p>
<label>Filter string<input type="text" name="filter_string"><br/></label>
<div class="error filter_string"></div>
<label>Filter regexp<input type="text" name="filter_regexp"><br></label>
Expand Down Expand Up @@ -162,11 +168,9 @@
<fieldset>
<legend>Injection URL (Optional)</legend>
<label>URL<input type="text" name="injection_url"></label>

<label><input type="radio" name="injection_url_type" value="XPATH" onClick="jQuery('.injection_url_type_XPATH').show()">XPath</label><br>
<input class="visibleif injection_url_type_XPATH" type="text" name="injection_url_xpath">

<label><input type="radio" name="injection_url_type" value="RAW" onClick="jQuery('.injection_url_type_XPATH').hide()">RAW</label>
<p>
The (GET) response from this URL will be available in the ${INJECTION_URL_VALUE} variable.
</p>
</fieldset>
</div>
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import static se.bjurr.prnfs.admin.AdminFormValues.FIELDS.header_name;
import static se.bjurr.prnfs.admin.AdminFormValues.FIELDS.header_value;
import static se.bjurr.prnfs.admin.AdminFormValues.FIELDS.injection_url;
import static se.bjurr.prnfs.admin.AdminFormValues.FIELDS.injection_url_type;
import static se.bjurr.prnfs.admin.AdminFormValues.FIELDS.injection_url_xpath;
import static se.bjurr.prnfs.admin.AdminFormValues.FIELDS.method;
import static se.bjurr.prnfs.admin.AdminFormValues.FIELDS.password;
import static se.bjurr.prnfs.admin.AdminFormValues.FIELDS.post_content;
Expand All @@ -36,8 +34,6 @@
import static se.bjurr.prnfs.admin.AdminFormValues.FIELDS.user;
import static se.bjurr.prnfs.admin.AdminFormValues.FORM_TYPE.BUTTON_CONFIG_FORM;
import static se.bjurr.prnfs.admin.AdminFormValues.FORM_TYPE.TRIGGER_CONFIG_FORM;
import static se.bjurr.prnfs.admin.AdminFormValues.INEJCTION_TYPE.RAW;
import static se.bjurr.prnfs.admin.AdminFormValues.INEJCTION_TYPE.XPATH;
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;
Expand Down Expand Up @@ -76,7 +72,6 @@

import org.junit.Test;

import se.bjurr.prnfs.admin.AdminFormValues.FIELDS;
import se.bjurr.prnfs.admin.utils.PrnfsTestBuilder;
import se.bjurr.prnfs.admin.utils.PullRequestEventBuilder;
import se.bjurr.prnfs.admin.utils.PullRequestRefBuilder;
Expand Down Expand Up @@ -1604,11 +1599,10 @@ public void testThatValueFromUrlCanBeUsedInInvocation() throws Exception {
.withFieldValue(events, OPENED.name()) //
.withFieldValue(FORM_TYPE, TRIGGER_CONFIG_FORM.name()) //
.withFieldValue(injection_url, "http://bjurr.se/get") //
.withFieldValue(FIELDS.injection_url_type, RAW.name()) //
.build() //
) //
.store() //
.withResponse("http://bjurr.se/get", "some content") //
.withResponse("http://bjurr.se/get", " \n some content \n ") //
.trigger( //
pullRequestEventBuilder() //
.withPullRequestAction(OPENED) //
Expand All @@ -1617,32 +1611,6 @@ public void testThatValueFromUrlCanBeUsedInInvocation() throws Exception {
.invokedOnlyUrl("http://bjurr.se/?some%20content");
}

@Test
public void testThatValueFromUrlXPathCanBeUsedInInvocation() throws Exception {
prnfsTestBuilder() //
.isLoggedInAsAdmin() //
.withNotification( //
notificationBuilder() //
.withFieldValue(url, "http://bjurr.se/?${" + INJECTION_URL_VALUE + "}") //
.withFieldValue(events, OPENED.name()) //
.withFieldValue(FORM_TYPE, TRIGGER_CONFIG_FORM.name()) //
.withFieldValue(injection_url, "http://bjurr.se/get") //
.withFieldValue(injection_url_type, XPATH.name()) //
.withFieldValue(injection_url_xpath, "concat(//crumbRequestField,\":\",//crumb)") //
.build() //
) //
.store() //
.withResponse(
"http://bjurr.se/get",
"<defaultCrumbIssuer><crumb>11d72c5cec68eaad9acf23a66f3576d2</crumb><crumbRequestField>.crumb</crumbRequestField></defaultCrumbIssuer>") //
.trigger( //
pullRequestEventBuilder() //
.withPullRequestAction(OPENED) //
.build() //
) //
.invokedOnlyUrl("http://bjurr.se/?.crumb:11d72c5cec68eaad9acf23a66f3576d2");
}

@Test
public void testThatProxyMayNotBeUsedWhenInvokingUrl() throws Exception {
prnfsTestBuilder()//
Expand Down

0 comments on commit defa101

Please sign in to comment.