go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/util/common.go (about) 1 // Copyright 2023 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 util contains utility functions 16 package util 17 18 import ( 19 "regexp" 20 21 "go.chromium.org/luci/common/errors" 22 ) 23 24 // ProjectRePattern is the regular expression pattern that matches 25 // validly formed LUCI Project names. 26 // From https://source.chromium.org/chromium/infra/infra/+/main:luci/appengine/components/components/config/common.py?q=PROJECT_ID_PATTERN 27 const ProjectRePattern = `[a-z0-9\-]{1,40}` 28 29 // VariantHashRePattern is the regular expression pattern that matches 30 // validly formed variant hash. 31 // From https://source.chromium.org/chromium/infra/infra/+/main:go/src/go.chromium.org/luci/analysis/internal/config/constants.go;l=23 32 const VariantHashRePattern = `[0-9a-f]{16}` 33 34 // RefHashRePattern is the regular expression pattern that matches 35 // validly formed ref hash. 36 // From https://source.chromium.org/chromium/infra/infra/+/main:go/src/go.chromium.org/luci/analysis/internal/config/constants.go;l=27 37 const RefHashRePattern = `[0-9a-f]{16}` 38 39 // projectRe matches validly formed LUCI Project names. 40 var projectRe = regexp.MustCompile(`^` + ProjectRePattern + `$`) 41 var variantHashRe = regexp.MustCompile(`^` + VariantHashRePattern + `$`) 42 var refHashRe = regexp.MustCompile(`^` + RefHashRePattern + `$`) 43 44 func ValidateProject(project string) error { 45 if project == "" { 46 return errors.Reason("unspecified").Err() 47 } 48 if !projectRe.MatchString(project) { 49 return errors.Reason("project %s must match %s", project, projectRe).Err() 50 } 51 return nil 52 } 53 54 func ValidateVariantHash(variantHash string) error { 55 if !variantHashRe.MatchString(variantHash) { 56 return errors.Reason("variant hash %s must match %s", variantHash, variantHashRe).Err() 57 } 58 return nil 59 } 60 61 func ValidateRefHash(refHash string) error { 62 if !refHashRe.MatchString(refHash) { 63 return errors.Reason("ref hash %s must match %s", refHash, refHashRe).Err() 64 } 65 return nil 66 }