@@ -18,6 +18,7 @@ package v1beta2
1818
1919import (
2020 "path"
21+ "regexp"
2122 "strings"
2223
2324 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -43,8 +44,14 @@ type Artifact struct {
4344 Revision string `json:"revision"`
4445
4546 // Checksum is the SHA256 checksum of the Artifact file.
47+ // Deprecated: use Artifact.Digest instead.
4648 // +optional
47- Checksum string `json:"checksum"`
49+ Checksum string `json:"checksum,omitempty"`
50+
51+ // Digest is the digest of the file in the form of '<algorithm>:<checksum>'.
52+ // +optional
53+ // +kubebuilder:validation:Pattern="^[a-z0-9]+(?:[.+_-][a-z0-9]+)*:[a-zA-Z0-9=_-]+$"
54+ Digest string `json:"digest"`
4855
4956 // LastUpdateTime is the timestamp corresponding to the last update of the
5057 // Artifact.
@@ -66,7 +73,7 @@ func (in *Artifact) HasRevision(revision string) bool {
6673 if in == nil {
6774 return false
6875 }
69- return in .Revision == revision
76+ return TransformLegacyRevision ( in .Revision ) == TransformLegacyRevision ( revision )
7077}
7178
7279// HasChecksum returns if the given checksum matches the current Checksum of
@@ -90,3 +97,60 @@ func ArtifactDir(kind, namespace, name string) string {
9097func ArtifactPath (kind , namespace , name , filename string ) string {
9198 return path .Join (ArtifactDir (kind , namespace , name ), filename )
9299}
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