Skip to content

更新目录结构和提交历史 #41

更新目录结构和提交历史

更新目录结构和提交历史 #41

name: 更新目录结构
on:
schedule:
- cron: '0 0 * * *' # 每天 UTC 00:00 运行
workflow_dispatch: # 允许手动触发
#push:
# branches:
# - master # 监听 master 分支上的推送事件
jobs:
update-directory-tree:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
token: ${{ secrets.PAT_TOKEN }} # 使用 PAT 进行 checkout
fetch-depth: 1
- name: Setup Git
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
- name: Install tree
run: sudo apt-get install -y tree
- name: Update Directory Structure
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)
# 创建临时文件
TEMP_FILE=$(mktemp)
# 处理 README.md
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" '
BEGIN {
in_tree = 0;
found_tree = 0;
}
# 当找到目录结构标题时
/^## 目录结构/ {
if (!found_tree) {
print $0; # 打印标题
print "最后更新时间:" date;
print "";
print "```";
# 移除tree输出中的最后一行(统计信息)
sub(/\n[0-9]+ directories.*$/, "", tree)
print tree;
print "";
print dir_count " directories, " file_count " files";
print "```";
found_tree = 1;
in_tree = 1;
next;
}
}
# 当在目录结构部分时,跳过直到找到下一个标题或文件结束
in_tree && /^##/ {
in_tree = 0;
print $0;
next;
}
# 跳过目录结构部分的内容
in_tree { next }
# 打印其他所有内容
{ print $0 }
# 如果没有找到目录结构部分,在文件末尾添加
END {
if (!found_tree) {
print "\n## 目录结构";
print "最后更新时间:" date;
print "";
print "```";
# 移除tree输出中的最后一行(统计信息)
sub(/\n[0-9]+ directories.*$/, "", tree)
print tree;
print "";
print dir_count " directories, " file_count " files";
print "```";
}
}
' README.md > "$TEMP_FILE"
# 替换原文件
mv "$TEMP_FILE" README.md
- name: Create Pull Request
run: |
# 检查是否有更改
if [[ -n "$(git status --porcelain)" ]]; then
# 获取当前时间作为分支名
BRANCH_NAME="directory-tree-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"
# 创建 PR
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 }}