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

feat: add optional tag_prefix #7

Merged
merged 1 commit into from
Jun 25, 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ release ; `tag`: creates a lightweight tag ; `none`: computes the next

**Optional** Format used to create tags. Default `"v%major%.%minor%.%patch%"`.

### `tag_prefixt`

**Optional** Tag prefix for monorepo. Default `""`.

### `tag`

**Optional** Tag to use. If left undefined, it will be computed using the tags
Expand Down
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ inputs:
required: true
default: 'v%major%.%minor%.%patch%'

tag_prefix:
description: 'Format used to create tags'
required: true
default: ''

tag:
description: 'Tag to use'
required: false
Expand All @@ -37,7 +42,7 @@ outputs:

runs:
using: 'docker'
image: 'docker://krogon/semver-release-action:v2.1.0'
image: 'docker://krogon/semver-release-action:v2.2.0'
args:
- ${{ inputs.release_branch }}
- ${{ inputs.release_strategy }}
Expand Down
5 changes: 5 additions & 0 deletions action.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ inputs:
required: true
default: 'v%major%.%minor%.%patch%'

tag_prefix:
description: 'Format used to create tags'
required: true
default: ''

tag:
description: 'Tag to use'
required: false
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/event/guard.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package event

import (
"io/ioutil"
"io"
"os"

"github.com/K-Phoen/semver-release-action/internal/pkg/action"
Expand Down Expand Up @@ -116,7 +116,7 @@ func readEvent(cmd *cobra.Command, filePath string) []byte {
action.AssertNoError(cmd, err, "could not open GitHub event file: %s", err)
defer file.Close()

b, err := ioutil.ReadAll(file)
b, err := io.ReadAll(file)
action.AssertNoError(cmd, err, "could not read GitHub event file: %s", err)

return b
Expand Down
14 changes: 11 additions & 3 deletions internal/pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (

func LatestTagCommand() *cobra.Command {
return &cobra.Command{
Use: "latest-tag [REPOSITORY] [GH_TOKEN] [TAG_FORMAT]",
Args: cobra.ExactArgs(3),
Use: "latest-tag [REPOSITORY] [GH_TOKEN] [TAG_FORMAT] [TAG_PREFIX]",
Args: cobra.ExactArgs(4),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
Magic number: 4, in detected (mnd)

Run: executeLatestTag,
}
}
Expand All @@ -33,6 +33,7 @@ func executeLatestTag(cmd *cobra.Command, args []string) {
repository := args[0]
githubToken := args[1]
tagFormat := args[2]
tagPrefix := args[3]

ctx := context.Background()

Expand All @@ -53,15 +54,22 @@ func executeLatestTag(cmd *cobra.Command, args []string) {
}
action.AssertNoError(cmd, err, "could not list git refs: %s", err)

latest := filterRemoteTags(refs, tagFormat, tagPrefix)
cmd.Printf("%sv%s", tagPrefix, latest)
}

func filterRemoteTags(refs []*github.Reference, tagFormat string, tagPrefix string) semver.Version {
latest := semver.MustParse("0.0.0")
tagFormatRegex := createRegexFromTagFormat(tagFormat)

for _, ref := range refs {
versionStr := strings.Replace(*ref.Ref, "refs/tags/", "", 1)
versionStr = strings.Replace(versionStr, tagPrefix, "", 1)
formatValid, _ := regexp.MatchString(tagFormatRegex, versionStr)
if !formatValid {
continue
}

version, err := semver.ParseTolerant(versionStr)
if err != nil {
continue
Expand All @@ -72,5 +80,5 @@ func executeLatestTag(cmd *cobra.Command, args []string) {
}
}

cmd.Printf("v%s", latest)
return latest
}
58 changes: 58 additions & 0 deletions internal/pkg/git/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package git

import (
"reflect"
"testing"

"github.com/blang/semver/v4"
"github.com/google/go-github/v45/github"
)

func Test_filterRemoteTags(t *testing.T) {
tagFormat := "v%major%.%minor%.%patch%"
tests := []struct {
name string
refs []*github.Reference
tagFormat string
tagPrefix string
want semver.Version
}{
{
name: "sample",
tagFormat: tagFormat,
tagPrefix: "",
refs: []*github.Reference{
{Ref: github.String("refs/tags/v0.5.5")},
{Ref: github.String("refs/tags/v1.2.3")},
},
want: semver.Version{Major: 1, Minor: 2, Patch: 3},
},
{
name: "sampleA",
tagFormat: tagFormat,
tagPrefix: "moduleA-",
refs: []*github.Reference{
{Ref: github.String("refs/tags/moduleA-v0.5.5")},
{Ref: github.String("refs/tags/moduleB-v1.2.3")},
},
want: semver.Version{Major: 0, Minor: 5, Patch: 5},
},
{
name: "sampleB",
tagFormat: tagFormat,
tagPrefix: "moduleB-",
refs: []*github.Reference{
{Ref: github.String("refs/tags/moduleA-v0.5.5")},
{Ref: github.String("refs/tags/moduleB-v1.2.3")},
},
want: semver.Version{Major: 1, Minor: 2, Patch: 3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := filterRemoteTags(tt.refs, tt.tagFormat, tt.tagPrefix); !reflect.DeepEqual(got, tt.want) {
t.Errorf("filterRemoteTags() = %v, want %v", got, tt.want)
}
})
}
}
5 changes: 5 additions & 0 deletions internal/pkg/semver/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func TestFormat(t *testing.T) {
format: "v%major%.%minor%.%patch%-RC",
expectedVersion: "v1.2.3-RC",
},
{
version: Version{major: 1, minor: 2, patch: 3},
format: "module-v%major%.%minor%.%patch%",
expectedVersion: "module-v1.2.3",
},
}

for _, testCase := range cases {
Expand Down
Loading