forked from github/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependabot_file.py
258 lines (231 loc) · 8.59 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
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
"""This module contains the function to build the dependabot.yml file for a repo"""
import base64
import copy
import io
import github3
import ruamel.yaml
from ruamel.yaml.scalarstring import SingleQuotedScalarString
# Define data structure for dependabot.yaml
data = {
"version": 2,
"registries": {},
"updates": [],
}
yaml = ruamel.yaml.YAML()
stream = io.StringIO()
def make_dependabot_config(
ecosystem,
group_dependencies,
schedule,
schedule_day,
labels,
dependabot_config,
extra_dependabot_config,
) -> 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
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)
Returns:
str: the dependabot configuration for the package ecosystem
"""
dependabot_config["updates"].append(
{
"package-ecosystem": SingleQuotedScalarString(ecosystem),
"directory": SingleQuotedScalarString("/"),
}
)
if extra_dependabot_config:
ecosystem_config = extra_dependabot_config.get(ecosystem)
if ecosystem_config:
dependabot_config["registries"][ecosystem] = ecosystem_config
dependabot_config["updates"][-1].update(
{"registries": [SingleQuotedScalarString(ecosystem)]}
)
else:
dependabot_config.pop("registries", None)
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")
},
}
}
)
return yaml.dump(dependabot_config, stream)
def build_dependabot_file(
repo,
group_dependencies,
exempt_ecosystems,
repo_specific_exemptions,
existing_config,
schedule,
schedule_day,
labels,
extra_dependabot_config,
) -> 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)
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"],
}
# 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 repo.file_contents(file):
package_managers_found[manager] = True
make_dependabot_config(
manager,
group_dependencies,
schedule,
schedule_day,
labels,
dependabot_file,
extra_dependabot_config,
)
break
except github3.exceptions.NotFoundError:
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,
)
break
except github3.exceptions.NotFoundError:
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,
)
break
except github3.exceptions.NotFoundError:
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"])