-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_github.py
More file actions
102 lines (80 loc) · 3.14 KB
/
app_github.py
File metadata and controls
102 lines (80 loc) · 3.14 KB
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
import logging
import requests
from github import Github
from log.app_log import log_function_entry_exit
@log_function_entry_exit
def is_github_api_key_valid(api_key):
github_api_url = "https://api.github.com/user"
headers = {"Authorization": f"token {api_key}"}
try:
response = requests.get(github_api_url, headers=headers)
return response.status_code == 200
except requests.RequestException:
return False
@log_function_entry_exit
def does_github_pull_request_exist(api_key, repo_name, pr_number):
github_api_url = f"https://api.github.com/repos/{repo_name}/pulls/{pr_number}"
headers = {"Authorization": f"token {api_key}"}
try:
response = requests.get(github_api_url, headers=headers)
return response.status_code == 200
except requests.RequestException:
return False
@log_function_entry_exit
def does_github_repository_exist(api_key, repo_name):
github_api_url = f"https://api.github.com/repos/{repo_name}"
headers = {"Authorization": f"token {api_key}"}
try:
response = requests.get(github_api_url, headers=headers)
return response.status_code == 200
except requests.RequestException:
return False
@log_function_entry_exit
def get_diff(api_key, repo_name, pull_request_number):
"""
Get the diff and commit message for a GitHub pull request.
:param api_key: The GitHub API key to use.
:param repo_name: The name of the repository.
:param pull_request_number: The number of the pull request within the repo history.
:return: A list of diffs and the commit message.
"""
data = Github(api_key)
pull = data.get_user().get_repo(repo_name).get_pull(int(pull_request_number))
diff = pull.get_files()
# get the latest commit message
commit_message = pull.get_commits().reversed[0].commit.message
diffs = []
for dif in diff:
diffs.append(dif.patch)
return diffs, commit_message
# TODO: integrate this function into the main code
@log_function_entry_exit
def set_comment(api_key, repo_name, pull_request_number, comment):
print("set_comment")
data = Github(api_key)
pull = data.get_user().get_repo(repo_name).get_pull(int(pull_request_number))
pull.edit(body=comment)
@log_function_entry_exit
def test_github_api_connectivity():
github_api_url = "https://api.github.com"
try:
response = requests.get(github_api_url)
if response.status_code != 404:
return True
else:
logging.warning("OpenAI API connection failed.")
return False
except requests.RequestException:
logging.error("get request failed")
return False
@log_function_entry_exit
def validate_github_inputs(api_key, repo_name, pr_number):
try:
is_valid_key = is_github_api_key_valid(api_key)
# repo_exists = does_github_repository_exist(api_key, repo_name)
# pr_exists = does_github_pull_request_exist(api_key, repo_name, pr_number)
status = is_valid_key # and repo_exists and pr_exists
except Exception as e:
logging.error(f"Error validating GitHub inputs: {e}")
return False
return status