github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/retry.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 yaml
    16  
    17  import "errors"
    18  
    19  // Retry defines retry logic.
    20  type Retry struct {
    21  	Max  int           `yaml:"max,omitempty"`
    22  	When Stringorslice `yaml:"when,omitempty"`
    23  }
    24  
    25  // UnmarshalYAML implements the unmarshal interface.
    26  func (v *Retry) UnmarshalYAML(unmarshal func(interface{}) error) error {
    27  	var out1 int
    28  	var out2 = struct {
    29  		Max  int           `yaml:"max"`
    30  		When Stringorslice `yaml:"when"`
    31  	}{}
    32  
    33  	if err := unmarshal(&out1); err == nil {
    34  		v.Max = out1
    35  		return nil
    36  	}
    37  
    38  	if err := unmarshal(&out2); err == nil {
    39  		v.Max = out2.Max
    40  		v.When = out2.When
    41  		return nil
    42  	}
    43  
    44  	return errors.New("failed to unmarshal retry")
    45  }
    46  
    47  // Enum of retry when types
    48  // https://docs.gitlab.com/ee/ci/yaml/#retrywhen
    49  const (
    50  	RetryAlways                 = "always"
    51  	RetryUnknownFailure         = "unknown_failure"
    52  	RetryScriptFailure          = "script_failure"
    53  	RetryApiFailure             = "api_failure"
    54  	RetrySturckOrTimeoutFailure = "stuck_or_timeout_failure"
    55  	RetryRunnerSystemFailure    = "runner_system_failure"
    56  	RetryRunnerUnsupported      = "runner_unsupported"
    57  	RetryStaleSchedule          = "stale_schedule"
    58  	RetryJobExecutionTimeout    = "job_execution_timeout"
    59  	RetryArchivedFailure        = "archived_failure"
    60  	RetrySchedulerFailure       = "scheduler_failure"
    61  	RetryIntegrityFailure       = "data_integrity_failure"
    62  )
    63  
    64  func (v *Retry) MarshalYAML() (interface{}, error) {
    65  	if v.Max > 0 && len(v.When) > 0 {
    66  		return struct {
    67  			Max  int           `yaml:"max,omitempty"`
    68  			When Stringorslice `yaml:"when,omitempty"`
    69  		}{
    70  			Max:  v.Max,
    71  			When: v.When,
    72  		}, nil
    73  	} else if v.Max > 0 {
    74  		return v.Max, nil
    75  	}
    76  	return nil, nil
    77  }