mirror of
https://github.com/thegeeklab/wp-docker-buildx.git
synced 2024-11-22 00:00:40 +00:00
fix: use semver tag helper from plugin lib (#76)
BREAKING CHANGE: A strict semver method is now used to automatically generate tags. Git tags that are not strictly semver-compatible will be ignored.
This commit is contained in:
parent
647e892c76
commit
2a7a54d18e
2
go.mod
2
go.mod
@ -4,7 +4,6 @@ go 1.21
|
||||
|
||||
require (
|
||||
github.com/cenkalti/backoff v2.2.1+incompatible
|
||||
github.com/coreos/go-semver v0.3.1
|
||||
github.com/rs/zerolog v1.31.0
|
||||
github.com/thegeeklab/wp-plugin-go v1.1.0
|
||||
github.com/urfave/cli/v2 v2.26.0
|
||||
@ -12,6 +11,7 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/Masterminds/semver/v3 v3.2.1 // indirect
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||
github.com/joho/godotenv v1.5.1 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
|
7
go.sum
7
go.sum
@ -1,7 +1,7 @@
|
||||
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
|
||||
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
|
||||
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
|
||||
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
|
||||
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
|
||||
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
@ -32,6 +32,3 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
||||
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/cenkalti/backoff"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/thegeeklab/wp-plugin-go/tag"
|
||||
"github.com/thegeeklab/wp-plugin-go/types"
|
||||
"github.com/urfave/cli/v2"
|
||||
"golang.org/x/sys/execabs"
|
||||
@ -49,13 +50,14 @@ func (p *Plugin) Validate() error {
|
||||
|
||||
if p.Settings.Build.TagsAuto {
|
||||
// return true if tag event or default branch
|
||||
if UseDefaultTag(
|
||||
if tag.IsTaggable(
|
||||
p.Settings.Build.Ref,
|
||||
p.Settings.Build.Branch,
|
||||
) {
|
||||
tag, err := DefaultTagSuffix(
|
||||
tag, err := tag.SemverTagSuffix(
|
||||
p.Settings.Build.Ref,
|
||||
p.Settings.Build.TagsSuffix,
|
||||
true,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot generate tags from %s, invalid semantic version: %w", p.Settings.Build.Ref, err)
|
||||
|
121
plugin/tags.go
121
plugin/tags.go
@ -1,121 +0,0 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/coreos/go-semver/semver"
|
||||
)
|
||||
|
||||
// DefaultTagSuffix returns a set of default suggested tags
|
||||
// based on the commit ref with an attached suffix.
|
||||
func DefaultTagSuffix(ref, suffix string) ([]string, error) {
|
||||
tags, err := DefaultTags(ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(suffix) == 0 {
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
for i, tag := range tags {
|
||||
if tag == "latest" {
|
||||
tags[i] = suffix
|
||||
} else {
|
||||
tags[i] = fmt.Sprintf("%s-%s", tag, suffix)
|
||||
}
|
||||
}
|
||||
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
func splitOff(input, delim string) string {
|
||||
const splits = 2
|
||||
parts := strings.SplitN(input, delim, splits)
|
||||
|
||||
if len(parts) == splits {
|
||||
return parts[0]
|
||||
}
|
||||
|
||||
return input
|
||||
}
|
||||
|
||||
// DefaultTags returns a set of default suggested tags based on
|
||||
// the commit ref.
|
||||
func DefaultTags(ref string) ([]string, error) {
|
||||
if !strings.HasPrefix(ref, "refs/tags/") {
|
||||
return []string{"latest"}, nil
|
||||
}
|
||||
|
||||
rawVersion := stripTagPrefix(ref)
|
||||
|
||||
version, err := semver.NewVersion(rawVersion)
|
||||
if err != nil {
|
||||
return []string{"latest"}, err
|
||||
}
|
||||
|
||||
if version.PreRelease != "" {
|
||||
return []string{
|
||||
version.String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
rawVersion = stripTagPrefix(ref)
|
||||
rawVersion = splitOff(splitOff(rawVersion, "+"), "-")
|
||||
//nolint:gomnd
|
||||
dotParts := strings.SplitN(rawVersion, ".", 3)
|
||||
|
||||
if version.Major == 0 {
|
||||
return []string{
|
||||
fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor),
|
||||
fmt.Sprintf(
|
||||
"%0*d.%0*d.%0*d",
|
||||
len(dotParts[0]),
|
||||
version.Major,
|
||||
len(dotParts[1]),
|
||||
version.Minor,
|
||||
len(dotParts[2]),
|
||||
version.Patch,
|
||||
),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return []string{
|
||||
fmt.Sprintf("%0*d", len(dotParts[0]), version.Major),
|
||||
fmt.Sprintf("%0*d.%0*d", len(dotParts[0]), version.Major, len(dotParts[1]), version.Minor),
|
||||
fmt.Sprintf(
|
||||
"%0*d.%0*d.%0*d",
|
||||
len(dotParts[0]),
|
||||
version.Major,
|
||||
len(dotParts[1]),
|
||||
version.Minor,
|
||||
len(dotParts[2]),
|
||||
version.Patch,
|
||||
),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UseDefaultTag to keep only default branch for latest tag.
|
||||
func UseDefaultTag(ref, defaultBranch string) bool {
|
||||
if strings.HasPrefix(ref, "refs/tags/") {
|
||||
return true
|
||||
}
|
||||
|
||||
if stripHeadPrefix(ref) == defaultBranch {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func stripHeadPrefix(ref string) string {
|
||||
return strings.TrimPrefix(ref, "refs/heads/")
|
||||
}
|
||||
|
||||
func stripTagPrefix(ref string) string {
|
||||
ref = strings.TrimPrefix(ref, "refs/tags/")
|
||||
ref = strings.TrimPrefix(ref, "v")
|
||||
|
||||
return ref
|
||||
}
|
@ -1,207 +0,0 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_stripTagPrefix(t *testing.T) {
|
||||
tests := []struct {
|
||||
Before string
|
||||
After string
|
||||
}{
|
||||
{"refs/tags/1.0.0", "1.0.0"},
|
||||
{"refs/tags/v1.0.0", "1.0.0"},
|
||||
{"v1.0.0", "1.0.0"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
got, want := stripTagPrefix(test.Before), test.After
|
||||
if got != want {
|
||||
t.Errorf("Got tag %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultTags(t *testing.T) {
|
||||
tests := []struct {
|
||||
Before string
|
||||
After []string
|
||||
}{
|
||||
{"", []string{"latest"}},
|
||||
{"refs/heads/main", []string{"latest"}},
|
||||
{"refs/tags/0.9.0", []string{"0.9", "0.9.0"}},
|
||||
{"refs/tags/1.0.0", []string{"1", "1.0", "1.0.0"}},
|
||||
{"refs/tags/v1.0.0+meta", []string{"1", "1.0", "1.0.0"}},
|
||||
{"refs/tags/v1.0.0", []string{"1", "1.0", "1.0.0"}},
|
||||
{"refs/tags/v1.0.0-alpha.1", []string{"1.0.0-alpha.1"}},
|
||||
{"refs/tags/v1.0.0-alpha", []string{"1.0.0-alpha"}},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
tags, err := DefaultTags(test.Before)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
got, want := tags, test.After
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("Got tag %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultTagsError(t *testing.T) {
|
||||
tests := []string{
|
||||
"refs/tags/x1.0.0",
|
||||
"refs/tags/20190203",
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
_, err := DefaultTags(test)
|
||||
if err == nil {
|
||||
t.Errorf("Expect tag error for %s", test)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultTagSuffix(t *testing.T) {
|
||||
tests := []struct {
|
||||
Before string
|
||||
Suffix string
|
||||
After []string
|
||||
}{
|
||||
// without suffix
|
||||
{
|
||||
After: []string{"latest"},
|
||||
},
|
||||
{
|
||||
Before: "refs/tags/v1.0.0",
|
||||
After: []string{
|
||||
"1",
|
||||
"1.0",
|
||||
"1.0.0",
|
||||
},
|
||||
},
|
||||
// with suffix
|
||||
{
|
||||
Suffix: "linux-amd64",
|
||||
After: []string{"linux-amd64"},
|
||||
},
|
||||
{
|
||||
Before: "refs/tags/v1.0.0",
|
||||
Suffix: "linux-amd64",
|
||||
After: []string{
|
||||
"1-linux-amd64",
|
||||
"1.0-linux-amd64",
|
||||
"1.0.0-linux-amd64",
|
||||
},
|
||||
},
|
||||
{
|
||||
Suffix: "nanoserver",
|
||||
After: []string{"nanoserver"},
|
||||
},
|
||||
{
|
||||
Before: "refs/tags/v1.9.2",
|
||||
Suffix: "nanoserver",
|
||||
After: []string{
|
||||
"1-nanoserver",
|
||||
"1.9-nanoserver",
|
||||
"1.9.2-nanoserver",
|
||||
},
|
||||
},
|
||||
{
|
||||
Before: "refs/tags/v18.06.0",
|
||||
Suffix: "nanoserver",
|
||||
After: []string{
|
||||
"18-nanoserver",
|
||||
"18.06-nanoserver",
|
||||
"18.06.0-nanoserver",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
tag, err := DefaultTagSuffix(test.Before, test.Suffix)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
got, want := tag, test.After
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("Got tag %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_stripHeadPrefix(t *testing.T) {
|
||||
type args struct {
|
||||
ref string
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
args: args{
|
||||
ref: "refs/heads/main",
|
||||
},
|
||||
want: "main",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
if got := stripHeadPrefix(tt.args.ref); got != tt.want {
|
||||
t.Errorf("stripHeadPrefix() = %v, want %v", got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUseDefaultTag(t *testing.T) {
|
||||
type args struct {
|
||||
ref string
|
||||
defaultBranch string
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "latest tag for default branch",
|
||||
args: args{
|
||||
ref: "refs/heads/main",
|
||||
defaultBranch: "main",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "build from tags",
|
||||
args: args{
|
||||
ref: "refs/tags/v1.0.0",
|
||||
defaultBranch: "main",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "skip build for not default branch",
|
||||
args: args{
|
||||
ref: "refs/heads/develop",
|
||||
defaultBranch: "main",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
if got := UseDefaultTag(tt.args.ref, tt.args.defaultBranch); got != tt.want {
|
||||
t.Errorf("%q. UseDefaultTag() = %v, want %v", tt.name, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user