From eb6570ab7d9a6a46919568d4c5896147eb66d650 Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Tue, 14 Jul 2020 16:06:20 +0200 Subject: [PATCH] Use fork of ghodss/yaml to use 'on' as key in CampaignSpec (#12153) As we noticed last week: we can't use `on` as a key in a `CampaignSpec` because `yaml.YAMLToJSON` would convert that `on` into a `true` and that in turn would fail when validated. The solution here is to use a custom fork of `ghodss/yaml` that allows passing in a custom unmarshal-function which does _not_ do the conversion. The function we're passing in comes from the yaml.v3 library which changed its behavior when parsing boolean values (see https://github.com/ghodss/yaml/pull/65). We lose the ability to use `YAMLToJSONStrict` since that only works with yaml.v2, but yaml.v3 already warns about duplicated keys and the JSON schema validation gives us enough of a safety net for the rest. --- go.mod | 5 ++++- go.sum | 2 ++ internal/campaigns/types.go | 4 +++- internal/campaigns/types_test.go | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index b6392efbdf99..dc0502244162 100644 --- a/go.mod +++ b/go.mod @@ -179,7 +179,7 @@ require ( gopkg.in/square/go-jose.v2 v2.5.1 // indirect gopkg.in/src-d/go-git.v4 v4.13.1 gopkg.in/yaml.v2 v2.3.0 - gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect + gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 honnef.co/go/tools v0.0.1-2020.1.4 // indirect ) @@ -206,3 +206,6 @@ replace github.com/russross/blackfriday => github.com/russross/blackfriday v1.5. replace github.com/dghubble/gologin => github.com/sourcegraph/gologin v1.0.2-0.20181110030308-c6f1b62954d8 replace github.com/golang/lint => golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f + +// See: https://github.com/ghodss/yaml/pull/65 +replace github.com/ghodss/yaml => github.com/sourcegraph/yaml v1.0.1-0.20200714132230-56936252f152 diff --git a/go.sum b/go.sum index da82e4ad97e6..3131a0be9371 100644 --- a/go.sum +++ b/go.sum @@ -1207,6 +1207,8 @@ github.com/sourcegraph/jsonx v0.0.0-20200625022044-c22a595bbad7 h1:G58fa9uWw59V5 github.com/sourcegraph/jsonx v0.0.0-20200625022044-c22a595bbad7/go.mod h1:7jkSQ2sdxwXMaIDxKJotTt+hwKnT9b/wbJFU7/ObUEY= github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e h1:qpG93cPwA5f7s/ZPBJnGOYQNK/vKsaDaseuKT5Asee8= github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA= +github.com/sourcegraph/yaml v1.0.1-0.20200714132230-56936252f152 h1:z/MpntplPaW6QW95pzcAR/72Z5TWDyDnSo0EOcyij9o= +github.com/sourcegraph/yaml v1.0.1-0.20200714132230-56936252f152/go.mod h1:GIjDIg/heH5DOkXY3YJ/wNhfHsQHoXGjl8G8amsYQ1I= github.com/sourcegraph/zoekt v0.0.0-20200714221025-dea986a54d01 h1:Qp+BmPgBkC4e0J5hAXS+UpdoPhQTkCrxYxkoyrxQ1nI= github.com/sourcegraph/zoekt v0.0.0-20200714221025-dea986a54d01/go.mod h1:WleTVLMEfvGF6uZ/mSWXVUH1H4NPxAcu6YbJ0TORdWc= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ= diff --git a/internal/campaigns/types.go b/internal/campaigns/types.go index d8ee6120f8c5..5c238559c0a9 100644 --- a/internal/campaigns/types.go +++ b/internal/campaigns/types.go @@ -25,6 +25,8 @@ import ( "github.com/sourcegraph/sourcegraph/internal/vcs/git" "github.com/sourcegraph/sourcegraph/schema" "github.com/xeipuuv/gojsonschema" + + yamlv3 "gopkg.in/yaml.v3" ) // SupportedExternalServices are the external service types currently supported @@ -1675,7 +1677,7 @@ func (cs *CampaignSpec) UnmarshalValidate() error { return errors.Wrap(err, "failed to compile CampaignSpec JSON schema") } - normalized, err := yaml.YAMLToJSON([]byte(cs.RawSpec)) + normalized, err := yaml.YAMLToJSONCustom([]byte(cs.RawSpec), yamlv3.Unmarshal) if err != nil { return errors.Wrapf(err, "failed to normalize JSON") } diff --git a/internal/campaigns/types_test.go b/internal/campaigns/types_test.go index 55131750932a..4b41a3b66d25 100644 --- a/internal/campaigns/types_test.go +++ b/internal/campaigns/types_test.go @@ -1120,7 +1120,7 @@ func TestCampaignSpecUnmarshalValidate(t *testing.T) { rawSpec: ` name: my-unique-name description: My description -'on': +on: - repositoriesMatchingQuery: lang:go func main - repository: github.com/sourcegraph/src-cli steps: