From b87776dd7d9b43d706cdecdfbc88a55ca0b25906 Mon Sep 17 00:00:00 2001 From: Yaroslav Afenkin <91559310+yaroslavafenkin@users.noreply.github.com> Date: Tue, 12 Nov 2024 17:57:38 +0200 Subject: [PATCH 1/2] [JENKINS-74067] Extract inline JavaScript from `BuildMonitorView/index.jelly` --- .../BuildMonitorView/collect-usage-stats.js | 31 ++++++ .../buildmonitor/BuildMonitorView/index.jelly | 94 +++---------------- .../BuildMonitorView/init-build-monitor.js | 19 ++++ .../BuildMonitorView/replace-stapler-proxy.js | 26 +++++ .../BuildMonitorView/restore-stapler-proxy.js | 7 ++ 5 files changed, 97 insertions(+), 80 deletions(-) create mode 100644 build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/collect-usage-stats.js create mode 100644 build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/init-build-monitor.js create mode 100644 build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/replace-stapler-proxy.js create mode 100644 build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/restore-stapler-proxy.js diff --git a/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/collect-usage-stats.js b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/collect-usage-stats.js new file mode 100644 index 00000000..764510b5 --- /dev/null +++ b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/collect-usage-stats.js @@ -0,0 +1,31 @@ +window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; + +const { + buildMonitorVersion, + jenkinsVersion, + installationSize, + itemsSize, + installationAudience, + anonymousCorrelationId +} = document.querySelector('.build-monitor-ga-data-holder').dataset; + +ga('create', 'UA-61694827-4', 'auto', { + 'userId': anonymousCorrelationId, + 'sampleRate': 1 +}); + +ga('set', { + 'forceSSL': true, + 'appName': 'Build Monitor', + 'appId': 'build-monitor-plugin', + + 'appVersion': buildMonitorVersion, + 'appInstallerId': jenkinsVersion, + + 'dimension1': installationSize, + 'dimension2': itemsSize, + 'dimension3': installationAudience, + 'dimension4': anonymousCorrelationId +}); + +ga('send', 'screenview', {screenName: 'Dashboard'}); diff --git a/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/index.jelly b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/index.jelly index 39ab2634..a5a30cab 100644 --- a/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/index.jelly +++ b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/index.jelly @@ -55,30 +55,14 @@ - + + @@ -128,44 +112,9 @@ - + - + @@ -180,26 +129,11 @@ - + + diff --git a/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/init-build-monitor.js b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/init-build-monitor.js new file mode 100644 index 00000000..2ac0f4a8 --- /dev/null +++ b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/init-build-monitor.js @@ -0,0 +1,19 @@ +'use strict'; + +(function() { + const { buildMonitorVersion, csrfCrumbFieldName } = document.querySelector(".build-monitor-data-holder").dataset; + + angular + .module('buildMonitor') + .constant('BUILD_MONITOR_VERSION', buildMonitorVersion) + .constant('CSRF_CRUMB_FIELD_NAME', csrfCrumbFieldName) + .config(function(proxyProvider, cookieJarProvider, hashCodeProvider) { + var hashCodeOf = hashCodeProvider.hashCodeOf; + + proxyProvider.configureProxiesUsing(window.bindings); + + cookieJarProvider.describe({ + label: 'buildMonitor.' + hashCodeOf(document.body.dataset.displayName) + }); + }); +})(); diff --git a/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/replace-stapler-proxy.js b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/replace-stapler-proxy.js new file mode 100644 index 00000000..ae3a4d50 --- /dev/null +++ b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/replace-stapler-proxy.js @@ -0,0 +1,26 @@ +/* + * todo: (13.08.2013) Replace the below workaround with a custom Jelly tag (ExposeBindTag) + * extending either org.kohsuke.stapler.jelly.BindTag or AbstractStaplerTag, + * that would supersede currently defective BindTag implementation: + * - https://groups.google.com/forum/#!topic/jenkinsci-dev/S9bhX4ts0g4 + * - https://issues.jenkins-ci.org/browse/JENKINS-18641 + * + * Defect in BindTag manifests itself by causing a JavaScript error and preventing scripts after + * the <st:bind> invocation from executing, which results in an "empty Build Monitor". + * The issue occurs on Jenkins 1.521-1.526, only if the jQuery plugin is used. + * + * Motivation behind a custom Jelly tag: + * Original implementation of the BindTag doesn't provide an easy way of handling AJAX errors, + * which may happen if a network connection is lost or when Jenkins is restarted (which then makes + * Stapler's binding hash obsolete and Jenkins return 404 for any subsequent requests). + * + * Custom Jelly tag should generate a JSON object exposing the binding, leaving the implementation + * of the proxy to the Developer. It makes more sense for a developer to require a binding adapter + * implementation specific to their JavaScript framework of choice, rather than for Stapler to try + * to predict what JavaScript libraries will ever be used with it in the future... + */ +window.originalMakeStaplerProxy = window.makeStaplerProxy; +window.makeStaplerProxy = function(url, crumb, methods) { + return { url, crumb, methods } +}; +window.bindings = {}; diff --git a/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/restore-stapler-proxy.js b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/restore-stapler-proxy.js new file mode 100644 index 00000000..297ba8ef --- /dev/null +++ b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/restore-stapler-proxy.js @@ -0,0 +1,7 @@ +window.bindings['buildMonitor'] = buildMonitorBind; +window.makeStaplerProxy = window.originalMakeStaplerProxy; +try { + delete window.originalMakeStaplerProxy; +} catch(e) { + window["originalMakeStaplerProxy"] = undefined; +} From f648f11eb046968b98c6cfa4948d8ec576bfd12f Mon Sep 17 00:00:00 2001 From: Yaroslav Afenkin <91559310+yaroslavafenkin@users.noreply.github.com> Date: Wed, 13 Nov 2024 12:49:48 +0200 Subject: [PATCH 2/2] Address https://github.com/jenkinsci/build-monitor-plugin/pull/1034#discussion_r1838516617 --- .../BuildMonitorView/collect-usage-stats.js | 52 ++++++++++--------- .../buildmonitor/BuildMonitorView/index.jelly | 16 +++--- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/collect-usage-stats.js b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/collect-usage-stats.js index 764510b5..f44981e2 100644 --- a/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/collect-usage-stats.js +++ b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/collect-usage-stats.js @@ -1,31 +1,33 @@ -window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; +window.addEventListener("DOMContentLoaded", () => { + window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; -const { - buildMonitorVersion, - jenkinsVersion, - installationSize, - itemsSize, - installationAudience, - anonymousCorrelationId -} = document.querySelector('.build-monitor-ga-data-holder').dataset; + const { + buildMonitorVersion, + jenkinsVersion, + installationSize, + itemsSize, + installationAudience, + anonymousCorrelationId + } = document.querySelector('.build-monitor-ga-data-holder').dataset; -ga('create', 'UA-61694827-4', 'auto', { - 'userId': anonymousCorrelationId, - 'sampleRate': 1 -}); + ga('create', 'UA-61694827-4', 'auto', { + 'userId': anonymousCorrelationId, + 'sampleRate': 1 + }); -ga('set', { - 'forceSSL': true, - 'appName': 'Build Monitor', - 'appId': 'build-monitor-plugin', + ga('set', { + 'forceSSL': true, + 'appName': 'Build Monitor', + 'appId': 'build-monitor-plugin', - 'appVersion': buildMonitorVersion, - 'appInstallerId': jenkinsVersion, + 'appVersion': buildMonitorVersion, + 'appInstallerId': jenkinsVersion, - 'dimension1': installationSize, - 'dimension2': itemsSize, - 'dimension3': installationAudience, - 'dimension4': anonymousCorrelationId -}); + 'dimension1': installationSize, + 'dimension2': itemsSize, + 'dimension3': installationAudience, + 'dimension4': anonymousCorrelationId + }); -ga('send', 'screenview', {screenName: 'Dashboard'}); + ga('send', 'screenview', {screenName: 'Dashboard'}); +}); diff --git a/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/index.jelly b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/index.jelly index a5a30cab..3a4895cb 100644 --- a/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/index.jelly +++ b/build-monitor-plugin/src/main/resources/com/smartcodeltd/jenkinsci/plugins/buildmonitor/BuildMonitorView/index.jelly @@ -55,13 +55,6 @@ - @@ -76,6 +69,15 @@ Jenkins

${it.title}

+ + +