-
Notifications
You must be signed in to change notification settings - Fork 2
/
_github_utilities.py
617 lines (503 loc) · 18.2 KB
/
_github_utilities.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# This file contains utility functions using the github API via github-python:
# https://github.com/PyGithub/PyGithub (licensed LGPL3)
#
import os
from functools import lru_cache
from ._utilities import catch_error
from ._logger import Log
@lru_cache(maxsize=1)
def get_github_repository(repository):
"""
Get the GitHub repository object.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
Returns
-------
github.Repository.Repository
The GitHub repository object.
"""
from github import Github
access_token = os.getenv('GITHUB_API_KEY')
# Create a PyGithub instance using the access token
g = Github(access_token)
# Get the repository object
return g.get_repo(repository)
@catch_error
def add_comment_to_issue(repository, issue, comment):
"""
Add a comment to a specific GitHub issue.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
issue : int
The issue number to add a comment to.
comment : str
The comment text to add to the issue.
"""
Log().log(f"-> add_comment_to_issue({repository}, {issue}, ...)")
repo = get_github_repository(repository)
# Get the issue object
issue_obj = repo.get_issue(issue)
# Add a new comment to the issue
issue_obj.create_comment(comment)
print(f"Comment added to issue #{issue} in repository {repository}.")
@catch_error
def get_conversation_on_issue(repository, issue):
"""
Retrieve the entire conversation (title, body, and comments) of a specific GitHub issue.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
issue : int
The issue number to retrieve the conversation for.
Returns
-------
str
The conversation string containing the issue title, body, and comments.
"""
Log().log(f"-> get_conversation_on_issue({repository}, {issue})")
repo = get_github_repository(repository)
# Get the issue by number
issue_obj = repo.get_issue(issue)
# Get the conversation as a string
conversation = f"Issue Title: {issue_obj.title}\n\n"
conversation += f"Issue Body:\n{issue_obj.body}\n\n"
# Get all comments on the issue
comments = issue_obj.get_comments()
# Append each comment to the conversation string
for comment in comments:
conversation += f"Comment by {comment.user.login}:\n{comment.body}\n\n"
return conversation
@catch_error
def get_most_recent_comment_on_issue(repository, issue):
"""
Retrieve the most recent comment on a specific GitHub issue.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
issue : int
The issue number to retrieve the most recent comment for.
Returns
-------
tuple
A tuple containing the username of the commenter and the comment text.
"""
Log().log(f"-> get_most_recent_comment_on_issue({repository}, {issue})")
repo = get_github_repository(repository)
# Get the issue by number
issue_obj = repo.get_issue(issue)
# Get all comments on the issue
comments = issue_obj.get_comments()
# return last comment
comments = list(comments)
if len(comments) > 0:
comment = comments[-1]
user = comment.user.login
text = comment.body
else:
user = issue_obj.user.login
text = issue_obj.body
if text is None:
text = ""
return user, text
@catch_error
def list_issues(repository: str, state: str = "open") -> dict:
"""
List all GitHub issues with a defined state on a specified repository.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
state : str, optional
The issue status: can be "open", "closed", or "all".
Returns
-------
dict
A dictionary of issues where keys are issue numbers and values are issue titles.
"""
Log().log(f"-> list_issues({repository}, {state})")
repo = get_github_repository(repository)
# Fetch all issues with the specified state
issues = repo.get_issues(state=state)
result = {}
# Populate issue dictionary
for issue in issues:
result[issue.number] = issue.title
return result
@catch_error
def get_github_issue_details(repository: str, issue: int) -> str:
"""
Retrieve detailed information about a specific GitHub issue.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
issue : int
The issue number to retrieve details for.
Returns
-------
str
A string containing detailed information about the issue.
"""
from ._utilities import remove_indentation
Log().log(f"-> get_github_issue_details({repository}, {issue})")
repo = get_github_repository(repository)
# Fetch the specified issue
issue = repo.get_issue(number=issue)
# Format issue details
content = remove_indentation(f"""Issue #{issue.number}: {issue.title}
State: {issue.state}
Created at: {issue.created_at}
Updated at: {issue.updated_at}
Closed at: {issue.closed_at}
Author: {issue.user.login}
Assignees: {', '.join([assignee.login for assignee in issue.assignees])}
Labels: {', '.join([label.name for label in issue.labels])}
Comments: {issue.comments}
Body:
{issue.body}""")
# Add comments if any
if issue.comments > 0:
content += "\n\nComments:"
comments = issue.get_comments()
for comment in comments:
content += f"\n\nComment by {comment.user.login} on {comment.created_at}:\n{comment.body}"
return content
@catch_error
def list_repository_files(repository: str) -> list:
"""
List all files in a given GitHub repository.
This function uses the GitHub API to retrieve and list all files
in the specified repository.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
Returns
-------
list
A list of strings, where each string is the path of a file in the repository.
"""
Log().log(f"-> list_repository_files({repository})")
# Initialize Github client
repo = get_github_repository(repository)
# Get all contents of the repository
contents = repo.get_contents("")
# List to store all file paths
all_files = []
# Iterate through all contents
while contents:
file_content = contents.pop(0)
if file_content.type == "dir":
# If it's a directory, get its contents and add them to the list
contents.extend(repo.get_contents(file_content.path))
else:
# If it's a file, add its path to the list
all_files.append(file_content.path)
return all_files
@catch_error
def get_repository_file_contents(repository: str, file_paths: list) -> dict:
"""
Retrieve the contents of specified files from a GitHub repository.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
file_paths : list
A list of file paths within the repository to retrieve the contents of.
Returns
-------
dict
A dictionary where keys are file paths and values are the contents of the files.
"""
Log().log(f"-> get_repository_file_contents({repository}, {file_paths})")
# Dictionary to store file contents
file_contents = {}
# Iterate through the file paths
for file_path in file_paths:
try:
# Get the file content
file_content = get_file_in_repository (repository, "main", file_path).decoded_content.decode()
# store the content
file_contents[file_path] = file_content
except Exception as e:
file_contents[file_path] = f"Error accessing {file_path}: {str(e)}"
return file_contents
@catch_error
def write_file_in_new_branch(repository, branch_name, file_path, new_content, commit_message="Update file"):
"""
Modifies or creates a specified file with new content and saves the changes in a new git branch.
The name of the new branch is returned.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
file_path : str
A file path within the repository to change the contents of.
new_content : str
Text content that should be written into the file.
commit_message : str, optional
The commit message for the changes. Default is "Update file".
Returns
-------
str
The name of the branch where the changed file is stored.
"""
Log().log(f"-> write_file_in_new_branch({repository}, {branch_name}, {file_path}, ...)")
# Authenticate with GitHub
repo = get_github_repository(repository)
# Commit the changes
if check_if_file_exists(repository, branch_name, file_path):
file = get_file_in_repository(repository, branch_name, file_path)
repo.update_file(file.path, commit_message, new_content, file.sha, branch=branch_name)
else:
repo.create_file(file_path, commit_message, new_content, branch=branch_name)
return f"File {file_path} successfully created in repository {repository} branch {branch_name}."
@catch_error
def create_branch(repository, parent_branch="main"):
"""
Creates a new branch in a given repository, derived from an optionally specified parent_branch and returns the name of the new branch.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
parent_branch : str, optional
The name of the parent branch from which the new branch will be created. Default is "main".
Returns
-------
str
The name of the newly created branch.
"""
Log().log(f"-> create_branch({repository}, {parent_branch})")
import random
import string
# Authenticate with GitHub
repo = get_github_repository(repository)
# Get the main branch
main_branch = repo.get_branch(parent_branch)
# Create a new branch
new_branch_name = "git-bob-mod-" + ''.join(random.choices(string.ascii_letters + string.digits, k=10))
repo.create_git_ref(ref=f"refs/heads/{new_branch_name}", sha=main_branch.commit.sha)
return new_branch_name
@catch_error
def check_if_file_exists(repository, branch_name, file_path):
"""
Checks if a specified file_path exists in a GitHub repository. Returns True if the file exists, False otherwise.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
branch_name: str
The name of the branch to check the file in.
file_path : str
The path of the file to check.
Returns
-------
bool
True if the file exists, False otherwise.
"""
Log().log(f"-> check_if_file_exists({repository}, {file_path})")
# Authenticate with GitHub
#repo = get_github_repository(repository)
try:
# Try to get the contents of the file
get_file_in_repository(repository, branch_name, file_path)
#contents = repo.get_contents(file_path)
return True
except:
return False
@lru_cache(maxsize=1)
def get_file_in_repository(repository, branch_name, file_path):
"""
Helper function to prevent multiple calls to the GitHub API for the same file.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
branch_name : str
The name of the branch to get the file content from.
file_path : str
The path of the file in the repository.
Returns
-------
github.ContentFile.ContentFile
The content file object of the specified file.
"""
print("loading file content...", file_path)
repo = get_github_repository(repository)
return repo.get_contents(file_path, ref=branch_name)
@catch_error
def send_pull_request(repository, branch_name, title, description):
"""
Create a pull request from a defined branch into the main branch.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
branch_name : str
The name of the branch that should be merged into main.
title : str
A one-liner explaining what was changed in the branch.
description : str
A more detailed description of what has happened.
If the changes are related to an issue write "closes #99 "
where 99 stands for the issue number the pull-request is related to.
Returns
-------
str
The URL to the pull-request that was just created.
"""
Log().log(f"-> send_pull_request({repository}, {branch_name}, ...)")
from ._ai_github_utilities import setup_ai_remark
# Authenticate with GitHub
repo = get_github_repository(repository)
# Create a pull request
remark = setup_ai_remark() + "\n\n"
pr = repo.create_pull(title=title, body=remark + description, head=branch_name, base="main")
return f"Pull request created: {pr.html_url}"
@catch_error
def check_access_and_ask_for_approval(user, repository, issue):
"""
Check if the user has access rights and ask for approval if necessary.
Parameters
----------
user : str
The username of the person requesting access.
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
issue : int
The issue number related to the access request.
Returns
-------
bool
True if the user has access rights, False otherwise.
"""
# Check if the user is a repository member
Log().log(f"-> check_access_and_ask_for_approval({user}, {repository}, {issue})")
from ._utilities import remove_indentation
from ._ai_github_utilities import setup_ai_remark
repo = get_github_repository(repository)
members = [member.login for member in repo.get_collaborators()]
remark = setup_ai_remark()
if user not in members:
print("User does not have access rights.")
member_names = ", ".join(["@" + str(m) for m in members])
add_comment_to_issue(repository, issue, remove_indentation(remove_indentation(f"""{remark}
Hi @{user},
thanks for reaching out! Unfortunately, I'm not allowed to respond to you directly.
I need approval from a repository member: {member_names}
Best,
git-bob
""")))
return False
return True
@catch_error
@catch_error
def get_diff_of_pull_request(repository, pull_request):
"""
Get the diff of a specific pull request in a GitHub repository.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
pull_request : int
The pull request number to retrieve the diff for.
Returns
-------
str
The diff of the pull request.
"""
import requests
Log().log(f"-> get_diff_of_pull_request({repository}, {pull_request})")
# Authenticate with GitHub
repo = get_github_repository(repository)
pull_request = repo.get_pull(pull_request)
print(pull_request.diff_url)
# read the content of a url
response = requests.get(pull_request.diff_url)
if response.status_code == 200:
# Return the content of the website
return response.text
else:
return None
@catch_error
def add_reaction_to_issue(repository, issue, reaction="+1"):
"""
Add a given reaction to a github issue.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
issue : int
The issue number to add a reaction to.
reaction : str
The reaction to add. Default is "+1".
"""
Log().log(f"-> add_reaction_to_issue({repository}, {issue}, {reaction})")
repo = get_github_repository(repository)
# Fetch the specified issue
issue = repo.get_issue(number=issue)
issue.create_reaction(reaction)
@catch_error
def add_reaction_to_last_comment_in_issue(repository, issue, reaction="+1"):
"""
Add a given reaction to the last comment in a github issue.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
issue : int
The issue number to add a reaction to.
reaction : str
The reaction to add. Default is "+1".
"""
Log().log(f"-> add_reaction_to_last_comment_in_issue({repository}, {issue}, {reaction})")
repo = get_github_repository(repository)
# Get the issue by number
issue_obj = repo.get_issue(issue)
# Get all comments on the issue
comments = issue_obj.get_comments()
# return last comment
comments = list(comments)
if len(comments) > 0:
comments[-1].create_reaction(reaction)
else:
issue_obj.create_reaction(reaction)
@catch_error
def get_diff_of_branches(repository, compare_branch, base_branch="main"):
"""
Get the diff between two branches in a GitHub repository.
Parameters
----------
repository : str
The full name of the GitHub repository (e.g., "username/repo-name").
compare_branch : str
The branch to compare against the base branch.
base_branch : str, optional
The base branch to compare against. Default is "main".
Returns
-------
str
The diff between the specified branches as a string.
"""
# Get the repository
repo = get_github_repository(repository)
# Get the comparison between branches
comparison = repo.compare(base_branch, compare_branch)
# Initialize output variable
output = []
# Collect the diff
for file in comparison.files:
output.append(f"\nFile: {file.filename}")
output.append(f"Status: {file.status}")
output.append("-" * 40)
if file.patch:
output.append(file.patch)
else:
output.append("No diff available (possibly a binary file)")
return "\n".join(output)