Skip to content

Commit

Permalink
Add githubRelease object to the template data for githubReleases pr…
Browse files Browse the repository at this point in the history
…ovider
  • Loading branch information
mumoshu committed May 19, 2020
1 parent 189011d commit f0d0ab1
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 49 deletions.
3 changes: 2 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ func New(log logr.Logger) *cobra.Command {
if err := man.Checkout(base); err != nil {
return err
}
if err := man.Up(); err != nil {
err = man.Up()
if err != nil {
return err
}
files := []string{"variant.mod", "variant.lock"}
Expand Down
17 changes: 14 additions & 3 deletions pkg/config/confapi/confapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,20 @@ type ModVersionLock struct {
}

type DepVersionLock struct {
Version string `yaml:"version"`
PreviousVersion string `yaml:"previousVersion,omitempty"`
Version string `yaml:"version"`
PreviousVersion string `yaml:"previousVersion,omitempty"`
Meta map[string]interface{} `yaml:",inline"`
}

func (l ModVersionLock) ToMap() map[string]interface{} {
return map[string]interface{}{"Dependencies": l.ToDepsMap(), "RawLock": l.RawLock}
deps := l.ToDepsMap()
m := map[string]interface{}{"Dependencies": deps, "RawLock": l.RawLock}

for k, v := range deps {
m[k] = v
}

return m
}

func (l ModVersionLock) ToDepsMap() map[string]interface{} {
Expand All @@ -102,6 +110,9 @@ func (l ModVersionLock) ToDepsMap() map[string]interface{} {
if v.PreviousVersion != "" {
m["previousVersion"] = v.PreviousVersion
}
for kk, vv := range v.Meta {
m[kk] = vv
}
deps[k] = m
}
return deps
Expand Down
104 changes: 91 additions & 13 deletions pkg/releasetracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type Release struct {
Version string

Description string

// Meta is the provider-specific metadata composed of arbitrary kv pairs
Meta map[string]interface{}
}

type Tracker struct {
Expand Down Expand Up @@ -233,9 +236,12 @@ func newGitHubReleasesProvider(spec GitHubReleases, r *Tracker) *httpJsonPathPro
url := fmt.Sprintf("https://%s/repos/%s/releases", host, spec.Source)

return &httpJsonPathProvider{
url: url,
jsonpath: "$[*].tag_name",
runtime: r,
url: url,
jsonpath: "$[*].tag_name",
metaKey: "githubRelease",
objectPath: "$[*]",
versionPath: "tag_name",
runtime: r,
}
}

Expand Down Expand Up @@ -331,13 +337,17 @@ type httpJsonPathProvider struct {
nextpagePath string
params map[string]string

metaKey string
objectPath string
versionPath string

runtime *Tracker
}

var _ ReleaseProvider = &httpJsonPathProvider{}

func (p *httpJsonPathProvider) All() ([]*Release, error) {
return p.runtime.releasesFromHttpJsonPath(p.url, p.jsonpath, p.nextpagePath, p.params)
return p.runtime.releasesFromHttpJsonPath(p)
}

func (p *Tracker) execScript(cmd string) ([]string, error) {
Expand Down Expand Up @@ -403,7 +413,12 @@ func (p *Tracker) releasesFromGetterJsonPath(spec GetterJSONPath) ([]*Release, e
return p.extractVersions(tmp, spec.Versions)
}

func (p *Tracker) releasesFromHttpJsonPath(url string, jpath string, nextpagePath string, params map[string]string) ([]*Release, error) {
func (p *Tracker) releasesFromHttpJsonPath(pp *httpJsonPathProvider) ([]*Release, error) {
url := pp.url
jpath := pp.jsonpath
nextpagePath := pp.nextpagePath
params := pp.params

query := ""
for k, v := range params {
if query != "" {
Expand Down Expand Up @@ -438,12 +453,21 @@ func (p *Tracker) releasesFromHttpJsonPath(url string, jpath string, nextpagePat

debug("http response: %v", res)

page, err := p.extractVersions(tmp, jpath)
if err != nil {
return nil, err
}
if pp.objectPath != "" && pp.versionPath != "" && pp.metaKey != "" {
page, err := p.extractObjects(tmp, pp.objectPath, pp.versionPath, pp.metaKey)
if err != nil {
return nil, err
}

releases = append(releases, page...)
} else {
page, err := p.extractVersions(tmp, jpath)
if err != nil {
return nil, err
}

releases = append(releases, page...)
releases = append(releases, page...)
}

if nextpagePath == "" {
break
Expand All @@ -460,6 +484,56 @@ func (p *Tracker) releasesFromHttpJsonPath(url string, jpath string, nextpagePat
return releases, nil
}

func (p *Tracker) extractObjects(tmp interface{}, objPath, verPath, metaKey string) ([]*Release, error) {
v, err := maputil.RecursivelyCastKeysToStrings(tmp)
if err != nil {
return nil, err
}

got, err := jsonpath.Get(objPath, v)
if err != nil {
return nil, err
}

var rs []*Release

switch typed := got.(type) {
case []interface{}:
for _, obj := range typed {
raw, err := jsonpath.Get(verPath, obj)
if err != nil {
return nil, err
}

s, ok := raw.(string)
if !ok {
return nil, fmt.Errorf("unexpected type of value: want string, got %T, value is %v", raw, raw)
}

v, err := p.parseVersion(s)
if err != nil {
return nil, err
}

meta := map[string]interface{}{
metaKey: obj,
}

rs = append(rs, &Release{
Semver: v,
Version: strings.TrimPrefix(s, "v"),
Meta: meta,
})
}
}

sort.Slice(rs, func(i, j int) bool {
return rs[i].Semver.LessThan(rs[j].Semver)
})

return rs, nil
}

func (p *Tracker) extractVersions(tmp interface{}, jpath string) ([]*Release, error) {
vs, err := p.extractVersionStrings(tmp, jpath)
if err != nil {
Expand Down Expand Up @@ -572,12 +646,16 @@ func nonSemverWorkaround(s string) string {
return s
}

func (p *Tracker) parseVersion(s string) (*semver.Version, error) {
fixedS := nonSemverWorkaround(strings.TrimSpace(s))

return semver.NewVersion(fixedS)
}

func (p *Tracker) versionStringsToReleases(vs []string) ([]*Release, error) {
rs := []*Release{}
for i, s := range vs {
fixedS := nonSemverWorkaround(strings.TrimSpace(s))

v, err := semver.NewVersion(fixedS)
v, err := p.parseVersion(s)
if err != nil {
e := fmt.Errorf("parsing version: index %d: %q: %v", i, s, err)
p.Logger.V(1).Info("ignoring error", "err", e)
Expand Down
42 changes: 23 additions & 19 deletions pkg/variantmod/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func (m *ModuleLoader) LoadModule(params confapi.ModuleParams) (mod *Module, err
}

func (m *ModuleLoader) InitModule(params confapi.ModuleParams, mod confapi.Module) (*Module, error) {
vals := mergeByOverwrite(Values{}, mod.Defaults, params.Arguments, params.LockedVersions.ToMap())
lockValues := params.LockedVersions.ToMap()

initialValues := mergeByOverwrite(Values{}, mod.Defaults, params.Arguments, lockValues)

verLock := params.LockedVersions

Expand All @@ -76,33 +78,33 @@ func (m *ModuleLoader) InitModule(params confapi.ModuleParams, mod confapi.Modul
r.VersionsFrom.Exec.Args = dep.VersionsFrom.Exec.Args
r.VersionsFrom.Exec.Command = dep.VersionsFrom.Exec.Command
if dep.VersionsFrom.DockerImageTags.Source != nil {
r.VersionsFrom.DockerImageTags.Source, err = dep.VersionsFrom.DockerImageTags.Source(vals)
r.VersionsFrom.DockerImageTags.Source, err = dep.VersionsFrom.DockerImageTags.Source(initialValues)
if err != nil {
return nil, err
}
}
if dep.VersionsFrom.GitHubReleases.Source != nil {
r.VersionsFrom.GitHubReleases.Source, err = dep.VersionsFrom.GitHubReleases.Source(vals)
r.VersionsFrom.GitHubReleases.Source, err = dep.VersionsFrom.GitHubReleases.Source(initialValues)
if err != nil {
return nil, err
}
r.VersionsFrom.GitHubReleases.Host = dep.VersionsFrom.GitHubReleases.Host
}
if dep.VersionsFrom.GitHubTags.Source != nil {
r.VersionsFrom.GitHubTags.Source, err = dep.VersionsFrom.GitHubTags.Source(vals)
r.VersionsFrom.GitHubTags.Source, err = dep.VersionsFrom.GitHubTags.Source(initialValues)
if err != nil {
return nil, err
}
r.VersionsFrom.GitHubTags.Host = dep.VersionsFrom.GitHubTags.Host
}
if dep.VersionsFrom.GitTags.Source != nil {
r.VersionsFrom.GitTags.Source, err = dep.VersionsFrom.GitTags.Source(vals)
r.VersionsFrom.GitTags.Source, err = dep.VersionsFrom.GitTags.Source(initialValues)
if err != nil {
return nil, err
}
}
if dep.VersionsFrom.JSONPath.Source != nil {
r.VersionsFrom.JSONPath.Source, err = dep.VersionsFrom.JSONPath.Source(vals)
r.VersionsFrom.JSONPath.Source, err = dep.VersionsFrom.JSONPath.Source(initialValues)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -159,11 +161,11 @@ func (m *ModuleLoader) InitModule(params confapi.ModuleParams, mod confapi.Modul
}

prev := verLock.Dependencies[alias].Version
vals[alias] = Values{"version": rel.Version, "previousVersion": prev}

verLock.Dependencies[alias] = confapi.DepVersionLock{
Version: rel.Version,
PreviousVersion: prev,
Meta: rel.Meta,
}
} else {
m.Logger.V(2).Info("no tracker found", "alias", alias)
Expand All @@ -180,17 +182,19 @@ func (m *ModuleLoader) InitModule(params confapi.ModuleParams, mod confapi.Modul
if err != nil {
return nil, fmt.Errorf("updating locked dependency %q: %w", alias, err)
}
vals[alias] = Values{"version": rel.Version}

verLock.Dependencies[alias] = confapi.DepVersionLock{Version: rel.Version}
verLock.Dependencies[alias] = confapi.DepVersionLock{
Version: rel.Version,
Meta: rel.Meta,
}
} else {
m.Logger.V(2).Info("no tracker found", "alias", alias)
}
}
}

// Regenerate template parameters from the up-to-date versions of dependencies
vals = mergeByOverwrite(Values{}, mod.Defaults, params.Arguments, verLock.ToDepsMap(), verLock.ToMap())
latestValues := mergeByOverwrite(Values{}, mod.Defaults, params.Arguments, verLock.ToMap())

// Load sub-modules
for alias, dep := range mod.Dependencies {
Expand All @@ -204,9 +208,9 @@ func (m *ModuleLoader) InitModule(params confapi.ModuleParams, mod confapi.Modul
dep.LockedVersions.Dependencies = map[string]confapi.DepVersionLock{}
}

args, err := dep.Arguments(vals)
args, err := dep.Arguments(latestValues)
if err != nil {
m.Logger.V(2).Info("renderargs failed with values", "vals", vals)
m.Logger.V(2).Info("renderargs failed with values", "latestValues", latestValues)
return nil, err
}
m.Logger.V(2).Info("loading dependency", "alias", alias, "dep", dep)
Expand All @@ -223,10 +227,10 @@ func (m *ModuleLoader) InitModule(params confapi.ModuleParams, mod confapi.Modul
}
submods[alias] = submod

vals = mergeByOverwrite(Values{}, vals, map[string]interface{}{alias: submod.Values})
//vals[alias] = submod.Values
latestValues = mergeByOverwrite(Values{}, latestValues, map[string]interface{}{alias: submod.Values})
//latestValues[alias] = submod.Values

m.Logger.V(1).Info("loaded dependency", "alias", alias, "vals", vals)
m.Logger.V(1).Info("loaded dependency", "alias", alias, "latestValues", latestValues)
}

execs := map[string]execversionmanager.Executable{}
Expand All @@ -235,7 +239,7 @@ func (m *ModuleLoader) InitModule(params confapi.ModuleParams, mod confapi.Modul
for _, p := range v.Platforms {
var src string
if p.Source != nil {
s, err := p.Source(vals)
s, err := p.Source(latestValues)
if err != nil {
return nil, err
}
Expand All @@ -244,7 +248,7 @@ func (m *ModuleLoader) InitModule(params confapi.ModuleParams, mod confapi.Modul

var docker yaml.OptionSpec
if p.Docker != nil {
d, err := p.Docker(vals)
d, err := p.Docker(latestValues)
if err != nil {
return nil, err
}
Expand All @@ -266,7 +270,7 @@ func (m *ModuleLoader) InitModule(params confapi.ModuleParams, mod confapi.Modul
&execversionmanager.Config{
Executables: execs,
},
execversionmanager.Values(vals),
execversionmanager.Values(latestValues),
execversionmanager.WD(m.AbsWorkDir),
execversionmanager.GoGetterWD(m.GoGetterAbsWorkDir),
execversionmanager.FS(m.FS),
Expand All @@ -277,7 +281,7 @@ func (m *ModuleLoader) InitModule(params confapi.ModuleParams, mod confapi.Modul

return &Module{
Alias: mod.Name,
Values: vals,
Values: latestValues,
ValuesSchema: mod.ValuesSchema,
Files: mod.Files,
Directories: mod.Directories,
Expand Down
Loading

0 comments on commit f0d0ab1

Please sign in to comment.