Skip to content

Commit de2befc

Browse files
authored
fix(internal/gapicgen): properly update modules that have no gapic changes (#5945)
Previously this was happening thanks to some logic the revolved around looking for UNKNOWN date in a generated gapic client. This logic was removed around a month ago and hense PRs that have genproto changes but no gapic changes have needed some manual updates. With this change the update process now parses out what kind of changes happened based on context aware commit messages. This means even if there we not chnages to the gapic client genproto will still get updated to the latest version if there were changes to message types.
1 parent 2fe50f7 commit de2befc

File tree

3 files changed

+88
-28
lines changed

3 files changed

+88
-28
lines changed

internal/gapicgen/generator/gapics.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"cloud.google.com/go/internal/gapicgen/execv"
3030
"cloud.google.com/go/internal/gapicgen/execv/gocmd"
3131
"cloud.google.com/go/internal/gapicgen/gensnippets"
32-
"cloud.google.com/go/internal/gapicgen/git"
3332
"gopkg.in/yaml.v2"
3433
)
3534

@@ -617,33 +616,6 @@ func ParseAPIShortnames(googleapisDir string, confs []*MicrogenConfig, manualEnt
617616
return shortnames, nil
618617
}
619618

620-
func (g *GapicGenerator) findModifiedDirs() ([]string, error) {
621-
log.Println("finding modifiled directories")
622-
files, err := git.FindModifiedAndUntrackedFiles(g.googleCloudDir)
623-
if err != nil {
624-
return nil, err
625-
}
626-
dirs := map[string]bool{}
627-
for _, file := range files {
628-
dir := filepath.Dir(filepath.Join(g.googleCloudDir, file))
629-
dirs[dir] = true
630-
}
631-
632-
// Add modified dirs from genproto. Sometimes only a request struct will be
633-
// updated, in these cases we should still make modifications the
634-
// corresponding gapic directories.
635-
for _, pkg := range g.modifiedPkgs {
636-
dir := filepath.Join(g.googleCloudDir, pkg)
637-
dirs[dir] = true
638-
}
639-
640-
var dirList []string
641-
for dir := range dirs {
642-
dirList = append(dirList, dir)
643-
}
644-
return dirList, nil
645-
}
646-
647619
func docURL(cloudDir, importPath string) (string, error) {
648620
suffix := strings.TrimPrefix(importPath, "cloud.google.com/go/")
649621
mod, err := gocmd.CurrentMod(filepath.Join(cloudDir, suffix))

internal/gapicgen/git/github.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os/user"
2525
"path"
2626
"path/filepath"
27+
"regexp"
2728
"strings"
2829
"time"
2930

@@ -68,6 +69,8 @@ genbot to assign reviewers to the google-cloud-go PR.
6869
`
6970
)
7071

72+
var conventionalCommitScopeRe = regexp.MustCompile(`.*\((.*)\): .*`)
73+
7174
// PullRequest represents a GitHub pull request.
7275
type PullRequest struct {
7376
Author string
@@ -402,6 +405,15 @@ func updateDeps(tmpDir string) error {
402405
updatedModDirs[modDir] = true
403406
}
404407

408+
// Find modules based on conventional commit messages.
409+
commitModDirs, err := processCommitMessage(tmpDir)
410+
if err != nil {
411+
return err
412+
}
413+
for _, v := range commitModDirs {
414+
updatedModDirs[v] = true
415+
}
416+
405417
// Update required modules.
406418
for modDir := range updatedModDirs {
407419
log.Printf("Updating module dir %q", modDir)
@@ -442,3 +454,40 @@ fi
442454
c.Dir = tmpDir
443455
return c.Run()
444456
}
457+
458+
// processCommitMessage process the context aware commits mentioned in the PR
459+
// body to determine what modules need to have dependency bumps.
460+
func processCommitMessage(dir string) ([]string, error) {
461+
c := execv.Command("git", "log", "-1", "--pretty=%B")
462+
c.Dir = dir
463+
out, err := c.Output()
464+
if err != nil {
465+
return nil, err
466+
}
467+
outStr := string(out)
468+
ss := strings.Split(outStr, "Changes:\n\n")
469+
if len(ss) != 2 {
470+
return nil, fmt.Errorf("unable to process commit msg")
471+
}
472+
commits := strings.Split(ss[1], "\n\n")
473+
var modDirs []string
474+
for _, v := range commits {
475+
pkg := parsePackage(v)
476+
if pkg == "" {
477+
continue
478+
}
479+
modDirs = append(modDirs, filepath.Join(dir, pkg))
480+
}
481+
return modDirs, nil
482+
}
483+
484+
// parsePackage parses a package name from the conventional commit scope of a
485+
// commit message.
486+
func parsePackage(msg string) string {
487+
matches := conventionalCommitScopeRe.FindStringSubmatch(msg)
488+
if len(matches) < 2 {
489+
return ""
490+
}
491+
return matches[1]
492+
493+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package git
16+
17+
import "testing"
18+
19+
func TestParsePackage(t *testing.T) {
20+
tests := []struct {
21+
name string
22+
input string
23+
want string
24+
}{
25+
{"found basic package", "feat(foo): something", "foo"},
26+
{"found nested package", "fix(foo/bar): something", "foo/bar"},
27+
{"no scope", "chore: something", ""},
28+
{"bad commit msg", "something happend", ""},
29+
}
30+
31+
for _, tc := range tests {
32+
t.Run(tc.name, func(t *testing.T) {
33+
got := parsePackage(tc.input)
34+
if got != tc.want {
35+
t.Errorf("got %q, want %q", got, tc.want)
36+
}
37+
})
38+
}
39+
}

0 commit comments

Comments
 (0)