Skip to content

Commit

Permalink
fix: allow parsing more configs (#25)
Browse files Browse the repository at this point in the history
Resolves #24
  • Loading branch information
Jack Lindamood authored Jan 6, 2023
1 parent 52efcd5 commit 3741ad8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
29 changes: 13 additions & 16 deletions internal/atlantis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions internal/atlantis/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3741ad8

Please sign in to comment.