From 68f4540091e0b13b485353aa70ec622f8c1e3656 Mon Sep 17 00:00:00 2001 From: Dave Gaeddert Date: Mon, 30 Sep 2019 22:37:41 -0500 Subject: [PATCH] Lockfile and manifest update specific settings (#9) --- internal/config/config_dependency.go | 58 ++++++++++++---------- internal/config/config_dependency_test.go | 6 +++ internal/config/config_lockfile_updates.go | 3 +- internal/config/config_manifest_updates.go | 6 +-- internal/config/settings.go | 33 ++++++++++++ internal/pullrequest/bitbucket/main.go | 8 ++- internal/pullrequest/github/main.go | 12 +++-- internal/pullrequest/gitlab/main.go | 10 ++-- internal/pullrequest/main.go | 1 + 9 files changed, 98 insertions(+), 39 deletions(-) create mode 100644 internal/config/settings.go diff --git a/internal/config/config_dependency.go b/internal/config/config_dependency.go index d899a8b..b759e0a 100644 --- a/internal/config/config_dependency.go +++ b/internal/config/config_dependency.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/dropseed/deps/internal/env" + "github.com/dropseed/deps/pkg/schema" ) // Dependency is a path + type in dependencies.yml @@ -13,11 +14,11 @@ 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() { @@ -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 { @@ -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)) @@ -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 } diff --git a/internal/config/config_dependency_test.go b/internal/config/config_dependency_test.go index a45e4c5..b0b5da3 100644 --- a/internal/config/config_dependency_test.go +++ b/internal/config/config_dependency_test.go @@ -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") } diff --git a/internal/config/config_lockfile_updates.go b/internal/config/config_lockfile_updates.go index 0738abf..c5075a0 100644 --- a/internal/config/config_lockfile_updates.go +++ b/internal/config/config_lockfile_updates.go @@ -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"` } diff --git a/internal/config/config_manifest_updates.go b/internal/config/config_manifest_updates.go index 0ffa5f5..7a7f35f 100644 --- a/internal/config/config_manifest_updates.go +++ b/internal/config/config_manifest_updates.go @@ -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 { diff --git a/internal/config/settings.go b/internal/config/settings.go new file mode 100644 index 0000000..f8a73b3 --- /dev/null +++ b/internal/config/settings.go @@ -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 +} diff --git a/internal/pullrequest/bitbucket/main.go b/internal/pullrequest/bitbucket/main.go index 71205f2..8a27f2d 100644 --- a/internal/pullrequest/bitbucket/main.go +++ b/internal/pullrequest/bitbucket/main.go @@ -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{} @@ -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) } @@ -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 } } diff --git a/internal/pullrequest/github/main.go b/internal/pullrequest/github/main.go index 9005348..4177996 100644 --- a/internal/pullrequest/github/main.go +++ b/internal/pullrequest/github/main.go @@ -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{} @@ -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) } @@ -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) diff --git a/internal/pullrequest/gitlab/main.go b/internal/pullrequest/gitlab/main.go index 4f7bce4..825f797 100644 --- a/internal/pullrequest/gitlab/main.go +++ b/internal/pullrequest/gitlab/main.go @@ -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{} @@ -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) } @@ -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)) @@ -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 } } diff --git a/internal/pullrequest/main.go b/internal/pullrequest/main.go index c4ca926..e845620 100644 --- a/internal/pullrequest/main.go +++ b/internal/pullrequest/main.go @@ -20,6 +20,7 @@ const BITBUCKET = "bitbucket" // PullrequestAdapter implements the basic Pullrequest functions type PullrequestAdapter interface { CreateOrUpdate() error + GetSetting(string) interface{} } type RepoAdapter interface {