go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/run/util.go (about)

     1  // Copyright 2021 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 run
    16  
    17  import (
    18  	"crypto/sha256"
    19  	"encoding/hex"
    20  	"sort"
    21  	"strconv"
    22  )
    23  
    24  // HasRootCL returns true if this Run has root CL specified.
    25  //
    26  // It means this run is instantly triggered Run in combined CLs mode (i.e.
    27  // multi-Cl mode).
    28  func (r *Run) HasRootCL() bool {
    29  	return r.RootCL != 0
    30  }
    31  
    32  // ComputeCLGroupKey constructs keys for ClGroupKey and the related
    33  // EquivalentClGroupKey.
    34  //
    35  // These are meant to be opaque keys unique to particular set of CLs and
    36  // patchsets for the purpose of grouping together Runs for the same sets of
    37  // patchsets. if isEquivalent is true, then the "min equivalent patchset" is
    38  // used instead of the latest patchset, so that trivial patchsets such as minor
    39  // rebases and CL description updates don't change the key.
    40  func ComputeCLGroupKey(cls []*RunCL, isEquivalent bool) string {
    41  	sort.Slice(cls, func(i, j int) bool {
    42  		// ExternalID includes host and change number but not patchset; but
    43  		// different patchsets of the same CL will never be included in the
    44  		// same list, so sorting on only ExternalID is sufficient.
    45  		return cls[i].ExternalID < cls[j].ExternalID
    46  	})
    47  	h := sha256.New()
    48  	// CL group keys are meant to be opaque keys. We'd like to avoid people
    49  	// depending on CL group key and equivalent CL group key sometimes being
    50  	// equal. We can do this by adding a salt to the hash.
    51  	if isEquivalent {
    52  		h.Write([]byte("equivalent_cl_group_key"))
    53  	}
    54  	separator := []byte{0}
    55  	for i, cl := range cls {
    56  		if i > 0 {
    57  			h.Write(separator)
    58  		}
    59  		h.Write([]byte(cl.Detail.GetGerrit().GetHost()))
    60  		h.Write(separator)
    61  		h.Write([]byte(strconv.FormatInt(cl.Detail.GetGerrit().GetInfo().GetNumber(), 10)))
    62  		h.Write(separator)
    63  		if isEquivalent {
    64  			h.Write([]byte(strconv.FormatInt(int64(cl.Detail.GetMinEquivalentPatchset()), 10)))
    65  		} else {
    66  			h.Write([]byte(strconv.FormatInt(int64(cl.Detail.GetPatchset()), 10)))
    67  		}
    68  	}
    69  	return hex.EncodeToString(h.Sum(nil)[:8])
    70  }
    71  
    72  // ShouldSubmit returns true if the run should submit the CL(s) at the end.
    73  func ShouldSubmit(r *Run) bool {
    74  	return r.Mode == FullRun || r.ModeDefinition.GetCqLabelValue() == 2
    75  }