1
0
Fork 0

Initial implementation

This commit is contained in:
Lauris BH 2022-07-27 00:16:28 +03:00
parent ef67fa529a
commit a3a8f082c2
No known key found for this signature in database
GPG Key ID: DFDE60A0093EB926
17 changed files with 596 additions and 4 deletions

20
.editorconfig Normal file
View File

@ -0,0 +1,20 @@
; https://editorconfig.org/
root = true
[*]
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
[{Makefile,go.mod,go.sum,*.go,.gitmodules}]
indent_style = tab
indent_size = 2
[*.md]
indent_size = 2
trim_trailing_whitespace = false
eclint_indent_style = unset

4
.gitignore vendored
View File

@ -14,10 +14,12 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
__debug_bin
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work
/pipeline-convert
/output

15
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,15 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"buildFlags": "-tags 'netgo osusergo'",
"program": "${workspaceRoot}/cmd/pipeline-convert/",
"args": ["-s", "drone/testdata/simple.yml", "-d", "output/"],
"cwd": "${workspaceFolder}",
}
]
}

35
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,35 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "go",
"linux": {
"args": ["build", "-ldflags", "-w -s", "-tags", "netgo osusergo", "-o", "pipeline-convert", "${workspaceRoot}/cmd/pipeline-convert/..." ]
},
"osx": {
"args": ["build", "-ldflags", "-w -s", "-tags", "netgo osusergo", "-o", "pipeline-convert", "${workspaceRoot}/cmd/pipeline-convert/..." ]
},
"windows": {
"args": ["build", "-ldflags", "-w -s", "-tags", "netgo osusergo", "-o", "pipeline-convert.exe", "\"${workspaceRoot}\\cmd\\pipeline-convert\\...\""]
},
"group": {
"isDefault": true,
"kind": "build"
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": true,
"clear": false
},
"options": {
"cwd": "${workspaceRoot}"
},
"problemMatcher": "$go"
}
]
}

12
.woodpecker.yml Normal file
View File

@ -0,0 +1,12 @@
pipeline:
test:
image: golang:1.18
pull: true
commands:
- "go test -ldflags='-w -s' -tags 'netgo osusergo' -cover -coverprofile=coverage.out -covermode=atomic -json ./... > report.json"
when:
event:
- push
- pull_request
branch:
- main

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) <year> <copyright holders>
Copyright (c) 2022 Lauris BH
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View File

@ -1,3 +1,5 @@
# woodpecker-pipeline-transform
# Woodpecker CI pipeline transform
Go library and utility to convert different pipelines to Woodpecker CI piplelines
Go library and utility to convert different pipelines to Woodpecker CI pipeline(s).
Currently supports converting only from Drone CI pipeline format with limited functionality.

View File

@ -0,0 +1,152 @@
// Copyright 2022 Lauris BH. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
transform "codeberg.org/lafriks/woodpecker-pipeline-transform"
"codeberg.org/lafriks/woodpecker-pipeline-transform/drone"
"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
)
var (
pipelineType string
sourcePath string
destPath string
)
func ReadSource(path string) ([]*transform.Source, error) {
fs, err := os.Stat(path)
if err != nil {
return nil, err
}
if !fs.IsDir() {
buf, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return []*transform.Source{
{
Name: fs.Name(),
Content: buf,
},
}, nil
}
sources := make([]*transform.Source, 0)
dir, err := os.ReadDir(path)
if err != nil {
return nil, err
}
for _, f := range dir {
if f.IsDir() {
continue
}
buf, err := os.ReadFile(filepath.Join(path, f.Name()))
if err != nil {
return nil, err
}
sources = append(sources, &transform.Source{
Name: f.Name(),
Content: buf,
})
}
return sources, nil
}
func WriteDest(pipelines []*transform.Pipeline, path string) error {
if len(pipelines) == 0 {
return nil
}
fs, err := os.Stat(path)
if (err != nil || !fs.IsDir()) && len(pipelines) > 1 {
return errors.New("source has multiple pipelines but destination path is not a directory")
}
if len(pipelines) > 1 {
if fs.Name() != ".woodpecker" {
path = filepath.Join(path, ".woodpecker")
}
if err = os.MkdirAll(path, 0o755); err != nil {
return err
}
}
for _, pipeline := range pipelines {
p := path
if len(pipelines) == 1 {
p = filepath.Join(p, ".woodpecker.yml")
} else {
p = filepath.Join(p, "."+pipeline.Name+".yml")
}
buf, err := yaml.Marshal(pipeline)
if err != nil {
return err
}
if err = os.WriteFile(p, buf, 0o644); err != nil {
return err
}
}
return nil
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "pipeline-convert",
Short: "Convert a pipeline from one format to another",
Long: `Convert a pipeline from one format to another.
Currently supports converting from Drone CI pipelines to Woodpecker CI pipeline format.`,
Run: func(cmd *cobra.Command, args []string) {
if pipelineType != "drone" {
fmt.Println("Error: unsupported pipeline format type")
os.Exit(1)
}
if sourcePath == "" {
fmt.Println("Error: source path is required")
cmd.Help()
os.Exit(1)
}
if destPath == "" {
fmt.Println("Error: destination path is required")
cmd.Help()
os.Exit(1)
}
sources, err := ReadSource(sourcePath)
if err != nil {
fmt.Printf("Error: failed to load source: %v\n", err)
os.Exit(1)
}
p := drone.New()
pipelines, err := p.Transform(sources)
if err != nil {
fmt.Printf("Error: failed to convert pipeline: %v\n", err)
os.Exit(1)
}
if err = WriteDest(pipelines, destPath); err != nil {
fmt.Printf("Error: failed to write converted pipeline to destination: %v\n", err)
os.Exit(1)
}
},
}
func main() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
rootCmd.Flags().StringVarP(&pipelineType, "type", "t", "drone", "pipeline format type (supported: drone)")
rootCmd.Flags().StringVarP(&sourcePath, "source", "s", "", "source pipeline file/dir path")
rootCmd.Flags().StringVarP(&destPath, "dest", "d", "", "destination pipeline file/dir path")
}

