go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/led/job/edit.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  	bbpb "go.chromium.org/luci/buildbucket/proto"
    19  	"go.chromium.org/luci/common/errors"
    20  	swarmingpb "go.chromium.org/luci/swarming/proto/api_v2"
    21  )
    22  
    23  // Editor represents low-level mutations you can make on a job.Definition.
    24  //
    25  // You may edit a job.Definition by calling its Edit method.
    26  type Editor interface {
    27  	// ClearCurrentIsolated removes all isolateds from this Definition.
    28  	ClearCurrentIsolated()
    29  
    30  	// ClearDimensions removes all dimensions.
    31  	ClearDimensions()
    32  
    33  	// SetDimensions sets the full set of dimensions.
    34  	SetDimensions(dims ExpiringDimensions)
    35  
    36  	// EditDimensions edits the swarming dimensions.
    37  	EditDimensions(dimEdits DimensionEditCommands)
    38  
    39  	// CIPDPkgs allows you to edit the cipd packages. The mapping is in the form
    40  	// of:
    41  	//
    42  	//    "subdir:name/of/package" -> "version"
    43  	//
    44  	// If version is empty, this package will be removed (if it's present).
    45  	CIPDPkgs(cipdPkgs CIPDPkgs)
    46  
    47  	// Env edits the swarming environment variables (i.e. those set before the
    48  	// user payload runs).
    49  	//
    50  	// The map given is a map of environment variable to its value; If the value
    51  	// is "", then the environment variable is removed.
    52  	Env(env map[string]string)
    53  
    54  	// PrefixPathEnv controls swarming's env_prefix mapping for $PATH.
    55  	//
    56  	// Values prepended with '!' will remove them from the existing list of values
    57  	// (if present). Otherwise these values will be appended to the current list
    58  	// of PATH-prefix-envs.
    59  	PrefixPathEnv(values []string)
    60  
    61  	// Priority edits the swarming task priority.
    62  	Priority(priority int32)
    63  
    64  	// SwarmingHostname allows you to modify the current SwarmingHostname used by
    65  	// this led pipeline.
    66  	SwarmingHostname(host string)
    67  
    68  	// TaskName allows you to set the swarming task name of the job.
    69  	TaskName(name string)
    70  
    71  	// Tags appends the given values to the task's tags.
    72  	Tags(values []string)
    73  }
    74  
    75  // HighLevelEditor represents high-level mutations you can make on
    76  // a job.Definition.
    77  //
    78  // Currently only Buildbucket job.Definitions support high level edits.
    79  //
    80  // You may edit a job.Definition by calling its HighLevelEdit method.
    81  type HighLevelEditor interface {
    82  	Editor
    83  
    84  	// Experimental sets the task to either be marked 'experimental' or not.
    85  	Experimental(isExperimental bool)
    86  
    87  	// Experiments enables or disables experiments.
    88  	//
    89  	// If `exps[<name>]` is true, the corresponding experiment will be enabled.
    90  	// If it is false, the experiment will be disabled (if it was enabled).
    91  	//
    92  	// Experiments enabled in the build, but not mentioned in `exps` map are left
    93  	// enabled.
    94  	Experiments(exps map[string]bool)
    95  
    96  	// Properties edits the recipe properties.
    97  	//
    98  	// `props` should be a mapping of the top-level property to JSON-encoded data
    99  	// for the value of this property. If the value is "", then the top-level
   100  	// property will be deleted.
   101  	//
   102  	// If `auto` is true, then values which do not decode as JSON will be treated
   103  	// as literal strings. For example, given a value `literal string` with
   104  	// auto=true, this would be equivalent to passing the value `"literal string"`
   105  	// with auto=false.
   106  	Properties(props map[string]string, auto bool)
   107  
   108  	// TaskPayloadSource sets the task payload to either use the provided CIPD
   109  	// package and version, or the UserPayload if they are empty.
   110  	//
   111  	// If cipdPkg is specified without cipdVers, cipdVers will be set to "latest".
   112  	TaskPayloadSource(cipdPkg, cipdVers string)
   113  
   114  	// TaskPayloadPath sets the location of where bbagent should locate the task
   115  	// payload data within the task.
   116  	TaskPayloadPath(path string)
   117  
   118  	// CASTaskPayload sets the cas reference as the build's user payload.
   119  	CASTaskPayload(path string, casRef *swarmingpb.CASReference)
   120  
   121  	// TaskPayloadCmd sets the arguments used to run the task payload.
   122  	//
   123  	// args[0] is the path, relative to the payload path, of the executable to
   124  	// run.
   125  	//
   126  	// NOTE: Kitchen tasks ignore this value.
   127  	TaskPayloadCmd(args []string)
   128  
   129  	// ClearGerritChanges removes all GerritChanges from the job.
   130  	ClearGerritChanges()
   131  
   132  	// AddGerritChange ensures the GerritChange is in the set of input CLs.
   133  	AddGerritChange(cl *bbpb.GerritChange)
   134  
   135  	// RemoveGerritChange ensures the GerritChange is not in the set of input CLs.
   136  	RemoveGerritChange(cl *bbpb.GerritChange)
   137  
   138  	// GitilesCommit sets the GitilesCommit.
   139  	//
   140  	// If `commit` is nil, removes the commit.
   141  	GitilesCommit(commit *bbpb.GitilesCommit)
   142  }
   143  
   144  // Edit runs a mutator function with an Editor.
   145  //
   146  // If one of the edit operations has an error, further method calls on the
   147  // Editor are ignored and this returns the error.
   148  func (jd *Definition) Edit(cb func(m Editor)) error {
   149  	var m interface {
   150  		Editor
   151  		Close() error
   152  	}
   153  
   154  	if jd.GetBuildbucket() != nil {
   155  		m = newBuildbucketEditor(jd)
   156  	} else {
   157  		m = newSwarmingEditor(jd)
   158  	}
   159  
   160  	cb(m)
   161  	return m.Close()
   162  }
   163  
   164  // HighLevelEdit runs a mutator function with a HighLevelEditor.
   165  //
   166  // If one of the edit operations has an error, further method calls on the
   167  // HighLevelEditor are ignored and this returns the error.
   168  func (jd *Definition) HighLevelEdit(cb func(m HighLevelEditor)) error {
   169  	var m interface {
   170  		HighLevelEditor
   171  		Close() error
   172  	}
   173  
   174  	if jd.GetBuildbucket() != nil {
   175  		m = newBuildbucketEditor(jd)
   176  	} else {
   177  		return errors.New("job.HighLevelEdit not supported for this Job")
   178  	}
   179  
   180  	cb(m)
   181  	return m.Close()
   182  }