Skip to content

Commit

Permalink
Lockfile and manifest update specific settings (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert authored Oct 1, 2019
1 parent 724e979 commit 68f4540
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 39 deletions.
58 changes: 32 additions & 26 deletions internal/config/config_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
"strings"

"github.com/dropseed/deps/internal/env"
"github.com/dropseed/deps/pkg/schema"
)

// Dependency is a path + type in dependencies.yml
type Dependency struct {
Type string `mapstructure:"type" yaml:"type" json:"type"`
// Version should be a git ref that is checked out/pulled
// Version string `mapstructure:"version,omitempty" yaml:"version,omitempty" json:"version,omitempty"`
Path string `mapstructure:"path,omitempty" yaml:"path,omitempty" json:"path,omitempty"`
Env map[string]string `mapstructure:"env,omitempty" yaml:"env,omitempty" json:"env"`
Settings map[string]interface{} `mapstructure:"settings,omitempty" yaml:"settings,omitempty" json:"settings"`
LockfileUpdates LockfileUpdates `mapstructure:"lockfile_updates,omitempty" yaml:"lockfile_updates,omitempty" json:"lockfile_updates,omitempty"`
ManifestUpdates ManifestUpdates `mapstructure:"manifest_updates,omitempty" yaml:"manifest_updates,omitempty" json:"manifest_updates,omitempty"`
Path string `mapstructure:"path,omitempty" yaml:"path,omitempty" json:"path,omitempty"`
Env map[string]string `mapstructure:"env,omitempty" yaml:"env,omitempty" json:"env"`
Settings Settings `mapstructure:"settings,omitempty" yaml:"settings,omitempty" json:"settings"`
LockfileUpdates LockfileUpdates `mapstructure:"lockfile_updates,omitempty" yaml:"lockfile_updates,omitempty" json:"lockfile_updates,omitempty"`
ManifestUpdates ManifestUpdates `mapstructure:"manifest_updates,omitempty" yaml:"manifest_updates,omitempty" json:"manifest_updates,omitempty"`
}

