更新目录结构和提交历史 #106
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: 更新目录结构和提交历史 | |
- 更新时间:$(TZ='Asia/Shanghai' date '+%Y年%m月%d日 %H:%M:%S') | |
- 目录数量:$((DIR_COUNT-1)) | |
- 文件数量:$FILE_COUNT | |
- 由 GitHub Actions 自动创建" | |
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 }} |