forked from kylin-x-kernel/StarryOS
-
Notifications
You must be signed in to change notification settings - Fork 0
226 lines (190 loc) · 8.06 KB
/
code-stats.yml
File metadata and controls
226 lines (190 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
name: Code Statistics
on:
workflow_dispatch: # 手动触发
inputs:
output_branch:
description: '输出结果的分支名称(默认:code-stats-results)'
required: false
default: 'code-stats-results'
schedule:
# 每天凌晨 2 点执行
- cron: '0 2 * * *'
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
code-stats:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout StarryOS
uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: Checkout test harness
uses: actions/checkout@v4
with:
repository: kylin-x-kernel/starry-test-harness
path: starry-test-harness
- name: Install dependencies
run: |
# 安装 tokei
cargo install --locked tokei
# 安装 yq
sudo wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
sudo chmod +x /usr/local/bin/yq
# 验证安装
tokei --version
yq --version
- name: Clone whitelist repositories
run: |
# 白名单仓库会克隆到 local_crates/ 目录
# 格式: 分支名|仓库URL|目录名
cat > repos.txt << 'EOF'
master|https://github.com/kylin-x-kernel/starry-ptrace|starry-ptrace
master|https://github.com/kylin-x-kernel/starry-test-harness|starry-test-harness
master|https://gitee.com/openkylin/rust-libutee|rust-libutee
master|https://gitee.com/openkylin/microkylin_manager|microkylin_manager
main|https://github.com/kylin-x-kernel/meta-starry.git|meta-starry
main|https://github.com/kylin-x-kernel/aarch64-pmuv3.git|aarch64-pmuv3
dev|https://github.com/kylin-x-kernel/page_table_multiarch.git|page_table_multiarch
EOF
# 添加更多仓库示例:
# main|https://github.com/org/repo|repo
# 批量克隆到 local_crates
while IFS='|' read -r branch repo name; do
[[ -z "$branch" ]] && continue
target="local_crates/$name"
if [[ -d "$target" ]]; then
echo "[skip] $target already exists"
else
echo "[clone] $repo ($branch) → $target"
git clone -b "$branch" --depth 1 "$repo" "$target"
fi
done < repos.txt
- name: Run code statistics
run: |
# 统计主仓库(排除 local_crates)和 local_crates 下所有子目录
python3 starry-test-harness/scripts/generate_loc_report.py \
--workspace "${{ github.workspace }}" \
--clone-dir local_crates \
--output ./code-stats
- name: Generate timestamp
id: timestamp
run: echo "date=$(date +'%Y%m%d-%H%M%S')" >> $GITHUB_OUTPUT
- name: Upload statistics as artifact
uses: actions/upload-artifact@v4
with:
name: code-statistics-${{ steps.timestamp.outputs.date }}
path: |
code-stats/loc.json
code-stats/loc.md
retention-days: 90
- name: Commit and push to results branch
env:
OUTPUT_BRANCH: ${{ github.event.inputs.output_branch || 'code-stats-results' }}
TIMESTAMP: ${{ steps.timestamp.outputs.date }}
run: |
# 配置 git
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# 备份统计结果到临时目录
mkdir -p /tmp/code-stats-backup
cp -r code-stats/* /tmp/code-stats-backup/
# 创建或切换到结果分支
git fetch origin ${OUTPUT_BRANCH} || true
# 检查分支是否存在
if git show-ref --verify --quiet refs/remotes/origin/${OUTPUT_BRANCH}; then
# 分支存在,切换
git checkout ${OUTPUT_BRANCH}
# 检查是否有非 reports 的文件/目录(说明分支被污染了)
shopt -s dotglob nullglob
needs_cleanup=false
for item in *; do
if [ "$item" != ".git" ] && [ "$item" != "reports" ]; then
needs_cleanup=true
break
fi
done
if [ "$needs_cleanup" = true ]; then
echo "Branch contains non-report files. Performing cleanup..."
# 备份旧的 reports 目录(如果存在)
if [ -d "reports" ]; then
echo "Backing up old reports..."
cp -r reports /tmp/old-reports-backup
fi
# 清理工作区(保留 .git)
for item in *; do
if [ "$item" != ".git" ]; then
rm -rf "$item"
fi
done
# 清空 git 索引
git rm -rf --cached . 2>/dev/null || true
# 恢复旧的 reports 目录
if [ -d "/tmp/old-reports-backup" ]; then
echo "Restoring old reports..."
cp -r /tmp/old-reports-backup reports
fi
else
echo "Branch is clean. Skipping cleanup."
fi
else
# 分支不存在,创建孤立分支
git checkout --orphan ${OUTPUT_BRANCH}
# 清空暂存区
git rm -rf . 2>/dev/null || true
fi
# 确保 reports 目录存在
mkdir -p reports/${TIMESTAMP}
# 从临时目录复制统计结果
cp /tmp/code-stats-backup/loc.json reports/${TIMESTAMP}/
cp /tmp/code-stats-backup/loc.md reports/${TIMESTAMP}/
cp /tmp/code-stats-backup/loc.json reports/latest.json
cp /tmp/code-stats-backup/loc.md reports/latest.md
# 创建索引文件
cat > reports/README.md << 'EOF'
# StarryOS 代码统计历史报告
## 最新报告
- [最新统计 (JSON)](./latest.json)
- [最新统计 (Markdown)](./latest.md)
## 历史报告
EOF
# 列出所有历史报告
for dir in reports/*/; do
if [ -d "$dir" ]; then
dirname=$(basename "$dir")
echo "- [\`${dirname}\`](./${dirname}/loc.md)" >> reports/README.md
fi
done
# 添加元信息
cat > reports/latest-info.json << EOF
{
"timestamp": "${TIMESTAMP}",
"commit": "${{ github.sha }}",
"branch": "${{ github.ref_name }}",
"workflow_run": "${{ github.run_id }}"
}
EOF
# 提交更改
git add reports/
git commit -m " 代码统计报告 - ${TIMESTAMP}" || echo "No changes to commit"
# 推送到远程
git push -f origin ${OUTPUT_BRANCH}
echo " 统计结果已推送到分支: ${OUTPUT_BRANCH}"
echo " 查看报告: https://github.com/${{ github.repository }}/tree/${OUTPUT_BRANCH}/reports"
- name: Display statistics summary
if: always()
run: |
echo "## 代码统计摘要" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ -f code-stats/loc.md ]; then
cat code-stats/loc.md >> $GITHUB_STEP_SUMMARY
else
echo "统计文件未生成" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "---" >> $GITHUB_STEP_SUMMARY
echo " 统计结果已保存到分支: \`${{ github.event.inputs.output_branch || 'code-stats-results' }}\`" >> $GITHUB_STEP_SUMMARY
echo " [查看历史报告](https://github.com/${{ github.repository }}/tree/${{ github.event.inputs.output_branch || 'code-stats-results' }}/reports)" >> $GITHUB_STEP_SUMMARY