Skip to content

Commit 8f409cc

Browse files
authored
feat: get commit history since last library generation (#1837)
Fixes #1706
1 parent 1f3b20a commit 8f409cc

File tree

7 files changed

+312
-297
lines changed

7 files changed

+312
-297
lines changed

internal/gitrepo/gitrepo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Repository interface {
3939
HeadHash() (string, error)
4040
ChangedFilesInCommit(commitHash string) ([]string, error)
4141
GetCommitsForPathsSinceTag(paths []string, tagName string) ([]*Commit, error)
42+
GetCommitsForPathsSinceCommit(paths []string, sinceCommit string) ([]*Commit, error)
4243
CreateBranchAndCheckout(name string) error
4344
Push(branchName string) error
4445
}

internal/librarian/command.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"strings"
2727
"time"
2828

29+
"github.com/googleapis/librarian/internal/conventionalcommits"
30+
2931
"github.com/googleapis/librarian/internal/docker"
3032

3133
"github.com/googleapis/librarian/internal/config"
@@ -276,6 +278,27 @@ func copyLibrary(dst, src string, library *config.LibraryState) error {
276278
return nil
277279
}
278280

281+
func coerceLibraryChanges(commits []*conventionalcommits.ConventionalCommit) []*config.Change {
282+
changes := make([]*config.Change, 0)
283+
for _, commit := range commits {
284+
clNum := ""
285+
if cl, ok := commit.Footers[KeyClNum]; ok {
286+
clNum = cl
287+
}
288+
289+
changeType := getChangeType(commit)
290+
changes = append(changes, &config.Change{
291+
Type: changeType,
292+
Subject: commit.Description,
293+
Body: commit.Body,
294+
ClNum: clNum,
295+
CommitHash: commit.SHA,
296+
})
297+
}
298+
299+
return changes
300+
}
301+
279302
// commitAndPush creates a commit and push request to GitHub for the generated
280303
// changes.
281304
// It uses the GitHub client to create a PR with the specified branch, title, and

internal/librarian/generate.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (r *generateRunner) generateSingleLibrary(ctx context.Context, libraryID, o
190190
// libraries interfering with each other, and makes it easier to see what
191191
// was generated for each library when debugging.
192192
libraryOutputDir := filepath.Join(outputDir, libraryID)
193-
if err := os.Mkdir(libraryOutputDir, 0755); err != nil {
193+
if err := os.MkdirAll(libraryOutputDir, 0755); err != nil {
194194
return err
195195
}
196196

@@ -199,6 +199,10 @@ func (r *generateRunner) generateSingleLibrary(ctx context.Context, libraryID, o
199199
return err
200200
}
201201

202+
if err := r.updateChangesSinceLastGeneration(generatedLibraryID); err != nil {
203+
return err
204+
}
205+
202206
if err := r.runBuildCommand(ctx, generatedLibraryID); err != nil {
203207
return err
204208
}
@@ -212,6 +216,21 @@ func (r *generateRunner) needsConfigure() bool {
212216
return r.cfg.API != "" && r.cfg.Library != "" && findLibraryByID(r.state, r.cfg.Library) == nil
213217
}
214218

219+
func (r *generateRunner) updateChangesSinceLastGeneration(libraryID string) error {
220+
for _, library := range r.state.Libraries {
221+
if library.ID == libraryID {
222+
commits, err := GetConventionalCommitsSinceLastGeneration(r.repo, library)
223+
if err != nil {
224+
return fmt.Errorf("failed to fetch conventional commits for library, %s: %w", library.ID, err)
225+
}
226+
library.Changes = coerceLibraryChanges(commits)
227+
break
228+
}
229+
}
230+
231+
return nil
232+
}
233+
215234
func (r *generateRunner) updateLastGeneratedCommitState(libraryID string) error {
216235
hash, err := r.sourceRepo.HeadHash()
217236
if err != nil {

0 commit comments

Comments
 (0)