Skip to content

Commit 7f80f98

Browse files
authored
refactor(librarian): move conventionalcommit to gitrepo (#2554)
Commit is in the name of the package so it makes sense that this code should be related to the git code. In a future PR I will remove the Commit type and/or replace it with ConventionalCommit. Updates: #2543
1 parent e236c7d commit 7f80f98

File tree

10 files changed

+94
-104
lines changed

10 files changed

+94
-104
lines changed

internal/config/state.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"regexp"
2121
"strings"
2222

23-
"github.com/googleapis/librarian/internal/conventionalcommits"
2423
"github.com/googleapis/librarian/internal/gitrepo"
2524
)
2625

@@ -114,7 +113,7 @@ type LibraryState struct {
114113
LastGeneratedCommit string `yaml:"last_generated_commit" json:"-"`
115114
// The changes from the language repository since the library was last released.
116115
// This field is ignored when writing to state.yaml.
117-
Changes []*conventionalcommits.ConventionalCommit `yaml:"-" json:"changes,omitempty"`
116+
Changes []*gitrepo.ConventionalCommit `yaml:"-" json:"changes,omitempty"`
118117
// A list of APIs that are part of this library.
119118
APIs []*API `yaml:"apis" json:"apis"`
120119
// A list of directories in the language repository where Librarian contributes code.

internal/docker/docker_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
"github.com/google/go-cmp/cmp"
2727
"github.com/googleapis/librarian/internal/config"
28-
"github.com/googleapis/librarian/internal/conventionalcommits"
28+
"github.com/googleapis/librarian/internal/gitrepo"
2929
)
3030

