Skip to content

Commit adab03e

Browse files
committed
ci: Replace the judgment file modification mechanism of bsp_building.yml and incorporate the PR status show into bsp_building.yml
1 parent d64ce33 commit adab03e

3 files changed

Lines changed: 142 additions & 121 deletions

File tree

.github/workflows/bsp_buildings.yml

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -41,66 +41,29 @@ permissions:
4141
jobs:
4242
generate-matrix:
4343
runs-on: ubuntu-22.04
44+
name: Generate Matrix
4445
outputs:
4546
filtered_matrix: ${{ steps.filter.outputs.filtered_matrix }}
4647
steps:
4748
- name: Checkout repo
4849
uses: actions/checkout@main
4950
with:
50-
sparse-checkout: .github/ALL_BSP_COMPILE.json
5151
persist-credentials: false
52-
- name: Read matrix config
53-
id: read-config
54-
run: |
55-
#读取ALL_BSP_COMPILE.json文件
56-
raw_matrix_base64=$(cat .github/ALL_BSP_COMPILE.json |egrep -v '^//'|base64 -w 0)
57-
echo "raw_matrix=$raw_matrix_base64" >> $GITHUB_OUTPUT
58-
- name: Get changed files
59-
id: changed_files
60-
if: github.event_name == 'pull_request'
61-
run: |
62-
changed_files=$(curl -s \
63-
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files" | \
64-
jq -r '.[].filename')
65-
prefix=$(echo "$changed_files" | cut -d'/' -f1 | sort -u )
66-
has_bsp=true
67-
for r in "$prefix"; do
68-
if [[ ! "$r" == "bsp" ]]; then
69-
has_bsp=false
70-
break
71-
fi
72-
done
73-
if [[ $has_bsp == true ]]; then
74-
changed_files=$(echo "${changed_files}"| cut -d'/' -f2 | sort -u| tr '\n' ',' | sed 's/,$//')
75-
echo "CHANGED_FILES=[${changed_files}]" >> $GITHUB_OUTPUT
76-
fi
77-
#获取修改文件的路径,如果有bsp以外的文件夹则编译全部bsp,否则获取对应文件夹名
78-
79-
echo "${changed_files}"
52+
fetch-depth: 0
8053
- name: Filter matrix
8154
id: filter
8255
run: |
83-
raw_matrix=$(echo "${{ steps.read-config.outputs.raw_matrix }}" | base64 --decode)
84-
85-
CHANGED_FILES=${{ steps.changed_files.outputs.CHANGED_FILES }}
86-
CHANGED_FILES=$(echo "$CHANGED_FILES" | sed 's/\[\|\]//g')
87-
# 将修改的文件路径与ALL_BSP_COMPILE.json文件的SUB_RTT_BSP进行判断,判断是否包含
88-
filtered_matrix=[]
89-
echo "${CHANGED_FILES}"
90-
if [[ -z "$CHANGED_FILES" ]]; then
91-
FILTER_CONDITION='.legs[]'
92-
filtered_matrix=$(jq -c "{legs: [$FILTER_CONDITION]}" <<< "$raw_matrix")
93-
else
94-
CONDITIONS=$(echo "$CHANGED_FILES" | awk 'BEGIN { RS="," } { printf "contains(\"%s\") or ", $1 }')
95-
CONDITIONS=${CONDITIONS% or }
96-
97-
FILTER_CONDITION=".legs[] | select(any(.SUB_RTT_BSP[]; $CONDITIONS))"
98-
99-
filtered_matrix=$(jq -c "{legs: [$FILTER_CONDITION]}" <<< "$raw_matrix")
100-
fi
56+
git config --global http.postBuffer 524288000
57+
git remote -v
58+
git fetch origin
59+
git branch
60+
git branch -a
61+
python tools/ci/git_diff_show.py origin/master
62+
raw_matrix=$(cat .github/ALL_BSP_COMPILE_TEMP.json)
10163
64+
FILTER_CONDITION='.legs[]'
65+
filtered_matrix=$(jq -c "{legs: [$FILTER_CONDITION]}" <<< "$raw_matrix")
10266
echo "filtered_matrix=${filtered_matrix}" >> $GITHUB_OUTPUT
103-
10467
echo ${filtered_matrix}
10568
build:
10669
runs-on: ubuntu-22.04

.github/workflows/git_diff_status_show.yml

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 131 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
# Change Logs:
77
# Date Author Notes
88
# 2025-05-15 supperthomas add PR status show
9-
#
10-
#!/usr/bin/env python3
9+
# 2025-05-22 hydevcode 替换bsp_building.yml的判断文件修改机制,并将PR status show合并进bsp_building.yml
1110
import subprocess
1211
import sys
1312
import os
@@ -16,6 +15,8 @@
1615
import locale
1716
from typing import List, Dict
1817

