-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathdependabot_file.py
More file actions
371 lines (334 loc) · 13.2 KB
/
dependabot_file.py
File metadata and controls
371 lines (334 loc) · 13.2 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""This module contains the function to build the dependabot.yml file for a repo"""
import base64
import copy
import io
from collections.abc import Mapping
import github3
import ruamel.yaml
from exceptions import OptionalFileNotFoundError, check_optional_file
from ruamel.yaml.scalarstring import SingleQuotedScalarString
# Define data structure for dependabot.yaml
data = {
"version": 2,
"updates": [],
}
yaml = ruamel.yaml.YAML()
stream = io.StringIO()
COOLDOWN_DAYS_KEYS_ORDERED = (
"default-days",
"semver-major-days",
"semver-minor-days",
"semver-patch-days",
)
VALID_COOLDOWN_DAYS_KEYS = frozenset(COOLDOWN_DAYS_KEYS_ORDERED)
VALID_COOLDOWN_KEYS = VALID_COOLDOWN_DAYS_KEYS | {"include", "exclude"}
MAX_COOLDOWN_LIST_ITEMS = 150
MIN_COOLDOWN_DAYS = 1
MAX_COOLDOWN_DAYS = 90
def validate_cooldown_config(cooldown):
"""
Validate the cooldown configuration from the dependabot config file.
Args:
cooldown: dict with cooldown configuration
Raises:
ValueError: if the cooldown configuration is invalid
"""
if not isinstance(cooldown, Mapping):
raise ValueError("Cooldown configuration must be a mapping")
unknown_keys = set(cooldown.keys()) - VALID_COOLDOWN_KEYS
if unknown_keys:
raise ValueError(
f"Unknown cooldown configuration keys: {', '.join(sorted(unknown_keys))}"
)
has_days = False
for key in VALID_COOLDOWN_DAYS_KEYS:
if key in cooldown:
value = cooldown[key]
if not isinstance(value, int) or isinstance(value, bool):
raise ValueError(
f"Cooldown '{key}' must be an integer between "
f"{MIN_COOLDOWN_DAYS} and {MAX_COOLDOWN_DAYS}, "
f"got {type(value).__name__}"
)
if value < MIN_COOLDOWN_DAYS or value > MAX_COOLDOWN_DAYS:
raise ValueError(
f"Cooldown '{key}' must be between "
f"{MIN_COOLDOWN_DAYS} and {MAX_COOLDOWN_DAYS}"
)
has_days = True
if not has_days:
raise ValueError(
"Cooldown configuration must include at least one of: "
+ ", ".join(sorted(VALID_COOLDOWN_DAYS_KEYS))
)
for list_key in ("include", "exclude"):
if list_key in cooldown:
items = cooldown[list_key]
if not isinstance(items, list):
raise ValueError(f"Cooldown '{list_key}' must be a list")
if len(items) > MAX_COOLDOWN_LIST_ITEMS:
raise ValueError(
f"Cooldown '{list_key}' must have at most {MAX_COOLDOWN_LIST_ITEMS} items"
)
for item in items:
if not isinstance(item, str):
raise ValueError(f"Cooldown '{list_key}' items must be strings")
def make_dependabot_config(
ecosystem,
group_dependencies,
schedule,
schedule_day,
labels,
dependabot_config,
extra_dependabot_config,
cooldown=None,
) -> None:
"""
Make the dependabot configuration for a specific package ecosystem.
Mutates dependabot_config in place by appending an update entry.
Args:
ecosystem: the package ecosystem to make the dependabot configuration for
group_dependencies: whether to group dependencies in the dependabot.yml file
schedule: the schedule to run dependabot ex: "daily"
schedule_day: the day of the week to run dependabot ex: "monday" if schedule is "weekly"
labels: the list of labels to be added to dependabot configuration
dependabot_config: extra dependabot configs
extra_dependabot_config: File with the configuration to add dependabot configs (ex: private registries)
cooldown: optional cooldown configuration dict to delay version update PRs
"""
dependabot_config["updates"].append(
{
"package-ecosystem": SingleQuotedScalarString(ecosystem),
"directory": SingleQuotedScalarString("/"),
}
)
if extra_dependabot_config:
ecosystem_config = extra_dependabot_config.get(ecosystem)
if ecosystem_config:
if "registries" not in dependabot_config:
dependabot_config.update({"registries": {}})
dependabot_config["registries"][ecosystem] = ecosystem_config
dependabot_config["updates"][-1].update(
{"registries": [SingleQuotedScalarString(ecosystem)]}
)
if schedule_day:
dependabot_config["updates"][-1].update(
{
"schedule": {
"interval": SingleQuotedScalarString(schedule),
"day": SingleQuotedScalarString(schedule_day),
},
}
)
else:
dependabot_config["updates"][-1].update(
{
"schedule": {"interval": SingleQuotedScalarString(schedule)},
}
)
if labels:
quoted_labels = []
for label in labels:
quoted_labels.append(SingleQuotedScalarString(label))
dependabot_config["updates"][-1].update({"labels": quoted_labels})
if group_dependencies:
dependabot_config["updates"][-1].update(
{
"groups": {
"production-dependencies": {
"dependency-type": SingleQuotedScalarString("production")
},
"development-dependencies": {
"dependency-type": SingleQuotedScalarString("development")
},
}
}
)
if cooldown:
cooldown_config = {}
for key in COOLDOWN_DAYS_KEYS_ORDERED:
if key in cooldown:
cooldown_config[key] = cooldown[key]
for list_key in ("include", "exclude"):
if list_key in cooldown:
cooldown_config[list_key] = [
SingleQuotedScalarString(item) for item in cooldown[list_key]
]
dependabot_config["updates"][-1].update({"cooldown": cooldown_config})
def build_dependabot_file(
repo,
group_dependencies,
exempt_ecosystems,
repo_specific_exemptions,
existing_config,
schedule,
schedule_day,
labels,
extra_dependabot_config,
cooldown=None,
) -> 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
exempt_ecosystems: the list of ecosystems to ignore
repo_specific_exemptions: the list of ecosystems to ignore for a specific repo
existing_config: the existing dependabot configuration file or None if it doesn't exist
schedule: the schedule to run dependabot ex: "daily"
schedule_day: the day of the week to run dependabot ex: "monday" if schedule is "daily"
labels: the list of labels to be added to dependabot configuration
extra_dependabot_config: File with the configuration to add dependabot configs (ex: private registries)
cooldown: optional cooldown configuration dict to delay version update PRs
Returns:
str: the dependabot.yml file for the repo
"""
package_managers_found = {
"bundler": False,
"npm": False,
"pip": False,
"cargo": False,
"gomod": False,
"composer": False,
"mix": False,
"nuget": False,
"docker": False,
"terraform": False,
"github-actions": False,
"maven": False,
}
# create a local copy in order to avoid overwriting the global exemption list
exempt_ecosystems_list = exempt_ecosystems.copy()
if existing_config:
yaml.preserve_quotes = True
try:
dependabot_file = yaml.load(base64.b64decode(existing_config.content))
except ruamel.yaml.YAMLError as e:
print(f"YAML indentation error: {e}")
raise
else:
dependabot_file = copy.deepcopy(data)
add_existing_ecosystem_to_exempt_list(exempt_ecosystems_list, dependabot_file)
# If there are repository specific exemptions,
# overwrite the global exemptions for this repo only
if repo_specific_exemptions and repo.full_name in repo_specific_exemptions:
exempt_ecosystems_list = []
for ecosystem in repo_specific_exemptions[repo.full_name]:
exempt_ecosystems_list.append(ecosystem)
package_managers = {
"bundler": ["Gemfile", "Gemfile.lock"],
"npm": ["package.json", "package-lock.json", "yarn.lock"],
"pip": [
"requirements.txt",
"Pipfile",
"Pipfile.lock",
"pyproject.toml",
"poetry.lock",
],
"cargo": ["Cargo.toml", "Cargo.lock"],
"gomod": ["go.mod"],
"composer": ["composer.json", "composer.lock"],
"mix": ["mix.exs", "mix.lock"],
"nuget": [
".nuspec",
".csproj",
],
"docker": ["Dockerfile"],
"maven": ["pom.xml"],
"gradle": ["build.gradle", "build.gradle.kts"],
}
# Detect package managers where manifest files have known names
for manager, manifest_files in package_managers.items():
if manager in exempt_ecosystems_list:
continue
for file in manifest_files:
try:
if check_optional_file(repo, file):
package_managers_found[manager] = True
make_dependabot_config(
manager,
group_dependencies,
schedule,
schedule_day,
labels,
dependabot_file,
extra_dependabot_config,
cooldown,
)
break
except OptionalFileNotFoundError:
# The file does not exist and is not required,
# so we should continue to the next one rather than raising error or logging
pass
# detect package managers with variable file names
if "terraform" not in exempt_ecosystems_list:
try:
for file in repo.directory_contents("/"):
if file[0].endswith(".tf"):
package_managers_found["terraform"] = True
make_dependabot_config(
"terraform",
group_dependencies,
schedule,
schedule_day,
labels,
dependabot_file,
extra_dependabot_config,
cooldown,
)
break
except github3.exceptions.NotFoundError:
# The file does not exist and is not required,
# so we should continue to the next one rather than raising error or logging
pass
if "github-actions" not in exempt_ecosystems_list:
try:
for file in repo.directory_contents(".github/workflows"):
if file[0].endswith(".yml") or file[0].endswith(".yaml"):
package_managers_found["github-actions"] = True
make_dependabot_config(
"github-actions",
group_dependencies,
schedule,
schedule_day,
labels,
dependabot_file,
extra_dependabot_config,
cooldown,
)
break
except github3.exceptions.NotFoundError:
# The file does not exist and is not required,
# so we should continue to the next one rather than raising error or logging
pass
if "devcontainers" not in exempt_ecosystems_list:
try:
for file in repo.directory_contents(".devcontainer"):
if file[0] == "devcontainer.json":
package_managers_found["devcontainers"] = True
make_dependabot_config(
"devcontainers",
group_dependencies,
schedule,
schedule_day,
labels,
dependabot_file,
extra_dependabot_config,
cooldown,
)
break
except github3.exceptions.NotFoundError:
# The file does not exist and is not required,
# so we should continue to the next one rather than raising error or logging
pass
if any(package_managers_found.values()):
return dependabot_file
return None
def add_existing_ecosystem_to_exempt_list(exempt_ecosystems, existing_config):
"""
Add the existing package ecosystems found in the dependabot.yml
to the exempt list so we don't get duplicate entries and maintain configuration settings
"""
if existing_config:
for entry in existing_config.get("updates", []):
exempt_ecosystems.append(entry["package-ecosystem"])