Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

make GetChartAndReadmeContents verify that the url was a github url #381

Merged
merged 1 commit into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions pkg/specs/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ import (
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/google/go-github/github"
"github.com/pkg/errors"
"github.com/replicatedhq/ship/pkg/api"
"github.com/replicatedhq/ship/pkg/constants"
"github.com/spf13/afero"

"path"

"github.com/google/go-github/github"
"github.com/replicatedhq/ship/pkg/api"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -139,6 +137,10 @@ func (g *GithubClient) GetChartAndReadmeContents(ctx context.Context, chartURLSt
return err
}

if !strings.Contains(chartURL.Host, "github.com") {
return errors.New(fmt.Sprintf("%s is not a Github URL", chartURLString))
}

owner, repo, branch, path, err := decodeGitHubURL(chartURL.Path)
if err != nil {
return err
Expand Down
27 changes: 23 additions & 4 deletions pkg/specs/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ var _ = Describe("GithubClient", func() {
logger: log.NewNopLogger(),
}

gitClient.GetChartAndReadmeContents(context.Background(), validGitURLWithPrefix)
err := gitClient.GetChartAndReadmeContents(context.Background(), validGitURLWithPrefix)
Expect(err).NotTo(HaveOccurred())

readme, err := gitClient.fs.ReadFile(path.Join(constants.KustomizeHelmPath, "README.md"))
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -109,7 +110,9 @@ var _ = Describe("GithubClient", func() {
logger: log.NewNopLogger(),
}

gitClient.GetChartAndReadmeContents(context.Background(), validGitURLWithoutPrefix)
err := gitClient.GetChartAndReadmeContents(context.Background(), validGitURLWithoutPrefix)
Expect(err).NotTo(HaveOccurred())

readme, err := gitClient.fs.ReadFile(path.Join(constants.KustomizeHelmPath, "README.md"))
Expect(err).NotTo(HaveOccurred())
chart, err := gitClient.fs.ReadFile(path.Join(constants.KustomizeHelmPath, "Chart.yaml"))
Expand All @@ -125,6 +128,22 @@ var _ = Describe("GithubClient", func() {
Expect(string(service)).To(Equal("service"))
})
})

Context("With a non-github url", func() {
It("should return an error", func() {
nonGithubURL := "gitlab.com/o/r"
mockFs := afero.Afero{Fs: afero.NewMemMapFs()}
gitClient := GithubClient{
client: client,
fs: mockFs,
logger: log.NewNopLogger(),
}

err := gitClient.GetChartAndReadmeContents(context.Background(), nonGithubURL)
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(Equal("http://gitlab.com/o/r is not a Github URL"))
})
})
})

Describe("decodeGitHubURL", func() {
Expand Down Expand Up @@ -171,7 +190,7 @@ var _ = Describe("GithubClient", func() {
Expect(err.Error()).To(Equal("github.com: unable to decode github url"))
})

It("should failed to decode a url with a path", func() {
It("should fail to decode a url with a path", func() {
chartPath := "github.com/o"
_, _, _, _, err := decodeGitHubURL(chartPath)
Expect(err).NotTo(BeNil())
Expand All @@ -182,7 +201,7 @@ var _ = Describe("GithubClient", func() {

Describe("calculateContentSHA", func() {
Context("With multiple files", func() {
It("should calculate the same sha, mulitple times", func() {
It("should calculate the same sha, multiple times", func() {
mockFs := afero.Afero{Fs: afero.NewMemMapFs()}
mockFs.WriteFile("Chart.yaml", []byte("chart.yaml"), 0755)
mockFs.WriteFile("templates/README.md", []byte("readme"), 0755)
Expand Down