Add functionality to calculate and log PR statistics on closure

This commit is contained in:
mrT23
2024-02-26 20:02:11 +02:00
parent 34fe2721fb
commit 4921c26432
3 changed files with 36 additions and 2 deletions

View File

@ -190,6 +190,8 @@ class GitProvider(ABC):
def auto_approve(self) -> bool: def auto_approve(self) -> bool:
return False return False
def calc_pr_statistics(self, pull_request_data: dict):
return {}
def get_main_pr_language(languages, files) -> str: def get_main_pr_language(languages, files) -> str:

View File

@ -701,3 +701,24 @@ class GithubProvider(GitProvider):
except Exception as e: except Exception as e:
get_logger().exception(f"Failed to auto-approve, error: {e}") get_logger().exception(f"Failed to auto-approve, error: {e}")
return False return False
def calc_pr_statistics(self, pull_request_data: dict):
try:
out = {}
from datetime import datetime
created_at = pull_request_data['created_at']
closed_at = pull_request_data['closed_at']
closed_at_datetime = datetime.strptime(closed_at, "%Y-%m-%dT%H:%M:%SZ")
created_at_datetime = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%SZ")
difference = closed_at_datetime - created_at_datetime
out['hours'] = difference.total_seconds() / 3600
out['commits'] = pull_request_data['commits']
out['comments'] = pull_request_data['comments']
out['review_comments'] = pull_request_data['review_comments']
out['changed_files'] = pull_request_data['changed_files']
out['additions'] = pull_request_data['additions']
out['deletions'] = pull_request_data['deletions']
except Exception as e:
get_logger().exception(f"Failed to calculate PR statistics, error: {e}")
return {}
return out

View File

@ -206,6 +206,14 @@ async def handle_push_trigger_for_new_commits(body: Dict[str, Any],
_duplicate_push_triggers[api_url] -= 1 _duplicate_push_triggers[api_url] -= 1
def handle_closed_pr(body, event, action, log_context):
pull_request = body.get("pull_request")
api_url = pull_request.get("url")
pr_statistics = get_git_provider()(pr_url=api_url).calc_pr_statistics(pull_request)
with get_logger().contextualize(pr_statistics=pr_statistics):
get_logger().info("PR-Agent closed pr statistics", analytics=True)
async def handle_request(body: Dict[str, Any], event: str): async def handle_request(body: Dict[str, Any], event: str):
""" """
Handle incoming GitHub webhook requests. Handle incoming GitHub webhook requests.
@ -229,13 +237,16 @@ async def handle_request(body: Dict[str, Any], event: str):
get_logger().debug(f'Request body', artifact=body) get_logger().debug(f'Request body', artifact=body)
await handle_comments_on_pr(body, event, sender, sender_id, action, log_context, agent) await handle_comments_on_pr(body, event, sender, sender_id, action, log_context, agent)
# handle new PRs # handle new PRs
elif event == 'pull_request' and action != 'synchronize': elif event == 'pull_request' and action != 'synchronize' and action != 'closed':
get_logger().debug(f'Request body', artifact=body) get_logger().debug(f'Request body', artifact=body)
await handle_new_pr_opened(body, event, sender, sender_id, action, log_context, agent) await handle_new_pr_opened(body, event, sender, sender_id, action, log_context, agent)
# handle pull_request event with synchronize action - "push trigger" for new commits # handle pull_request event with synchronize action - "push trigger" for new commits
elif event == 'pull_request' and action == 'synchronize': elif event == 'pull_request' and action == 'synchronize':
get_logger().debug(f'Request body', artifact=body) get_logger().debug(f'Request body', artifact=body)
await handle_push_trigger_for_new_commits(body, event, sender, sender_id, action, log_context, agent) await handle_push_trigger_for_new_commits(body, event, sender, sender_id, action, log_context, agent)
elif event == 'pull_request' and action == 'closed':
if get_settings().get("CONFIG.ANALYTICS_FOLDER", ""):
handle_closed_pr(body, event, action, log_context)
else: else:
get_logger().info(f"event {event=} action {action=} does not require any handling") get_logger().info(f"event {event=} action {action=} does not require any handling")
return {} return {}