kumar1907 commited on
Commit
3866642
·
verified ·
1 Parent(s): 479a6e6

Update pr_fetcher.py

Browse files
Files changed (1) hide show
  1. pr_fetcher.py +10 -17
pr_fetcher.py CHANGED
@@ -1,28 +1,21 @@
1
  import requests
2
 
3
- def fetch_pr_diff(repo_owner, repo_name, pr_number, github_token):
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/{repo_owner}/{repo_name}/pulls/{pr_number}"
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 {response.status_code}: {response.text}"
 
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}"