Created
June 1, 2016 18:03
-
-
Save jhyland87/fd6657f4a9ea7429a8c2fe7d84d98f69 to your computer and use it in GitHub Desktop.
Custom jQuery code to introduce new functionality to the Jenkins UI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Jenkins Custom JavaScript/jQuery Code | |
* Author: Justin Hyland | |
* Created: 06/01/16 | |
* This file is loaded by Jenkins via the Simple Theme Plugin (https://wiki.jenkins-ci.org/display/JENKINS/Simple+Theme+Plugin) | |
* in combination with the jQuery Plugin (https://wiki.jenkins-ci.org/display/JENKINS/jQuery+Plugin), and is used to introduce any | |
* customized functionality for the UI, such as setting parameter values based on other parameter values or the job name, etc. | |
* | |
* Logic: The controller.init gets executed on document.ready, and attempts to deduce the viewers username (via the profile link | |
* in the upper right), and details about the job (via the URL). Since all jobs are separated into the appropriate folder named | |
* after the environment (in the top level), thats how the environment is retrieved, as well as the job name and folder name. | |
* Then it's easy to tell if this is a build being executed by searching for /build on the end of the URL. | |
* Once the controller.init retrieves all of the above information (known as Request Details), it iterates through the functions | |
* stored in controller.jenkinsFunctions, executing them in the order they're stored, and handing the request details as the | |
* parameter. | |
* | |
* Implemented Functionality: | |
* - Deployments.setDeployRepo gets executed for the job titled Deploy_WebApp within any environment, and simply updates the | |
* parameter named Repository whenever the Web_Application parameter is changed. The Repository value is based off of the | |
* Web_Application value. The purpose is to assist the builder in selecting the proper repository for the Web_Application being | |
* being deployed. Deploying the wrong repository to a web application would be a big problem. | |
* | |
*/ | |
(function($){ | |
$( document ).ready(function() { | |
var httpPath = window.location.pathname | |
controller.init( httpPath ) | |
}) | |
/** | |
* Main controller | |
*/ | |
var controller = { | |
/** | |
* Controller Initiation - Automatically executed when document.ready, handed the | |
* windows location pathname | |
*/ | |
init: function( httpPath ){ | |
// If the GET parameter 'debug' is set to 1 or true, or the window.debug variable is set to 1 or true, then enable debugging | |
if( utils.getParam( 'debug' ) == 'true' || utils.getParam( 'debug' ) == '1' || window.debug == true || window.debug == 1 ) | |
utils.debug = true | |
// Object to contain details about the current request (username, folder, build, etc) | |
var reqDetails = { | |
// Default the username to null, reset it if found | |
username: null | |
} | |
// Try to get the users login from the profile link | |
var $accountLink = $( 'div.login > span > a' ) | |
if( $accountLink ){ | |
if( $accountLink.attr('href') ){ | |
var linkHrefMatch = $accountLink.attr('href').match( /^\/user\/(.*)$/ ) | |
if( linkHrefMatch ){ | |
utils.console.debug( 'Username: ' + linkHrefMatch[1]) | |
reqDetails.username = linkHrefMatch[1] | |
} | |
else { | |
utils.console.debug( 'Href not matched' ) | |
} | |
} | |
else { | |
utils.console.debug( 'No href in profile link' ) | |
} | |
} | |
else { | |
utils.console.debug( 'No account link found' ) | |
} | |
// Attempt to gather as much details from the URL as possible | |
var regexPattern = /^\/job\/(Production|Development|Staging)\/job\/([a-zA-Z0-9_-]+)\/job\/([a-zA-Z0-9_-]+)\/?(build)?\/?$/, | |
pathMatch = httpPath.match( regexPattern ) | |
if( ! pathMatch ){ | |
utils.console.debug( 'Current URL path does not match regex pattern' ) | |
return | |
} | |
// Attempt to gather as much details from the URL as possible | |
reqDetails.env = pathMatch[1] | |
reqDetails.folder = pathMatch[2] | |
reqDetails.job = pathMatch[3] | |
reqDetails.build = pathMatch[4] === 'build' ? true : false | |
utils.console.debug( 'Request Details: ', reqDetails ) | |
$.each(controller.jenkinsFunctions, function( name, func ){ | |
utils.console.debug('Executing ' + name) | |
func( reqDetails ) | |
}) | |
}, | |
/** | |
* jQuery functions to be executed (In order they are listed) | |
*/ | |
jenkinsFunctions: { | |
// Sets the Repository parameter based on the Web_Application value - Should excecute for any deployment builds | |
webappDeploySetRepo: function( reqDetails ){ | |
utils.console.debug('Welcome to jenkinsFunctions.webappDeploySetRepo()! You gave me the details ', reqDetails) | |
// Only execute the setDeployRepo if the job is a Deploy_WebApp job, and we're on the build form | |
if( reqDetails.job === 'Deploy_WebApp' && reqDetails.build === true ) | |
Deployments.setDeployRepo( reqDetails ) | |
} | |
} | |
} | |
/** | |
* Extra utilities to make life easier | |
*/ | |
var utils = { | |
debug: false, | |
/** | |
* Wrapper around the console debug/warn functions | |
*/ | |
console: { | |
debug: function( ){ | |
if( utils.debug === true ) | |
console.debug.apply(console, arguments ) | |
}, | |
warn: function( str ){ | |
console.warn.apply(console, arguments ) | |
} | |
}, | |
/** | |
* Retrieve the value of a specified GET param within the URL | |
*/ | |
getParam: function(sParam) { | |
var sPageURL = decodeURIComponent(window.location.search.substring(1)), | |
sURLVariables = sPageURL.split('&'), | |
sParameterName | |
for ( var i = 0; i < sURLVariables.length; i++ ) { | |
sParameterName = sURLVariables[i].split('=') | |
if (sParameterName[0] === sParam) | |
return sParameterName[1] === undefined ? true : sParameterName[1] | |
} | |
} | |
} | |
/** | |
* Functionality for Deployment related jobs/builds | |
*/ | |
var Deployments = { | |
// Executes for Deploy_WebApp builds only - Sets the value of the Repository parameter based off of the value set in the | |
// Web_Application parameter. | |
setDeployRepo: function( reqDetails ){ | |
utils.console.debug( 'Deploying to: ', reqDetails.env ) | |
var $webappSel = $( "input[value='Web_Application']" ).next("select[name='value']" ), | |
$repoSel = $( "input[value='Repository']" ).next("select[name='value']" ), | |
setRepo | |
// Whenever the Web_Application parameter is changed, execute the below logic to decide what the repo value should be, | |
// or clear it out, if the Web_Application was also cleared | |
$webappSel.change(function() { | |
if( ! $webappSel.val() ){ | |
utils.console.debug( 'Webapp cleared - Clearing repo' ) | |
setRepo= '' | |
} | |
else { | |
utils.console.debug( 'Webapp changed to: ', $webappSel.val() ) | |
// Use Regular Expression to deduce what application is being deployed, based off of the prefix in the Web_Application value | |
var webappName = $webappSel.val().match(/^(?:dev|stage)?(.*)\.cy-motion.com/) | |
// If the regex match was successful, then set the repository value | |
if ( webappName ){ | |
switch ( webappName[1] ) { | |
case 'api': | |
setRepo= 'API' | |
break | |
case 'static': | |
setRepo= 'Static' | |
break | |
case 'secure': | |
setRepo= 'WebApp' | |
break | |
case 'www': | |
default: | |
setRepo= 'Web' | |
break | |
} | |
utils.console.debug('Matched String: ' + webappName[1] + ' - Setting the repo to ' + setRepo) | |
} | |
// If the regex match failed, then default to an empty repo value | |
else { | |
utils.console.debug('Nothing Matched - Defaulting to the Web repo') | |
setRepo= '' | |
} | |
} | |
// Update the repository value | |
utils.console.debug( 'Updating repository value to: ' + setRepo ) | |
$repoSel.val( setRepo ).change() | |
}) | |
} | |
} | |
})(jQuery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment