-
Notifications
You must be signed in to change notification settings - Fork 0
134 lines (119 loc) · 4.51 KB
/
update-directory-tree.yml
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
name: 更新目录结构和提交历史
on:
schedule:
- cron: '0 0 * * *' # 每天 UTC 00:00 运行
workflow_dispatch: # 允许手动触发
jobs:
update-directory-tree-and-commit-history:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
token: ${{ secrets.PAT_TOKEN }}
fetch-depth: 0 # 获取所有历史记录
- name: Setup Git
run: |
git config --global user.email "[email protected]"
git config --global user.name "Zengwenliang0416"
- name: Install tree
run: sudo apt-get install -y tree
- name: Update Directory Structure and Commit History
run: |
TREE_OUTPUT=$(tree -L 2 -I '.git|.github|node_modules' --charset=utf8)
DIR_COUNT=$(find . -type d -not -path '*/\.*' -not -path './node_modules*' | wc -l)
FILE_COUNT=$(find . -type f -not -path '*/\.*' -not -path './node_modules*' | wc -l)
# 优化提交历史格式
COMMIT_HISTORY=$(git log -n 10 --pretty=format:"%h|%s|%an|%ad" --date=format:"%Y-%m-%d %H:%M" | \
awk -F'|' '{printf "| %s | %.60s | %s | %s |\n", $1, $2, $3, $4}')
TEMP_FILE=$(mktemp)
awk -v tree="$TREE_OUTPUT" -v date="$(TZ='Asia/Shanghai' date '+%Y年%m月%d日 %H:%M:%S')" \
-v dir_count="$((DIR_COUNT-1))" -v file_count="$FILE_COUNT" \
-v commit_history="$COMMIT_HISTORY" '
BEGIN {
in_tree = 0; found_tree = 0;
in_history = 0; found_history = 0;
}
/^## 目录结构/ {
if (!found_tree) {
print $0
print "最后更新时间:" date
print ""
print "```"
sub(/\n[0-9]+ directories.*$/, "", tree)
print tree
print ""
print dir_count " directories, " file_count " files"
print "```"
found_tree = 1
in_tree = 1
next
}
}
/^## 最近提交/ {
if (!found_history) {
print $0
print ""
print "| Commit | Description | Author | Date |"
print "|--------|-------------|--------|------|"
print commit_history
found_history = 1
in_history = 1
next
}
}
(in_tree || in_history) && /^##/ {
in_tree = 0
in_history = 0
print $0
next
}
in_tree || in_history { next }
{ print $0 }
END {
if (!found_tree) {
print "\n## 目录结构"
print "最后更新时间:" date
print ""
print "```"
sub(/\n[0-9]+ directories.*$/, "", tree)
print tree
print ""
print dir_count " directories, " file_count " files"
print "```"
}
if (!found_history) {
print "\n## 最近提交"
print ""
print "| Commit | Description | Author | Date |"
print "|--------|-------------|--------|------|"
print commit_history
}
}
' README.md > "$TEMP_FILE"
mv "$TEMP_FILE" README.md
- name: Create Pull Request
run: |
if [[ -n "$(git status --porcelain)" ]]; then
BRANCH_NAME="directory-tree-and-history-updates-$(TZ='Asia/Shanghai' date +%Y%m%d-%H%M%S)"
git checkout -b "$BRANCH_NAME"
git add README.md
git commit -m "docs: 📝 更新目录结构和提交历史"
git push origin "$BRANCH_NAME"
gh pr create \
--title "docs: 📝 自动更新目录结构和提交历史" \
--body "自动更新 README.md 中的目录结构和提交历史
- 更新时间:$(TZ='Asia/Shanghai' date '+%Y年%m月%d日 %H:%M:%S')
- 目录数量:$((DIR_COUNT-1))
- 文件数量:$FILE_COUNT
- 由 GitHub Actions 自动创建" \
--base master \
--head "$BRANCH_NAME"
else
echo "No changes to commit"
fi
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}