forked from github/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependabot_file.py
146 lines (129 loc) · 4.73 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
"""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) -> 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, exempt_ecosystems, existing_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
existing_config: the existing dependabot configuration file or None if it doesn't exist
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,
}
if existing_config:
dependabot_file = existing_config.decoded.decode("utf-8")
else:
dependabot_file = """---
version: 2
updates:
"""
add_existing_ecosystem_to_exempt_list(exempt_ecosystems, existing_config)
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"],
}
# Detect package managers where manifest files have known names
for manager, manifest_files in package_managers.items():
if manager in exempt_ecosystems:
continue
for file in manifest_files:
try:
if repo.file_contents(file):
package_managers_found[manager] = True
dependabot_file += make_dependabot_config(
manager, group_dependencies
)
break
except github3.exceptions.NotFoundError:
pass
# detect package managers with variable file names
if "terraform" not in exempt_ecosystems:
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
)
break
except github3.exceptions.NotFoundError:
pass
if "github-actions" not in exempt_ecosystems:
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
)
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"])