|
| 1 | +/* |
| 2 | +Copyright 2024 The Tekton 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 types |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/google/go-cmp/cmp" |
| 21 | +) |
| 22 | + |
| 23 | +// Algorithm Standard cryptographic hash algorithm |
| 24 | +type Algorithm string |
| 25 | + |
| 26 | +// Artifact represents an artifact within a system, potentially containing multiple values |
| 27 | +// associated with it. |
| 28 | +type Artifact struct { |
| 29 | + // The artifact's identifying category name |
| 30 | + Name string `json:"name,omitempty"` |
| 31 | + // A collection of values related to the artifact |
| 32 | + Values []ArtifactValue `json:"values,omitempty"` |
| 33 | + // Indicate if the artifact is a build output or a by-product |
| 34 | + BuildOutput bool `json:"buildOutput,omitempty"` |
| 35 | +} |
| 36 | + |
| 37 | +// ArtifactValue represents a specific value or data element within an Artifact. |
| 38 | +type ArtifactValue struct { |
| 39 | + Digest map[Algorithm]string `json:"digest,omitempty"` // Algorithm-specific digests for verifying the content (e.g., SHA256) |
| 40 | + Uri string `json:"uri,omitempty"` // Location where the artifact value can be retrieved |
| 41 | +} |
| 42 | + |
| 43 | +// TaskRunStepArtifact represents an artifact produced or used by a step within a task run. |
| 44 | +// It directly uses the Artifact type for its structure. |
| 45 | +type TaskRunStepArtifact = Artifact |
| 46 | + |
| 47 | +// Artifacts represents the collection of input and output artifacts associated with |
| 48 | +// a task run or a similar process. Artifacts in this context are units of data or resources |
| 49 | +// that the process either consumes as input or produces as output. |
| 50 | +type Artifacts struct { |
| 51 | + Inputs []Artifact `json:"inputs,omitempty"` |
| 52 | + Outputs []Artifact `json:"outputs,omitempty"` |
| 53 | +} |
| 54 | + |
| 55 | +func (a *Artifacts) Merge(another *Artifacts) { |
| 56 | + inputMap := make(map[string][]ArtifactValue) |
| 57 | + var newInputs []Artifact |
| 58 | + |
| 59 | + for _, v := range a.Inputs { |
| 60 | + inputMap[v.Name] = v.Values |
| 61 | + } |
| 62 | + if another != nil { |
| 63 | + for _, v := range another.Inputs { |
| 64 | + _, ok := inputMap[v.Name] |
| 65 | + if !ok { |
| 66 | + inputMap[v.Name] = []ArtifactValue{} |
| 67 | + } |
| 68 | + for _, vv := range v.Values { |
| 69 | + exists := false |
| 70 | + for _, av := range inputMap[v.Name] { |
| 71 | + if cmp.Equal(vv, av) { |
| 72 | + exists = true |
| 73 | + break |
| 74 | + } |
| 75 | + } |
| 76 | + if !exists { |
| 77 | + inputMap[v.Name] = append(inputMap[v.Name], vv) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + for k, v := range inputMap { |
| 84 | + newInputs = append(newInputs, Artifact{ |
| 85 | + Name: k, |
| 86 | + Values: v, |
| 87 | + }) |
| 88 | + } |
| 89 | + |
| 90 | + outputMap := make(map[string]Artifact) |
| 91 | + var newOutputs []Artifact |
| 92 | + for _, v := range a.Outputs { |
| 93 | + outputMap[v.Name] = v |
| 94 | + } |
| 95 | + |
| 96 | + if another != nil { |
| 97 | + for _, v := range another.Outputs { |
| 98 | + _, ok := outputMap[v.Name] |
| 99 | + if !ok { |
| 100 | + outputMap[v.Name] = Artifact{Name: v.Name, Values: []ArtifactValue{}, BuildOutput: v.BuildOutput} |
| 101 | + } |
| 102 | + // only update buildOutput to true. |
| 103 | + // Do not convert to false if it was true before. |
| 104 | + if v.BuildOutput { |
| 105 | + art := outputMap[v.Name] |
| 106 | + art.BuildOutput = v.BuildOutput |
| 107 | + outputMap[v.Name] = art |
| 108 | + } |
| 109 | + for _, vv := range v.Values { |
| 110 | + exists := false |
| 111 | + for _, av := range outputMap[v.Name].Values { |
| 112 | + if cmp.Equal(vv, av) { |
| 113 | + exists = true |
| 114 | + break |
| 115 | + } |
| 116 | + } |
| 117 | + if !exists { |
| 118 | + art := outputMap[v.Name] |
| 119 | + art.Values = append(art.Values, vv) |
| 120 | + outputMap[v.Name] = art |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + for _, v := range outputMap { |
| 127 | + newOutputs = append(newOutputs, Artifact{ |
| 128 | + Name: v.Name, |
| 129 | + Values: v.Values, |
| 130 | + BuildOutput: v.BuildOutput, |
| 131 | + }) |
| 132 | + } |
| 133 | + a.Inputs = newInputs |
| 134 | + a.Outputs = newOutputs |
| 135 | +} |
0 commit comments