Skip to content

Commit

Permalink
chore: Upgrade yaml v3 (#4172)
Browse files Browse the repository at this point in the history
* fix(deps): update module gopkg.in/yaml.v2 to v3 in go.mod

* chore: Upgrade to go-yaml v3

* Fix comment

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: PePe Amengual <[email protected]>
  • Loading branch information
3 people authored Jan 25, 2024
1 parent fe32f86 commit 34ef59d
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"

"github.com/runatlantis/atlantis/server"
"github.com/runatlantis/atlantis/server/events/vcs/testdata"
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ require (
go.uber.org/zap v1.26.0
golang.org/x/term v0.16.0
golang.org/x/text v0.14.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand Down Expand Up @@ -132,5 +132,4 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,6 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
20 changes: 16 additions & 4 deletions server/core/config/parser_validator.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package config

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -12,7 +14,7 @@ import (
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/runatlantis/atlantis/server/core/config/valid"
yaml "gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v3"
)

// ParserValidator parses and validates server-side repo config files and
Expand Down Expand Up @@ -58,7 +60,12 @@ func (p *ParserValidator) ParseRepoCfg(absRepoDir string, globalCfg valid.Global

func (p *ParserValidator) ParseRepoCfgData(repoCfgData []byte, globalCfg valid.GlobalCfg, repoID string, branch string) (valid.RepoCfg, error) {
var rawConfig raw.RepoCfg
if err := yaml.UnmarshalStrict(repoCfgData, &rawConfig); err != nil {

decoder := yaml.NewDecoder(bytes.NewReader(repoCfgData))
decoder.KnownFields(true)

err := decoder.Decode(&rawConfig)
if err != nil && !errors.Is(err, io.EOF) {
return valid.RepoCfg{}, err
}

Expand Down Expand Up @@ -98,7 +105,7 @@ func (p *ParserValidator) ParseRepoCfgData(repoCfgData []byte, globalCfg valid.G
}
}

err := globalCfg.ValidateRepoCfg(validConfig, repoID)
err = globalCfg.ValidateRepoCfg(validConfig, repoID)
return validConfig, err
}

Expand All @@ -115,7 +122,12 @@ func (p *ParserValidator) ParseGlobalCfg(configFile string, defaultCfg valid.Glo
}

var rawCfg raw.GlobalCfg
if err := yaml.UnmarshalStrict(configData, &rawCfg); err != nil {

decoder := yaml.NewDecoder(bytes.NewReader(configData))
decoder.KnownFields(true)

err = decoder.Decode(&rawCfg)
if err != nil && !errors.Is(err, io.EOF) {
return valid.GlobalCfg{}, err
}

Expand Down
6 changes: 3 additions & 3 deletions server/core/config/parser_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ projects:`,
input: `
version: 3
projects:
- `,
- {}`,
expErr: "projects: (0: (dir: cannot be blank.).).",
},
{
Expand Down Expand Up @@ -625,7 +625,7 @@ projects:
input: `
version: 3
projects:
-`,
- {}`,
expErr: "projects: (0: (dir: cannot be blank.).).",
},
{
Expand All @@ -634,7 +634,7 @@ projects:
version: 3
projects:
- dir: "."
-`,
- {}`,
expErr: "projects: (1: (dir: cannot be blank.).).",
},
{
Expand Down
3 changes: 1 addition & 2 deletions server/core/config/raw/autodiscover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/runatlantis/atlantis/server/core/config/valid"
. "github.com/runatlantis/atlantis/testing"
yaml "gopkg.in/yaml.v2"
)

func TestAutoDiscover_UnmarshalYAML(t *testing.T) {
Expand Down Expand Up @@ -37,7 +36,7 @@ mode: enabled
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
var a raw.AutoDiscover
err := yaml.UnmarshalStrict([]byte(c.input), &a)
err := unmarshalString(c.input, &a)
Ok(t, err)
Equals(t, c.exp, a)
})
Expand Down
5 changes: 2 additions & 3 deletions server/core/config/raw/autoplan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/runatlantis/atlantis/server/core/config/valid"
. "github.com/runatlantis/atlantis/testing"
yaml "gopkg.in/yaml.v2"
)

func TestAutoPlan_UnmarshalYAML(t *testing.T) {
Expand Down Expand Up @@ -50,7 +49,7 @@ when_modified: ["something-else"]
input: `
enabled: false
when_modified:
-
- ""
`,
exp: raw.Autoplan{
Enabled: Bool(false),
Expand All @@ -62,7 +61,7 @@ when_modified:
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
var a raw.Autoplan
err := yaml.UnmarshalStrict([]byte(c.input), &a)
err := unmarshalString(c.input, &a)
Ok(t, err)
Equals(t, c.exp, a)
})
Expand Down
3 changes: 1 addition & 2 deletions server/core/config/raw/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)

func TestMetrics_Unmarshal(t *testing.T) {
Expand All @@ -22,7 +21,7 @@ prometheus:

var result raw.Metrics

err := yaml.UnmarshalStrict([]byte(rawYaml), &result)
err := unmarshalString(rawYaml, &result)
assert.NoError(t, err)
})

Expand Down
6 changes: 3 additions & 3 deletions server/core/config/raw/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/runatlantis/atlantis/server/core/config/valid"
. "github.com/runatlantis/atlantis/testing"
yaml "gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v3"
)

func TestPolicySetsConfig_YAMLMarshalling(t *testing.T) {
Expand Down Expand Up @@ -42,7 +42,7 @@ policy_sets:
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
var got raw.PolicySets
err := yaml.UnmarshalStrict([]byte(c.input), &got)
err := unmarshalString(c.input, &got)
if c.expErr != "" {
ErrEquals(t, c.expErr, err)
return
Expand All @@ -54,7 +54,7 @@ policy_sets:
Ok(t, err)

var got2 raw.PolicySets
err = yaml.UnmarshalStrict([]byte(c.input), &got2)
err = unmarshalString(c.input, &got2)
Ok(t, err)
Equals(t, got2, got)
})
Expand Down
3 changes: 1 addition & 2 deletions server/core/config/raw/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/runatlantis/atlantis/server/core/config/valid"
. "github.com/runatlantis/atlantis/testing"
yaml "gopkg.in/yaml.v2"
)

func TestProject_UnmarshalYAML(t *testing.T) {
Expand Down Expand Up @@ -74,7 +73,7 @@ execution_order_group: 10`,
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
var p raw.Project
err := yaml.UnmarshalStrict([]byte(c.input), &p)
err := unmarshalString(c.input, &p)
Ok(t, err)
Equals(t, c.exp, p)
})
Expand Down
20 changes: 20 additions & 0 deletions server/core/config/raw/raw_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package raw_test

import (
"io"
"strings"

"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)

// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool { return &v }
Expand All @@ -11,3 +19,15 @@ func Int(v int) *int { return &v }
// String is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func String(v string) *string { return &v }

// Helper function to unmarshal from strings
func unmarshalString(in string, out interface{}) error {
decoder := yaml.NewDecoder(strings.NewReader(in))
decoder.KnownFields(true)

err := decoder.Decode(out)
if errors.Is(err, io.EOF) {
return nil
}
return err
}
3 changes: 1 addition & 2 deletions server/core/config/raw/repo_cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/runatlantis/atlantis/server/core/config/valid"
. "github.com/runatlantis/atlantis/testing"
yaml "gopkg.in/yaml.v2"
)

func TestConfig_UnmarshalYAML(t *testing.T) {
Expand Down Expand Up @@ -190,7 +189,7 @@ allowed_regexp_prefixes:
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
var conf raw.RepoCfg
err := yaml.UnmarshalStrict([]byte(c.input), &conf)
err := unmarshalString(c.input, &conf)
if c.expErr != "" {
ErrEquals(t, c.expErr, err)
return
Expand Down
3 changes: 1 addition & 2 deletions server/core/config/raw/stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/runatlantis/atlantis/server/core/config/valid"
. "github.com/runatlantis/atlantis/testing"
yaml "gopkg.in/yaml.v2"
)

func TestStage_UnmarshalYAML(t *testing.T) {
Expand Down Expand Up @@ -41,7 +40,7 @@ steps: [step1]
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
var a raw.Stage
err := yaml.UnmarshalStrict([]byte(c.input), &a)
err := unmarshalString(c.input, &a)
Ok(t, err)
Equals(t, c.exp, a)
})
Expand Down
6 changes: 3 additions & 3 deletions server/core/config/raw/step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/runatlantis/atlantis/server/core/config/valid"
. "github.com/runatlantis/atlantis/testing"
yaml "gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v3"
)

func TestStepConfig_YAMLMarshalling(t *testing.T) {
Expand Down Expand Up @@ -155,7 +155,7 @@ key:
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
var got raw.Step
err := yaml.UnmarshalStrict([]byte(c.input), &got)
err := unmarshalString(c.input, &got)
if c.expErr != "" {
ErrEquals(t, c.expErr, err)
return
Expand All @@ -167,7 +167,7 @@ key:
Ok(t, err)

var got2 raw.Step
err = yaml.UnmarshalStrict([]byte(c.input), &got2)
err = unmarshalString(c.input, &got2)
Ok(t, err)
Equals(t, got2, got)
})
Expand Down
6 changes: 3 additions & 3 deletions server/core/config/raw/workflow_step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/runatlantis/atlantis/server/core/config/valid"
. "github.com/runatlantis/atlantis/testing"
yaml "gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v3"
)

