go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/run/options.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  	"fmt"
    19  	"strings"
    20  
    21  	"go.chromium.org/luci/common/git/footer"
    22  	"go.chromium.org/luci/cv/internal/changelist"
    23  	"go.chromium.org/luci/cv/internal/common"
    24  )
    25  
    26  // ExtractOptions computes the Run Options from 1 CL.
    27  func ExtractOptions(snapshot *changelist.Snapshot) *Options {
    28  	byKey := make(map[string][]string, len(snapshot.GetMetadata()))
    29  	for _, pair := range snapshot.GetMetadata() {
    30  		k, v := pair.GetKey(), pair.GetValue()
    31  		byKey[k] = append(byKey[k], v)
    32  	}
    33  
    34  	valuesOf := func(mkey, lkey string) []string {
    35  		var out []string
    36  
    37  		switch k := footer.NormalizeKey(mkey); {
    38  		case mkey == "":
    39  		case k != mkey:
    40  			panic(fmt.Errorf("use normalized key %q not %q in CV code", k, mkey))
    41  		default:
    42  			out = append(out, byKey[mkey]...)
    43  		}
    44  
    45  		if lkey != "" {
    46  			out = append(out, byKey[lkey]...)
    47  		}
    48  		return out
    49  	}
    50  
    51  	has := func(mkey, lkey, value string) bool {
    52  		if l := strings.ToLower(value); l != value {
    53  			panic(fmt.Errorf("use lowercase value %q not %q in CV code", l, value))
    54  		}
    55  		for _, v := range valuesOf(mkey, lkey) {
    56  			if strings.ToLower(v) == value {
    57  				return true
    58  			}
    59  		}
    60  		return false
    61  	}
    62  
    63  	isTrue := func(mkey, lkey string) bool { return has(mkey, lkey, "true") }
    64  
    65  	o := &Options{}
    66  	if isTrue(common.FooterNoEquivalentBuilders, common.FooterLegacyNoEquivalentBuilders) {
    67  		o.SkipEquivalentBuilders = true
    68  	}
    69  	if isTrue(common.FooterCQDoNotCancelTryjobs, "") {
    70  		o.AvoidCancellingTryjobs = true
    71  	}
    72  	if isTrue(common.FooterNoTreeChecks, common.FooterLegacyNoTreeChecks) {
    73  		o.SkipTreeChecks = true
    74  	}
    75  	if isTrue(common.FooterNoTry, common.FooterLegacyNoTry) {
    76  		o.SkipTryjobs = true
    77  	}
    78  	if isTrue(common.FooterNoPresubmit, common.FooterLegacyPresubmit) {
    79  		o.SkipPresubmit = true
    80  	}
    81  	o.IncludedTryjobs = append(o.IncludedTryjobs, valuesOf(
    82  		common.FooterCQIncludeTryjobs,
    83  		common.FooterLegacyCQIncludeTryjobs)...)
    84  	o.OverriddenTryjobs = append(o.OverriddenTryjobs, valuesOf(common.FooterOverrideTryjobsForAutomation, "")...)
    85  	o.CustomTryjobTags = append(o.CustomTryjobTags, valuesOf(common.FooterCQClTag, "")...)
    86  	return o
    87  }
    88  
    89  // MergeOptions merges two Run Options.
    90  //
    91  // Does not modify the passed object, but may return either of them.
    92  func MergeOptions(a, b *Options) *Options {
    93  	switch {
    94  	case a == nil:
    95  		return b
    96  	case b == nil:
    97  		return a
    98  	}
    99  	return &Options{
   100  		AvoidCancellingTryjobs: a.AvoidCancellingTryjobs || b.AvoidCancellingTryjobs,
   101  		SkipTryjobs:            a.SkipTryjobs || b.SkipTryjobs,
   102  		SkipPresubmit:          a.SkipPresubmit || b.SkipPresubmit,
   103  		SkipEquivalentBuilders: a.SkipEquivalentBuilders || b.SkipEquivalentBuilders,
   104  		SkipTreeChecks:         a.SkipTreeChecks || b.SkipTreeChecks,
   105  		IncludedTryjobs:        append(a.IncludedTryjobs, b.IncludedTryjobs...),
   106  		OverriddenTryjobs:      append(a.OverriddenTryjobs, b.OverriddenTryjobs...),
   107  		CustomTryjobTags:       append(a.CustomTryjobTags, b.CustomTryjobTags...),
   108  	}
   109  }