github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/bitbucket/util.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 bitbucket 16 17 import ( 18 "fmt" 19 "time" 20 21 bitbucket "github.com/drone/go-convert/convert/bitbucket/yaml" 22 harness "github.com/drone/spec/dist/go" 23 24 "github.com/gotidy/ptr" 25 ) 26 27 // 28 // this file contains helper functions that convert 29 // bitbucket data structures to harness data structures 30 // structures. these functions are all stateless 31 // (they do not rely on snapshots of walk state). 32 // 33 34 // helper function converts a bitbucket size enum to a 35 // harness resource class. 36 func convertSize(size bitbucket.Size) string { 37 switch size { 38 case bitbucket.Size2x: // 8GB 39 return "large" 40 case bitbucket.Size4x: // 16GB 41 return "xlarge" 42 case bitbucket.Size8x: // 32GB 43 return "xxlarge" 44 case bitbucket.Size1x: // 4GB 45 return "standard" 46 default: 47 return "" 48 } 49 } 50 51 // helper function converts the bitbucket stage clone 52 // configuration to a harness stage clone configuration. 53 func convertClone(stage *bitbucket.Stage) *harness.CloneStage { 54 var clones []*bitbucket.Clone 55 56 // loop through the steps and if a step 57 // defines clone behavior 58 for _, step := range extractSteps(stage) { 59 if step.Clone != nil { 60 clones = append(clones, step.Clone) 61 } 62 } 63 64 // if there are not clone configurations at 65 // the step-level we can return a nil clone. 66 if len(clones) == 0 { 67 return nil 68 } 69 70 clone := new(harness.CloneStage) 71 for _, v := range clones { 72 if v.Depth != nil { 73 if v.Depth.Value > int(clone.Depth) { 74 clone.Depth = int64(v.Depth.Value) 75 } 76 } 77 if v.SkipVerify { 78 clone.Insecure = true 79 } 80 if v.Enabled != nil && !ptr.ToBool(v.Enabled) { 81 // TODO 82 } 83 } 84 85 return clone 86 } 87 88 // helper function converts the bitbucket global clone 89 // configuration to a global harness clone configuration. 90 func convertCloneGlobal(clone *bitbucket.Clone) *harness.Clone { 91 if clone == nil { 92 return nil 93 } 94 95 to := new(harness.Clone) 96 to.Insecure = clone.SkipVerify 97 98 if clone.Depth != nil { 99 to.Depth = int64(clone.Depth.Value) 100 } 101 102 // disable cloning globally if the user has 103 // explicityly disabled this functionality 104 if clone.Enabled != nil && ptr.ToBool(clone.Enabled) == false { 105 to.Disabled = true 106 } 107 108 return to 109 } 110 111 // helper function converts the bitbucket global cache to 112 // a harness stage cache, filtered by the list of cache names. 113 func convertCache(defs *bitbucket.Definitions, caches []string) *harness.Cache { 114 if defs == nil || len(defs.Caches) == 0 || len(caches) == 0 { 115 return nil 116 } 117 118 cache := new(harness.Cache) 119 cache.Enabled = true 120 121 var files []string 122 var paths []string 123 124 for _, name := range caches { 125 src, ok := defs.Caches[name] 126 if !ok { 127 continue 128 } 129 paths = append(paths, src.Path) 130 if src.Key != nil { 131 files = append(files, src.Key.Files...) 132 } 133 } 134 135 for _, name := range caches { 136 switch name { 137 case "composer": 138 paths = append(paths, "composer") 139 paths = append(paths, "~/.composer/cache") 140 case "dotnetcore": 141 paths = append(paths, "dotnetcore") 142 paths = append(paths, "~/.nuget/packages") 143 case "gradle": 144 paths = append(paths, "gradle") 145 paths = append(paths, "~/.gradle/caches") 146 case "ivy2": 147 paths = append(paths, "ivy2") 148 paths = append(paths, "~/.ivy2/cache") 149 case "maven": 150 paths = append(paths, "maven") 151 paths = append(paths, "~/.m2/repository") 152 case "node": 153 paths = append(paths, "node") 154 paths = append(paths, "node_modules") 155 case "pip": 156 paths = append(paths, "pip") 157 paths = append(paths, "~/.cache/pip") 158 case "sbt": 159 paths = append(paths, "sbt") 160 paths = append(paths, "ivy2") 161 paths = append(paths, "~/.ivy2/cache") 162 } 163 } 164 165 cache.Paths = paths 166 return cache 167 } 168 169 // helper function converts the bitbucket global defaults 170 // to the harness global default configuration. 171 func convertDefault(config *bitbucket.Config) *harness.Default { 172 173 // if the global pipeline configuration sections 174 // are empty or nil, return nil 175 if config.Clone == nil && 176 config.Image == nil && 177 config.Options == nil { 178 return nil 179 } 180 181 if config.Image == nil { 182 // Username 183 // Password 184 } 185 if config.Options == nil { 186 // Docker (bool) 187 // MaxTime (int) 188 // Size (1x, 2x, 4x, 8x) 189 // Credentials ??? 190 } 191 192 var def *harness.Default 193 194 // if the user has configured global clone defaults, 195 // convert this to pipeline-level clone settings. 196 if config.Clone != nil { 197 // create the default if not already created. 198 if def == nil { 199 def = new(harness.Default) 200 } 201 def.Clone = convertCloneGlobal(config.Clone) 202 203 // if the clone is disabled we need to make 204 // sure it isn't explicitly enabled for any steps. 205 if def.Clone.Disabled { 206 for _, step := range extractAllSteps(config.Pipelines.Default) { 207 if step.Clone != nil && ptr.ToBool(step.Clone.Enabled) { 208 def.Clone.Disabled = false 209 break 210 } 211 } 212 } 213 } 214 215 return def 216 } 217 218 // helper function converts an integer of minutes to a time 219 // duration string. 220 func minuteToDurationString(v int64) string { 221 dur := time.Duration(v) * time.Minute 222 return fmt.Sprint(dur) 223 }