Skip to content

Commit 6fd2bd7

Browse files
CharlieTLepellaredMrAlias
authored
Add pip to dbotconf (#307)
* Add pip to dbotconf requirements.txt files will now be tracked with dependabot. This is related to open-telemetry/opentelemetry-go#3996. * Create struct for updates * Update dbotconf/internal/verify_test.go Co-authored-by: Robert Pająk <[email protected]> * fix shadow declaration of "updates" * Generate change log * Update .chloggen/pip.yaml * Update dbotconf/internal/conf.go Co-authored-by: Tyler Yahn <[email protected]> * Update dbotconf/internal/testdata/dependabot.yml --------- Co-authored-by: Robert Pająk <[email protected]> Co-authored-by: Tyler Yahn <[email protected]>
1 parent 748ea6a commit 6fd2bd7

File tree

8 files changed

+288
-93
lines changed

8 files changed

+288
-93
lines changed

.chloggen/pip.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. crosslink)
5+
component: dbotconf
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: "Add support for pip package ecosystem."
9+
10+
# One or more tracking issues related to the change
11+
issues: [307]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

dbotconf/internal/conf.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ const (
1919
ghPkgEco = "github-actions"
2020
dockerPkgEco = "docker"
2121
gomodPkgEco = "gomod"
22+
pipPkgEco = "pip"
2223
)
2324

2425
var (
2526
weeklySchedule = schedule{Interval: "weekly", Day: "sunday"}
2627
actionLabels = []string{"dependencies", "actions", "Skip Changelog"}
2728
dockerLabels = []string{"dependencies", "docker", "Skip Changelog"}
2829
goLabels = []string{"dependencies", "go", "Skip Changelog"}
30+
pipLabels = []string{"dependencies", "python", "Skip Changelog"}
2931
)
3032

