github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/trigger.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  type Trigger struct {
    20  	Project  string   `yaml:"-"`
    21  	Branch   string   `yaml:"branch,omitempty"`
    22  	Include  string   `yaml:"include,omitempty"`
    23  	Strategy string   `yaml:"strategy,omitempty"`
    24  	Forward  *Forward `yaml:"forward,omitempty"`
    25  }
    26  
    27  type Forward struct {
    28  	YamlVariables     *bool `yaml:"yaml_variables,omitempty"`
    29  	PipelineVariables bool  `yaml:"pipeline_variables,omitempty"`
    30  }
    31  
    32  // UnmarshalYAML implements the yaml.Unmarshaler interface.
    33  func (v *Trigger) UnmarshalYAML(unmarshal func(interface{}) error) error {
    34  	var out1 string
    35  	var out2 = struct {
    36  		Project  string   `yaml:"project,omitempty"`
    37  		Branch   string   `yaml:"branch,omitempty"`
    38  		Include  string   `yaml:"include,omitempty"`
    39  		Strategy string   `yaml:"strategy,omitempty"`
    40  		Forward  *Forward `yaml:"forward,omitempty"`
    41  	}{}
    42  
    43  	if err := unmarshal(&out1); err == nil {
    44  		v.Project = out1
    45  		return nil
    46  	}
    47  
    48  	if err := unmarshal(&out2); err == nil {
    49  		v.Project = out2.Project
    50  		v.Branch = out2.Branch
    51  		v.Include = out2.Include
    52  		v.Strategy = out2.Strategy
    53  		v.Forward = out2.Forward
    54  		return nil
    55  	}
    56  
    57  	return errors.New("failed to unmarshal trigger")
    58  }
    59  
    60  func (v *Trigger) MarshalYAML() (interface{}, error) {
    61  	// Always marshal as a struct
    62  	return struct {
    63  		Project  string   `yaml:"project,omitempty"`
    64  		Branch   string   `yaml:"branch,omitempty"`
    65  		Include  string   `yaml:"include,omitempty"`
    66  		Strategy string   `yaml:"strategy,omitempty"`
    67  		Forward  *Forward `yaml:"forward,omitempty"`
    68  	}{
    69  		Project:  v.Project,
    70  		Branch:   v.Branch,
    71  		Include:  v.Include,
    72  		Strategy: v.Strategy,
    73  		Forward:  v.Forward,
    74  	}, nil
    75  }