From ffe6f17380976a78751b4a04c06d7bd159371d7a Mon Sep 17 00:00:00 2001 From: Siarhei Fedartsou Date: Sat, 11 May 2024 20:03:22 +0200 Subject: [PATCH] Add benchmarks comparison job --- scripts/ci/post_benchmark_results.py | 29 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/scripts/ci/post_benchmark_results.py b/scripts/ci/post_benchmark_results.py index 331c1cee8..9dc5e93d9 100644 --- a/scripts/ci/post_benchmark_results.py +++ b/scripts/ci/post_benchmark_results.py @@ -2,6 +2,7 @@ import requests import os import re import sys +import json GITHUB_TOKEN = os.getenv('GITHUB_TOKEN') REPO = os.getenv('GITHUB_REPOSITORY') @@ -14,15 +15,15 @@ def create_markdown_table(results): rows = [f"| {result['name']} | {result['base']} | {result['pr']} |" for result in results] return f"{header}\n" + "\n".join(rows) -def get_pr_comments(repo_owner, repo_name, pr_number): - url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/issues/{pr_number}/comments" +def get_pr_details(repo_owner, repo_name, pr_number): + url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/pulls/{pr_number}" headers = {'Authorization': f'token {GITHUB_TOKEN}'} response = requests.get(url, headers=headers) response.raise_for_status() return response.json() -def update_comment(comment_id, repo_owner, repo_name, body): - url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/issues/comments/{comment_id}" +def update_pr_description(repo_owner, repo_name, pr_number, body): + url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/pulls/{pr_number}" headers = {'Authorization': f'token {GITHUB_TOKEN}'} data = {'body': body} response = requests.patch(url, headers=headers, json=data) @@ -60,27 +61,27 @@ def main(): benchmark_results = collect_benchmark_results(base_folder, pr_folder) - comments = get_pr_comments(REPO_OWNER, REPO_NAME, PR_NUMBER) - if not comments or len(comments) > 0: - print("No comments found on this PR.") - exit(1) + print(json.dumps(benchmark_results, indent=2)) + + pr_details = get_pr_details(REPO_OWNER, REPO_NAME, PR_NUMBER) + pr_body = pr_details.get('body', '') + - first_comment = comments[0] markdown_table = create_markdown_table(benchmark_results) new_benchmark_section = f"\n## Benchmark Results\n{markdown_table}\n" - if re.search(r'.*', first_comment['body'], re.DOTALL): + if re.search(r'.*', pr_body, re.DOTALL): updated_body = re.sub( r'.*', new_benchmark_section, - first_comment['body'], + pr_body, flags=re.DOTALL ) else: - updated_body = f"{first_comment['body']}\n\n{new_benchmark_section}" + updated_body = f"{pr_body}\n\n{new_benchmark_section}" - update_comment(first_comment['id'], REPO_OWNER, REPO_NAME, updated_body) - print("PR comment updated successfully.") + update_pr_description(REPO_OWNER, REPO_NAME, PR_NUMBER, updated_body) + print("PR description updated successfully.")