Skip to content

Commit

Permalink
Merge pull request #9 from utilitywarehouse/allChanges-handle-go-pack…
Browse files Browse the repository at this point in the history
…ages

All changes -  handle go nested packages
  • Loading branch information
matiit authored May 24, 2023
2 parents 3637bc4 + c52c924 commit 36e077b
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 4 deletions.
36 changes: 32 additions & 4 deletions patrol/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions patrol/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions patrol/testdata/nestedassets/commits/1/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
12 changes: 12 additions & 0 deletions patrol/testdata/nestedassets/commits/1/go.sum
Original file line number Diff line number Diff line change
@@ -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=
7 changes: 7 additions & 0 deletions patrol/testdata/nestedassets/commits/1/sub/migrations/001.sql
Original file line number Diff line number Diff line change
@@ -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
);
9 changes: 9 additions & 0 deletions patrol/testdata/nestedassets/commits/1/sub/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package sub

import (
"github.com/sirupsen/logrus/hooks/writer"
)

type A struct {
writer.Hook
}
1 change: 1 addition & 0 deletions patrol/testdata/nestedassets/commits/2/changes.patrol
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/utilitywarehouse/modules/sub
9 changes: 9 additions & 0 deletions patrol/testdata/nestedassets/commits/2/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
12 changes: 12 additions & 0 deletions patrol/testdata/nestedassets/commits/2/go.sum
Original file line number Diff line number Diff line change
@@ -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=
8 changes: 8 additions & 0 deletions patrol/testdata/nestedassets/commits/2/sub/migrations/001.sql
Original file line number Diff line number Diff line change
@@ -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
);
9 changes: 9 additions & 0 deletions patrol/testdata/nestedassets/commits/2/sub/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package sub

import (
"github.com/sirupsen/logrus/hooks/writer"
)

type A struct {
writer.Hook
}

0 comments on commit 36e077b

Please sign in to comment.