go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/util/buildbucket_util.go (about)

     1  // Copyright 2022 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  	configpb "go.chromium.org/luci/bisection/proto/config"
    20  	pb "go.chromium.org/luci/bisection/proto/v1"
    21  	bbpb "go.chromium.org/luci/buildbucket/proto"
    22  	"google.golang.org/protobuf/proto"
    23  )
    24  
    25  // Check if a step is compile step.
    26  // For the moment, we only check for the step name.
    27  // In the future, we may want to check for step tag (crbug.com/1353978)
    28  func IsCompileStep(step *bbpb.Step) bool {
    29  	return step.GetName() == "compile"
    30  }
    31  
    32  // GetGitilesCommitForBuild returns the gitiles commit for a build
    33  // The commit position is taken from output properties if available.
    34  func GetGitilesCommitForBuild(build *bbpb.Build) *bbpb.GitilesCommit {
    35  	commit := &bbpb.GitilesCommit{}
    36  	input := build.GetInput()
    37  	if input != nil && input.GitilesCommit != nil {
    38  		proto.Merge(commit, input.GitilesCommit)
    39  	}
    40  	// Commit position only exists in output, not in input
    41  	output := build.GetOutput()
    42  	if output != nil && output.GitilesCommit != nil {
    43  		// Just do a sanity check here
    44  		outputCommit := output.GitilesCommit
    45  		if outputCommit.Host == commit.Host && outputCommit.Project == commit.Project && outputCommit.Id == commit.Id && outputCommit.Ref == commit.Ref {
    46  			commit.Position = outputCommit.Position
    47  		}
    48  	}
    49  	return commit
    50  }
    51  
    52  func ToDimensionsPB(dimensions []*bbpb.RequestedDimension) *pb.Dimensions {
    53  	dims := []*pb.Dimension{}
    54  	for _, d := range dimensions {
    55  		dims = append(dims, &pb.Dimension{
    56  			Key:   d.Key,
    57  			Value: d.Value,
    58  		})
    59  	}
    60  	return &pb.Dimensions{Dimensions: dims}
    61  }
    62  
    63  // GetSheriffRotationsForBuild returns the sheriff rotations for a build.
    64  // If there is no sheriff rotation set, return empty list.
    65  func GetSheriffRotationsForBuild(build *bbpb.Build) []string {
    66  	fields := build.GetInput().GetProperties().GetFields()
    67  	value, found := fields["sheriff_rotations"]
    68  	if !found {
    69  		return []string{}
    70  	}
    71  	listValue := value.GetListValue()
    72  	if listValue == nil {
    73  		return []string{}
    74  	}
    75  	var rotations []string
    76  	for _, value := range listValue.Values {
    77  		rotation := value.GetStringValue()
    78  		if rotation != "" {
    79  			rotations = append(rotations, rotation)
    80  		}
    81  	}
    82  	return rotations
    83  }
    84  
    85  func BuilderFromConfigBuilder(builder *configpb.Builder) *bbpb.BuilderID {
    86  	return &bbpb.BuilderID{
    87  		Project: builder.Project,
    88  		Bucket:  builder.Bucket,
    89  		Builder: builder.Builder,
    90  	}
    91  }
    92  
    93  // GetBuilderGroup returns the builder group for a build.
    94  // If there is no builder group set, return empty string.
    95  func GetBuilderGroup(build *bbpb.Build) string {
    96  	fields := build.GetInput().GetProperties().GetFields()
    97  	value, found := fields["builder_group"]
    98  	if !found {
    99  		return ""
   100  	}
   101  	return value.GetStringValue()
   102  }
   103  
   104  func GetTaskDimensions(build *bbpb.Build) []*bbpb.RequestedDimension {
   105  	if build.GetInfra().GetSwarming().GetTaskDimensions() != nil {
   106  		return build.GetInfra().GetSwarming().GetTaskDimensions()
   107  	}
   108  	return build.GetInfra().GetBackend().GetTaskDimensions()
   109  }