Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USE URL from API when private #428

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 25 additions & 4 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import (
"golang.org/x/oauth2"
)

// Repository represents a GitHub repository.
type Repository struct {
Private *bool `json:"private,omitempty"`
}

type Release struct {
Name string
Description string
Expand All @@ -39,8 +44,9 @@ type Release struct {
}

type Asset struct {
Path string
URL string
Path string
BrowserDownloadURL string
URL string
}

// Client is the client for interacting with the GitHub API
Expand Down Expand Up @@ -95,8 +101,13 @@ func (c *Client) GetRelease(_ context.Context, tag string) (*Release, error) {
result := &Release{
Assets: []*Asset{},
}
for _, ass := range release.Assets {
asset := &Asset{*ass.Name, *ass.BrowserDownloadURL}

for _, releaseAsset := range release.Assets {
asset := &Asset{
*releaseAsset.Name,
*releaseAsset.BrowserDownloadURL,
*releaseAsset.URL,
}
result.Assets = append(result.Assets, asset)
}
return result, nil
Expand Down Expand Up @@ -126,6 +137,16 @@ func (c *Client) CreateRelease(_ context.Context, input *Release) error {
return nil
}

func (c *Client) GetRepository() (*Repository, error) {
r, _, err := c.Repositories.Get(context.TODO(), c.owner, c.repo)
if err != nil {
return nil, err
}
return &Repository{
Private: r.Private,
}, nil
}

// CreatePullRequest creates a pull request in the repository specified by repoURL.
// The return value is the pull request URL.
func (c *Client) CreatePullRequest(owner string, repo string, message string, head string, base string) (string, error) {
Expand Down
31 changes: 27 additions & 4 deletions pkg/releaser/releaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type GitHub interface {
CreateRelease(ctx context.Context, input *github.Release) error
GetRelease(ctx context.Context, tag string) (*github.Release, error)
CreatePullRequest(owner string, repo string, message string, head string, base string) (string, error)
GetRepository() (*github.Repository, error)
}

type Git interface {
Expand Down Expand Up @@ -138,6 +139,12 @@ func (r *Releaser) UpdateIndexFile() (bool, error) {
return false, err
}

// GetRepository to confirm if it's Private
repository, err := r.github.GetRepository()
if err != nil {
return false, err
}

var update bool
for _, chartPackage := range chartPackages {
ch, err := loader.LoadFile(chartPackage)
Expand All @@ -162,8 +169,9 @@ func (r *Releaser) UpdateIndexFile() (bool, error) {
}

for _, asset := range release.Assets {
downloadURL, _ := url.Parse(asset.URL)
downloadURL, _ := url.Parse(asset.BrowserDownloadURL)
name := filepath.Base(downloadURL.Path)

// Ignore any other files added in the release by the users.
if filepath.Ext(name) != chartAssetFileExtension {
continue
Expand All @@ -173,7 +181,11 @@ func (r *Releaser) UpdateIndexFile() (bool, error) {
packageName, packageVersion := tagParts[0], tagParts[1]
fmt.Printf("Found %s-%s.tgz\n", packageName, packageVersion)
if _, err := indexFile.Get(packageName, packageVersion); err != nil {
if err := r.addToIndexFile(indexFile, downloadURL.String()); err != nil {
overrideURL := ""
if *repository.Private {
overrideURL = asset.URL
}
if err := r.addToIndexFile(indexFile, downloadURL.String(), overrideURL); err != nil {
return false, err
}
update = true
Expand Down Expand Up @@ -255,7 +267,7 @@ func (r *Releaser) splitPackageNameAndVersion(pkg string) []string {
return []string{pkg[0:delimIndex], pkg[delimIndex+1:]}
}

func (r *Releaser) addToIndexFile(indexFile *repo.IndexFile, url string) error {
func (r *Releaser) addToIndexFile(indexFile *repo.IndexFile, url string, overrideURL string) error {
arch := filepath.Join(r.config.PackagePath, filepath.Base(url))

// extract chart metadata
Expand Down Expand Up @@ -284,7 +296,18 @@ func (r *Releaser) addToIndexFile(indexFile *repo.IndexFile, url string) error {
}

// Add to index
return indexFile.MustAdd(c.Metadata, filepath.Base(arch), strings.Join(s, "/"), hash)
addToIndex := indexFile.MustAdd(c.Metadata, filepath.Base(arch), strings.Join(s, "/"), hash)

// replace the URL for Private GitHub repos
if overrideURL != "" {
for _, entry := range indexFile.Entries[c.Metadata.Name] {
if entry.Digest == hash {
entry.URLs = []string{overrideURL}
}
}
}

return addToIndex
}

// CreateReleases finds and uploads Helm chart packages to GitHub
Expand Down
17 changes: 12 additions & 5 deletions pkg/releaser/releaser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,25 @@ func (f *FakeGitHub) GetRelease(ctx context.Context, tag string) (*github.Releas
Description: "A Helm chart for Kubernetes",
Assets: []*github.Asset{
{
Path: "testdata/release-packages/test-chart-0.1.0.tgz",
URL: "https://myrepo/charts/test-chart-0.1.0.tgz",
Path: "testdata/release-packages/test-chart-0.1.0.tgz",
BrowserDownloadURL: "https://myrepo/charts/test-chart-0.1.0.tgz",
},
{
Path: "testdata/release-packages/third-party-file-0.1.0.txt",
URL: "https://myrepo/charts/third-party-file-0.1.0.txt",
Path: "testdata/release-packages/third-party-file-0.1.0.txt",
BrowserDownloadURL: "https://myrepo/charts/third-party-file-0.1.0.txt",
},
},
}
return release, nil
}

func (f *FakeGitHub) GetRepository() (*github.Repository, error) {
b := false
return &github.Repository{
Private: &b,
}, nil
}

func (f *FakeGitHub) CreatePullRequest(owner string, repo string, message string, head string, base string) (string, error) {
f.Called(owner, repo, message, head, base)
return "https://github.com/owner/repo/pull/42", nil
Expand Down Expand Up @@ -336,7 +343,7 @@ func TestReleaser_addToIndexFile(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
indexFile := repo.NewIndexFile()
url := fmt.Sprintf("https://myrepo/charts/%s-%s.tgz", tt.chart, tt.version)
err := tt.releaser.addToIndexFile(indexFile, url)
err := tt.releaser.addToIndexFile(indexFile, url, "")
if tt.error {
assert.Error(t, err)
assert.False(t, indexFile.Has(tt.chart, tt.version))
Expand Down