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

Commit

Permalink
Adding repo and global admin pages #25
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed May 3, 2016
1 parent 1fc12a7 commit 8904d03
Show file tree
Hide file tree
Showing 10 changed files with 399 additions and 594 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ Changelog of Pull Request Notifier for Bitbucket.
### GitHub [#109](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/109) Refactor admin pages
Refactoring #109

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

### GitHub [#25](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/25) Enable configuration in per-repository hook screen
Adding repo and global admin pages #25

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

Adding project and repo filter to notification #25

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

### No issue
changelog maven plugin 1.29
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,16 @@ The filter text as well as the URL support variables. These are:
* ${PULL_REQUEST_FROM_REPO_PROJECT_ID} Example: 1
* ${PULL_REQUEST_FROM_REPO_PROJECT_KEY} Example: PROJECT_1
* ${PULL_REQUEST_FROM_REPO_SLUG} Example: rep_1
* And same variables for TO, like: ${PULL_REQUEST_TO_HASH}
* ${PULL_REQUEST_TO_SSH_CLONE_URL} Example: ssh://git@localhost:7999/project_1/rep_1
* ${PULL_REQUEST_TO_HTTP_CLONE_URL} Example: http://admin@localhost:7990/bitbucket/scm/project_1/rep_1.git
* ${PULL_REQUEST_TO_HASH} Example: 6053a1eaa1c009dd11092d09a72f3c41af1b59ad
* ${PULL_REQUEST_TO_ID} Example: refs/heads/branchmodmerge
* ${PULL_REQUEST_TO_BRANCH} Example: branchmodmerge
* ${PULL_REQUEST_TO_REPO_ID} Example: 1
* ${PULL_REQUEST_TO_REPO_NAME} Example: rep_1
* ${PULL_REQUEST_TO_REPO_PROJECT_ID} Example: 1
* ${PULL_REQUEST_TO_REPO_PROJECT_KEY} Example: PROJECT_1
* ${PULL_REQUEST_TO_REPO_SLUG} Example: rep_1

The ${PULL_REQUEST_USER...} contains information about the user who issued the event. Who commented it, who rejected it, who approved it...

Expand Down
47 changes: 44 additions & 3 deletions src/main/java/se/bjurr/prnfb/presentation/GlobalAdminServlet.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
package se.bjurr.prnfb.presentation;

import static com.google.common.base.Optional.absent;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.ImmutableMap.of;
import static com.google.common.collect.Maps.newHashMap;

import java.net.URI;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.repository.RepositoryService;
import com.atlassian.sal.api.auth.LoginUriProvider;
import com.atlassian.sal.api.user.UserManager;
import com.atlassian.sal.api.user.UserProfile;
import com.atlassian.templaterenderer.TemplateRenderer;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;

public class GlobalAdminServlet extends HttpServlet {
private static final long serialVersionUID = 3846987953228399693L;
private final LoginUriProvider loginUriProvider;
private final TemplateRenderer renderer;
private final RepositoryService repositoryService;
private final UserManager userManager;

public GlobalAdminServlet(UserManager userManager, LoginUriProvider loginUriProvider, TemplateRenderer renderer) {
public GlobalAdminServlet(UserManager userManager, LoginUriProvider loginUriProvider, TemplateRenderer renderer,
RepositoryService repositoryService) {
this.userManager = userManager;
this.loginUriProvider = loginUriProvider;
this.renderer = renderer;
this.repositoryService = repositoryService;
}

@Override
Expand All @@ -33,8 +45,20 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) {
response.sendRedirect(this.loginUriProvider.getLoginUri(getUri(request)).toASCIIString());
return;
}
response.setContentType("text/html;charset=utf-8");
this.renderer.render("admin.vm", response.getWriter());

final Optional<Repository> repository = getRepository(request.getPathInfo());
Map<String, Object> context = newHashMap();
if (repository.isPresent()) {
context = of( //
"repository", repository.orNull() //
);
}

response.setContentType("text/html;charset=UTF-8");
this.renderer.render( //
"admin.vm", //
context, //
response.getWriter());
} catch (Exception e) {
propagate(e);
}
Expand All @@ -48,4 +72,21 @@ private URI getUri(HttpServletRequest request) {
}
return URI.create(builder.toString());
}

@VisibleForTesting
Optional<Repository> getRepository(String pathInfo) {
if (pathInfo == null || !pathInfo.contains("/") || pathInfo.endsWith("prnfb/admin")
|| pathInfo.endsWith("prnfb/admin/")) {
return absent();
}
String[] components = pathInfo.split("/");
if (components.length == 0) {
return absent();
}
String project = components[components.length - 2];
String repoSlug = components[components.length - 1];
final Repository repository = checkNotNull(this.repositoryService.getBySlug(project, repoSlug), //
"Did not find " + project + " " + repoSlug);
return Optional.of(repository);
}
}
65 changes: 0 additions & 65 deletions src/main/resources/admin.css
Original file line number Diff line number Diff line change
@@ -1,65 +0,0 @@
.config-area {
border: 1px;
border-color: black;
border-style: dotted;
width: 400px;
padding: 5px 15px;
}

fieldset {
border: 1px;
padding: 5px 15px;
}

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

.left {
float: left;
}

.right {
float: right;
}

.error {
font-weight: bold;
color: red;
}

input[type="text"],
input[type="password"] {
width: 100%;
}

.prnfb > form {
margin: 10px 0 0 0;
}

.prnfb-template {
display: none;
}

.visibleif {
display: none;
}

th {
text-align: left;
}

td > input[type="text"] {
width: 90%;
margin: 0 10% 0 0;
float: left
}

.expandable .content {
display: none;
}

