github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/azure/convert.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 azure converts Azure Devops pipelines to Harness pipelines.
    16  package azure
    17  
    18  import (
    19  	"bytes"
    20  	"errors"
    21  	"io"
    22  	"os"
    23  
    24  	harness "github.com/drone/spec/dist/go"
    25  
    26  	"github.com/drone/go-convert/internal/store"
    27  	"github.com/ghodss/yaml"
    28  )
    29  
    30  // Converter converts a Azure Devops pipeline to a Harness
    31  // v1 pipeline.
    32  type Converter struct {
    33  	kubeEnabled   bool
    34  	kubeNamespace string
    35  	kubeConnector string
    36  	dockerhubConn string
    37  	identifiers   *store.Identifiers
    38  
    39  	// // as we walk the yaml, we store a
    40  	// // a snapshot of the current node and
    41  	// // its parents.
    42  	// config *azure.Pipeline
    43  	// stage  *azure.Stage
    44  }
    45  
    46  // New creates a new Converter that converts an Azure
    47  // Devops pipeline to a harness v1 pipeline.
    48  func New(options ...Option) *Converter {
    49  	d := new(Converter)
    50  
    51  	// create the unique identifier store. this store
    52  	// is used for registering unique identifiers to
    53  	// prevent duplicate names, unique index violations.
    54  	d.identifiers = store.New()
    55  
    56  	// loop through and apply the options.
    57  	for _, option := range options {
    58  		option(d)
    59  	}
    60  
    61  	// set the default kubernetes namespace.
    62  	if d.kubeNamespace == "" {
    63  		d.kubeNamespace = "default"
    64  	}
    65  
    66  	// set the runtime to kubernetes if the kubernetes
    67  	// connector is configured.
    68  	if d.kubeConnector != "" {
    69  		d.kubeEnabled = true
    70  	}
    71  
    72  	return d
    73  }
    74  
    75  // Convert downgrades a v1 pipeline.
    76  func (d *Converter) Convert(r io.Reader) ([]byte, error) {
    77  	// src, err := azure.Parse(r)
    78  	// if err != nil {
    79  	// 	return nil, err
    80  	// }
    81  	// d.config = src // push the azure config to the state
    82  	// return d.convert()
    83  	return nil, errors.New("not implemented")
    84  }
    85  
    86  // ConvertString downgrades a v1 pipeline.
    87  func (d *Converter) ConvertBytes(b []byte) ([]byte, error) {
    88  	return d.Convert(
    89  		bytes.NewBuffer(b),
    90  	)
    91  }
    92  
    93  // ConvertString downgrades a v1 pipeline.
    94  func (d *Converter) ConvertString(s string) ([]byte, error) {
    95  	return d.Convert(
    96  		bytes.NewBufferString(s),
    97  	)
    98  }
    99  
   100  // ConvertFile downgrades a v1 pipeline.
   101  func (d *Converter) ConvertFile(p string) ([]byte, error) {
   102  	f, err := os.Open(p)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  	defer f.Close()
   107  	return d.Convert(f)
   108  }
   109  
   110  // converts converts an Azure Devops pipeline to a
   111  // Harness v1 pipeline.
   112  func (d *Converter) convert() ([]byte, error) {
   113  
   114  	// create the harness pipeline spec
   115  	pipeline := &harness.Pipeline{
   116  		// Default: convertDefault(d.config),
   117  	}
   118  
   119  	// create the harness pipeline resource
   120  	config := &harness.Config{
   121  		Version: 1,
   122  		Kind:    "pipeline",
   123  		Spec:    pipeline,
   124  	}
   125  
   126  	// for _, steps := range d.config.Pipelines.Default {
   127  	// 	if steps.Stage != nil {
   128  	// 		// TODO support for fast-fail
   129  	// 		d.stage = steps.Stage // push the stage to the state
   130  	// 		stage := d.convertStage()
   131  	// 		pipeline.Stages = append(pipeline.Stages, stage)
   132  	// 	}
   133  	// }
   134  
   135  	// marshal the harness yaml
   136  	out, err := yaml.Marshal(config)
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  
   141  	return out, nil
   142  }