Skip to content

Commit 90d0c5e

Browse files
authored
feat: parse service config from api source (#873)
Fixes: #777
1 parent a5695eb commit 90d0c5e

File tree

13 files changed

+303
-61
lines changed

13 files changed

+303
-61
lines changed

internal/config/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ type LibraryState struct {
9090
// The commit hash from the API definition repository at which the library was last generated.
9191
LastGeneratedCommit string `yaml:"last_generated_commit"`
9292
// A list of APIs that are part of this library.
93-
APIs []API `yaml:"apis"`
93+
APIs []*API `yaml:"apis"`
9494
// A list of directories in the language repository where Librarian contributes code.
9595
SourcePaths []string `yaml:"source_paths"`
9696
// A list of regular expressions for files and directories to preserve during the copy and remove process.

internal/config/state_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestLibrarianState_Validate(t *testing.T) {
3232
{
3333
ID: "a/b",
3434
SourcePaths: []string{"src/a", "src/b"},
35-
APIs: []API{
35+
APIs: []*API{
3636
{
3737
Path: "a/b/v1",
3838
},
@@ -48,7 +48,7 @@ func TestLibrarianState_Validate(t *testing.T) {
4848
{
4949
ID: "a/b",
5050
SourcePaths: []string{"src/a", "src/b"},
51-
APIs: []API{
51+
APIs: []*API{
5252
{
5353
Path: "a/b/v1",
5454
},
@@ -85,7 +85,7 @@ func TestLibrary_Validate(t *testing.T) {
8585
library: &LibraryState{
8686
ID: "a/b",
8787
SourcePaths: []string{"src/a", "src/b"},
88-
APIs: []API{
88+
APIs: []*API{
8989
{
9090
Path: "a/b/v1",
9191
},
@@ -96,7 +96,7 @@ func TestLibrary_Validate(t *testing.T) {
9696
name: "missing id",
9797
library: &LibraryState{
9898
SourcePaths: []string{"src/a", "src/b"},
99-
APIs: []API{
99+
APIs: []*API{
100100
{
101101
Path: "a/b/v1",
102102
},
@@ -109,7 +109,7 @@ func TestLibrary_Validate(t *testing.T) {
109109
library: &LibraryState{
110110
ID: ".",
111111
SourcePaths: []string{"src/a", "src/b"},
112-
APIs: []API{
112+
APIs: []*API{
113113
{
114114
Path: "a/b/v1",
115115
},
@@ -122,7 +122,7 @@ func TestLibrary_Validate(t *testing.T) {
122122
library: &LibraryState{
123123
ID: "..",
124124
SourcePaths: []string{"src/a", "src/b"},
125-
APIs: []API{
125+
APIs: []*API{
126126
{
127127
Path: "a/b/v1",
128128
},
@@ -134,7 +134,7 @@ func TestLibrary_Validate(t *testing.T) {
134134
name: "missing source paths",
135135
library: &LibraryState{
136136
ID: "a/b",
137-
APIs: []API{
137+
APIs: []*API{
138138
{
139139
Path: "a/b/v1",
140140
},
@@ -156,7 +156,7 @@ func TestLibrary_Validate(t *testing.T) {
156156
ID: "a/b",
157157
Version: "1.2.3",
158158
SourcePaths: []string{"src/a", "src/b"},
159-
APIs: []API{
159+
APIs: []*API{
160160
{
161161
Path: "a/b/v1",
162162
},
@@ -168,7 +168,7 @@ func TestLibrary_Validate(t *testing.T) {
168168
library: &LibraryState{
169169
ID: "a/b!",
170170
SourcePaths: []string{"src/a", "src/b"},
171-
APIs: []API{
171+
APIs: []*API{
172172
{
173173
Path: "a/b/v1",
174174
},
@@ -182,7 +182,7 @@ func TestLibrary_Validate(t *testing.T) {
182182
ID: "a/b",
183183
LastGeneratedCommit: "not-a-hex-string",
184184
SourcePaths: []string{"src/a", "src/b"},
185-
APIs: []API{
185+
APIs: []*API{
186186
{
187187
Path: "a/b/v1",
188188
},
@@ -196,7 +196,7 @@ func TestLibrary_Validate(t *testing.T) {
196196
ID: "a/b",
197197
LastGeneratedCommit: "deadbeef",
198198
SourcePaths: []string{"src/a", "src/b"},
199-
APIs: []API{
199+
APIs: []*API{
200200
{
201201
Path: "a/b/v1",
202202
},
@@ -209,7 +209,7 @@ func TestLibrary_Validate(t *testing.T) {
209209
library: &LibraryState{
210210
ID: "a/b",
211211
SourcePaths: []string{"src/a"},
212-
APIs: []API{{Path: "a/b/v1"}},
212+
APIs: []*API{{Path: "a/b/v1"}},
213213
PreserveRegex: []string{".*\\.txt"},
214214
},
215215
},
@@ -218,7 +218,7 @@ func TestLibrary_Validate(t *testing.T) {
218218
library: &LibraryState{
219219
ID: "a/b",
220220
SourcePaths: []string{"src/a"},
221-
APIs: []API{{Path: "a/b/v1"}},
221+
APIs: []*API{{Path: "a/b/v1"}},
222222
PreserveRegex: []string{"["},
223223
},
224224
wantErr: true,
@@ -228,7 +228,7 @@ func TestLibrary_Validate(t *testing.T) {
228228
library: &LibraryState{
229229
ID: "a/b",
230230
SourcePaths: []string{"src/a"},
231-
APIs: []API{{Path: "a/b/v1"}},
231+
APIs: []*API{{Path: "a/b/v1"}},
232232
RemoveRegex: []string{".*\\.log"},
233233
},
234234
},
@@ -237,7 +237,7 @@ func TestLibrary_Validate(t *testing.T) {
237237
library: &LibraryState{
238238
ID: "a/b",
239239
SourcePaths: []string{"src/a"},
240-
APIs: []API{{Path: "a/b/v1"}},
240+
APIs: []*API{{Path: "a/b/v1"}},
241241
RemoveRegex: []string{"("},
242242
},
243243
wantErr: true,

internal/docker/docker_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func TestToGenerateRequestJSON(t *testing.T) {
309309
ID: "google-cloud-go",
310310
Version: "1.0.0",
311311
LastGeneratedCommit: "abcd123",
312-
APIs: []config.API{
312+
APIs: []*config.API{
313313
{
314314
Path: "google/cloud/compute/v1",
315315
ServiceConfig: "example_service_config.yaml",
@@ -328,7 +328,7 @@ func TestToGenerateRequestJSON(t *testing.T) {
328328
{
329329
ID: "google-cloud-storage",
330330
Version: "1.2.3",
331-
APIs: []config.API{
331+
APIs: []*config.API{
332332
{
333333
Path: "google/storage/v1",
334334
ServiceConfig: "storage_service_config.yaml",

internal/librarian/generate.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func newGenerateRunner(cfg *config.Config) (*generateRunner, error) {
118118
if err != nil {
119119
return nil, err
120120
}
121-
state, config, err := loadRepoStateAndConfig(repo)
121+
state, pipelineConfig, err := loadRepoStateAndConfig(repo, cfg.Source)
122122
if err != nil {
123123
return nil, err
124124
}
@@ -136,7 +136,7 @@ func newGenerateRunner(cfg *config.Config) (*generateRunner, error) {
136136
return nil, fmt.Errorf("failed to create GitHub client: %w", err)
137137
}
138138
}
139-
container, err := docker.New(workRoot, image, cfg.Project, cfg.UserUID, cfg.UserGID, config)
139+
container, err := docker.New(workRoot, image, cfg.Project, cfg.UserUID, cfg.UserGID, pipelineConfig)
140140
if err != nil {
141141
return nil, err
142142
}
@@ -145,7 +145,7 @@ func newGenerateRunner(cfg *config.Config) (*generateRunner, error) {
145145
workRoot: workRoot,
146146
repo: repo,
147147
state: state,
148-
config: config,
148+
config: pipelineConfig,
149149
image: image,
150150
ghClient: ghClient,
151151
containerClient: container,
@@ -247,7 +247,7 @@ func (r *generateRunner) runBuildCommand(ctx context.Context, outputDir, library
247247
// by fetching the single file) if flatRepoUrl has been specified. If neither the repo
248248
// root not the repo url has been specified, we always perform raw generation.
249249
func (r *generateRunner) detectIfLibraryConfigured(ctx context.Context) (bool, error) {
250-
apiPath, repo := r.cfg.API, r.cfg.Repo
250+
apiPath, repo, source := r.cfg.API, r.cfg.Repo, r.cfg.Source
251251
if repo == "" {
252252
slog.Warn("repo is not specified, cannot check if library exists")
253253
return false, nil
@@ -259,13 +259,13 @@ func (r *generateRunner) detectIfLibraryConfigured(ctx context.Context) (bool, e
259259
err error
260260
)
261261
if isUrl(repo) {
262-
pipelineState, err = fetchRemoteLibrarianState(ctx, r.ghClient, "HEAD")
262+
pipelineState, err = fetchRemoteLibrarianState(ctx, r.ghClient, "HEAD", source)
263263
if err != nil {
264264
return false, err
265265
}
266266
} else {
267267
// repo is a directory
268-
pipelineState, err = loadLibrarianStateFile(filepath.Join(repo, config.GeneratorInputDir, pipelineStateFile))
268+
pipelineState, err = loadLibrarianStateFile(filepath.Join(repo, config.GeneratorInputDir, pipelineStateFile), source)
269269
if err != nil {
270270
return false, err
271271
}

internal/librarian/generate_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestDetectIfLibraryConfigured(t *testing.T) {
6868
Libraries: []*config.LibraryState{
6969
{
7070
ID: "some-library",
71-
APIs: []config.API{{Path: "some/api"}},
71+
APIs: []*config.API{{Path: "some/api", ServiceConfig: "api_config.yaml"}},
7272
SourcePaths: []string{"src/a"},
7373
},
7474
},
@@ -83,7 +83,7 @@ func TestDetectIfLibraryConfigured(t *testing.T) {
8383
Libraries: []*config.LibraryState{
8484
{
8585
ID: "some-library",
86-
APIs: []config.API{{Path: "some/api"}},
86+
APIs: []*config.API{{Path: "some/api", ServiceConfig: "api_config.yaml"}},
8787
SourcePaths: []string{"src/a"},
8888
},
8989
},
@@ -160,7 +160,7 @@ func TestRunGenerateCommand(t *testing.T) {
160160
Libraries: []*config.LibraryState{
161161
{
162162
ID: "some-library",
163-
APIs: []config.API{{Path: "some/api"}},
163+
APIs: []*config.API{{Path: "some/api"}},
164164
},
165165
},
166166
},
@@ -182,7 +182,7 @@ func TestRunGenerateCommand(t *testing.T) {
182182
Libraries: []*config.LibraryState{
183183
{
184184
ID: "some-library",
185-
APIs: []config.API{{Path: "some/api"}},
185+
APIs: []*config.API{{Path: "some/api"}},
186186
},
187187
},
188188
},
@@ -322,7 +322,7 @@ func TestNewGenerateRunner(t *testing.T) {
322322
Libraries: []*config.LibraryState{
323323
{
324324
ID: "some-library",
325-
APIs: []config.API{{Path: "some/api"}},
325+
APIs: []*config.API{{Path: "some/api", ServiceConfig: "api_config.yaml"}},
326326
SourcePaths: []string{"src/a"},
327327
},
328328
},
@@ -400,7 +400,7 @@ func TestGenerateRun(t *testing.T) {
400400
Libraries: []*config.LibraryState{
401401
{
402402
ID: "some-library",
403-
APIs: []config.API{{Path: "some/api"}},
403+
APIs: []*config.API{{Path: "some/api"}},
404404
},
405405
},
406406
},

0 commit comments

Comments
 (0)