3133
type dependabotConfig struct {

dbotconf/internal/generate.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const header = "# File generated by dbotconf; DO NOT EDIT."
3030
var buildConfigFunc = buildConfig
3131

3232
// buildConfig constructs a dependabotConfig for all modules in the repo.
33-
func buildConfig(root string, mods []*modfile.File, dockerFiles []string) (*dependabotConfig, error) {
33+
func buildConfig(root string, mods []*modfile.File, dockerFiles []string, pipFiles []string) (*dependabotConfig, error) {
3434
c := &dependabotConfig{
3535
Version: version2,
3636
Updates: []update{
@@ -70,6 +70,21 @@ func buildConfig(root string, mods []*modfile.File, dockerFiles []string) (*depe
7070
Schedule: weeklySchedule,
7171
})
7272
}
73+
74+
for _, p := range pipFiles {
75+
local, err := localPath(root, p)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
c.Updates = append(c.Updates, update{
81+
PackageEcosystem: pipPkgEco,
82+
Directory: local,
83+
Labels: pipLabels,
84+
Schedule: weeklySchedule,
85+
})
86+
}
87+
7388
return c, nil
7489
}
7590

@@ -88,7 +103,12 @@ func generate() error {
88103
return err
89104
}
90105

91-
c, err := buildConfigFunc(root, mods, dockerFiles)
106+
pipFiles, err := allPipFunc(root)
107+
if err != nil {
108+
return err
109+
}
110+
111+
c, err := buildConfigFunc(root, mods, dockerFiles, pipFiles)
92112
if err != nil {
93113
return err
94114
}

dbotconf/internal/generate_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ func TestBuildConfig(t *testing.T) {
6969
"/home/user/repo/a/",
7070
"/home/user/repo/b/",
7171
}
72+
pipFiles := []string{
73+
"/home/user/repo/requirements.txt",
74+
}
7275

73-
got, err := buildConfig(root, mods, dockerFiles)
76+
got, err := buildConfig(root, mods, dockerFiles, pipFiles)
7477
require.NoError(t, err)
7578
assert.Equal(t, &dependabotConfig{
7679
Version: version2,
@@ -82,6 +85,7 @@ func TestBuildConfig(t *testing.T) {
8285
newUpdate(gomodPkgEco, "/", goLabels),
8386
newUpdate(gomodPkgEco, "/a", goLabels),
8487
newUpdate(gomodPkgEco, "/b", goLabels),
88+
newUpdate(pipPkgEco, "/", pipLabels),
8589
},
8690
}, got)
8791
}
@@ -119,11 +123,17 @@ func TestRunGenerateReturnBuildConfigError(t *testing.T) {
119123
allDockerFunc = func(string) ([]string, error) {
120124
return nil, nil
121125
}
126+
t.Cleanup(func(f func(string) ([]string, error)) func() {
127+
return func() { allPipFunc = f }
128+
}(allPipFunc))
129+
allPipFunc = func(string) ([]string, error) {
130+
return nil, nil
131+
}
122132

123-
t.Cleanup(func(f func(string, []*modfile.File, []string) (*dependabotConfig, error)) func() {
133+
t.Cleanup(func(f func(string, []*modfile.File, []string, []string) (*dependabotConfig, error)) func() {
124134
return func() { buildConfigFunc = f }
125135
}(buildConfigFunc))
126-
buildConfigFunc = func(string, []*modfile.File, []string) (*dependabotConfig, error) {
136+
buildConfigFunc = func(string, []*modfile.File, []string, []string) (*dependabotConfig, error) {
127137
return nil, assert.AnError
128138
}
129139
assert.ErrorIs(t, generate(), assert.AnError)

dbotconf/internal/mods.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
var (
2828
allModsFunc = allMods
2929
allDockerFunc = allDocker
30+
allPipFunc = allPip
3031
configuredUpdatesFunc = configuredUpdates
3132
)
3233

@@ -51,6 +52,10 @@ func allDocker(root string) ([]string, error) {
5152
return repo.FindFilePatternDirs(root, "*Dockerfile*")
5253
}
5354

55+
func allPip(root string) ([]string, error) {
56+
return repo.FindFilePatternDirs(root, "*requirements.txt")
57+
}
58+
5459
// localModPath returns the dependabot appropriate directory name for module
5560
// mod that resides in a repo with root.
5661
func localModPath(root string, mod *modfile.File) (string, error) {
Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,66 @@
11
# File generated by dbotconf; DO NOT EDIT.
22
version: 2
33
updates:
4-
- package-ecosystem: github-actions
5-
directory: /
6-
labels:
7-
- dependencies
8-
- actions
9-
- Skip Changelog
10-
schedule:
11-
interval: weekly
12-
day: sunday
13-
- package-ecosystem: docker
14-
directory: /
15-
labels:
16-
- dependencies
17-
- docker
18-
- Skip Changelog
19-
schedule:
20-
interval: weekly
21-
day: sunday
22-
- package-ecosystem: docker
23-
directory: /a/b/example
24-
labels:
25-
- dependencies
26-
- docker
27-
- Skip Changelog
28-
schedule:
29-
interval: weekly
30-
day: sunday
31-
- package-ecosystem: gomod
32-
directory: /
33-
labels:
34-
- dependencies
35-
- actions
36-
- Skip Changelog
37-
schedule:
38-
interval: weekly
39-
day: sunday
40-
- package-ecosystem: gomod
41-
directory: /a
42-
labels:
43-
- dependencies
44-
- actions
45-
- Skip Changelog
46-
schedule:
47-
interval: weekly
48-
day: sunday
49-
- package-ecosystem: gomod
50-
directory: /a/b
51-
labels:
52-
- dependencies
53-
- actions
54-
- Skip Changelog
55-
schedule:
56-
interval: weekly
57-
day: sunday
4+
- package-ecosystem: github-actions
5+
directory: /
6+
labels:
7+
- dependencies
8+
- actions
9+
- Skip Changelog
10+
schedule:
11+
interval: weekly
12+
day: sunday
13+
- package-ecosystem: docker
14+
directory: /
15+
labels:
16+
- dependencies
17+
- docker
18+
- Skip Changelog
19+
schedule:
20+
interval: weekly
21+
day: sunday
22+
- package-ecosystem: docker
23+
directory: /a/b/example
24+
labels:
25+
- dependencies
26+
- docker
27+
- Skip Changelog
28+
schedule:
29+
interval: weekly
30+
day: sunday
31+
- package-ecosystem: gomod
32+
directory: /
33+
labels:
34+
- dependencies
35+
- actions
36+
- Skip Changelog
37+
schedule:
38+
interval: weekly
39+
day: sunday
40+
- package-ecosystem: gomod
41+
directory: /a
42+
labels:
43+
- dependencies
44+
- actions
45+
- Skip Changelog
46+
schedule:
47+
interval: weekly
48+
day: sunday
49+
- package-ecosystem: gomod
50+
directory: /a/b
51+
labels:
52+
- dependencies
53+
- actions
54+
- Skip Changelog
55+
schedule:
56+
interval: weekly
57+
day: sunday
58+
- package-ecosystem: pip
59+
directory: /
60+
labels:
61+
- dependencies
62+
- python
63+
- Skip Changelog
64+
schedule:
65+
interval: weekly
66+
day: sunday

dbotconf/internal/verify.go

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,46 @@ var (
2929
errNotEnoughArg = errors.New("path argument required")
3030
)
3131

32-
// configuredUpdates returns the set of Go modules and Dockerfiles dependabot
33-
// is configured to check updates for.
34-
func configuredUpdates(path string) (mods map[string]struct{}, docker map[string]struct{}, err error) {
32+
type updates struct {
33+
mods map[string]struct{}
34+
docker map[string]struct{}
35+
pip map[string]struct{}
36+
}
37+
38+
// configuredUpdates returns updates configured in the dependabot configuration
39+
func configuredUpdates(path string) (u updates, err error) {
3540
f, err := os.Open(filepath.Clean(path))
3641
if errors.Is(err, os.ErrNotExist) {
37-
return nil, nil, fmt.Errorf("dependabot configuration file does not exist: %s", path)
42+
return updates{}, fmt.Errorf("dependabot configuration file does not exist: %s", path)
3843
}
3944
if err != nil {
40-
return nil, nil, fmt.Errorf("failed to read dependabot configuration file: %s", path)
45+
return updates{}, fmt.Errorf("failed to read dependabot configuration file: %s", path)
4146
}
4247

4348
var c dependabotConfig
4449
if err := yaml.NewDecoder(f).Decode(&c); err != nil {
45-
return nil, nil, fmt.Errorf("invalid dependabot configuration: %w", err)
50+
return updates{}, fmt.Errorf("invalid dependabot configuration: %w", err)
4651
}
4752

48-
mods = make(map[string]struct{})
49-
docker = make(map[string]struct{})
53+
mods := make(map[string]struct{})
54+
docker := make(map[string]struct{})
55+
pip := make(map[string]struct{})
5056
for _, u := range c.Updates {
5157
if u.PackageEcosystem == dockerPkgEco {
5258
docker[u.Directory] = struct{}{}
5359
}
5460
if u.PackageEcosystem == gomodPkgEco {
5561
mods[u.Directory] = struct{}{}
5662
}
63+
if u.PackageEcosystem == pipPkgEco {
64+
pip[u.Directory] = struct{}{}
65+
}
5766
}
58-
return mods, docker, nil
67+
return updates{mods, docker, pip}, nil
5968
}
6069

61-
// verify ensures dependabot configuration contains a check for all modules and
62-
// Dockerfiles.
70+
// verify ensures dependabot configuration contains a check for all modules,
71+
// Dockerfiles, and requirements.txt files.
6372
func verify(args []string) error {
6473
switch len(args) {
6574
case 0:
@@ -80,7 +89,13 @@ func verify(args []string) error {
8089
return err
8190
}
8291

83-
modUp, dockerUp, err := configuredUpdatesFunc(args[0])
92+
pipFiles, err := allPipFunc(root)
93+
if err != nil {
94+
return err
95+
}
96+
97+
u, err := configuredUpdatesFunc(args[0])
98+
8499
if err != nil {
85100
return err
86101
}
@@ -92,7 +107,7 @@ func verify(args []string) error {
92107
return err
93108
}
94109

95-
if _, ok := modUp[local]; !ok {
110+
if _, ok := u.mods[local]; !ok {
96111
missingMod = append(missingMod, local)
97112
}
98113
}
@@ -103,19 +118,33 @@ func verify(args []string) error {
103118
return err
104119
}
105120

106-
if _, ok := dockerUp[local]; !ok {
121+
if _, ok := u.docker[local]; !ok {
107122
missingDocker = append(missingDocker, local)
108123
}
109124
}
125+
var missingPip []string
126+
for _, p := range pipFiles {
127+
local, err := localPath(root, p)
128+
if err != nil {
129+
return err
130+
}
110131

111-
if len(missingMod) > 0 || len(missingDocker) > 0 {
132+
if _, ok := u.pip[local]; !ok {
133+
missingPip = append(missingPip, local)
134+
}
135+
}
136+
137+
if len(missingMod) > 0 || len(missingDocker) > 0 || len(missingPip) > 0 {
112138
msg := "missing update check(s):"
113139
if len(missingMod) > 0 {
114140
msg = fmt.Sprintf("%s\n- Go mod files: %s", msg, strings.Join(missingMod, ", "))
115141
}
116142
if len(missingDocker) > 0 {
117143
msg = fmt.Sprintf("%s\n- Dockerfiles: %s", msg, strings.Join(missingDocker, ", "))
118144
}
145+
if len(missingPip) > 0 {
146+
msg = fmt.Sprintf("%s\n- Pip files: %s", msg, strings.Join(missingPip, ", "))
147+
}
119148
msg += "\n"
120149
return errors.New(msg)
121150
}

0 commit comments

Comments
 (0)