@@ -21,6 +21,7 @@ def main(): # pragma: no cover
21
21
gh_app_id ,
22
22
gh_app_installation_id ,
23
23
gh_app_private_key ,
24
+ gh_app_enterprise_only ,
24
25
token ,
25
26
ghe ,
26
27
exempt_repositories_list ,
@@ -46,12 +47,17 @@ def main(): # pragma: no cover
46
47
47
48
# Auth to GitHub.com or GHE
48
49
github_connection = auth .auth_to_github (
49
- token , gh_app_id , gh_app_installation_id , gh_app_private_key , ghe
50
+ token ,
51
+ gh_app_id ,
52
+ gh_app_installation_id ,
53
+ gh_app_private_key ,
54
+ ghe ,
55
+ gh_app_enterprise_only ,
50
56
)
51
57
52
58
if not token and gh_app_id and gh_app_installation_id and gh_app_private_key :
53
59
token = auth .get_github_app_installation_token (
54
- gh_app_id , gh_app_private_key , gh_app_installation_id
60
+ ghe , gh_app_id , gh_app_private_key , gh_app_installation_id
55
61
)
56
62
57
63
# If Project ID is set, lookup the global project ID
@@ -61,7 +67,7 @@ def main(): # pragma: no cover
61
67
raise ValueError (
62
68
"ORGANIZATION environment variable was not set. Please set it"
63
69
)
64
- project_id = get_global_project_id (token , organization , project_id )
70
+ project_id = get_global_project_id (ghe , token , organization , project_id )
65
71
66
72
# Get the repositories from the organization, team name, or list of repositories
67
73
repos = get_repos_iterator (
@@ -78,13 +84,13 @@ def main(): # pragma: no cover
78
84
79
85
# Check all the things to see if repo is eligble for a pr/issue
80
86
if repo .full_name in exempt_repositories_list :
81
- print ("Skipping " + repo .full_name + " (exempted)" )
87
+ print (f "Skipping { repo .full_name } (exempted)" )
82
88
continue
83
89
if repo .archived :
84
- print ("Skipping " + repo .full_name + " (archived)" )
90
+ print (f "Skipping { repo .full_name } (archived)" )
85
91
continue
86
92
if repo .visibility .lower () not in filter_visibility :
87
- print ("Skipping " + repo .full_name + " (visibility-filtered)" )
93
+ print (f "Skipping { repo .full_name } (visibility-filtered)" )
88
94
continue
89
95
existing_config = None
90
96
filename_list = [".github/dependabot.yaml" , ".github/dependabot.yml" ]
@@ -97,19 +103,17 @@ def main(): # pragma: no cover
97
103
98
104
if existing_config and not update_existing :
99
105
print (
100
- "Skipping "
101
- + repo .full_name
102
- + " (dependabot file already exists and update_existing is False)"
106
+ f"Skipping { repo .full_name } (dependabot file already exists and update_existing is False)"
103
107
)
104
108
continue
105
109
106
110
if created_after_date and is_repo_created_date_before (
107
111
repo .created_at , created_after_date
108
112
):
109
- print ("Skipping " + repo .full_name + " (created after filter)" )
113
+ print (f "Skipping { repo .full_name } (created after filter)" )
110
114
continue
111
115
112
- print ("Checking " + repo .full_name + " for compatible package managers" )
116
+ print (f "Checking { repo .full_name } for compatible package managers" )
113
117
# Try to detect package managers and build a dependabot file
114
118
dependabot_file = build_dependabot_file (
115
119
repo ,
@@ -133,42 +137,36 @@ def main(): # pragma: no cover
133
137
if not skip :
134
138
print ("\t Eligible for configuring dependabot." )
135
139
count_eligible += 1
136
- print ("\t Configuration:\n " + dependabot_file )
140
+ print (f "\t Configuration:\n { dependabot_file } " )
137
141
if follow_up_type == "pull" :
138
142
# Try to detect if the repo already has an open pull request for dependabot
139
143
skip = check_pending_pulls_for_duplicates (title , repo )
140
144
if not skip :
141
145
print ("\t Eligible for configuring dependabot." )
142
146
count_eligible += 1
143
- print ("\t Configuration:\n " + dependabot_file )
147
+ print (f "\t Configuration:\n { dependabot_file } " )
144
148
continue
145
149
146
150
# Get dependabot security updates enabled if possible
147
151
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 )
152
+ if not is_dependabot_security_updates_enabled (
153
+ ghe , repo .owner , repo .name , token
154
+ ):
155
+ enable_dependabot_security_updates (ghe , repo .owner , repo .name , token )
150
156
151
157
if follow_up_type == "issue" :
152
158
skip = check_pending_issues_for_duplicates (title , repo )
153
159
if not skip :
154
160
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
- )
161
+ body_issue = f"{ body } \n \n ```yaml\n # { dependabot_filename_to_use } \n { dependabot_file } \n ```"
164
162
issue = repo .create_issue (title , body_issue )
165
- print ("\t Created issue " + issue .html_url )
163
+ print (f "\t Created issue { issue .html_url } " )
166
164
if project_id :
167
165
issue_id = get_global_issue_id (
168
- token , organization , repo .name , issue .number
166
+ ghe , token , organization , repo .name , issue .number
169
167
)
170
- link_item_to_project (token , project_id , issue_id )
171
- print ("\t Linked issue to project " + project_id )
168
+ link_item_to_project (ghe , token , project_id , issue_id )
169
+ print (f "\t Linked issue to project { project_id } " )
172
170
else :
173
171
# Try to detect if the repo already has an open pull request for dependabot
174
172
skip = check_pending_pulls_for_duplicates (title , repo )
@@ -186,19 +184,19 @@ def main(): # pragma: no cover
186
184
dependabot_filename_to_use ,
187
185
existing_config ,
188
186
)
189
- print ("\t Created pull request " + pull .html_url )
187
+ print (f "\t Created pull request { pull .html_url } " )
190
188
if project_id :
191
189
pr_id = get_global_pr_id (
192
- token , organization , repo .name , pull .number
190
+ ghe , token , organization , repo .name , pull .number
193
191
)
194
- response = link_item_to_project (token , project_id , pr_id )
192
+ response = link_item_to_project (ghe , token , project_id , pr_id )
195
193
if response :
196
- print ("\t Linked pull request to project " + project_id )
194
+ print (f "\t Linked pull request to project { project_id } " )
197
195
except github3 .exceptions .NotFoundError :
198
196
print ("\t Failed to create pull request. Check write permissions." )
199
197
continue
200
198
201
- print ("Done. " + str (count_eligible ) + " repositories were eligible." )
199
+ print (f "Done. { str (count_eligible )} repositories were eligible." )
202
200
203
201
204
202
def is_repo_created_date_before (repo_created_at : str , created_after_date : str ):
@@ -209,11 +207,13 @@ def is_repo_created_date_before(repo_created_at: str, created_after_date: str):
209
207
)
210
208
211
209
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
210
+ def is_dependabot_security_updates_enabled (ghe , owner , repo , access_token ):
211
+ """
212
+ Check if Dependabot security updates are enabled at the /repos/:owner/:repo/automated-security-fixes endpoint using the requests library
213
+ API: https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#check-if-automated-security-fixes-are-enabled-for-a-repository
215
214
"""
216
- url = f"https://api.github.com/repos/{ owner } /{ repo } /automated-security-fixes"
215
+ api_endpoint = f"{ ghe } /api/v3" if ghe else "https://api.github.com"
216
+ url = f"{ api_endpoint } /repos/{ owner } /{ repo } /automated-security-fixes"
217
217
headers = {
218
218
"Authorization" : f"Bearer { access_token } " ,
219
219
"Accept" : "application/vnd.github.london-preview+json" ,
@@ -247,9 +247,13 @@ def check_existing_config(repo, filename):
247
247
return None
248
248
249
249
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"
250
+ def enable_dependabot_security_updates (ghe , owner , repo , access_token ):
251
+ """
252
+ Enable Dependabot security updates at the /repos/:owner/:repo/automated-security-fixes endpoint using the requests library
253
+ API: https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#enable-automated-security-fixes
254
+ """
255
+ api_endpoint = f"{ ghe } /api/v3" if ghe else "https://api.github.com"
256
+ url = f"{ api_endpoint } /repos/{ owner } /{ repo } /automated-security-fixes"
253
257
headers = {
254
258
"Authorization" : f"Bearer { access_token } " ,
255
259
"Accept" : "application/vnd.github.london-preview+json" ,
@@ -290,7 +294,7 @@ def check_pending_pulls_for_duplicates(title, repo) -> bool:
290
294
skip = False
291
295
for pull_request in pull_requests :
292
296
if pull_request .title .startswith (title ):
293
- print ("\t Pull request already exists: " + pull_request .html_url )
297
+ print (f "\t Pull request already exists: { pull_request .html_url } " )
294
298
skip = True
295
299
break
296
300
return skip
@@ -302,7 +306,7 @@ def check_pending_issues_for_duplicates(title, repo) -> bool:
302
306
skip = False
303
307
for issue in issues :
304
308
if issue .title .startswith (title ):
305
- print ("\t Issue already exists: " + issue .html_url )
309
+ print (f "\t Issue already exists: { issue .html_url } " )
306
310
skip = True
307
311
break
308
312
return skip
@@ -344,9 +348,13 @@ def commit_changes(
344
348
return pull
345
349
346
350
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"
351
+ def get_global_project_id (ghe , token , organization , number ):
352
+ """
353
+ Fetches the project ID from GitHub's GraphQL API.
354
+ API: https://docs.github.com/en/graphql/guides/forming-calls-with-graphql
355
+ """
356
+ api_endpoint = f"{ ghe } /api/v3" if ghe else "https://api.github.com"
357
+ url = f"{ api_endpoint } /graphql"
350
358
headers = {"Authorization" : f"Bearer { token } " }
351
359
data = {
352
360
"query" : f'query{{organization(login: "{ organization } ") {{projectV2(number: { number } ){{id}}}}}}'
@@ -366,9 +374,13 @@ def get_global_project_id(token, organization, number):
366
374
return None
367
375
368
376
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"
377
+ def get_global_issue_id (ghe , token , organization , repository , issue_number ):
378
+ """
379
+ Fetches the issue ID from GitHub's GraphQL API
380
+ API: https://docs.github.com/en/graphql/guides/forming-calls-with-graphql
381
+ """
382
+ api_endpoint = f"{ ghe } /api/v3" if ghe else "https://api.github.com"
383
+ url = f"{ api_endpoint } /graphql"
372
384
headers = {"Authorization" : f"Bearer { token } " }
373
385
data = {
374
386
"query" : f"""
@@ -396,9 +408,13 @@ def get_global_issue_id(token, organization, repository, issue_number):
396
408
return None
397
409
398
410
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"
411
+ def get_global_pr_id (ghe , token , organization , repository , pr_number ):
412
+ """
413
+ Fetches the pull request ID from GitHub's GraphQL API
414
+ API: https://docs.github.com/en/graphql/guides/forming-calls-with-graphql
415
+ """
416
+ api_endpoint = f"{ ghe } /api/v3" if ghe else "https://api.github.com"
417
+ url = f"{ api_endpoint } /graphql"
402
418
headers = {"Authorization" : f"Bearer { token } " }
403
419
data = {
404
420
"query" : f"""
@@ -426,9 +442,13 @@ def get_global_pr_id(token, organization, repository, pr_number):
426
442
return None
427
443
428
444
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"
445
+ def link_item_to_project (ghe , token , project_id , item_id ):
446
+ """
447
+ Links an item (issue or pull request) to a project in GitHub.
448
+ API: https://docs.github.com/en/graphql/guides/forming-calls-with-graphql
449
+ """
450
+ api_endpoint = f"{ ghe } /api/v3" if ghe else "https://api.github.com"
451
+ url = f"{ api_endpoint } /graphql"
432
452
headers = {"Authorization" : f"Bearer { token } " }
433
453
data = {
434
454
"query" : f'mutation {{addProjectV2ItemById(input: {{projectId: "{ project_id } ", contentId: "{ item_id } "}}) {{item {{id}}}}}}'
0 commit comments