Skip to content

Commit 7bd7d28

Browse files
authored
fix(gittag) ensure gittag targets does not fail due to (unspecified) version filters (updatecli#1407)
* Ensure that the gittag end-to-end test is valid and covers all cases Signed-off-by: Damien Duportal <damien.duportal@gmail.com> * chore(versionFilter) export errors as reusable types Signed-off-by: Damien Duportal <damien.duportal@gmail.com> * fix(gittag) ensure gittag target does not errors with default versionFilters Signed-off-by: Damien Duportal <damien.duportal@gmail.com> * fixup Signed-off-by: Damien Duportal <damien.duportal@gmail.com> --------- Signed-off-by: Damien Duportal <damien.duportal@gmail.com>
1 parent ef102c4 commit 7bd7d28

8 files changed

Lines changed: 147 additions & 47 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ coverage.txt
3333

3434
manpages/*
3535
completions/*
36+
*.log
37+
e2e/test_results_e2e*yaml

e2e/updatecli.d/success.d/gitTag.yaml

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: End to end test of the gittag resource
2+
3+
scms:
4+
github-repo:
5+
kind: github
6+
spec:
7+
branch: main
8+
owner: updatecli
9+
repository: updatecli
10+
token: '{{ requiredEnv "GITHUB_TOKEN" }}'
11+
username: '{{ requiredEnv "GITHUB_ACTOR" }}'
12+
git-repo:
13+
kind: git
14+
spec:
15+
branch: main
16+
url: https://github.com/updatecli/updatecli.git
17+
18+
sources:
19+
src-git:
20+
name: Get the latest git tag through git
21+
kind: gittag
22+
scmid: git-repo
23+
src-github:
24+
name: Get the latest git tag through GitHub
25+
kind: gittag
26+
scmid: github-repo
27+
echo-version:
28+
name: Returns a (SemVer) valid version to be used with the gittag targets, which must not exist in the repositories
29+
kind: shell
30+
spec:
31+
command: echo "0.0.1"
32+
33+
34+
conditions:
35+
check-git:
36+
name: Check the tag 'v0.30.0' exists in the git repository
37+
kind: gittag
38+
scmid: git-repo
39+
disablesourceinput: true
40+
spec:
41+
versionfilter:
42+
pattern: 'v0.30.0'
43+
check-github:
44+
name: Check the tag 'v0.30.0' exists in the GitHub repository
45+
kind: gittag
46+
scmid: github-repo
47+
disablesourceinput: true
48+
spec:
49+
versionfilter:
50+
pattern: 'v0.30.0'
51+
52+
targets:
53+
create-git-tag:
54+
name: Create the git tag 'v0.0.1' in the git repository
55+
kind: gittag
56+
scmid: git-repo
57+
sourceid: echo-version
58+
spec:
59+
message: "Created by Updatecli"
60+
create-git-tag-already-exist:
61+
name: Create the (already existing) latest tag in the git repository
62+
kind: gittag
63+
scmid: git-repo
64+
sourceid: src-git
65+
spec:
66+
message: "Created by Updatecli"
67+
create-github-tag:
68+
name: Create the git tag 'v0.0.1' in the GitHub repository
69+
kind: gittag
70+
scmid: github-repo
71+
sourceid: echo-version
72+
spec:
73+
message: "Created by Updatecli"

pkg/plugins/resources/gittag/target.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/sirupsen/logrus"
77
"github.com/updatecli/updatecli/pkg/core/pipeline/scm"
88
"github.com/updatecli/updatecli/pkg/core/result"
9+
"github.com/updatecli/updatecli/pkg/plugins/utils/version"
910
)
1011

1112
// Target creates a tag if needed from a local git repository, without pushing the tag
@@ -55,7 +56,7 @@ func (gt *GitTag) target(source string, dryRun bool, resultTarget *result.Target
5556
resultTarget.Files = []string{""}
5657

5758
// Fail if a pattern is specified
58-
if gt.versionFilter.Pattern != "" {
59+
if gt.spec.VersionFilter.Pattern != "" {
5960
return fmt.Errorf("target validation error: spec.versionfilter.pattern is not allowed for targets of type gittag")
6061
}
6162

@@ -73,7 +74,8 @@ func (gt *GitTag) target(source string, dryRun bool, resultTarget *result.Target
7374
}
7475

7576
gt.foundVersion, err = gt.versionFilter.Search(tags)
76-
if err != nil && err.Error() != fmt.Sprintf("No version found matching pattern %q", source) {
77+
notFoundError := &version.ErrNoVersionFoundForPattern{Pattern: source}
78+
if err != nil && err.Error() != notFoundError.Error() {
7779
return err
7880
}
7981

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package version
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var (
9+
// ErrNoVersionFound return a error when no version couldn't be found
10+
ErrNoVersionFound error = errors.New("no version found")
11+
// ErrNoVersionsFound return a error when the versions list is empty
12+
ErrNoVersionsFound error = errors.New("versions list empty")
13+
// ErrNoValidSemVerFound return a error when the versions list is empty
14+
ErrNoValidSemVerFound error = errors.New("no valid semantic version found")
15+
)
16+
17+
// ErrNoVersionFoundForPattern returns when a given pattern does not find any version
18+
type ErrNoVersionFoundForPattern struct {
19+
Pattern string
20+
}
21+
22+
func (e *ErrNoVersionFoundForPattern) Error() string {
23+
return fmt.Sprintf("no version found matching pattern %q", e.Pattern)
24+
}
25+
26+
// ErrUnsupportedVersionKind returns when the provided version filter is unsupported
27+
type ErrUnsupportedVersionKind struct {
28+
Kind string
29+
}
30+
31+
func (e *ErrUnsupportedVersionKind) Error() string {
32+
return fmt.Sprintf("unsupported version kind %q", e.Kind)
33+
}
34+
35+
// ErrUnsupportedVersionKind returns when the provided combination of version and pattern filter is unsupported
36+
type ErrUnsupportedVersionKindPattern struct {
37+
Kind string
38+
Pattern string
39+
}
40+
41+
func (e *ErrUnsupportedVersionKindPattern) Error() string {
42+
return fmt.Sprintf("unsupported version kind %q with pattern %q", e.Kind, e.Pattern)
43+
}
44+
45+
// ErrUnsupportedVersionKind returns when the provided combination of version and pattern filter is unsupported
46+
type ErrIncorrectSemVerConstraint struct {
47+
SemVerConstraint string
48+
}
49+
50+
func (e *ErrIncorrectSemVerConstraint) Error() string {
51+
return fmt.Sprintf("wrong semantic versioning constraint %q", e.SemVerConstraint)
52+
}

pkg/plugins/utils/version/filter.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (f Filter) Validate() error {
6868
}
6969
}
7070
if !ok {
71-
return fmt.Errorf("unsupported version kind %q", f.Kind)
71+
return &ErrUnsupportedVersionKind{Kind: f.Kind}
7272
}
7373
return nil
7474
}
@@ -124,10 +124,10 @@ func (f *Filter) Search(versions []string) (Version, error) {
124124

125125
return s.FoundVersion, nil
126126
default:
127-
return foundVersion, fmt.Errorf("unsupported version kind %q with pattern %q", f.Kind, f.Pattern)
127+
return foundVersion, &ErrUnsupportedVersionKindPattern{Pattern: f.Pattern, Kind: f.Kind}
128128
}
129129

130-
return foundVersion, fmt.Errorf("no version found matching pattern %q", f.Pattern)
130+
return foundVersion, &ErrNoVersionFoundForPattern{Pattern: f.Pattern}
131131
}
132132

133133
// IsZero return true if filter is not initialized
@@ -218,13 +218,13 @@ func (f *Filter) GreaterThanPattern(version string) (string, error) {
218218
fmt.Println(version)
219219
_, err = sv.NewConstraint(version)
220220
if err != nil {
221-
return "", fmt.Errorf("wrong semantic versioning constraint %q", version)
221+
return "", &ErrIncorrectSemVerConstraint{SemVerConstraint: version}
222222
}
223223
return version, nil
224224

225225
default:
226226
return f.Pattern, nil
227227
}
228228
}
229-
return "", fmt.Errorf("kind %q not supported", f.Kind)
229+
return "", &ErrUnsupportedVersionKind{Kind: f.Kind}
230230
}

pkg/plugins/utils/version/filter_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package version
22

33
import (
44
"errors"
5-
"fmt"
65
"testing"
76

87
"github.com/stretchr/testify/assert"
@@ -105,7 +104,7 @@ func TestSearch(t *testing.T) {
105104
},
106105
versions: []string{"updatecli-1.0", "updatecli-2.0", "updatecli-3.0"},
107106
want: Version{},
108-
wantErr: fmt.Errorf(`no version found matching pattern "^updatecli-4.(\\d*)$"`),
107+
wantErr: &ErrNoVersionFoundForPattern{Pattern: "^updatecli-4.(\\d*)$"},
109108
},
110109
}
111110
for _, tt := range tests {
@@ -152,7 +151,7 @@ func TestValidate(t *testing.T) {
152151
Kind: "noExist",
153152
Pattern: "~2",
154153
},
155-
wantErr: errors.New(`unsupported version kind "noExist"`),
154+
wantErr: &ErrUnsupportedVersionKind{Kind: "noExist"},
156155
},
157156
}
158157
for _, tt := range tests {
@@ -306,23 +305,28 @@ func TestGreaterThanPattern(t *testing.T) {
306305
Kind: SEMVERVERSIONKIND,
307306
Pattern: "*",
308307
},
309-
version: "v0.0.0-20220606043923-3cf50f8a0a29", want: ">=0.0.0-20220606043923-3cf50f8a0a29",
308+
version: "v0.0.0-20220606043923-3cf50f8a0a29",
309+
want: ">=0.0.0-20220606043923-3cf50f8a0a29",
310310
},
311311
{
312312
name: "Wrong Semver Version",
313313
filter: Filter{
314314
Kind: SEMVERVERSIONKIND,
315315
Pattern: "*",
316316
},
317-
version: "v0.0.0_20220606043923-3cf50f8a0a29", want: "", wantErr: errors.New("wrong semantic versioning constraint \"v0.0.0_20220606043923-3cf50f8a0a29\""),
317+
version: "v0.0.0_20220606043923-3cf50f8a0a29",
318+
want: "",
319+
wantErr: &ErrIncorrectSemVerConstraint{SemVerConstraint: "v0.0.0_20220606043923-3cf50f8a0a29"},
318320
},
319321
{
320322
name: "Wrong Semver Constraint",
321323
filter: Filter{
322324
Kind: SEMVERVERSIONKIND,
323325
Pattern: "*",
324326
},
325-
version: "1.0 - 2.0 !!!", want: "", wantErr: errors.New("wrong semantic versioning constraint \"1.0 - 2.0 !!!\""),
327+
version: "1.0 - 2.0 !!!",
328+
want: "",
329+
wantErr: &ErrIncorrectSemVerConstraint{SemVerConstraint: "1.0 - 2.0 !!!"},
326330
},
327331
}
328332
for _, tt := range tests {

pkg/plugins/utils/version/semver.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package version
22

33
import (
4-
"errors"
5-
"fmt"
64
"sort"
75

86
sv "github.com/Masterminds/semver/v3"
@@ -17,13 +15,6 @@ type Semver struct {
1715
Strict bool
1816
}
1917

20-
var (
21-
// ErrNoVersionFound return a error when no version couldn't be found
22-
ErrNoVersionFound error = errors.New("no version found")
23-
// ErrNoVersionsFound return a error when the versions list is empty
24-
ErrNoVersionsFound error = errors.New("versions list empty")
25-
)
26-
2718
// Init creates a new semver object
2819
func (s *Semver) Init(versions []string) error {
2920

@@ -49,7 +40,7 @@ func (s *Semver) Init(versions []string) error {
4940
return nil
5041
}
5142

52-
return fmt.Errorf("no valid semantic version found")
43+
return ErrNoValidSemVerFound
5344
}
5445

5546
// Sort re-order a list of versions with the newest version first

0 commit comments

Comments
 (0)