Skip to content

Commit

Permalink
Move semantic version convenience methods to BoxBase
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew DeVenny <[email protected]>
  • Loading branch information
matthewdevenny committed Aug 7, 2024
1 parent 4054e5d commit bb13ee6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
45 changes: 45 additions & 0 deletions src/com/boxboat/jenkins/pipeline/BoxBase.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.boxboat.jenkins.pipeline

import com.boxboat.jenkins.library.SemVer
import com.boxboat.jenkins.library.Utils
import com.boxboat.jenkins.library.config.CommonConfigBase
import com.boxboat.jenkins.library.config.Config
Expand Down Expand Up @@ -339,4 +340,48 @@ abstract class BoxBase<T extends CommonConfigBase> implements Serializable {
gitRepo?.resetAndClean()
}

/**
* getCurrSemVer
* retrieve the current semantic version for the provided gitTagPrefix and event
**/
SemVer getCurrSemVer(String gitTagPrefix, String event) {
// getBuildVersions returns singleton - If buildVersions already checked out, will initialized object
def buildVersions = Config.getBuildVersions()
return buildVersions.getRepoEventVersion(gitRepo.getRemotePath(), gitTagPrefix, event)
}

/**
* getNextSemVer
* retrieve the next semantic version
**/
SemVer getNextSemVer(SemVer startingSemVer, SemVer baseSemVer, String tagPrefix, String tagType) {
def nextSemVer = startingSemVer?.copy()

def buildVersions = Config.getBuildVersions()
// If nextSemVer doesn't exist or its version without prerelease is smaller than baseSemVer, use baseSemVer
if (nextSemVer == null || !nextSemVer.isValid || (baseSemVer.compareTo(nextSemVer.copyNoPrerelease()) > 0)) {
nextSemVer = baseSemVer.copy()
if (startingSemVer == null || !startingSemVer.isValid) {
nextSemVer.setPreviousVersion("")
} else {
nextSemVer.setPreviousVersion(startingSemVer.toString())
}
} else if (tagType == "release") {
nextSemVer.patch++
nextSemVer.setPreviousVersion(startingSemVer.toString())
}

if (tagType != "release") {
def releaseSemVer = buildVersions.getRepoEventVersion(gitRepo.getRemotePath(), tagPrefix, "tag/release")
if (releaseSemVer != null && releaseSemVer.isValid && releaseSemVer >= nextSemVer) {
nextSemVer = releaseSemVer.copy()
nextSemVer.setPreviousVersion(releaseSemVer.toString())
nextSemVer.patch++
}
nextSemVer.incrementPreRelease(tagType)
}

return nextSemVer
}

}
29 changes: 2 additions & 27 deletions src/com/boxboat/jenkins/pipeline/promote/BoxPromote.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,13 @@ class BoxPromote extends BoxBase<PromoteConfig> implements Serializable {
* Get the current semVer for promotion
**/
SemVer getCurrSemVer() {
// getBuildVersions returns singleton - If buildVersions already checked out, will initialized object
def buildVersions = Config.getBuildVersions()
return buildVersions.getRepoEventVersion(gitRepo.getRemotePath(), config.gitTagPrefix, promotion.promoteToEvent)
return this.getCurrSemVer(config.gitTagPrefix, promotion.promoteToEvent)
}

/**
* Get the next semVer to promote to
**/
SemVer getNextSemVer(String tagType) {
def nextSemVer = currSemVer?.copy()
// Return the user defined version if it is set, else get nextSemVer like usual
if (promoteToVersion) {
SemVer promoteToSemVer = new SemVer(promoteToVersion)
Expand All @@ -169,29 +166,7 @@ class BoxPromote extends BoxBase<PromoteConfig> implements Serializable {
return promoteToSemVer
}

def buildVersions = Config.getBuildVersions()
// If nextSemVer doesn't exist or its version without prerelease is smaller than baseSemVer, use baseSemVer
if (nextSemVer == null || !nextSemVer.isValid || (baseSemVer.compareTo(nextSemVer.copyNoPrerelease()) > 0)) {
nextSemVer = baseSemVer.copy()
if (currSemVer == null || !currSemVer.isValid) {
nextSemVer.setPreviousVersion("")
} else {
nextSemVer.setPreviousVersion(currSemVer.toString())
}
} else if (tagType == "release") {
nextSemVer.patch++
nextSemVer.setPreviousVersion(currSemVer.toString())
}

if (tagType != "release") {
def releaseSemVer = buildVersions.getRepoEventVersion(gitRepo.getRemotePath(), config.gitTagPrefix, "tag/release")
if (releaseSemVer != null && releaseSemVer.isValid && releaseSemVer >= nextSemVer) {
nextSemVer = releaseSemVer.copy()
nextSemVer.setPreviousVersion(releaseSemVer.toString())
nextSemVer.patch++
}
nextSemVer.incrementPreRelease(tagType)
}
nextSemVer = this.getNextSemVer(currSemVer, baseSemVer, config.gitTagPrefix, tagType)

return nextSemVer
}
Expand Down

0 comments on commit bb13ee6

Please sign in to comment.