Skip to content

Commit 32a3e3b

Browse files
committed
Merge branch 'ci/description_comments' into 'main'
Moved Static Memory Numbers from MR comments to description See merge request app-frameworks/esp-matter!1031
2 parents c39d4ae + 27fc964 commit 32a3e3b

File tree

1 file changed

+65
-17
lines changed

1 file changed

+65
-17
lines changed

tools/ci/post_results.py

+65-17
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,69 @@
88
import glob
99
import argparse
1010
import logging
11+
import re
1112

1213
# Gitlab Configurations
1314
gitlab_api_url = os.getenv("CI_API_V4_URL")
1415
gitlab_token = os.getenv("GITLAB_MR_COMMENT_TOKEN")
1516
ci_project_id = os.getenv("CI_PROJECT_ID")
1617
ci_merge_request_iid = os.getenv("CI_MERGE_REQUEST_IID")
1718

19+
20+
# Fetch the current GitLab MR description
21+
def fetch_merge_request_description():
22+
url = f"{gitlab_api_url}/projects/{ci_project_id}/merge_requests/{ci_merge_request_iid}"
23+
headers = {"PRIVATE-TOKEN": gitlab_token}
24+
response = requests.get(url, headers=headers)
25+
response.raise_for_status()
26+
return response.json().get("description", "")
27+
28+
# Update the GitLab MR description
29+
def update_merge_request_description(updated_description):
30+
url = f"{gitlab_api_url}/projects/{ci_project_id}/merge_requests/{ci_merge_request_iid}"
31+
headers = {"PRIVATE-TOKEN": gitlab_token}
32+
data = {"description": updated_description}
33+
response = requests.put(url, headers=headers, json=data)
34+
response.raise_for_status()
35+
print("Successfully updated the MR description.")
36+
37+
def update_memory_results_title(description):
38+
header_start = "<!-- START: Memory Header -->"
39+
header_end = "<!-- END: Memory Header -->"
40+
if header_start in description and header_end in description:
41+
return description # Return as is if header already exists
42+
43+
header_section_content = "#### Gitlab CI Memory Numbers (Do Not Edit) \n"
44+
header_section = f"{header_start}\n{header_section_content}{header_end}"
45+
46+
updated_description = description.strip() + "\n\n" + header_section
47+
return updated_description
48+
49+
# Updates the memory results section
50+
def update_memory_results_section(description, chip_name, example, output):
51+
marker_start = f"<!-- START: Memory Results for {chip_name} -->"
52+
marker_end = f"<!-- END: Memory Results for {chip_name} -->"
53+
54+
chip_section_content = (
55+
f"<details open><summary><b>Static Memory Footprint for target: {chip_name}, example: {example}</b></summary>\n\n"
56+
f"```{output}```\n"
57+
f"</details>\n"
58+
)
59+
60+
chip_section = f"{marker_start}\n{chip_section_content}{marker_end}"
61+
62+
if marker_start in description and marker_end in description:
63+
updated_description = re.sub(
64+
rf"{re.escape(marker_start)}.*?{re.escape(marker_end)}",
65+
chip_section,
66+
description,
67+
flags=re.DOTALL,
68+
)
69+
else:
70+
updated_description = description.strip() + "\n\n" + chip_section
71+
72+
return updated_description
73+
1874
# Fetch the id of the pipeline for a branch with the specified commit id (default main branch)
1975
def fetch_pipeline_for_commit(commit_sha, branch_name="main"):
2076
url = f"{gitlab_api_url}/projects/{ci_project_id}/pipelines"
@@ -75,22 +131,6 @@ def execute_idf_size_command(old_file_path, new_file_path):
75131
except subprocess.CalledProcessError as e:
76132
raise
77133

78-
# Post the results to gitlab MR.
79-
def post_results_to_gitlab_mr(output, chip_name, example):
80-
if not all([gitlab_api_url, gitlab_token, ci_project_id, ci_merge_request_iid]):
81-
print("Missing required environment variables. Results not posted.")
82-
return
83-
84-
markdown_output = f"<details open><summary><b>Static Memory Footprint for target: {chip_name}, example: {example}</b></summary>\n\n```\n{output}\n```\n</details>"
85-
url = f"{gitlab_api_url}/projects/{ci_project_id}/merge_requests/{ci_merge_request_iid}/notes"
86-
headers = {"PRIVATE-TOKEN": gitlab_token}
87-
data = {"body": markdown_output}
88-
response = requests.post(url, headers=headers, json=data)
89-
if response.status_code == 201:
90-
print("Successfully posted results to GitLab MR.")
91-
else:
92-
print("Failed to post results to GitLab MR.")
93-
94134
def main():
95135

96136
logging.basicConfig(level=logging.WARNING, format="%(asctime)s - %(levelname)s - %(message)s")
@@ -117,7 +157,15 @@ def main():
117157
download_ref_map_file(args.chip, target_job_id, args.ref_map_file)
118158

119159
size_diff_output = execute_idf_size_command(args.ref_map_file, current_map_file)
120-
post_results_to_gitlab_mr(size_diff_output, args.chip, args.example)
160+
161+
current_description_without_title = fetch_merge_request_description()
162+
updated_title = update_memory_results_title(current_description_without_title)
163+
update_merge_request_description(updated_title)
164+
current_description = fetch_merge_request_description()
165+
updated_description = update_memory_results_section(
166+
current_description, args.chip, args.example, size_diff_output
167+
)
168+
update_merge_request_description(updated_description)
121169
except FileNotFoundError as e:
122170
logging.error(f"Error occurred while posting results to GitLab MR: File not found {e}")
123171
except Exception as e:

0 commit comments

Comments
 (0)