@@ -18,6 +18,7 @@ package v1beta2
18
18
19
19
import (
20
20
"path"
21
+ "regexp"
21
22
"strings"
22
23
23
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -72,7 +73,7 @@ func (in *Artifact) HasRevision(revision string) bool {
72
73
if in == nil {
73
74
return false
74
75
}
75
- return in .Revision == revision
76
+ return TransformLegacyRevision ( in .Revision ) == TransformLegacyRevision ( revision )
76
77
}
77
78
78
79
// HasChecksum returns if the given checksum matches the current Checksum of
@@ -96,3 +97,60 @@ func ArtifactDir(kind, namespace, name string) string {
96
97
func ArtifactPath (kind , namespace , name , filename string ) string {
97
98
return path .Join (ArtifactDir (kind , namespace , name ), filename )
98
99
}
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
+ }
0 commit comments