github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/harness/yaml/step.go (about) 1 // Copyright 2022 Harness, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package yaml 16 17 import ( 18 "encoding/json" 19 "fmt" 20 ) 21 22 type ( 23 Step struct { // TODO missing failure strategies 24 ID string `json:"identifier,omitempty" yaml:"identifier,omitempty"` 25 Description string `json:"description,omitempty" yaml:"description,omitempty"` 26 Name string `json:"name,omitempty" yaml:"name,omitempty"` 27 Skip string `json:"skipCondition,omitempty" yaml:"skipCondition,omitempty"` 28 Spec interface{} `json:"spec,omitempty" yaml:"spec,omitempty"` 29 Timeout Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"` 30 Type string `json:"type,omitempty" yaml:"type,omitempty"` 31 When *StepWhen `json:"when,omitempty" yaml:"when,omitempty"` 32 Env map[string]string `json:"envVariables,omitempty" yaml:"envVariables,omitempty"` 33 Strategy *Strategy `json:"strategy,omitempty" yaml:"strategy,omitempty"` 34 } 35 36 // 37 // Step specifications 38 // 39 40 StepArtifactoryUpload struct { 41 // TODO 42 } 43 44 StepBarrier struct { 45 // TODO 46 } 47 48 StepBuildAndPushECR struct { 49 // TODO 50 } 51 52 StepBuildAndPushGCR struct { 53 // TODO 54 } 55 56 StepFlagConfiguration struct { 57 // TODO 58 } 59 60 StepGCSUpload struct { 61 // TODO 62 } 63 64 StepHarnessApproval struct { 65 ApprovalMessage string `json:"approvalMessage,omitempty" yaml:"approvalMessage,omitempty"` 66 Approvers *Approvers `json:"approvers,omitempty" yaml:"approvers,omitempty"` 67 ApproverInputs []*ApproverInput `json:"approverInputs,omitempty" yaml:"approverInputs,omitempty"` 68 IncludePipelineExecutionHistory string `json:"includePipelineExecutionHistory,omitempty" yaml:"includePipelineExecutionHistory,omitempty"` 69 } 70 71 StepRestoreCacheGCS struct { 72 // TODO 73 } 74 75 StepRestoreCacheS3 struct { 76 // TODO 77 } 78 79 StepRunTests struct { 80 // TODO 81 } 82 83 StepSaveCacheGCS struct { 84 // TODO 85 } 86 87 StepSaveCacheS3 struct { 88 // TODO 89 } 90 91 StepDocker struct { 92 BuildsArgs map[string]string `json:"buildArgs,omitempty" yaml:"buildArgs,omitempty"` 93 ConnectorRef string `json:"connectorRef,omitempty" yaml:"connectorRef,omitempty"` 94 Context string `json:"context,omitempty" yaml:"context,omitempty"` 95 Dockerfile string `json:"dockerfile,omitempty" yaml:"dockerfile,omitempty"` 96 Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` 97 Optimize bool `json:"optimize,omitempty" yaml:"optimize,omitempty"` 98 Privileged bool `json:"privileged,omitempty" yaml:"privileged,omitempty"` 99 RemoteCacheRepo string `json:"remoteCacheRepo,omitempty" yaml:"remoteCacheRepo,omitempty"` 100 Repo string `json:"repo,omitempty" yaml:"repo,omitempty"` 101 Reports []*Report `json:"reports,omitempty" yaml:"reports,omitempty"` 102 Resources *Resources `json:"resources,omitempty" yaml:"resources,omitempty"` 103 RunAsUser string `json:"runAsUser,omitempty" yaml:"runAsUser,omitempty"` 104 Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"` 105 Target string `json:"target,omitempty" yaml:"target,omitempty"` 106 Caching bool `json:"caching,omitempty" yaml:"caching,omitempty"` 107 } 108 109 StepHTTP struct { 110 URL string `json:"url,omitempty" yaml:"url,omitempty"` 111 Method string `json:"method,omitempty" yaml:"method,omitempty"` 112 Headers []*Variable `json:"headers,omitempty" yaml:"headers,omitempty"` 113 OutputVariables []*Variable `json:"outputVariables,omitempty" yaml:"outputVariables,omitempty"` 114 RequestBody string `json:"requestBody,omitempty" yaml:"requestBody,omitempty"` 115 Assertion string `json:"assertion,omitempty" yaml:"assertion,omitempty"` 116 117 // NOTE the below fields are not part of the 118 // official schema, however, they are useful for 119 // executing docker pipelines. 120 121 ConnRef string `json:"connectorRef,omitempty" yaml:"connectorRef,omitempty"` 122 Image string `json:"image,omitempty" yaml:"image,omitempty"` 123 ImagePullPolicy string `json:"imagePullPolicy,omitempty" yaml:"imagePullPolicy,omitempty"` 124 RunAsUser string `json:"runAsUser,omitempty" yaml:"runAsUser,omitempty"` 125 } 126 127 StepPlugin struct { 128 Env map[string]string `json:"envVariables,omitempty" yaml:"envVariables,omitempty"` 129 ConnRef string `json:"connectorRef,omitempty" yaml:"connectorRef,omitempty"` 130 Image string `json:"image,omitempty" yaml:"image,omitempty"` 131 ImagePullPolicy string `json:"imagePullPolicy,omitempty" yaml:"imagePullPolicy,omitempty"` 132 Privileged bool `json:"privileged,omitempty" yaml:"privileged,omitempty"` 133 Reports []*Report `json:"reports,omitempty" yaml:"reports,omitempty"` 134 Resources *Resources `json:"resources,omitempty" yaml:"resources,omitempty"` 135 RunAsUser string `json:"runAsUser,omitempty" yaml:"runAsUser,omitempty"` 136 Settings map[string]interface{} `json:"settings,omitempty" yaml:"settings,omitempty"` 137 } 138 139 StepAction struct { 140 Uses string `json:"uses,omitempty" yaml:"uses,omitempty"` 141 With map[string]interface{} `json:"with,omitempty" yaml:"with,omitempty"` 142 Envs map[string]string `json:"env,omitempty" yaml:"env,omitempty"` 143 } 144 145 StepBitrise struct { 146 Uses string `json:"uses,omitempty" yaml:"uses,omitempty"` 147 With map[string]interface{} `json:"with,omitempty" yaml:"with,omitempty"` 148 Envs map[string]string `json:"env,omitempty" yaml:"env,omitempty"` 149 } 150 151 StepRun struct { 152 Env map[string]string `json:"envVariables,omitempty" yaml:"envVariables,omitempty"` 153 Command string `json:"command,omitempty" yaml:"command,omitempty"` 154 ConnRef string `json:"connectorRef,omitempty" yaml:"connectorRef,omitempty"` 155 Image string `json:"image,omitempty" yaml:"image,omitempty"` 156 ImagePullPolicy string `json:"imagePullPolicy,omitempty" yaml:"imagePullPolicy,omitempty"` 157 Outputs []string `json:"outputVariables,omitempty" yaml:"outputVariables,omitempty"` 158 Privileged bool `json:"privileged,omitempty" yaml:"privileged,omitempty"` 159 Resources *Resources `json:"resources,omitempty" yaml:"resources,omitempty"` 160 RunAsUser string `json:"runAsUser,omitempty" yaml:"runAsUser,omitempty"` 161 Reports *Report `json:"reports,omitempty" yaml:"reports,omitempty"` 162 Shell string `json:"shell,omitempty" yaml:"shell,omitempty"` 163 } 164 165 StepBackground struct { 166 Command string `json:"command,omitempty" yaml:"command,omitempty"` 167 ConnRef string `json:"connectorRef,omitempty" yaml:"connectorRef,omitempty"` 168 Entrypoint []string `json:"entrypoint,omitempty" yaml:"entrypoint,omitempty"` 169 Env map[string]string `json:"envVariables,omitempty" yaml:"envVariables,omitempty"` 170 Image string `json:"image,omitempty" yaml:"image,omitempty"` 171 ImagePullPolicy string `json:"imagePullPolicy,omitempty" yaml:"imagePullPolicy,omitempty"` 172 PortBindings map[string]string `json:"portBindings,omitempty" yaml:"portBindings,omitempty"` 173 Privileged bool `json:"privileged,omitempty" yaml:"privileged,omitempty"` 174 Resources *Resources `json:"resources,omitempty" yaml:"resources,omitempty"` 175 RunAsUser string `json:"runAsUser,omitempty" yaml:"runAsUser,omitempty"` 176 } 177 178 StepScript struct { 179 DelegateSelectors string `json:"delegateSelectors,omitempty" yaml:"delegateSelectors,omitempty"` 180 EnvironmentVariables []*Variable `json:"environmentVariables,omitempty" yaml:"environmentVariables,omitempty"` 181 ExecutionTarget *ExecutionTarget `json:"executionTarget,omitempty" yaml:"executionTarget,omitempty"` 182 Metadata string `json:"metadata,omitempty" yaml:"metadata,omitempty"` 183 OnDelegate bool `json:"onDelegate,omitempty" yaml:"onDelegate,omitempty"` 184 OutputVariables []*Variable `json:"outputVariables,omitempty" yaml:"outputVariables,omitempty"` 185 Shell string `json:"shell,omitempty" yaml:"shell,omitempty"` // Bash|Powershell 186 Source *Source `json:"source,omitempty" yaml:"source,omitempty"` 187 188 // NOTE the below fields are not part of the 189 // official schema, however, they are useful for 190 // executing docker pipelines. 191 192 ConnRef string `json:"connectorRef,omitempty" yaml:"connectorRef,omitempty"` 193 Image string `json:"image,omitempty" yaml:"image,omitempty"` 194 ImagePullPolicy string `json:"imagePullPolicy,omitempty" yaml:"imagePullPolicy,omitempty"` 195 RunAsUser string `json:"runAsUser,omitempty" yaml:"runAsUser,omitempty"` 196 } 197 198 StepS3Upload struct { 199 Bucket string `json:"bucket,omitempty" yaml:"bucket,omitempty"` 200 ConnectorRef string `json:"connectorRef,omitempty" yaml:"connectorRef,omitempty"` 201 Endpoint string `json:"endpoint,omitempty" yaml:"endpoint,omitempty"` 202 Region string `json:"region,omitempty" yaml:"region,omitempty"` 203 Resources *Resources `json:"resources,omitempty" yaml:"resources,omitempty"` 204 RunAsUser string `json:"runAsUser,omitempty" yaml:"runAsUser,omitempty"` 205 SourcePath string `json:"sourcePath,omitempty" yaml:"sourcePath,omitempty"` 206 Target string `json:"target,omitempty" yaml:"target,omitempty"` 207 } 208 209 // 210 // Supporting structures 211 // 212 213 Approvers struct { 214 MinimumCount int `json:"minimumCount,omitempty" yaml:"minimumCount,omitempty"` 215 DisallowPipelineExecutor bool `json:"disallowPipelineExecutor,omitempty" yaml:"disallowPipelineExecutor,omitempty"` 216 UserGroups []string `json:"userGroups,omitempty" yaml:"userGroups,omitempty"` 217 } 218 219 ApproverInput struct { 220 Name string `json:"name,omitempty" yaml:"name,omitempty"` 221 DefaultValue string `json:"defaultValue,omitempty" yaml:"defaultValue,omitempty"` 222 } 223 224 ExecutionTarget struct { 225 ConnectorRef string `json:"connectorRef,omitempty" yaml:"connectorRef,omitempty"` 226 Script string `json:"script,omitempty" yaml:"script,omitempty"` 227 WorkingDirectory string `json:"workingDirectory,omitempty" yaml:"workingDirectory,omitempty"` 228 } 229 230 Source struct { 231 Type string `json:"type,omitempty" yaml:"type,omitempty"` 232 Spec SourceSpec `json:"spec,omitempty" yaml:"spec,omitempty"` 233 } 234 235 SourceSpec struct { 236 Script string `json:"script,omitempty" yaml:"script,omitempty"` 237 } 238 239 StepWhen struct { 240 StageStatus string `json:"stageStatus,omitempty" yaml:"stageStatus,omitempty"` 241 Condition string `json:"condition,omitempty" yaml:"condition,omitempty"` 242 } 243 ) 244 245 // UnmarshalJSON implement the json.Unmarshaler interface. 246 func (s *Step) UnmarshalJSON(data []byte) error { 247 type S Step 248 type T struct { 249 *S 250 Spec json.RawMessage `json:"spec"` 251 } 252 253 obj := &T{S: (*S)(s)} 254 if err := json.Unmarshal(data, obj); err != nil { 255 return err 256 } 257 258 switch s.Type { 259 case StepTypeRun: 260 s.Spec = new(StepRun) 261 case StepTypePlugin: 262 s.Spec = new(StepPlugin) 263 case StepTypeBuildAndPushDockerRegistry: 264 s.Spec = new(StepDocker) 265 case StepTypeS3Upload: 266 s.Spec = new(StepS3Upload) 267 case StepTypeShellScript: 268 s.Spec = new(StepScript) 269 case StepTypeHTTP: 270 s.Spec = new(StepHTTP) 271 default: 272 return fmt.Errorf("unknown step type %s", s.Type) 273 } 274 275 return json.Unmarshal(obj.Spec, s.Spec) 276 }