From befb816eef1067f61ca7898ce258e156c3017174 Mon Sep 17 00:00:00 2001 From: Juan Manuel Perez Date: Wed, 3 Apr 2024 17:25:22 +0200 Subject: [PATCH 1/4] output major and minor version on next-version command --- .github/workflows/self_test.yaml | 14 +++++++++++++- src/app/nextversion/nextversion.go | 23 ++++++++++++++++------- src/app/nextversion/nextversion_test.go | 2 +- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/.github/workflows/self_test.yaml b/.github/workflows/self_test.yaml index f799e0f0..17493dd0 100644 --- a/.github/workflows/self_test.yaml +++ b/.github/workflows/self_test.yaml @@ -259,10 +259,22 @@ jobs: yaml: ${{ env.MOCK_REPO }}/changelog.yaml git-root: ${{ env.MOCK_REPO }} - run: | + errors=0 + if [[ "${{ steps.next-version.outputs.next-version }}" != "v2.0.0" ]]; then echo "next-version should have returned v2.0.0" >&2 - exit 1 + (( errors++ )) + fi + if [[ "${{ steps.next-version.outputs.major }}" != "v2" ]]; then + echo "next-version should have returned v2.0.0" >&2 + (( errors++ )) fi + if [[ "${{ steps.next-version.outputs.major-minor }}" != "v2.0" ]]; then + echo "next-version should have returned v2.0.0" >&2 + (( errors++ )) + fi + + exit ${errors} link-dependencies: name: link-dependencies action diff --git a/src/app/nextversion/nextversion.go b/src/app/nextversion/nextversion.go index a1bf51f7..f2d5f075 100644 --- a/src/app/nextversion/nextversion.go +++ b/src/app/nextversion/nextversion.go @@ -29,7 +29,11 @@ const ( failFlag = "fail" ) -const nextVersionOutput = "next-version" +const ( + nextVersionOutput = "next-version" + majorOutput = "major" + majorMinorOutput = "major-minor" +) // Cmd is the cli.Command object for the next-version command. // @@ -146,7 +150,6 @@ func NextVersion(cCtx *cli.Context) error { return fmt.Errorf("computing next version: %w", err) } - nextStr := "" switch { case nextOverride != nil && next != nil: if nextOverride.LessThan(next) { @@ -154,14 +157,13 @@ func NextVersion(cCtx *cli.Context) error { } else { log.Infof("Overriding next version from autocomputed %v to %v", next.String(), nextOverride.String()) } - nextStr = nextOverride.String() + next = nextOverride case nextOverride != nil && next == nil: log.Infof("Could not compute automatic bump, using overridden version") - nextStr = nextOverride.String() + next = nextOverride case nextOverride == nil && next != nil: - nextStr = next.String() // If we don't have an override, the bumper did not bump anything, and the user has set `--fail`, then we should error out. if errors.Is(err, bumper.ErrNoNewVersion) { log.Warnf("None of the changelog entries produced a version bump, returning current version") @@ -174,8 +176,15 @@ func NextVersion(cCtx *cli.Context) error { return fmt.Errorf("bumping source: %w", err) } - _, _ = fmt.Fprintf(cCtx.App.Writer, "%s%s\n", cCtx.String(outputPrefix), nextStr) - gh.SetOutput(nextVersionOutput, fmt.Sprintf("%s%s", cCtx.String(outputPrefix), nextStr)) + prefix := cCtx.String(outputPrefix) + nextVersion := fmt.Sprintf("%s%s", prefix, next.String()) + nextMajor := fmt.Sprintf("%s%d", prefix, next.Major()) + nextMajorMinor := fmt.Sprintf("%s%d.%d", prefix, next.Major(), next.Minor()) + + _, _ = fmt.Fprintf(cCtx.App.Writer, "%s\n", nextVersion) + gh.SetOutput(nextVersionOutput, nextVersion) + gh.SetOutput(majorOutput, nextMajor) + gh.SetOutput(majorMinorOutput, nextMajorMinor) return nil } diff --git a/src/app/nextversion/nextversion_test.go b/src/app/nextversion/nextversion_test.go index f6760ad6..6d0594c2 100644 --- a/src/app/nextversion/nextversion_test.go +++ b/src/app/nextversion/nextversion_test.go @@ -79,7 +79,7 @@ changes: name: "Bumps_Major_GHA", globalargs: "-gha=true", args: "-current v1.2.3", - expected: "v2.0.0\n::set-output name=next-version::v2.0.0", + expected: "v2.0.0\n::set-output name=next-version::v2.0.0\n::set-output name=major::v2\n::set-output name=major-minor::v2.0", yaml: strings.TrimSpace(` changes: - type: breaking From 38961317c5b514fd8044fd0852537e2051a979c8 Mon Sep 17 00:00:00 2001 From: Juan Manuel Perez Date: Wed, 3 Apr 2024 17:42:49 +0200 Subject: [PATCH 2/4] address linter issues --- src/app/nextversion/nextversion.go | 13 ++++--------- src/changelog/linker/mapper/leadingv_test.go | 12 ++++++------ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/app/nextversion/nextversion.go b/src/app/nextversion/nextversion.go index f2d5f075..4601ae36 100644 --- a/src/app/nextversion/nextversion.go +++ b/src/app/nextversion/nextversion.go @@ -176,15 +176,10 @@ func NextVersion(cCtx *cli.Context) error { return fmt.Errorf("bumping source: %w", err) } - prefix := cCtx.String(outputPrefix) - nextVersion := fmt.Sprintf("%s%s", prefix, next.String()) - nextMajor := fmt.Sprintf("%s%d", prefix, next.Major()) - nextMajorMinor := fmt.Sprintf("%s%d.%d", prefix, next.Major(), next.Minor()) - - _, _ = fmt.Fprintf(cCtx.App.Writer, "%s\n", nextVersion) - gh.SetOutput(nextVersionOutput, nextVersion) - gh.SetOutput(majorOutput, nextMajor) - gh.SetOutput(majorMinorOutput, nextMajorMinor) + _, _ = fmt.Fprintf(cCtx.App.Writer, "%s\n", fmt.Sprintf("%s%s", cCtx.String(outputPrefix), next.String())) + gh.SetOutput(nextVersionOutput, fmt.Sprintf("%s%s", cCtx.String(outputPrefix), next.String())) + gh.SetOutput(majorOutput, fmt.Sprintf("%s%d", cCtx.String(outputPrefix), next.Major())) + gh.SetOutput(majorMinorOutput, fmt.Sprintf("%s%d.%d", cCtx.String(outputPrefix), next.Major(), next.Minor())) return nil } diff --git a/src/changelog/linker/mapper/leadingv_test.go b/src/changelog/linker/mapper/leadingv_test.go index 6af27280..8a152460 100644 --- a/src/changelog/linker/mapper/leadingv_test.go +++ b/src/changelog/linker/mapper/leadingv_test.go @@ -88,7 +88,7 @@ func TestLeadingVCheck_Map(t *testing.T) { name: "Check will not pass even prepending v", wrapped: &mapperMock{link: "link"}, dep: changelog.Dependency{To: semver.MustParse("1.2.3")}, - checkLink: func(link string) (bool, error) { + checkLink: func(_ string) (bool, error) { return false, nil }, expected: "", @@ -98,7 +98,7 @@ func TestLeadingVCheck_Map(t *testing.T) { name: "Check will not pass even removing v", wrapped: &mapperMock{link: "link"}, dep: changelog.Dependency{To: semver.MustParse("v1.2.3")}, - checkLink: func(link string) (bool, error) { + checkLink: func(_ string) (bool, error) { return false, nil }, expected: "", @@ -108,7 +108,7 @@ func TestLeadingVCheck_Map(t *testing.T) { name: "Check returns an error", wrapped: &mapperMock{link: "link"}, dep: changelog.Dependency{To: semver.MustParse("1.2.3")}, - checkLink: func(link string) (bool, error) { + checkLink: func(_ string) (bool, error) { return false, errors.New("") }, expected: "link-1.2.3", // It uses the link from the underlying mapper. @@ -204,14 +204,14 @@ func TestLeadingVCheck_checkLink(t *testing.T) { { name: "Request OK", ok: true, - handler: func(w http.ResponseWriter, r *http.Request) { + handler: func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }, }, { name: "Not found", ok: false, - handler: func(w http.ResponseWriter, r *http.Request) { + handler: func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNotFound) }, }, @@ -219,7 +219,7 @@ func TestLeadingVCheck_checkLink(t *testing.T) { name: "Timeout", ok: false, err: true, - handler: func(w http.ResponseWriter, r *http.Request) { + handler: func(w http.ResponseWriter, _ *http.Request) { time.Sleep(2 * time.Second) w.WriteHeader(http.StatusOK) }, From af3c65d22a339b53b9579b7c0fa998170e3d78fd Mon Sep 17 00:00:00 2001 From: Juan Manuel Perez Date: Thu, 4 Apr 2024 13:56:13 +0200 Subject: [PATCH 3/4] address PR comments --- .github/workflows/self_test.yaml | 4 ++-- next-version/README.md | 9 +++++---- src/app/nextversion/nextversion.go | 4 ++-- src/app/nextversion/nextversion_test.go | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.github/workflows/self_test.yaml b/.github/workflows/self_test.yaml index 17493dd0..e244a796 100644 --- a/.github/workflows/self_test.yaml +++ b/.github/workflows/self_test.yaml @@ -265,11 +265,11 @@ jobs: echo "next-version should have returned v2.0.0" >&2 (( errors++ )) fi - if [[ "${{ steps.next-version.outputs.major }}" != "v2" ]]; then + if [[ "${{ steps.next-version.outputs.next-major }}" != "v2" ]]; then echo "next-version should have returned v2.0.0" >&2 (( errors++ )) fi - if [[ "${{ steps.next-version.outputs.major-minor }}" != "v2.0" ]]; then + if [[ "${{ steps.next-version.outputs.next-major-minor }}" != "v2.0" ]]; then echo "next-version should have returned v2.0.0" >&2 (( errors++ )) fi diff --git a/next-version/README.md b/next-version/README.md index 850a65c6..a56fb120 100644 --- a/next-version/README.md +++ b/next-version/README.md @@ -1,6 +1,6 @@ # 🛠️ `next-version` -Compute next version according to changelog.yaml and Semver conventions. +Compute next version according to changelog.yaml and Semver conventions. It will output the major version and the minor too, so it can be used to tag actions and docker images that do not need to be pinned to the whole version but to respect the interface that it has. ## Example Usage @@ -12,11 +12,13 @@ Example generating the next version from a repo with version tags with last rele tag-prefix: my-project- ``` -If for example there are breaking changes, the output will be `v2.0.0` +If for example there are breaking changes, the output will be `v2.0.0`, `v2.0`, and `v2`. ## Outputs -`next-version`: Returns Semver next version, with leading v +`next-version`: Returns Semver next version, with leading v. E.g.: `v3.4.1` +`next-version-major`: Returns only the major version, with leading v. E.g.: `v3` +`next-version-major-minor`: Returns major and minor version, with leading v. E.g.: `v3.4` ## Parameters @@ -44,4 +46,3 @@ release-toolkit is licensed under the [Apache 2.0](http://apache.org/licenses/LI ## Disclaimer This tool is provided by New Relic AS IS, without warranty of any kind. New Relic does not guarantee that the tool will: not cause any disruption to services or systems; provide results that are complete or 100% accurate; correct or cure any detected vulnerability; or provide specific remediation advice. - diff --git a/src/app/nextversion/nextversion.go b/src/app/nextversion/nextversion.go index 4601ae36..fde5972c 100644 --- a/src/app/nextversion/nextversion.go +++ b/src/app/nextversion/nextversion.go @@ -31,8 +31,8 @@ const ( const ( nextVersionOutput = "next-version" - majorOutput = "major" - majorMinorOutput = "major-minor" + majorOutput = "next-major" + majorMinorOutput = "next-major-minor" ) // Cmd is the cli.Command object for the next-version command. diff --git a/src/app/nextversion/nextversion_test.go b/src/app/nextversion/nextversion_test.go index 6d0594c2..12c64bdc 100644 --- a/src/app/nextversion/nextversion_test.go +++ b/src/app/nextversion/nextversion_test.go @@ -79,7 +79,7 @@ changes: name: "Bumps_Major_GHA", globalargs: "-gha=true", args: "-current v1.2.3", - expected: "v2.0.0\n::set-output name=next-version::v2.0.0\n::set-output name=major::v2\n::set-output name=major-minor::v2.0", + expected: "v2.0.0\n::set-output name=next-version::v2.0.0\n::set-output name=next-major::v2\n::set-output name=next-major-minor::v2.0", yaml: strings.TrimSpace(` changes: - type: breaking From 216dc52929dba8f34fda73ca2df5b8fc42abf6c8 Mon Sep 17 00:00:00 2001 From: Juan Manuel Perez Date: Thu, 4 Apr 2024 16:40:35 +0200 Subject: [PATCH 4/4] give consistency to the names --- .github/workflows/self_test.yaml | 4 ++-- src/app/nextversion/nextversion.go | 4 ++-- src/app/nextversion/nextversion_test.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/self_test.yaml b/.github/workflows/self_test.yaml index e244a796..c3eb72dd 100644 --- a/.github/workflows/self_test.yaml +++ b/.github/workflows/self_test.yaml @@ -265,11 +265,11 @@ jobs: echo "next-version should have returned v2.0.0" >&2 (( errors++ )) fi - if [[ "${{ steps.next-version.outputs.next-major }}" != "v2" ]]; then + if [[ "${{ steps.next-version.outputs.next-version-major }}" != "v2" ]]; then echo "next-version should have returned v2.0.0" >&2 (( errors++ )) fi - if [[ "${{ steps.next-version.outputs.next-major-minor }}" != "v2.0" ]]; then + if [[ "${{ steps.next-version.outputs.next-version-major-minor }}" != "v2.0" ]]; then echo "next-version should have returned v2.0.0" >&2 (( errors++ )) fi diff --git a/src/app/nextversion/nextversion.go b/src/app/nextversion/nextversion.go index fde5972c..fdcb7536 100644 --- a/src/app/nextversion/nextversion.go +++ b/src/app/nextversion/nextversion.go @@ -31,8 +31,8 @@ const ( const ( nextVersionOutput = "next-version" - majorOutput = "next-major" - majorMinorOutput = "next-major-minor" + majorOutput = "next-version-major" + majorMinorOutput = "next-version-major-minor" ) // Cmd is the cli.Command object for the next-version command. diff --git a/src/app/nextversion/nextversion_test.go b/src/app/nextversion/nextversion_test.go index 12c64bdc..572b1ae5 100644 --- a/src/app/nextversion/nextversion_test.go +++ b/src/app/nextversion/nextversion_test.go @@ -79,7 +79,7 @@ changes: name: "Bumps_Major_GHA", globalargs: "-gha=true", args: "-current v1.2.3", - expected: "v2.0.0\n::set-output name=next-version::v2.0.0\n::set-output name=next-major::v2\n::set-output name=next-major-minor::v2.0", + expected: "v2.0.0\n::set-output name=next-version::v2.0.0\n::set-output name=next-version-major::v2\n::set-output name=next-version-major-minor::v2.0", yaml: strings.TrimSpace(` changes: - type: breaking