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

Commit

Permalink
Merge pull request #203 from marccampbell/safe-split-url
Browse files Browse the repository at this point in the history
Handle invalid github urls on the init command
  • Loading branch information
marccampbell authored Jul 30, 2018
2 parents 39586b6 + 1b15d23 commit df7e670
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
27 changes: 22 additions & 5 deletions pkg/specs/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ func (g *GithubClient) GetChartAndReadmeContents(ctx context.Context, chartURLSt
if err != nil {
return err
}
chartPath := chartURL.Path
splitPath := strings.Split(chartPath, "/")
owner := splitPath[1]
repo := splitPath[2]
path := strings.Join(splitPath[3:], "/")

owner, repo, path, err := decodeGitHubUrl(chartURL.Path)
if err != nil {
return err
}

debug.Log("event", "checkExists", "path", constants.KustomizeHelmPath)
saveDirExists, err := g.fs.Exists(constants.KustomizeHelmPath)
Expand Down Expand Up @@ -168,3 +168,20 @@ func (r *Resolver) ResolveChartMetadata(ctx context.Context, path string) (api.H
md.Readme = string(readme)
return md, nil
}

func decodeGitHubUrl(chartPath string) (string, string, string, error) {
splitPath := strings.Split(chartPath, "/")

if len(splitPath) < 3 {
return "", "", "", errors.Wrapf(errors.New("unable to decode github url"), chartPath)
}

owner := splitPath[1]
repo := splitPath[2]
path := ""
if len(splitPath) > 3 {
path = strings.Join(splitPath[3:], "/")
}

return owner, repo, path, nil
}
42 changes: 41 additions & 1 deletion pkg/specs/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var _ = Describe("GithubClient", func() {

Context("With a url not prefixed with http", func() {
It("should fetch and persist README.md and Chart.yaml", func() {
validGitURLWithoutPrefix := "github.com/o/r/"
validGitURLWithoutPrefix := "github.com/o/r"
mockFs := afero.Afero{Fs: afero.NewMemMapFs()}
gitClient := GithubClient{
client: client,
Expand All @@ -111,6 +111,46 @@ var _ = Describe("GithubClient", func() {
})
})
})

Describe("decodeGitHubUrl", func() {
Context("With a valid github url", func() {
It("should decode a valid url without a path", func() {
chartPath := "github.com/o/r"
o, r, p, err := decodeGitHubUrl(chartPath)
Expect(err).NotTo(HaveOccurred())

Expect(o).To(Equal("o"))
Expect(r).To(Equal("r"))
Expect(p).To(Equal(""))
})

It("should decode a valid url with a path", func() {
chartPath := "github.com/o/r/stable/chart"
o, r, p, err := decodeGitHubUrl(chartPath)
Expect(err).NotTo(HaveOccurred())

Expect(o).To(Equal("o"))
Expect(r).To(Equal("r"))
Expect(p).To(Equal("stable/chart"))
})
})

Context("With an invalid github url", func() {
It("should failed to decode a url without a path", func() {
chartPath := "github.com"
_, _, _, err := decodeGitHubUrl(chartPath)
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(Equal("github.com: unable to decode github url"))
})

It("should failed to decode a url with a path", func() {
chartPath := "github.com/o"
_, _, _, err := decodeGitHubUrl(chartPath)
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(Equal("github.com/o: unable to decode github url"))
})
})
})
})

var _ = AfterSuite(func() {
Expand Down

0 comments on commit df7e670

Please sign in to comment.