18+
import json
19+
from typing import List
1920
class FileDiff:
2021
def __init__(self, path: str, status: str, size_change: int = 0, old_size: int = 0, new_size: int = 0):
2122
self.path = path
@@ -48,7 +49,7 @@ def get_diff_files(self) -> List[FileDiff]:
4849
sys.exit(1)
4950

5051
# 获取差异文件列表
51-
diff_cmd = f"git diff --name-status {merge_base} "
52+
diff_cmd = f"git diff --name-status {merge_base} HEAD"
5253
print(diff_cmd)
5354
try:
5455
output = subprocess.check_output(diff_cmd.split(), stderr=subprocess.STDOUT)
@@ -139,22 +140,128 @@ def format_size(size: int) -> str:
139140
else:
140141
return f"{size / (1024 * 1024 * 1024):.1f} GB"
141142

143+
def is_bsp(path):
144+
return os.path.isfile(os.path.join(path, "rtconfig.h"))
145+
146+
def filter_bsp_config(file_diffs: List[FileDiff], config_path: str):
147+
# 读取原始JSON配置
148+
with open(config_path, 'r', encoding='utf-8') as f:
149+
config = json.load(f)
150+
151+
# 获取所有修改的文件路径(统一使用Linux风格路径)
152+
modified_paths = [diff.path.replace('\\', '/') for diff in file_diffs]
153+
print(modified_paths)
154+
if not modified_paths:
155+
print("master分支运行")
156+
return
157+
158+
bsp_paths = set()
159+
bsp_in_but_not_bsp_paths = set()
160+
all_print_paths = set()
161+
for modified_path in modified_paths:
162+
parts = modified_path.strip(os.sep).split('/')
163+
if not parts:
164+
continue
165+
first_level = parts[0]
166+
first_level_path = os.path.join(os.getcwd(), first_level)
167+
168+
#处理bsp路径的逻辑
169+
if first_level == "bsp":
170+
temp_path=os.path.join(os.getcwd(), modified_path)
171+
# 判断是否是文件
172+
if not os.path.isdir(modified_path):
173+
temp_path = os.path.dirname(temp_path)
174+
175+
#循环搜索每一级是否有rtconfig.h
176+
while temp_path !=first_level_path:
177+
if is_bsp(temp_path):
178+
bsp_paths.add(temp_path)
179+
180+
break
181+
temp_path=os.path.dirname(temp_path)
182+
183+
if temp_path ==first_level_path:
184+
bsp_in_but_not_bsp_paths.add(parts[1])
185+
186+
else:
187+
#非bsp路径逻辑
188+
all_print_paths.add(first_level_path)
189+
190+
# 变成相对路径
191+
bsp_list = set()
192+
for path in sorted(bsp_paths):
193+
current_working_dir = os.path.join(os.getcwd(), "bsp/")
194+
if path.startswith(current_working_dir):
195+
bsp_list.add(path[len(current_working_dir):].lstrip(os.sep))
196+
else:
197+
bsp_list.add(path) # 如果 first_level_path 不以 current_working_dir 开头,则保持不变
198+
199+
# 处理修改了bsp外的文件的情况
200+
filtered_bsp = [
201+
path for path in bsp_list
202+
if path.split('/')[0] not in bsp_in_but_not_bsp_paths
203+
]
204+
205+
merged_result = filtered_bsp + list(bsp_in_but_not_bsp_paths)
206+
207+
filtered_legs = []
208+
for leg in config["legs"]:
209+
matched_paths = [
210+
path for path in leg.get("SUB_RTT_BSP", [])
211+
if any(keyword in path for keyword in merged_result)
212+
]
213+
if matched_paths:
214+
filtered_legs.append({**leg, "SUB_RTT_BSP": matched_paths})
215+
216+
# 生成新的配置
217+
new_config = {"legs": filtered_legs}
218+
219+
# 判断有没有修改到bsp外的文件,有的话则编译全部
220+
if not all_print_paths:
221+
print(new_config)
222+
file_name = ".github/ALL_BSP_COMPILE_TEMP.json"
223+
224+
# 将 new_config 写入文件
225+
with open(file_name, "w", encoding="utf-8") as file:
226+
json.dump(new_config, file, ensure_ascii=False, indent=4)
227+
228+
142229
def main():
230+
# 源文件路径
231+
source_file = ".github/ALL_BSP_COMPILE.json" # 替换为你的文件路径
232+
233+
# 目标文件路径
234+
target_file = ".github/ALL_BSP_COMPILE_TEMP.json" # 替换为你的目标文件路径
235+
236+
# 读取源文件并过滤掉带有 // 的行
237+
with open(source_file, "r", encoding="utf-8") as infile, open(target_file, "w", encoding="utf-8") as outfile:
238+
for line in infile:
239+
if not line.lstrip().startswith("//"):
240+
outfile.write(line)
241+
143242
parser = argparse.ArgumentParser(description='Compare current branch with target branch and show file differences.')
144243
parser.add_argument('target_branch', help='Target branch to compare against (e.g., master)')
145244
args = parser.parse_args()
146245

