go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/pbutil/test_exoneration.go (about)

     1  // Copyright 2019 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  	"fmt"
    19  	"net/url"
    20  )
    21  
    22  var testExonerationNameRe = regexpf(`^invocations/(%s)/tests/([^/]+)/exonerations/(.+)$`, invocationIDPattern)
    23  
    24  // TestExonerationName synthesizes a test exoneration name.
    25  // Assumes invocation and exoneration IDs are valid.
    26  func TestExonerationName(invocationID, testID, exonerationID string) string {
    27  	return fmt.Sprintf("invocations/%s/tests/%s/exonerations/%s", invocationID, url.PathEscape(testID), exonerationID)
    28  }
    29  
    30  // ParseTestExonerationName extracts invocation, test id and exoneration IDs
    31  // from the name.
    32  func ParseTestExonerationName(name string) (invocationID, testID, exonerationID string, err error) {
    33  	if name == "" {
    34  		return "", "", "", unspecified()
    35  	}
    36  	m := testExonerationNameRe.FindStringSubmatch(name)
    37  	if m == nil {
    38  		return "", "", "", doesNotMatch(testExonerationNameRe)
    39  	}
    40  	invocationID = m[1]
    41  	if testID, err = url.PathUnescape(m[2]); err != nil {
    42  		return "", "", "", err
    43  	}
    44  	exonerationID = m[3]
    45  	return
    46  }
    47  
    48  // ValidateTestExonerationName returns a non-nil error if the test exoneration
    49  // name is invalid.
    50  func ValidateTestExonerationName(name string) error {
    51  	_, _, _, err := ParseTestExonerationName(name)
    52  	return err
    53  }