github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/harness/yaml/stage.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 (
    18  	"encoding/json"
    19  	"fmt"
    20  )
    21  
    22  type (
    23  	// Stage defines a pipeline stage.
    24  	Stage struct {
    25  		ID          string      `json:"identifier,omitempty"   yaml:"identifier,omitempty"`
    26  		Description string      `json:"description,omitempty"  yaml:"description,omitempty"`
    27  		Name        string      `json:"name,omitempty"         yaml:"name,omitempty"`
    28  		Spec        interface{} `json:"spec,omitempty"         yaml:"spec,omitempty"`
    29  		Type        string      `json:"type,omitempty"         yaml:"type,omitempty"`
    30  		Vars        []*Variable `json:"variables,omitempty"    yaml:"variables,omitempty"`
    31  		When        *StageWhen  `json:"when,omitempty"         yaml:"when,omitempty"`
    32  		Strategy    *Strategy   `json:"strategy,omitempty"     yaml:"strategy,omitempty"`
    33  	}
    34  
    35  	// StageApproval defines an approval stage.
    36  	StageApproval struct {
    37  		// TODO
    38  	}
    39  
    40  	// StageCI defines a continuous integration stage.
    41  	StageCI struct {
    42  		Cache          *Cache          `json:"caching,omitempty"             yaml:"caching,omitempty"`
    43  		Clone          bool            `json:"cloneCodebase,omitempty"       yaml:"cloneCodebase,omitempty"`
    44  		Execution      Execution       `json:"execution,omitempty"           yaml:"execution,omitempty"`
    45  		Infrastructure *Infrastructure `json:"infrastructure,omitempty"      yaml:"infrastructure,omitempty"`
    46  		Platform       *Platform       `json:"platform,omitempty"            yaml:"platform,omitempty"`
    47  		Runtime        *Runtime        `json:"runtime,omitempty"            yaml:"runtime,omitempty"`
    48  		Services       []*Service      `json:"serviceDependencies,omitempty" yaml:"serviceDependencies,omitempty"`
    49  		SharedPaths    []string        `json:"sharedPaths,omitempty"         yaml:"sharedPaths,omitempty"`
    50  	}
    51  
    52  	// StageDeployment defines a deployment stage.
    53  	StageDeployment struct {
    54  		// TODO
    55  	}
    56  
    57  	// StageFeatureFlag defines a feature flag stage.
    58  	StageFeatureFlag struct {
    59  		Execution *Execution `json:"execution,omitempty" yaml:"execution,omitempty"`
    60  	}
    61  
    62  	StageWhen struct {
    63  		PipelineStatus string `json:"pipelineStatus,omitempty" yaml:"pipelineStatus,omitempty"`
    64  		Condition      string `json:"condition,omitempty" yaml:"condition,omitempty"`
    65  	}
    66  
    67  	Strategy struct {
    68  		Matrix      map[string]interface{} `json:"matrix,omitempty" yaml:"matrix,omitempty"`
    69  		Parallelism *Parallelism           `json:"parallelism,omitempty" yaml:"parallelism,omitempty"`
    70  		Repeat      *Repeat                `json:"repeat,omitempty" yaml:"repeat,omitempty"`
    71  	}
    72  
    73  	Exclusion map[string]string
    74  
    75  	Parallelism struct {
    76  		Number         int `yaml:"parallelism"`
    77  		MaxConcurrency int `yaml:"maxConcurrency"`
    78  	}
    79  
    80  	Repeat struct {
    81  		Times          int      `yaml:"times,omitempty"`
    82  		Items          []string `yaml:"items,omitempty"`
    83  		MaxConcurrency int      `yaml:"maxConcurrency,omitempty"`
    84  	}
    85  )
    86  
    87  // UnmarshalJSON implement the json.Unmarshaler interface.
    88  func (s *Stage) UnmarshalJSON(data []byte) error {
    89  	type S Stage
    90  	type T struct {
    91  		*S
    92  		Spec json.RawMessage `json:"spec"`
    93  	}
    94  
    95  	obj := &T{S: (*S)(s)}
    96  	if err := json.Unmarshal(data, obj); err != nil {
    97  		return err
    98  	}
    99  
   100  	switch s.Type {
   101  	case StageTypeCI:
   102  		s.Spec = new(StageCI)
   103  	case StageTypeFeatureFlag:
   104  		s.Spec = new(StageFeatureFlag)
   105  	default:
   106  		return fmt.Errorf("unknown stage type %s", s.Type)
   107  	}
   108  	return json.Unmarshal(obj.Spec, s.Spec)
   109  }