-
Notifications
You must be signed in to change notification settings - Fork 18
/
dependabot_file.py
218 lines (198 loc) · 7.74 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
"""This module contains the function to build the dependabot.yml file for a repo"""
import github3
import yaml
def make_dependabot_config(
ecosystem, group_dependencies, indent, schedule, schedule_day, labels
) -> 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
indent: the number of spaces to indent the dependabot configuration ex: " "
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
Returns:
str: the dependabot configuration for the package ecosystem
"""
schedule_day_line = ""
if schedule_day:
schedule_day_line += f"""
{indent}{indent}{indent}day: '{schedule_day}'"""
dependabot_config = f"""{indent}- package-ecosystem: '{ecosystem}'
{indent}{indent}directory: '/'
{indent}{indent}schedule:
{indent}{indent}{indent}interval: '{schedule}'{schedule_day_line}
"""
if labels:
dependabot_config += f"""{indent}{indent}labels:
"""
for label in labels:
dependabot_config += f"""{indent}{indent}{indent}- \"{label}\"
"""
if group_dependencies:
dependabot_config += f"""{indent}{indent}groups:
{indent}{indent}{indent}production-dependencies:
{indent}{indent}{indent}{indent}dependency-type: 'production'
{indent}{indent}{indent}development-dependencies:
{indent}{indent}{indent}{indent}dependency-type: 'development'
"""
return dependabot_config
def build_dependabot_file(
repo,
group_dependencies,
exempt_ecosystems,
repo_specific_exemptions,
existing_config,
schedule,
schedule_day,
labels,
) -> 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
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,
}
DEFAULT_INDENT = 2 # pylint: disable=invalid-name
# create a local copy in order to avoid overwriting the global exemption list
exempt_ecosystems_list = exempt_ecosystems.copy()
if existing_config:
dependabot_file = existing_config.decoded.decode("utf-8")
ecosystem_line = next(
line
for line in dependabot_file.splitlines()
if "- package-ecosystem:" in line
)
indent = " " * (len(ecosystem_line) - len(ecosystem_line.lstrip()))
if len(indent) < DEFAULT_INDENT:
print(
f"Invalid dependabot.yml file. No indentation found. Skipping {repo.full_name}"
)
return None
else:
indent = " " * DEFAULT_INDENT
dependabot_file = """---
version: 2
updates:
"""
add_existing_ecosystem_to_exempt_list(exempt_ecosystems_list, existing_config)
# 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
# If the last thing in the file is not a newline,
# add one before adding a new language config to the file
if dependabot_file and dependabot_file[-1] != "\n":
dependabot_file += "\n"
dependabot_file += make_dependabot_config(
manager,
group_dependencies,
indent,
schedule,
schedule_day,
labels,
)
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
dependabot_file += make_dependabot_config(
"terraform",
group_dependencies,
indent,
schedule,
schedule_day,
labels,
)
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
dependabot_file += make_dependabot_config(
"github-actions",
group_dependencies,
indent,
schedule,
schedule_day,
labels,
)
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:
existing_config_obj = yaml.safe_load(existing_config.decoded)
if existing_config_obj:
for entry in existing_config_obj.get("updates", []):
exempt_ecosystems.append(entry["package-ecosystem"])