Skip to content

Commit eaa4a4f

Browse files
committed
api: introduce TransformLegacyRevision helper
This allows consumers to better handle the transition to the new RFC-0005 format ("/" -> "@" separation). Signed-off-by: Hidde Beydals <[email protected]>
1 parent 469c938 commit eaa4a4f

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

api/v1beta2/artifact_types.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1beta2
1818

1919
import (
2020
"path"
21+
"regexp"
2122
"strings"
2223

2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -72,7 +73,7 @@ func (in *Artifact) HasRevision(revision string) bool {
7273
if in == nil {
7374
return false
7475
}
75-
return in.Revision == revision
76+
return TransformLegacyRevision(in.Revision) == TransformLegacyRevision(revision)
7677
}
7778

7879
// HasChecksum returns if the given checksum matches the current Checksum of
@@ -96,3 +97,60 @@ func ArtifactDir(kind, namespace, name string) string {
9697
func ArtifactPath(kind, namespace, name, filename string) string {
9798
return path.Join(ArtifactDir(kind, namespace, name), filename)
9899
}
100+
101+
// TransformLegacyRevision transforms a "legacy" revision string into a "new"
102+
// revision string. It accepts the following formats:
103+
//
104+
// - main/5394cb7f48332b2de7c17dd8b8384bbc84b7e738
105+
// - feature/branch/5394cb7f48332b2de7c17dd8b8384bbc84b7e738
106+
// - HEAD/5394cb7f48332b2de7c17dd8b8384bbc84b7e738
107+
// - tag/55609ff9d959589ed917ce32e6bc0f0a36809565f308602c15c3668965979edc
108+
// - d52bde83c5b2bd0fa7910264e0afc3ac9cfe9b6636ca29c05c09742f01d5a4bd
109+
//
110+
// Which are transformed into the following formats respectively:
111+
//
112+
// - main@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738
113+
// - feature/branch@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738
114+
// - sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738
115+
// - tag@sha256:55609ff9d959589ed917ce32e6bc0f0a36809565f308602c15c3668965979edc
116+
// - sha256:d52bde83c5b2bd0fa7910264e0afc3ac9cfe9b6636ca29c05c09742f01d5a4bd
117+
//
118+
// Deprecated, this function exists for backwards compatibility with existing
119+
// resources, and to provide a transition period. Will be removed in a future
120+
// release.
121+
func TransformLegacyRevision(rev string) string {
122+
if rev != "" && strings.LastIndex(rev, ":") == -1 {
123+
if i := strings.LastIndex(rev, "/"); i >= 0 {
124+
sha := rev[i+1:]
125+
if algo := determineSHAType(sha); algo != "" {
126+
if name := rev[:i]; name != "HEAD" {
127+
return name + "@" + algo + ":" + sha
128+
}
129+
return algo + ":" + sha
130+
}
131+
}
132+
if algo := determineSHAType(rev); algo != "" {
133+
return algo + ":" + rev
134+
}
135+
}
136+
return rev
137+
}
138+
139+
// isAlphaNumHex returns true if the given string only contains 0-9 and a-f
140+
// characters.
141+
var isAlphaNumHex = regexp.MustCompile(`^[0-9a-f]+$`).MatchString
142+
143+
// determineSHAType returns the SHA algorithm used to compute the provided hex.
144+
// The determination is heuristic and based on the length of the hex string. If
145+
// the size is not recognized, an empty string is returned.
146+
func determineSHAType(hex string) string {
147+
if isAlphaNumHex(hex) {
148+
switch len(hex) {
149+
case 40:
150+
return "sha1"
151+
case 64:
152+
return "sha256"
153+
}
154+
}
155+
return ""
156+
}

api/v1beta2/artifact_types_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright 2023 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta2
18+
19+
import "testing"
20+
21+
func TestTransformLegacyRevision(t *testing.T) {
22+
tests := []struct {
23+
rev string
24+
want string
25+
}{
26+
{
27+
rev: "HEAD/5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
28+
want: "sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
29+
},
30+
{
31+
rev: "main/5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
32+
want: "main@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
33+
},
34+
{
35+
rev: "main@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
36+
want: "main@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
37+
},
38+
{
39+
rev: "feature/branch/5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
40+
want: "feature/branch@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
41+
},
42+
{
43+
rev: "feature/branch@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
44+
want: "feature/branch@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
45+
},
46+
{
47+
rev: "5ac85ca617f3774baff4ae0a420b810b2546dbc9af9f346b1d55c5ed9873c55c",
48+
want: "sha256:5ac85ca617f3774baff4ae0a420b810b2546dbc9af9f346b1d55c5ed9873c55c",
49+
},
50+
{
51+
rev: "v1.0.0",
52+
want: "v1.0.0",
53+
},
54+
{
55+
rev: "v1.0.0-rc1",
56+
want: "v1.0.0-rc1",
57+
},
58+
{
59+
rev: "v1.0.0-rc1+metadata",
60+
want: "v1.0.0-rc1+metadata",
61+
},
62+
{
63+
rev: "arbitrary/revision",
64+
want: "arbitrary/revision",
65+
},
66+
{
67+
rev: "5394cb7f48332b2de7c17dd8b8384bbc84b7xxxx",
68+
want: "5394cb7f48332b2de7c17dd8b8384bbc84b7xxxx",
69+
},
70+
}
71+
for _, tt := range tests {
72+
t.Run(tt.rev, func(t *testing.T) {
73+
if got := TransformLegacyRevision(tt.rev); got != tt.want {
74+
t.Errorf("TransformLegacyRevision() = %v, want %v", got, tt.want)
75+
}
76+
})
77+
}
78+
}

0 commit comments

Comments
 (0)