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

a few internal refactors and added tests #418

Merged
merged 6 commits into from
Aug 1, 2023
Merged
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
2 changes: 1 addition & 1 deletion internal/commands/release_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func getGithubRemoteRepoOwnerAndName(repo *git.Repository) (string, string, erro
return "", "", fmt.Errorf("remote github URL not found for repo")
}

repoOwner, repoName, err := gh.OwnerAndRepoFromURI(remoteURL)
repoOwner, repoName, err := gh.RepositoryOwnerAndNameFromPath(remoteURL)
if err != nil {
return "", "", err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/release_notes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestReleaseNotes_Execute(t *testing.T) {
please.Expect(issuesQuery.IssueIDs).To(Equal([]string{"54000", "54321"}))
please.Expect(issuesQuery.IssueLabels).To(Equal([]string{"tropical", "20000"}))

t.Log(out.String())
//t.Log(out.String())
please.Expect(out.String()).To(Equal(releaseNotesExpectedOutput))
})
}
Expand Down
11 changes: 11 additions & 0 deletions internal/commands/test_tile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"

Expand Down Expand Up @@ -61,6 +62,16 @@ var _ = Describe("kiln test", func() {
Expect(output.String()).To(BeEmpty())
})
})
AfterEach(func() {
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
vendorDir := filepath.Join(filepath.Dir(filepath.Dir(wd)), "vendor")
if info, err := os.Stat(vendorDir); err == nil && info.IsDir() { // no error
_ = os.RemoveAll(vendorDir)
}
})

When("when the tile directory does not exist", func() {
It("runs all the tests with initalized collaborators", func() {
Expand Down
6 changes: 3 additions & 3 deletions internal/component/github_release_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type ReleaseByTagGetter interface {
}

func (grs *GithubReleaseSource) GetGithubReleaseWithTag(ctx context.Context, s cargo.BOSHReleaseTarballSpecification) (*github.RepositoryRelease, error) {
repoOwner, repoName, err := gh.OwnerAndRepoFromURI(s.GitHubRepository)
repoOwner, repoName, err := gh.RepositoryOwnerAndNameFromPath(s.GitHubRepository)
if err != nil {
return nil, ErrNotFound
}
Expand All @@ -118,7 +118,7 @@ func (grs *GithubReleaseSource) GetLatestMatchingRelease(ctx context.Context, s
return nil, fmt.Errorf("expected version to be a constraint")
}

repoOwner, repoName, err := gh.OwnerAndRepoFromURI(s.GitHubRepository)
repoOwner, repoName, err := gh.RepositoryOwnerAndNameFromPath(s.GitHubRepository)
if err != nil {
return nil, ErrNotFound
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func (grs *GithubReleaseSource) getLockFromRelease(ctx context.Context, r *githu
}

func (grs *GithubReleaseSource) getReleaseSHA1(ctx context.Context, s cargo.BOSHReleaseTarballSpecification, id int64) (string, error) {
repoOwner, repoName, err := gh.OwnerAndRepoFromURI(s.GitHubRepository)
repoOwner, repoName, err := gh.RepositoryOwnerAndNameFromPath(s.GitHubRepository)
if err != nil {
return "", fmt.Errorf("could not parse repository name: %v", err)
}
Expand Down
4 changes: 1 addition & 3 deletions internal/gh/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ import (
)

func Client(ctx context.Context, accessToken string) *github.Client {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken})
tokenClient := oauth2.NewClient(ctx, tokenSource)
return github.NewClient(tokenClient)
return github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken})))
}
17 changes: 17 additions & 0 deletions internal/gh/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gh_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/pivotal-cf/kiln/internal/gh"
)

func TestClient(t *testing.T) {
ctx := context.Background()
token := "xxx"
ghClient := gh.Client(ctx, token)
require.NotNil(t, ghClient.Client())
}
43 changes: 0 additions & 43 deletions internal/gh/github_uri.go

This file was deleted.

29 changes: 29 additions & 0 deletions internal/gh/uri.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gh

import (
"fmt"
"net/url"
"path/filepath"
"strings"
)

// RepositoryOwnerAndNameFromPath is from the github-release-source branch
// once that one is merged we should that one instead of this one
func RepositoryOwnerAndNameFromPath(urlStr string) (owner, repo string, err error) {
wrapError := func(urlStr string, err error) error {
return fmt.Errorf("failed to parse owner and repo name from URI %q: %w", urlStr, err)
}
urlStr = strings.TrimPrefix(urlStr, "git@github.com:")
u, err := url.Parse(urlStr)
if err != nil {
return "", "", wrapError(urlStr, err)
}
if filepath.Ext(u.Path) == ".git" {
u.Path = strings.TrimSuffix(u.Path, ".git")
}
owner, repo, found := strings.Cut(strings.TrimPrefix(u.Path, "/"), "/")
if !found || owner == "" || repo == "" {
return owner, repo, wrapError(urlStr, fmt.Errorf("path missing expected parts"))
}
return owner, repo, nil
}
64 changes: 64 additions & 0 deletions internal/gh/uri_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package gh_test

import (
"github.com/pivotal-cf/kiln/internal/gh"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func Test_RepositoryOwnerAndNameFromPath(t *testing.T) {
for _, tt := range []struct {
Name,
URI,
RepositoryOwner, RepositoryName,
ErrorSubstring string
}{
{
Name: "valid url",
URI: "https://github.com/crhntr/hello-release",
RepositoryOwner: "crhntr", RepositoryName: "hello-release",
},
{
Name: "ssh url",
URI: "git@github.com:crhntr/hello-release.git",
RepositoryOwner: "crhntr", RepositoryName: "hello-release",
},
{
Name: "empty ssh path",
URI: "git@github.com:",
ErrorSubstring: "path missing expected parts",
},
{
Name: "not a valid ssh path",
URI: "git@github.com:?invalid_url?",
ErrorSubstring: "path missing expected parts",
},
{
Name: "missing repo name",
URI: "https://github.com/crhntr",
ErrorSubstring: "path missing expected parts",
},
{
Name: "missing repo owner",
URI: "https://github.com//crhntr",
ErrorSubstring: "path missing expected parts",
},
{
Name: "invalid URL",
URI: "/?bell-character=\x07",
ErrorSubstring: "invalid control character in URL",
},
} {
t.Run(tt.Name, func(t *testing.T) {
repoOwner, repoName, err := gh.RepositoryOwnerAndNameFromPath(tt.URI)
if tt.ErrorSubstring != "" {
require.ErrorContains(t, err, tt.ErrorSubstring)
} else {
require.NoError(t, err)
assert.Equal(t, tt.RepositoryOwner, repoOwner)
assert.Equal(t, tt.RepositoryName, repoName)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/cargo/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func ReleaseNotes(ctx context.Context, repoService RepositoryReleaseLister, kf K
}

func fetchReleasesFromRepo(ctx context.Context, repoService RepositoryReleaseLister, repository string, from, to *semver.Version) []*github.RepositoryRelease {
owner, repo, err := gh.OwnerAndRepoFromURI(repository)
owner, repo, err := gh.RepositoryOwnerAndNameFromPath(repository)
if err != nil {
return nil
}
Expand Down