Skip to content

Commit 8756e01

Browse files
authored
feat: get library changes in release init command (#1780)
Updates #1008
1 parent a0973af commit 8756e01

File tree

6 files changed

+599
-314
lines changed

6 files changed

+599
-314
lines changed

internal/config/state.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ type LibraryState struct {
9494
Version string `yaml:"version" json:"version"`
9595
// The commit hash from the API definition repository at which the library was last generated.
9696
LastGeneratedCommit string `yaml:"last_generated_commit" json:"last_generated_commit"`
97+
// The changes from the language repository since the library was last released.
98+
// This field is ignored when writing to state.yaml.
99+
Changes []*Change `yaml:"-" json:"changes,omitempty"`
97100
// A list of APIs that are part of this library.
98101
APIs []*API `yaml:"apis" json:"apis"`
99102
// A list of directories in the language repository where Librarian contributes code.
@@ -213,6 +216,20 @@ func (a *API) Validate() error {
213216
return nil
214217
}
215218

219+
// Change represents the changelog of a library.
220+
type Change struct {
221+
// The type of the change, should be one of the conventional type.
222+
Type string `yaml:"type" json:"type"`
223+
// The summary of the change.
224+
Subject string `yaml:"subject" json:"subject"`
225+
// The body of the change.
226+
Body string `yaml:"body" json:"body"`
227+
// The Changelist number in piper associated with this change.
228+
ClNum string `yaml:"piper_cl_number" json:"piper_cl_number"`
229+
// The commit hash in the source repository associated with this change.
230+
CommitHash string `yaml:"source_commit_hash" json:"source_commit_hash"`
231+
}
232+
216233
// invalidPathChars contains characters that are invalid in path components,
217234
// plus path separators and the null byte.
218235
const invalidPathChars = "<>:\"|?*/\\\x00"

internal/librarian/init_test.go

Lines changed: 0 additions & 234 deletions
This file was deleted.

internal/librarian/librarian_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@
1515
package librarian
1616

1717
import (
18+
"fmt"
1819
"log"
20+
"math/rand"
1921
"os"
2022
"os/exec"
2123
"path/filepath"
2224
"testing"
2325

26+
"github.com/go-git/go-git/v5"
27+
"github.com/go-git/go-git/v5/plumbing/object"
28+
2429
"github.com/googleapis/librarian/internal/config"
2530
"github.com/googleapis/librarian/internal/gitrepo"
2631
"gopkg.in/yaml.v3"
@@ -169,3 +174,68 @@ func runGit(t *testing.T, dir string, args ...string) {
169174
t.Fatalf("git %v: %v", args, err)
170175
}
171176
}
177+
178+
// setupRepoForGetCommits creates an empty gitrepo and creates some commits and
179+
// tags.
180+
//
181+
// Each commit has a file path and a commit message.
182+
// Note that pathAndMessages should at least have one element. All tags are created
183+
// after the first commit.
184+
func setupRepoForGetCommits(t *testing.T, pathAndMessages []pathAndMessage, tags []string) *gitrepo.LocalRepository {
185+
t.Helper()
186+
dir := t.TempDir()
187+
gitRepo, err := git.PlainInit(dir, false)
188+
if err != nil {
189+
t.Fatalf("git.PlainInit failed: %v", err)
190+
}
191+
192+
createAndCommit := func(path, msg string) {
193+
w, err := gitRepo.Worktree()
194+
if err != nil {
195+
t.Fatalf("Worktree() failed: %v", err)
196+
}
197+
fullPath := filepath.Join(dir, path)
198+
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
199+
t.Fatalf("os.MkdirAll failed: %v", err)
200+
}
201+
content := fmt.Sprintf("content-%d", rand.Intn(10000))
202+
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
203+
t.Fatalf("os.WriteFile failed: %v", err)
204+
}
205+
if _, err := w.Add(path); err != nil {
206+
t.Fatalf("w.Add failed: %v", err)
207+
}
208+
_, err = w.Commit(msg, &git.CommitOptions{
209+
Author: &object.Signature{Name: "Test", Email: "[email protected]"},
210+
})
211+
if err != nil {
212+
t.Fatalf("w.Commit failed: %v", err)
213+
}
214+
}
215+
216+
createAndCommit(pathAndMessages[0].path, pathAndMessages[0].message)
217+
head, err := gitRepo.Head()
218+
if err != nil {
219+
t.Fatalf("repo.Head() failed: %v", err)
220+
}
221+
for _, tag := range tags {
222+
if _, err := gitRepo.CreateTag(tag, head.Hash(), nil); err != nil {
223+
t.Fatalf("CreateTag failed: %v", err)
224+
}
225+
}
226+
227+
for _, pam := range pathAndMessages[1:] {
228+
createAndCommit(pam.path, pam.message)
229+
}
230+
231+
r, err := gitrepo.NewRepository(&gitrepo.RepositoryOptions{Dir: dir})
232+
if err != nil {
233+
t.Fatalf("gitrepo.NewRepository failed: %v", err)
234+
}
235+
return r
236+
}
237+
238+
type pathAndMessage struct {
239+
path string
240+
message string
241+
}

0 commit comments

Comments
 (0)