go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/internal/buildsource/builder.go (about)

     1  // Copyright 2017 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 buildsource
    16  
    17  import (
    18  	"strings"
    19  
    20  	"go.chromium.org/luci/common/errors"
    21  	"go.chromium.org/luci/grpc/grpcutil"
    22  
    23  	"go.chromium.org/luci/milo/internal/model"
    24  )
    25  
    26  // BuilderID is the universal ID of a builder, and has the form:
    27  //
    28  //	buildbucket/bucket/builder
    29  type BuilderID string
    30  
    31  // Split breaks the BuilderID into pieces.
    32  //   - backend is always 'buildbucket'
    33  //   - backendGroup is either the bucket or builder group
    34  //   - builderName is the builder name.
    35  //
    36  // Returns an error if the BuilderID is malformed (wrong # slashes) or if any of
    37  // the pieces are empty.
    38  func (b BuilderID) Split() (backend, backendGroup, builderName string, err error) {
    39  	toks := strings.SplitN(string(b), "/", 3)
    40  	if len(toks) != 3 {
    41  		err = errors.Reason("bad BuilderID: not enough tokens: %q", b).
    42  			Tag(grpcutil.InvalidArgumentTag).Err()
    43  		return
    44  	}
    45  	backend, backendGroup, builderName = toks[0], toks[1], toks[2]
    46  	switch {
    47  	case backend != "buildbucket":
    48  		err = errors.Reason("bad BuilderID: unknown backend %q", backend).
    49  			Tag(grpcutil.InvalidArgumentTag).Err()
    50  	case backendGroup == "":
    51  		err = errors.New("bad BuilderID: empty backendGroup", grpcutil.InvalidArgumentTag)
    52  	case builderName == "":
    53  		err = errors.New("bad BuilderID: empty builderName", grpcutil.InvalidArgumentTag)
    54  	}
    55  	return
    56  }
    57  
    58  // SelfLink returns LUCI URL of the builder.
    59  func (b BuilderID) SelfLink(project string) string {
    60  	return model.BuilderIDLink(string(b), project)
    61  }