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 // systems we check to see if the configuration
// file is a legacy (pre 1.0) .drone.yml format. // file is a legacy (pre 1.0) .drone.yml format.
if legacy.Match(d) { if legacy.Match(d) {
return legacy.Convert(d) return legacy.Convert(d, m.URL)
} }
// else return the unmodified configuration // else return the unmodified configuration
// back to the caller. // 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 // Convert converts the yaml configuration file from
// the legacy format to the 1.0+ format. // the legacy format to the 1.0+ format.
func Convert(d []byte) ([]byte, error) { func Convert(d []byte, remote string) ([]byte, error) {
return yaml.Convert(d) return yaml.Convert(d, remote)
} }

View File

@ -7,6 +7,8 @@ package yaml
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"net/url"
"os"
"sort" "sort"
"strings" "strings"
@ -38,7 +40,7 @@ type Config struct {
// Convert converts the yaml configuration file from // Convert converts the yaml configuration file from
// the legacy format to the 1.0+ format. // 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 // hack: this is a hack to support teams migrating
// from 0.8 to 1.0 that are using yaml merge keys. // from 0.8 to 1.0 that are using yaml merge keys.
// it can be removed in a future version. // it can be removed in a future version.
@ -65,6 +67,15 @@ func Convert(d []byte) ([]byte, error) {
pipeline.Workspace.Path = "" 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 { if len(from.Clone.Containers) != 0 {
pipeline.Clone.Disable = true pipeline.Clone.Disable = true
for _, container := range from.Clone.Containers { 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.Include = from.Branches.Include
pipeline.Trigger.Branch.Exclude = from.Branches.Exclude 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 { if from.Matrix != nil {
axes, err := matrix.Parse(d) axes, err := matrix.Parse(d)
@ -351,3 +371,20 @@ func toVolumes(from *Config) []*droneyaml.Volume {
} }
return to 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) { func TestConvert(t *testing.T) {
tests := []struct { tests := []struct {
before, after string before, after, url string
}{ }{
{ {
before: "testdata/simple.yml", before: "testdata/simple.yml",
@ -53,7 +53,7 @@ func TestConvert(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
c, err := Convert(a) c, err := Convert(a, test.url)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return 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: steps:
- name: test - name: test
pull: default pull: if-not-exists
image: golang:1.11 image: golang:1.11
commands: commands:
- go test -v ./... - go test -v ./...
services: services:
- name: redis - name: redis
pull: default pull: if-not-exists
image: redis:2.6 image: redis:2.6
--- ---
@ -28,14 +28,14 @@ platform:
steps: steps:
- name: test - name: test
pull: default pull: if-not-exists
image: golang:1.10 image: golang:1.10
commands: commands:
- go test -v ./... - go test -v ./...
services: services:
- name: redis - name: redis
pull: default pull: if-not-exists
image: redis:2.6 image: redis:2.6
--- ---
@ -48,14 +48,14 @@ platform:
steps: steps:
- name: test - name: test
pull: default pull: if-not-exists
image: golang:1.9 image: golang:1.9
commands: commands:
- go test -v ./... - go test -v ./...
services: services:
- name: redis - name: redis
pull: default pull: if-not-exists
image: redis:2.6 image: redis:2.6
... ...

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -21,7 +21,12 @@ type Metadata struct {
// determine the yaml configuration format. // determine the yaml configuration format.
Filename string 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 // pipeline if the configuration format defines
// multiple pipelines (like Bitbucket) // multiple pipelines (like Bitbucket)
Ref string Ref string