Skip to content

Commit

Permalink
add ability to bump build labels
Browse files Browse the repository at this point in the history
it has pretty much the same logic as a prerelease.

it seems like the blang/semver library intentionally avoided attaching a
version number to the build label (most likely due to the semver spec
saying it will be ignored when establishing precedence
https://semver.org/#spec-item-10). But we have a use for it as other
tools don't fully adhere to that spec.

Signed-off-by: Bohan Chen <[email protected]>
  • Loading branch information
chenbh committed May 26, 2022
1 parent 1d321fc commit 94423ac
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 49 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,17 @@ be one of:
type, (e.g. `alpha` vs. `beta`), the type is switched and the prerelease
version is reset to `1`. If the version is *not* already a pre-release, then
`pre` is added, starting at `1`.

The value of `pre` can be anything you like; the value will be `pre`-pended (_hah_) to a numeric value. For example, `pre: build` will result in a semver of `x.y.z-build.<number>`, `pre: alpha` becomes `x.y.z-alpha.<number>`, and `pre: my-preferred-naming-convention` becomes `x.y.z-my-preferred-naming-convention.<number>`

If `pre_without_version` is set as `true`, the value will be `pre` and no version number. So `SNAPSHOT` will still be as `SNAPSHOT`,
The value of `pre` can be anything you like; the value will be `pre`-pended (_hah_) to a numeric value. For example, `pre: foo` will result in a semver of `x.y.z-foo.<number>`, `pre: alpha` becomes `x.y.z-alpha.<number>`, and `pre: my-preferred-naming-convention` becomes `x.y.z-my-preferred-naming-convention.<number>`

* `build`: *Optional.* Same as `pre` but for build labels (e.g. `build: foo`
will result in a semver of `x.y.z+foo.<number>`, `build: alpha` becomes
`x.y.z+alpha.<number>`.

It is valid for a semver to be both a prerelease and a build, for example,
`pre: alpha, build: test` results in `x.y.z-alpha.<number>+test.<number>`
* `pre_without_version`: *Optional.* When bumping to a prerelease, drop the
version if set to `true`.
Examples:
* Major version bump: version file = 1.2.4-SNAPSHOT, release version = 2.0.0
* Minor version bump: version file = 1.2.4-SNAPSHOT, release version = 1.3.0
Expand Down
2 changes: 1 addition & 1 deletion in/in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/concourse/semver-resource/models"
"github.com/nu7hatch/gouuid"
uuid "github.com/nu7hatch/gouuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
Expand Down
7 changes: 5 additions & 2 deletions in/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ func main() {
}

bumped := version.BumpFromParams(
request.Params.Bump,
request.Params.Bump,
request.Params.Pre,
request.Params.PreWithoutVersion).Apply(inputVersion)
request.Params.PreWithoutVersion,
request.Params.Build,
request.Params.BuildWithoutVersion,
).Apply(inputVersion)

if !bumped.Equals(inputVersion) {
fmt.Fprintf(os.Stderr, "bumped locally from %s to %s\n", inputVersion, bumped)
Expand Down
16 changes: 10 additions & 6 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ type InResponse struct {
}

type InParams struct {
Bump string `json:"bump"`
Pre string `json:"pre"`
PreWithoutVersion bool `json:"pre_without_version"`
Bump string `json:"bump"`
Pre string `json:"pre"`
Build string `json:"build"`
PreWithoutVersion bool `json:"pre_without_version"`
BuildWithoutVersion bool `json:"build_without_version"`
}

type OutRequest struct {
Expand All @@ -35,9 +37,11 @@ type OutResponse struct {
type OutParams struct {
File string `json:"file"`

Bump string `json:"bump"`
Pre string `json:"pre"`
PreWithoutVersion bool `json:"pre_without_version"`
Bump string `json:"bump"`
Pre string `json:"pre"`
Build string `json:"build"`
PreWithoutVersion bool `json:"pre_without_version"`
BuildWithoutVersion bool `json:"build_without_version"`
}

type CheckRequest struct {
Expand Down
9 changes: 6 additions & 3 deletions out/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ func main() {
}
} else if request.Params.Bump != "" || request.Params.Pre != "" {
bump := version.BumpFromParams(
request.Params.Bump,
request.Params.Pre,
request.Params.PreWithoutVersion)
request.Params.Bump,
request.Params.Pre,
request.Params.PreWithoutVersion,
request.Params.Build,
request.Params.BuildWithoutVersion,
)

newVersion, err = driver.Bump(bump)
if err != nil {
Expand Down
139 changes: 138 additions & 1 deletion out/out_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/concourse/semver-resource/models"
"github.com/nu7hatch/gouuid"
uuid "github.com/nu7hatch/gouuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
Expand Down Expand Up @@ -269,5 +269,142 @@ var _ = Describe("Out", func() {
})
})
})

Context("when bumping the version to a build", func() {
BeforeEach(func() {
request.Params.Build = "build"
})

Context("when the version is not a build", func() {
BeforeEach(func() {
putVersion("1.2.3")
})

It("reports the bumped version as the version", func() {
Expect(response.Version.Number).To(Equal("1.2.3+build.1"))
})

It("saves the contents of the file in the configured bucket", func() {
Expect(getVersion()).To(Equal("1.2.3+build.1"))
})

Context("when doing a semantic bump at the same time", func() {
BeforeEach(func() {
request.Params.Bump = "minor"
})

It("reports the bumped version as the version", func() {
Expect(response.Version.Number).To(Equal("1.3.0+build.1"))
})

It("saves the contents of the file in the configured bucket", func() {
Expect(getVersion()).To(Equal("1.3.0+build.1"))
})
})
})

Context("when the version is the same build", func() {
BeforeEach(func() {
putVersion("1.2.3+build.2")
})

It("reports the bumped version as the version", func() {
Expect(response.Version.Number).To(Equal("1.2.3+build.3"))
})

It("saves the contents of the file in the configured bucket", func() {
Expect(getVersion()).To(Equal("1.2.3+build.3"))
})

Context("when doing a semantic bump at the same time", func() {
BeforeEach(func() {
request.Params.Bump = "minor"
})

It("reports the bumped version as the version", func() {
Expect(response.Version.Number).To(Equal("1.3.0+build.1"))
})

It("saves the contents of the file in the configured bucket", func() {
Expect(getVersion()).To(Equal("1.3.0+build.1"))
})
})
})

Context("when the version is a different build", func() {
BeforeEach(func() {
putVersion("1.2.3-beta.2")
})

It("reports the bumped version as the version", func() {
Expect(response.Version.Number).To(Equal("1.2.3+build.1"))
})

It("saves the contents of the file in the configured bucket", func() {
Expect(getVersion()).To(Equal("1.2.3+build.1"))
})

Context("when doing a semantic bump at the same time", func() {
BeforeEach(func() {
request.Params.Bump = "minor"
})

It("reports the bumped version as the version", func() {
Expect(response.Version.Number).To(Equal("1.3.0+build.1"))
})

It("saves the contents of the file in the configured bucket", func() {
Expect(getVersion()).To(Equal("1.3.0+build.1"))
})
})
})
})

Context("when bumping the version to a prerelease and build", func() {
BeforeEach(func() {
request.Params.Pre = "alpha"
request.Params.Build = "build"
})

Context("when the version is just a base version", func() {
BeforeEach(func() {
putVersion("1.2.3")
})

It("reports the bumped version as the version", func() {
Expect(response.Version.Number).To(Equal("1.2.3-alpha.1+build.1"))
})
})

Context("when the version has a prerelease", func() {
BeforeEach(func() {
putVersion("1.2.3-alpha.1")
})

It("reports the bumped version as the version", func() {
Expect(response.Version.Number).To(Equal("1.2.3-alpha.2+build.1"))
})
})

Context("when the version has a build", func() {
BeforeEach(func() {
putVersion("1.2.3+build.1")
})

It("reports the bumped version as the version", func() {
Expect(response.Version.Number).To(Equal("1.2.3-alpha.1+build.2"))
})
})

Context("when the version has a build and prerelease", func() {
BeforeEach(func() {
putVersion("1.2.3-alpha.1+build.1")
})

It("reports the bumped version as the version", func() {
Expect(response.Version.Number).To(Equal("1.2.3-alpha.2+build.2"))
})
})
})
})
})
11 changes: 7 additions & 4 deletions test/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ put_uri_with_bump() {
},
params: {
bump: $(echo $3 | jq -R .),
pre: $(echo $4 | jq -R .)
pre: $(echo $4 | jq -R .),
build: $(echo $5 | jq -R .)
}
}" | ${resource_dir}/out "$2" | tee /dev/stderr
}
Expand All @@ -168,7 +169,8 @@ put_uri_with_bump_and_initial() {
},
params: {
bump: $(echo $4 | jq -R .),
pre: $(echo $5 | jq -R .)
pre: $(echo $5 | jq -R .),
build: $(echo $6 | jq -R .)
}
}" | ${resource_dir}/out "$2" | tee /dev/stderr
}
Expand All @@ -180,11 +182,12 @@ put_uri_with_bump_and_message() {
uri: $(echo $1 | jq -R .),
branch: \"master\",
file: \"some-file\",
commit_message: \"$(echo $5)\"
commit_message: \"$(echo $6)\"
},
params: {
bump: $(echo $3 | jq -R .),
pre: $(echo $4 | jq -R .)
pre: $(echo $4 | jq -R .),
build: $(echo $5 | jq -R .)
}
}" | ${resource_dir}/out "$2" | tee /dev/stderr
}
32 changes: 16 additions & 16 deletions test/put.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ it_can_put_and_bump_first_version() {
# cannot push to repo while it's checked out to a branch
git -C $repo checkout refs/heads/master

put_uri_with_bump $repo $src minor alpha | jq -e "
.version == {number: \"0.1.0-alpha.1\"}
put_uri_with_bump $repo $src minor alpha build | jq -e "
.version == {number: \"0.1.0-alpha.1+build.1\"}
"

# switch back to master
git -C $repo checkout master

test -e $repo/some-file
test "$(cat $repo/some-file)" = 0.1.0-alpha.1
test "$(cat $repo/some-file)" = 0.1.0-alpha.1+build.1
}

it_can_put_and_bump_first_version_with_initial() {
Expand All @@ -95,15 +95,15 @@ it_can_put_and_bump_first_version_with_initial() {
# cannot push to repo while it's checked out to a branch
git -C $repo checkout refs/heads/master

put_uri_with_bump_and_initial $repo $src 1.2.3 minor alpha | jq -e "
.version == {number: \"1.3.0-alpha.1\"}
put_uri_with_bump_and_initial $repo $src 1.2.3 minor alpha build | jq -e "
.version == {number: \"1.3.0-alpha.1+build.1\"}
"

# switch back to master
git -C $repo checkout master

test -e $repo/some-file
test "$(cat $repo/some-file)" = 1.3.0-alpha.1
test "$(cat $repo/some-file)" = 1.3.0-alpha.1+build.1
}

it_can_put_and_bump_over_existing_version() {
Expand All @@ -116,15 +116,15 @@ it_can_put_and_bump_over_existing_version() {
# cannot push to repo while it's checked out to a branch
git -C $repo checkout refs/heads/master

put_uri_with_bump $repo $src minor alpha | jq -e "
.version == {number: \"1.3.0-alpha.1\"}
put_uri_with_bump $repo $src minor alpha build | jq -e "
.version == {number: \"1.3.0-alpha.1+build.1\"}
"

# switch back to master
git -C $repo checkout master

test -e $repo/some-file
test "$(cat $repo/some-file)" = 1.3.0-alpha.1
test "$(cat $repo/some-file)" = 1.3.0-alpha.1+build.1
}

it_can_put_and_bump_with_message_over_existing_version() {
Expand All @@ -139,15 +139,15 @@ it_can_put_and_bump_with_message_over_existing_version() {
# cannot push to repo while it's checked out to a branch
git -C $repo checkout refs/heads/master

put_uri_with_bump_and_message $repo $src minor alpha "$message" | jq -e "
.version == {number: \"1.3.0-alpha.1\"}
put_uri_with_bump_and_message $repo $src minor alpha build "$message" | jq -e "
.version == {number: \"1.3.0-alpha.1+build.1\"}
"

# switch back to master
git -C $repo checkout master

test -e $repo/some-file
test "$(cat $repo/some-file)" = 1.3.0-alpha.1
test "$(cat $repo/some-file)" = 1.3.0-alpha.1+build.1
test "$(git -C $repo log -n1 --pretty=%B)" = "$message"
}

Expand All @@ -163,17 +163,17 @@ it_can_put_and_bump_with_message_and_replace_over_existing_version() {
# cannot push to repo while it's checked out to a branch
git -C $repo checkout refs/heads/master

put_uri_with_bump_and_message $repo $src minor alpha "$message" | jq -e "
.version == {number: \"1.3.0-alpha.1\"}
put_uri_with_bump_and_message $repo $src minor alpha build "$message" | jq -e "
.version == {number: \"1.3.0-alpha.1+build.1\"}
"

# switch back to master
git -C $repo checkout master

local expected_message="This is a commit message on some-file with 1.3.0-alpha.1"
local expected_message="This is a commit message on some-file with 1.3.0-alpha.1+build.1"

test -e $repo/some-file
test "$(cat $repo/some-file)" = 1.3.0-alpha.1
test "$(cat $repo/some-file)" = 1.3.0-alpha.1+build.1
test "$(git -C $repo log -n1 --pretty=%B)" = "$expected_message"
}

Expand Down
Loading

0 comments on commit 94423ac

Please sign in to comment.