From a3ffb87243eae5b43c6dc1ff97aa2c4dc26b0a7e Mon Sep 17 00:00:00 2001 From: krogon Date: Tue, 25 Jun 2024 09:58:16 +0200 Subject: [PATCH] feat: add optional tag_prefix --- README.md | 4 +++ action.yml | 7 +++- action.yml.dist | 5 +++ internal/pkg/event/guard.go | 4 +-- internal/pkg/git/git.go | 14 ++++++-- internal/pkg/git/git_test.go | 58 +++++++++++++++++++++++++++++++ internal/pkg/semver/model_test.go | 5 +++ 7 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 internal/pkg/git/git_test.go diff --git a/README.md b/README.md index af5da22..080adc3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/action.yml b/action.yml index 678233f..9e57040 100644 --- a/action.yml +++ b/action.yml @@ -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 @@ -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 }} diff --git a/action.yml.dist b/action.yml.dist index 0a79f0e..ec3c5bd 100644 --- a/action.yml.dist +++ b/action.yml.dist @@ -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 diff --git a/internal/pkg/event/guard.go b/internal/pkg/event/guard.go index ae05fa3..23be4d7 100644 --- a/internal/pkg/event/guard.go +++ b/internal/pkg/event/guard.go @@ -1,7 +1,7 @@ package event import ( - "io/ioutil" + "io" "os" "github.com/K-Phoen/semver-release-action/internal/pkg/action" @@ -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 diff --git a/internal/pkg/git/git.go b/internal/pkg/git/git.go index f321699..ef752af 100644 --- a/internal/pkg/git/git.go +++ b/internal/pkg/git/git.go @@ -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), Run: executeLatestTag, } } @@ -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() @@ -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 @@ -72,5 +80,5 @@ func executeLatestTag(cmd *cobra.Command, args []string) { } } - cmd.Printf("v%s", latest) + return latest } diff --git a/internal/pkg/git/git_test.go b/internal/pkg/git/git_test.go new file mode 100644 index 0000000..9e0646f --- /dev/null +++ b/internal/pkg/git/git_test.go @@ -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) + } + }) + } +} diff --git a/internal/pkg/semver/model_test.go b/internal/pkg/semver/model_test.go index 5215519..1cb4ed3 100644 --- a/internal/pkg/semver/model_test.go +++ b/internal/pkg/semver/model_test.go @@ -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 {