70
drone/drone.go Normal file
View File

@ -0,0 +1,70 @@
// Copyright 2022 Lauris BH. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package drone
import (
"bytes"
transform "codeberg.org/lafriks/woodpecker-pipeline-transform"
"github.com/goccy/go-yaml"
)
func New() *DronePipeline {
return &DronePipeline{}
}
type DronePipeline struct{}
func (d DronePipeline) ConvertImage(image string) string {
return image
}
func (d DronePipeline) Convert(pipeline *Pipeline) (*transform.Pipeline, error) {
if pipeline.Kind != "pipeline" {
return nil, transform.UnsupportedError
}
if pipeline.Type != "docker" {
return nil, transform.UnsupportedError
}
p := &transform.Pipeline{
Name: pipeline.Name,
Steps: make(transform.Steps, 0, len(pipeline.Steps)),
}
for _, step := range pipeline.Steps {
p.Steps = append(p.Steps, &transform.Step{
Name: step.Name,
Image: d.ConvertImage(step.Image),
Commands: step.Commands,
})
}
return p, nil
}
func (d DronePipeline) Transform(sources []*transform.Source) ([]*transform.Pipeline, error) {
p := make([]*transform.Pipeline, 0, len(sources))
for _, source := range sources {
dec := yaml.NewDecoder(bytes.NewReader(source.Content))
var err error
for err == nil {
pipeline := &Pipeline{}
if err = dec.Decode(pipeline); err != nil {
if err.Error() != "EOF" {
return nil, err
}
break
}
r, err := d.Convert(pipeline)
if err != nil {
return nil, err
}
p = append(p, r)
}
}
return p, nil
}

61
drone/drone_test.go Normal file
View File

