Skip to content

Commit 64a09f2

Browse files
authored
feat(internal/librarian/java): pass repo and artifact to gapic-generator-java (#4775)
Pass repo and artifact (distribution name) as parameter to gapic-generator-java. This is new feature to support o11y features. Context googleapis/sdk-platform-java#4120. Fix #4773
1 parent bba5793 commit 64a09f2

5 files changed

Lines changed: 81 additions & 21 deletions

File tree

doc/config-schema.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This document describes the schema for the librarian.yaml.
88
| :--- | :--- | :--- |
99
| `language` | string | Is the language for this workspace (go, python, rust). |
1010
| `version` | string | Is the librarian tool version to use. |
11-
| `repo` | string | Is the repository name, such as "googleapis/google-cloud-python".<br><br>TODO(https://github.com/googleapis/librarian/issues/3003): Remove this field when .repo-metadata.json generation is removed. |
11+
| `repo` | string | Is the repository name, such as "googleapis/google-cloud-python". It is used for:<br>- Providing to the Java GAPIC generator for observability features.<br>- Generating the .repo-metadata.json. |
1212
| `sources` | [Sources](#sources-configuration) (optional) | References external source repositories. |
1313
| `release` | [Release](#release-configuration) (optional) | Holds the configuration parameter for publishing and release subcommands. |
1414
| `default` | [Default](#default-configuration) (optional) | Contains default settings for all libraries. They apply to all libraries unless overridden. |

internal/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ type Config struct {
2727
Version string `yaml:"version,omitempty"`
2828

2929
// Repo is the repository name, such as "googleapis/google-cloud-python".
30-
//
31-
// TODO(https://github.com/googleapis/librarian/issues/3003): Remove this
32-
// field when .repo-metadata.json generation is removed.
30+
// It is used for:
31+
// - Providing to the Java GAPIC generator for observability features.
32+
// - Generating the .repo-metadata.json.
3333
Repo string `yaml:"repo,omitempty"`
3434

3535
// Sources references external source repositories.

internal/librarian/java/generate.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ func Generate(ctx context.Context, cfg *config.Config, library *config.Library,
5252
return fmt.Errorf("failed to create output directory: %w", err)
5353
}
5454
for _, api := range library.APIs {
55-
if err := generateAPI(ctx, api, library, googleapisDir, outdir); err != nil {
55+
if err := generateAPI(ctx, cfg, api, library, googleapisDir, outdir); err != nil {
5656
return fmt.Errorf("failed to generate api %q: %w", api.Path, err)
5757
}
5858
}
5959
return postProcessLibrary(cfg, library, outdir, googleapisDir)
6060
}
6161

62-
func generateAPI(ctx context.Context, api *config.API, library *config.Library, googleapisDir, outdir string) error {
62+
func generateAPI(ctx context.Context, cfg *config.Config, api *config.API, library *config.Library, googleapisDir, outdir string) error {
6363
version := serviceconfig.ExtractVersion(api.Path)
6464
if version == "" {
6565
return fmt.Errorf("failed to generate api: failed to extract version from api path %q", api.Path)
@@ -114,7 +114,7 @@ func generateAPI(ctx context.Context, api *config.API, library *config.Library,
114114
}
115115
}
116116
// 3. Generate GAPIC library.
117-
gapicOpts, err := resolveGAPICOptions(api, javaAPI, googleapisDir, apiCfg)
117+
gapicOpts, err := resolveGAPICOptions(cfg, library, api, javaAPI, googleapisDir, apiCfg)
118118
if err != nil {
119119
return fmt.Errorf("failed to resolve gapic options: %w", err)
120120
}
@@ -132,6 +132,18 @@ func generateAPI(ctx context.Context, api *config.API, library *config.Library,
132132
return nil
133133
}
134134

135+
func deriveDistributionName(library *config.Library) string {
136+
groupID := "com.google.cloud"
137+
if library.Java != nil && library.Java.GroupID != "" {
138+
groupID = library.Java.GroupID
139+
}
140+
artifactID := library.Name
141+
if !strings.HasPrefix(artifactID, cloudPrefix) {
142+
artifactID = cloudPrefix + artifactID
143+
}
144+
return fmt.Sprintf("%s:%s", groupID, artifactID)
145+
}
146+
135147
var runProtoc = func(ctx context.Context, args []string) error {
136148
return command.Run(ctx, "protoc", args...)
137149
}
@@ -166,10 +178,14 @@ func gapicProtocArgs(apiProtos, additionalProtos []string, googleapisDir, gapicD
166178
return args
167179
}
168180

169-
func resolveGAPICOptions(api *config.API, javaAPI *config.JavaAPI, googleapisDir string, apiCfgs *serviceconfig.API) ([]string, error) {
181+
func resolveGAPICOptions(cfg *config.Config, library *config.Library, api *config.API, javaAPI *config.JavaAPI, googleapisDir string, apiCfgs *serviceconfig.API) ([]string, error) {
170182
// gapicOpts are passed to the GAPIC generator via --java_gapic_opt.
171183
// "metadata" enables the generation of gapic_metadata.json and GraalVM reflect-config.json.
172184
gapicOpts := []string{"metadata"}
185+
186+
gapicOpts = append(gapicOpts, gapicOpt("repo", cfg.Repo))
187+
gapicOpts = append(gapicOpts, gapicOpt("artifact", deriveDistributionName(library)))
188+
173189
if apiCfgs != nil && apiCfgs.ServiceConfig != "" {
174190
// api-service-config specifies the service YAML (e.g., logging_v2.yaml) which
175191
// contains documentation, HTTP rules, and other API-level configuration.

internal/librarian/java/generate_test.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,26 @@ const googleapisDir = "../../testdata/googleapis"
3636
func TestResolveGAPICOptions(t *testing.T) {
3737
for _, test := range []struct {
3838
name string
39+
cfg *config.Config
40+
library *config.Library
3941
api *config.API
4042
javaAPI *config.JavaAPI
4143
apiCfgs *serviceconfig.API
4244
expected []string
4345
}{
4446
{
4547
name: "basic case",
48+
cfg: &config.Config{Repo: "googleapis/google-cloud-java"},
49+
library: &config.Library{Name: "secretmanager"},
4650
api: &config.API{Path: "google/cloud/secretmanager/v1"},
4751
javaAPI: &config.JavaAPI{Path: "google/cloud/secretmanager/v1"},
4852
apiCfgs: &serviceconfig.API{Transports: map[string]serviceconfig.Transport{
4953
config.LanguageJava: serviceconfig.GRPCRest,
5054
}},
5155
expected: []string{
5256
"metadata",
57+
"repo=googleapis/google-cloud-java",
58+
"artifact=com.google.cloud:google-cloud-secretmanager",
5359
"gapic-config=" + filepath.Join(googleapisDir, "google/cloud/secretmanager/v1/secretmanager_gapic.yaml"),
5460
"grpc-service-config=" + filepath.Join(googleapisDir, "google/cloud/secretmanager/v1/secretmanager_grpc_service_config.json"),
5561
"transport=grpc+rest",
@@ -58,6 +64,8 @@ func TestResolveGAPICOptions(t *testing.T) {
5864
},
5965
{
6066
name: "rest transport",
67+
cfg: &config.Config{Repo: "googleapis/google-cloud-java"},
68+
library: &config.Library{Name: "secretmanager"},
6169
api: &config.API{Path: "google/cloud/secretmanager/v1"},
6270
javaAPI: &config.JavaAPI{Path: "google/cloud/secretmanager/v1"},
6371

@@ -66,6 +74,8 @@ func TestResolveGAPICOptions(t *testing.T) {
6674
}},
6775
expected: []string{
6876
"metadata",
77+
"repo=googleapis/google-cloud-java",
78+
"artifact=com.google.cloud:google-cloud-secretmanager",
6979
"gapic-config=" + filepath.Join(googleapisDir, "google/cloud/secretmanager/v1/secretmanager_gapic.yaml"),
7080
"grpc-service-config=" + filepath.Join(googleapisDir, "google/cloud/secretmanager/v1/secretmanager_grpc_service_config.json"),
7181
"transport=rest",
@@ -74,6 +84,8 @@ func TestResolveGAPICOptions(t *testing.T) {
7484
},
7585
{
7686
name: "no rest numeric enum case",
87+
cfg: &config.Config{Repo: "googleapis/google-cloud-java"},
88+
library: &config.Library{Name: "secretmanager"},
7789
api: &config.API{Path: "google/cloud/secretmanager/v1"},
7890
javaAPI: &config.JavaAPI{Path: "google/cloud/secretmanager/v1", NoRestNumericEnums: true},
7991

@@ -82,6 +94,8 @@ func TestResolveGAPICOptions(t *testing.T) {
8294
}},
8395
expected: []string{
8496
"metadata",
97+
"repo=googleapis/google-cloud-java",
98+
"artifact=com.google.cloud:google-cloud-secretmanager",
8599
"gapic-config=" + filepath.Join(googleapisDir, "google/cloud/secretmanager/v1/secretmanager_gapic.yaml"),
86100
"grpc-service-config=" + filepath.Join(googleapisDir, "google/cloud/secretmanager/v1/secretmanager_grpc_service_config.json"),
87101
"transport=grpc+rest",
@@ -90,7 +104,7 @@ func TestResolveGAPICOptions(t *testing.T) {
90104
} {
91105
t.Run(test.name, func(t *testing.T) {
92106
t.Parallel()
93-
got, err := resolveGAPICOptions(test.api, test.javaAPI, googleapisDir, test.apiCfgs)
107+
got, err := resolveGAPICOptions(test.cfg, test.library, test.api, test.javaAPI, googleapisDir, test.apiCfgs)
94108
if err != nil {
95109
t.Fatal(err)
96110
}
@@ -101,6 +115,40 @@ func TestResolveGAPICOptions(t *testing.T) {
101115
}
102116
}
103117

118+
func TestDeriveDistributionName(t *testing.T) {
119+
for _, test := range []struct {
120+
name string
121+
library *config.Library
122+
want string
123+
}{
124+
{
125+
name: "default case",
126+
library: &config.Library{Name: "secretmanager"},
127+
want: "com.google.cloud:google-cloud-secretmanager",
128+
},
129+
{
130+
name: "groupID override",
131+
library: &config.Library{
132+
Name: "secretmanager",
133+
Java: &config.JavaModule{GroupID: "com.custom"},
134+
},
135+
want: "com.custom:google-cloud-secretmanager",
136+
},
137+
{
138+
name: "library name already has prefix",
139+
library: &config.Library{Name: "google-cloud-secretmanager"},
140+
want: "com.google.cloud:google-cloud-secretmanager",
141+
},
142+
} {
143+
t.Run(test.name, func(t *testing.T) {
144+
got := deriveDistributionName(test.library)
145+
if got != test.want {
146+
t.Errorf("deriveDistributionName() = %q, want %q", got, test.want)
147+
}
148+
})
149+
}
150+
}
151+
104152
func TestResolveGAPICOptions_MultipleConfigsError(t *testing.T) {
105153
testCases := []struct {
106154
name string
@@ -142,7 +190,7 @@ func TestResolveGAPICOptions_MultipleConfigsError(t *testing.T) {
142190
apiCfgs := &serviceconfig.API{Transports: map[string]serviceconfig.Transport{
143191
config.LanguageJava: serviceconfig.GRPC,
144192
}}
145-
_, err := resolveGAPICOptions(&config.API{Path: tc.apiPath}, &config.JavaAPI{Path: tc.apiPath}, tmpDir, apiCfgs)
193+
_, err := resolveGAPICOptions(&config.Config{Repo: "test-repo"}, &config.Library{Name: "test"}, &config.API{Path: tc.apiPath}, &config.JavaAPI{Path: tc.apiPath}, tmpDir, apiCfgs)
146194
if err == nil || !strings.Contains(err.Error(), tc.wantErr) {
147195
t.Errorf("resolveGAPICOptions() error = %v, wantErr %v", err, tc.wantErr)
148196
}
@@ -298,10 +346,13 @@ func TestGenerateAPI(t *testing.T) {
298346
testhelper.RequireCommand(t, "protoc-gen-java_gapic")
299347
testhelper.RequireCommand(t, "protoc-gen-java_grpc")
300348
outdir := t.TempDir()
349+
cfg := &config.Config{Repo: "googleapis/google-cloud-java"}
350+
library := &config.Library{Name: "secretmanager", Output: outdir}
301351
err := generateAPI(
302352
t.Context(),
353+
cfg,
303354
&config.API{Path: "google/cloud/secretmanager/v1"},
304-
&config.Library{Name: "secretmanager", Output: outdir},
355+
library,
305356
googleapisDir,
306357
outdir,
307358
)
@@ -327,9 +378,10 @@ func TestGenerateAPI_NoTools(t *testing.T) {
327378
}
328379
outdir := t.TempDir()
329380
api := &config.API{Path: "google/cloud/secretmanager/v1"}
381+
cfg := &config.Config{Repo: "googleapis/google-cloud-java"}
330382
library := &config.Library{Name: "secretmanager", Output: outdir}
331383

332-
err := generateAPI(t.Context(), api, library, googleapisDir, outdir)
384+
err := generateAPI(t.Context(), cfg, api, library, googleapisDir, outdir)
333385
if err != nil {
334386
t.Fatal(err)
335387
}

internal/librarian/java/postprocess.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,7 @@ func deriveRepoMetadata(cfg *config.Config, library *config.Library, googleapisD
295295

296296
// distribution_name default for Java is groupId:artifactId
297297
if !strings.Contains(metadata.DistributionName, ":") {
298-
groupID := "com.google.cloud"
299-
if library.Java != nil && library.Java.GroupID != "" {
300-
groupID = library.Java.GroupID
301-
}
302-
artifactID := library.Name
303-
if !strings.HasPrefix(artifactID, cloudPrefix) {
304-
artifactID = cloudPrefix + artifactID
305-
}
306-
metadata.DistributionName = fmt.Sprintf("%s:%s", groupID, artifactID)
298+
metadata.DistributionName = deriveDistributionName(library)
307299
}
308300
// Default ClientDocumentation uses artifact ID
309301
if metadata.ClientDocumentation == "" {

0 commit comments

Comments
 (0)