Skip to content

Commit

Permalink
Merge pull request #1267 from Codium-ai/tr/should_process
Browse files Browse the repository at this point in the history
feat: enhance PR processing logic across GitLab, GitHub, and Bitbucket
  • Loading branch information
mrT23 authored Oct 2, 2024
2 parents af5a50a + e21d9dc commit 481c2a5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
7 changes: 5 additions & 2 deletions pr_agent/servers/bitbucket_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ async def handle_manifest(request: Request, response: Response):
return JSONResponse(manifest_obj)


async def _perform_commands_bitbucket(commands_conf: str, agent: PRAgent, api_url: str, log_context: dict):
async def _perform_commands_bitbucket(commands_conf: str, agent: PRAgent, api_url: str, log_context: dict, data: dict):
apply_repo_settings(api_url)
if data.get("event", "") == "pullrequest:created":
if not should_process_pr_logic(data):
return
commands = get_settings().get(f"bitbucket_app.{commands_conf}", {})
get_settings().set("config.is_auto_command", True)
for command in commands:
Expand Down Expand Up @@ -193,7 +196,7 @@ async def inner():
if get_identity_provider().verify_eligibility("bitbucket",
sender_id, pr_url) is not Eligibility.NOT_ELIGIBLE:
if get_settings().get("bitbucket_app.pr_commands"):
await _perform_commands_bitbucket("pr_commands", PRAgent(), pr_url, log_context)
await _perform_commands_bitbucket("pr_commands", PRAgent(), pr_url, log_context, data)
elif event == "pullrequest:comment_created":
pr_url = data["data"]["pullrequest"]["links"]["html"]["href"]
log_context["api_url"] = pr_url
Expand Down
8 changes: 5 additions & 3 deletions pr_agent/servers/github_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def is_bot_user(sender, sender_type):
return False


def should_process_pr_logic(sender_type, sender, body) -> bool:
def should_process_pr_logic(body) -> bool:
try:
pull_request = body.get("pull_request", {})
title = pull_request.get("title", "")
Expand Down Expand Up @@ -306,10 +306,10 @@ async def handle_request(body: Dict[str, Any], event: str):
log_context, sender, sender_id, sender_type = get_log_context(body, event, action, build_number)

# logic to ignore PRs opened by bot, PRs with specific titles, labels, source branches, or target branches
if is_bot_user(sender, sender_type):
if is_bot_user(sender, sender_type) and 'check_run' not in body:
return {}
if action != 'created' and 'check_run' not in body:
if not should_process_pr_logic(sender_type, sender, body):
if not should_process_pr_logic(body):
return {}

if 'check_run' in body: # handle failed checks
Expand Down Expand Up @@ -373,6 +373,8 @@ def _check_pull_request_event(action: str, body: dict, log_context: dict) -> Tup
async def _perform_auto_commands_github(commands_conf: str, agent: PRAgent, body: dict, api_url: str,
log_context: dict):
apply_repo_settings(api_url)
if not should_process_pr_logic(body): # Here we already updated the configuration with the repo settings
return {}
commands = get_settings().get(f"github_app.{commands_conf}")
if not commands:
get_logger().info(f"New PR, but no auto commands configured")
Expand Down
16 changes: 11 additions & 5 deletions pr_agent/servers/gitlab_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ async def handle_request(api_url: str, body: str, log_context: dict, sender_id:


async def _perform_commands_gitlab(commands_conf: str, agent: PRAgent, api_url: str,
log_context: dict):
log_context: dict, data: dict):
apply_repo_settings(api_url)
if not should_process_pr_logic(data): # Here we already updated the configurations
return
commands = get_settings().get(f"gitlab.{commands_conf}", {})
get_settings().set("config.is_auto_command", True)
for command in commands:
Expand Down Expand Up @@ -90,8 +92,12 @@ def is_bot_user(data) -> bool:
return False


def should_process_pr_logic(data, title) -> bool:
def should_process_pr_logic(data) -> bool:
try:
if not data.get('object_attributes', {}):
return False
title = data['object_attributes'].get('title')

# logic to ignore MRs for titles, labels and source, target branches.
ignore_mr_title = get_settings().get("CONFIG.IGNORE_PR_TITLE", [])
ignore_mr_labels = get_settings().get("CONFIG.IGNORE_PR_LABELS", [])
Expand Down Expand Up @@ -173,9 +179,9 @@ async def inner(data: dict):
# ignore bot users
if is_bot_user(data):
return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder({"message": "success"}))
if data.get('event_type') != 'note' and data.get('object_attributes', {}): # not a comment
if data.get('event_type') != 'note': # not a comment
# ignore MRs based on title, labels, source and target branches
if not should_process_pr_logic(data, data['object_attributes'].get('title')):
if not should_process_pr_logic(data):
return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder({"message": "success"}))

log_context["sender"] = sender
Expand Down Expand Up @@ -220,7 +226,7 @@ async def inner(data: dict):
content=jsonable_encoder({"message": "success"}))

get_logger().debug(f'A push event has been received: {url}')
await _perform_commands_gitlab("push_commands", PRAgent(), url, log_context)
await _perform_commands_gitlab("push_commands", PRAgent(), url, log_context, data)
except Exception as e:
get_logger().error(f"Failed to handle push event: {e}")

Expand Down

0 comments on commit 481c2a5

Please sign in to comment.