This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #654 from dexhorthy/wip-setgithub
mechanism for setting github contents during local dev
- Loading branch information
Showing
12 changed files
with
315 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
assets: | ||
v1: | ||
- github: | ||
repo: replicatedhq/test-charts | ||
ref: master | ||
path: /release.yml | ||
dest: ./ | ||
lifecycle: | ||
v1: | ||
- render: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"v1": { | ||
"config": { | ||
"test_option": "abc123_test-option-value" | ||
}, | ||
"contentSHA": "b9b0e107bb87dd3843d1d1178a6e9109d42d5e86ac8885a172f23c291b62d3b3", | ||
"metadata": { | ||
"applicationType": "replicated.app", | ||
"customerID": "-Am-_6i5pw0u4AbspOwKN4lZUCn49u_G", | ||
"installationID": "RULNveQLrLj4GDum1yQhvKAcABh0GcLY", | ||
"releaseNotes": "", | ||
"version": "0.0.4" | ||
}, | ||
"releaseName": "ship" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
assets: | ||
v1: | ||
- github: | ||
repo: replicatedhq/test-charts | ||
ref: master | ||
path: /release.yml | ||
dest: ./ | ||
lifecycle: | ||
v1: | ||
- render: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
assets: | ||
v1: | ||
- github: | ||
repo: replicatedhq/test-charts | ||
ref: master | ||
path: /release.yml | ||
dest: ./ | ||
lifecycle: | ||
v1: | ||
- render: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"v1": { | ||
"config": { | ||
"test_option": "abc123_test-option-value" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
customer_id: "-Am-_6i5pw0u4AbspOwKN4lZUCn49u_G" | ||
installation_id: "RULNveQLrLj4GDum1yQhvKAcABh0GcLY" | ||
release_version: "0.0.4" | ||
set_github_contents: "replicatedhq/test-charts:/release.yml:master:.ship" | ||
disable_online: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// local.go has methods for resolving a local ship.yaml file, and patching in api.Release info | ||
// that would usually be returned by pg.replicated.com | ||
package replicatedapp | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/go-kit/kit/log/level" | ||
"github.com/pkg/errors" | ||
"github.com/replicatedhq/ship/pkg/constants" | ||
) | ||
|
||
func (r *resolver) resolveRunbookRelease() (*ShipRelease, error) { | ||
debug := level.Debug(log.With(r.Logger, "method", "resolveRunbookRelease")) | ||
debug.Log("phase", "load-specs", "from", "runbook", "file", r.Runbook) | ||
|
||
specYAML, err := r.FS.ReadFile(r.Runbook) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "read specs from %s", r.Runbook) | ||
} | ||
debug.Log("phase", "load-specs", "from", "runbook", "file", r.Runbook, "spec", specYAML) | ||
|
||
if err := r.persistSpec(specYAML); err != nil { | ||
return nil, errors.Wrapf(err, "serialize last-used YAML to disk") | ||
} | ||
debug.Log("phase", "write-yaml", "from", r.Runbook, "write-location", constants.ReleasePath) | ||
|
||
fakeGithubContents, err := r.loadLocalGitHubContents() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "load fake github contents") | ||
} | ||
|
||
return &ShipRelease{ | ||
Spec: string(specYAML), | ||
ChannelName: r.SetChannelName, | ||
ChannelIcon: r.SetChannelIcon, | ||
Semver: r.RunbookReleaseSemver, | ||
GithubContents: fakeGithubContents, | ||
}, nil | ||
} | ||
|
||
func (r *resolver) loadLocalGitHubContents() ([]GithubContent, error) { | ||
debug := level.Debug(log.With(r.Logger, "method", "loadLocalGitHubContents")) | ||
var fakeGithubContents []GithubContent | ||
for _, content := range r.SetGitHubContents { | ||
debug.Log("event", "githubcontents.set", "received", content) | ||
split := strings.Split(content, ":") | ||
if len(split) != 4 { | ||
return nil, errors.Errorf("set-github-contents %q invalid, expected a REPO:REPO_PATH:REF:LOCAL_PATH", content) | ||
} | ||
repo := split[0] | ||
repoPath := split[1] | ||
ref := split[2] | ||
localpath := split[3] | ||
|
||
debug.Log("event", "githubcontents.loadFiles", "localPath", localpath) | ||
files, err := r.loadLocalGithubFiles(localpath, repoPath) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "set github files") | ||
} | ||
|
||
fakeGithubContents = append(fakeGithubContents, GithubContent{ | ||
Repo: repo, | ||
Path: repoPath, | ||
Ref: ref, | ||
Files: files, | ||
}) | ||
debug.Log("event", "githubcontents.set.finished", "received", content) | ||
} | ||
return fakeGithubContents, nil | ||
} | ||
|
||
func (r *resolver) loadLocalGithubFiles(localpath string, repoPath string) ([]GithubFile, error) { | ||
debug := level.Debug(log.With(r.Logger, "method", "loadLocalGitHubFiles")) | ||
var files []GithubFile | ||
err := r.FS.Walk(localpath, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return errors.Wrapf(err, "walk %s from %s", info.Name(), path) | ||
} | ||
|
||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
walkRepoPath := strings.TrimPrefix(path, localpath) | ||
if !strings.HasPrefix(walkRepoPath, repoPath) { | ||
return nil | ||
} | ||
|
||
contents, err := r.FS.ReadFile(path) | ||
if err != nil { | ||
return errors.Wrapf(err, "read %s from %s", info.Name(), path) | ||
} | ||
debug.Log("event", "githubcontents.loadFile.complete", "path", path, "name", info.Name()) | ||
|
||
encodedData := &bytes.Buffer{} | ||
encoder := base64.NewEncoder(base64.StdEncoding, encodedData) | ||
defer encoder.Close() | ||
encoder.Write(contents) | ||
sha := fmt.Sprintf("%x", sha256.Sum256(contents)) | ||
files = append(files, GithubFile{ | ||
Name: info.Name(), | ||
Path: walkRepoPath, | ||
Sha: sha, | ||
Size: info.Size(), | ||
Data: encodedData.String(), | ||
}) | ||
return nil | ||
}) | ||
return files, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package replicatedapp | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/replicatedhq/ship/pkg/testing/logger" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLoadLocalGitHubContents(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
githubContent []string | ||
fs map[string]string | ||
expectContents []GithubContent | ||
}{ | ||
{ | ||
name: "none to set", | ||
githubContent: nil, | ||
expectContents: nil, | ||
}, | ||
{ | ||
name: "set one file", | ||
fs: map[string]string{ | ||
"/foo/bar.txt": "some-contents", | ||
}, | ||
githubContent: []string{"replicatedhq/test-stuff:/bar.txt:master:/foo"}, | ||
expectContents: []GithubContent{ | ||
{ | ||
Repo: "replicatedhq/test-stuff", | ||
Path: "/bar.txt", | ||
Ref: "master", | ||
Files: []GithubFile{ | ||
{ | ||
Path: "/bar.txt", | ||
Name: "bar.txt", | ||
Size: 13, | ||
Sha: "6e32ea34db1b3755d7dec972eb72c705338f0dd8e0be881d966963438fb2e800", | ||
Data: "c29tZS1jb250ZW50", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "set many files from two repos", | ||
fs: map[string]string{ | ||
"/foo/bar.txt": "some-contents", | ||
"/foo/baz.txt": "some-contents", | ||
"/foo/bar/baz.txt": "some-contents", | ||
"/spam/eggs.txt": "some-other-contents", | ||
}, | ||
githubContent: []string{ | ||
"replicatedhq/test-stuff:/:master:/foo", | ||
"replicatedhq/other-tests:/eggs.txt:release:/spam", | ||
}, | ||
expectContents: []GithubContent{ | ||
{ | ||
Repo: "replicatedhq/test-stuff", | ||
Path: "/", | ||
Ref: "master", | ||
Files: []GithubFile{ | ||
{ | ||
Path: "/bar/baz.txt", | ||
Name: "baz.txt", | ||
Size: 13, | ||
Sha: "6e32ea34db1b3755d7dec972eb72c705338f0dd8e0be881d966963438fb2e800", | ||
Data: "c29tZS1jb250ZW50", | ||
}, | ||
{ | ||
Path: "/bar.txt", | ||
Name: "bar.txt", | ||
Size: 13, | ||
Sha: "6e32ea34db1b3755d7dec972eb72c705338f0dd8e0be881d966963438fb2e800", | ||
Data: "c29tZS1jb250ZW50", | ||
}, | ||
{ | ||
Path: "/baz.txt", | ||
Name: "baz.txt", | ||
Size: 13, | ||
Sha: "6e32ea34db1b3755d7dec972eb72c705338f0dd8e0be881d966963438fb2e800", | ||
Data: "c29tZS1jb250ZW50", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Repo: "replicatedhq/other-tests", | ||
Path: "/eggs.txt", | ||
Ref: "release", | ||
Files: []GithubFile{ | ||
{ | ||
Path: "/eggs.txt", | ||
Name: "eggs.txt", | ||
Size: 19, | ||
Sha: "a2c0a8c54d71e14e9533749c32716c12f92f61294dfdce4f3b4c07303c0119b0", | ||
Data: "c29tZS1vdGhlci1jb250ZW50", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
req := require.New(t) | ||
mockFs := afero.Afero{Fs: afero.NewMemMapFs()} | ||
|
||
for key, value := range test.fs { | ||
err := mockFs.WriteFile(key, []byte(value), 0777) | ||
req.NoError(err) | ||
} | ||
|
||
resolver := &resolver{ | ||
Logger: &logger.TestLogger{T: t}, | ||
FS: mockFs, | ||
SetGitHubContents: test.githubContent, | ||
} | ||
|
||
result, err := resolver.loadLocalGitHubContents() | ||
|
||
req.NoError(err) | ||
req.Equal(test.expectContents, result) | ||
}) | ||
} | ||
} |
Oops, something went wrong.