go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/led/job/info_buildbucket.go (about)

     1  // Copyright 2020 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 job
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/golang/protobuf/jsonpb"
    22  	"google.golang.org/protobuf/proto"
    23  
    24  	bbpb "go.chromium.org/luci/buildbucket/proto"
    25  	"go.chromium.org/luci/common/data/strpair"
    26  	"go.chromium.org/luci/common/errors"
    27  	swarmingpb "go.chromium.org/luci/swarming/proto/api_v2"
    28  )
    29  
    30  type bbInfo struct {
    31  	*Buildbucket
    32  }
    33  
    34  var _ Info = bbInfo{}
    35  
    36  func (b bbInfo) SwarmingHostname() string {
    37  	if b.GetBbagentArgs().GetBuild().GetInfra().GetSwarming() != nil {
    38  		return b.GetBbagentArgs().GetBuild().GetInfra().GetSwarming().GetHostname()
    39  	}
    40  	backendTarget := b.GetBbagentArgs().GetBuild().GetInfra().GetBackend().GetTask().GetId().GetTarget()
    41  	if backendTarget == "" {
    42  		return ""
    43  	}
    44  	if strings.HasPrefix(backendTarget, "swarming://") {
    45  		return fmt.Sprintf("%s.appspot.com", strings.TrimPrefix(backendTarget, "swarming://"))
    46  	}
    47  	return ""
    48  }
    49  
    50  func (b bbInfo) TaskName() string {
    51  	return b.GetName()
    52  }
    53  
    54  func (b bbInfo) CurrentIsolated() (*swarmingpb.CASReference, error) {
    55  	cas, _ := b.Payload()
    56  	if cas != nil {
    57  		return &swarmingpb.CASReference{
    58  			CasInstance: cas.GetCasInstance(),
    59  			Digest: &swarmingpb.Digest{
    60  				Hash:      cas.GetDigest().GetHash(),
    61  				SizeBytes: cas.GetDigest().GetSizeBytes(),
    62  			},
    63  		}, nil
    64  	}
    65  	return nil, nil
    66  }
    67  
    68  func (b bbInfo) Dimensions() (ExpiringDimensions, error) {
    69  	ldims := logicalDimensions{}
    70  	var dimensions []*bbpb.RequestedDimension
    71  	if b.BbagentArgs.Build.Infra.Swarming != nil {
    72  		dimensions = b.BbagentArgs.Build.Infra.Swarming.TaskDimensions
    73  	} else {
    74  		dimensions = b.BbagentArgs.Build.Infra.Backend.GetTaskDimensions()
    75  	}
    76  	for _, reqDim := range dimensions {
    77  		exp := reqDim.Expiration
    78  		if exp == nil {
    79  			exp = b.BbagentArgs.Build.SchedulingTimeout
    80  		}
    81  		ldims.update(reqDim.Key, reqDim.Value, exp)
    82  	}
    83  	return ldims.toExpiringDimensions(), nil
    84  }
    85  
    86  func (b bbInfo) CIPDPkgs() (ret CIPDPkgs, err error) {
    87  	if !b.BbagentDownloadCIPDPkgs() {
    88  		ret = CIPDPkgs{}
    89  		ret.fromList(b.CipdPackages)
    90  		return
    91  	}
    92  	return nil, errors.Reason("not supported for Buildbucket v2 builds").Err()
    93  }
    94  
    95  func (b bbInfo) Env() (ret map[string]string, err error) {
    96  	ret = make(map[string]string, len(b.EnvVars))
    97  	for _, pair := range b.EnvVars {
    98  		ret[pair.Key] = pair.Value
    99  	}
   100  	return
   101  }
   102  
   103  func (b bbInfo) Priority() int32 {
   104  	return b.GetBbagentArgs().GetBuild().GetInfra().GetSwarming().GetPriority()
   105  }
   106  
   107  func (b bbInfo) PrefixPathEnv() (ret []string, err error) {
   108  	for _, keyVals := range b.EnvPrefixes {
   109  		if keyVals.Key == "PATH" {
   110  			ret = make([]string, len(keyVals.Value))
   111  			copy(ret, keyVals.Value)
   112  			break
   113  		}
   114  	}
   115  	return
   116  }
   117  
   118  func (b bbInfo) Tags() (ret []string) {
   119  	lenTags := len(b.GetBbagentArgs().GetBuild().GetTags())
   120  	if lenTags > 0 {
   121  		ret = make([]string, len(b.ExtraTags))
   122  		for _, t := range b.BbagentArgs.Build.Tags {
   123  			ret = append(ret, strpair.Format(t.Key, t.Value))
   124  		}
   125  	}
   126  	return
   127  }
   128  
   129  func (b bbInfo) Experimental() bool {
   130  	return b.GetBbagentArgs().GetBuild().GetInput().GetExperimental()
   131  }
   132  
   133  func (b bbInfo) Experiments() (ret []string) {
   134  	if exps := b.GetBbagentArgs().GetBuild().GetInput().GetExperiments(); len(exps) > 0 {
   135  		ret = make([]string, len(exps))
   136  		copy(ret, exps)
   137  	}
   138  	return
   139  }
   140  
   141  func (b bbInfo) Properties() (ret map[string]string, err error) {
   142  	if p := b.GetBbagentArgs().GetBuild().GetInput().GetProperties(); p != nil {
   143  		m := (&jsonpb.Marshaler{})
   144  		ret = make(map[string]string, len(p.Fields))
   145  		for key, field := range p.Fields {
   146  			if ret[key], err = m.MarshalToString(field); err != nil {
   147  				ret = nil
   148  				return
   149  			}
   150  		}
   151  	}
   152  	return
   153  }
   154  
   155  func (b bbInfo) GerritChanges() (ret []*bbpb.GerritChange) {
   156  	if changes := b.GetBbagentArgs().GetBuild().GetInput().GetGerritChanges(); len(changes) > 0 {
   157  		ret = make([]*bbpb.GerritChange, len(changes))
   158  		for i, change := range changes {
   159  			ret[i] = proto.Clone(change).(*bbpb.GerritChange)
   160  		}
   161  	}
   162  	return
   163  }
   164  
   165  func (b bbInfo) GitilesCommit() (ret *bbpb.GitilesCommit) {
   166  	if gc := b.GetBbagentArgs().GetBuild().GetInput().GetGitilesCommit(); gc != nil {
   167  		ret = proto.Clone(gc).(*bbpb.GitilesCommit)
   168  	}
   169  	return
   170  }
   171  
   172  func (b bbInfo) TaskPayloadSource() (cipdPkg, cipdVers string) {
   173  	_, cipd := b.Payload()
   174  	if len(cipd.GetSpecs()) > 0 {
   175  		cipdPkg = cipd.Specs[0].GetPackage()
   176  		cipdVers = cipd.Specs[0].GetVersion()
   177  		return
   178  	}
   179  	// Fall back to getting payload from exe.
   180  	exe := b.GetBbagentArgs().GetBuild().GetExe()
   181  	cipdPkg = exe.GetCipdPackage()
   182  	cipdVers = exe.GetCipdVersion()
   183  	return
   184  }
   185  
   186  func (b bbInfo) TaskPayloadPath() (path string) {
   187  	return b.PayloadPath()
   188  }
   189  
   190  func (b bbInfo) TaskPayloadCmd() (args []string) {
   191  	args = append(args, b.GetBbagentArgs().GetBuild().GetExe().GetCmd()...)
   192  	return
   193  }