Compare commits

..

12 Commits

Author SHA1 Message Date
63a703c000 Handle marketplace hook 2023-07-28 11:30:51 +03:00
a8780f722d Handle marketplace hook 2023-07-28 03:22:25 +03:00
1a8fce1505 Updated handling of installation id 2023-07-28 02:44:28 +03:00
8519b106f9 Updated .gitignore 2023-07-28 02:28:50 +03:00
d375dd62fe Merge pull request #141 from patryk-kowalski-ds/pg/pip_package
Transition to pip package with pyproject.toml
2023-07-28 02:23:06 +03:00
3770bf8031 Update setup.py 2023-07-28 02:22:38 +03:00
42388b1f8d Merge pull request #146 from idavidov/idsvidov/gitlabpaginator_fix
Fix for GitLab Paginator in GitLab Provider
2023-07-28 02:01:04 +03:00
0167003bbc handle no diffs 2023-07-28 01:59:10 +03:00
2ce91fbdf5 Merge pull request #148 from eltociear/patch-1
Fix typo in PR_COMPRESSION.md
2023-07-28 01:50:30 +03:00
aa7659d6bf Fix typo in PR_COMPRESSION.md
Withing -> Within
2023-07-28 00:18:58 +09:00
99ed9b22a1 latest documentation suggest get_all not all
https://python-gitlab.readthedocs.io/en/stable/api-usage.html#pagination
2023-07-27 15:39:19 +03:00
eee6d51b40 issue #145
get all diffs in merge request and not only gitlab default 20
2023-07-27 14:41:36 +03:00
7 changed files with 44 additions and 17 deletions

4
.gitignore vendored
View File

@ -3,4 +3,6 @@ venv/
pr_agent/settings/.secrets.toml
__pycache__
dist/
*.egg-info/
*.egg-info/
build/
review.md

View File

@ -31,7 +31,7 @@ We prioritize additions over deletions:
- File patches are a list of hunks, remove all hunks of type deletion-only from the hunks in the file patch
#### Adaptive and token-aware file patch fitting
We use [tiktoken](https://github.com/openai/tiktoken) to tokenize the patches after the modifications described above, and we use the following strategy to fit the patches into the prompt:
1. Withing each language we sort the files by the number of tokens in the file (in descending order):
1. Within each language we sort the files by the number of tokens in the file (in descending order):
* ```[[file2.py, file.py],[file4.jsx, file3.js],[readme.md]]```
2. Iterate through the patches in the order described above
2. Add the patches to the prompt until the prompt reaches a certain buffer from the max token length
@ -39,4 +39,4 @@ We use [tiktoken](https://github.com/openai/tiktoken) to tokenize the patches af
4. If we haven't reached the max token length, add the `deleted files` to the prompt until the prompt reaches the max token length (hard stop), skip the rest of the patches.
### Example
![](https://codium.ai/images/git_patch_logic.png)
![](https://codium.ai/images/git_patch_logic.png)

View File

@ -5,6 +5,7 @@ from urllib.parse import urlparse
from github import AppAuthentication, Auth, Github, GithubException
from retry import retry
from starlette_context import context
from pr_agent.config_loader import settings
@ -17,7 +18,10 @@ from ..servers.utils import RateLimitExceeded
class GithubProvider(GitProvider):
def __init__(self, pr_url: Optional[str] = None, incremental=IncrementalPR(False)):
self.repo_obj = None
self.installation_id = settings.get("GITHUB.INSTALLATION_ID")
try:
self.installation_id = context.get("installation_id", None)
except Exception:
self.installation_id = None
self.github_client = self._get_github_client()
self.repo = None
self.pr_num = None

View File

@ -11,6 +11,8 @@ from pr_agent.config_loader import settings
from ..algo.language_handler import is_valid_file
from .git_provider import EDIT_TYPE, FilePatchInfo, GitProvider
logger = logging.getLogger()
class GitLabProvider(GitProvider):
@ -48,7 +50,12 @@ class GitLabProvider(GitProvider):
def _set_merge_request(self, merge_request_url: str):
self.id_project, self.id_mr = self._parse_merge_request_url(merge_request_url)
self.mr = self._get_merge_request()
self.last_diff = self.mr.diffs.list()[-1]
try:
self.last_diff = self.mr.diffs.list(get_all=True)[-1]
except IndexError as e:
logger.error(f"Could not get diff for merge request {self.id_mr}")
raise ValueError(f"Could not get diff for merge request {self.id_mr}") from e
def _get_pr_file_content(self, file_path: str, branch: str) -> str:
try:

View File

@ -4,6 +4,9 @@ import sys
import uvicorn
from fastapi import APIRouter, FastAPI, HTTPException, Request, Response
from starlette.middleware import Middleware
from starlette_context import context
from starlette_context.middleware import RawContextMiddleware
from pr_agent.agent.pr_agent import PRAgent
from pr_agent.config_loader import settings
@ -20,24 +23,35 @@ async def handle_github_webhooks(request: Request, response: Response):
Verifies the request signature, parses the request body, and passes it to the handle_request function for further processing.
"""
logging.debug("Received a GitHub webhook")
body = await get_body(request)
logging.debug(f'Request body:\n{body}')
installation_id = body.get("installation", {}).get("id")
context["installation_id"] = installation_id
return await handle_request(body)
@router.post("/api/v1/marketplace_webhooks")
async def handle_marketplace_webhooks(request: Request, response: Response):
body = await get_body(request)
logging.info(f'Request body:\n{body}')
async def get_body(request):
try:
body = await request.json()
except Exception as e:
logging.error("Error parsing request body", e)
raise HTTPException(status_code=400, detail="Error parsing request body") from e
body_bytes = await request.body()
signature_header = request.headers.get('x-hub-signature-256', None)
webhook_secret = getattr(settings.github, 'webhook_secret', None)
if webhook_secret:
verify_signature(body_bytes, webhook_secret, signature_header)
logging.debug(f'Request body:\n{body}')
return await handle_request(body)
return body
async def handle_request(body: Dict[str, Any]):
@ -48,8 +62,6 @@ async def handle_request(body: Dict[str, Any]):
body: The request body.
"""
action = body.get("action")
installation_id = body.get("installation", {}).get("id")
settings.set("GITHUB.INSTALLATION_ID", installation_id)
agent = PRAgent()
if action == 'created':
@ -85,7 +97,8 @@ async def root():
def start():
# Override the deployment type to app
settings.set("GITHUB.DEPLOYMENT_TYPE", "app")
app = FastAPI()
middleware = [Middleware(RawContextMiddleware)]
app = FastAPI(middleware=middleware)
app.include_router(router)
uvicorn.run(app, host="0.0.0.0", port=3000)

View File

@ -41,6 +41,7 @@ dependencies = [
"aiohttp~=3.8.4",
"atlassian-python-api==3.39.0",
"GitPython~=3.1.32",
"starlette-context==0.3.6"
]
[project.urls]

View File

@ -2,4 +2,4 @@
# see: https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html
from setuptools import setup
setup()
setup()