Add benchmarks comparison job

This commit is contained in:
Siarhei Fedartsou 2024-05-11 20:03:22 +02:00
parent 2423687b2f
commit ffe6f17380

View File

@ -2,6 +2,7 @@ import requests
import os import os
import re import re
import sys import sys
import json
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN') GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
REPO = os.getenv('GITHUB_REPOSITORY') 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] rows = [f"| {result['name']} | {result['base']} | {result['pr']} |" for result in results]
return f"{header}\n" + "\n".join(rows) return f"{header}\n" + "\n".join(rows)
def get_pr_comments(repo_owner, repo_name, pr_number): def get_pr_details(repo_owner, repo_name, pr_number):
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/issues/{pr_number}/comments" url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/pulls/{pr_number}"
headers = {'Authorization': f'token {GITHUB_TOKEN}'} headers = {'Authorization': f'token {GITHUB_TOKEN}'}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
def update_comment(comment_id, repo_owner, repo_name, body): def update_pr_description(repo_owner, repo_name, pr_number, body):
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/issues/comments/{comment_id}" url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/pulls/{pr_number}"
headers = {'Authorization': f'token {GITHUB_TOKEN}'} headers = {'Authorization': f'token {GITHUB_TOKEN}'}
data = {'body': body} data = {'body': body}
response = requests.patch(url, headers=headers, json=data) response = requests.patch(url, headers=headers, json=data)
@ -60,27 +61,27 @@ def main():
benchmark_results = collect_benchmark_results(base_folder, pr_folder) benchmark_results = collect_benchmark_results(base_folder, pr_folder)
comments = get_pr_comments(REPO_OWNER, REPO_NAME, PR_NUMBER) print(json.dumps(benchmark_results, indent=2))
if not comments or len(comments) > 0:
print("No comments found on this PR.") pr_details = get_pr_details(REPO_OWNER, REPO_NAME, PR_NUMBER)
exit(1) pr_body = pr_details.get('body', '')
first_comment = comments[0]
markdown_table = create_markdown_table(benchmark_results) markdown_table = create_markdown_table(benchmark_results)
new_benchmark_section = f"<!-- BENCHMARK_RESULTS_START -->\n## Benchmark Results\n{markdown_table}\n<!-- BENCHMARK_RESULTS_END -->" new_benchmark_section = f"<!-- BENCHMARK_RESULTS_START -->\n## Benchmark Results\n{markdown_table}\n<!-- BENCHMARK_RESULTS_END -->"
if re.search(r'<!-- BENCHMARK_RESULTS_START -->.*<!-- BENCHMARK_RESULTS_END -->', first_comment['body'], re.DOTALL): if re.search(r'<!-- BENCHMARK_RESULTS_START -->.*<!-- BENCHMARK_RESULTS_END -->', pr_body, re.DOTALL):
updated_body = re.sub( updated_body = re.sub(
r'<!-- BENCHMARK_RESULTS_START -->.*<!-- BENCHMARK_RESULTS_END -->', r'<!-- BENCHMARK_RESULTS_START -->.*<!-- BENCHMARK_RESULTS_END -->',
new_benchmark_section, new_benchmark_section,
first_comment['body'], pr_body,
flags=re.DOTALL flags=re.DOTALL
) )
else: 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) update_pr_description(REPO_OWNER, REPO_NAME, PR_NUMBER, updated_body)
print("PR comment updated successfully.") print("PR description updated successfully.")