Skip to content

Commit

Permalink
Merge pull request #24 from metal-stack/versions-without-leading-v
Browse files Browse the repository at this point in the history
Allow version tags not to start with "v".
  • Loading branch information
Gerrit91 committed Sep 7, 2020
2 parents 844a7ed + 2c668d0 commit 724128f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
14 changes: 6 additions & 8 deletions pkg/webhooks/github/actions/release_vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ func (r *ReleaseVector) AddToRelaseVector(ctx context.Context, p *ReleaseVectorP
r.logger.Debugw("skip adding new version to release vector because not a release vector repo", "repo", p.RepositoryName, "release", p.TagName)
return nil
}

tag := p.TagName
if !strings.HasPrefix(tag, "v") {
r.logger.Debugw("skip adding new version to release vector because not starting with v", "repo", p.RepositoryName, "release", tag)
trimmed := strings.TrimPrefix(tag, "v")
_, err := semver.Make(trimmed)
if err != nil {
r.logger.Infow("skip adding new version to release vector because not a valid semver release tag", "repo", p.RepositoryName, "release", tag, "parse-error", err)
return nil
}

Expand All @@ -112,11 +115,6 @@ func (r *ReleaseVector) AddToRelaseVector(ctx context.Context, p *ReleaseVectorP
lock.Lock()
defer once.Do(func() { lock.Unlock() })

version, err := semver.Make(tag[1:])
if err != nil {
return errors.Wrap(err, "not a valid semver release tag")
}

token, err := r.client.GitToken(ctx)
if err != nil {
return errors.Wrap(err, "error creating git token")
Expand All @@ -142,7 +140,7 @@ func (r *ReleaseVector) AddToRelaseVector(ctx context.Context, p *ReleaseVectorP
}

for _, patch := range patches {
err = patch.Apply(reader, writer, version.String())
err = patch.Apply(reader, writer, tag)
if err != nil {
return errors.Wrap(err, "error applying release updates")
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/webhooks/modifiers/file-patchers/yaml_path_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func (p YAMLPathPatch) Apply(cn ContentReader, cw ContentWriter, newValue string
}

if p.versionCompare {
newValue = strings.TrimPrefix(newValue, "v")
trimmedValue := strings.TrimPrefix(newValue, "v")

newVersion, err := semver.Parse(newValue)
newVersion, err := semver.Parse(trimmedValue)
if err != nil {
return err
}
Expand All @@ -86,8 +86,6 @@ func (p YAMLPathPatch) Apply(cn ContentReader, cw ContentWriter, newValue string
return nil
}
}

newValue = "v" + newValue
}

if p.template != nil {
Expand Down
22 changes: 7 additions & 15 deletions pkg/webhooks/modifiers/file-patchers/yaml_path_patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,25 @@ func TestYAMLPathVersionPatches_Apply(t *testing.T) {
{
name: "replace a path with version comparison",
p: YAMLPathPatch{file: "example.yaml", yamlPath: "a", versionCompare: true},
newValue: "0.0.2",
newValue: "v0.0.2",
input: "a: v0.0.1",
output: "a: v0.0.2\n",
wantErr: false,
},
{
name: "replace a path with version comparison when there was no semantic version before",
p: YAMLPathPatch{file: "example.yaml", yamlPath: "a", versionCompare: true},
newValue: "0.0.2",
newValue: "v0.0.2",
input: "a: bla",
output: "a: v0.0.2\n",
wantErr: false,
},
{
name: "replace a path without original prefix with version comparison",
p: YAMLPathPatch{file: "example.yaml", yamlPath: "a", versionCompare: true},
newValue: "0.0.2",
input: "a: 0.0.1",
output: "a: v0.0.2\n",
wantErr: false,
},
{
name: "replace a path with no prefix with version comparison",
p: YAMLPathPatch{file: "example.yaml", yamlPath: "a", versionCompare: true},
newValue: "0.0.2",
input: "a: 0.0.1",
output: "a: v0.0.2\n",
output: "a: 0.0.2\n",
wantErr: false,
},
{
Expand All @@ -144,25 +136,25 @@ func TestYAMLPathVersionPatches_Apply(t *testing.T) {
{
name: "replace with a template and version comparison",
p: YAMLPathPatch{file: "example.yaml", yamlPath: "a", template: &tpl, versionCompare: true},
newValue: "0.0.2",
newValue: "v0.0.2",
input: "a: http://server.io/v0.0.1.exe",
output: "a: http://server.io/v0.0.2.exe\n",
wantErr: false,
},
{
name: "change nothing on lower version with template",
p: YAMLPathPatch{file: "example.yaml", yamlPath: "a", template: &tpl, versionCompare: true},
newValue: "0.0.2",
newValue: "v0.0.2",
input: "a: http://server.io/v0.0.3.exe",
output: "a: http://server.io/v0.0.3.exe\n",
wantErr: false,
},
{
name: "replace with a template and version comparison when there was no semantic version before",
name: "replace with a template and version comparison when there is no semantic version",
p: YAMLPathPatch{file: "example.yaml", yamlPath: "a", template: &tpl, versionCompare: true},
newValue: "0.0.2",
input: "a: http://server.io/bla.exe",
output: "a: http://server.io/v0.0.2.exe\n",
output: "a: http://server.io/0.0.2.exe\n",
wantErr: false,
},
}
Expand Down

0 comments on commit 724128f

Please sign in to comment.