From f94795ca0b45b0b10821d4ab991db494d70fd6bf Mon Sep 17 00:00:00 2001 From: Mateusz Tracz Date: Wed, 24 May 2023 13:00:15 +0100 Subject: [PATCH 1/2] Add testdata --- patrol/repo_test.go | 7 +++++++ patrol/testdata/nestedassets/commits/1/go.mod | 9 +++++++++ patrol/testdata/nestedassets/commits/1/go.sum | 12 ++++++++++++ .../nestedassets/commits/1/sub/migrations/001.sql | 7 +++++++ patrol/testdata/nestedassets/commits/1/sub/module.go | 9 +++++++++ .../testdata/nestedassets/commits/2/changes.patrol | 1 + patrol/testdata/nestedassets/commits/2/go.mod | 9 +++++++++ patrol/testdata/nestedassets/commits/2/go.sum | 12 ++++++++++++ .../nestedassets/commits/2/sub/migrations/001.sql | 8 ++++++++ patrol/testdata/nestedassets/commits/2/sub/module.go | 9 +++++++++ 10 files changed, 83 insertions(+) create mode 100644 patrol/testdata/nestedassets/commits/1/go.mod create mode 100644 patrol/testdata/nestedassets/commits/1/go.sum create mode 100644 patrol/testdata/nestedassets/commits/1/sub/migrations/001.sql create mode 100644 patrol/testdata/nestedassets/commits/1/sub/module.go create mode 100644 patrol/testdata/nestedassets/commits/2/changes.patrol create mode 100644 patrol/testdata/nestedassets/commits/2/go.mod create mode 100644 patrol/testdata/nestedassets/commits/2/go.sum create mode 100644 patrol/testdata/nestedassets/commits/2/sub/migrations/001.sql create mode 100644 patrol/testdata/nestedassets/commits/2/sub/module.go diff --git a/patrol/repo_test.go b/patrol/repo_test.go index 9ab2553..eb58361 100644 --- a/patrol/repo_test.go +++ b/patrol/repo_test.go @@ -53,6 +53,13 @@ func TestRepo(t *testing.T) { "should flag a package as changed", AllFiles: true, }, + RepoTest{ + TestdataFolder: "nestedassets", + Name: "change in files that are not go source files, nested", + Description: "A change to a file that is not a go source file\n" + + "should flag a sub package as changed", + AllFiles: true, + }, } tests.Run(t) diff --git a/patrol/testdata/nestedassets/commits/1/go.mod b/patrol/testdata/nestedassets/commits/1/go.mod new file mode 100644 index 0000000..e5cb8b4 --- /dev/null +++ b/patrol/testdata/nestedassets/commits/1/go.mod @@ -0,0 +1,9 @@ +module github.com/utilitywarehouse/modules + +go 1.17 + +require github.com/sirupsen/logrus v1.8.1 + +require ( + golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect +) diff --git a/patrol/testdata/nestedassets/commits/1/go.sum b/patrol/testdata/nestedassets/commits/1/go.sum new file mode 100644 index 0000000..a42fced --- /dev/null +++ b/patrol/testdata/nestedassets/commits/1/go.sum @@ -0,0 +1,12 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g= +github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU= +github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/patrol/testdata/nestedassets/commits/1/sub/migrations/001.sql b/patrol/testdata/nestedassets/commits/1/sub/migrations/001.sql new file mode 100644 index 0000000..59f18f3 --- /dev/null +++ b/patrol/testdata/nestedassets/commits/1/sub/migrations/001.sql @@ -0,0 +1,7 @@ +CREATE TABLE users ( + id SERIAL PRIMARY KEY, + username VARCHAR(255) UNIQUE NOT NULL, + password VARCHAR(255) NOT NULL, + email VARCHAR(255) UNIQUE NOT NULL, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); \ No newline at end of file diff --git a/patrol/testdata/nestedassets/commits/1/sub/module.go b/patrol/testdata/nestedassets/commits/1/sub/module.go new file mode 100644 index 0000000..10cad93 --- /dev/null +++ b/patrol/testdata/nestedassets/commits/1/sub/module.go @@ -0,0 +1,9 @@ +package sub + +import ( + "github.com/sirupsen/logrus/hooks/writer" +) + +type A struct { + writer.Hook +} diff --git a/patrol/testdata/nestedassets/commits/2/changes.patrol b/patrol/testdata/nestedassets/commits/2/changes.patrol new file mode 100644 index 0000000..955ff8c --- /dev/null +++ b/patrol/testdata/nestedassets/commits/2/changes.patrol @@ -0,0 +1 @@ +github.com/utilitywarehouse/modules/sub diff --git a/patrol/testdata/nestedassets/commits/2/go.mod b/patrol/testdata/nestedassets/commits/2/go.mod new file mode 100644 index 0000000..e5cb8b4 --- /dev/null +++ b/patrol/testdata/nestedassets/commits/2/go.mod @@ -0,0 +1,9 @@ +module github.com/utilitywarehouse/modules + +go 1.17 + +require github.com/sirupsen/logrus v1.8.1 + +require ( + golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect +) diff --git a/patrol/testdata/nestedassets/commits/2/go.sum b/patrol/testdata/nestedassets/commits/2/go.sum new file mode 100644 index 0000000..a42fced --- /dev/null +++ b/patrol/testdata/nestedassets/commits/2/go.sum @@ -0,0 +1,12 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g= +github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU= +github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/patrol/testdata/nestedassets/commits/2/sub/migrations/001.sql b/patrol/testdata/nestedassets/commits/2/sub/migrations/001.sql new file mode 100644 index 0000000..9318892 --- /dev/null +++ b/patrol/testdata/nestedassets/commits/2/sub/migrations/001.sql @@ -0,0 +1,8 @@ +CREATE TABLE users ( + id SERIAL PRIMARY KEY, + username VARCHAR(255) UNIQUE NOT NULL, + password VARCHAR(255) NOT NULL, + email VARCHAR(255) UNIQUE NOT NULL, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); \ No newline at end of file diff --git a/patrol/testdata/nestedassets/commits/2/sub/module.go b/patrol/testdata/nestedassets/commits/2/sub/module.go new file mode 100644 index 0000000..10cad93 --- /dev/null +++ b/patrol/testdata/nestedassets/commits/2/sub/module.go @@ -0,0 +1,9 @@ +package sub + +import ( + "github.com/sirupsen/logrus/hooks/writer" +) + +type A struct { + writer.Hook +} From c52c92417a285f15db8375b9ea32fe1a38bfde6d Mon Sep 17 00:00:00 2001 From: Mateusz Tracz Date: Wed, 24 May 2023 13:09:32 +0100 Subject: [PATCH 2/2] Handle nested changes --- patrol/repo.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/patrol/repo.go b/patrol/repo.go index ed1865e..b312676 100644 --- a/patrol/repo.go +++ b/patrol/repo.go @@ -245,7 +245,8 @@ func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error { } for _, change := range diff { - if !allFiles && !strings.HasSuffix(change.From.Name, ".go") { + goFile := strings.HasSuffix(change.From.Name, ".go") + if !allFiles && !goFile { // we're only interested in Go files continue } @@ -259,11 +260,17 @@ func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error { // package is part of our module if pkgName == "" { - if allFiles { - pkgName = r.ModuleName() - } else { + if goFile { + // go files are always in packages pkgName = r.ModuleName() + "/" + filepath.Dir(change.From.Name) } + if allFiles && !goFile { + // Non go files belong to the closest package + pkgName, err = r.closestPackageForFileInModule(change.From.Name) + if err != nil { + return err + } + } } r.flagPackageAsChanged(pkgName) @@ -272,6 +279,27 @@ func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error { return nil } +// closestPackageForFileInModule returns the closest go package path for the given file +// it will return the module name if no package is found +func (r *Repo) closestPackageForFileInModule(fileName string) (string, error) { + currentDir := filepath.Dir(fileName) + for currentDir != "." { + files, err := os.ReadDir(r.path + "/" + currentDir) + if err != nil { + return "", err + } + + for _, f := range files { + if strings.HasSuffix(f.Name(), ".go") { + return r.ModuleName() + "/" + currentDir, nil + } + } + + currentDir = filepath.Dir(currentDir) + } + return r.ModuleName(), nil +} + // detectGoModulesChanges finds differences in dependencies required by // HEAD:go.mod and {revision}:go.mod and flags as changed any packages // depending on any of the changed dependencies.