func TestWorkflowHook_YAMLMarshalling(t *testing.T) {
Expand Down Expand Up @@ -54,7 +54,7 @@ key:
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
var got raw.WorkflowHook
err := yaml.UnmarshalStrict([]byte(c.input), &got)
err := unmarshalString(c.input, &got)
if c.expErr != "" {
ErrEquals(t, c.expErr, err)
return
Expand All @@ -66,7 +66,7 @@ key:
Ok(t, err)

var got2 raw.WorkflowHook
err = yaml.UnmarshalStrict([]byte(c.input), &got2)
err = unmarshalString(c.input, &got2)
Ok(t, err)
Equals(t, got2, got)
})
Expand Down
3 changes: 1 addition & 2 deletions server/core/config/raw/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/runatlantis/atlantis/server/core/config/raw"
"github.com/runatlantis/atlantis/server/core/config/valid"
. "github.com/runatlantis/atlantis/testing"
yaml "gopkg.in/yaml.v2"
)

func TestWorkflow_UnmarshalYAML(t *testing.T) {
Expand Down Expand Up @@ -106,7 +105,7 @@ apply:
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
var w raw.Workflow
err := yaml.UnmarshalStrict([]byte(c.input), &w)
err := unmarshalString(c.input, &w)
if c.expErr != "" {
ErrEquals(t, c.expErr, err)
return
Expand Down

0 comments on commit 34ef59d

Please sign in to comment.