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

Commit

Permalink
Adding buttons after refactoring #109
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed May 8, 2016
1 parent d937d3c commit 5a16a69
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 55 deletions.
35 changes: 11 additions & 24 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,26 @@ Changelog of Pull Request Notifier for Bitbucket.

## Unreleased
### GitHub [#109](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/109) Refactor admin pages
Adding migration code for old settings format
Adding buttons after refactoring

[103427258c50c4b](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/103427258c50c4b) Tomas Bjerre *2016-05-08 11:55:28*
[4647eea2af4d8f7](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/4647eea2af4d8f7) Tomas Bjerre *2016-05-08 20:22:34*

Admin GUI
Adding migration code for old settings format

[d937d3cc18bdfd0](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/d937d3cc18bdfd0) Tomas Bjerre *2016-05-08 13:08:07*

* Also adding EVERYTHING_URL variable . Resolved to every other variable.
Admin GUI

[da4e64ab06398ff](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/da4e64ab06398ff) Tomas Bjerre *2016-05-08 08:16:03*
[319f5de5c123e06](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/319f5de5c123e06) Tomas Bjerre *2016-05-08 13:07:55*

Refactoring

[3e1fddaecb4aab8](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/3e1fddaecb4aab8) Tomas Bjerre *2016-05-03 16:01:52*

### GitHub [#114](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/114) variable which contains all valid variables?
Admin GUI

* Also adding EVERYTHING_URL variable . Resolved to every other variable.

[da4e64ab06398ff](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/da4e64ab06398ff) Tomas Bjerre *2016-05-08 08:16:03*

### GitHub [#25](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/25) Enable configuration in per-repository hook screen
Admin GUI

* Also adding EVERYTHING_URL variable . Resolved to every other variable.
Admin GUI

[da4e64ab06398ff](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/da4e64ab06398ff) Tomas Bjerre *2016-05-08 08:16:03*
[319f5de5c123e06](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/319f5de5c123e06) Tomas Bjerre *2016-05-08 13:07:55*

Adding repo and global admin pages

Expand All @@ -41,17 +34,11 @@ Changelog of Pull Request Notifier for Bitbucket.
[1fc12a72100afe8](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/1fc12a72100afe8) Tomas Bjerre *2016-05-03 16:44:25*

### GitHub [#55](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/55) remove width limit
Admin GUI

* Also adding EVERYTHING_URL variable . Resolved to every other variable.
Admin GUI

[da4e64ab06398ff](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/da4e64ab06398ff) Tomas Bjerre *2016-05-08 08:16:03*
[319f5de5c123e06](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/319f5de5c123e06) Tomas Bjerre *2016-05-08 13:07:55*

### No issue
Adding migration code for old settings format

[515086b440e76de](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/515086b440e76de) Tomas Bjerre *2016-05-08 08:48:18*

changelog maven plugin 1.29

[25ccd05b465bd4a](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/25ccd05b465bd4a) Tomas Bjerre *2016-04-14 16:36:16*
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Some rest resources are available. You can figure out the JSON structure by look
* `GET /projectKey/{projectKey}` Get all buttons for the project.
* `GET /projectKey/{projectKey}/repositorySlug/{repositorySlug}` Get all buttons for the project and repository.
* `POST` Save a button.
* `POST /press` Press the button.
* `POST {uuid}/press/repository/{repositoryId}/pullrequest/{pullRequestId}` Press the button.

### Jenkins
Parameterized Jenkins jobs can be triggered remotely via:
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/se/bjurr/prnfb/presentation/ButtonServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import se.bjurr.prnfb.presentation.dto.ButtonDTO;
Expand Down Expand Up @@ -128,11 +127,11 @@ public Response get(@PathParam("uuid") UUID uuid) {
}

