更新目录结构和提交历史 #45
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 "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 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=short) | |
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 "```" | |
print commit_history | |
print "```" | |
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 "```" | |
print commit_history | |
print "```" | |
} | |
} | |
' README.md > "$TEMP_FILE" | |
mv "$TEMP_FILE" README.md | |
- name: Create or Update Pull Request | |
run: | | |
BRANCH_NAME="auto-update-directory-tree-and-history" | |
# 检查分支是否存在,如果不存在则创建 | |
if ! git ls-remote --exit-code --heads origin $BRANCH_NAME; then | |
git checkout -b $BRANCH_NAME | |
else | |
git checkout $BRANCH_NAME | |
git pull origin $BRANCH_NAME | |
git reset --hard origin/master | |
fi | |
# 检查是否有更改 | |
if [[ -n "$(git status --porcelain)" ]]; then | |
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 -f origin $BRANCH_NAME | |
# 检查PR是否存在 | |
PR_EXISTS=$(gh pr list --base master --head $BRANCH_NAME --json number --jq length) | |
if [ "$PR_EXISTS" -eq "0" ]; then | |
# 创建新的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 | |
# 更新现有的PR | |
gh pr edit \ | |
--title "docs: 自动更新目录结构和提交历史" \ | |
--body "自动更新 README.md 中的目录结构和提交历史 | |
- 更新时间:$(TZ='Asia/Shanghai' date '+%Y年%m月%d日 %H:%M:%S') | |
- 目录数量:$((DIR_COUNT-1)) | |
- 文件数量:$FILE_COUNT | |
- 由 GitHub Actions 自动创建" \ | |
$BRANCH_NAME | |
fi | |
else | |
echo "No changes to commit" | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} |