legacy configurations should pull if-not-exists

This commit is contained in:
Brad Rydzewski 2019-07-17 08:09:47 -07:00
parent 7180721ccf
commit d14ac477f9
11 changed files with 100 additions and 29 deletions

View File

@ -29,7 +29,7 @@ func Convert(d []byte, m Metadata) ([]byte, error) {
// systems we check to see if the configuration
// file is a legacy (pre 1.0) .drone.yml format.
if legacy.Match(d) {
return legacy.Convert(d)
return legacy.Convert(d, m.URL)
}
// else return the unmodified configuration
// back to the caller.

View File

@ -8,6 +8,6 @@ import "github.com/drone/drone-yaml/yaml/converter/legacy/internal"
// Convert converts the yaml configuration file from
// the legacy format to the 1.0+ format.
func Convert(d []byte) ([]byte, error) {
return yaml.Convert(d)
func Convert(d []byte, remote string) ([]byte, error) {
return yaml.Convert(d, remote)
}

View File

@ -7,6 +7,8 @@ package yaml
import (
"bytes"
"fmt"
"net/url"
"os"
"sort"
"strings"
@ -38,7 +40,7 @@ type Config struct {
// Convert converts the yaml configuration file from
// the legacy format to the 1.0+ format.
func Convert(d []byte) ([]byte, error) {
func Convert(d []byte, remote string) ([]byte, error) {
// hack: this is a hack to support teams migrating
// from 0.8 to 1.0 that are using yaml merge keys.
// it can be removed in a future version.
@ -65,6 +67,15 @@ func Convert(d []byte) ([]byte, error) {
pipeline.Workspace.Path = ""
}
if os.Getenv("DRONE_CONVERT_YAML_DEFAULT_WORKSPACE") == "true" {
if pipeline.Workspace.Base == "" {
pipeline.Workspace.Base = "/drone"
}
if pipeline.Workspace.Path == "" {
pipeline.Workspace.Path = toWorkspacePath(remote)
}
}
if len(from.Clone.Containers) != 0 {
pipeline.Clone.Disable = true
for _, container := range from.Clone.Containers {
@ -98,6 +109,15 @@ func Convert(d []byte) ([]byte, error) {
pipeline.Trigger.Branch.Include = from.Branches.Include
pipeline.Trigger.Branch.Exclude = from.Branches.Exclude
// registry credentials need to be emulated in 0.8. The
// migration utility automatically creates a secret named
// .dockerconfigjson for the registry credentials, which
// could be automatically added to the converted
// configuration. THIS HAS NOT BEEN THOROUGHLY TESTED.
if os.Getenv("DRONE_CONVERT_YAML_DEFAULT_PULL_SECRETS") == "true" {
pipeline.PullSecrets = []string{".dockerconfigjson"}
}
if from.Matrix != nil {
axes, err := matrix.Parse(d)
@ -351,3 +371,20 @@ func toVolumes(from *Config) []*droneyaml.Volume {
}
return to
}
// helper fucntion creates the workspace path using the
// repsotiory url.
func toWorkspacePath(link string) string {
parsed, err := url.Parse(link)
if err != nil {
return "src"
}
hostname := parsed.Hostname()
if hostname == "" {
return "src"
}
path := parsed.Path
path = strings.TrimPrefix(path, "/")
path = strings.TrimSuffix(path, "/")
return "src/" + hostname + "/" + path
}

View File

@ -14,7 +14,7 @@ import (
func TestConvert(t *testing.T) {
tests := []struct {
before, after string
before, after, url string
}{
{
before: "testdata/simple.yml",
@ -53,7 +53,7 @@ func TestConvert(t *testing.T) {
t.Error(err)
return
}
c, err := Convert(a)
c, err := Convert(a, test.url)
if err != nil {
t.Error(err)
return
@ -66,3 +66,32 @@ func TestConvert(t *testing.T) {
}
}
}
func TestWorkspacePath(t *testing.T) {
tests := []struct{
a string
b string
}{
{
a: "",
b: "src",
},
{
a: "https://github.com/octocat/hello-world",
b: "src/github.com/octocat/hello-world",
},
{
a: "https://github.com:80/octocat/hello-world",
b: "src/github.com/octocat/hello-world",
},
{
a: "github.com:80/octocat/hello-world",
b: "src",
},
}
for _, test := range tests {
if got, want := toWorkspacePath(test.a), test.b; got != want {
t.Errorf("Want workspace path %s, got %s", want, got)
}
}
}

View File

@ -8,14 +8,14 @@ platform:
steps:
- name: test
pull: default
pull: if-not-exists
image: golang:1.11
commands:
- go test -v ./...
services:
- name: redis
pull: default
pull: if-not-exists
image: redis:2.6
---
@ -28,14 +28,14 @@ platform:
steps:
- name: test
pull: default
pull: if-not-exists
image: golang:1.10
commands:
- go test -v ./...
services:
- name: redis
pull: default
pull: if-not-exists
image: redis:2.6
---
@ -48,14 +48,14 @@ platform:
steps:
- name: test
pull: default
pull: if-not-exists
image: golang:1.9
commands:
- go test -v ./...
services:
- name: redis
pull: default
pull: if-not-exists
image: redis:2.6
...

View File

@ -8,14 +8,14 @@ platform:
steps:
- name: test
pull: default
pull: if-not-exists
image: golang:1.11
commands:
- go test -v ./...
services:
- name: redis
pull: default
pull: if-not-exists
image: redis:2.6
---
@ -28,14 +28,14 @@ platform:
steps:
- name: test
pull: default
pull: if-not-exists
image: golang:1.11
commands:
- go test -v ./...
services:
- name: redis
pull: default
pull: if-not-exists
image: redis:2.8
---
@ -48,14 +48,14 @@ platform:
steps:
- name: test
pull: default
pull: if-not-exists
image: golang:1.10
commands:
- go test -v ./...
services:
- name: redis
pull: default
pull: if-not-exists
image: redis:2.6
---
@ -68,14 +68,14 @@ platform:
steps:
- name: test
pull: default
pull: if-not-exists
image: golang:1.10
commands:
- go test -v ./...
services:
- name: redis
pull: default
pull: if-not-exists
image: redis:2.8
...

View File

@ -12,7 +12,7 @@ workspace:
steps:
- name: build
pull: default
pull: if-not-exists
image: golang
commands:
- go get
@ -25,7 +25,7 @@ steps:
path: /go/bin
- name: test
pull: default
pull: if-not-exists
image: golang:latest
commands:
- go test -v
@ -34,7 +34,7 @@ steps:
path: /go/bin
- name: docker
pull: default
pull: if-not-exists
image: plugins/docker
settings:
repo: octocat/hello-world
@ -48,7 +48,7 @@ steps:
- master
- name: slack
pull: default
pull: if-not-exists
image: plugins/slack
settings:
channel: general
@ -58,7 +58,7 @@ steps:
services:
- name: database
pull: default
pull: if-not-exists
image: mysql
environment:
MYSQL_PASSWORD: bar

View File

@ -8,7 +8,7 @@ platform:
steps:
- name: docker
pull: default
pull: if-not-exists
image: plugins/docker
settings:
repo: octocat/hello-world

View File

@ -8,7 +8,7 @@ platform:
steps:
- name: docker
pull: default
pull: if-not-exists
image: plugins/docker
settings:
repo: octocat/hello-world

View File

@ -8,7 +8,7 @@ platform:
steps:
- name: docker
pull: default
pull: if-not-exists
image: plugins/docker
settings:
repo: octocat/hello-world

View File

@ -21,7 +21,12 @@ type Metadata struct {
// determine the yaml configuration format.
Filename string
// Ref of the commit use to choose the correct
// URL of the repository used to create the repository
// workspace directory using the fully qualified name.
// e.g. /drone/src/github.com/octocat/hello-world
URL string
// Ref of the commit used to choose the correct
// pipeline if the configuration format defines
// multiple pipelines (like Bitbucket)
Ref string