go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/pbutil/baseline.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 pbutil
    16  
    17  import (
    18  	"strings"
    19  )
    20  
    21  const baselineIDPattern = `[a-z0-9\-_.]{1,100}:[a-zA-Z0-9\-_.\(\) ]{1,128}`
    22  
    23  var baselineIDRe = regexpf("^%s$", baselineIDPattern)
    24  var baselineNameRe = regexpf("^projects/%s/baselines/%s", projectPattern, baselineIDPattern)
    25  
    26  // ValidateBaselineID returns a non-nil error if the id is invalid.
    27  func ValidateBaselineID(baseline string) error {
    28  	return validateWithRe(baselineIDRe, baseline)
    29  }
    30  
    31  // ParseBaselineName extracts the project and baselineID.
    32  func ParseBaselineName(name string) (project, baselineID string, err error) {
    33  	if name == "" {
    34  		return "", "", unspecified()
    35  	}
    36  
    37  	if m := baselineNameRe.FindStringSubmatch(name); m == nil {
    38  		return "", "", doesNotMatch(baselineNameRe)
    39  	}
    40  
    41  	sp := strings.Split(name, "/")
    42  	return sp[1], sp[3], nil
    43  }