Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output major and minor version on next-version command (NR-233241) #188

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .github/workflows/self_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.next-version-major }}" != "v2" ]]; then
echo "next-version should have returned v2.0.0" >&2
(( errors++ ))
fi
if [[ "${{ steps.next-version.outputs.next-version-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
Expand Down
9 changes: 5 additions & 4 deletions next-version/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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`
kang-makes marked this conversation as resolved.
Show resolved Hide resolved

## Parameters

Expand Down Expand Up @@ -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.

18 changes: 11 additions & 7 deletions src/app/nextversion/nextversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ const (
failFlag = "fail"
)

const nextVersionOutput = "next-version"
const (
nextVersionOutput = "next-version"
majorOutput = "next-version-major"
majorMinorOutput = "next-version-major-minor"
)

// Cmd is the cli.Command object for the next-version command.
//
Expand Down Expand Up @@ -146,22 +150,20 @@ 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) {
log.Warnf("Next version should be %v, overriding to lower version %v", next.String(), nextOverride.String())
} 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")
Expand All @@ -174,8 +176,10 @@ 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))
_, _ = 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
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/nextversion/nextversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=next-version-major::v2\n::set-output name=next-version-major-minor::v2.0",
yaml: strings.TrimSpace(`
changes:
- type: breaking
Expand Down
12 changes: 6 additions & 6 deletions src/changelog/linker/mapper/leadingv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -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: "",
Expand All @@ -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.
Expand Down Expand Up @@ -204,22 +204,22 @@ 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)
},
},
{
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)
},
Expand Down
Loading