@@ -72,14 +72,12 @@ func main() {
72
72
log .Println ("googleapis-dir set to" , * googleapisDir )
73
73
log .Println ("branch set to" , * branchOverride )
74
74
log .Println ("prFilepath is" , * prFilepath )
75
+ log .Println ("directories are" , * directories )
75
76
76
- var modules []string
77
+ dirSlice := []string {}
77
78
if * directories != "" {
78
79
dirSlice := strings .Split (* directories , "," )
79
- for _ , dir := range dirSlice {
80
- modules = append (modules , filepath .Join (* clientRoot , dir ))
81
- }
82
- log .Println ("Postprocessor running on" , modules )
80
+ log .Println ("Postprocessor running on" , dirSlice )
83
81
} else {
84
82
log .Println ("Postprocessor running on all modules." )
85
83
}
@@ -103,11 +101,13 @@ func main() {
103
101
c := & config {
104
102
googleapisDir : * googleapisDir ,
105
103
googleCloudDir : * clientRoot ,
106
- modules : modules ,
104
+ modules : dirSlice ,
107
105
branchOverride : * branchOverride ,
108
106
githubUsername : * githubUsername ,
109
107
prFilepath : * prFilepath ,
110
108
runAll : runAll ,
109
+ prTitle : "" ,
110
+ prBody : "" ,
111
111
}
112
112
113
113
if err := c .run (ctx ); err != nil {
@@ -120,11 +120,19 @@ func main() {
120
120
type config struct {
121
121
googleapisDir string
122
122
googleCloudDir string
123
- modules []string
123
+
124
+ // At this time modules are either provided at the time of invocation locally
125
+ // and extracted from the open OwlBot PR description. If we would like
126
+ // the postprocessor to be able to be run on non-OwlBot PRs, we would
127
+ // need to change the method of populating this field.
128
+ modules []string
129
+
124
130
branchOverride string
125
131
githubUsername string
126
132
prFilepath string
127
133
runAll bool
134
+ prTitle string
135
+ prBody string
128
136
}
129
137
130
138
// runAll uses git to tell if the PR being updated should run all post
@@ -150,7 +158,16 @@ func (c *config) run(ctx context.Context) error {
150
158
log .Println ("exiting post processing early" )
151
159
return nil
152
160
}
153
- if err := gocmd .ModTidyAll (c .googleCloudDir ); err != nil {
161
+ // TODO(guadriana): Once the PR for initializing new modules is merged, we
162
+ // will need to add logic here that sets c.modules to modConfigs (a slice of
163
+ // all modules) if branchOverride != ""
164
+
165
+ // TODO(codyoss): In the future we may want to make it possible to be able
166
+ // to run this locally with a user defined remote branch.
167
+ if err := c .SetScopesAndPRInfo (ctx ); err != nil {
168
+ return err
169
+ }
170
+ if err := c .TidyAffectedMods (); err != nil {
154
171
return err
155
172
}
156
173
if err := gocmd .Vet (c .googleCloudDir ); err != nil {
@@ -162,28 +179,44 @@ func (c *config) run(ctx context.Context) error {
162
179
if _ , err := c .Manifest (generator .MicrogenGapicConfigs ); err != nil {
163
180
return err
164
181
}
165
- // TODO(codyoss): In the future we may want to make it possible to be able
166
- // to run this locally with a user defined remote branch.
167
- if err := c .AmendPRDescription (ctx ); err != nil {
182
+ if err := c .WritePRInfoToFile (); err != nil {
168
183
return err
169
184
}
170
185
return nil
171
186
}
172
187
188
+ func (c * config ) getDirs () []string {
189
+ dirs := []string {}
190
+ for _ , module := range c .modules {
191
+ dirs = append (dirs , filepath .Join (c .googleCloudDir , module ))
192
+ }
193
+ return dirs
194
+ }
195
+
196
+ func (c * config ) TidyAffectedMods () error {
197
+ dirs := c .getDirs ()
198
+ for _ , dir := range dirs {
199
+ if err := gocmd .ModTidy (dir ); err != nil {
200
+ return err
201
+ }
202
+ }
203
+ return nil
204
+ }
205
+
173
206
// RegenSnippets regenerates the snippets for all GAPICs configured to be generated.
174
207
func (c * config ) RegenSnippets () error {
175
208
log .Println ("regenerating snippets" )
176
-
177
209
snippetDir := filepath .Join (c .googleCloudDir , "internal" , "generated" , "snippets" )
178
- apiShortnames , err := generator . ParseAPIShortnames ( c . googleapisDir , generator . MicrogenGapicConfigs , generator . ManualEntries )
179
-
210
+ confs := c . getChangedClientConfs ( )
211
+ apiShortnames , err := generator . ParseAPIShortnames ( c . googleapisDir , confs , generator . ManualEntries )
180
212
if err != nil {
181
213
return err
182
214
}
183
- if err := gensnippets .GenerateSnippetsDirs (c .googleCloudDir , snippetDir , apiShortnames , c .modules ); err != nil {
215
+ dirs := c .getDirs ()
216
+ if err := gensnippets .GenerateSnippetsDirs (c .googleCloudDir , snippetDir , apiShortnames , dirs ); err != nil {
184
217
log .Printf ("warning: got the following non-fatal errors generating snippets: %v" , err )
185
218
}
186
- if err := c .replaceAllForSnippets (c . googleCloudDir , snippetDir ); err != nil {
219
+ if err := c .replaceAllForSnippets (snippetDir ); err != nil {
187
220
return err
188
221
}
189
222
if err := gocmd .ModTidy (snippetDir ); err != nil {
@@ -193,15 +226,44 @@ func (c *config) RegenSnippets() error {
193
226
return nil
194
227
}
195
228
196
- func (c * config ) replaceAllForSnippets (googleCloudDir , snippetDir string ) error {
197
- return execv .ForEachMod (googleCloudDir , func (dir string ) error {
229
+ // getChangedClientConfs iterates through the MicrogenGapicConfigs and returns
230
+ // a slice of the entries corresponding to modified modules and clients
231
+ func (c * config ) getChangedClientConfs () []* generator.MicrogenConfig {
232
+ if len (c .modules ) != 0 {
233
+ runConfs := []* generator.MicrogenConfig {}
234
+ for _ , conf := range generator .MicrogenGapicConfigs {
235
+ for _ , scope := range c .modules {
236
+ scopePathElement := "/" + scope + "/"
237
+ if strings .Contains (conf .InputDirectoryPath , scopePathElement ) {
238
+ runConfs = append (runConfs , conf )
239
+ }
240
+ }
241
+ }
242
+ return runConfs
243
+ }
244
+ return generator .MicrogenGapicConfigs
245
+ }
246
+
247
+ func (c * config ) replaceAllForSnippets (snippetDir string ) error {
248
+ return execv .ForEachMod (c .googleCloudDir , func (dir string ) error {
249
+ processMod := false
198
250
if c .modules != nil {
251
+ // Checking each path component in its entirety prevents mistaken addition of modules whose names
252
+ // contain the scope as a substring. For example if the scope is "video" we do not want to regenerate
253
+ // snippets for "videointelligence"
254
+ dirSlice := strings .Split (dir , "/" )
199
255
for _ , mod := range c .modules {
200
- if ! strings .Contains (dir , mod ) {
201
- return nil
256
+ for _ , dirElem := range dirSlice {
257
+ if mod == dirElem {
258
+ processMod = true
259
+ break
260
+ }
202
261
}
203
262
}
204
263
}
264
+ if ! processMod {
265
+ return nil
266
+ }
205
267
if dir == snippetDir {
206
268
return nil
207
269
}
@@ -280,7 +342,7 @@ func docURL(cloudDir, importPath string) (string, error) {
280
342
return "https://cloud.google.com/go/docs/reference/" + mod + "/latest/" + pkgPath , nil
281
343
}
282
344
283
- func (c * config ) AmendPRDescription (ctx context.Context ) error {
345
+ func (c * config ) SetScopesAndPRInfo (ctx context.Context ) error {
284
346
log .Println ("Amending PR title and body" )
285
347
pr , err := c .getPR (ctx )
286
348
if err != nil {
@@ -290,13 +352,16 @@ func (c *config) AmendPRDescription(ctx context.Context) error {
290
352
if err != nil {
291
353
return err
292
354
}
293
- return c .writePRCommitToFile (newPRTitle , newPRBody )
355
+ c .prTitle = newPRTitle
356
+ c .prBody = newPRBody
357
+ return nil
294
358
}
295
359
296
360
func (c * config ) processCommit (title , body string ) (string , string , error ) {
297
361
var newPRTitle string
298
362
var commitTitle string
299
363
var commitTitleIndex int
364
+ var modules []string
300
365
301
366
bodySlice := strings .Split (body , "\n " )
302
367
for index , line := range bodySlice {
@@ -312,7 +377,12 @@ func (c *config) processCommit(title, body string) (string, string, error) {
312
377
continue
313
378
}
314
379
hash := extractHashFromLine (line )
315
- scope , err := c .getScopeFromGoogleapisCommitHash (hash )
380
+ scopes , err := c .getScopesFromGoogleapisCommitHash (hash )
381
+ modules = append (modules , scopes ... )
382
+ var scope string
383
+ if len (scopes ) == 1 {
384
+ scope = scopes [0 ]
385
+ }
316
386
if err != nil {
317
387
return "" , "" , err
318
388
}
@@ -324,6 +394,7 @@ func (c *config) processCommit(title, body string) (string, string, error) {
324
394
bodySlice [commitTitleIndex ] = newCommitTitle
325
395
}
326
396
body = strings .Join (bodySlice , "\n " )
397
+ c .modules = append (c .modules , modules ... )
327
398
return newPRTitle , body , nil
328
399
}
329
400
@@ -349,14 +420,14 @@ func (c *config) getPR(ctx context.Context) (*github.PullRequest, error) {
349
420
return owlbotPR , nil
350
421
}
351
422
352
- func (c * config ) getScopeFromGoogleapisCommitHash (commitHash string ) (string , error ) {
423
+ func (c * config ) getScopesFromGoogleapisCommitHash (commitHash string ) ([] string , error ) {
353
424
files , err := c .filesChanged (commitHash )
354
425
if err != nil {
355
- return "" , err
426
+ return nil , err
356
427
}
357
428
// if no files changed, return empty string
358
429
if len (files ) == 0 {
359
- return "" , nil
430
+ return nil , nil
360
431
}
361
432
scopesMap := make (map [string ]bool )
362
433
scopes := []string {}
@@ -375,12 +446,7 @@ func (c *config) getScopeFromGoogleapisCommitHash(commitHash string) (string, er
375
446
}
376
447
}
377
448
}
378
- // if no in-scope packages are found or if many packages found, return empty string
379
- if len (scopes ) != 1 {
380
- return "" , nil
381
- }
382
- // if single scope found, return
383
- return scopes [0 ], nil
449
+ return scopes , nil
384
450
}
385
451
386
452
// filesChanged returns a list of files changed in a commit for the provdied
@@ -421,8 +487,9 @@ func updateCommitTitle(title, titlePkg string) string {
421
487
return newTitle
422
488
}
423
489
424
- // writePRCommitToFile uses OwlBot env variable specified path to write updated PR title and body at that location
425
- func (c * config ) writePRCommitToFile (title , body string ) error {
490
+ // WritePRInfoToFile uses OwlBot env variable specified path to write updated
491
+ // PR title and body at that location
492
+ func (c * config ) WritePRInfoToFile () error {
426
493
// if file exists at location, delete
427
494
if err := os .Remove (c .prFilepath ); err != nil {
428
495
if errors .Is (err , fs .ErrNotExist ) {
@@ -436,7 +503,12 @@ func (c *config) writePRCommitToFile(title, body string) error {
436
503
return err
437
504
}
438
505
defer f .Close ()
439
- if _ , err := f .WriteString (fmt .Sprintf ("%s\n \n %s" , title , body )); err != nil {
506
+ if c .prTitle == "" && c .prBody == "" {
507
+ log .Println ("No updated PR info found, will not write PR title and description to file." )
508
+ return nil
509
+ }
510
+ log .Println ("Writing PR title and description to file." )
511
+ if _ , err := f .WriteString (fmt .Sprintf ("%s\n \n %s" , c .prTitle , c .prBody )); err != nil {
440
512
return err
441
513
}
442
514
return nil
0 commit comments