forked from github/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evergreen.py
336 lines (287 loc) · 11.9 KB
/
evergreen.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
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
"""This file contains the main() and other functions needed to open an issue/PR dependabot is not enabled but could be"""
import uuid
from datetime import datetime
import auth
import env
import github3
import requests
from dependabot_file import build_dependabot_file
def main(): # pragma: no cover
"""Run the main program"""
# Get the environment variables
(
organization,
repository_list,
token,
ghe,
exempt_repositories_list,
follow_up_type,
title,
body,
created_after_date,
dry_run,
commit_message,
project_id,
group_dependencies,
enable_security_updates,
) = env.get_env_vars()
# Auth to GitHub.com or GHE
github_connection = auth.auth_to_github(token, ghe)
# If Project ID is set lookup the global project ID
if project_id:
# Check Organization is set as it is required for linking to a project
if not organization:
raise ValueError(
"ORGANIZATION environment variable was not set. Please set it"
)
project_id = get_global_project_id(token, organization, project_id)
# Get the repositories from the organization or list of repositories
repos = get_repos_iterator(organization, repository_list, github_connection)
# Iterate through the repositories and open an issue/PR if dependabot is not enabled
count_eligible = 0
for repo in repos:
# Check all the things to see if repo is eligble for a pr/issue
if repo.full_name in exempt_repositories_list:
continue
if repo.archived:
continue
try:
if repo.file_contents(".github/dependabot.yml").size > 0:
continue
except github3.exceptions.NotFoundError:
pass
try:
if repo.file_contents(".github/dependabot.yaml").size > 0:
continue
except github3.exceptions.NotFoundError:
pass
if created_after_date and repo.created_at.replace(
tzinfo=None
) < datetime.strptime(created_after_date, "%Y-%m-%d"):
continue
print("Checking " + repo.full_name)
# Try to detect package managers and build a dependabot file
dependabot_file = build_dependabot_file(repo, group_dependencies)
if dependabot_file is None:
print("\tNo compatible package manager found")
continue
# If dry_run is set, just print the dependabot file
if dry_run:
if follow_up_type == "issue":
skip = check_pending_issues_for_duplicates(title, repo)
if not skip:
print("\tEligible for configuring dependabot.")
count_eligible += 1
print("\tConfiguration:\n" + dependabot_file)
if follow_up_type == "pull":
# Try to detect if the repo already has an open pull request for dependabot
skip = check_pending_pulls_for_duplicates(title, repo)
if not skip:
print("\tEligible for configuring dependabot.")
count_eligible += 1
print("\tConfiguration:\n" + dependabot_file)
continue
# Get dependabot security updates enabled if possible
if enable_security_updates:
if not is_dependabot_security_updates_enabled(repo.owner, repo.name, token):
enable_dependabot_security_updates(repo.owner, repo.name, token)
if follow_up_type == "issue":
skip = check_pending_issues_for_duplicates(title, repo)
if not skip:
count_eligible += 1
body_issue = (
body
+ "\n\n```yaml\n"
+ "# .github/dependabot.yml\n"
+ dependabot_file
+ "\n```"
)
issue = repo.create_issue(title, body_issue)
print("\tCreated issue " + issue.html_url)
if project_id:
issue_id = get_global_issue_id(
token, organization, repo.name, issue.number
)
link_item_to_project(token, project_id, issue_id)
print("\tLinked issue to project " + project_id)
else:
count_eligible += 1
# Try to detect if the repo already has an open pull request for dependabot
skip = check_pending_pulls_for_duplicates(title, repo)
# Create a dependabot.yaml file, a branch, and a PR
if not skip:
try:
pull = commit_changes(
title, body, repo, dependabot_file, commit_message
)
print("\tCreated pull request " + pull.html_url)
if project_id:
pr_id = get_global_pr_id(
token, organization, repo.name, pull.number
)
response = link_item_to_project(token, project_id, pr_id)
if response:
print("\tLinked pull request to project " + project_id)
except github3.exceptions.NotFoundError:
print("\tFailed to create pull request. Check write permissions.")
continue
print("Done. " + str(count_eligible) + " repositories were eligible.")
def is_dependabot_security_updates_enabled(owner, repo, access_token):
"""Check if Dependabot security updates are enabled at the /repos/:owner/:repo/automated-security-fixes endpoint using the requests library"""
url = f"https://api.github.com/repos/{owner}/{repo}/automated-security-fixes"
headers = {
"Authorization": f"Bearer {access_token}",
"Accept": "application/vnd.github.london-preview+json",
}
response = requests.get(url, headers=headers, timeout=20)
if response.status_code == 200:
return response.json()["enabled"]
return False
def enable_dependabot_security_updates(owner, repo, access_token):
"""Enable Dependabot security updates at the /repos/:owner/:repo/automated-security-fixes endpoint using the requests library"""
url = f"https://api.github.com/repos/{owner}/{repo}/automated-security-fixes"
headers = {
"Authorization": f"Bearer {access_token}",
"Accept": "application/vnd.github.london-preview+json",
}
response = requests.put(url, headers=headers, timeout=20)
if response.status_code == 204:
print("\tDependabot security updates enabled successfully.")
else:
print("\tFailed to enable Dependabot security updates.")
def get_repos_iterator(organization, repository_list, github_connection):
"""Get the repositories from the organization or list of repositories"""
repos = []
if organization and not repository_list:
repos = github_connection.organization(organization).repositories()
else:
# Get the repositories from the repository_list
for repo in repository_list:
repos.append(
github_connection.repository(repo.split("/")[0], repo.split("/")[1])
)
return repos
def check_pending_pulls_for_duplicates(title, repo) -> bool:
"""Check if there are any open pull requests for dependabot and return the bool skip"""
pull_requests = repo.pull_requests(state="open")
skip = False
for pull_request in pull_requests:
if pull_request.title.startswith(title):
print("\tPull request already exists: " + pull_request.html_url)
skip = True
break
return skip
def check_pending_issues_for_duplicates(title, repo) -> bool:
"""Check if there are any open issues for dependabot and return the bool skip"""
issues = repo.issues(state="open")
skip = False
for issue in issues:
if issue.title.startswith(title):
print("\tIssue already exists: " + issue.html_url)
skip = True
break
return skip
def commit_changes(title, body, repo, dependabot_file, message):
"""Commit the changes to the repo and open a pull reques and return the pull request object"""
default_branch = repo.default_branch
# Get latest commit sha from default branch
default_branch_commit = repo.ref("heads/" + default_branch).object.sha
front_matter = "refs/heads/"
branch_name = "dependabot-" + str(uuid.uuid4())
repo.create_ref(front_matter + branch_name, default_branch_commit)
repo.create_file(
path=".github/dependabot.yaml",
message=message,
content=dependabot_file.encode(), # Convert to bytes object
branch=branch_name,
)
pull = repo.create_pull(
title=title, body=body, head=branch_name, base=repo.default_branch
)
return pull
def get_global_project_id(token, organization, number):
"""Fetches the project ID from GitHub's GraphQL API."""
url = "https://api.github.com/graphql"
headers = {"Authorization": f"Bearer {token}"}
data = {
"query": f'query{{organization(login: "{organization}") {{projectV2(number: {number}){{id}}}}}}'
}
try:
response = requests.post(url, headers=headers, json=data, timeout=20)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
return None
try:
return response.json()["data"]["organization"]["projectV2"]["id"]
except KeyError as e:
print(f"Failed to parse response: {e}")
return None
def get_global_issue_id(token, organization, repository, issue_number):
"""Fetches the issue ID from GitHub's GraphQL API"""
url = "https://api.github.com/graphql"
headers = {"Authorization": f"Bearer {token}"}
data = {
"query": f"""
query {{
repository(owner: "{organization}", name: "{repository}") {{
issue(number: {issue_number}) {{
id
}}
}}
}}
"""
}
try:
response = requests.post(url, headers=headers, json=data, timeout=20)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
return None
try:
return response.json()["data"]["repository"]["issue"]["id"]
except KeyError as e:
print(f"Failed to parse response: {e}")
return None
def get_global_pr_id(token, organization, repository, pr_number):
"""Fetches the pull request ID from GitHub's GraphQL API"""
url = "https://api.github.com/graphql"
headers = {"Authorization": f"Bearer {token}"}
data = {
"query": f"""
query {{
repository(owner: "{organization}", name: "{repository}") {{
pullRequest(number: {pr_number}) {{
id
}}
}}
}}
"""
}
try:
response = requests.post(url, headers=headers, json=data, timeout=20)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
return None
try:
return response.json()["data"]["repository"]["pullRequest"]["id"]
except KeyError as e:
print(f"Failed to parse response: {e}")
return None
def link_item_to_project(token, project_id, item_id):
"""Links an item (issue or pull request) to a project in GitHub."""
url = "https://api.github.com/graphql"
headers = {"Authorization": f"Bearer {token}"}
data = {
"query": f'mutation {{addProjectV2ItemById(input: {{projectId: "{project_id}", contentId: "{item_id}"}}) {{item {{id}}}}}}'
}
try:
response = requests.post(url, headers=headers, json=data, timeout=20)
response.raise_for_status()
return response
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
return None
if __name__ == "__main__":
main() # pragma: no cover