147246
analyzer = GitDiffAnalyzer(args.target_branch)
148247
file_diffs = analyzer.get_diff_files()
149-
248+
150249
# 生成报告
151250
generate_report(file_diffs, args.target_branch)
251+
252+
filter_bsp_config(file_diffs,".github/ALL_BSP_COMPILE_TEMP.json")
253+
152254

255+
def add_summary(text):
256+
"""
257+
add summary to github action.
258+
"""
259+
os.system(f'echo "{text}" >> $GITHUB_STEP_SUMMARY ;')
153260

154-
155261
def generate_report(file_diffs: List[FileDiff], target_branch: str):
156262
"""生成差异报告"""
157-
print(f"\n=== Comparison between {target_branch} and current branch ===\n")
263+
264+
add_summary(f"\n=== Comparison between {target_branch} and current branch ===\n")
158265

159266
# 分类统计
160267
added_files = [f for f in file_diffs if f.status == 'A']
@@ -169,33 +276,34 @@ def generate_report(file_diffs: List[FileDiff], target_branch: str):
169276
# 计算总的大小变化
170277
total_size_change = sum(f.size_change for f in file_diffs)
171278

172-
print(f"Total changes: {len(file_diffs)} files")
173-
print(f"Added: {len(added_files)} files ({format_size(total_added)})")
174-
print(f"Removed: {len(removed_files)} files ({format_size(total_removed)})")
175-
print(f"Modified: {len(modified_files)} files ({format_size(total_modified)})")
176-
print(f"Renamed: {len(renamed_files)} files")
177-
print(f"\nTotal size change: {format_size(total_size_change)}")
178-
print("\n" + "="*60 + "\n")
279+
280+
add_summary(f" Total changes: {len(file_diffs)} files")
281+
add_summary(f" Added: {len(added_files)} files ({format_size(total_added)})")
282+
add_summary(f" Removed: {len(removed_files)} files ({format_size(total_removed)})")
283+
add_summary(f" Modified: {len(modified_files)} files ({format_size(total_modified)})")
284+
add_summary(f" Renamed: {len(renamed_files)} files")
285+
add_summary(f"\n Total size change: {format_size(total_size_change)}")
286+
add_summary("\n" + "="*60 + "\n")
179287

180288
# 显示详细差异
181289
for diff in file_diffs:
182-
print(diff)
290+
add_summary(diff)
183291

184292
# 详细展示修改和新增的文件大小
185293
if diff.status == 'A':
186-
print(f" Size: {format_size(diff.new_size)}")
294+
add_summary(f"Size: {format_size(diff.new_size)}")
187295
elif diff.status == 'M':
188-
print(f" Original size: {format_size(diff.old_size)}")
189-
print(f" New size: {format_size(diff.new_size)}")
190-
print(f" Size change: {format_size(abs(diff.size_change))}")
296+
add_summary(f"Original size: {format_size(diff.old_size)}")
297+
add_summary(f"New size: {format_size(diff.new_size)}")
298+
add_summary(f"Size change: {format_size(abs(diff.size_change))}")
191299
elif diff.status == 'R':
192-
print(f" Original size: {format_size(diff.old_size)}")
193-
print(f" New size: {format_size(diff.new_size)}")
194-
print(f" Size change: {format_size(abs(diff.size_change))}")
300+
add_summary(f"Original size: {format_size(diff.old_size)}")
301+
add_summary(f"New size: {format_size(diff.new_size)}")
302+
add_summary(f"Size change: {format_size(abs(diff.size_change))}")
195303
elif diff.status == 'D':
196-
print(f" Original size: {format_size(diff.old_size)}")
304+
add_summary(f"Original size: {format_size(diff.old_size)}")
197305

198-
print("-" * 50)
306+
add_summary("-" * 50)
199307

200308
if __name__ == "__main__":
201309
main()

0 commit comments

Comments
 (0)