3131
func TestNew(t *testing.T) {
@@ -929,7 +929,7 @@ func TestReleaseInitRequestContent(t *testing.T) {
929929
ID: "my-library",
930930
Version: "1.1.0",
931931
ReleaseTriggered: true,
932-
Changes: []*conventionalcommits.ConventionalCommit{
932+
Changes: []*gitrepo.ConventionalCommit{
933933
{
934934
Type: "feat",
935935
Subject: "new feature",

internal/conventionalcommits/conventional_commits.go renamed to internal/gitrepo/conventional_commits.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package conventionalcommits
15+
package gitrepo
1616

1717
import (
1818
"encoding/json"
@@ -22,8 +22,6 @@ import (
2222
"regexp"
2323
"strings"
2424
"time"
25-
26-
"github.com/googleapis/librarian/internal/gitrepo"
2725
)
2826

2927
const (
@@ -125,7 +123,7 @@ func (c *ConventionalCommit) MarshalJSON() ([]byte, error) {
125123
// Malformed override or nested blocks (e.g., with a missing end marker) are
126124
// ignored. Any commit part that is found but fails to parse as a valid
127125
// conventional commit is logged and skipped.
128-
func ParseCommits(commit *gitrepo.Commit, libraryID string) ([]*ConventionalCommit, error) {
126+
func ParseCommits(commit *Commit, libraryID string) ([]*ConventionalCommit, error) {
129127
message := commit.Message
130128
if strings.TrimSpace(message) == "" {
131129
return nil, ErrEmptyCommitMessage
@@ -196,7 +194,7 @@ func extractCommitParts(message string) []commitPart {
196194

197195
// parseSimpleCommit parses a simple commit message and returns a slice of ConventionalCommit.
198196
// A simple commit message is commit that does not include override or nested commits.
199-
func parseSimpleCommit(commitPart commitPart, commit *gitrepo.Commit, libraryID string) ([]*ConventionalCommit, error) {
197+
func parseSimpleCommit(commitPart commitPart, commit *Commit, libraryID string) ([]*ConventionalCommit, error) {
200198
trimmedMessage := strings.TrimSpace(commitPart.message)
201199
if trimmedMessage == "" {
202200
return nil, fmt.Errorf("empty commit message")

internal/conventionalcommits/conventional_commits_test.go renamed to internal/gitrepo/conventional_commits_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package conventionalcommits
15+
package gitrepo
1616

1717
import (
1818
"strings"
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/go-git/go-git/v5/plumbing"
2323
"github.com/google/go-cmp/cmp"
24-
"github.com/googleapis/librarian/internal/gitrepo"
2524
)
2625

2726
func TestParseCommits(t *testing.T) {
@@ -546,7 +545,7 @@ END_COMMIT_OVERRIDE`,
546545
},
547546
} {
548547
t.Run(test.name, func(t *testing.T) {
549-
commit := &gitrepo.Commit{
548+
commit := &Commit{
550549
Message: test.message,
551550
Hash: plumbing.NewHash("fake-sha"),
552551
When: now,

internal/librarian/commit_version_analyzer.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ import (
2121
"strings"
2222

2323
"github.com/googleapis/librarian/internal/config"
24-
"github.com/googleapis/librarian/internal/conventionalcommits"
2524
"github.com/googleapis/librarian/internal/gitrepo"
2625
"github.com/googleapis/librarian/internal/semver"
2726
)
2827

2928
// getConventionalCommitsSinceLastRelease returns all conventional commits for the given library since the
3029
// version specified in the state file. The repo should be the language repo.
31-
func getConventionalCommitsSinceLastRelease(repo gitrepo.Repository, library *config.LibraryState, tag string) ([]*conventionalcommits.ConventionalCommit, error) {
30+
func getConventionalCommitsSinceLastRelease(repo gitrepo.Repository, library *config.LibraryState, tag string) ([]*gitrepo.ConventionalCommit, error) {
3231
commits, err := repo.GetCommitsForPathsSinceTag(library.SourceRoots, tag)
3332

3433
if err != nil {
@@ -59,10 +58,10 @@ func shouldIncludeForRelease(files, sourceRoots, excludePaths []string) bool {
5958
// getConventionalCommitsSinceLastGeneration returns all conventional commits for
6059
// all API paths in given library since the last generation. The repo input should
6160
// be the googleapis source repo.
62-
func getConventionalCommitsSinceLastGeneration(sourceRepo, languageRepo gitrepo.Repository, library *config.LibraryState, lastGenCommit string) ([]*conventionalcommits.ConventionalCommit, error) {
61+
func getConventionalCommitsSinceLastGeneration(sourceRepo, languageRepo gitrepo.Repository, library *config.LibraryState, lastGenCommit string) ([]*gitrepo.ConventionalCommit, error) {
6362
if lastGenCommit == "" {
6463
slog.Info("the last generation commit is empty, skip fetching conventional commits", "library", library.ID)
65-
return make([]*conventionalcommits.ConventionalCommit, 0), nil
64+
return make([]*gitrepo.ConventionalCommit, 0), nil
6665
}
6766

6867
apiPaths := make([]string, 0)
@@ -131,8 +130,8 @@ func shouldIncludeForGeneration(sourceFiles, languageRepoFiles []string, library
131130
}
132131

133132
// libraryFilter filters a list of conventional commits by library ID.
134-
func libraryFilter(commits []*conventionalcommits.ConventionalCommit, libraryID string) []*conventionalcommits.ConventionalCommit {
135-
var filteredCommits []*conventionalcommits.ConventionalCommit
133+
func libraryFilter(commits []*gitrepo.ConventionalCommit, libraryID string) []*gitrepo.ConventionalCommit {
134+
var filteredCommits []*gitrepo.ConventionalCommit
136135
for _, commit := range commits {
137136
if libraryIDs, ok := commit.Footers["Library-IDs"]; ok {
138137
ids := strings.Split(libraryIDs, ",")
@@ -152,8 +151,8 @@ func libraryFilter(commits []*conventionalcommits.ConventionalCommit, libraryID
152151
// convertToConventionalCommits converts a list of commits in a git repo into a list
153152
// of conventional commits. The filesFilter parameter is custom filter out non-matching
154153
// files depending on a generation or a release change.
155-
func convertToConventionalCommits(sourceRepo gitrepo.Repository, library *config.LibraryState, commits []*gitrepo.Commit, filesFilter func(files []string) bool) ([]*conventionalcommits.ConventionalCommit, error) {
156-
var conventionalCommits []*conventionalcommits.ConventionalCommit
154+
func convertToConventionalCommits(sourceRepo gitrepo.Repository, library *config.LibraryState, commits []*gitrepo.Commit, filesFilter func(files []string) bool) ([]*gitrepo.ConventionalCommit, error) {
155+
var conventionalCommits []*gitrepo.ConventionalCommit
157156
for _, commit := range commits {
158157
files, err := sourceRepo.ChangedFilesInCommit(commit.Hash.String())
159158
if err != nil {
@@ -162,7 +161,7 @@ func convertToConventionalCommits(sourceRepo gitrepo.Repository, library *config
162161
if !filesFilter(files) {
163162
continue
164163
}
165-
parsedCommits, err := conventionalcommits.ParseCommits(commit, library.ID)
164+
parsedCommits, err := gitrepo.ParseCommits(commit, library.ID)
166165
if err != nil {
167166
return nil, fmt.Errorf("failed to parse commit %s: %w", commit.Hash.String(), err)
168167
}
@@ -195,13 +194,13 @@ func isUnderAnyPath(file string, paths []string) bool {
195194
}
196195

197196
// NextVersion calculates the next semantic version based on a slice of conventional commits.
198-
func NextVersion(commits []*conventionalcommits.ConventionalCommit, currentVersion string) (string, error) {
197+
func NextVersion(commits []*gitrepo.ConventionalCommit, currentVersion string) (string, error) {
199198
highestChange := getHighestChange(commits)
200199
return semver.DeriveNext(highestChange, currentVersion)
201200
}
202201

203202
// getHighestChange determines the highest-ranking change type from a slice of commits.
204-
func getHighestChange(commits []*conventionalcommits.ConventionalCommit) semver.ChangeLevel {
203+
func getHighestChange(commits []*gitrepo.ConventionalCommit) semver.ChangeLevel {
205204
highestChange := semver.None
206205
for _, commit := range commits {
207206
var currentChange semver.ChangeLevel

0 commit comments

Comments
 (0)