Skip to content

Commit

Permalink
Add patch branch regex for patch version
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Jan 11, 2021
1 parent 99998e9 commit f2957d0
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Samo is a tool to help with a project release.

[![License](https://img.shields.io/github/license/lorislab/semver-release-maven-plugin?style=for-the-badge&logo=apache)](https://www.apache.org/licenses/LICENSE-2.0)
[![License](https://img.shields.io/github/license/lorislab/samo?style=for-the-badge&logo=apache)](https://www.apache.org/licenses/LICENSE-2.0)
[![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/lorislab/samo/build/master?logo=github&style=for-the-badge)](https://github.com/lorislab/samo/actions?query=workflow%3Abuild)
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/lorislab/samo?sort=semver&logo=github&style=for-the-badge)](https://github.com/lorislab/samo/releases/latest)

Expand Down
23 changes: 11 additions & 12 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createVersions(p project.Project, op commonFlags) project.Versions {
}

func createVersionsFrom(p project.Project, op commonFlags, versions []string) project.Versions {
return project.CreateVersions(p, versions, op.HashLength, op.BuildNumberLength, op.BuildNumber, op.FirstVersion)
return project.CreateVersions(p, versions, op.HashLength, op.BuildNumberLength, op.BuildNumber, op.FirstVersion, op.ReleaseMajor, op.PatchBranchRegex)
}

func readOptions(options interface{}) {
Expand All @@ -42,10 +42,7 @@ func readOptions(options interface{}) {

func loadProject(p commonFlags) project.Project {

file := p.File
projectType := project.Type(p.Type)

result := findProject(file, projectType, p.FirstVersion)
result := findProject(p)
if result != nil {
log.WithFields(log.Fields{
"type": result.Type(),
Expand All @@ -56,23 +53,25 @@ func loadProject(p commonFlags) project.Project {

// failed loading the poject
log.WithFields(log.Fields{
"type": projectType,
"file": file,
"type": p.Type,
"file": p.File,
}).Fatal("Could to find project file. Please specified the type --type.")
return nil
}

func findProject(file string, projectType project.Type, firstVer string) project.Project {
func findProject(p commonFlags) project.Project {

projectType := project.Type(p.Type)

// find the project type
if len(projectType) > 0 {
switch projectType {
case project.Maven:
return maven.Load(file)
return maven.Load(p.File)
case project.Npm:
return npm.Load(file)
return npm.Load(p.File)
case project.Git:
return git.Load(file, firstVer)
return git.Load(p.File, p.FirstVersion, p.PatchBranchRegex, p.ReleaseMajor)
}
}

Expand All @@ -87,7 +86,7 @@ func findProject(file string, projectType project.Type, firstVer string) project
return project
}
// priority 3 git
project = git.Load("", firstVer)
project = git.Load("", p.FirstVersion, p.PatchBranchRegex, p.ReleaseMajor)
if project != nil {
return project
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ type commonFlags struct {
BuildNumber string `mapstructure:"build-number"`
BuildNumberLength int `mapstructure:"build-length"`
FirstVersion string `mapstructure:"first-version"`
PatchBranchRegex string `mapstructure:"patch-branch-regex"`
ReleaseMajor bool `mapstructure:"release-major"`
}

type projectFlags struct {
Project commonFlags `mapstructure:",squash"`
ReleaseTagMessage string `mapstructure:"release-tag-message"`
ReleaseMajor bool `mapstructure:"release-major"`
SkipNextDev bool `mapstructure:"skip-next-dev"`
NextDevMsg string `mapstructure:"next-dev-message"`
SkipPush bool `mapstructure:"skip-push"`
Expand All @@ -44,7 +45,6 @@ var (
r := project.ProjectRequest{
Project: p,
TagMsg: op.ReleaseTagMessage,
Major: op.ReleaseMajor,
SkipPush: op.SkipPush,
SkipNextDev: op.SkipNextDev,
CommitMsg: op.NextDevMsg,
Expand Down Expand Up @@ -79,12 +79,13 @@ func initProject() {
addChildCmd(rootCmd, projectCmd)
addSliceFlag(projectCmd, "version", "", []string{project.VerVersion}, "project version type, custom or "+verList)
addFlag(projectCmd, "build-number", "b", "rc{{ .Number }}.{{ .Hash }}", "the build number (temmplate) [Number,Hash,Count]")
addFlag(projectCmd, "patch-branch-regex", "", "", `patch branch regex (if match increment patch version). For example: ^release/\d\.\d\..$`)
addIntFlag(projectCmd, "build-length", "e", 3, "the build number length.")
addIntFlag(projectCmd, "build-hash", "", 12, "the git hash length")
addFlag(projectCmd, "first-version", "", "0.0.0", "the first version of the project")
addBoolFlag(projectCmd, "release-major", "", false, "create a major release")

addChildCmd(projectCmd, createReleaseCmd)
addBoolFlag(createReleaseCmd, "release-major", "", false, "create a major release")
addFlag(createReleaseCmd, "release-tag-message", "", "{{ .Version }}", "the release tag message. (template) [Version]")
nd := addBoolFlag(createReleaseCmd, "skip-next-dev", "", false, "skip update project file (if exists) to next dev version")
ndm := addFlag(createReleaseCmd, "next-dev-message", "", "Create new development version [{{ .Version }}]", "commit message for new development version (template) [Version]")
Expand Down
6 changes: 3 additions & 3 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (g GitProject) SetVersion(version string) {
log.WithField("type", g.Type()).Warn("This project does not support project file changes")
}

func Load(filename, firstVer string) project.Project {
func Load(filename, firstVer, patchBranchRegex string, major bool) project.Project {

if len(filename) == 0 {
filename = ".git"
Expand All @@ -65,8 +65,8 @@ func Load(filename, firstVer string) project.Project {
version: version,
}

versions := project.CreateVersions(result, nil, 0, 0, "", firstVer)
tmp := versions.NextReleaseVersion(false)
versions := project.CreateVersions(result, nil, 0, 0, "", firstVer, major, patchBranchRegex)
tmp := versions.NextReleaseVersion()
result.version = tmp.String()

return &result
Expand Down
3 changes: 1 addition & 2 deletions project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type ProjectRequest struct {
Versions Versions
CommitMsg string
TagMsg string
Major bool
SkipPush bool
SkipNextDev bool
Tag string
Expand Down Expand Up @@ -75,7 +74,7 @@ func (r ProjectRequest) releaseNextDev() {
}

currentVersion := r.Versions.SemVer()
tmp := r.Versions.NextReleaseVersion(r.Major)
tmp := r.Versions.NextReleaseVersion()

tmp, err := tmp.SetPrerelease(currentVersion.Prerelease())
if err != nil {
Expand Down
66 changes: 50 additions & 16 deletions project/versions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package project

import (
"regexp"
"strings"

"github.com/Masterminds/semver"
Expand Down Expand Up @@ -32,24 +33,15 @@ type Versions struct {
HashLength int
BuildNumberLength int
BuildNumber string
PatchBranch bool
Major bool
versions map[string]string
semVer *semver.Version
}

// NextReleaseVersion creates next release version
func (v Versions) NextReleaseVersion(major bool) semver.Version {
ver := v.semVer
if major {
if ver.Patch() != 0 {
log.WithField("version", ver.String()).Fatal("Can not created major release from the patch version!")
}
tmp := ver.IncMajor()
return tmp
}
if ver.Patch() != 0 {
return ver.IncPatch()
}
return ver.IncMinor()
func (v Versions) NextReleaseVersion() semver.Version {
return *nextReleaseVersion(v.semVer, v.Major, v.PatchBranch)
}

func (v Versions) SemVer() *semver.Version {
Expand Down Expand Up @@ -166,20 +158,45 @@ func (v Versions) is(key string) bool {
return len(v.versions[key]) > 0
}

func CreateVersions(project Project, versions []string, hashLength, buildNumberLength int, buildNumber, firstVer string) Versions {
func CreateVersions(project Project, versions []string, hashLength, buildNumberLength int, buildNumber, firstVer string,
major bool, patchBranchRegex string) Versions {

patchBranh := false
if len(patchBranchRegex) > 0 {
branch := tools.GitBranch()

match, e := regexp.MatchString(patchBranchRegex, branch)
if e != nil {
log.WithFields(log.Fields{
"branch": branch,
"regex": patchBranchRegex,
"error": e,
}).Fatal("Error parsing patch branch regex")
}
log.WithFields(log.Fields{
"branch": branch,
"regex": patchBranchRegex,
"match": match,
}).Debug("Check patch branch regex")
patchBranh = match
}

semVer := SemVer(project.Version())
ver, custom := createVersions(semVer, versions, hashLength, buildNumberLength, buildNumber, firstVer)
ver, custom := createVersions(semVer, versions, hashLength, buildNumberLength, buildNumber, firstVer, major, patchBranh)
return Versions{
custom: custom,
HashLength: hashLength,
BuildNumberLength: buildNumberLength,
BuildNumber: buildNumber,
versions: ver,
semVer: semVer,
Major: major,
PatchBranch: patchBranh,
}
}

func createVersions(semVer *semver.Version, versions []string, hashLength, buildNumberLength int, buildNumber, firstVer string) (map[string]string, []string) {
func createVersions(semVer *semver.Version, versions []string, hashLength, buildNumberLength int, buildNumber, firstVer string,
major, patchBranch bool) (map[string]string, []string) {
result := make(map[string]string)
custom := []string{}

Expand Down Expand Up @@ -297,3 +314,20 @@ func SemVer(version string) *semver.Version {
}
return result
}

// NextReleaseVersion creates next release version
func nextReleaseVersion(ver *semver.Version, major, patchBranch bool) *semver.Version {
if major {
if ver.Patch() != 0 {
log.WithField("version", ver.String()).Fatal("Can not created major release from the patch version!")
}
tmp := ver.IncMajor()
return &tmp
}
if ver.Patch() != 0 || patchBranch {
tmp := ver.IncPatch()
return &tmp
}
tmp := ver.IncMinor()
return &tmp
}

0 comments on commit f2957d0

Please sign in to comment.