.expandable.expanded .content {
display: inherit;
}
173 changes: 15 additions & 158 deletions src/main/resources/admin.js
Original file line number Diff line number Diff line change
@@ -1,160 +1,17 @@
(function($) {
'use strict';
var config_resource = AJS.contextPath() + "/rest/prnfb-admin/1.0/";
$(document).ready(function() {
function getEmpties($headers) {
var empties = [];
$('.header', $headers).each(function(iheader, $header) {
var allValue = "";
$('input[type="text"]', $header).each(function(iinput, $input) {
allValue += $input.value.trim();
});
if (allValue === "") {
empties.push($header);
}
});
return empties;
}

function adjustHeaders($headers) {
var empties = getEmpties($headers);
if (empties.length === 0) {
$headers.append($(".prnfb-template .header")[0].outerHTML);
}

if (empties.length > 1) {
empties[1].remove();
}
}

function setEvents() {
$('input[name="delete"]').click(function(e) {
var $form = $(this).closest('form');
var formIdentifier = $('input[name="FORM_IDENTIFIER"]', $form).val();
$.ajax({
url: config_resource + formIdentifier,
dataType: "json",
type: "DELETE",
error: function(xhr, data, error) {
console.log(xhr);
console.log(data);
console.log(error);
},
success: function(data, text, xhr) {
$form.remove();
}
});
});

$('.expandable').each(function(index, el) {
var $element = $(el);
$element.find('.toggle').click(function() {
$element.toggleClass('expanded');
});
});
//If there are only a few triggers configured, they can be expanded by default without confusion.
if ($('.expandable').length < 4) {
$('.expandable').addClass('expanded');
}

$('.headers').keyup(function(e) {
var $headers = $(this);
adjustHeaders($headers);
});

$('input[name="save"]').click(function(e) {
var $form = $(this).closest('form');
$(".post", $form).html("Saving...");
$.ajax({
url: config_resource,
dataType: "json",
type: "POST",
contentType: "application/json",
data: JSON.stringify($form.serializeArray(), null, 2),
processData: false,
error: function(xhr, data, error) {
$(".error." + xhr.responseJSON.field, $form).html(xhr.responseJSON.error);
if (xhr.responseJSON.field) {
$(".post", $form).html("There were errors, form not saved!");
} else {
$(".post", $form).html(xhr.responseText);
}
},
success: function(data, text, xhr) {
getAll();
}
});
});
}

function addNewForm(formType) {
var $template = $(".prnfb-template-" + formType).clone();
$('input[name="delete"]', $template).remove();
$('input[name=method][value=GET]', $template).attr('checked', 'checked');
$('.expandable', $template).addClass('expanded');
$(".prnfb-" + formType).append($template.html());
}
define('plugin/prnfb/admin', [
'jquery',
'aui',
'plugin/prnfb/utils'
], function($, AJS, common) {

function getAll() {
$.ajax({
url: config_resource,
dataType: "json"
}).done(function(configs) {
$(".prnfb-TRIGGER_CONFIG_FORM").html("");
$(".prnfb-BUTTON_CONFIG_FORM").html("");
$(".prnfb-GLOBAL_SETTINGS").html("");
$.each(configs, function(index, config) {
var formType = 'TRIGGER_CONFIG_FORM';
$.each(config, function(fieldIndex, field_map) {
if (field_map.name === 'FORM_TYPE') {
formType = field_map.value;
}
});

var $template = $(".prnfb-template-" + formType).clone();

$.each(config, function(fieldIndex, field_map) {
var safe_value = field_map.value.replace(/[^a-zA-Z\_]/g, '');
$('.variable[data-variable="' + field_map.name + '"]', $template).html(field_map.value);
$('input[type="text"][name="' + field_map.name + '"]', $template).attr('value', field_map.value);
$('input[type="password"][name="' + field_map.name + '"]', $template).attr('value', field_map.value);
$('textarea[name="' + field_map.name + '"]', $template).text(field_map.value);
$('input[type="hidden"][name="' + field_map.name + '"]', $template).attr('value', field_map.value);
$('input[type="checkbox"][name="' + field_map.name + '"][value="' + safe_value + '"]', $template).attr('checked', 'checked');
$('input[type="radio"][name="' + field_map.name + '"][value="' + safe_value + '"]', $template).attr('checked', 'checked');
$('.visibleif.' + field_map.name + '_' + safe_value, $template).show();
});

var header_names = [];
var header_values = [];
$.each(config, function(fieldIndex, field_map) {
if (field_map.name === 'header_name') {
header_names.push(field_map.value);
} else if (field_map.name === 'header_value') {
header_values.push(field_map.value);
}
});
for (var i = 0; i < header_names.length; i++) {
$('input[type="text"][name="header_name"]', $template).last().attr('value', header_names[i]);
$('input[type="text"][name="header_value"]', $template).last().attr('value', header_values[i]);
adjustHeaders($(".headers", $template));
}

if (!$('input[name=method]:checked', $template).val()) {
$('input[name=method][value=GET]', $template).attr('checked', 'checked');
}

$(".prnfb-" + formType).append($template.html());
});
addNewForm('TRIGGER_CONFIG_FORM');
addNewForm('BUTTON_CONFIG_FORM');
if ($('[name="FORM_TYPE"][value="GLOBAL_SETTINGS"]').length < 2) {
addNewForm('GLOBAL_SETTINGS');
}
setEvents();
});
}

getAll();
$(document).ready(function() {
var globalRepoAdminUrl = AJS.contextPath() + "/rest/prnfb-admin/1.0"; 
$.getJSON(globalRepoAdminUrl, function(data) {
common.setupRepoSettingsForm(data);
});
});
})(AJS.$ || jQuery);
});

AJS.$(document).ready(function() {
require('plugin/prnfb/admin');
});
Loading

0 comments on commit 8904d03

Please sign in to comment.