Skip to content

Commit

Permalink
prepare for github
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmeister committed Nov 12, 2021
1 parent 3f8d8ed commit d4bdc08
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 60 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: test
on:
push:
branches:
- main
pull_request:
branches:
- main
Expand Down
42 changes: 10 additions & 32 deletions internal/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,18 @@ import (
"gopkg.in/yaml.v3"
)

func visitAnnotationsRecursion(trace []string, node *yaml.Node, identifier string, fn func(keyNode *yaml.Node, valueNode *yaml.Node, trace []string, annotation string) error) error {
if node.Kind == yaml.MappingNode {
for i := 0; i < len(node.Content); i += 2 {
keyNode := node.Content[i]
valueNode := node.Content[i+1]
if valueNode.Kind == yaml.ScalarNode {
annotationStr := strings.TrimPrefix(valueNode.LineComment, "#")
annotationJson := map[string]interface{}{}
utiljson.Unmarshal([]byte(annotationStr), &annotationJson)
annotation, ok := annotationJson["$"+identifier].(string)
if ok {
err := fn(keyNode, valueNode, append(trace, keyNode.Value), annotation)
if err != nil {
return err
}
}
} else {
err := visitAnnotationsRecursion(append(trace, keyNode.Value), valueNode, identifier, fn)
if err != nil {
return err
}
}
}
} else {
for _, child := range node.Content {
err := visitAnnotationsRecursion(trace, child, identifier, fn)
func VisitAnnotations(node *yaml.Node, identifier string, fn func(trace yamlTrace, yamlNode *yaml.Node, annotation string) error) error {
return VisitYaml(node, func(trace yamlTrace, yamlNode *yaml.Node) error {
annotationStr := strings.TrimPrefix(yamlNode.LineComment, "#")
annotationJson := map[string]interface{}{}
utiljson.Unmarshal([]byte(annotationStr), &annotationJson)
annotation, ok := annotationJson["$"+identifier].(string)
if ok {
err := fn(trace, yamlNode, annotation)
if err != nil {
return err
}
}
}
return nil
}

func VisitAnnotations(node *yaml.Node, identifier string, fn func(keyNode *yaml.Node, valueNode *yaml.Node, trace []string, annotation string) error) error {
return visitAnnotationsRecursion([]string{}, node, identifier, fn)
return nil
})
}
2 changes: 1 addition & 1 deletion internal/annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ foo:
doc := &yaml.Node{}
err := readYaml([]byte(input), doc)
if assert.NoError(t, err) {
err = VisitAnnotations(doc, "append", func(keyNode *yaml.Node, valueNode *yaml.Node, trace []string, annotation string) error {
err = VisitAnnotations(doc, "append", func(trace yamlTrace, valueNode *yaml.Node, annotation string) error {
valueNode.Value = valueNode.Value + annotation
return nil
})
Expand Down
35 changes: 34 additions & 1 deletion internal/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ type CacheResource struct {
Timestamp time.Time `yaml:"timestamp"`
}

type CacheAction struct {
Identifier string `yaml:"identifier"`
Timestamp time.Time `yaml:"timestamp"`
}

type Cache struct {
Resources []CacheResource `yaml:"resources"`
Actions []CacheAction `yaml:"actions"`
}

func LoadCache(bytes []byte) (*Cache, error) {
Expand All @@ -38,7 +44,7 @@ func SaveCache(cache Cache) (*[]byte, error) {
func LoadCacheFromFile(file string) (*Cache, error) {
cacheRaw, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
return &Cache{}, nil
}
cache, err := LoadCache(cacheRaw)
if err != nil {
Expand Down Expand Up @@ -69,12 +75,14 @@ func (c Cache) UpdateResource(r CacheResource) Cache {
if r2.RegistryName == r.RegistryName && r2.ResourceName == r.ResourceName {
return Cache{
Resources: append(c.Resources[:i], append([]CacheResource{r}, c.Resources[i+1:]...)...),
Actions: c.Actions,
}
}
}

return Cache{
Resources: append(c.Resources, r),
Actions: c.Actions,
}
}

Expand All @@ -86,3 +94,28 @@ func (c Cache) FindResource(registryName string, resourceName string) *CacheReso
}
return nil
}

func (c Cache) AddAction(identifier string) Cache {
for i, a := range c.Actions {
if a.Identifier == identifier {
return Cache{
Resources: c.Resources,
Actions: append(c.Actions[:i], append([]CacheAction{CacheAction{Identifier: identifier, Timestamp: time.Now()}}, c.Actions[i+1:]...)...),
}
}
}

return Cache{
Resources: c.Resources,
Actions: append(c.Actions, CacheAction{Identifier: identifier, Timestamp: time.Now()}),
}
}

func (c Cache) HasAction(identifier string) bool {
for _, a := range c.Actions {
if a.Identifier == identifier {
return true
}
}
return false
}
9 changes: 9 additions & 0 deletions internal/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func TestCache(t *testing.T) {
- "21.04"
- "21.10"
timestamp: 2006-01-02T15:04:05Z
actions:
- identifier: identifier
timestamp: 2006-01-02T15:04:05Z
`

c1, err := LoadCache([]byte(yaml))
Expand All @@ -32,6 +35,12 @@ func TestCache(t *testing.T) {
Timestamp: ts,
},
},
Actions: []CacheAction{
{
Identifier: "identifier",
Timestamp: ts,
},
},
}
if !c1.Equal(c2) {
t.Errorf("expected %v, got %v", c2, c1)
Expand Down
24 changes: 24 additions & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,21 @@ type RawConfigPolicy struct {
Extracts []RawConfigPolicyExtract `mapstructure:"extracts"`
}

type RawConfigGitGitHub struct {
Owner string `mapstructure:"owner"`
Repo string `mapstructure:"repo"`
AccessToken string `mapstructure:"accessToken"`
}

type RawConfigGit struct {
GitHub *RawConfigGitGitHub `mapstructure:"gitHub"`
}

type RawConfig struct {
Files RawConfigFiles `mapstructure:"files"`
Registries map[string]RawConfigRegistry `mapstructure:"registries"`
Policies map[string]RawConfigPolicy `mapstructure:"policies"`
Git RawConfigGit `mapstructure:"git"`
}

type ConfigFiles struct {
Expand All @@ -77,6 +88,7 @@ type Config struct {
Files ConfigFiles
Registries map[string]Registry
Policies map[string]Policy
Git Git
}

func LoadConfig(viperInst viper.Viper) (*Config, error) {
Expand Down Expand Up @@ -168,12 +180,24 @@ func LoadConfig(viperInst viper.Viper) (*Config, error) {
}
}

git := Git{}
if config.Git.GitHub != nil {
git.Provider = GitHubGitProvider{
Owner: config.Git.GitHub.Owner,
Repo: config.Git.GitHub.Repo,
AccessToken: config.Git.GitHub.AccessToken,
}
} else {
git.Provider = LocalGitProvider{}
}

return &Config{
Files: ConfigFiles{
Includes: fileIncludes,
Excludes: fileExcludes,
},
Registries: registries,
Policies: policies,
Git: git,
}, nil
}
8 changes: 8 additions & 0 deletions internal/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,17 @@ func TestLoadConfig(t *testing.T) {
},
},
},
Git: Git{
Provider: GitHubGitProvider{
Owner: "owner",
Repo: "repo",
AccessToken: "access_token",
},
},
}

assert.Equal(t, c2.Files, c1.Files)
assert.Equal(t, c2.Registries, c1.Registries)
assert.Equal(t, c2.Policies, c1.Policies)
assert.Equal(t, c2.Git, c1.Git)
}
5 changes: 5 additions & 0 deletions internal/config_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ policies:
pinMinor: yes
pinPatch: yes
allowPrereleases: yes
git:
gitHub:
owner: owner
repo: repo
accessToken: access_token
74 changes: 74 additions & 0 deletions internal/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package internal

import (
"fmt"
"path/filepath"

"gopkg.in/yaml.v3"
)

type Change struct {
RegistryName string
ResourceName string
OldVersion string
NewVersion string
File string
Trace []interface{}
OldValue string
NewValue string
}

type Git struct {
Provider GitProvider
}

type GitProvider interface {
Apply(dir string, changes []Change) error
Request(dir string, changes []Change) error
}

type LocalGitProvider struct{}

type GitHubGitProvider struct {
Owner string
Repo string
AccessToken string
}

func (p LocalGitProvider) Apply(dir string, changes []Change) error {
for _, change := range changes {
file := filepath.Join(dir, change.File)
doc := yaml.Node{}
err := fileReadYaml(file, &doc)
if err != nil {
return err
}

VisitYaml(&doc, func(trace yamlTrace, node *yaml.Node) error {
if trace.Equal(change.Trace) {
node.Value = change.NewValue
}
return nil
})

err = fileWriteYaml(file, &doc)
if err != nil {
return err
}
}
return nil
}

func (p LocalGitProvider) Request(dir string, changes []Change) error {
return fmt.Errorf("local git provider does not support request")
}

func (p GitHubGitProvider) Apply(dir string, changes []Change) error {
// TODO implement making a commit and pushing it to github repository default branch
return fmt.Errorf("gitHub git provider apply is not yet implemented")
}

func (p GitHubGitProvider) Request(dir string, changes []Change) error {
// TODO implement making a commit to a new branch and creating a pull request on github repository against default branch
return fmt.Errorf("gitHub git provider apply is not yet implemented")
}
Loading

0 comments on commit d4bdc08

Please sign in to comment.