Skip to content

Commit 36e077b

Browse files
authored
Merge pull request #9 from utilitywarehouse/allChanges-handle-go-packages
All changes - handle go nested packages
2 parents 3637bc4 + c52c924 commit 36e077b

File tree

11 files changed

+115
-4
lines changed

11 files changed

+115
-4
lines changed

patrol/repo.go

+32-4
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error {
245245
}
246246

247247
for _, change := range diff {
248-
if !allFiles && !strings.HasSuffix(change.From.Name, ".go") {
248+
goFile := strings.HasSuffix(change.From.Name, ".go")
249+
if !allFiles && !goFile {
249250
// we're only interested in Go files
250251
continue
251252
}
@@ -259,11 +260,17 @@ func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error {
259260

260261
// package is part of our module
261262
if pkgName == "" {
262-
if allFiles {
263-
pkgName = r.ModuleName()
264-
} else {
263+
if goFile {
264+
// go files are always in packages
265265
pkgName = r.ModuleName() + "/" + filepath.Dir(change.From.Name)
266266
}
267+
if allFiles && !goFile {
268+
// Non go files belong to the closest package
269+
pkgName, err = r.closestPackageForFileInModule(change.From.Name)
270+
if err != nil {
271+
return err
272+
}
273+
}
267274
}
268275

269276
r.flagPackageAsChanged(pkgName)
@@ -272,6 +279,27 @@ func (r *Repo) detectInternalChangesFrom(revision string, allFiles bool) error {
272279
return nil
273280
}
274281

282+
// closestPackageForFileInModule returns the closest go package path for the given file
283+
// it will return the module name if no package is found
284+
func (r *Repo) closestPackageForFileInModule(fileName string) (string, error) {
285+
currentDir := filepath.Dir(fileName)
286+
for currentDir != "." {
287+
files, err := os.ReadDir(r.path + "/" + currentDir)
288+
if err != nil {
289+
return "", err
290+
}
291+
292+
for _, f := range files {
293+
if strings.HasSuffix(f.Name(), ".go") {
294+
return r.ModuleName() + "/" + currentDir, nil
295+
}
296+
}
297+
298+
currentDir = filepath.Dir(currentDir)
299+
}
300+
return r.ModuleName(), nil
301+
}
302+
275303
// detectGoModulesChanges finds differences in dependencies required by
276304
// HEAD:go.mod and {revision}:go.mod and flags as changed any packages
277305
// depending on any of the changed dependencies.

patrol/repo_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ func TestRepo(t *testing.T) {
5353
"should flag a package as changed",
5454
AllFiles: true,
5555
},
56+
RepoTest{
57+
TestdataFolder: "nestedassets",
58+
Name: "change in files that are not go source files, nested",
59+
Description: "A change to a file that is not a go source file\n" +
60+
"should flag a sub package as changed",
61+
AllFiles: true,
62+
},
5663
}
5764

5865
tests.Run(t)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/utilitywarehouse/modules
2+
3+
go 1.17
4+
5+
require github.com/sirupsen/logrus v1.8.1
6+
7+
require (
8+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
9+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g=
4+
github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU=
8+
github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A=
9+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
10+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
11+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
12+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE users (
2+
id SERIAL PRIMARY KEY,
3+
username VARCHAR(255) UNIQUE NOT NULL,
4+
password VARCHAR(255) NOT NULL,
5+
email VARCHAR(255) UNIQUE NOT NULL,
6+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
7+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package sub
2+
3+
import (
4+
"github.com/sirupsen/logrus/hooks/writer"
5+
)
6+
7+
type A struct {
8+
writer.Hook
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github.com/utilitywarehouse/modules/sub
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/utilitywarehouse/modules
2+
3+
go 1.17
4+
5+
require github.com/sirupsen/logrus v1.8.1
6+
7+
require (
8+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
9+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g=
4+
github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU=
8+
github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A=
9+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
10+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
11+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
12+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE users (
2+
id SERIAL PRIMARY KEY,
3+
username VARCHAR(255) UNIQUE NOT NULL,
4+
password VARCHAR(255) NOT NULL,
5+
email VARCHAR(255) UNIQUE NOT NULL,
6+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
7+
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
8+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package sub
2+
3+
import (
4+
"github.com/sirupsen/logrus/hooks/writer"
5+
)
6+
7+
type A struct {
8+
writer.Hook
9+
}

0 commit comments

Comments
 (0)