#!/usr/bin/env groovy /* Calculate the current version by querying a list of assets in a Nexus Repository. Return '0.0.1' if there are no matching assets. Jenkins Plugin dependencies: - HTTP Request - Plugin Utilty Steps */ Map call(Map config = [:]) { Map defaults = [ nexusBase: 'https://repo.thejimnicholson.com', searchAPI: 'service/rest/v1/search', queryParams: [ 'repository': 'tools', 'sort': 'name', 'name': 'tests/test-something*' ] ] Map runConfig = defaults + config nexusURL = runConfig.nexusBase + '/' + runConfig.searchAPI nexusSearch = buildUrlWithQueryParams(nexusURL, runConfig.queryParams) response = httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', url: nexusSearch, wrapAsMultipart: false body = response.content assets = readJSON text: body latest = assets.items.last() runConfig.nexusQuery = nexusSearch runConfig.latestVersion = latest.name return runConfig } def buildUrlWithQueryParams(baseUrl, queryParams) { def url = baseUrl if (!queryParams.empty) { def queryParamsString = queryParams.collect { key, value -> "${URLEncoder.encode(key, 'UTF-8')}=${URLEncoder.encode(value, 'UTF-8')}" }.join('&') url += '?' + queryParamsString } return url }