Skip to content

Commit

Permalink
refactor: require GitHub release source methods to receive a pointer
Browse files Browse the repository at this point in the history
this makes type assertions easier
  • Loading branch information
crhntr committed May 10, 2023
1 parent b5f8eb7 commit 22c58df
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
16 changes: 8 additions & 8 deletions internal/component/github_release_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ func NewGithubReleaseSource(c cargo.ReleaseSourceConfig) *GithubReleaseSource {

// Configuration returns the configuration of the ReleaseSource that came from the kilnfile.
// It should not be modified.
func (grs GithubReleaseSource) Configuration() cargo.ReleaseSourceConfig {
func (grs *GithubReleaseSource) Configuration() cargo.ReleaseSourceConfig {
return grs.ReleaseSourceConfig
}

// GetMatchedRelease uses the Name and Version and if supported StemcellOS and StemcellVersion
// fields on Requirement to download a specific release.
func (grs GithubReleaseSource) GetMatchedRelease(s Spec) (Lock, error) {
func (grs *GithubReleaseSource) GetMatchedRelease(s Spec) (Lock, error) {
_, err := semver.NewVersion(s.Version)
if err != nil {
return Lock{}, fmt.Errorf("expected version to be an exact version")
Expand All @@ -90,7 +90,7 @@ type ReleaseByTagGetter interface {
GetReleaseByTag(ctx context.Context, owner, repo, tag string) (*github.RepositoryRelease, *github.Response, error)
}

func (grs GithubReleaseSource) GetGithubReleaseWithTag(ctx context.Context, s Spec) (*github.RepositoryRelease, error) {
func (grs *GithubReleaseSource) GetGithubReleaseWithTag(ctx context.Context, s Spec) (*github.RepositoryRelease, error) {
repoOwner, repoName, err := gh.OwnerAndRepoFromURI(s.GitHubRepository)
if err != nil {
return nil, ErrNotFound
Expand All @@ -112,7 +112,7 @@ func (grs GithubReleaseSource) GetGithubReleaseWithTag(ctx context.Context, s Sp
return release, nil
}

func (grs GithubReleaseSource) GetLatestMatchingRelease(ctx context.Context, s Spec) (*github.RepositoryRelease, error) {
func (grs *GithubReleaseSource) GetLatestMatchingRelease(ctx context.Context, s Spec) (*github.RepositoryRelease, error) {
c, err := s.VersionConstraints()
if err != nil {
return nil, fmt.Errorf("expected version to be a constraint")
Expand Down Expand Up @@ -178,7 +178,7 @@ func (grs GithubReleaseSource) GetLatestMatchingRelease(ctx context.Context, s S

// FindReleaseVersion may use any of the fields on Requirement to return the best matching
// release.
func (grs GithubReleaseSource) FindReleaseVersion(s Spec, noDownload bool) (Lock, error) {
func (grs *GithubReleaseSource) FindReleaseVersion(s Spec, noDownload bool) (Lock, error) {
ctx := context.TODO()
release, err := grs.GetLatestMatchingRelease(ctx, s)
if err != nil {
Expand All @@ -188,7 +188,7 @@ func (grs GithubReleaseSource) FindReleaseVersion(s Spec, noDownload bool) (Lock
return grs.getLockFromRelease(ctx, release, s, noDownload)
}

func (grs GithubReleaseSource) getLockFromRelease(ctx context.Context, r *github.RepositoryRelease, s Spec, noDownload bool) (Lock, error) {
func (grs *GithubReleaseSource) getLockFromRelease(ctx context.Context, r *github.RepositoryRelease, s Spec, noDownload bool) (Lock, error) {
lockVersion := strings.TrimPrefix(r.GetTagName(), "v")
expectedAssetName := fmt.Sprintf("%s-%s.tgz", s.Name, lockVersion)
malformedAssetName := fmt.Sprintf("%s-v%s.tgz", s.Name, lockVersion)
Expand Down Expand Up @@ -221,7 +221,7 @@ func (grs GithubReleaseSource) getLockFromRelease(ctx context.Context, r *github
return Lock{}, fmt.Errorf("no matching GitHub release asset file name equal to %q", expectedAssetName)
}

func (grs GithubReleaseSource) getReleaseSHA1(ctx context.Context, s Spec, id int64) (string, error) {
func (grs *GithubReleaseSource) getReleaseSHA1(ctx context.Context, s Spec, id int64) (string, error) {
repoOwner, repoName, err := gh.OwnerAndRepoFromURI(s.GitHubRepository)
if err != nil {
return "", fmt.Errorf("could not parse repository name: %v", err)
Expand All @@ -243,7 +243,7 @@ type ReleasesLister interface {
// DownloadRelease downloads the release and writes the resulting file to the releasesDir.
// It should also calculate and set the SHA1 field on the Local result; it does not need
// to ensure the sums match, the caller must verify this.
func (grs GithubReleaseSource) DownloadRelease(releaseDir string, remoteRelease Lock) (Local, error) {
func (grs *GithubReleaseSource) DownloadRelease(releaseDir string, remoteRelease Lock) (Local, error) {
grs.Logger.Printf(logLineDownload, remoteRelease.Name, ReleaseSourceTypeGithub, grs.ID)
return downloadRelease(context.TODO(), releaseDir, remoteRelease, grs, grs.Logger)
}
Expand Down
15 changes: 15 additions & 0 deletions internal/component/github_release_source_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ import (
"github.com/pivotal-cf/kiln/internal/component/fakes_internal"
)

func TestGithubReleaseSource_interface_compliance(t *testing.T) {
t.Run("non-ptr", func(t *testing.T) {
please := NewWithT(t)
var grs any = GithubReleaseSource{}
_, ok := grs.(ReleaseSource)
please.Expect(ok).To(BeFalse())
})
t.Run("ptr", func(t *testing.T) {
please := NewWithT(t)
var grs any = new(GithubReleaseSource)
_, ok := grs.(ReleaseSource)
please.Expect(ok).To(BeTrue())
})
}

func TestGithubReleaseSource_downloadRelease(t *testing.T) {
lock := Lock{
Name: "routing",
Expand Down

0 comments on commit 22c58df

Please sign in to comment.