Skip to content

Commit da195dc

Browse files
authored
fix(librarian): filter commits by library id (#2537)
Fixes #2529
1 parent 5c95940 commit da195dc

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

internal/librarian/commit_version_analyzer.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ func shouldIncludeForGeneration(files, apiPaths []string) bool {
9696
return false
9797
}
9898

99+
// libraryFilter filters a list of conventional commits by library ID.
100+
func libraryFilter(commits []*conventionalcommits.ConventionalCommit, libraryID string) []*conventionalcommits.ConventionalCommit {
101+
var filteredCommits []*conventionalcommits.ConventionalCommit
102+
for _, commit := range commits {
103+
if libraryIDs, ok := commit.Footers["Library-IDs"]; ok {
104+
ids := strings.Split(libraryIDs, ",")
105+
for _, id := range ids {
106+
if strings.TrimSpace(id) == libraryID {
107+
filteredCommits = append(filteredCommits, commit)
108+
break
109+
}
110+
}
111+
} else if commit.LibraryID == libraryID {
112+
filteredCommits = append(filteredCommits, commit)
113+
}
114+
}
115+
return filteredCommits
116+
}
117+
99118
// convertToConventionalCommits converts a list of commits in a git repo into a list
100119
// of conventional commits. The filesFilter parameter is custom filter out non-matching
101120
// files depending on a generation or a release change.
@@ -116,6 +135,9 @@ func convertToConventionalCommits(repo gitrepo.Repository, library *config.Libra
116135
if parsedCommits == nil {
117136
continue
118137
}
138+
139+
parsedCommits = libraryFilter(parsedCommits, library.ID)
140+
119141
for _, pc := range parsedCommits {
120142
pc.CommitHash = commit.Hash.String()
121143
}

internal/librarian/commit_version_analyzer_test.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ func TestShouldIncludeForGeneration(t *testing.T) {
180180
}
181181

182182
func TestGetConventionalCommitsSinceLastRelease(t *testing.T) {
183+
183184
t.Parallel()
185+
184186
pathAndMessages := []pathAndMessage{
185187
{
186188
path: "foo/a.txt",
@@ -202,8 +204,46 @@ func TestGetConventionalCommitsSinceLastRelease(t *testing.T) {
202204
path: "foo/c.txt",
203205
message: "feat(foo): another feature for foo",
204206
},
207+
{
208+
path: "foo/something.txt",
209+
message: `BEGIN_COMMIT_OVERRIDE
210+
211+
BEGIN_NESTED_COMMIT
212+
fix: a bug1 fix
213+
214+
This is another body.
215+
216+
PiperOrigin-RevId: 573342
217+
Library-IDs: foo
218+
Source-link: [googleapis/googleapis@fedcba0](https://github.com/googleapis/googleapis/commit/fedcba0)
219+
END_NESTED_COMMIT
220+
221+
BEGIN_NESTED_COMMIT
222+
fix: a bug2 fix
223+
224+
This is another body.
225+
226+
PiperOrigin-RevId: 573342
227+
Library-IDs: bar
228+
Source-link: [googleapis/googleapis@fedcba0](https://github.com/googleapis/googleapis/commit/fedcba0)
229+
END_NESTED_COMMIT
230+
231+
BEGIN_NESTED_COMMIT
232+
fix: a bug3 fix
233+
234+
This is another body.
235+
236+
PiperOrigin-RevId: 573342
237+
Library-IDs: foo, bar
238+
Source-link: [googleapis/googleapis@fedcba0](https://github.com/googleapis/googleapis/commit/fedcba0)
239+
END_NESTED_COMMIT
240+
241+
END_COMMIT_OVERRIDE`,
242+
},
205243
}
244+
206245
repoWithCommits := setupRepoForGetCommits(t, pathAndMessages, []string{"foo-v1.0.0"})
246+
207247
for _, test := range []struct {
208248
name string
209249
repo gitrepo.Repository
@@ -223,6 +263,28 @@ func TestGetConventionalCommitsSinceLastRelease(t *testing.T) {
223263
ReleaseExcludePaths: []string{"foo/README.md"},
224264
},
225265
want: []*conventionalcommits.ConventionalCommit{
266+
{
267+
Type: "fix",
268+
Subject: "a bug1 fix",
269+
LibraryID: "foo",
270+
Footers: map[string]string{
271+
"PiperOrigin-RevId": "573342",
272+
"Library-IDs": "foo",
273+
"Source-link": "[googleapis/googleapis@fedcba0](https://github.com/googleapis/googleapis/commit/fedcba0)",
274+
},
275+
IsNested: true,
276+
},
277+
{
278+
Type: "fix",
279+
Subject: "a bug3 fix",
280+
LibraryID: "foo",
281+
Footers: map[string]string{
282+
"PiperOrigin-RevId": "573342",
283+
"Library-IDs": "foo, bar",
284+
"Source-link": "[googleapis/googleapis@fedcba0](https://github.com/googleapis/googleapis/commit/fedcba0)",
285+
},
286+
IsNested: true,
287+
},
226288
{
227289
Type: "feat",
228290
Scope: "foo",
@@ -574,3 +636,75 @@ func TestNextVersion(t *testing.T) {
574636
})
575637
}
576638
}
639+
640+
func TestLibraryFilter(t *testing.T) {
641+
t.Parallel()
642+
commits := []*conventionalcommits.ConventionalCommit{
643+
{
644+
LibraryID: "foo",
645+
Footers: map[string]string{},
646+
},
647+
{
648+
LibraryID: "bar",
649+
Footers: map[string]string{},
650+
},
651+
{
652+
Footers: map[string]string{
653+
"Library-IDs": "foo",
654+
},
655+
},
656+
{
657+
Footers: map[string]string{
658+
"Library-IDs": "bar",
659+
},
660+
},
661+
{
662+
Footers: map[string]string{
663+
"Library-IDs": "foo, bar",
664+
},
665+
},
666+
{
667+
Footers: map[string]string{
668+
"Library-IDs": "foo,bar",
669+
},
670+
},
671+
}
672+
for _, test := range []struct {
673+
name string
674+
libraryID string
675+
want []*conventionalcommits.ConventionalCommit
676+
}{
677+
{
678+
name: "filter by foo",
679+
libraryID: "foo",
680+
want: []*conventionalcommits.ConventionalCommit{
681+
commits[0],
682+
commits[2],
683+
commits[4],
684+
commits[5],
685+
},
686+
},
687+
{
688+
name: "filter by bar",
689+
libraryID: "bar",
690+
want: []*conventionalcommits.ConventionalCommit{
691+
commits[1],
692+
commits[3],
693+
commits[4],
694+
commits[5],
695+
},
696+
},
697+
{
698+
name: "filter by baz",
699+
libraryID: "baz",
700+
want: nil,
701+
},
702+
} {
703+
t.Run(test.name, func(t *testing.T) {
704+
got := libraryFilter(commits, test.libraryID)
705+
if diff := cmp.Diff(test.want, got); diff != "" {
706+
t.Errorf("libraryFilter() mismatch (-want +got):\n%s", diff)
707+
}
708+
})
709+
}
710+
}

0 commit comments

Comments
 (0)