Skip to content

Commit

Permalink
fix: get the right credentials for bosh release repo release listing
Browse files Browse the repository at this point in the history
Co-Authored-By: Ajita Jain <[email protected]>
  • Loading branch information
crhntr and jajita committed Oct 4, 2024
1 parent 5597bf8 commit 835c7e3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
29 changes: 19 additions & 10 deletions internal/gh/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,36 @@ import (

// 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) {
func RepositoryOwnerAndNameFromPath(urlStr string) (string, string, error) {
_, owner, repo, err := RepositoryHostOwnerAndNameFromPath(urlStr)
return owner, repo, err
}

func RepositoryHostOwnerAndNameFromPath(urlStr string) (string, string, string, error) {
wrapError := func(urlStr string, err error) error {
return fmt.Errorf("failed to parse owner and repo name from URI %q: %w", urlStr, err)
}

sshReg := regexp.MustCompile(`(?m)git@(?P<host>.*):(?P<owner>[^/]+)/(?P<name>.*)\.git`)
if m := sshReg.FindStringSubmatch(urlStr); m != nil {
owner = m[sshReg.SubexpIndex("owner")]
repo = m[sshReg.SubexpIndex("name")]
return owner, repo, nil
if strings.HasPrefix(urlStr, "git@") {
exp := regexp.MustCompile(`git@(?P<host>.*):(?P<owner>[^/]+)/(?P<name>.+)\.git`)
m := exp.FindStringSubmatch(urlStr)
if m == nil {
return "", "", "", fmt.Errorf("path missing expected parts")
}
host := m[exp.SubexpIndex("host")]
owner := m[exp.SubexpIndex("owner")]
repo := m[exp.SubexpIndex("name")]
return host, owner, repo, nil
}
u, err := url.Parse(urlStr)
if err != nil {
return "", "", wrapError(urlStr, err)
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, wrapError(urlStr, fmt.Errorf("path missing expected parts"))
}
return owner, repo, nil
return u.Host, owner, repo, nil
}
23 changes: 20 additions & 3 deletions internal/gh/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,29 @@ func Test_RepositoryOwnerAndNameFromPath(t *testing.T) {
Name,
URI,
RepositoryOwner, RepositoryName,
RepositoryHost,
ErrorSubstring string
}{
{
Name: "valid url",
URI: "https://github.com/crhntr/hello-release",
RepositoryOwner: "crhntr", RepositoryName: "hello-release",
RepositoryOwner: "crhntr", RepositoryName: "hello-release", RepositoryHost: "github.com",
},
{
Name: "ssh url",
URI: "[email protected]:crhntr/hello-release.git",
RepositoryOwner: "crhntr", RepositoryName: "hello-release",
RepositoryOwner: "crhntr", RepositoryName: "hello-release", RepositoryHost: "github.com",
},
{
Name: "empty ssh path",
URI: "[email protected]:",
ErrorSubstring: "path missing expected parts",
},
{
Name: "github enterprise",
URI: "[email protected]:x/y.git",
RepositoryOwner: "x", RepositoryName: "y", RepositoryHost: "example.com",
},
{
Name: "not a valid ssh path",
URI: "[email protected]:?invalid_url?",
Expand All @@ -52,13 +58,24 @@ func Test_RepositoryOwnerAndNameFromPath(t *testing.T) {
},
} {
t.Run(tt.Name, func(t *testing.T) {
repoOwner, repoName, err := gh.RepositoryOwnerAndNameFromPath(tt.URI)
repoHost, repoOwner, repoName, err := gh.RepositoryHostOwnerAndNameFromPath(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)
assert.Equal(t, tt.RepositoryHost, repoHost)
}

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)
assert.Equal(t, tt.RepositoryHost, repoHost)
}
})
}
Expand Down
25 changes: 20 additions & 5 deletions pkg/cargo/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ type repositoryReleaseLister interface {

type (
RepositoryReleaseLister = repositoryReleaseLister
githubClientFunc func(Kilnfile, BOSHReleaseTarballLock) (repositoryReleaseLister, error)
githubClientFunc func(ctx context.Context, kilnfile Kilnfile, lock BOSHReleaseTarballLock) (repositoryReleaseLister, error)
)

func releaseNotes(ctx context.Context, kf Kilnfile, list BumpList, client githubClientFunc) (BumpList, error) {
Expand Down Expand Up @@ -184,9 +184,23 @@ func releaseNotes(ctx context.Context, kf Kilnfile, list BumpList, client github
}

func ReleaseNotes(ctx context.Context, kf Kilnfile, list BumpList) (BumpList, error) {
return releaseNotes(ctx, kf, list, func(kilnfile Kilnfile, lock BOSHReleaseTarballLock) (repositoryReleaseLister, error) {
return releaseNotes(ctx, kf, list, listerForRelease(kf))
}

func listerForRelease(kf Kilnfile) func(ctx context.Context, _ Kilnfile, lock BOSHReleaseTarballLock) (repositoryReleaseLister, error) {
return func(ctx context.Context, kilnfile Kilnfile, lock BOSHReleaseTarballLock) (repositoryReleaseLister, error) {
spec, err := kf.BOSHReleaseTarballSpecification(lock.Name)
if err != nil {
return nil, err
}

_, owner, _, err := gh.RepositoryHostOwnerAndNameFromPath(spec.GitHubRepository)
if err != nil {
return nil, err
}

i := slices.IndexFunc(kf.ReleaseSources, func(config ReleaseSourceConfig) bool {
return BOSHReleaseTarballSourceID(config) == lock.RemoteSource
return config.Type == BOSHReleaseTarballSourceTypeGithub && config.Org == owner
})
if i < 0 {
return nil, fmt.Errorf("release source with id %s not found", lock.RemoteSource)
Expand All @@ -197,7 +211,7 @@ func ReleaseNotes(ctx context.Context, kf Kilnfile, list BumpList) (BumpList, er
return nil, err
}
return client.Repositories, err
})
}
}

func fetchReleasesFromRepo(ctx context.Context, repoService RepositoryReleaseLister, repository string, from, to *semver.Version) []*github.RepositoryRelease {
Expand Down Expand Up @@ -226,8 +240,9 @@ func fetchReleasesFromRepo(ctx context.Context, repoService RepositoryReleaseLis
}

func fetchReleasesForBump(ctx context.Context, kf Kilnfile, bump Bump, client githubClientFunc) Bump {
lister, err := client(kf, bump.To)
lister, err := client(ctx, kf, bump.To)
if err != nil {
log.Println(err)
return bump
}
spec, err := kf.BOSHReleaseTarballSpecification(bump.Name)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cargo/bump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestInternal_addReleaseNotes(t *testing.T) {
To: BOSHReleaseTarballLock{Version: "10"},
From: BOSHReleaseTarballLock{Version: "9"},
},
}, func(kilnfile Kilnfile, lock BOSHReleaseTarballLock) (repositoryReleaseLister, error) {
}, func(ctx context.Context, kilnfile Kilnfile, lock BOSHReleaseTarballLock) (repositoryReleaseLister, error) {
return releaseLister, nil
})
please.Expect(err).NotTo(HaveOccurred())
Expand Down

0 comments on commit 835c7e3

Please sign in to comment.