func (dependency *Dependency) Compile() {
Expand All @@ -28,16 +29,22 @@ func (dependency *Dependency) Compile() {

// set defaults
if dependency.Settings == nil {
dependency.Settings = map[string]interface{}{}
dependency.Settings = Settings{}
}
if dependency.LockfileUpdates.Enabled == nil {
t := true
dependency.LockfileUpdates.Enabled = &t
}
if dependency.LockfileUpdates.Settings == nil {
dependency.LockfileUpdates.Settings = Settings{}
}
if dependency.ManifestUpdates.Enabled == nil {
t := true
dependency.ManifestUpdates.Enabled = &t
}
if dependency.ManifestUpdates.Settings == nil {
dependency.ManifestUpdates.Settings = Settings{}
}

// if no filters then set the default 1
if len(dependency.ManifestUpdates.Filters) == 0 {
Expand All @@ -61,13 +68,7 @@ func (dependency *Dependency) Compile() {
func (dependency *Dependency) Environ() ([]string, error) {
environ := os.Environ()

for k, v := range dependency.Settings {
environString, err := env.SettingToEnviron(k, v)
if err != nil {
return nil, err
}
environ = append(environ, environString)
}
environ = append(environ, dependency.Settings.AsEnviron()...)

for k, v := range dependency.Env {
environ = append(environ, fmt.Sprintf("%s=%s", k, v))
Expand All @@ -76,22 +77,27 @@ func (dependency *Dependency) Environ() ([]string, error) {
return environ, nil
}

func (dependency *Dependency) GetSetting(name string) interface{} {
// Settings can be provided by (in order of priority):
// 1. YAML config
// 2. Env vars
func (dependency *Dependency) GetSettingForSchema(name string, deps *schema.Dependencies) interface{} {
// 1. Env
// 2. Settings
// 3. Lockfile settings (if lockfiles)
// 4. Manifest settings (if manifests)

if dependency.Settings != nil {
for k, v := range dependency.Settings {
if strings.ToLower(k) == strings.ToLower(name) {
return v
}
}
value := env.SettingFromEnviron(name)

if v := dependency.Settings.Get(name); v != nil {
value = v
}

// Lockfile- and Manifest-specific settings take priority over general settings

if v := dependency.LockfileUpdates.Settings.Get(name); v != nil && deps.Lockfiles != nil && len(deps.Lockfiles) > 0 {
value = v
}

if v := env.SettingFromEnviron(name); v != nil {
return v
if v := dependency.ManifestUpdates.Settings.Get(name); v != nil && deps.Manifests != nil && len(deps.Manifests) > 0 {
value = v
}

return nil
return value
}
6 changes: 6 additions & 0 deletions internal/config/config_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ func TestEmptyCompile(t *testing.T) {
if !*dep.LockfileUpdates.Enabled {
t.Error("lockfile updates disabled")
}
if dep.LockfileUpdates.Settings == nil {
t.Error("lockfile settings nil")
}
if !*dep.ManifestUpdates.Enabled {
t.Error("manifest updates disabled")
}
if dep.ManifestUpdates.Settings == nil {
t.Error("manifest settings nil")
}
if len(dep.ManifestUpdates.Filters) != 1 {
t.Error("manifest filters wrong")
}
Expand Down
3 changes: 2 additions & 1 deletion internal/config/config_lockfile_updates.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package config

type LockfileUpdates struct {
Enabled *bool `mapstructure:"enabled,omitempty" yaml:"enabled,omitempty" json:"enabled,omitempty"`
Enabled *bool `mapstructure:"enabled,omitempty" yaml:"enabled,omitempty" json:"enabled,omitempty"`
Settings Settings `mapstructure:"settings,omitempty" yaml:"settings,omitempty" json:"settings"`
}
6 changes: 3 additions & 3 deletions internal/config/config_manifest_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
)

type ManifestUpdates struct {
Enabled *bool `mapstructure:"enabled,omitempty" yaml:"enabled,omitempty" json:"enabled,omitempty"`
Filters []*Filter `mapstructure:"filters,omitempty" yaml:"filters,omitempty" json:"filters,omitempty"`
// ConstraintPrefix string `mapstructure:"constraint_prefix,omitempty" yaml:"constraint_prefix,omitempty" json:"constraint_prefix,omitempty"`
Enabled *bool `mapstructure:"enabled,omitempty" yaml:"enabled,omitempty" json:"enabled,omitempty"`
Settings Settings `mapstructure:"settings,omitempty" yaml:"settings,omitempty" json:"settings"`
Filters []*Filter `mapstructure:"filters,omitempty" yaml:"filters,omitempty" json:"filters,omitempty"`
}

type Filter struct {
Expand Down
33 changes: 33 additions & 0 deletions internal/config/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package config

import (
"strings"

"github.com/dropseed/deps/internal/env"
)

type Settings map[string]interface{}

func (s Settings) Get(name string) interface{} {
for k, v := range s {
if strings.ToLower(k) == strings.ToLower(name) {
return v
}
}

return nil
}

func (s Settings) AsEnviron() []string {
environ := []string{}

for k, v := range s {
environString, err := env.SettingToEnviron(k, v)
if err != nil {
panic(err)
}
environ = append(environ, environString)
}

return environ
}
8 changes: 6 additions & 2 deletions internal/pullrequest/bitbucket/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func NewPullRequest(base string, head string, deps *schema.Dependencies, cfg *co
}, nil
}

func (pr *PullRequest) GetSetting(name string) interface{} {
return pr.Config.GetSettingForSchema(name, pr.Dependencies)
}

func (pr *PullRequest) request(verb string, url string, input []byte) (*http.Response, string, error) {
client := &http.Client{}

Expand Down Expand Up @@ -93,7 +97,7 @@ func (pr *PullRequest) CreateOrUpdate() error {

func (pr *PullRequest) getPullRequestOptions() map[string]interface{} {
base := pr.Base
if target := pr.Config.GetSetting("bitbucket_destination"); target != nil {
if target := pr.GetSetting("bitbucket_destination"); target != nil {
base = target.(string)
}

Expand All @@ -117,7 +121,7 @@ func (pr *PullRequest) getPullRequestOptions() map[string]interface{} {
}

for _, f := range otherFields {
if s := pr.Config.GetSetting(fmt.Sprintf("bitbucket_%s", f)); s != nil {
if s := pr.GetSetting(fmt.Sprintf("bitbucket_%s", f)); s != nil {
pullrequestMap[f] = s
}
}
Expand Down
12 changes: 8 additions & 4 deletions internal/pullrequest/github/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func NewPullRequest(base string, head string, deps *schema.Dependencies, cfg *co
}, nil
}

func (pr *PullRequest) GetSetting(name string) interface{} {
return pr.Config.GetSettingForSchema(name, pr.Dependencies)
}

func (pr *PullRequest) request(verb string, url string, input []byte) (*http.Response, string, error) {
client := &http.Client{}

Expand Down Expand Up @@ -83,7 +87,7 @@ func (pr *PullRequest) pullsURL() string {

func (pr *PullRequest) getCreateJSONData() ([]byte, error) {
base := pr.Base
if override := pr.Config.GetSetting("github_base_branch"); override != nil {
if override := pr.GetSetting("github_base_branch"); override != nil {
base = override.(string)
}

Expand Down Expand Up @@ -160,9 +164,9 @@ func (pr *PullRequest) getExisting() (map[string]interface{}, error) {
func (pr *PullRequest) CreateOrUpdate() error {
// check the optional settings now, before actually creating the PR (which we'll have to update)

labels := pr.Config.GetSetting("github_labels")
assignees := pr.Config.GetSetting("github_assignees")
milestone := pr.Config.GetSetting("github_milestone")
labels := pr.GetSetting("github_labels")
assignees := pr.GetSetting("github_assignees")
milestone := pr.GetSetting("github_milestone")

fmt.Printf("Preparing to open GitHub pull request for %v\n", pr.RepoFullName)

Expand Down
10 changes: 7 additions & 3 deletions internal/pullrequest/gitlab/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func NewMergeRequest(base string, head string, deps *schema.Dependencies, cfg *c
}, nil
}

func (pr *MergeRequest) GetSetting(name string) interface{} {
return pr.Config.GetSettingForSchema(name, pr.Dependencies)
}

func (pr *MergeRequest) request(verb string, url string, input []byte) (*http.Response, string, error) {
client := &http.Client{}

Expand Down Expand Up @@ -125,7 +129,7 @@ func (pr *MergeRequest) update(iid string, data []byte) error {

func (pr *MergeRequest) getMergeRequestOptions() map[string]interface{} {
base := pr.Base
if target := pr.Config.GetSetting("gitlab_target_branch"); target != nil {
if target := pr.GetSetting("gitlab_target_branch"); target != nil {
base = target.(string)
}

Expand All @@ -135,7 +139,7 @@ func (pr *MergeRequest) getMergeRequestOptions() map[string]interface{} {
pullrequestMap["target_branch"] = base
pullrequestMap["description"] = pr.Body

if labels := pr.Config.GetSetting("gitlab_labels"); labels != nil {
if labels := pr.GetSetting("gitlab_labels"); labels != nil {
labelStrings := []string{}
for _, l := range labels.([]interface{}) {
labelStrings = append(labelStrings, l.(string))
Expand All @@ -155,7 +159,7 @@ func (pr *MergeRequest) getMergeRequestOptions() map[string]interface{} {
}

for _, f := range otherFields {
if s := pr.Config.GetSetting(fmt.Sprintf("gitlab_%s", f)); s != nil {
if s := pr.GetSetting(fmt.Sprintf("gitlab_%s", f)); s != nil {
pullrequestMap[f] = s
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/pullrequest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const BITBUCKET = "bitbucket"
// PullrequestAdapter implements the basic Pullrequest functions
type PullrequestAdapter interface {
CreateOrUpdate() error
GetSetting(string) interface{}
}

type RepoAdapter interface {
Expand Down

0 comments on commit 68f4540

Please sign in to comment.