From 22c58df4d50e8e28b5ef2b9b6657ce2460206870 Mon Sep 17 00:00:00 2001 From: Christopher Hunter Date: Fri, 28 Apr 2023 11:59:32 -0400 Subject: [PATCH] refactor: require GitHub release source methods to receive a pointer this makes type assertions easier --- internal/component/github_release_source.go | 16 ++++++++-------- .../github_release_source_internal_test.go | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/internal/component/github_release_source.go b/internal/component/github_release_source.go index 95647c00f..38b19657a 100644 --- a/internal/component/github_release_source.go +++ b/internal/component/github_release_source.go @@ -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") @@ -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 @@ -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") @@ -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 { @@ -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) @@ -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) @@ -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) } diff --git a/internal/component/github_release_source_internal_test.go b/internal/component/github_release_source_internal_test.go index 5da7a51f7..3c6dd2735 100644 --- a/internal/component/github_release_source_internal_test.go +++ b/internal/component/github_release_source_internal_test.go @@ -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",