Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit220f8a5

Browse files
authored
feat(internal/postprocessor): only regen snippets for changed modules (#7300)
This change identifies the changed modules in the PR using the commit URLs in the open OwlBot PR description. The scopes are used to limit snippet regeneration to only those modules.
1 parent935ba3f commit220f8a5

File tree

1 file changed

+107
-35
lines changed

1 file changed

+107
-35
lines changed

‎internal/postprocessor/main.go‎

Lines changed: 107 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,12 @@ func main() {
7272
log.Println("googleapis-dir set to",*googleapisDir)
7373
log.Println("branch set to",*branchOverride)
7474
log.Println("prFilepath is",*prFilepath)
75+
log.Println("directories are",*directories)
7576

76-
varmodules []string
77+
dirSlice:= []string{}
7778
if*directories!="" {
7879
dirSlice:=strings.Split(*directories,",")
79-
for_,dir:=rangedirSlice {
80-
modules=append(modules,filepath.Join(*clientRoot,dir))
81-
}
82-
log.Println("Postprocessor running on",modules)
80+
log.Println("Postprocessor running on",dirSlice)
8381
}else {
8482
log.Println("Postprocessor running on all modules.")
8583
}
@@ -103,11 +101,13 @@ func main() {
103101
c:=&config{
104102
googleapisDir:*googleapisDir,
105103
googleCloudDir:*clientRoot,
106-
modules:modules,
104+
modules:dirSlice,
107105
branchOverride:*branchOverride,
108106
githubUsername:*githubUsername,
109107
prFilepath:*prFilepath,
110108
runAll:runAll,
109+
prTitle:"",
110+
prBody:"",
111111
}
112112

113113
iferr:=c.run(ctx);err!=nil {
@@ -120,11 +120,19 @@ func main() {
120120
typeconfigstruct {
121121
googleapisDirstring
122122
googleCloudDirstring
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+
124130
branchOverridestring
125131
githubUsernamestring
126132
prFilepathstring
127133
runAllbool
134+
prTitlestring
135+
prBodystring
128136
}
129137

130138
// 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 {
150158
log.Println("exiting post processing early")
151159
returnnil
152160
}
153-
iferr:=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+
iferr:=c.SetScopesAndPRInfo(ctx);err!=nil {
168+
returnerr
169+
}
170+
iferr:=c.TidyAffectedMods();err!=nil {
154171
returnerr
155172
}
156173
iferr:=gocmd.Vet(c.googleCloudDir);err!=nil {
@@ -162,28 +179,44 @@ func (c *config) run(ctx context.Context) error {
162179
if_,err:=c.Manifest(generator.MicrogenGapicConfigs);err!=nil {
163180
returnerr
164181
}
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-
iferr:=c.AmendPRDescription(ctx);err!=nil {
182+
iferr:=c.WritePRInfoToFile();err!=nil {
168183
returnerr
169184
}
170185
returnnil
171186
}
172187

188+
func (c*config)getDirs() []string {
189+
dirs:= []string{}
190+
for_,module:=rangec.modules {
191+
dirs=append(dirs,filepath.Join(c.googleCloudDir,module))
192+
}
193+
returndirs
194+
}
195+
196+
func (c*config)TidyAffectedMods()error {
197+
dirs:=c.getDirs()
198+
for_,dir:=rangedirs {
199+
iferr:=gocmd.ModTidy(dir);err!=nil {
200+
returnerr
201+
}
202+
}
203+
returnnil
204+
}
205+
173206
// RegenSnippets regenerates the snippets for all GAPICs configured to be generated.
174207
func (c*config)RegenSnippets()error {
175208
log.Println("regenerating snippets")
176-
177209
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)
180212
iferr!=nil {
181213
returnerr
182214
}
183-
iferr:=gensnippets.GenerateSnippetsDirs(c.googleCloudDir,snippetDir,apiShortnames,c.modules);err!=nil {
215+
dirs:=c.getDirs()
216+
iferr:=gensnippets.GenerateSnippetsDirs(c.googleCloudDir,snippetDir,apiShortnames,dirs);err!=nil {
184217
log.Printf("warning: got the following non-fatal errors generating snippets: %v",err)
185218
}
186-
iferr:=c.replaceAllForSnippets(c.googleCloudDir,snippetDir);err!=nil {
219+
iferr:=c.replaceAllForSnippets(snippetDir);err!=nil {
187220
returnerr
188221
}
189222
iferr:=gocmd.ModTidy(snippetDir);err!=nil {
@@ -193,15 +226,44 @@ func (c *config) RegenSnippets() error {
193226
returnnil
194227
}
195228

196-
func (c*config)replaceAllForSnippets(googleCloudDir,snippetDirstring)error {
197-
returnexecv.ForEachMod(googleCloudDir,func(dirstring)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+
iflen(c.modules)!=0 {
233+
runConfs:= []*generator.MicrogenConfig{}
234+
for_,conf:=rangegenerator.MicrogenGapicConfigs {
235+
for_,scope:=rangec.modules {
236+
scopePathElement:="/"+scope+"/"
237+
ifstrings.Contains(conf.InputDirectoryPath,scopePathElement) {
238+
runConfs=append(runConfs,conf)
239+
}
240+
}
241+
}
242+
returnrunConfs
243+
}
244+
returngenerator.MicrogenGapicConfigs
245+
}
246+
247+
func (c*config)replaceAllForSnippets(snippetDirstring)error {
248+
returnexecv.ForEachMod(c.googleCloudDir,func(dirstring)error {
249+
processMod:=false
198250
ifc.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,"/")
199255
for_,mod:=rangec.modules {
200-
if!strings.Contains(dir,mod) {
201-
returnnil
256+
for_,dirElem:=rangedirSlice {
257+
ifmod==dirElem {
258+
processMod=true
259+
break
260+
}
202261
}
203262
}
204263
}
264+
if!processMod {
265+
returnnil
266+
}
205267
ifdir==snippetDir {
206268
returnnil
207269
}
@@ -280,7 +342,7 @@ func docURL(cloudDir, importPath string) (string, error) {
280342
return"https://cloud.google.com/go/docs/reference/"+mod+"/latest/"+pkgPath,nil
281343
}
282344

283-
func (c*config)AmendPRDescription(ctx context.Context)error {
345+
func (c*config)SetScopesAndPRInfo(ctx context.Context)error {
284346
log.Println("Amending PR title and body")
285347
pr,err:=c.getPR(ctx)
286348
iferr!=nil {
@@ -290,13 +352,16 @@ func (c *config) AmendPRDescription(ctx context.Context) error {
290352
iferr!=nil {
291353
returnerr
292354
}
293-
returnc.writePRCommitToFile(newPRTitle,newPRBody)
355+
c.prTitle=newPRTitle
356+
c.prBody=newPRBody
357+
returnnil
294358
}
295359

296360
func (c*config)processCommit(title,bodystring) (string,string,error) {
297361
varnewPRTitlestring
298362
varcommitTitlestring
299363
varcommitTitleIndexint
364+
varmodules []string
300365

301366
bodySlice:=strings.Split(body,"\n")
302367
forindex,line:=rangebodySlice {
@@ -312,7 +377,12 @@ func (c *config) processCommit(title, body string) (string, string, error) {
312377
continue
313378
}
314379
hash:=extractHashFromLine(line)
315-
scope,err:=c.getScopeFromGoogleapisCommitHash(hash)
380+
scopes,err:=c.getScopesFromGoogleapisCommitHash(hash)
381+
modules=append(modules,scopes...)
382+
varscopestring
383+
iflen(scopes)==1 {
384+
scope=scopes[0]
385+
}
316386
iferr!=nil {
317387
return"","",err
318388
}
@@ -324,6 +394,7 @@ func (c *config) processCommit(title, body string) (string, string, error) {
324394
bodySlice[commitTitleIndex]=newCommitTitle
325395
}
326396
body=strings.Join(bodySlice,"\n")
397+
c.modules=append(c.modules,modules...)
327398
returnnewPRTitle,body,nil
328399
}
329400

@@ -349,14 +420,14 @@ func (c *config) getPR(ctx context.Context) (*github.PullRequest, error) {
349420
returnowlbotPR,nil
350421
}
351422

352-
func (c*config)getScopeFromGoogleapisCommitHash(commitHashstring) (string,error) {
423+
func (c*config)getScopesFromGoogleapisCommitHash(commitHashstring) ([]string,error) {
353424
files,err:=c.filesChanged(commitHash)
354425
iferr!=nil {
355-
return"",err
426+
returnnil,err
356427
}
357428
// if no files changed, return empty string
358429
iflen(files)==0 {
359-
return"",nil
430+
returnnil,nil
360431
}
361432
scopesMap:=make(map[string]bool)
362433
scopes:= []string{}
@@ -375,12 +446,7 @@ func (c *config) getScopeFromGoogleapisCommitHash(commitHash string) (string, er
375446
}
376447
}
377448
}
378-
// if no in-scope packages are found or if many packages found, return empty string
379-
iflen(scopes)!=1 {
380-
return"",nil
381-
}
382-
// if single scope found, return
383-
returnscopes[0],nil
449+
returnscopes,nil
384450
}
385451

386452
// filesChanged returns a list of files changed in a commit for the provdied
@@ -421,8 +487,9 @@ func updateCommitTitle(title, titlePkg string) string {
421487
returnnewTitle
422488
}
423489

424-
// writePRCommitToFile uses OwlBot env variable specified path to write updated PR title and body at that location
425-
func (c*config)writePRCommitToFile(title,bodystring)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 {
426493
// if file exists at location, delete
427494
iferr:=os.Remove(c.prFilepath);err!=nil {
428495
iferrors.Is(err,fs.ErrNotExist) {
@@ -436,7 +503,12 @@ func (c *config) writePRCommitToFile(title, body string) error {
436503
returnerr
437504
}
438505
deferf.Close()
439-
if_,err:=f.WriteString(fmt.Sprintf("%s\n\n%s",title,body));err!=nil {
506+
ifc.prTitle==""&&c.prBody=="" {
507+
log.Println("No updated PR info found, will not write PR title and description to file.")
508+
returnnil
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 {
440512
returnerr
441513
}
442514
returnnil

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp