Skip to content

Commit 40e7bc3

Browse files
authored
fix: get conventional commits from source repository (#1867)
Function `updateChangesSinceLastGeneration` may not be needed since we don't need to pass api changes to language container. Fixes #1866
1 parent 4cd4ae7 commit 40e7bc3

File tree

4 files changed

+169
-4
lines changed

4 files changed

+169
-4
lines changed

internal/librarian/generate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func newGenerateRunner(cfg *config.Config) (*generateRunner, error) {
126126
func (r *generateRunner) run(ctx context.Context) error {
127127
outputDir := filepath.Join(r.workRoot, "output")
128128
if err := os.Mkdir(outputDir, 0755); err != nil {
129-
return err
129+
return fmt.Errorf("failed to make output directory, %s: %w", outputDir, err)
130130
}
131131
slog.Info("Code will be generated", "dir", outputDir)
132132

@@ -219,7 +219,7 @@ func (r *generateRunner) needsConfigure() bool {
219219
func (r *generateRunner) updateChangesSinceLastGeneration(libraryID string) error {
220220
for _, library := range r.state.Libraries {
221221
if library.ID == libraryID {
222-
commits, err := GetConventionalCommitsSinceLastGeneration(r.repo, library)
222+
commits, err := GetConventionalCommitsSinceLastGeneration(r.sourceRepo, library)
223223
if err != nil {
224224
return fmt.Errorf("failed to fetch conventional commits for library, %s: %w", library.ID, err)
225225
}

internal/librarian/generate_test.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525
"testing"
2626

27+
"github.com/go-git/go-git/v5/plumbing"
2728
"github.com/google/go-cmp/cmp"
2829
"github.com/googleapis/librarian/internal/config"
2930
"github.com/googleapis/librarian/internal/gitrepo"
@@ -750,6 +751,7 @@ func TestGenerateScenarios(t *testing.T) {
750751
build: true,
751752
wantGenerateCalls: 1,
752753
wantErr: true,
754+
wantErrMsg: "failed to make output directory",
753755
},
754756
{
755757
name: "generate error",
@@ -873,6 +875,17 @@ func TestGenerateScenarios(t *testing.T) {
873875
t.Fatal(err)
874876
}
875877

878+
// Create a symlink in the output directory to trigger an error.
879+
if test.name == "symlink in output" {
880+
outputDir := filepath.Join(r.workRoot, "output")
881+
if err := os.MkdirAll(outputDir, 0755); err != nil {
882+
t.Fatalf("os.MkdirAll() = %v", err)
883+
}
884+
if err := os.Symlink("target", filepath.Join(outputDir, "symlink")); err != nil {
885+
t.Fatalf("os.Symlink() = %v", err)
886+
}
887+
}
888+
876889
err := r.run(context.Background())
877890
if test.wantErr {
878891
if err == nil {
@@ -1468,3 +1481,139 @@ func TestCompileRegexps(t *testing.T) {
14681481
})
14691482
}
14701483
}
1484+
1485+
func TestUpdateChangesSinceLastGeneration(t *testing.T) {
1486+
t.Parallel()
1487+
hash1 := plumbing.NewHash("1234567")
1488+
hash2 := plumbing.NewHash("abcdefg")
1489+
for _, test := range []struct {
1490+
name string
1491+
libraryID string
1492+
libraries []*config.LibraryState
1493+
repo gitrepo.Repository
1494+
want *config.LibraryState
1495+
wantErr bool
1496+
wantErrMsg string
1497+
}{
1498+
{
1499+
name: "update changes in a library",
1500+
libraryID: "another-id",
1501+
libraries: []*config.LibraryState{
1502+
{
1503+
ID: "example-d",
1504+
},
1505+
{
1506+
ID: "another-id",
1507+
LastGeneratedCommit: "fake-sha",
1508+
APIs: []*config.API{
1509+
{
1510+
Path: "api/one/path",
1511+
},
1512+
{
1513+
Path: "api/another/path",
1514+
},
1515+
},
1516+
},
1517+
},
1518+
repo: &MockRepository{
1519+
GetCommitsForPathsSinceLastGenByPath: map[string][]*gitrepo.Commit{
1520+
"api/one/path": {
1521+
{
1522+
Message: "feat: new feature\n\nThis is body.\n\nPiperOrigin-RevId: 98765",
1523+
Hash: hash1,
1524+
},
1525+
},
1526+
"api/another/path": {
1527+
{
1528+
Message: "fix: a bug fix\n\nThis is another body.\n\nPiperOrigin-RevId: 573342",
1529+
Hash: hash2,
1530+
},
1531+
},
1532+
},
1533+
ChangedFilesInCommitValueByHash: map[string][]string{
1534+
hash1.String(): {
1535+
"api/one/path/file.txt",
1536+
"api/another/path/example.txt",
1537+
},
1538+
hash2.String(): {
1539+
"api/one/path/another-file.txt",
1540+
"api/another/path/another-example.txt",
1541+
},
1542+
},
1543+
},
1544+
want: &config.LibraryState{
1545+
ID: "another-id",
1546+
LastGeneratedCommit: "fake-sha",
1547+
APIs: []*config.API{
1548+
{
1549+
Path: "api/one/path",
1550+
},
1551+
{
1552+
Path: "api/another/path",
1553+
},
1554+
},
1555+
Changes: []*config.Change{
1556+
{
1557+
Type: "feat",
1558+
Subject: "new feature",
1559+
Body: "This is body.",
1560+
ClNum: "98765",
1561+
CommitHash: hash1.String(),
1562+
},
1563+
{
1564+
Type: "fix",
1565+
Subject: "a bug fix",
1566+
Body: "This is another body.",
1567+
ClNum: "573342",
1568+
CommitHash: hash2.String(),
1569+
},
1570+
},
1571+
},
1572+
},
1573+
{
1574+
name: "failed to get conventional commits",
1575+
libraryID: "another-id",
1576+
libraries: []*config.LibraryState{
1577+
{
1578+
ID: "example-d",
1579+
},
1580+
{
1581+
ID: "another-id",
1582+
},
1583+
},
1584+
repo: &MockRepository{
1585+
GetCommitsForPathsSinceLastGenError: errors.New("simulated error"),
1586+
},
1587+
wantErr: true,
1588+
wantErrMsg: "failed to fetch conventional commits for library",
1589+
},
1590+
} {
1591+
t.Run(test.name, func(t *testing.T) {
1592+
runner := &generateRunner{
1593+
sourceRepo: test.repo,
1594+
state: &config.LibrarianState{
1595+
Libraries: test.libraries,
1596+
},
1597+
}
1598+
err := runner.updateChangesSinceLastGeneration(test.libraryID)
1599+
if test.wantErr {
1600+
if err == nil {
1601+
t.Error("updateChangesSinceLastGeneration() should fail")
1602+
}
1603+
if !strings.Contains(err.Error(), test.wantErrMsg) {
1604+
t.Errorf("want error message %s, got %s", test.wantErrMsg, err.Error())
1605+
}
1606+
1607+
return
1608+
}
1609+
1610+
if err != nil {
1611+
t.Errorf("updateChangesSinceLastGeneration() failed: %q", err)
1612+
}
1613+
1614+
if diff := cmp.Diff(test.want, runner.state.Libraries[1]); diff != "" {
1615+
t.Errorf("updateChangesSinceLastGeneration() dirs mismatch (-want +got):\n%s", diff)
1616+
}
1617+
})
1618+
}
1619+
}

internal/librarian/mocks_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ type MockRepository struct {
273273
GetCommitsForPathsSinceTagValueByTag map[string][]*gitrepo.Commit
274274
GetCommitsForPathsSinceTagError error
275275
GetCommitsForPathsSinceLastGenValue []*gitrepo.Commit
276+
GetCommitsForPathsSinceLastGenByPath map[string][]*gitrepo.Commit
276277
GetCommitsForPathsSinceLastGenError error
277278
ChangedFilesInCommitValue []string
278279
ChangedFilesInCommitValueByHash map[string][]string
@@ -327,6 +328,16 @@ func (m *MockRepository) GetCommitsForPathsSinceCommit(paths []string, sinceComm
327328
if m.GetCommitsForPathsSinceLastGenError != nil {
328329
return nil, m.GetCommitsForPathsSinceLastGenError
329330
}
331+
if m.GetCommitsForPathsSinceLastGenByPath != nil {
332+
allCommits := make([]*gitrepo.Commit, 0)
333+
for _, path := range paths {
334+
if commits, ok := m.GetCommitsForPathsSinceLastGenByPath[path]; ok {
335+
allCommits = append(allCommits, commits...)
336+
}
337+
}
338+
339+
return allCommits, nil
340+
}
330341
return m.GetCommitsForPathsSinceLastGenValue, nil
331342
}
332343

internal/librarian/release_please_lite.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ func GetConventionalCommitsSinceLastRelease(repo gitrepo.Repository, library *co
3939
}
4040

4141
// GetConventionalCommitsSinceLastGeneration returns all conventional commits for
42-
// the given library since the last generation.
42+
// all API paths in given library since the last generation.
4343
func GetConventionalCommitsSinceLastGeneration(repo gitrepo.Repository, library *config.LibraryState) ([]*conventionalcommits.ConventionalCommit, error) {
44-
commits, err := repo.GetCommitsForPathsSinceCommit(library.SourceRoots, library.LastGeneratedCommit)
44+
apiPaths := make([]string, 0)
45+
for _, oneAPI := range library.APIs {
46+
apiPaths = append(apiPaths, oneAPI.Path)
47+
}
48+
49+
commits, err := repo.GetCommitsForPathsSinceCommit(apiPaths, library.LastGeneratedCommit)
4550
if err != nil {
4651
return nil, fmt.Errorf("failed to get commits for library %s: %w", library.ID, err)
4752
}

0 commit comments

Comments
 (0)