8
8
import glob
9
9
import argparse
10
10
import logging
11
+ import re
11
12
12
13
# Gitlab Configurations
13
14
gitlab_api_url = os .getenv ("CI_API_V4_URL" )
14
15
gitlab_token = os .getenv ("GITLAB_MR_COMMENT_TOKEN" )
15
16
ci_project_id = os .getenv ("CI_PROJECT_ID" )
16
17
ci_merge_request_iid = os .getenv ("CI_MERGE_REQUEST_IID" )
17
18
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
+
18
74
# Fetch the id of the pipeline for a branch with the specified commit id (default main branch)
19
75
def fetch_pipeline_for_commit (commit_sha , branch_name = "main" ):
20
76
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):
75
131
except subprocess .CalledProcessError as e :
76
132
raise
77
133
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
-
94
134
def main ():
95
135
96
136
logging .basicConfig (level = logging .WARNING , format = "%(asctime)s - %(levelname)s - %(message)s" )
@@ -117,7 +157,15 @@ def main():
117
157
download_ref_map_file (args .chip , target_job_id , args .ref_map_file )
118
158
119
159
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 )
121
169
except FileNotFoundError as e :
122
170
logging .error (f"Error occurred while posting results to GitLab MR: File not found { e } " )
123
171
except Exception as e :
0 commit comments