From 12df0f4986fda651c8d465cb57e4c591eb5b6808 Mon Sep 17 00:00:00 2001 From: Jim Nicholson Date: Wed, 7 Feb 2024 22:19:21 -0800 Subject: [PATCH] Options for version and setting build number --- .groovylintrc.json | 6 ++++ vars/addVersionFromNexusRepo.groovy | 49 ++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/.groovylintrc.json b/.groovylintrc.json index f5b873d..b73f501 100644 --- a/.groovylintrc.json +++ b/.groovylintrc.json @@ -1,9 +1,15 @@ { "extends": "recommended", "rules": { + "CompileStatic": { + "enabled": false + }, "DuplicateStringLiteral": { "enabled": false }, + "FactoryMethodName": { + "enabled": false + }, "Indentation": { "enabled": false } diff --git a/vars/addVersionFromNexusRepo.groovy b/vars/addVersionFromNexusRepo.groovy index e752003..da6d986 100644 --- a/vars/addVersionFromNexusRepo.groovy +++ b/vars/addVersionFromNexusRepo.groovy @@ -1,8 +1,8 @@ #!/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. +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: @@ -11,10 +11,20 @@ Jenkins Plugin dependencies: */ +// Define the enum +enum Flags { + + BUILDNUM_IS_COMMITS_SINCE_TAG, + BUILDNUM_IS_JOB_NUMBER, + BUILDNUM_IS_FROM_QUERY + +} + Map call(Map config = [:]) { Map defaults = [ nexusBase: 'https://repo.thejimnicholson.com', searchAPI: 'service/rest/v1/search', + build_flag: Flags.BUILDNUM_IS_FROM_QUERY, queryParams: [ 'repository': 'tools', 'sort': 'name', @@ -33,16 +43,41 @@ Map call(Map config = [:]) { body = response.content assets = readJSON text: body latest = assets.items.last() - runConfig.nexusQuery = nexusSearch - runConfig.latestVersion = latest.name + version = latest.version + if (version == 'null') { + matches = (latest.name =~ /.*-([0-9\.]+)-([0-9]+)\.)[0]/)[0] + version = matches[1] + query_build = matches[2] + } + switch (build_flag) { + case Flags.BUILDNUM_IS_COMMITS_SINCE_TAG: + build = sh( + script: 'git rev-list $(git tag | tail -1).. --count', + returnStdout: true + ).trim() + break + case Flags.BUILDNUM_IS_JOB_NUMBER: + build = env.BUILD_NUMBER + break + case Flags.BUILDNUM_IS_FROM_QUERY: + build = query_build + break + default: + build = query_build + break + } + runConfig.nexusQuery = nexusSearch + runConfig.latestVersion = version + runConfig.latestBuild = build return runConfig } -def buildUrlWithQueryParams(baseUrl, queryParams) { - def url = baseUrl +/* groovylint-disable-next-line MethodParameterTypeRequired, NoDef */ +String buildUrlWithQueryParams(baseUrl, queryParams) { + url = baseUrl if (!queryParams.empty) { - def queryParamsString = queryParams.collect { key, value -> + queryParamsString = queryParams.collect { key, value -> "${URLEncoder.encode(key, 'UTF-8')}=${URLEncoder.encode(value, 'UTF-8')}" }.join('&') url += '?' + queryParamsString