@POST
@Path("/press")
@Path("{uuid}/press/repository/{repositoryId}/pullrequest/{pullRequestId}")
@XsrfProtectionExcluded
@Produces(APPLICATION_JSON)
public Response press(@QueryParam("repositoryId") Integer repositoryId,
@QueryParam("pullRequestId") Long pullRequestId, @QueryParam("uuid") final UUID buttionUuid) {
public Response press(@PathParam("repositoryId") Integer repositoryId, @PathParam("pullRequestId") Long pullRequestId,
@PathParam("uuid") final UUID buttionUuid) {
PrnfbButton button = this.settingsService.getButton(buttionUuid);
if (!this.userCheckService.isAllowedUseButton(button)) {
return status(UNAUTHORIZED).build();
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/se/bjurr/prnfb/service/PrnfbRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.atlassian.bitbucket.repository.RepositoryService;
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Supplier;

public class PrnfbRenderer {
Expand Down Expand Up @@ -64,12 +65,7 @@ private String getRenderedString(String string, Boolean forUrl, ClientKeyStore c
String resolved = variable.resolve(this.pullRequest, this.pullRequestAction, this.applicationUser,
this.repositoryService, this.propertiesService, this.prnfbNotification, this.variables, clientKeyStore,
shouldAcceptAnyCertificate);
string = string.replaceAll(regExpStr, forUrl ? encode(resolved, UTF_8.name()) : resolved);
return string;
}

private String regexp(PrnfbVariable variable) {
return "\\$\\{" + variable.name() + "\\}";
return getRenderedStringResolved(string, forUrl, regExpStr, resolved);
}

private String renderVariable(String string, Boolean forUrl, ClientKeyStore clientKeyStore,
Expand All @@ -84,4 +80,21 @@ private String renderVariable(String string, Boolean forUrl, ClientKeyStore clie
}
return string;
}

@VisibleForTesting
String getRenderedStringResolved(String string, Boolean forUrl, final String regExpStr, String resolved)
throws UnsupportedEncodingException {
String replaceWith = forUrl ? encode(resolved, UTF_8.name()) : resolved;
try {
string = string.replaceAll(regExpStr, replaceWith);
} catch (IllegalArgumentException e) {
throw new RuntimeException("Tried to replace " + regExpStr + " with " + replaceWith, e);
}
return string;
}

@VisibleForTesting
String regexp(PrnfbVariable variable) {
return "\\$\\{" + variable.name() + "\\}";
}
}
2 changes: 1 addition & 1 deletion src/main/java/se/bjurr/prnfb/service/PrnfbVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String resolve(PullRequest pullRequest, PrnfbPullRequestAction pullReques
List<String> parts = newArrayList();
for (PrnfbVariable v : PrnfbVariable.values()) {
if (v != EVERYTHING_URL) {
parts.add(v.name() + "=${" + v.name() + "}");
parts.add(v.name() + "=\\${" + v.name() + "}");
}
}
return on('&').join(parts);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/se/bjurr/prnfb/service/SettingsService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package se.bjurr.prnfb.service;

import static com.atlassian.bitbucket.permission.Permission.ADMIN;
import static com.google.common.base.Joiner.on;
import static com.google.common.base.Predicates.not;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.filter;
Expand Down Expand Up @@ -111,7 +112,11 @@ public Optional<PrnfbNotification> findNotification(UUID notificationUuid) {
}

public PrnfbButton getButton(UUID buttionUuid) {
return find(getButtons(), withUuid(buttionUuid));
Optional<PrnfbButton> foundOpt = findButton(buttionUuid);
if (!foundOpt.isPresent()) {
throw new RuntimeException(buttionUuid + " not fond in:\n" + on('\n').join(getButtons()));
}
return foundOpt.get();
}

public List<PrnfbButton> getButtons() {
Expand Down
13 changes: 5 additions & 8 deletions src/main/resources/admin.vm
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,15 @@
</legend>
<div class="radio">
<label>
<input class="radio" type="radio" name="ALWAYS"> trigger for any merge condition</label>
<input class="radio" type="radio" name="triggerIfCanMerge" value="ALWAYS"> trigger for any merge condition</label>
</div>
<div class="radio">
<label>
<input class="radio" type="radio" name="CONFLICTING"> trigger only when merge is conflicting</label>
<input class="radio" type="radio" name="triggerIfCanMerge" value="CONFLICTING"> trigger only when merge is conflicting</label>
</div>
<div class="radio">
<label>
<input class="radio" type="radio" name="NOT_CONFLICTING"> trigger only when merge is not conflicting</label>
<input class="radio" type="radio" name="triggerIfCanMerge" value="NOT_CONFLICTING"> trigger only when merge is not conflicting</label>
</div>
</fieldset>

Expand Down Expand Up @@ -482,11 +482,8 @@
<input class="text text-field" type="text" name="headers[][value]" value="{value}">
</div>
</script>
<div class="listfields template" data-template="header-template" data-field="headers" data-target=".listfields">
<div class="field-group listfield">
<input class="text text-field" type="text" name="headers[][name]">
<input class="text text-field" type="text" name="headers[][value]">
</div>
<div class="listfields template" data-template="header-template" data-field="headers" data-target=".listfields" data-empty="{'name':'','value':''}">

</div>
</fieldset>

Expand Down
10 changes: 4 additions & 6 deletions src/main/resources/pr-triggerbutton.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ define('plugin/prnfb/pr-triggerbutton', [
'bitbucket/util/state'
], function($, AJS, pageState) {

var getResourceUrl = function() {
return AJS.contextPath() + '/rest/prnfb-admin/1.0/manual/?repositoryId=' + pageState.getRepository().id + '&pullRequestId=' + pageState.getPullRequest().id;
};
var buttonsAdminUrl = AJS.contextPath() + "/rest/prnfb-admin/1.0/settings/buttons"; 

var waiting = '<span class="aui-icon aui-icon-wait aui-icon-small">Wait</span>';

Expand All @@ -30,18 +28,18 @@ define('plugin/prnfb/pr-triggerbutton', [
function loadSettingsAndShowButtons() {
var hasButtons = false;
$buttonDropdownItems.empty();
$.get(getResourceUrl(), function(settings) {
$.get(buttonsAdminUrl + '/repository/' + pageState.getRepository().id + '/pullrequest/' + pageState.getPullRequest().id, function(settings) {
settings.forEach(function(item) {
hasButtons = true;

var $buttonDropdownItem = $('<li><a class="aui-icon-container" href="#">' + item.title + '</a></li>');
var $buttonDropdownItem = $('<li><a class="aui-icon-container" href="#">' + item.name + '</a></li>');
$buttonDropdownItem.find("a").click(function() {
var $this = $(this);
$this.attr("disabled", "disabled");
$this.attr("aria-disabled", "true");
$this.prepend(waiting);

$.post(getResourceUrl() + '&formIdentifier=' + item.formIdentifier, function() {
$.post(buttonsAdminUrl + '/' + item.uuid + '/press/repository/' + pageState.getRepository().id + '/pullrequest/' + pageState.getPullRequest().id, function() {
setTimeout(function() {
$this.removeAttr("disabled");
$this.removeAttr("aria-disabled");
Expand Down
12 changes: 10 additions & 2 deletions src/main/resources/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,15 @@ define('plugin/prnfb/utils', [
var template = $(el).data('template');
var field = $(el).data('field');
var target = $(el).data('target');
var emptyJson = $(el).data('empty').replace(/\'/g, '"');
var empty = JSON.parse(emptyJson);
var rendered = "";
for (var i = 0; i < data[field].length; i++)
rendered += AJS.template.load(template).fill(data[field][i]);
if (data[field]) {
for (var i = 0; i < data[field].length; i++) {
rendered += AJS.template.load(template).fill(data[field][i]);
}
}
rendered += AJS.template.load(template).fill(empty);
$(target).html(rendered);
});
}
Expand Down Expand Up @@ -214,6 +220,8 @@ define('plugin/prnfb/utils', [
}
});

populateForm(formSelector, {});

$(formSelector).submit(function(e) {
e.preventDefault();
postForm(restResource, formSelector, populateSelect);
Expand Down
15 changes: 14 additions & 1 deletion src/test/java/se/bjurr/prnfb/service/PrnfbRendererTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static se.bjurr.prnfb.listener.PrnfbPullRequestAction.APPROVED;
import static se.bjurr.prnfb.service.PrnfbVariable.EVERYTHING_URL;
import static se.bjurr.prnfb.service.PrnfbVariable.INJECTION_URL_VALUE;
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_COMMENT_TEXT;
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_FROM_HASH;
import static se.bjurr.prnfb.service.PrnfbVariable.PULL_REQUEST_MERGE_COMMIT;
import static se.bjurr.prnfb.settings.PrnfbNotificationBuilder.prnfbNotificationBuilder;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.junit.Before;
Expand Down Expand Up @@ -72,10 +74,21 @@ public void before() throws ValidationException {
public void invoke(UrlInvoker toInvoke) {
toInvoke.setResponseString("theResponse");
}

});
}

@Test
public void testThatEverythingCanBeRendered() throws UnsupportedEncodingException {
String actual = this.sut.getRenderedStringResolved("asd ${" + EVERYTHING_URL.name() + "} asd", this.forUrl, this.sut
.regexp(EVERYTHING_URL), EVERYTHING_URL.resolve(this.pullRequest, this.pullRequestAction, this.applicationUser,
this.repositoryService, this.propertiesService, this.prnfbNotification, this.variables, this.clientKeyStore,
this.shouldAcceptAnyCertificate));

assertThat(actual)//
.isEqualTo(
"asd BUTTON_TRIGGER_TITLE=${BUTTON_TRIGGER_TITLE}&INJECTION_URL_VALUE=${INJECTION_URL_VALUE}&PULL_REQUEST_ACTION=${PULL_REQUEST_ACTION}&PULL_REQUEST_AUTHOR_DISPLAY_NAME=${PULL_REQUEST_AUTHOR_DISPLAY_NAME}&PULL_REQUEST_AUTHOR_EMAIL=${PULL_REQUEST_AUTHOR_EMAIL}&PULL_REQUEST_AUTHOR_ID=${PULL_REQUEST_AUTHOR_ID}&PULL_REQUEST_AUTHOR_NAME=${PULL_REQUEST_AUTHOR_NAME}&PULL_REQUEST_AUTHOR_SLUG=${PULL_REQUEST_AUTHOR_SLUG}&PULL_REQUEST_COMMENT_TEXT=${PULL_REQUEST_COMMENT_TEXT}&PULL_REQUEST_FROM_BRANCH=${PULL_REQUEST_FROM_BRANCH}&PULL_REQUEST_FROM_HASH=${PULL_REQUEST_FROM_HASH}&PULL_REQUEST_FROM_HTTP_CLONE_URL=${PULL_REQUEST_FROM_HTTP_CLONE_URL}&PULL_REQUEST_FROM_ID=${PULL_REQUEST_FROM_ID}&PULL_REQUEST_FROM_REPO_ID=${PULL_REQUEST_FROM_REPO_ID}&PULL_REQUEST_FROM_REPO_NAME=${PULL_REQUEST_FROM_REPO_NAME}&PULL_REQUEST_FROM_REPO_PROJECT_ID=${PULL_REQUEST_FROM_REPO_PROJECT_ID}&PULL_REQUEST_FROM_REPO_PROJECT_KEY=${PULL_REQUEST_FROM_REPO_PROJECT_KEY}&PULL_REQUEST_FROM_REPO_SLUG=${PULL_REQUEST_FROM_REPO_SLUG}&PULL_REQUEST_FROM_SSH_CLONE_URL=${PULL_REQUEST_FROM_SSH_CLONE_URL}&PULL_REQUEST_ID=${PULL_REQUEST_ID}&PULL_REQUEST_MERGE_COMMIT=${PULL_REQUEST_MERGE_COMMIT}&PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT=${PULL_REQUEST_PARTICIPANTS_APPROVED_COUNT}&PULL_REQUEST_REVIEWERS=${PULL_REQUEST_REVIEWERS}&PULL_REQUEST_REVIEWERS_APPROVED_COUNT=${PULL_REQUEST_REVIEWERS_APPROVED_COUNT}&PULL_REQUEST_REVIEWERS_ID=${PULL_REQUEST_REVIEWERS_ID}&PULL_REQUEST_REVIEWERS_SLUG=${PULL_REQUEST_REVIEWERS_SLUG}&PULL_REQUEST_TITLE=${PULL_REQUEST_TITLE}&PULL_REQUEST_TO_BRANCH=${PULL_REQUEST_TO_BRANCH}&PULL_REQUEST_TO_HASH=${PULL_REQUEST_TO_HASH}&PULL_REQUEST_TO_HTTP_CLONE_URL=${PULL_REQUEST_TO_HTTP_CLONE_URL}&PULL_REQUEST_TO_ID=${PULL_REQUEST_TO_ID}&PULL_REQUEST_TO_REPO_ID=${PULL_REQUEST_TO_REPO_ID}&PULL_REQUEST_TO_REPO_NAME=${PULL_REQUEST_TO_REPO_NAME}&PULL_REQUEST_TO_REPO_PROJECT_ID=${PULL_REQUEST_TO_REPO_PROJECT_ID}&PULL_REQUEST_TO_REPO_PROJECT_KEY=${PULL_REQUEST_TO_REPO_PROJECT_KEY}&PULL_REQUEST_TO_REPO_SLUG=${PULL_REQUEST_TO_REPO_SLUG}&PULL_REQUEST_TO_SSH_CLONE_URL=${PULL_REQUEST_TO_SSH_CLONE_URL}&PULL_REQUEST_URL=${PULL_REQUEST_URL}&PULL_REQUEST_USER_DISPLAY_NAME=${PULL_REQUEST_USER_DISPLAY_NAME}&PULL_REQUEST_USER_EMAIL_ADDRESS=${PULL_REQUEST_USER_EMAIL_ADDRESS}&PULL_REQUEST_USER_ID=${PULL_REQUEST_USER_ID}&PULL_REQUEST_USER_NAME=${PULL_REQUEST_USER_NAME}&PULL_REQUEST_USER_SLUG=${PULL_REQUEST_USER_SLUG}&PULL_REQUEST_VERSION=${PULL_REQUEST_VERSION} asd");
}

@Test
public void testThatInjectionUrlCanBeRendered() throws ValidationException {
this.prnfbNotification = prnfbNotificationBuilder(this.prnfbNotification)//
Expand Down
Loading

0 comments on commit 5a16a69

Please sign in to comment.