forked from github/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependabot_file.py
233 lines (212 loc) · 8.33 KB
/
dependabot_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""This module contains the function to build the dependabot.yml file for a repo"""
import github3
def make_dependabot_config(ecosystem, group_dependencies) -> str:
"""
Make the dependabot configuration for a specific package ecosystem
Args:
ecosystem: the package ecosystem to make the dependabot configuration for
group_dependencies: whether to group dependencies in the dependabot.yml file
Returns:
str: the dependabot configuration for the package ecosystem
"""
dependabot_config = f""" - package-ecosystem: '{ecosystem}'
directory: '/'
schedule:
interval: 'weekly'
"""
if group_dependencies:
dependabot_config += """ groups:
production-dependencies:
dependency-type: 'production'
development-dependencies:
dependency-type: 'development'
"""
return dependabot_config
def build_dependabot_file(repo, group_dependencies) -> str | None:
"""
Build the dependabot.yml file for a repo based on the repo contents
Args:
repo: the repository to build the dependabot.yml file for
group_dependencies: whether to group dependencies in the dependabot.yml file
Returns:
str: the dependabot.yml file for the repo
"""
compatible_package_manager_found = False
bundler_found = False
npm_found = False
pip_found = False
cargo_found = False
gomod_found = False
composer_found = False
hex_found = False
nuget_found = False
docker_found = False
dependabot_file = """---
version: 2
updates:
"""
try:
if repo.file_contents("Gemfile") and not bundler_found:
compatible_package_manager_found = True
bundler_found = True
dependabot_file += make_dependabot_config("bundler", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("Gemfile.lock") and not bundler_found:
compatible_package_manager_found = True
bundler_found = True
dependabot_file += make_dependabot_config("bundler", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("package.json") and not npm_found:
compatible_package_manager_found = True
npm_found = True
dependabot_file += make_dependabot_config("npm", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("package-lock.json") and not npm_found:
compatible_package_manager_found = True
npm_found = True
dependabot_file += make_dependabot_config("npm", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("yarn.lock") and not npm_found:
compatible_package_manager_found = True
npm_found = True
dependabot_file += make_dependabot_config("npm", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("requirements.txt") and not pip_found:
compatible_package_manager_found = True
pip_found = True
dependabot_file += make_dependabot_config("pip", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("Pipfile") and not pip_found:
compatible_package_manager_found = True
pip_found = True
dependabot_file += make_dependabot_config("pip", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("Pipfile.lock") and not pip_found:
compatible_package_manager_found = True
pip_found = True
dependabot_file += make_dependabot_config("pip", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("pyproject.toml") and not pip_found:
compatible_package_manager_found = True
pip_found = True
dependabot_file += make_dependabot_config("pip", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("poetry.lock") and not pip_found:
compatible_package_manager_found = True
pip_found = True
dependabot_file += make_dependabot_config("pip", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("Cargo.toml") and not cargo_found:
compatible_package_manager_found = True
cargo_found = True
dependabot_file += make_dependabot_config("cargo", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("Cargo.lock") and not cargo_found:
compatible_package_manager_found = True
cargo_found = True
dependabot_file += make_dependabot_config("cargo", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("go.mod") and not gomod_found:
compatible_package_manager_found = True
gomod_found = True
dependabot_file += make_dependabot_config("gomod", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("composer.json") and not composer_found:
compatible_package_manager_found = True
composer_found = True
dependabot_file += make_dependabot_config("composer", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("composer.lock") and not composer_found:
compatible_package_manager_found = True
composer_found = True
dependabot_file += make_dependabot_config("composer", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("mix.exs") and not hex_found:
compatible_package_manager_found = True
hex_found = True
dependabot_file += make_dependabot_config("hex", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("mix.lock") and not hex_found:
compatible_package_manager_found = True
hex_found = True
dependabot_file += make_dependabot_config("hex", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
for file in repo.directory_contents(".github/workflows"):
if file[0].endswith(".yml") or file[0].endswith(".yaml"):
compatible_package_manager_found = True
dependabot_file += make_dependabot_config(
"github-actions", group_dependencies
)
break
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents("Dockerfile") and not docker_found:
compatible_package_manager_found = True
docker_found = True
dependabot_file += make_dependabot_config("docker", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents(".nuspec") and not nuget_found:
compatible_package_manager_found = True
nuget_found = True
dependabot_file += make_dependabot_config("nuget", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents(".csproj") and not nuget_found:
compatible_package_manager_found = True
nuget_found = True
dependabot_file += make_dependabot_config("nuget", group_dependencies)
except github3.exceptions.NotFoundError:
pass
try:
# detect if the repo has a any terraform files
terraform_files = False
for file in repo.directory_contents("/"):
if file[0].endswith(".tf"):
terraform_files = True
break
if terraform_files:
compatible_package_manager_found = True
dependabot_file += make_dependabot_config("terraform", group_dependencies)
except github3.exceptions.NotFoundError:
pass
if compatible_package_manager_found:
return dependabot_file
return None