Skip to content

Commit 7f365a4

Browse files
authored
fix(internal/librarian): use source repo if api-source flag is not specified (#1633)
Fixes #1631
1 parent 29881b8 commit 7f365a4

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

internal/librarian/generate.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,10 @@ func newGenerateRunner(cfg *config.Config) (*generateRunner, error) {
111111
return nil, err
112112
}
113113

114-
apiSource := cfg.APISource
115-
if apiSource == "" {
116-
apiSource = "https://github.com/googleapis/googleapis"
114+
if cfg.APISource == "" {
115+
cfg.APISource = "https://github.com/googleapis/googleapis"
117116
}
118-
sourceRepo, err := cloneOrOpenRepo(workRoot, apiSource, cfg.CI)
117+
sourceRepo, err := cloneOrOpenRepo(workRoot, cfg.APISource, cfg.CI)
119118
if err != nil {
120119
return nil, err
121120
}
@@ -255,7 +254,7 @@ func (r *generateRunner) runGenerateCommand(ctx context.Context, libraryID, outp
255254
if findLibraryByID(r.state, libraryID) == nil {
256255
return "", fmt.Errorf("library %q not configured yet, generation stopped", libraryID)
257256
}
258-
apiRoot, err := filepath.Abs(r.cfg.APISource)
257+
apiRoot, err := filepath.Abs(r.sourceRepo.GetDir())
259258
if err != nil {
260259
return "", err
261260
}

internal/librarian/generate_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func TestRunGenerateCommand(t *testing.T) {
7171
APISource: t.TempDir(),
7272
},
7373
repo: test.repo,
74+
sourceRepo: newTestGitRepo(t),
7475
ghClient: test.ghClient,
7576
state: test.state,
7677
containerClient: test.container,
@@ -387,7 +388,6 @@ func TestNewGenerateRunner(t *testing.T) {
387388
{
388389
name: "missing image",
389390
cfg: &config.Config{
390-
391391
API: "some/api",
392392
APISource: t.TempDir(),
393393
Repo: "https://github.com/googleapis/librarian.git",
@@ -406,6 +406,16 @@ func TestNewGenerateRunner(t *testing.T) {
406406
GitHubToken: "gh-token",
407407
},
408408
},
409+
{
410+
name: "empty API source",
411+
cfg: &config.Config{
412+
API: "some/api",
413+
APISource: "", // This will trigger the clone of googleapis
414+
Repo: newTestGitRepo(t).GetDir(),
415+
WorkRoot: t.TempDir(),
416+
Image: "gcr.io/test/test-image",
417+
},
418+
},
409419
{
410420
name: "clone googleapis fails",
411421
cfg: &config.Config{
@@ -456,7 +466,7 @@ func TestNewGenerateRunner(t *testing.T) {
456466

457467
if test.cfg.APISource == "" && test.cfg.WorkRoot != "" {
458468
if test.name == "clone googleapis fails" {
459-
// The function will try to clone googleapis into the workroot.
469+
// The function will try to clone googleapis into the current work directory.
460470
// To make it fail, create a non-empty, non-git directory.
461471
googleapisDir := filepath.Join(test.cfg.WorkRoot, "googleapis")
462472
if err := os.MkdirAll(googleapisDir, 0755); err != nil {
@@ -466,7 +476,7 @@ func TestNewGenerateRunner(t *testing.T) {
466476
t.Fatalf("os.WriteFile() = %v", err)
467477
}
468478
} else {
469-
// The function will try to clone googleapis into the workroot.
479+
// The function will try to clone googleapis into the current work directory.
470480
// To prevent a real clone, we can pre-create a fake googleapis repo.
471481
googleapisDir := filepath.Join(test.cfg.WorkRoot, "googleapis")
472482
if err := os.MkdirAll(googleapisDir, 0755); err != nil {

0 commit comments

Comments
 (0)