Skip to content

Commit

Permalink
Enhance logging and refactor code in gitlab_webhook.py, update logger…
Browse files Browse the repository at this point in the history
… level in bitbucket_app.py and github_polling.py, remove redundant section in github.md
  • Loading branch information
mrT23 committed Mar 11, 2024
1 parent 5f529c9 commit 6c5697b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
12 changes: 0 additions & 12 deletions docs/docs/installation/github.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,6 @@ When you open your next PR, you should see a comment from `github-actions` bot w
PR_CODE_SUGGESTIONS.NUM_CODE_SUGGESTIONS: 6 # Increase number of code suggestions
```

---
## Run as a polling server
Request reviews by tagging your GitHub user on a PR

Follow [steps 1-3](#run-as-a-github-action) of the GitHub Action setup.

Run the following command to start the server:

```
python pr_agent/servers/github_polling.py
```

---

## Run as a GitHub App
Expand Down
2 changes: 1 addition & 1 deletion pr_agent/servers/bitbucket_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from pr_agent.tools.pr_description import PRDescription
from pr_agent.tools.pr_reviewer import PRReviewer

setup_logger(fmt=LoggingFormat.JSON)
setup_logger(fmt=LoggingFormat.JSON, level="DEBUG")
router = APIRouter()
secret_provider = get_secret_provider() if get_settings().get("CONFIG.SECRET_PROVIDER") else None

Expand Down
2 changes: 1 addition & 1 deletion pr_agent/servers/github_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pr_agent.git_providers import get_git_provider
from pr_agent.log import LoggingFormat, get_logger, setup_logger

setup_logger(fmt=LoggingFormat.JSON)
setup_logger(fmt=LoggingFormat.JSON, level="DEBUG")
NOTIFICATION_URL = "https://api.github.com/notifications"


Expand Down
55 changes: 35 additions & 20 deletions pr_agent/servers/gitlab_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pr_agent.log import LoggingFormat, get_logger, setup_logger
from pr_agent.secret_providers import get_secret_provider

setup_logger(fmt=LoggingFormat.JSON)
setup_logger(fmt=LoggingFormat.JSON, level="DEBUG")
router = APIRouter()

secret_provider = get_secret_provider() if get_settings().get("CONFIG.SECRET_PROVIDER") else None
Expand Down Expand Up @@ -51,6 +51,9 @@ async def _perform_commands_gitlab(commands_conf: str, agent: PRAgent, api_url:
@router.post("/webhook")
async def gitlab_webhook(background_tasks: BackgroundTasks, request: Request):
log_context = {"server_type": "gitlab_app"}
get_logger().debug("Received a GitLab webhook")

# Check if the request is authorized
if request.headers.get("X-Gitlab-Token") and secret_provider:
request_token = request.headers.get("X-Gitlab-Token")
secret = secret_provider.get_secret(request_token)
Expand All @@ -66,46 +69,58 @@ async def gitlab_webhook(background_tasks: BackgroundTasks, request: Request):
elif get_settings().get("GITLAB.SHARED_SECRET"):
secret = get_settings().get("GITLAB.SHARED_SECRET")
if not request.headers.get("X-Gitlab-Token") == secret:
get_logger().error(f"Failed to validate secret")
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content=jsonable_encoder({"message": "unauthorized"}))
else:
get_logger().error(f"Failed to validate secret")
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content=jsonable_encoder({"message": "unauthorized"}))
gitlab_token = get_settings().get("GITLAB.PERSONAL_ACCESS_TOKEN", None)
if not gitlab_token:
get_logger().error(f"No gitlab token found")
return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content=jsonable_encoder({"message": "unauthorized"}))

data = await request.json()
get_logger().info(json.dumps(data))
get_logger().info("GitLab data", json.dumps(data))

if data.get('object_kind') == 'merge_request' and data['object_attributes'].get('action') in ['open', 'reopen']:
get_logger().info(f"A merge request has been opened: {data['object_attributes'].get('title')}")
url = data['object_attributes'].get('url')
get_logger().info(f"New merge request: {url}")
await _perform_commands_gitlab("pr_commands", PRAgent(), url, log_context)
# handle_request(background_tasks, url, "/review", log_context)
elif data.get('object_kind') == 'note' and data['event_type'] == 'note':
elif data.get('object_kind') == 'note' and data['event_type'] == 'note': # comment on MR
if 'merge_request' in data:
mr = data['merge_request']
url = mr.get('url')
get_logger().info(f"A comment has been added to a merge request: {url}")
body = data.get('object_attributes', {}).get('note')
if data.get('object_attributes', {}).get('type') == 'DiffNote' and '/ask' in body:
line_range_ = data['object_attributes']['position']['line_range']

# if line_range_['start']['type'] == 'new':
start_line = line_range_['start']['new_line']
end_line = line_range_['end']['new_line']
# else:
# start_line = line_range_['start']['old_line']
# end_line = line_range_['end']['old_line']

question = body.replace('/ask', '').strip()
path = data['object_attributes']['position']['new_path']
side = 'RIGHT'# if line_range_['start']['type'] == 'new' else 'LEFT'
comment_id = data['object_attributes']["discussion_id"]
get_logger().info(f"Handling line comment")
body = f"/ask_line --line_start={start_line} --line_end={end_line} --side={side} --file_name={path} --comment_id={comment_id} {question}"
if data.get('object_attributes', {}).get('type') == 'DiffNote' and '/ask' in body: # /ask_line
body = handle_ask_line(body, data)

handle_request(background_tasks, url, body, log_context)

return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder({"message": "success"}))


def handle_ask_line(body, data):
try:
line_range_ = data['object_attributes']['position']['line_range']
# if line_range_['start']['type'] == 'new':
start_line = line_range_['start']['new_line']
end_line = line_range_['end']['new_line']
# else:
# start_line = line_range_['start']['old_line']
# end_line = line_range_['end']['old_line']
question = body.replace('/ask', '').strip()
path = data['object_attributes']['position']['new_path']
side = 'RIGHT' # if line_range_['start']['type'] == 'new' else 'LEFT'
comment_id = data['object_attributes']["discussion_id"]
get_logger().info(f"Handling line comment")
body = f"/ask_line --line_start={start_line} --line_end={end_line} --side={side} --file_name={path} --comment_id={comment_id} {question}"
except Exception as e:
get_logger().error(f"Failed to handle ask line comment: {e}")
return body


@router.get("/")
async def root():
return {"status": "ok"}
Expand Down

0 comments on commit 6c5697b

Please sign in to comment.