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

Commit

Permalink
Changes for bitbucket 7+ (#372)
Browse files Browse the repository at this point in the history
Co-authored-by: sreer <[email protected]>
  • Loading branch information
sreer-pega and sreer authored Dec 5, 2020
1 parent ec66010 commit 7de2b9c
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 135 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>se.bjurr.prnfb</groupId>
<artifactId>pull-request-notifier-for-bitbucket</artifactId>
<version>4.2-SNAPSHOT</version>
<version>5.0-SNAPSHOT</version>
<organization>
<name>Tomas Bjerre AB</name>
<url>https://tomasbjerreab.se/</url>
Expand Down Expand Up @@ -332,9 +332,9 @@ Changelog of Pull Request Notifier for Bitbucket.
<changelog.version>1.60</changelog.version>
<fmt.version>2.9</fmt.version>
<violations.version>1.21</violations.version>
<bitbucket.version>6.8.0-bbs-cdn-m1</bitbucket.version>
<bitbucket.version>7.1.1</bitbucket.version>
<bitbucket.data.version>${bitbucket.version}</bitbucket.data.version>
<quick.reload.version>2.0.0</quick.reload.version>
<amps.version>8.0.2</amps.version>
</properties>
</project>
</project>
11 changes: 5 additions & 6 deletions src/main/resources/admin.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
define('plugin/prnfb/admin', [
'jquery',
'@atlassian/aui',
'plugin/prnfb/utils',
'wrm/context-path'
], function($, AJS, utils, contextPath) {
var settingsAdminUrlPostUrl = contextPath + "/rest/prnfb-admin/1.0/settings";
'plugin/prnfb/utils'
], function($, AJS, utils) {
var settingsAdminUrlPostUrl = "/rest/prnfb-admin/1.0/settings";
var settingsAdminUrl = settingsAdminUrlPostUrl;

var notificationsAdminUrlPostUrl = contextPath + "/rest/prnfb-admin/1.0/settings/notifications";
var notificationsAdminUrlPostUrl = "/rest/prnfb-admin/1.0/settings/notifications";
var notificationsAdminUrl = notificationsAdminUrlPostUrl;

var buttonsAdminUrlPostUrl = contextPath + "/rest/prnfb-admin/1.0/settings/buttons";
var buttonsAdminUrlPostUrl = "/rest/prnfb-admin/1.0/settings/buttons";
var buttonsAdminUrl = buttonsAdminUrlPostUrl;

var projectKey;
Expand Down
44 changes: 35 additions & 9 deletions src/main/resources/atlassian-plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@

<web-resource name="Admin Web Resources" key="resources">
<dependency>com.atlassian.auiplugin:ajs</dependency>
<dependency>com.atlassian.plugins.atlassian-plugins-webresource-plugin:context-path</dependency>
<dependency>com.atlassian.auiplugin:template</dependency>
<dependency>com.atlassian.bitbucket.server.bitbucket-web-api:server</dependency>
<resource type="download" name="3rdparty.js" location="3rdparty.js" />
<resource type="download" name="utils.js" location="utils.js" />
<resource type="download" name="admin.js" location="admin.js" />
Expand All @@ -58,18 +58,44 @@
</link>
</web-item>

<client-web-item key="se.bjurr.prnfb.triggerbutton" name="Trigger Notification" weight="50" section="bitbucket.pullrequest.action">
<dependency>${project.groupId}-${project.artifactId}:pr-triggerbutton</dependency>
<label key="bitbucket.web.pull-request.toolbar.triggerbutton">pr-triggerbutton</label>
</client-web-item>
<web-item key="custom-buttons0" section="bitbucket.ui.pullrequest.action">
<label key="" />
</web-item>
<web-item key="custom-buttons1" section="bitbucket.ui.pullrequest.action">
<label key="" />
</web-item>
<web-item key="custom-buttons2" section="bitbucket.ui.pullrequest.action">
<label key="" />
</web-item>
<web-item key="custom-buttons3" section="bitbucket.ui.pullrequest.action">
<label key="" />
</web-item>
<web-item key="custom-buttons4" section="bitbucket.ui.pullrequest.action">
<label key="" />
</web-item>
<web-item key="custom-buttons5" section="bitbucket.ui.pullrequest.action">
<label key="" />
</web-item>
<web-item key="custom-buttons6" section="bitbucket.ui.pullrequest.action">
<label key="" />
</web-item>
<web-item key="custom-buttons7" section="bitbucket.ui.pullrequest.action">
<label key="" />
</web-item>
<web-item key="custom-buttons8" section="bitbucket.ui.pullrequest.action">
<label key="" />
</web-item>
<web-item key="custom-buttons9" section="bitbucket.ui.pullrequest.action">
<label key="" />
</web-item>

<web-resource key="pr-triggerbutton">
<resource type="download" name="3rdparty.js" location="3rdparty.js" />
<resource type="download" name="utils.js" location="utils.js" />
<resource type="download" name="pr-triggerbutton.js" location="/pr-triggerbutton.js" />
<resource type="download" name="pr-triggerbutton.js" location="pr-triggerbutton.js" />
<dependency>com.atlassian.bitbucket.bitbucket-web-plugin:global</dependency>
<dependency>com.atlassian.auiplugin:ajs</dependency>
<dependency>com.atlassian.plugins.atlassian-plugins-webresource-plugin:context-path</dependency>
<context>bitbucket.page.pullRequest.view</context>
<dependency>com.atlassian.bitbucket.server.bitbucket-web-api:server</dependency>
<context>bitbucket.ui.pullrequest.action</context>
</web-resource>
</atlassian-plugin>
</atlassian-plugin>
178 changes: 72 additions & 106 deletions src/main/resources/pr-triggerbutton.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
define('plugin/prnfb/pr-triggerbutton', [
require([
'@atlassian/clientside-extensions-registry',
'jquery',
'@atlassian/aui',
'bitbucket/util/state',
'underscore',
'plugin/prnfb/3rdparty',
'wrm/context-path'
], function($, AJS, pageState, _, thirdParty, contextPath) {
var buttonsAdminUrl = contextPath + "/rest/prnfb-admin/1.0/settings/buttons";
'bitbucket/util/server',
'@bitbucket/apps/pull-requests/initial-data'
], function(registry, $, AJS, _, thirdParty, srv, prData) {
var prId = prData.pullRequest.id
var repoId = prData.repository.id
var buttonsAdminUrl = "/rest/prnfb-admin/1.0/settings/buttons";

var waiting = '<span class="aui-icon aui-icon-wait aui-icon-small">Wait</span>';
var $buttonArea = $('#pull-request-header-more').find('.aui-button').first().closest('ul');
$buttonArea.find('.aui-button').each(function(index, auiButton) {
var buttonText = $(auiButton).text().trim();
if (buttonText === '' || buttonText === 'pr-triggerbutton') {
//An empty button is added by 'client-web-item' in atlassian-plugin.xml
$(auiButton).remove();
}
});

var buttonTemplate = function(name) {
return $('<li><button class="aui-button aui-button-link prnfb-button" role="menuitem">' + name + '</button></li>');
};

var dialogTemplate = function(name, content) {
var escapedName = _.escape(name);
Expand Down Expand Up @@ -185,7 +176,7 @@ define('plugin/prnfb/pr-triggerbutton', [
});
} else {
AJS.flag({
close: 'auto',
close: 'manual',
type: 'error',
title: notificationResponse.notificationName.replace(/<script>/g, 'script'),
body: '<p>' + notificationResponse.status + ' ' + notificationResponse.uri + '</p>' +
Expand All @@ -205,106 +196,81 @@ define('plugin/prnfb/pr-triggerbutton', [
}
};

function loadSettingsAndShowButtons() {
$.get(buttonsAdminUrl + '/repository/' + pageState.getRepository().id + '/pullrequest/' + pageState.getPullRequest().id, function(settings) {
$buttonArea.find('.prnfb-button').remove();
settings.forEach(function(item) {
var $buttonDropdownItem = buttonTemplate(item.name.replace(/<script>/g, 'script'));
$buttonDropdownItem.click(function() {
var $this = $(this);

var enableButton = function() {
$this.removeAttr("disabled");
$this.removeAttr("aria-disabled");
$this.find("span").remove();
};
var disableButton = function() {
$this.attr("disabled", "disabled");
$this.attr("aria-disabled", "true");
$this.prepend(waiting);
};

var submitButton = function(formResult) {
disableButton();
$.ajax({
"type": "POST",
"url": buttonsAdminUrl + '/' + item.uuid + '/press/repository/' + pageState.getRepository().id + '/pullrequest/' + pageState.getPullRequest().id,
"data": {
"form": formResult
},
"success": function(content) {
setTimeout(function() {
enableButton();
function submitButton(item, formResult) {
srv.ajax({
"type": "POST",
"url": buttonsAdminUrl + '/' + item.uuid + '/press/repository/' + repoId +'/pullrequest/' + prId,
"data": {
"form": formResult
},
"success": function(content) {
setTimeout(function() {
if (content.confirmation == "on") {
presentResult(content.notificationResponses);
presentResult(content.notificationResponses);
}
}, 500);
},
"error": function(content) {
enableButton();
AJS.flag({
close: 'auto',
}, 500);
},
"error": function(content) {
AJS.flag({
close: 'manual',
type: 'error',
title: "Unknown error",
body: '<p>' + content.status + '</p>' + '<p>Check the Bitbucket Server log for more details.</p>'
});
}
});

if (item.redirectUrl) {
redirect();
}
};

var redirect = function() {
disableButton();
window.location.replace(item.redirectUrl);
};

if (item.confirmationText || item.buttonFormList && item.buttonFormList.length > 0) {
// Create the form and dialog
var confirmationText = confirmationTextTemplate(item.confirmationText);
var form = formTemplate(item.buttonFormList);
var formHtml = $("<div/>").append(confirmationText).append(form).html();
var $dialog = $(dialogTemplate(item.name, formHtml));
$dialog.appendTo($("body"));
});
}
});

var dialogRef = AJS.dialog2($dialog);
if (item.redirectUrl) {
window.location.replace(item.redirectUrl);
}
}

// When you submit the form, we will post to the server with all the
// form data.
AJS.$("#dialog-submit-button").click(function(e) {
var formResult = $dialog.find("form").serializeJSON();
e.preventDefault();
dialogRef.hide();
function loadSettingsAndShowButtons() {
srv.rest({
url : buttonsAdminUrl + '/repository/' + repoId + '/pullrequest/' + prId,
success : function(settings) {
settings.forEach(function(item, index) {
registry.registerExtension(
'se.bjurr.prnfs.pull-request-notifier-for-stash:custom-buttons' + index,
function buttonFactory(extensionAPI, context) {
return {
type: 'button',
label: item.name,
onAction : function() {
if (item.confirmationText || item.buttonFormList && item.buttonFormList.length > 0) {
// Create the form and dialog
var confirmationText = confirmationTextTemplate(item.confirmationText);
var form = formTemplate(item.buttonFormList);
var formHtml = $("<div/>").append(confirmationText).append(form).html();
var $dialog = $(dialogTemplate(item.name, formHtml));
$dialog.appendTo($("body"));

submitButton(formResult);
});
AJS.$("#dialog-close-button").click(function(e) {
e.preventDefault();
dialogRef.hide();
});
dialogRef.show();
} else {
submitButton(null);
}
var dialogRef = AJS.dialog2($dialog);

});
$buttonArea.append($buttonDropdownItem);
});
// When you submit the form, we will post to the server with all the form data.
AJS.$("#dialog-submit-button").click(function(e) {
var formResult = $dialog.find("form").serializeJSON();
e.preventDefault();
dialogRef.hide();
submitButton(item, formResult);
});
AJS.$("#dialog-close-button").click(function(e) {
e.preventDefault();
dialogRef.hide();
});
dialogRef.show();
} else {
submitButton(item, null);
}
}
};
}
);
});
}
});
}

loadSettingsAndShowButtons();

//If a reviewer approves the PR, then a button may become visible
$('.aui-button.approve').click(function() {
setTimeout(function() {
loadSettingsAndShowButtons();
}, 1000);
});
});

AJS.$(document).ready(function() {
require('plugin/prnfb/pr-triggerbutton');
});
28 changes: 17 additions & 11 deletions src/main/resources/utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
define('plugin/prnfb/utils', [
'jquery',
'plugin/prnfb/3rdparty',
'wrm/context-path'
], function($, trdparty, contextPath) {
'bitbucket/util/server'
], function($, trdparty, srv) {

function postForm(url, formSelector, whenDone) {
$('.statusresponse').empty();
var jsonString = $(formSelector).serializeJSON();
$.ajax({
srv.ajax({
url: url,
type: "POST",
contentType: "application/json; charset=utf-8",
Expand Down Expand Up @@ -60,9 +60,12 @@ define('plugin/prnfb/utils', [
}

function getProjects(whenDone) {
var projectsUrl = contextPath + "/rest/api/1.0/projects?limit=999999";
$.getJSON(projectsUrl, function(data) {
whenDone(data);
var projectsUrl = "/rest/api/1.0/projects?limit=999999";
srv.rest ({
url : projectsUrl,
success: function(data) {
whenDone(data);
}
});
}

Expand Down Expand Up @@ -91,10 +94,13 @@ define('plugin/prnfb/utils', [
whenDone();
return;
}
var reposUrl = contextPath + "/rest/api/1.0/projects/" + projectKey + "/repos?limit=999999";
$.getJSON(reposUrl, function(data) {
whenDone(data);
});
var reposUrl = "/rest/api/1.0/projects/" + projectKey + "/repos?limit=999999";
srv.rest ({
url : reposUrl,
success: function(data) {
whenDone(data);
}
});
}

function setupProjectAndRepoSettingsInForm($form, hasProjectAndRepo) {
Expand Down Expand Up @@ -249,7 +255,7 @@ define('plugin/prnfb/utils', [
e.preventDefault();
var uuid = $(formSelector).find('[name=uuid]').val();
if (uuid) {
$.ajax({
srv.ajax({
url: postUrl + '/' + uuid,
type: 'DELETE',
success: function(result) {
Expand Down

0 comments on commit 7de2b9c

Please sign in to comment.