Skip to content

Commit

Permalink
USE URL from API when private
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Clegg <[email protected]>
  • Loading branch information
mattclegg committed Jul 2, 2024
1 parent d83b45b commit b0489e2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
28 changes: 24 additions & 4 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
"golang.org/x/oauth2"
)

type Repository struct {
*github.Repository
}

type Release struct {
Name string
Description string
Expand All @@ -39,8 +43,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 +100,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 +136,16 @@ func (c *Client) CreateRelease(_ context.Context, input *Release) error {
return nil
}

func (c *Client) GetRepository() (*Repository, error) {
repository, _, err := c.Repositories.Get(context.TODO(), c.owner, c.repo)
if err != nil {
return nil, err
}
return &Repository{
Repository: repository,
}, 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
10 changes: 5 additions & 5 deletions pkg/releaser/releaser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ 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",
},
},
}
Expand Down Expand Up @@ -336,7 +336,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

0 comments on commit b0489e2

Please sign in to comment.