github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/circle/optimize.go (about)

     1  // Copyright 2022 Harness, Inc.
     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 circle
    16  
    17  import (
    18  	circle "github.com/drone/go-convert/convert/circle/yaml"
    19  	harness "github.com/drone/spec/dist/go"
    20  )
    21  
    22  // this is a poor attempt at an optimization to remove
    23  // individual cache steps and replace with cache intelligence,
    24  // if possible. The criteris is that there can only be one
    25  // save step, and zero or one restore steps. If both a save
    26  // and restore step exist, they must have the same cache key.
    27  func optimizeCache(stage *harness.StageCI) {
    28  	var save *harness.StepPlugin
    29  	var restore *harness.StepPlugin
    30  	var steps []*harness.Step
    31  
    32  	for _, step := range stage.Steps {
    33  		spec, ok := step.Spec.(*harness.StepPlugin)
    34  		if ok == false || spec.With == nil {
    35  			steps = append(steps, step)
    36  			continue
    37  		}
    38  		if spec.With["rebuild"] == "true" {
    39  			if save != nil {
    40  				return // exit if multiple save
    41  			} else {
    42  				save = spec
    43  			}
    44  		} else if spec.With["restore"] == "true" {
    45  			if restore != nil {
    46  				return // exit if multiple restore
    47  			} else {
    48  				restore = spec
    49  			}
    50  		} else {
    51  			steps = append(steps, step)
    52  		}
    53  	}
    54  	if save == nil {
    55  		return
    56  	}
    57  	if restore != nil &&
    58  		save.With["cache_key"] != restore.With["cache_key"] {
    59  		return
    60  	}
    61  
    62  	stage.Steps = steps
    63  	stage.Cache = &harness.Cache{
    64  		Enabled: true,
    65  		Key:     save.With["cache_key"].(string),
    66  		Paths:   save.With["mount"].(circle.Stringorslice),
    67  	}
    68  }
    69  
    70  // this is a helper function that optimizes stages that
    71  // have a single group step.
    72  func optimizeGroup(stage *harness.StageCI) {
    73  	if len(stage.Steps) != 1 {
    74  		return
    75  	}
    76  	step := stage.Steps[0]
    77  	if step.Spec == nil {
    78  		return // should never happen
    79  	}
    80  	if group, ok := step.Spec.(*harness.StepGroup); ok {
    81  		stage.Steps = group.Steps
    82  	}
    83  }