Skip to content

Commit c4d97d7

Browse files
authored
feat(tool/cmd/migrate): use googleapis_commitish from generation_config.yaml (#4514)
Librarian now uses the googleapis_commitish field from the Java generation_config.yaml to fetch the specific version of googleapis used during library generation. Sample [librarian.yaml](https://paste.googleplex.com/5052837236178944) produced with migrate in google-cloud-java. For #4306
1 parent 454889c commit c4d97d7

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

tool/cmd/migrate/java.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const (
2929
generationConfigFileName = "generation_config.yaml"
3030
)
3131

32+
var (
33+
fetchSourceWithCommit = fetchGoogleapisWithCommit
34+
)
35+
3236
type javaGAPICInfo struct {
3337
NoRestNumericEnums bool
3438
NoSamples bool
@@ -119,19 +123,24 @@ type LibraryConfig struct {
119123

120124
// GenerationConfig represents the root of generation_config.yaml.
121125
type GenerationConfig struct {
122-
Libraries []LibraryConfig `yaml:"libraries"`
126+
GoogleapisCommitish string `yaml:"googleapis_commitish"`
127+
Libraries []LibraryConfig `yaml:"libraries"`
123128
}
124129

125130
func runJavaMigration(ctx context.Context, repoPath string) error {
126131
gen, err := readGenerationConfig(repoPath)
127132
if err != nil {
128133
return err
129134
}
130-
src, err := fetchSource(ctx)
135+
commit := gen.GoogleapisCommitish
136+
if commit == "" {
137+
commit = "master"
138+
}
139+
src, err := fetchSourceWithCommit(ctx, commit)
131140
if err != nil {
132141
return errFetchSource
133142
}
134-
cfg := buildConfig(gen, src.Dir)
143+
cfg := buildConfig(gen, src)
135144
if cfg == nil {
136145
return fmt.Errorf("no libraries found to migrate")
137146
}
@@ -150,7 +159,7 @@ func readGenerationConfig(path string) (*GenerationConfig, error) {
150159
}
151160

152161
// buildConfig converts a GenerationConfig to a Librarian Config.
153-
func buildConfig(gen *GenerationConfig, googleapisDir string) *config.Config {
162+
func buildConfig(gen *GenerationConfig, src *config.Source) *config.Config {
154163
var libs []*config.Library
155164
for _, l := range gen.Libraries {
156165
name := l.LibraryName
@@ -165,7 +174,7 @@ func buildConfig(gen *GenerationConfig, googleapisDir string) *config.Config {
165174
}
166175
apis = append(apis, &config.API{Path: g.ProtoPath})
167176

168-
info, err := parseJavaBazel(googleapisDir, g.ProtoPath)
177+
info, err := parseJavaBazel(src.Dir, g.ProtoPath)
169178
if err != nil {
170179
log.Printf("Warning: failed to parse BUILD.bazel for %s: %v", g.ProtoPath, err)
171180
continue
@@ -218,7 +227,7 @@ func buildConfig(gen *GenerationConfig, googleapisDir string) *config.Config {
218227
Language: "java",
219228
Default: &config.Default{},
220229
Sources: &config.Sources{
221-
Googleapis: &config.Source{Dir: googleapisDir},
230+
Googleapis: src,
222231
},
223232
Libraries: libs,
224233
Repo: "googleapis/google-cloud-java",

tool/cmd/migrate/java_test.go

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

2727
func TestRunJavaMigration(t *testing.T) {
28-
fetchSource = func(ctx context.Context) (*config.Source, error) {
28+
fetchSourceWithCommit = func(ctx context.Context, commitish string) (*config.Source, error) {
2929
return &config.Source{
30-
Commit: "abcd123",
30+
Commit: commitish,
3131
SHA256: "sha123",
3232
Dir: "../../internal/testdata/googleapis",
3333
}, nil
@@ -265,7 +265,7 @@ func TestBuildConfig(t *testing.T) {
265265
},
266266
} {
267267
t.Run(test.name, func(t *testing.T) {
268-
got := buildConfig(test.gen, "../../internal/testdata/googleapis")
268+
got := buildConfig(test.gen, &config.Source{Dir: "../../internal/testdata/googleapis"})
269269
if diff := cmp.Diff(test.want, got); diff != "" {
270270
t.Errorf("mismatch (-want +got):\n%s", diff)
271271
}

tool/cmd/migrate/legacylibrarian.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,27 +177,31 @@ func buildConfigFromLibrarian(ctx context.Context, input *MigrationInput) (*conf
177177
}
178178

179179
func fetchGoogleapis(ctx context.Context) (*config.Source, error) {
180+
return fetchGoogleapisWithCommit(ctx, fetch.DefaultBranchMaster)
181+
}
182+
183+
func fetchGoogleapisWithCommit(ctx context.Context, commitish string) (*config.Source, error) {
180184
endpoint := &fetch.Endpoints{
181185
API: "https://api.github.com",
182186
Download: "https://github.com",
183187
}
184188
repo := &fetch.Repo{
185189
Org: "googleapis",
186190
Repo: "googleapis",
187-
Branch: fetch.DefaultBranchMaster,
191+
Branch: commitish,
188192
}
189-
latestCommit, sha256, err := fetch.LatestCommitAndChecksum(endpoint, repo)
193+
commit, sha256, err := fetch.LatestCommitAndChecksum(endpoint, repo)
190194
if err != nil {
191195
return nil, err
192196
}
193197

194-
dir, err := fetch.RepoDir(ctx, googleapisRepo, latestCommit, sha256)
198+
dir, err := fetch.RepoDir(ctx, googleapisRepo, commit, sha256)
195199
if err != nil {
196200
return nil, err
197201
}
198202

199203
return &config.Source{
200-
Commit: latestCommit,
204+
Commit: commit,
201205
SHA256: sha256,
202206
Dir: dir,
203207
}, nil

0 commit comments

Comments
 (0)