From 3741ad857fe45974b07646786502cd512069345b Mon Sep 17 00:00:00 2001 From: Jack Lindamood Date: Fri, 6 Jan 2023 10:45:25 -0800 Subject: [PATCH] fix: allow parsing more configs (#25) Resolves #24 --- README.md | 8 ++++---- internal/atlantis/config.go | 29 +++++++++++++---------------- internal/atlantis/config_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 8efe3291..bae2279a 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ The general workflow of this repository is: 2. Find an atlantis.yaml file inside the repository 3. Use atlantis to run /plan on each project in the atlantis.yaml file 4. For each project with drift - 1. Trigger a github workflow that can resolve the drift - 2. Comment the existance of the drift in slack -5. For each project directory in the atlantis.yamnl + 1. Trigger a GitHub workflow that can resolve the drift + 2. Comment the existence of the drift in slack +5. For each project directory in the atlantis.yaml 1. Run workspace list 2. If any workspace isn't tracked by atlantis, notify slack -There is an optional flag to cache drift results inside DynamoDB so we don't check the same directory twice in a short period of time. +There is an optional flag to cache drift results inside DynamoDB, so we don't check the same directory twice in a short period of time. # Example for "Trigger a github workflow that can resolve the drift" diff --git a/internal/atlantis/config.go b/internal/atlantis/config.go index 9a37b66a..b83ff327 100644 --- a/internal/atlantis/config.go +++ b/internal/atlantis/config.go @@ -4,9 +4,11 @@ import ( "fmt" "github.com/runatlantis/atlantis/server/core/config" "github.com/runatlantis/atlantis/server/core/config/valid" + "gopkg.in/yaml.v3" "os" "path/filepath" "sort" + "strings" ) type DirectoriesWithWorkspaces map[string][]string @@ -20,7 +22,7 @@ func (d DirectoriesWithWorkspaces) SortedKeys() []string { return keys } -func ConfigToWorkspaces(cfg *valid.RepoCfg) DirectoriesWithWorkspaces { +func ConfigToWorkspaces(cfg *SimpleAtlantisConfig) DirectoriesWithWorkspaces { workspaces := make(DirectoriesWithWorkspaces) for _, p := range cfg.Projects { if _, exists := workspaces[p.Dir]; !exists { @@ -31,25 +33,20 @@ func ConfigToWorkspaces(cfg *valid.RepoCfg) DirectoriesWithWorkspaces { return workspaces } -func ParseRepoConfig(body string) (*valid.RepoCfg, error) { - t := true - var pv config.ParserValidator - vg := valid.GlobalCfg{ - Repos: []valid.Repo{ - { - ID: "terraform", - AllowCustomWorkflows: &t, - }, - }, - } - vc, err := pv.ParseRepoCfgData([]byte(body), vg, "terraform") - if err != nil { +type SimpleAtlantisConfig struct { + Version int + Projects []valid.Project +} + +func ParseRepoConfig(body string) (*SimpleAtlantisConfig, error) { + var ret SimpleAtlantisConfig + if err := yaml.NewDecoder(strings.NewReader(body)).Decode(&ret); err != nil { return nil, fmt.Errorf("error parsing config: %s", err) } - return &vc, nil + return &ret, nil } -func ParseRepoConfigFromDir(dir string) (*valid.RepoCfg, error) { +func ParseRepoConfigFromDir(dir string) (*SimpleAtlantisConfig, error) { filename := filepath.Join(dir, config.AtlantisYAMLFilename) body, err := os.ReadFile(filename) if err != nil { diff --git a/internal/atlantis/config_test.go b/internal/atlantis/config_test.go index e4cb67df..5b22a1ed 100644 --- a/internal/atlantis/config_test.go +++ b/internal/atlantis/config_test.go @@ -26,6 +26,35 @@ projects: - '*.tf' ` +const exampleFromGithubIssue = `version: 3 +automerge: true +delete_source_branch_on_merge: true +parallel_plan: true +parallel_apply: true +allowed_regexp_prefixes: +- lab/ +- staging/ +- prod/ +projects: +- name: pepe-ue2-lab-cloudtrail + workspace: pepe-ue2-lab + workflow: workflow-1 + dir: components/terraform/cloudtrail + terraform_version: v1.2.9 + delete_source_branch_on_merge: false + autoplan: + enabled: true + when_modified: + - '**/*.tf' + - $PROJECT_NAME.tfvars.json + apply_requirements: + - approved` + +func TestParseRepoConfig(t *testing.T) { + _, err := ParseRepoConfig(exampleFromGithubIssue) + require.NoError(t, err) +} + func TestParseRepoConfigFromDir(t *testing.T) { dirName, err := os.MkdirTemp("", "config-test") require.NoError(t, err)