go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/led/job/edit_cipd_pkgs.go (about) 1 // Copyright 2020 The LUCI Authors. 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 job 16 17 import ( 18 "fmt" 19 "strings" 20 21 swarmingpb "go.chromium.org/luci/swarming/proto/api_v2" 22 ) 23 24 // CIPDPkgs is a mapping of the CIPD packages within a Definition in the form 25 // of: 26 // 27 // "subdir:name/of/package" -> "version" 28 type CIPDPkgs map[string]string 29 30 func (c CIPDPkgs) fromList(pkgs []*swarmingpb.CipdPackage) { 31 for _, pkg := range pkgs { 32 if path := pkg.Path; path == "" || path == "." { 33 c[pkg.PackageName] = pkg.Version 34 } else { 35 c[fmt.Sprintf("%s:%s", path, pkg.PackageName)] = pkg.Version 36 } 37 } 38 } 39 40 func (c CIPDPkgs) equal(other CIPDPkgs) bool { 41 if len(c) != len(other) { 42 return false 43 } 44 for key := range c { 45 if c[key] != other[key] { 46 return false 47 } 48 } 49 return true 50 } 51 52 func (c CIPDPkgs) updateCipdPkgs(pinSets *[]*swarmingpb.CipdPackage) { 53 if len(c) == 0 { 54 return 55 } 56 57 // subdir -> pkg -> version 58 currentMapping := map[string]map[string]string{} 59 for _, pin := range *pinSets { 60 subdirM, ok := currentMapping[pin.Path] 61 if !ok { 62 subdirM = map[string]string{} 63 currentMapping[pin.Path] = subdirM 64 } 65 subdirM[pin.PackageName] = pin.Version 66 } 67 68 for subdirPkg, vers := range c { 69 subdir := "." 70 pkg := subdirPkg 71 if toks := strings.SplitN(subdirPkg, ":", 2); len(toks) > 1 { 72 subdir, pkg = toks[0], toks[1] 73 if subdir == "" { 74 subdir = "." 75 } 76 } 77 if vers == "" { 78 delete(currentMapping[subdir], pkg) 79 } else { 80 subdirM, ok := currentMapping[subdir] 81 if !ok { 82 subdirM = map[string]string{} 83 currentMapping[subdir] = subdirM 84 } 85 subdirM[pkg] = vers 86 } 87 } 88 89 newPkgs := make([]*swarmingpb.CipdPackage, 0, len(*pinSets)+len(c)) 90 for _, subdir := range keysOf(currentMapping) { 91 for _, pkg := range keysOf(currentMapping[subdir]) { 92 newPkgs = append(newPkgs, &swarmingpb.CipdPackage{ 93 Path: subdir, 94 PackageName: pkg, 95 Version: currentMapping[subdir][pkg], 96 }) 97 } 98 } 99 *pinSets = newPkgs 100 }