From 4921c26432c37e3bbdd6f25a7385e4e205436186 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Mon, 26 Feb 2024 20:02:11 +0200 Subject: [PATCH 1/2] Add functionality to calculate and log PR statistics on closure --- pr_agent/git_providers/git_provider.py | 2 ++ pr_agent/git_providers/github_provider.py | 23 ++++++++++++++++++++++- pr_agent/servers/github_app.py | 13 ++++++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pr_agent/git_providers/git_provider.py b/pr_agent/git_providers/git_provider.py index ec4c8c54..3ef86709 100644 --- a/pr_agent/git_providers/git_provider.py +++ b/pr_agent/git_providers/git_provider.py @@ -190,6 +190,8 @@ class GitProvider(ABC): def auto_approve(self) -> bool: return False + def calc_pr_statistics(self, pull_request_data: dict): + return {} def get_main_pr_language(languages, files) -> str: diff --git a/pr_agent/git_providers/github_provider.py b/pr_agent/git_providers/github_provider.py index 1c23b328..ea152b9e 100644 --- a/pr_agent/git_providers/github_provider.py +++ b/pr_agent/git_providers/github_provider.py @@ -700,4 +700,25 @@ class GithubProvider(GitProvider): return False except Exception as e: get_logger().exception(f"Failed to auto-approve, error: {e}") - return False \ No newline at end of file + 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 diff --git a/pr_agent/servers/github_app.py b/pr_agent/servers/github_app.py index 445facba..777e3b13 100644 --- a/pr_agent/servers/github_app.py +++ b/pr_agent/servers/github_app.py @@ -206,6 +206,14 @@ async def handle_push_trigger_for_new_commits(body: Dict[str, Any], _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): """ 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) await handle_comments_on_pr(body, event, sender, sender_id, action, log_context, agent) # 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) 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 elif event == 'pull_request' and action == 'synchronize': get_logger().debug(f'Request body', artifact=body) 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: get_logger().info(f"event {event=} action {action=} does not require any handling") return {} From 60a37158b1763871d0031ca7735b9a067732552c Mon Sep 17 00:00:00 2001 From: mrT23 Date: Mon, 26 Feb 2024 20:09:01 +0200 Subject: [PATCH 2/2] Add functionality to calculate and log PR statistics on closure --- pr_agent/servers/github_app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pr_agent/servers/github_app.py b/pr_agent/servers/github_app.py index 777e3b13..0b379777 100644 --- a/pr_agent/servers/github_app.py +++ b/pr_agent/servers/github_app.py @@ -211,7 +211,7 @@ def handle_closed_pr(body, event, action, log_context): 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) + get_logger().info("PR-Agent statistics for closed PR", analytics=True) async def handle_request(body: Dict[str, Any], event: str):