Skip to content

Commit 7eee2b5

Browse files
authored
refactor(librarian): decouple commits data type in state (#2572)
This package should have its own representation of commits that is simplified down to just the information it needs. Updates: #2543
1 parent 10493ed commit 7eee2b5

File tree

6 files changed

+74
-55
lines changed

6 files changed

+74
-55
lines changed

internal/config/state.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ type LibraryState struct {
113113
LastGeneratedCommit string `yaml:"last_generated_commit" json:"-"`
114114
// The changes from the language repository since the library was last released.
115115
// This field is ignored when writing to state.yaml.
116-
Changes []*gitrepo.ConventionalCommit `yaml:"-" json:"changes,omitempty"`
116+
Changes []*Commit `yaml:"-" json:"changes,omitempty"`
117117
// A list of APIs that are part of this library.
118118
APIs []*API `yaml:"apis" json:"apis"`
119119
// A list of directories in the language repository where Librarian contributes code.
@@ -142,6 +142,20 @@ type LibraryState struct {
142142
ErrorMessage string `yaml:"-" json:"error,omitempty"`
143143
}
144144

145+
// Commit represents a single commit in the release notes.
146+
type Commit struct {
147+
// Type is the type of change (e.g., "feat", "fix", "docs").
148+
Type string `json:"type"`
149+
// Subject is the short summary of the change.
150+
Subject string `json:"subject"`
151+
// Body is the long-form description of the change.
152+
Body string `json:"body"`
153+
// CommitHash is the full commit hash.
154+
CommitHash string `json:"commit_hash,omitempty"`
155+
// PiperCLNumber is the Piper CL number associated with the commit.
156+
PiperCLNumber string `json:"piper_cl_number,omitempty"`
157+
}
158+
145159
var (
146160
libraryIDRegex = regexp.MustCompile(`^[a-zA-Z0-9/._-]+$`)
147161
semverRegex = regexp.MustCompile(`^v?\d+\.\d+\.\d+$`)

internal/docker/docker_test.go

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

2626
"github.com/google/go-cmp/cmp"
2727
"github.com/googleapis/librarian/internal/config"
28-
"github.com/googleapis/librarian/internal/gitrepo"
2928
)
3029

3130
func TestNew(t *testing.T) {
@@ -929,15 +928,13 @@ func TestReleaseInitRequestContent(t *testing.T) {
929928
ID: "my-library",
930929
Version: "1.1.0",
931930
ReleaseTriggered: true,
932-
Changes: []*gitrepo.ConventionalCommit{
931+
Changes: []*config.Commit{
933932
{
934-
Type: "feat",
935-
Subject: "new feature",
936-
Body: "body of feature",
937-
Footers: map[string]string{
938-
"PiperOrigin-RevId": "12345",
939-
},
940-
CommitHash: "1234",
933+
Type: "feat",
934+
Subject: "new feature",
935+
Body: "body of feature",
936+
PiperCLNumber: "12345",
937+
CommitHash: "1234",
941938
},
942939
},
943940
},

internal/librarian/release_init.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (r *initRunner) updateLibrary(library *config.LibraryState, commits []*gitr
266266

267267
// Update the previous version, we need this value when creating release note.
268268
library.PreviousVersion = library.Version
269-
library.Changes = commits
269+
library.Changes = toCommit(commits)
270270
library.Version = nextVersion
271271
library.ReleaseTriggered = true
272272
return nil
@@ -295,3 +295,21 @@ func (r *initRunner) determineNextVersion(commits []*gitrepo.ConventionalCommit,
295295
// Compare versions and pick latest
296296
return semver.MaxVersion(nextVersionFromCommits, libraryConfig.NextVersion), nil
297297
}
298+
299+
// toCommit converts a slice of gitrepo.ConventionalCommit to a slice of config.Commit.
300+
// If the ConventionalCommit has NestedCommits, they are also extracted and
301+
// converted.
302+
func toCommit(c []*gitrepo.ConventionalCommit) []*config.Commit {
303+
var commits []*config.Commit
304+
for _, cc := range c {
305+
commits = append(commits, &config.Commit{
306+
Type: cc.Type,
307+
Subject: cc.Subject,
308+
Body: cc.Body,
309+
CommitHash: cc.CommitHash,
310+
PiperCLNumber: cc.Footers["PiperOrigin-RevId"],
311+
})
312+
}
313+
return commits
314+
315+
}

internal/librarian/release_init_test.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,16 +1258,16 @@ func TestUpdateLibrary(t *testing.T) {
12581258
ID: "one-id",
12591259
Version: "1.3.0",
12601260
PreviousVersion: "1.2.3",
1261-
Changes: []*gitrepo.ConventionalCommit{
1261+
Changes: []*config.Commit{
12621262
{
12631263
Type: "fix",
12641264
Subject: "change a typo",
12651265
},
12661266
{
1267-
Type: "feat",
1268-
Subject: "add a config file",
1269-
Body: "This is the body.",
1270-
Footers: map[string]string{"PiperOrigin-RevId": "12345"},
1267+
Type: "feat",
1268+
Subject: "add a config file",
1269+
Body: "This is the body.",
1270+
PiperCLNumber: "12345",
12711271
},
12721272
},
12731273
ReleaseTriggered: true,
@@ -1296,16 +1296,16 @@ func TestUpdateLibrary(t *testing.T) {
12961296
ID: "one-id",
12971297
Version: "5.0.0", // Use the `--version` value`
12981298
PreviousVersion: "1.2.3",
1299-
Changes: []*gitrepo.ConventionalCommit{
1299+
Changes: []*config.Commit{
13001300
{
13011301
Type: "fix",
13021302
Subject: "change a typo",
13031303
},
13041304
{
1305-
Type: "feat",
1306-
Subject: "add a config file",
1307-
Body: "This is the body.",
1308-
Footers: map[string]string{"PiperOrigin-RevId": "12345"},
1305+
Type: "feat",
1306+
Subject: "add a config file",
1307+
Body: "This is the body.",
1308+
PiperCLNumber: "12345",
13091309
},
13101310
},
13111311
ReleaseTriggered: true,
@@ -1359,20 +1359,15 @@ func TestUpdateLibrary(t *testing.T) {
13591359
ID: "one-id",
13601360
Version: "2.0.0",
13611361
PreviousVersion: "1.2.3",
1362-
Changes: []*gitrepo.ConventionalCommit{
1362+
Changes: []*config.Commit{
13631363
{
13641364
Type: "feat",
13651365
Subject: "add another config file",
13661366
Body: "This is the body",
1367-
Footers: map[string]string{
1368-
"BREAKING CHANGE": "this is a breaking change",
1369-
},
1370-
IsBreaking: true,
13711367
},
13721368
{
1373-
Type: "feat",
1374-
Subject: "change a typo",
1375-
IsBreaking: true,
1369+
Type: "feat",
1370+
Subject: "change a typo",
13761371
},
13771372
},
13781373
ReleaseTriggered: true,
@@ -1424,7 +1419,7 @@ func TestUpdateLibrary(t *testing.T) {
14241419
PreviousVersion: "1.2.3",
14251420
Version: "5.0.0", // Use the `--version` override value
14261421
ReleaseTriggered: true,
1427-
Changes: []*gitrepo.ConventionalCommit{
1422+
Changes: []*config.Commit{
14281423
{
14291424
Type: "chore",
14301425
Subject: "a chore",

internal/librarian/release_notes.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ Language Image: {{.ImageVersion}}
7979
{{ range .CommitSections }}
8080
### {{.Heading}}
8181
{{ range .Commits }}
82-
{{ if index .Footers "PiperOrigin-RevId" -}}
83-
* {{.Subject}} (PiperOrigin-RevId: {{index .Footers "PiperOrigin-RevId"}}) ([{{shortSHA .CommitHash}}]({{"https://github.com/"}}{{$noteSection.RepoOwner}}/{{$noteSection.RepoName}}/commit/{{shortSHA .CommitHash}}))
82+
{{ if .PiperCLNumber -}}
83+
* {{.Subject}} (PiperOrigin-RevId: {{.PiperCLNumber}}) ([{{shortSHA .CommitHash}}]({{"https://github.com/"}}{{$noteSection.RepoOwner}}/{{$noteSection.RepoName}}/commit/{{shortSHA .CommitHash}}))
8484
{{- else -}}
8585
* {{.Subject}} ([{{shortSHA .CommitHash}}]({{"https://github.com/"}}{{$noteSection.RepoOwner}}/{{$noteSection.RepoName}}/commit/{{shortSHA .CommitHash}}))
8686
{{- end }}
@@ -184,7 +184,7 @@ type releaseNoteSection struct {
184184

185185
type commitSection struct {
186186
Heading string
187-
Commits []*gitrepo.ConventionalCommit
187+
Commits []*config.Commit
188188
}
189189

190190
// formatOnboardPRBody creates the body of an onboarding pull request.
@@ -376,7 +376,7 @@ func formatLibraryReleaseNotes(library *config.LibraryState, ghRepo *github.Repo
376376
newTag := config.FormatTag(tagFormat, library.ID, newVersion)
377377
previousTag := config.FormatTag(tagFormat, library.ID, library.PreviousVersion)
378378

379-
commitsByType := make(map[string][]*gitrepo.ConventionalCommit)
379+
commitsByType := make(map[string][]*config.Commit)
380380
for _, commit := range library.Changes {
381381
commitsByType[commit.Type] = append(commitsByType[commit.Type], commit)
382382
}

internal/librarian/release_notes_test.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ func TestFormatReleaseNotes(t *testing.T) {
923923
// this is the NewVersion in the release note.
924924
Version: "1.1.0",
925925
PreviousVersion: "1.0.0",
926-
Changes: []*gitrepo.ConventionalCommit{
926+
Changes: []*config.Commit{
927927
{
928928
Type: "feat",
929929
Subject: "new feature",
@@ -967,22 +967,18 @@ Language Image: go:1.21
967967
// this is the NewVersion in the release note.
968968
Version: "1.1.0",
969969
PreviousVersion: "1.0.0",
970-
Changes: []*gitrepo.ConventionalCommit{
970+
Changes: []*config.Commit{
971971
{
972-
Type: "feat",
973-
Subject: "new feature",
974-
CommitHash: hash1.String(),
975-
Footers: map[string]string{
976-
"PiperOrigin-RevId": "123456",
977-
},
972+
Type: "feat",
973+
Subject: "new feature",
974+
CommitHash: hash1.String(),
975+
PiperCLNumber: "123456",
978976
},
979977
{
980-
Type: "fix",
981-
Subject: "a bug fix",
982-
CommitHash: hash2.String(),
983-
Footers: map[string]string{
984-
"PiperOrigin-RevId": "987654",
985-
},
978+
Type: "fix",
979+
Subject: "a bug fix",
980+
CommitHash: hash2.String(),
981+
PiperCLNumber: "987654",
986982
},
987983
},
988984
ReleaseTriggered: true,
@@ -1017,7 +1013,7 @@ Language Image: go:1.21
10171013
// this is the NewVersion in the release note.
10181014
Version: "1.1.0",
10191015
PreviousVersion: "1.0.0",
1020-
Changes: []*gitrepo.ConventionalCommit{
1016+
Changes: []*config.Commit{
10211017
{
10221018
Type: "feat",
10231019
Subject: "new feature",
@@ -1060,7 +1056,7 @@ Language Image: go:1.21
10601056
Version: "1.1.0",
10611057
PreviousVersion: "1.0.0",
10621058
ReleaseTriggered: true,
1063-
Changes: []*gitrepo.ConventionalCommit{
1059+
Changes: []*config.Commit{
10641060
{
10651061
Type: "feat",
10661062
Subject: "feature for a",
@@ -1074,7 +1070,7 @@ Language Image: go:1.21
10741070
Version: "2.0.1",
10751071
PreviousVersion: "2.0.0",
10761072
ReleaseTriggered: true,
1077-
Changes: []*gitrepo.ConventionalCommit{
1073+
Changes: []*config.Commit{
10781074
{
10791075
Type: "fix",
10801076
Subject: "fix for b",
@@ -1120,7 +1116,7 @@ Language Image: go:1.21
11201116
Version: "1.1.0",
11211117
PreviousVersion: "1.0.0",
11221118
ReleaseTriggered: true,
1123-
Changes: []*gitrepo.ConventionalCommit{
1119+
Changes: []*config.Commit{
11241120
{
11251121
Type: "feat",
11261122
Subject: "new feature",
@@ -1160,7 +1156,7 @@ Language Image: go:1.21
11601156
Version: "1.1.0",
11611157
PreviousVersion: "1.0.0",
11621158
ReleaseTriggered: true,
1163-
Changes: []*gitrepo.ConventionalCommit{
1159+
Changes: []*config.Commit{
11641160
{
11651161
Type: "feat",
11661162
Subject: "new feature",
@@ -1205,13 +1201,12 @@ Language Image: go:1.21
12051201
Version: "1.1.0",
12061202
PreviousVersion: "1.0.0",
12071203
ReleaseTriggered: true,
1208-
Changes: []*gitrepo.ConventionalCommit{
1204+
Changes: []*config.Commit{
12091205
{
12101206
Type: "chore",
12111207
Subject: "some chore",
12121208
Body: "this is the body",
12131209
CommitHash: hash1.String(),
1214-
IsNested: true,
12151210
},
12161211
},
12171212
},

0 commit comments

Comments
 (0)