1- define ( 'plugin/prnfb/pr-triggerbutton' , [
1+ require ( [
2+ '@atlassian/clientside-extensions-registry' ,
23 'jquery' ,
34 '@atlassian/aui' ,
4- 'bitbucket/util/state' ,
55 'underscore' ,
66 'plugin/prnfb/3rdparty' ,
7- 'wrm/context-path'
8- ] , function ( $ , AJS , pageState , _ , thirdParty , contextPath ) {
9- var buttonsAdminUrl = contextPath + "/rest/prnfb-admin/1.0/settings/buttons" ;
7+ 'bitbucket/util/server' ,
8+ '@bitbucket/apps/pull-requests/initial-data'
9+ ] , function ( registry , $ , AJS , _ , thirdParty , srv , prData ) {
10+ var prId = prData . pullRequest . id
11+ var repoId = prData . repository . id
12+ var buttonsAdminUrl = "/rest/prnfb-admin/1.0/settings/buttons" ;
1013
1114 var waiting = '<span class="aui-icon aui-icon-wait aui-icon-small">Wait</span>' ;
12- var $buttonArea = $ ( '#pull-request-header-more' ) . find ( '.aui-button' ) . first ( ) . closest ( 'ul' ) ;
13- $buttonArea . find ( '.aui-button' ) . each ( function ( index , auiButton ) {
14- var buttonText = $ ( auiButton ) . text ( ) . trim ( ) ;
15- if ( buttonText === '' || buttonText === 'pr-triggerbutton' ) {
16- //An empty button is added by 'client-web-item' in atlassian-plugin.xml
17- $ ( auiButton ) . remove ( ) ;
18- }
19- } ) ;
20-
21- var buttonTemplate = function ( name ) {
22- return $ ( '<li><button class="aui-button aui-button-link prnfb-button" role="menuitem">' + name + '</button></li>' ) ;
23- } ;
2415
2516 var dialogTemplate = function ( name , content ) {
2617 var escapedName = _ . escape ( name ) ;
@@ -185,7 +176,7 @@ define('plugin/prnfb/pr-triggerbutton', [
185176 } ) ;
186177 } else {
187178 AJS . flag ( {
188- close : 'auto ' ,
179+ close : 'manual ' ,
189180 type : 'error' ,
190181 title : notificationResponse . notificationName . replace ( / < s c r i p t > / g, 'script' ) ,
191182 body : '<p>' + notificationResponse . status + ' ' + notificationResponse . uri + '</p>' +
@@ -205,106 +196,81 @@ define('plugin/prnfb/pr-triggerbutton', [
205196 }
206197 } ;
207198
208- function loadSettingsAndShowButtons ( ) {
209- $ . get ( buttonsAdminUrl + '/repository/' + pageState . getRepository ( ) . id + '/pullrequest/' + pageState . getPullRequest ( ) . id , function ( settings ) {
210- $buttonArea . find ( '.prnfb-button' ) . remove ( ) ;
211- settings . forEach ( function ( item ) {
212- var $buttonDropdownItem = buttonTemplate ( item . name . replace ( / < s c r i p t > / g, 'script' ) ) ;
213- $buttonDropdownItem . click ( function ( ) {
214- var $this = $ ( this ) ;
215-
216- var enableButton = function ( ) {
217- $this . removeAttr ( "disabled" ) ;
218- $this . removeAttr ( "aria-disabled" ) ;
219- $this . find ( "span" ) . remove ( ) ;
220- } ;
221- var disableButton = function ( ) {
222- $this . attr ( "disabled" , "disabled" ) ;
223- $this . attr ( "aria-disabled" , "true" ) ;
224- $this . prepend ( waiting ) ;
225- } ;
226-
227- var submitButton = function ( formResult ) {
228- disableButton ( ) ;
229- $ . ajax ( {
230- "type" : "POST" ,
231- "url" : buttonsAdminUrl + '/' + item . uuid + '/press/repository/' + pageState . getRepository ( ) . id + '/pullrequest/' + pageState . getPullRequest ( ) . id ,
232- "data" : {
233- "form" : formResult
234- } ,
235- "success" : function ( content ) {
236- setTimeout ( function ( ) {
237- enableButton ( ) ;
199+ function submitButton ( item , formResult ) {
200+ srv . ajax ( {
201+ "type" : "POST" ,
202+ "url" : buttonsAdminUrl + '/' + item . uuid + '/press/repository/' + repoId + '/pullrequest/' + prId ,
203+ "data" : {
204+ "form" : formResult
205+ } ,
206+ "success" : function ( content ) {
207+ setTimeout ( function ( ) {
238208 if ( content . confirmation == "on" ) {
239- presentResult ( content . notificationResponses ) ;
209+ presentResult ( content . notificationResponses ) ;
240210 }
241- } , 500 ) ;
242- } ,
243- "error" : function ( content ) {
244- enableButton ( ) ;
245- AJS . flag ( {
246- close : 'auto' ,
211+ } , 500 ) ;
212+ } ,
213+ "error" : function ( content ) {
214+ AJS . flag ( {
215+ close : 'manual' ,
247216 type : 'error' ,
248217 title : "Unknown error" ,
249218 body : '<p>' + content . status + '</p>' + '<p>Check the Bitbucket Server log for more details.</p>'
250- } ) ;
251- }
252- } ) ;
253-
254- if ( item . redirectUrl ) {
255- redirect ( ) ;
256- }
257- } ;
258-
259- var redirect = function ( ) {
260- disableButton ( ) ;
261- window . location . replace ( item . redirectUrl ) ;
262- } ;
263-
264- if ( item . confirmationText || item . buttonFormList && item . buttonFormList . length > 0 ) {
265- // Create the form and dialog
266- var confirmationText = confirmationTextTemplate ( item . confirmationText ) ;
267- var form = formTemplate ( item . buttonFormList ) ;
268- var formHtml = $ ( "<div/>" ) . append ( confirmationText ) . append ( form ) . html ( ) ;
269- var $dialog = $ ( dialogTemplate ( item . name , formHtml ) ) ;
270- $dialog . appendTo ( $ ( "body" ) ) ;
219+ } ) ;
220+ }
221+ } ) ;
271222
272- var dialogRef = AJS . dialog2 ( $dialog ) ;
223+ if ( item . redirectUrl ) {
224+ window . location . replace ( item . redirectUrl ) ;
225+ }
226+ }
273227
274- // When you submit the form, we will post to the server with all the
275- // form data.
276- AJS . $ ( "#dialog-submit-button" ) . click ( function ( e ) {
277- var formResult = $dialog . find ( "form" ) . serializeJSON ( ) ;
278- e . preventDefault ( ) ;
279- dialogRef . hide ( ) ;
228+ function loadSettingsAndShowButtons ( ) {
229+ srv . rest ( {
230+ url : buttonsAdminUrl + '/repository/' + repoId + '/pullrequest/' + prId ,
231+ success : function ( settings ) {
232+ settings . forEach ( function ( item , index ) {
233+ registry . registerExtension (
234+ 'se.bjurr.prnfs.pull-request-notifier-for-stash:custom-buttons' + index ,
235+ function buttonFactory ( extensionAPI , context ) {
236+ return {
237+ type : 'button' ,
238+ label : item . name ,
239+ onAction : function ( ) {
240+ if ( item . confirmationText || item . buttonFormList && item . buttonFormList . length > 0 ) {
241+ // Create the form and dialog
242+ var confirmationText = confirmationTextTemplate ( item . confirmationText ) ;
243+ var form = formTemplate ( item . buttonFormList ) ;
244+ var formHtml = $ ( "<div/>" ) . append ( confirmationText ) . append ( form ) . html ( ) ;
245+ var $dialog = $ ( dialogTemplate ( item . name , formHtml ) ) ;
246+ $dialog . appendTo ( $ ( "body" ) ) ;
280247
281- submitButton ( formResult ) ;
282- } ) ;
283- AJS . $ ( "#dialog-close-button" ) . click ( function ( e ) {
284- e . preventDefault ( ) ;
285- dialogRef . hide ( ) ;
286- } ) ;
287- dialogRef . show ( ) ;
288- } else {
289- submitButton ( null ) ;
290- }
248+ var dialogRef = AJS . dialog2 ( $dialog ) ;
291249
292- } ) ;
293- $buttonArea . append ( $buttonDropdownItem ) ;
294- } ) ;
250+ // When you submit the form, we will post to the server with all the form data.
251+ AJS . $ ( "#dialog-submit-button" ) . click ( function ( e ) {
252+ var formResult = $dialog . find ( "form" ) . serializeJSON ( ) ;
253+ e . preventDefault ( ) ;
254+ dialogRef . hide ( ) ;
255+ submitButton ( item , formResult ) ;
256+ } ) ;
257+ AJS . $ ( "#dialog-close-button" ) . click ( function ( e ) {
258+ e . preventDefault ( ) ;
259+ dialogRef . hide ( ) ;
260+ } ) ;
261+ dialogRef . show ( ) ;
262+ } else {
263+ submitButton ( item , null ) ;
264+ }
265+ }
266+ } ;
267+ }
268+ ) ;
269+ } ) ;
270+ }
295271 } ) ;
296272 }
297273
298274 loadSettingsAndShowButtons ( ) ;
299275
300- //If a reviewer approves the PR, then a button may become visible
301- $ ( '.aui-button.approve' ) . click ( function ( ) {
302- setTimeout ( function ( ) {
303- loadSettingsAndShowButtons ( ) ;
304- } , 1000 ) ;
305- } ) ;
306- } ) ;
307-
308- AJS . $ ( document ) . ready ( function ( ) {
309- require ( 'plugin/prnfb/pr-triggerbutton' ) ;
310276} ) ;
0 commit comments