Skip to content

Commit 1c71bd9

Browse files
authored
fix: parse github remote from local directory (#2328)
Fixes #2327
1 parent 756e72f commit 1c71bd9

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

e2e_test.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,19 +590,29 @@ func TestReleaseInit(t *testing.T) {
590590
}
591591

592592
func TestReleaseTagAndRelease(t *testing.T) {
593-
t.Parallel()
594593
for _, test := range []struct {
595-
name string
596-
prBody string
597-
push bool
598-
wantErr bool
594+
name string
595+
prBody string
596+
repoPath string
597+
repoURL string
598+
push bool
599+
wantErr bool
599600
}{
600601
{
601602
name: "runs successfully",
602603
prBody: `<details><summary>go-google-cloud-pubsub-v1: v1.0.1</summary>
604+
### Features
605+
- feat: new feature
606+
</details>`,
607+
repoURL: "https://github.com/googleapis/librarian",
608+
},
609+
{
610+
name: "runs successfully from cloned repo",
611+
prBody: `<details><summary>go-google-cloud-pubsub-v1: v1.0.1</summary>
603612
### Features
604613
- feat: new feature
605614
</details>`,
615+
repoPath: "testdata/e2e/release/init/single_commit",
606616
},
607617
} {
608618
t.Run(test.name, func(t *testing.T) {
@@ -709,12 +719,20 @@ libraries:
709719
}))
710720
defer server.Close()
711721

722+
repo := test.repoURL
723+
if test.repoPath != "" {
724+
repo = t.TempDir()
725+
err := initRepo(t, repo, test.repoPath)
726+
if err != nil {
727+
t.Fatalf("error initializing fake git repo %s", err)
728+
}
729+
}
712730
cmdArgs := []string{
713731
"run",
714732
"github.com/googleapis/librarian/cmd/librarian",
715733
"release",
716734
"tag-and-release",
717-
"--repo=https://github.com/googleapis/librarian",
735+
fmt.Sprintf("--repo=%s", repo),
718736
fmt.Sprintf("--github-api-endpoint=%s/", server.URL),
719737
"--pr=https://github.com/googleapis/librarian/pull/123",
720738
}
@@ -775,6 +793,7 @@ func newMockGitHubServer(t *testing.T, prTitleFragment string) *httptest.Server
775793
// initRepo initiates a git repo in the given directory, copy
776794
// files from source directory and create a commit.
777795
func initRepo(t *testing.T, dir, source string) error {
796+
t.Logf("initializing repo, dir %s, source %s", dir, source)
778797
if err := os.CopyFS(dir, os.DirFS(source)); err != nil {
779798
return err
780799
}

internal/librarian/tag_and_release.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"log/slog"
2222
"net/url"
23+
"path/filepath"
2324
"regexp"
2425
"slices"
2526
"strconv"
@@ -28,6 +29,7 @@ import (
2829

2930
"github.com/googleapis/librarian/internal/config"
3031
"github.com/googleapis/librarian/internal/github"
32+
"github.com/googleapis/librarian/internal/gitrepo"
3133
)
3234

3335
const (
@@ -51,7 +53,7 @@ func newTagAndReleaseRunner(cfg *config.Config) (*tagAndReleaseRunner, error) {
5153
if cfg.GitHubToken == "" {
5254
return nil, fmt.Errorf("`LIBRARIAN_GITHUB_TOKEN` must be set")
5355
}
54-
repo, err := github.ParseRemote(cfg.Repo)
56+
repo, err := parseRemote(cfg.Repo)
5557
if err != nil {
5658
return nil, err
5759
}
@@ -71,6 +73,24 @@ func newTagAndReleaseRunner(cfg *config.Config) (*tagAndReleaseRunner, error) {
7173
}, nil
7274
}
7375

76+
func parseRemote(repo string) (*github.Repository, error) {
77+
if isURL(repo) {
78+
return github.ParseRemote(repo)
79+
}
80+
// repo is a directory
81+
absRepoRoot, err := filepath.Abs(repo)
82+
if err != nil {
83+
return nil, err
84+
}
85+
githubRepo, err := gitrepo.NewRepository(&gitrepo.RepositoryOptions{
86+
Dir: absRepoRoot,
87+
})
88+
if err != nil {
89+
return nil, err
90+
}
91+
return github.FetchGitHubRepoFromRemote(githubRepo)
92+
}
93+
7494
func (r *tagAndReleaseRunner) run(ctx context.Context) error {
7595
slog.Info("running tag-and-release command")
7696
prs, err := r.determinePullRequestsToProcess(ctx)

0 commit comments

Comments
 (0)