From d14ac477f9832101b02f0987eabc6d12b025292d Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Wed, 17 Jul 2019 08:09:47 -0700 Subject: [PATCH] legacy configurations should pull if-not-exists --- yaml/converter/convert.go | 2 +- yaml/converter/legacy/convert.go | 4 +- yaml/converter/legacy/internal/config.go | 39 ++++++++++++++++++- yaml/converter/legacy/internal/config_test.go | 33 +++++++++++++++- .../internal/testdata/matrix_1.yml.golden | 12 +++--- .../internal/testdata/matrix_2.yml.golden | 16 ++++---- .../internal/testdata/simple.yml.golden | 10 ++--- .../internal/testdata/vault_1.yml.golden | 2 +- .../internal/testdata/vault_2.yml.golden | 2 +- .../internal/testdata/vault_3.yml.golden | 2 +- yaml/converter/metadata.go | 7 +++- 11 files changed, 100 insertions(+), 29 deletions(-) diff --git a/yaml/converter/convert.go b/yaml/converter/convert.go index d6feff2..d5b02a6 100644 --- a/yaml/converter/convert.go +++ b/yaml/converter/convert.go @@ -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. diff --git a/yaml/converter/legacy/convert.go b/yaml/converter/legacy/convert.go index cb80e70..f23ffa6 100644 --- a/yaml/converter/legacy/convert.go +++ b/yaml/converter/legacy/convert.go @@ -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) } diff --git a/yaml/converter/legacy/internal/config.go b/yaml/converter/legacy/internal/config.go index 501e53a..2bcafa9 100644 --- a/yaml/converter/legacy/internal/config.go +++ b/yaml/converter/legacy/internal/config.go @@ -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 +} \ No newline at end of file diff --git a/yaml/converter/legacy/internal/config_test.go b/yaml/converter/legacy/internal/config_test.go index 3b30d75..60aed07 100644 --- a/yaml/converter/legacy/internal/config_test.go +++ b/yaml/converter/legacy/internal/config_test.go @@ -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) + } + } +} \ No newline at end of file diff --git a/yaml/converter/legacy/internal/testdata/matrix_1.yml.golden b/yaml/converter/legacy/internal/testdata/matrix_1.yml.golden index a78c2a6..b70b76c 100644 --- a/yaml/converter/legacy/internal/testdata/matrix_1.yml.golden +++ b/yaml/converter/legacy/internal/testdata/matrix_1.yml.golden @@ -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 ... diff --git a/yaml/converter/legacy/internal/testdata/matrix_2.yml.golden b/yaml/converter/legacy/internal/testdata/matrix_2.yml.golden index 609709d..c8681e4 100644 --- a/yaml/converter/legacy/internal/testdata/matrix_2.yml.golden +++ b/yaml/converter/legacy/internal/testdata/matrix_2.yml.golden @@ -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 ... diff --git a/yaml/converter/legacy/internal/testdata/simple.yml.golden b/yaml/converter/legacy/internal/testdata/simple.yml.golden index dbe1583..7e29a9c 100644 --- a/yaml/converter/legacy/internal/testdata/simple.yml.golden +++ b/yaml/converter/legacy/internal/testdata/simple.yml.golden @@ -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 diff --git a/yaml/converter/legacy/internal/testdata/vault_1.yml.golden b/yaml/converter/legacy/internal/testdata/vault_1.yml.golden index a50d634..3a08d2a 100644 --- a/yaml/converter/legacy/internal/testdata/vault_1.yml.golden +++ b/yaml/converter/legacy/internal/testdata/vault_1.yml.golden @@ -8,7 +8,7 @@ platform: steps: - name: docker - pull: default + pull: if-not-exists image: plugins/docker settings: repo: octocat/hello-world diff --git a/yaml/converter/legacy/internal/testdata/vault_2.yml.golden b/yaml/converter/legacy/internal/testdata/vault_2.yml.golden index 7e9fc74..b4c60aa 100644 --- a/yaml/converter/legacy/internal/testdata/vault_2.yml.golden +++ b/yaml/converter/legacy/internal/testdata/vault_2.yml.golden @@ -8,7 +8,7 @@ platform: steps: - name: docker - pull: default + pull: if-not-exists image: plugins/docker settings: repo: octocat/hello-world diff --git a/yaml/converter/legacy/internal/testdata/vault_3.yml.golden b/yaml/converter/legacy/internal/testdata/vault_3.yml.golden index 7e9fc74..b4c60aa 100644 --- a/yaml/converter/legacy/internal/testdata/vault_3.yml.golden +++ b/yaml/converter/legacy/internal/testdata/vault_3.yml.golden @@ -8,7 +8,7 @@ platform: steps: - name: docker - pull: default + pull: if-not-exists image: plugins/docker settings: repo: octocat/hello-world diff --git a/yaml/converter/metadata.go b/yaml/converter/metadata.go index a3e3fff..9697e19 100644 --- a/yaml/converter/metadata.go +++ b/yaml/converter/metadata.go @@ -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