Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(internal/gapicgen): properly update modules that have no gapic changes #5945

Merged
merged 3 commits into from Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 0 additions & 28 deletions internal/gapicgen/generator/gapics.go
Expand Up @@ -29,7 +29,6 @@ import (
"cloud.google.com/go/internal/gapicgen/execv"
"cloud.google.com/go/internal/gapicgen/execv/gocmd"
"cloud.google.com/go/internal/gapicgen/gensnippets"
"cloud.google.com/go/internal/gapicgen/git"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -617,33 +616,6 @@ func ParseAPIShortnames(googleapisDir string, confs []*MicrogenConfig, manualEnt
return shortnames, nil
}

func (g *GapicGenerator) findModifiedDirs() ([]string, error) {
log.Println("finding modifiled directories")
files, err := git.FindModifiedAndUntrackedFiles(g.googleCloudDir)
if err != nil {
return nil, err
}
dirs := map[string]bool{}
for _, file := range files {
dir := filepath.Dir(filepath.Join(g.googleCloudDir, file))
dirs[dir] = true
}

// Add modified dirs from genproto. Sometimes only a request struct will be
// updated, in these cases we should still make modifications the
// corresponding gapic directories.
for _, pkg := range g.modifiedPkgs {
dir := filepath.Join(g.googleCloudDir, pkg)
dirs[dir] = true
}

var dirList []string
for dir := range dirs {
dirList = append(dirList, dir)
}
return dirList, nil
}

codyoss marked this conversation as resolved.
Show resolved Hide resolved
func docURL(cloudDir, importPath string) (string, error) {
suffix := strings.TrimPrefix(importPath, "cloud.google.com/go/")
mod, err := gocmd.CurrentMod(filepath.Join(cloudDir, suffix))
Expand Down
49 changes: 49 additions & 0 deletions internal/gapicgen/git/github.go
Expand Up @@ -24,6 +24,7 @@ import (
"os/user"
"path"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -68,6 +69,8 @@ genbot to assign reviewers to the google-cloud-go PR.
`
)

var conventionalCommitScopeRe = regexp.MustCompile(`.*\((.*)\): .*`)

// PullRequest represents a GitHub pull request.
type PullRequest struct {
Author string
Expand Down Expand Up @@ -402,6 +405,15 @@ func updateDeps(tmpDir string) error {
updatedModDirs[modDir] = true
}

// Find modules based on conventional commit messages.
commitModDirs, err := processCommitMessage(tmpDir)
if err != nil {
return err
}
for _, v := range commitModDirs {
updatedModDirs[v] = true
}

// Update required modules.
for modDir := range updatedModDirs {
log.Printf("Updating module dir %q", modDir)
Expand Down Expand Up @@ -442,3 +454,40 @@ fi
c.Dir = tmpDir
return c.Run()
}

// processCommitMessage process the context aware commits mentioned in the PR
// body to determine what modules need to have dependency bumps.
func processCommitMessage(dir string) ([]string, error) {
c := execv.Command("git", "log", "-1", "--pretty=%B")
c.Dir = dir
out, err := c.Output()
if err != nil {
return nil, err
}
outStr := string(out)
ss := strings.Split(outStr, "Changes:\n\n")
if len(ss) != 2 {
return nil, fmt.Errorf("unable to process commit msg")
}
commits := strings.Split(ss[1], "\n\n")
var modDirs []string
for _, v := range commits {
pkg := parsePackage(v)
if pkg == "" {
continue
}
modDirs = append(modDirs, filepath.Join(dir, pkg))
}
return modDirs, nil
}

// parsePackage parses a package name from the conventional commit scope of a
// commit message.
func parsePackage(msg string) string {
matches := conventionalCommitScopeRe.FindStringSubmatch(msg)
if len(matches) < 2 {
return ""
}
return matches[1]

}
39 changes: 39 additions & 0 deletions internal/gapicgen/git/github_test.go
@@ -0,0 +1,39 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package git

import "testing"

func TestParsePackage(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{"found basic package", "feat(foo): something", "foo"},
{"found nested package", "fix(foo/bar): something", "foo/bar"},
{"no scope", "chore: something", ""},
{"bad commit msg", "something happend", ""},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := parsePackage(tc.input)
if got != tc.want {
t.Errorf("got %q, want %q", got, tc.want)
}
})
}
}