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 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"<!-- 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(
r'<!-- BENCHMARK_RESULTS_START -->.*<!-- BENCHMARK_RESULTS_END -->',
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.")