@ -0,0 +1,61 @@
// Copyright 2022 Lauris BH. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package drone_test
import (
"os"
"testing"
transform "codeberg.org/lafriks/woodpecker-pipeline-transform"
"codeberg.org/lafriks/woodpecker-pipeline-transform/drone"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func getPipelineByName(pipelines []*transform.Pipeline, name string) *transform.Pipeline {
for _, pipeline := range pipelines {
if pipeline.Name == name {
return pipeline
}
}
return nil
}
func TestTransformSimple(t *testing.T) {
buf, err := os.ReadFile("testdata/simple.yml")
require.NoError(t, err)
d := drone.New()
pipelines, err := d.Transform([]*transform.Source{
{
Name: "simple",
Content: buf,
},
})
require.NoError(t, err)
require.Len(t, pipelines, 2)
pipeline := getPipelineByName(pipelines, "build")
require.NotNil(t, pipeline, "build pipeline not found")
assert.Len(t, pipeline.Steps, 2)
assert.Equal(t, "test", pipeline.Steps[0].Name)
assert.Equal(t, "golang:1.18", pipeline.Steps[0].Image)
assert.Len(t, pipeline.Steps[0].Commands, 1)
assert.Equal(t, "build", pipeline.Steps[1].Name)
assert.Equal(t, "golang:1.18", pipeline.Steps[1].Image)
assert.Len(t, pipeline.Steps[1].Commands, 1)
pipeline = getPipelineByName(pipelines, "deploy")
require.NotNil(t, pipeline, "deploy pipeline not found")
assert.Len(t, pipeline.Steps, 1)
assert.Equal(t, "deploy", pipeline.Steps[0].Name)
assert.Equal(t, "alpine:latest", pipeline.Steps[0].Image)
assert.Len(t, pipeline.Steps[0].Commands, 1)
}

18
drone/pipeline.go Normal file
View File

@ -0,0 +1,18 @@
// Copyright 2022 Lauris BH. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package drone
type Step struct {
Name string `yaml:"name"`
Image string `yaml:"image"`
Commands []string `yaml:"commands"`
}
type Pipeline struct {
Kind string `yaml:"kind"`
Type string `yaml:"type"`
Name string `yaml:"name"`
Steps []*Step `yaml:"steps"`
}

33
drone/testdata/simple.yml vendored Normal file
View File

@ -0,0 +1,33 @@
---
kind: pipeline
type: docker
name: build
steps:
- name: test
image: golang:1.18
commands:
- go test
- name: build
image: golang:1.18
commands:
- go build
---
kind: pipeline
type: docker
name: deploy
depends_on:
- build
trigger:
status:
- success
steps:
- name: deploy
image: alpine:latest
commands:
- echo "Deploy"

22
go.mod Normal file
View File

@ -0,0 +1,22 @@
module codeberg.org/lafriks/woodpecker-pipeline-transform
go 1.18
require (
github.com/goccy/go-yaml v1.9.5
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.4.0
)
require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

51
go.sum Normal file
View File

@ -0,0 +1,51 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/goccy/go-yaml v1.9.5 h1:Eh/+3uk9kLxG4koCX6lRMAPS1OaMSAi+FJcya0INdB0=
github.com/goccy/go-yaml v1.9.5/go.mod h1:U/jl18uSupI5rdI2jmuCswEA2htH9eXfferR3KfscvA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

43
pipeline.go Normal file
View File

@ -0,0 +1,43 @@
// Copyright 2022 Lauris BH. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package transform
import (
"errors"
"github.com/goccy/go-yaml"
)
var UnsupportedError = errors.New("unsupported pipeline")
type Step struct {
Name string `yaml:"-"`
Image string `yaml:"image"`
Commands []string `yaml:"commands,omitempty"`
}
type Steps []*Step
func (s Steps) MarshalYAML() (interface{}, error) {
v := make(yaml.MapSlice, len(s))
for i, step := range s {
v[i] = yaml.MapItem{
Key: step.Name,
Value: step,
}
}
return v, nil
}
type Workspace struct {
Base string `yaml:"base"`
Path string `yaml:"path"`
}
type Pipeline struct {
Name string `yaml:"-"`
Workspace *Workspace `yaml:"workspace,omitempty"`
Steps Steps `yaml:"pipeline"`
}

42
pipeline_test.go Normal file
View File

@ -0,0 +1,42 @@
// Copyright 2022 Lauris BH. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package transform_test
import (
"testing"
transform "codeberg.org/lafriks/woodpecker-pipeline-transform"
"github.com/goccy/go-yaml"
"github.com/stretchr/testify/assert"
)
func TestPipelineUnmarshal(t *testing.T) {
p := transform.Pipeline{
Steps: transform.Steps{
&transform.Step{
Name: "step1",
Image: "alpine:latest",
Commands: []string{"echo Step 1", "echo $${CI_JOB_ID}"},
},
&transform.Step{
Name: "step2",
Image: "alpine:latest",
},
},
}
buf, err := yaml.Marshal(&p)
assert.NoError(t, err)
assert.Equal(t, `pipeline:
step1:
image: alpine:latest
commands:
- echo Step 1
- echo $${CI_JOB_ID}
step2:
image: alpine:latest
`, string(buf))
}

14
transform.go Normal file
View File

@ -0,0 +1,14 @@
// Copyright 2022 Lauris BH. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package transform
type Source struct {
Name string
Content []byte
}
type Transformer interface {
Transform([]*Source) ([]*Pipeline, error)
}