Skip to content

Commit 21faab7

Browse files
authored
chore: fix source of truth for generate when doing releases (#3509)
Currently when running generate after bumping versions, it was reading directly from librarian.yaml (which wasn't updated with the latest version) so cargo.toml and readme files were not getting updated. Instead of writing to disk every time for every library, I updated the logic to use the generate function that takes in the config object. However generate updates the config object, so we need to pass in a clone so the original object doesn't get modified, as we write it back to librarian.yaml at the end of release with updated versions. I do think this can be reworked but in order to be ready for tuesday's release I am proposing this fix and will open separate ticket to refactor when time allows.
1 parent 2a42ed3 commit 21faab7

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

internal/librarian/release.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package librarian
1616

1717
import (
1818
"context"
19+
"encoding/json"
1920
"errors"
2021
"fmt"
2122
"strings"
@@ -90,8 +91,16 @@ func runRelease(ctx context.Context, cmd *cli.Command) error {
9091
return err
9192
}
9293

94+
if cfg.Sources == nil || cfg.Sources.Googleapis == nil {
95+
return errNoGoogleapiSourceInfo
96+
}
97+
googleapisDir, err := fetchSource(ctx, cfg.Sources.Googleapis, googleapisRepo)
98+
if err != nil {
99+
return err
100+
}
101+
93102
if all {
94-
if err = releaseAll(ctx, cfg, lastTag, gitExe); err != nil {
103+
if err = releaseAll(ctx, cfg, lastTag, gitExe, googleapisDir); err != nil {
95104
return err
96105
}
97106
} else {
@@ -103,14 +112,14 @@ func runRelease(ctx context.Context, cmd *cli.Command) error {
103112
if err != nil {
104113
return err
105114
}
106-
if err = releaseLibrary(ctx, cfg, libConfg, libConfg.Output, lastTag, gitExe); err != nil {
115+
if err = releaseLibrary(ctx, cfg, libConfg, libConfg.Output, lastTag, gitExe, googleapisDir); err != nil {
107116
return err
108117
}
109118
}
110119
return RunTidyOnConfig(ctx, cfg)
111120
}
112121

113-
func releaseAll(ctx context.Context, cfg *config.Config, lastTag, gitExe string) error {
122+
func releaseAll(ctx context.Context, cfg *config.Config, lastTag, gitExe, googleapisDir string) error {
114123
filesChanged, err := git.FilesChangedSince(ctx, lastTag, gitExe, cfg.Release.IgnoredChanges)
115124
if err != nil {
116125
return err
@@ -121,7 +130,7 @@ func releaseAll(ctx context.Context, cfg *config.Config, lastTag, gitExe string)
121130
return err
122131
}
123132
if shouldRelease(library, filesChanged, library.Output) {
124-
if err := releaseLibrary(ctx, cfg, library, library.Output, lastTag, gitExe); err != nil {
133+
if err := releaseLibrary(ctx, cfg, library, library.Output, lastTag, gitExe, googleapisDir); err != nil {
125134
return err
126135
}
127136
}
@@ -145,7 +154,7 @@ func shouldRelease(library *config.Library, filesChanged []string, srcPath strin
145154
return false
146155
}
147156

148-
func releaseLibrary(ctx context.Context, cfg *config.Config, libConfig *config.Library, srcPath, lastTag, gitExe string) error {
157+
func releaseLibrary(ctx context.Context, cfg *config.Config, libConfig *config.Library, srcPath, lastTag, gitExe, googleapisDir string) error {
149158
switch cfg.Language {
150159
case languageFake:
151160
return fakeReleaseLibrary(libConfig)
@@ -160,7 +169,14 @@ func releaseLibrary(ctx context.Context, cfg *config.Config, libConfig *config.L
160169
if err := rust.ReleaseLibrary(libConfig, srcPath); err != nil {
161170
return err
162171
}
163-
if err := runGenerate(ctx, false, libConfig.Name); err != nil {
172+
copyConfig, err := cloneConfig(cfg)
173+
if err != nil {
174+
return err
175+
}
176+
if _, err := generateLibrary(ctx, copyConfig, googleapisDir, libConfig.Name); err != nil {
177+
return err
178+
}
179+
if err := formatLibrary(ctx, cfg.Language, libConfig); err != nil {
164180
return err
165181
}
166182
return nil
@@ -181,3 +197,15 @@ func libraryByName(c *config.Config, name string) (*config.Library, error) {
181197
}
182198
return nil, errLibraryNotFound
183199
}
200+
201+
func cloneConfig(orig *config.Config) (*config.Config, error) {
202+
data, err := json.Marshal(orig)
203+
if err != nil {
204+
return nil, err
205+
}
206+
var copy config.Config
207+
if err := json.Unmarshal(data, &copy); err != nil {
208+
return nil, err
209+
}
210+
return &copy, nil
211+
}

internal/librarian/release_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func TestRelease(t *testing.T) {
278278
for _, test := range tests {
279279
t.Run(test.name, func(t *testing.T) {
280280
libConfg := &config.Library{}
281-
err := releaseLibrary(t.Context(), cfg, libConfg, test.srcPath, test.lastTag, "git")
281+
err := releaseLibrary(t.Context(), cfg, libConfg, test.srcPath, test.lastTag, "git", "")
282282
if err != nil {
283283
t.Fatalf("releaseLibrary() error = %v", err)
284284
}
@@ -349,7 +349,7 @@ func TestReleaseAll(t *testing.T) {
349349
}
350350
remoteDir := testhelper.SetupRepoWithChange(t, tag)
351351
testhelper.CloneRepository(t, remoteDir)
352-
err := releaseAll(t.Context(), config, tag, "git")
352+
err := releaseAll(t.Context(), config, tag, "git", "")
353353
if err != nil {
354354
t.Fatal(err)
355355
}

0 commit comments

Comments
 (0)