diff --git a/vars/addVersionFromNexusRepo.groovy b/vars/findVersionFromNexusRepo.groovy similarity index 52% rename from vars/addVersionFromNexusRepo.groovy rename to vars/findVersionFromNexusRepo.groovy index 066d6bb..93422b0 100644 --- a/vars/addVersionFromNexusRepo.groovy +++ b/vars/findVersionFromNexusRepo.groovy @@ -26,31 +26,42 @@ Map call(Map config = [:], Map queryParams = [:]) { Map runConfig = defaults + config Map query = queryParamsDefaults + queryParams + def version + def response + nexusURL = runConfig.nexusBase + '/' + runConfig.searchAPI nexusSearch = buildUrlWithQueryParams(nexusURL, query) - + response = httpRequest acceptType: 'APPLICATION_JSON', - contentType: 'APPLICATION_JSON', - url: nexusSearch, - wrapAsMultipart: false + contentType: 'APPLICATION_JSON', + url: nexusSearch, + wrapAsMultipart: false + + body = response.content assets = readJSON text: body - latest = assets.items.last() - version = latest.version ?: (latest.name =~ /.*-([0-9\.]+)-([0-9]+)\..*/)[0][1] - build = latest.version ?: (latest.name =~ /.*-([0-9\.]+)-([0-9]+)\..*/)[0][2] - - if (version == null) { - // Call the error method and pass a string indicating version was null and name is not parseable - error('Version was null and name is not parseable') + if (assets.items && !assets.items.isEmpty()) { + latest = assets.items.last() + /* We have to use instanceof here. The value SHOULD be null, but it comes back + as class rather than null. This is probably a bug in the Pipeline Utilities + plugin. */ + /* groovylint-disable-next-line Instanceof */ + if (latest.version instanceof net.sf.json.JSONNull && latest.name) { + echo "Search result version field was null, looking in name field for a version" + def match = (latest.name =~ /(?<=-)([0-9\.]{1,3})(?:-|\.)[^-\.]+/) + if (!match) { + echo "Match: ${match}" + error("Regular expression match failed for latest.name: ${latest.name}") + } + version = match[0][1] + } else if (latest.version) { + version = latest.version + } + } else { + version = '0.0.1' + unstable('No matching assets found, assuming this is the first version') } - - if (build == null) { - unstable("Build number could not be found in latest asset match (${latest.name})") - } - build = build ?: env.BUILD_NUMBER - runConfig.version = version - runConfig.build = build - return runConfig + return version } /* groovylint-disable-next-line MethodParameterTypeRequired, NoDef */