Spaces:
Sleeping
Sleeping
Update pr_fetcher.py
Browse files- pr_fetcher.py +10 -17
pr_fetcher.py
CHANGED
|
@@ -1,28 +1,21 @@
|
|
| 1 |
import requests
|
| 2 |
|
| 3 |
-
def fetch_pr_diff(
|
| 4 |
-
"""
|
| 5 |
-
Fetches the diff of a pull request from a GitHub repository.
|
| 6 |
-
|
| 7 |
-
Args:
|
| 8 |
-
repo_owner (str): The owner of the GitHub repository.
|
| 9 |
-
repo_name (str): The name of the GitHub repository.
|
| 10 |
-
pr_number (int): The pull request number.
|
| 11 |
-
github_token (str): A personal access token for GitHub API.
|
| 12 |
-
|
| 13 |
-
Returns:
|
| 14 |
-
str: The diff text of the pull request, or an error message.
|
| 15 |
-
"""
|
| 16 |
headers = {
|
| 17 |
-
"Accept": "application/vnd.github.v3.diff"
|
| 18 |
-
"Authorization": f"Bearer {github_token}"
|
| 19 |
}
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
url = f"https://api.github.com/repos/{
|
| 22 |
|
| 23 |
response = requests.get(url, headers=headers)
|
| 24 |
|
| 25 |
if response.status_code == 200:
|
| 26 |
return response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
else:
|
| 28 |
-
return f"Error
|
|
|
|
| 1 |
import requests
|
| 2 |
|
| 3 |
+
def fetch_pr_diff(owner, repo, pr_number, token=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
headers = {
|
| 5 |
+
"Accept": "application/vnd.github.v3.diff"
|
|
|
|
| 6 |
}
|
| 7 |
+
if token:
|
| 8 |
+
headers["Authorization"] = f"token {token}"
|
| 9 |
|
| 10 |
+
url = f"https://api.github.com/repos/{owner}/{repo}/pulls/{pr_number}"
|
| 11 |
|
| 12 |
response = requests.get(url, headers=headers)
|
| 13 |
|
| 14 |
if response.status_code == 200:
|
| 15 |
return response.text
|
| 16 |
+
elif response.status_code == 404:
|
| 17 |
+
return "Error: PR not found. Check the repo and PR number."
|
| 18 |
+
elif response.status_code == 401:
|
| 19 |
+
return "Error: Unauthorized. Check your GitHub token."
|
| 20 |
else:
|
| 21 |
+
return f"Error: Could not fetch PR. Status code: {response.status_code}"
|