@@ -45,28 +45,20 @@ def main(): # pragma: no cover
45
45
) = env .get_env_vars ()
46
46
47
47
# Auth to GitHub.com or GHE
48
- github_connection = auth .auth_to_github (
49
- token , gh_app_id , gh_app_installation_id , gh_app_private_key , ghe
50
- )
48
+ github_connection = auth .auth_to_github (token , gh_app_id , gh_app_installation_id , gh_app_private_key , ghe )
51
49
52
50
if not token and gh_app_id and gh_app_installation_id and gh_app_private_key :
53
- token = auth .get_github_app_installation_token (
54
- gh_app_id , gh_app_private_key , gh_app_installation_id
55
- )
51
+ token = auth .get_github_app_installation_token (ghe , gh_app_id , gh_app_private_key , gh_app_installation_id )
56
52
57
53
# If Project ID is set, lookup the global project ID
58
54
if project_id :
59
55
# Check Organization is set as it is required for linking to a project
60
56
if not organization :
61
- raise ValueError (
62
- "ORGANIZATION environment variable was not set. Please set it"
63
- )
64
- project_id = get_global_project_id (token , organization , project_id )
57
+ raise ValueError ("ORGANIZATION environment variable was not set. Please set it" )
58
+ project_id = get_global_project_id (ghe , token , organization , project_id )
65
59
66
60
# Get the repositories from the organization, team name, or list of repositories
67
- repos = get_repos_iterator (
68
- organization , team_name , repository_list , github_connection
69
- )
61
+ repos = get_repos_iterator (organization , team_name , repository_list , github_connection )
70
62
71
63
# Iterate through the repositories and open an issue/PR if dependabot is not enabled
72
64
count_eligible = 0
@@ -78,13 +70,13 @@ def main(): # pragma: no cover
78
70
79
71
# Check all the things to see if repo is eligble for a pr/issue
80
72
if repo .full_name in exempt_repositories_list :
81
- print ("Skipping " + repo .full_name + " (exempted)" )
73
+ print (f "Skipping { repo .full_name } (exempted)" )
82
74
continue
83
75
if repo .archived :
84
- print ("Skipping " + repo .full_name + " (archived)" )
76
+ print (f "Skipping { repo .full_name } (archived)" )
85
77
continue
86
78
if repo .visibility .lower () not in filter_visibility :
87
- print ("Skipping " + repo .full_name + " (visibility-filtered)" )
79
+ print (f "Skipping { repo .full_name } (visibility-filtered)" )
88
80
continue
89
81
existing_config = None
90
82
filename_list = [".github/dependabot.yaml" , ".github/dependabot.yml" ]
@@ -96,20 +88,14 @@ def main(): # pragma: no cover
96
88
break
97
89
98
90
if existing_config and not update_existing :
99
- print (
100
- "Skipping "
101
- + repo .full_name
102
- + " (dependabot file already exists and update_existing is False)"
103
- )
91
+ print (f"Skipping { repo .full_name } (dependabot file already exists and update_existing is False)" )
104
92
continue
105
93
106
- if created_after_date and is_repo_created_date_before (
107
- repo .created_at , created_after_date
108
- ):
109
- print ("Skipping " + repo .full_name + " (created after filter)" )
94
+ if created_after_date and is_repo_created_date_before (repo .created_at , created_after_date ):
95
+ print (f"Skipping { repo .full_name } (created after filter)" )
110
96
continue
111
97
112
- print ("Checking " + repo .full_name + " for compatible package managers" )
98
+ print (f "Checking { repo .full_name } for compatible package managers" )
113
99
# Try to detect package managers and build a dependabot file
114
100
dependabot_file = build_dependabot_file (
115
101
repo ,
@@ -133,42 +119,32 @@ def main(): # pragma: no cover
133
119
if not skip :
134
120
print ("\t Eligible for configuring dependabot." )
135
121
count_eligible += 1
136
- print ("\t Configuration:\n " + dependabot_file )
122
+ print (f "\t Configuration:\n { dependabot_file } " )
137
123
if follow_up_type == "pull" :
138
124
# Try to detect if the repo already has an open pull request for dependabot
139
125
skip = check_pending_pulls_for_duplicates (title , repo )
140
126
if not skip :
141
127
print ("\t Eligible for configuring dependabot." )
142
128
count_eligible += 1
143
- print ("\t Configuration:\n " + dependabot_file )
129
+ print (f "\t Configuration:\n { dependabot_file } " )
144
130
continue
145
131
146
132
# Get dependabot security updates enabled if possible
147
133
if enable_security_updates :
148
- if not is_dependabot_security_updates_enabled (repo .owner , repo .name , token ):
149
- enable_dependabot_security_updates (repo .owner , repo .name , token )
134
+ if not is_dependabot_security_updates_enabled (ghe , repo .owner , repo .name , token ):
135
+ enable_dependabot_security_updates (ghe , repo .owner , repo .name , token )
150
136
151
137
if follow_up_type == "issue" :
152
138
skip = check_pending_issues_for_duplicates (title , repo )
153
139
if not skip :
154
140
count_eligible += 1
155
- body_issue = (
156
- body
157
- + "\n \n ```yaml\n "
158
- + "# "
159
- + dependabot_filename_to_use
160
- + "\n "
161
- + dependabot_file
162
- + "\n ```"
163
- )
141
+ body_issue = f"{ body } \n \n ```yaml\n # { dependabot_filename_to_use } \n { dependabot_file } \n ```"
164
142
issue = repo .create_issue (title , body_issue )
165
- print ("\t Created issue " + issue .html_url )
143
+ print (f "\t Created issue { issue .html_url } " )
166
144
if project_id :
167
- issue_id = get_global_issue_id (
168
- token , organization , repo .name , issue .number
169
- )
170
- link_item_to_project (token , project_id , issue_id )
171
- print ("\t Linked issue to project " + project_id )
145
+ issue_id = get_global_issue_id (ghe , token , organization , repo .name , issue .number )
146
+ link_item_to_project (ghe , token , project_id , issue_id )
147
+ print (f"\t Linked issue to project { project_id } " )
172
148
else :
173
149
# Try to detect if the repo already has an open pull request for dependabot
174
150
skip = check_pending_pulls_for_duplicates (title , repo )
@@ -186,34 +162,32 @@ def main(): # pragma: no cover
186
162
dependabot_filename_to_use ,
187
163
existing_config ,
188
164
)
189
- print ("\t Created pull request " + pull .html_url )
165
+ print (f "\t Created pull request { pull .html_url } " )
190
166
if project_id :
191
- pr_id = get_global_pr_id (
192
- token , organization , repo .name , pull .number
193
- )
194
- response = link_item_to_project (token , project_id , pr_id )
167
+ pr_id = get_global_pr_id (ghe , token , organization , repo .name , pull .number )
168
+ response = link_item_to_project (ghe , token , project_id , pr_id )
195
169
if response :
196
- print ("\t Linked pull request to project " + project_id )
170
+ print (f "\t Linked pull request to project { project_id } " )
197
171
except github3 .exceptions .NotFoundError :
198
172
print ("\t Failed to create pull request. Check write permissions." )
199
173
continue
200
174
201
- print ("Done. " + str (count_eligible ) + " repositories were eligible." )
175
+ print (f "Done. { str (count_eligible )} repositories were eligible." )
202
176
203
177
204
178
def is_repo_created_date_before (repo_created_at : str , created_after_date : str ):
205
179
"""Check if the repository was created before the created_after_date"""
206
180
repo_created_at_date = datetime .fromisoformat (repo_created_at ).replace (tzinfo = None )
207
- return created_after_date and repo_created_at_date < datetime .strptime (
208
- created_after_date , "%Y-%m-%d"
209
- )
181
+ return created_after_date and repo_created_at_date < datetime .strptime (created_after_date , "%Y-%m-%d" )
210
182
211
183
212
- def is_dependabot_security_updates_enabled (owner , repo , access_token ):
213
- """Check if Dependabot security updates are enabled at the
214
- /repos/:owner/:repo/automated-security-fixes endpoint using the requests library
184
+ def is_dependabot_security_updates_enabled (ghe , owner , repo , access_token ):
185
+ """
186
+ Check if Dependabot security updates are enabled at the /repos/:owner/:repo/automated-security-fixes endpoint using the requests library
187
+ API: https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#check-if-automated-security-fixes-are-enabled-for-a-repository
215
188
"""
216
- url = f"https://api.github.com/repos/{ owner } /{ repo } /automated-security-fixes"
189
+ api_endpoint = f"{ ghe } /api/v3" if ghe else "https://api.github.com"
190
+ url = f"{ api_endpoint } /repos/{ owner } /{ repo } /automated-security-fixes"
217
191
headers = {
218
192
"Authorization" : f"Bearer { access_token } " ,
219
193
"Accept" : "application/vnd.github.london-preview+json" ,
@@ -247,9 +221,13 @@ def check_existing_config(repo, filename):
247
221
return None
248
222
249
223
250
- def enable_dependabot_security_updates (owner , repo , access_token ):
251
- """Enable Dependabot security updates at the /repos/:owner/:repo/automated-security-fixes endpoint using the requests library"""
252
- url = f"https://api.github.com/repos/{ owner } /{ repo } /automated-security-fixes"
224
+ def enable_dependabot_security_updates (ghe , owner , repo , access_token ):
225
+ """
226
+ Enable Dependabot security updates at the /repos/:owner/:repo/automated-security-fixes endpoint using the requests library
227
+ API: https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#enable-automated-security-fixes
228
+ """
229
+ api_endpoint = f"{ ghe } /api/v3" if ghe else "https://api.github.com"
230
+ url = f"{ api_endpoint } /repos/{ owner } /{ repo } /automated-security-fixes"
253
231
headers = {
254
232
"Authorization" : f"Bearer { access_token } " ,
255
233
"Accept" : "application/vnd.github.london-preview+json" ,
@@ -277,9 +255,7 @@ def get_repos_iterator(organization, team_name, repository_list, github_connecti
277
255
else :
278
256
# Get the repositories from the repository_list
279
257
for repo in repository_list :
280
- repos .append (
281
- github_connection .repository (repo .split ("/" )[0 ], repo .split ("/" )[1 ])
282
- )
258
+ repos .append (github_connection .repository (repo .split ("/" )[0 ], repo .split ("/" )[1 ]))
283
259
284
260
return repos
285
261
@@ -290,7 +266,7 @@ def check_pending_pulls_for_duplicates(title, repo) -> bool:
290
266
skip = False
291
267
for pull_request in pull_requests :
292
268
if pull_request .title .startswith (title ):
293
- print ("\t Pull request already exists: " + pull_request .html_url )
269
+ print (f "\t Pull request already exists: { pull_request .html_url } " )
294
270
skip = True
295
271
break
296
272
return skip
@@ -302,7 +278,7 @@ def check_pending_issues_for_duplicates(title, repo) -> bool:
302
278
skip = False
303
279
for issue in issues :
304
280
if issue .title .startswith (title ):
305
- print ("\t Issue already exists: " + issue .html_url )
281
+ print (f "\t Issue already exists: { issue .html_url } " )
306
282
skip = True
307
283
break
308
284
return skip
@@ -338,19 +314,19 @@ def commit_changes(
338
314
branch = branch_name ,
339
315
)
340
316
341
- pull = repo .create_pull (
342
- title = title , body = body , head = branch_name , base = repo .default_branch
343
- )
317
+ pull = repo .create_pull (title = title , body = body , head = branch_name , base = repo .default_branch )
344
318
return pull
345
319
346
320
347
- def get_global_project_id (token , organization , number ):
348
- """Fetches the project ID from GitHub's GraphQL API."""
349
- url = "https://api.github.com/graphql"
321
+ def get_global_project_id (ghe , token , organization , number ):
322
+ """
323
+ Fetches the project ID from GitHub's GraphQL API.
324
+ API: https://docs.github.com/en/graphql/guides/forming-calls-with-graphql
325
+ """
326
+ api_endpoint = f"{ ghe } /api/v3" if ghe else "https://api.github.com"
327
+ url = f"{ api_endpoint } /graphql"
350
328
headers = {"Authorization" : f"Bearer { token } " }
351
- data = {
352
- "query" : f'query{{organization(login: "{ organization } ") {{projectV2(number: { number } ){{id}}}}}}'
353
- }
329
+ data = {"query" : f'query{{organization(login: "{ organization } ") {{projectV2(number: { number } ){{id}}}}}}' }
354
330
355
331
try :
356
332
response = requests .post (url , headers = headers , json = data , timeout = 20 )
@@ -366,9 +342,13 @@ def get_global_project_id(token, organization, number):
366
342
return None
367
343
368
344
369
- def get_global_issue_id (token , organization , repository , issue_number ):
370
- """Fetches the issue ID from GitHub's GraphQL API"""
371
- url = "https://api.github.com/graphql"
345
+ def get_global_issue_id (ghe , token , organization , repository , issue_number ):
346
+ """
347
+ Fetches the issue ID from GitHub's GraphQL API
348
+ API: https://docs.github.com/en/graphql/guides/forming-calls-with-graphql
349
+ """
350
+ api_endpoint = f"{ ghe } /api/v3" if ghe else "https://api.github.com"
351
+ url = f"{ api_endpoint } /graphql"
372
352
headers = {"Authorization" : f"Bearer { token } " }
373
353
data = {
374
354
"query" : f"""
@@ -396,9 +376,13 @@ def get_global_issue_id(token, organization, repository, issue_number):
396
376
return None
397
377
398
378
399
- def get_global_pr_id (token , organization , repository , pr_number ):
400
- """Fetches the pull request ID from GitHub's GraphQL API"""
401
- url = "https://api.github.com/graphql"
379
+ def get_global_pr_id (ghe , token , organization , repository , pr_number ):
380
+ """
381
+ Fetches the pull request ID from GitHub's GraphQL API
382
+ API: https://docs.github.com/en/graphql/guides/forming-calls-with-graphql
383
+ """
384
+ api_endpoint = f"{ ghe } /api/v3" if ghe else "https://api.github.com"
385
+ url = f"{ api_endpoint } /graphql"
402
386
headers = {"Authorization" : f"Bearer { token } " }
403
387
data = {
404
388
"query" : f"""
@@ -426,13 +410,15 @@ def get_global_pr_id(token, organization, repository, pr_number):
426
410
return None
427
411
428
412
429
- def link_item_to_project (token , project_id , item_id ):
430
- """Links an item (issue or pull request) to a project in GitHub."""
431
- url = "https://api.github.com/graphql"
413
+ def link_item_to_project (ghe , token , project_id , item_id ):
414
+ """
415
+ Links an item (issue or pull request) to a project in GitHub.
416
+ API: https://docs.github.com/en/graphql/guides/forming-calls-with-graphql
417
+ """
418
+ api_endpoint = f"{ ghe } /api/v3" if ghe else "https://api.github.com"
419
+ url = f"{ api_endpoint } /graphql"
432
420
headers = {"Authorization" : f"Bearer { token } " }
433
- data = {
434
- "query" : f'mutation {{addProjectV2ItemById(input: {{projectId: "{ project_id } ", contentId: "{ item_id } "}}) {{item {{id}}}}}}'
435
- }
421
+ data = {"query" : f'mutation {{addProjectV2ItemById(input: {{projectId: "{ project_id } ", contentId: "{ item_id } "}}) {{item {{id}}}}}}' }
436
422
437
423
try :
438
424
response = requests .post (url , headers = headers , json = data , timeout = 20 )
0 commit comments