fix(backend): Fix Github PR blocks (#8908)

<!-- Clearly explain the need for these changes: -->

### Background

The Github PR blocks are not able to function properly because the
correct endpoint is not getting called.
- Resolves #8667

### Changes 🏗️
* Added logic to derive correct endpoint from the given PR url.
* This logic is implemented in multiple blocks viz.
`GithubReadPullRequestBlock`, `GithubAssignPRReviewerBlock`,
`GithubUnassignPRReviewerBlock`, `GithubListPRReviewersBlock`.

### Test
* Github List PR Reviewers
<img width="511" alt="Screenshot 2024-12-03 at 11 03 59 PM"
src="https://github.com/user-attachments/assets/9c69edcf-c2f4-42d2-954d-0fc4d903ae22">

* Github Read Pull Request (Include Pr Changes checked)
<img width="417" alt="Screenshot 2024-12-06 at 12 01 41 PM"
src="https://github.com/user-attachments/assets/986fada7-7fbb-41b6-a42a-47d1e11fa562">

---------

Co-authored-by: Zamil Majdy <zamil.majdy@agpt.co>
This commit is contained in:
vishesh10 2024-12-09 20:07:02 +05:30 committed by GitHub
parent 6997e2a170
commit 281cd2910b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,3 +1,5 @@
import re
from typing_extensions import TypedDict
from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema
@ -253,7 +255,7 @@ class GithubReadPullRequestBlock(Block):
@staticmethod
def read_pr_changes(credentials: GithubCredentials, pr_url: str) -> str:
api = get_api(credentials)
files_url = pr_url + "/files"
files_url = prepare_pr_api_url(pr_url=pr_url, path="files")
response = api.get(files_url)
files = response.json()
changes = []
@ -331,7 +333,7 @@ class GithubAssignPRReviewerBlock(Block):
credentials: GithubCredentials, pr_url: str, reviewer: str
) -> str:
api = get_api(credentials)
reviewers_url = pr_url + "/requested_reviewers"
reviewers_url = prepare_pr_api_url(pr_url=pr_url, path="requested_reviewers")
data = {"reviewers": [reviewer]}
api.post(reviewers_url, json=data)
return "Reviewer assigned successfully"
@ -398,7 +400,7 @@ class GithubUnassignPRReviewerBlock(Block):
credentials: GithubCredentials, pr_url: str, reviewer: str
) -> str:
api = get_api(credentials)
reviewers_url = pr_url + "/requested_reviewers"
reviewers_url = prepare_pr_api_url(pr_url=pr_url, path="requested_reviewers")
data = {"reviewers": [reviewer]}
api.delete(reviewers_url, json=data)
return "Reviewer unassigned successfully"
@ -478,7 +480,7 @@ class GithubListPRReviewersBlock(Block):
credentials: GithubCredentials, pr_url: str
) -> list[Output.ReviewerItem]:
api = get_api(credentials)
reviewers_url = pr_url + "/requested_reviewers"
reviewers_url = prepare_pr_api_url(pr_url=pr_url, path="requested_reviewers")
response = api.get(reviewers_url)
data = response.json()
reviewers: list[GithubListPRReviewersBlock.Output.ReviewerItem] = [
@ -499,3 +501,14 @@ class GithubListPRReviewersBlock(Block):
input_data.pr_url,
)
yield from (("reviewer", reviewer) for reviewer in reviewers)
def prepare_pr_api_url(pr_url: str, path: str) -> str:
# Pattern to capture the base repository URL and the pull request number
pattern = r"^(?:https?://)?([^/]+/[^/]+/[^/]+)/pull/(\d+)"
match = re.match(pattern, pr_url)
if not match:
return pr_url
base_url, pr_number = match.groups()
return f"{base_url}/pulls/{pr_number}/{path}"