grim/convey

Parents 402ef700adef
Children c47dd1f8ad1d
Add unmarshalling support for complex bitbucket image's. Refs #114
--- a/bitbucket/loader.go Sat Jul 22 10:45:04 2017 -0500
+++ b/bitbucket/loader.go Sat Jul 22 11:43:01 2017 -0500
@@ -43,18 +43,18 @@
plan.Stages[0].Tasks = append(plan.Stages[0].Tasks, name)
}
-func addScript(name, image string, script []string, plan plans.Plan, cfg *config.Config) {
+func addScript(name string, image image, script []string, plan plans.Plan, cfg *config.Config) {
task := &docker.Run{
Shell: "/bin/sh",
Script: script,
- Image: image,
+ Image: image.Name,
WorkDir: "/workspace",
}
addTask(name, task, plan, cfg)
}
-func addPipeline(name, defImage string, pipelines []pipeline, cfg *config.Config) {
+func addPipeline(name string, defImage image, pipelines []pipeline, cfg *config.Config) {
plan := plans.Plan{
Stages: []stages.Stage{
stages.Stage{
@@ -67,7 +67,7 @@
for idx, pipeline := range pipelines {
image := defImage
- if pipeline.Steps.Image != "" {
+ if pipeline.Steps.Image.Name != "" {
image = pipeline.Steps.Image
}
@@ -119,7 +119,7 @@
cfg.Plans[name] = plan
}
-func addPipelines(base, defImage string, pipelines map[string][]pipeline, cfg *config.Config) {
+func addPipelines(base string, defImage image, pipelines map[string][]pipeline, cfg *config.Config) {
for name, branchPipeline := range pipelines {
if len(branchPipeline) > 0 {
name := fmt.Sprintf("%s-%s", base, name)
--- a/bitbucket/types.go Sat Jul 22 10:45:04 2017 -0500
+++ b/bitbucket/types.go Sat Jul 22 11:43:01 2017 -0500
@@ -17,8 +17,48 @@
*/
package bitbucket
+import (
+ "github.com/go-yaml/yaml"
+)
+
+type image struct {
+ Name string `yaml:"name"`
+ Username string `yaml:"username"`
+ Password string `yaml:"password"`
+ EMail string `yaml:"email"`
+}
+
+func (i *image) UnmarshalYAML(unmarshal func(interface{}) error) error {
+ // first check if this is the basic format where it's just a name
+ name := ""
+ err := unmarshal(&name)
+ if err == nil {
+ *i = image{
+ Name: name,
+ }
+
+ return nil
+ }
+
+ // if we got a yaml type error, try to unmarshal it as the struct
+ if _, ok := err.(*yaml.TypeError); ok {
+ type rawImage image
+ raw := rawImage{}
+
+ if err := unmarshal(&raw); err != nil {
+ return err
+ }
+
+ *i = image(raw)
+
+ return nil
+ }
+
+ return err
+}
+
type step struct {
- Image string `yaml:"image"`
+ Image image `yaml:"image"`
Script []string `yaml:"script"`
Services []string `yaml:"services"`
}
@@ -36,7 +76,7 @@
}
type service struct {
- Image string `yaml:"image"`
+ Image image `yaml:"image"`
Environment []string `yaml:"environment"`
Username string `yaml:"username"`
Password string `yaml:"password"`
@@ -50,12 +90,8 @@
Docker bool `yaml:"docker"`
}
-type clone struct {
- Depth int `yaml:"depth"`
-}
-
type bitbucketPipelines struct {
- Image string `yaml:"image"`
+ Image image `yaml:"image"`
Clone clone `yaml:"clone"`
Pipelines pipelines `yaml:"pipelines"`
Definitions definition `yaml:"definitions"`
@@ -75,6 +111,10 @@
return nil
}
+type clone struct {
+ Depth int `yaml:"depth"`
+}
+
func (c *clone) UnmarshalYAML(unmarshal func(interface{}) error) error {
type rawClone clone
raw := rawClone{Depth: 50}
--- a/bitbucket/unmarshal_test.go Sat Jul 22 10:45:04 2017 -0500
+++ b/bitbucket/unmarshal_test.go Sat Jul 22 11:43:01 2017 -0500
@@ -43,7 +43,9 @@
Expect(err).To(BeNil())
expected := bitbucketPipelines{
- Image: "python:3",
+ Image: image{
+ Name: "python:3",
+ },
Pipelines: pipelines{
Default: []pipeline{
pipeline{
@@ -91,7 +93,9 @@
Expect(err).To(BeNil())
expected := bitbucketPipelines{
- Image: "pidgin/builder-debian:stretch",
+ Image: image{
+ Name: "pidgin/builder-debian:stretch",
+ },
Pipelines: pipelines{
Default: []pipeline{
pipeline{
@@ -109,7 +113,9 @@
"release-2.x.y": []pipeline{
pipeline{
Steps: step{
- Image: "pidgin/release-builder:release-2.x.y",
+ Image: image{
+ Name: "pidgin/release-builder:release-2.x.y",
+ },
Script: []string{
"set -ex",
"./autogen.sh --enable-debug",
@@ -163,3 +169,77 @@
}
Expect(actual).To(Equal(expected))
}
+
+func (b *bitbucketSuite) TestUnmarshalComplexGlobalImage(t *testing.T) {
+ yamlData := `pipelines:
+image:
+ name: foobar
+pipelines:
+ default:
+ - step:
+ script:
+ - true
+ `
+ var actual bitbucketPipelines
+ err := yaml.Unmarshal([]byte(yamlData), &actual)
+
+ Expect(err).To(BeNil())
+
+ expected := bitbucketPipelines{
+ Image: image{
+ Name: "foobar",
+ },
+ Pipelines: pipelines{
+ Default: []pipeline{
+ pipeline{
+ Steps: step{
+ Script: []string{
+ "true",
+ },
+ },
+ },
+ },
+ },
+ }
+ Expect(actual).To(Equal(expected))
+}
+
+func (b *bitbucketSuite) TestUnmarshalComplexStepImage(t *testing.T) {
+ yamlData := `pipelines:
+pipelines:
+ default:
+ - step:
+ image:
+ name: name
+ username: user
+ password: pass
+ email: test@example.com
+ script:
+ - true
+ `
+ var actual bitbucketPipelines
+ err := yaml.Unmarshal([]byte(yamlData), &actual)
+
+ Expect(err).To(BeNil())
+
+ expected := bitbucketPipelines{
+ Pipelines: pipelines{
+ Default: []pipeline{
+ pipeline{
+ Steps: step{
+ Image: image{
+ Name: "name",
+ Username: "user",
+ Password: "pass",
+ EMail: "test@example.com",
+ },
+ Script: []string{
+ "true",
+ },
+ },
+ },
+ },
+ },
+ }
+ Expect(actual).To(Equal(expected))
+}