Skip to content

Commit 1bdb6b8

Browse files
authored
Detect changes in external modules' subpackages (#3)
* Detect changes in external modules' subpackages Previously patrol would only catch a dependency upgrade in go.mod if a local package was referring the top-level package in that dependency. For example if your go.mod looked something like: ``` module github.com/your-org/your-module go 1.17 require github.com/another-org/their-module v1.8.0 ``` And you were to upgrade github.com/another-org/their-module to version v1.8.1, only the packages in your module relying on github.com/another-org/their-module would be marked as changed, while packages depending on other nested package in that dependency wouldn't (e.g. github.com/another-org/their-module/writer).
1 parent acb5260 commit 1bdb6b8

File tree

18 files changed

+162
-5
lines changed

18 files changed

+162
-5
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
steps:
1010
- name: Checkout
1111
uses: actions/checkout@v2
12-
- uses: utilitywarehouse/setup-go@main
12+
- uses: utilitywarehouse/actions-go/setup@main
1313
with:
1414
go-version: '^1.17'
1515
- name: Lint
@@ -22,7 +22,7 @@ jobs:
2222
runs-on: ubuntu-latest
2323
steps:
2424
- uses: actions/checkout@v2
25-
- uses: utilitywarehouse/setup-go@main
25+
- uses: utilitywarehouse/actions-go/setup@main
2626
with:
2727
go-version: '^1.17'
2828
- name: test

patrol/patrol_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ func (test *RepoTest) Run(t *testing.T) {
3535
tmp, err := os.MkdirTemp("", "")
3636
require.NoError(t, err)
3737

38-
fmt.Println(tmp)
39-
4038
// init repo
4139
repo, err := git.PlainInit(tmp, false)
4240
require.NoError(t, err)
@@ -105,7 +103,9 @@ type RepoTests []RepoTest
105103

106104
func (tests RepoTests) Run(t *testing.T) {
107105
for _, test := range tests {
108-
test.Run(t)
106+
t.Run(test.Name, func(t *testing.T) {
107+
test.Run(t)
108+
})
109109
}
110110
}
111111

patrol/repo.go

+23
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,30 @@ func (r *Repo) addPackage(path string, imports []string) {
151151
}
152152
r.addDependant(pkg, dependency)
153153
alreadyProcessedImports[dependency] = struct{}{}
154+
155+
// if the dependency is part of an external dependency (defined in go.mod)
156+
// add the parent module as a dependency as well so that a simple version
157+
// change would mark this package as changed
158+
if parent, ok := r.externalModule(dependency); ok {
159+
if _, alreadyProcessed := alreadyProcessedImports[parent]; alreadyProcessed {
160+
continue
161+
}
162+
r.addDependant(pkg, parent)
163+
alreadyProcessedImports[parent] = struct{}{}
164+
}
165+
}
166+
}
167+
168+
// externalModule checks if the given package is part of one of the modules required
169+
// as dependencies in go.mod. If it is it returns the name of the parent
170+
// package and true.
171+
func (r *Repo) externalModule(pkg string) (string, bool) {
172+
for _, req := range r.Module.Require {
173+
if strings.HasPrefix(pkg, req.Mod.Path) {
174+
return req.Mod.Path, true
175+
}
154176
}
177+
return "", false
155178
}
156179

157180
// addDependant adds dependant as one of the dependants of the package

patrol/repo_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ func TestRepo(t *testing.T) {
2828
Description: "A change to package x that is being tested " +
2929
"using x_test package should not result in a stack overflow :D",
3030
},
31+
RepoTest{
32+
TestdataFolder: "submodules",
33+
Name: "change in go modules dependency sub package",
34+
Description: "A change to a go modules dependency\n" +
35+
"should flag depending packages as changed",
36+
},
37+
RepoTest{
38+
TestdataFolder: "alias",
39+
Name: "change in go modules dependency that was aliased",
40+
Description: "A change to a go modules dependency\n" +
41+
"should flag depending packages as changed",
42+
},
3143
}
3244

3345
tests.Run(t)
+9
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.0
6+
7+
require (
8+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
9+
)
+12
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=
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
log "github.com/sirupsen/logrus"
5+
)
6+
7+
func main() {
8+
log.Debug("hey")
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github.com/utilitywarehouse/modules
+9
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+
)
+12
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=
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
log "github.com/sirupsen/logrus"
5+
)
6+
7+
func main() {
8+
log.Debug("hey")
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/utilitywarehouse/submodules
2+
3+
go 1.17
4+
5+
require github.com/sirupsen/logrus v1.8.0
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,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/submodules/sub
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/utilitywarehouse/submodules
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,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)