Skip to content

Commit

Permalink
implement apply and request for github
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmeister committed Nov 13, 2021
1 parent 502a0fc commit df5141a
Show file tree
Hide file tree
Showing 13 changed files with 424 additions and 123 deletions.
24 changes: 22 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,54 @@ go 1.17
require (
github.com/blang/semver/v4 v4.0.0
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/go-git/go-git/v5 v5.4.2
github.com/google/go-cmp v0.5.6
github.com/google/go-github/v40 v40.0.0
github.com/heroku/docker-registry-client v0.0.0-20211012143308-9463674c8930
github.com/mitchellh/mapstructure v1.4.2
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.7.0
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f
golang.org/x/sys v0.0.0-20211111213525-f221eed1c01e // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

require (
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.64.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
67 changes: 67 additions & 0 deletions go.sum

Large diffs are not rendered by default.

33 changes: 0 additions & 33 deletions internal/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ 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 Down Expand Up @@ -75,14 +69,12 @@ 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 @@ -94,28 +86,3 @@ 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: 0 additions & 9 deletions internal/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ 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 @@ -35,12 +32,6 @@ actions:
Timestamp: ts,
},
},
Actions: []CacheAction{
{
Identifier: "identifier",
Timestamp: ts,
},
},
}
if !c1.Equal(c2) {
t.Errorf("expected %v, got %v", c2, c1)
Expand Down
100 changes: 100 additions & 0 deletions internal/change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package internal

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"path/filepath"
"strings"

"gopkg.in/yaml.v3"
)

type Change struct {
RegistryName string
ResourceName string
OldVersion string
NewVersion string
File string
Trace yamlTrace
OldValue string
NewValue string
}

type Changes []Change

func (c Change) Identifier() string {
return c.File + "#" + c.Trace.ToString() + "#" + c.NewValue
}

func (c Change) Hash() []byte {
identifier := c.Identifier()
hash := sha256.Sum256([]byte(identifier))
return hash[:]
}

func (cs Changes) Hash() []byte {
temp := []byte{}
for _, c := range cs {
temp = append(temp, c.Hash()...)
}
hash := sha256.Sum256(temp)
return hash[:]
}

func (cs Changes) Branch(prefix string) string {
return prefix + "/" + hex.EncodeToString(cs.Hash()[:8])
}

func (c Change) Message() string {
return fmt.Sprintf("Update %s:%s from %s to %s", c.File, c.Trace.ToString(), c.OldValue, c.NewValue)
}

func (cs Changes) Message() string {
lines := []string{}
if len(cs) > 1 {
lines = append(lines, "Update", "")
}
for _, c := range cs {
lines = append(lines, c.Message())
}
return strings.Join(lines, "\n")
}

func (c Change) Apply(dir string, fileHooks ...func(file string) error) error {
file := filepath.Join(dir, c.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(c.Trace) {
node.Value = c.NewValue
}
return nil
})

err = fileWriteYaml(file, &doc)
if err != nil {
return err
}
for _, hook := range fileHooks {
err := hook(c.File)
if err != nil {
return err
}
}
return nil
}

func (cs Changes) Apply(dir string, fileHooks ...func(file string) error) error {
for _, c := range cs {
err := c.Apply(dir, fileHooks...)
if err != nil {
return err
}
}
return nil
}
35 changes: 35 additions & 0 deletions internal/change_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package internal

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestChangeIdentifier(t *testing.T) {
c := Change{
RegistryName: "my-registry",
ResourceName: "my-resource",
OldVersion: "1.0.0",
NewVersion: "2.0.0",
OldValue: "my-image:1.0.0",
NewValue: "my-image:2.0.0",
File: "folder/file.yaml",
Trace: []interface{}{"spec", 0, "image"},
}
assert.Equal(t, "folder/file.yaml#spec.0.image#my-image:2.0.0", c.Identifier())
}

func TestChangesBranch(t *testing.T) {
cs := Changes{Change{
RegistryName: "my-registry",
ResourceName: "my-resource",
OldVersion: "1.0.0",
NewVersion: "2.0.0",
OldValue: "my-image:1.0.0",
NewValue: "my-image:2.0.0",
File: "folder/file.yaml",
Trace: []interface{}{"spec", 0, "image"},
}}
assert.Equal(t, "git-ops-update/b192fbd55a666b30", cs.Branch("git-ops-update"))
}
23 changes: 20 additions & 3 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ type RawConfigGitGitHub struct {
AccessToken string `mapstructure:"accessToken"`
}

type RawConfigGitAuthor struct {
Name string `mapstructure:"name"`
Email string `mapstructure:"email"`
}

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

Expand Down Expand Up @@ -118,6 +124,9 @@ func LoadConfig(viperInst viper.Viper) (*Config, error) {

registries := map[string]Registry{}
for rn, r := range config.Registries {
if !validateName(rn) {
return nil, fmt.Errorf("registry name %s is invalid", rn)
}
if r.Docker != nil {
registries[rn] = DockerRegistry{
Interval: r.Interval,
Expand All @@ -143,6 +152,9 @@ func LoadConfig(viperInst viper.Viper) (*Config, error) {

policies := map[string]Policy{}
for pn, p := range config.Policies {
if !validateName(pn) {
return nil, fmt.Errorf("policy name %s is invalid", pn)
}
extracts := []Extract{}
for ei, e := range p.Extracts {
if e.Lexicographic != nil {
Expand Down Expand Up @@ -181,14 +193,19 @@ func LoadConfig(viperInst viper.Viper) (*Config, error) {
}

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

return &Config{
Expand Down
6 changes: 4 additions & 2 deletions internal/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ func TestLoadConfig(t *testing.T) {
},
Git: Git{
Provider: GitHubGitProvider{
Owner: "owner",
Repo: "repo",
Author: GitAuthor{
Name: "name",
Email: "email",
},
AccessToken: "access_token",
},
},
Expand Down
5 changes: 3 additions & 2 deletions internal/config_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ policies:
pinPatch: yes
allowPrereleases: yes
git:
author:
name: name
email: email
gitHub:
owner: owner
repo: repo
accessToken: access_token
Loading

0 comments on commit df5141a

Please sign in to comment.