|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package librarian |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/googleapis/librarian/internal/config" |
| 22 | + "github.com/googleapis/librarian/internal/gitrepo" |
| 23 | +) |
| 24 | + |
| 25 | +const defaultTagFormat = "{id}-{version}" |
| 26 | + |
| 27 | +// GetConventionalCommitsSinceLastRelease returns all conventional commits for the given library since the |
| 28 | +// version specified in the state file. |
| 29 | +func GetConventionalCommitsSinceLastRelease(repo gitrepo.Repository, library *config.LibraryState) ([]*gitrepo.ConventionalCommit, error) { |
| 30 | + tag := formatTag(library) |
| 31 | + commits, err := repo.GetCommitsForPathsSinceTag(library.SourceRoots, tag) |
| 32 | + if err != nil { |
| 33 | + return nil, fmt.Errorf("failed to get commits for library %s: %w", library.ID, err) |
| 34 | + } |
| 35 | + var conventionalCommits []*gitrepo.ConventionalCommit |
| 36 | + for _, commit := range commits { |
| 37 | + files, err := repo.ChangedFilesInCommit(commit.Hash.String()) |
| 38 | + if err != nil { |
| 39 | + return nil, fmt.Errorf("failed to get changed files for commit %s: %w", commit.Hash.String(), err) |
| 40 | + } |
| 41 | + if shouldExclude(files, library.ReleaseExcludePaths) { |
| 42 | + continue |
| 43 | + } |
| 44 | + conventionalCommit, err := gitrepo.ParseCommit(commit.Message, commit.Hash.String()) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("failed to parse commit %s: %w", commit.Hash.String(), err) |
| 47 | + } |
| 48 | + conventionalCommits = append(conventionalCommits, conventionalCommit) |
| 49 | + } |
| 50 | + return conventionalCommits, nil |
| 51 | +} |
| 52 | + |
| 53 | +// shouldExclude determines if a commit should be excluded from a release. |
| 54 | +// It returns true if all files in the commit match one of the exclude paths. |
| 55 | +func shouldExclude(files, excludePaths []string) bool { |
| 56 | + for _, file := range files { |
| 57 | + excluded := false |
| 58 | + for _, excludePath := range excludePaths { |
| 59 | + if strings.HasPrefix(file, excludePath) { |
| 60 | + excluded = true |
| 61 | + break |
| 62 | + } |
| 63 | + } |
| 64 | + if !excluded { |
| 65 | + return false |
| 66 | + } |
| 67 | + } |
| 68 | + return true |
| 69 | +} |
| 70 | + |
| 71 | +// formatTag returns the git tag for a given library version. |
| 72 | +func formatTag(library *config.LibraryState) string { |
| 73 | + tagFormat := library.TagFormat |
| 74 | + if tagFormat == "" { |
| 75 | + tagFormat = defaultTagFormat |
| 76 | + } |
| 77 | + r := strings.NewReplacer("{id}", library.ID, "{version}", library.Version) |
| 78 | + return r.Replace(tagFormat) |
| 79 | +} |
0 commit comments