Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sync): Sitemap Creator 1.0.4 #10

Merged
merged 14 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ updates:
- package-ecosystem: "npm" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
interval: "daily"
- package-ecosystem: "github-actions" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
interval: "daily"
35 changes: 28 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
> 这是 Sitemap Creator 的稳定版仓库。预发行版仓库请前往 [fjwxzde/Sitemap_Creator_Pre-Release](https://github.com/fjwxzde/Sitemap_Creator_Pre-Release) 查看。

[![GitHub Release](https://img.shields.io/github/release/DuckDuckStudio/Sitemap_Creator?style=flat)](https://github.com/DuckDuckStudio/Sitemap_Creator/releases/latest)
[反馈Bug🐛](https://github.com/DuckDuckStudio/Sitemap_Creator/issues) | [使用示例🚀](#4-使用示例)
[反馈Bug🐛](https://github.com/DuckDuckStudio/Sitemap_Creator/issues) | [使用示例🚀](#6-使用示例)

## 为什么选择 Sitemap Creator 🏆
| | Sitemap | Creator | |
|-----|-----|----|----|
| 无需本地操作 | ✅ | 稳定更新 | ✅ |
| 完全免费 | ✅ | 修改时区 | ✅ |
| 完全免费 | ✅ | 修改时区<sup>[5](#5-设置时区)</sup> | ✅ |
| 指定更新/创建方式 | ✅ | 不遗漏页面 | ✅ |
| 忽略页面 | ✅ | 指定网站地图存放位置 | ✅ |
| 指定页面文件类型 | ✅ | 中文文档+输出 | ✅ |
Expand All @@ -26,14 +26,16 @@
|-----|-----|-----|-----|-----|
| `location` | 网站地图的存放位置 (例如 `docs/sitemap.xml`) | `./sitemap.xml` (即仓库根目录) | 否 | / |
| `token` | 用于创建更新网站地图的拉取请求的 Token | `${{ github.token }}` | 否 | 您的 Token 至少应该具有 `repo` 权限来推送修改,如果使用默认的 Action Token 则需要在仓库设置中给 GitHub Action 写入权限<sup>[1](#1-如何允许-github-action-创建拉取请求--推送修改)</sup> |
| `timezone` | 设置生成时使用的时区 | `Asia/Shanghai` (上海,UTF+8,CST) | 否 | 遵循 IANA时区数据库(也称为Olson时区数据库)的格式 |
| `timezone` | 设置生成时使用的时区 | `Asia/Shanghai` (上海,UTC+8,CST,Ubuntu/Macos格式) | 否 | 请依据您的 Runner 设置该参数<sup>[5](#5-设置时区)</sup> |
| `basic_link` | 指向你网站的基础链接 | `https://${{ github.event.repository.owner.login }}.github.io/${{ github.event.repository.name }}` | 否 | 结尾不要带 `/` |
| `file_type` | 网页文件的类型 (例如使用 docsify 部署的就是 md,可指定多个类型) | `html,md` | 否 | 不带`.`,`md`类型会自动去掉后缀名 |
| `ignore_file` | 指定哪些文件不包含在网站地图中 | `啥都没有` | 否 | `,`间隔 |
| `website_path` | 你的网站内容的位置 (例如 `./` (根目录) 或 `docs`) | `./` (根目录) | **是** | / |
| `base_branch` | 仓库主分支 (`main`,`master` 等) | `main` | 否 | / |
| `label` | 创建拉取请求时添加的标签 | / | 否 | 会自动移除`'`、`"`、<code>\`</code>,可以设置`debug: true`来查看运行情况,标签间用`,`分隔 |
| `reviewer` | 创建拉取请求时指定的审查者 | / | 否 | 会自动鉴权,如果指定的审查者不是仓库的协作者则无法添加 |
| `author_name` | 更新提交的撰写者名 | `github-actions[bot]` | 否 | 这里指定的是提交的撰写者的名称,不是拉取请求的创建者的名称。拉取请求的创建者为 Token 所有者 |
| `author_email` | 更新提交的撰写者邮箱 | `41898282+github-actions[bot]@users.noreply.github.com` (不知道从哪找来的 GitHub Action [bot] 的邮箱) | 否 | 这里指定的是提交的撰写者的邮箱,不是拉取请求的创建者的邮箱。拉取请求的创建者为 Token 所有者 |
| `auto_merge` | 启用自动合并的方式 (不指定则不启用自动合并) | / | 否 | [可用的自动合并方式](#3-可用的自动合并方式),[什么是自动合并](https://docs.github.com/zh/pull-requests/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/automatically-merging-a-pull-request) |
| `update` | 指定更新网站地图的方式 (直接提交或拉取请求) | `拉取请求` | 否 | [可用的参数值](#4-可用的修改网站地图的方式) |
| `debug` | 控制调试输出的开关 | `false` | 否 | 你用`true`还是`1`随便,js里真值<sup>[2](#2-java-script-中有哪些可用真值)</sup>的都行 |
Expand All @@ -42,7 +44,15 @@
### 1. 如何允许 GitHub Action 创建拉取请求 / 推送修改
打开仓库 Settings (上方栏) > Code and automation (左侧栏) > Actions (左侧栏子类别) > General (子类别) > Workflow permissions (划到最下面):

![记得按 Save 保存](docs/imgs/README/1.png)
![记得按 Save 保存](docs/imgs/README/1.png)

如果你希望进行更精细的访问控制,你可以在你的工作流中添加以下内容:
```yml
# 相关文档: https://docs.github.com/zh/actions/writing-workflows/choosing-what-your-workflow-does/controlling-permissions-for-github_token
permissions:
contents: write # 允许修改仓库内容,例如提交、发行版等
```
(如果需要拉取请求的话**设置中用于创建拉取请求的那个权限还是要勾下**)

### 2. Java Script 中有哪些可用真值
请见[真值 - MDN Web 文档术语表:Web 相关术语的定义 | MDN](https://developer.mozilla.org/zh-CN/docs/Glossary/Truthy)。
Expand All @@ -64,11 +74,22 @@
| `pr`、`pullrequest`、`pullrequests`、`prs`、`拉取请求` | 创建拉取请求 (默认) |
| `commit`、`提交`、`直接提交`、`directcommit`、`commitdirectly` | 直接提交到主分支 |

### 5. 使用示例
### 5. 设置时区
请按照您的工作流使用的 Runner 来设置时区。
#### 查看可用时区
| Runner OS | 查看方式 | 是否支持默认时区 |
|-----|-----|-----|
| Windows | `TZUTIL /l` | ❌ |
| Linux | `timedatectl list-timezones` | ✅ |
| MacOS | `systemsetup -gettimezone` | ✅ |

> 注: Windows 上的时区是一定要指定的,默认的 `Asia/Shanghai` (亚洲/上海) 在 Windows 上不适用,应改用 `China Standard Time` (中国标准时间 CST) 。

### 6. 使用示例
```yml
name: 生成 Sitemap

# GitHub Actiion DuckDuckStudio/Sitemap_Creator 版本 1.0.3 示例工作流
# GitHub Actiion DuckDuckStudio/Sitemap_Creator 版本 1.0.4 示例工作流
# https://github.com/marketplace/actions/sitemap-creator-stable
# Under the [GNU Affero General Public License v3.0](https://github.com/DuckDuckStudio/Sitemap_Creator/blob/main/LICENSE)

Expand All @@ -88,7 +109,7 @@ jobs:

steps:
- name: 更新网站地图
uses: DuckDuckStudio/[email protected].3
uses: DuckDuckStudio/[email protected].4
with:
location: "docs/sitemap.xml"
basic_link: "https://duckduckstudio.github.io/Articles/#" # docsify 部署的
Expand Down
257 changes: 43 additions & 214 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,24 @@ inputs:
required: false
description: 创建拉取请求时指定的审查者

author_name:
required: false
description: 更新提交的撰写者名
default: "github-actions[bot]"

author_email:
required: false
description: 更新提交的撰写者邮箱
default: "41898282+github-actions[bot]@users.noreply.github.com"

runs:
using: composite
steps:
- name: 检出仓库
uses: actions/checkout@v4
with:
fetch-depth: 0 # 检出完整记录
# ref: ${{ inputs.base_branch }} 不确定这个要不要加,后续看情况吧

- name: 设置 Node.js 环境
uses: actions/setup-node@v4
Expand All @@ -95,30 +106,37 @@ runs:

- name: 设置时区
shell: bash
run: sudo timedatectl set-timezone ${{ inputs.timezone }}
if: ${{ runner.os == 'Linux' }}
env:
TZ: ${{ inputs.timezone }}
run: sudo timedatectl set-timezone $TZ

- name: 设置时区
shell: pwsh
if: ${{ runner.os == 'Windows' }}
env:
TZ: ${{ inputs.timezone }}
run: |
tzutil /s $env:TZ

- name: 创建 Sitemap
- name: 设置时区
shell: bash
if: ${{ runner.os == 'macOS' }}
env:
LOCATION: ${{ inputs.location }}
BASIC_LINK: ${{ inputs.basic_link }}
FILE_TYPE: ${{ inputs.file_type }}
IGNORE_FILE: ${{ inputs.ignore_file }}
WEBSITE_PATH: ${{ inputs.website_path }}
DEBUG: ${{ inputs.debug }}
TZ: ${{ inputs.timezone }}
run: sudo systemsetup -settimezone $TZ

- name: 获取生成脚本
shell: bash
run: |
# 获取生成脚本
git clone https://github.com/DuckDuckStudio/Sitemap_Creator -b main # 稳定版
cp Sitemap_Creator/generate-sitemap.mjs Sitemap_Creator.mjs
cp Sitemap_Creator/index.mjs Sitemap_Creator.mjs
rm -r Sitemap_Creator

# 生成网站地图
node Sitemap_Creator.mjs
rm Sitemap_Creator.mjs

- name: 提交并推送 sitemap.xml
- name: 生成网站地图
shell: bash
env:
# 这几乎包含了所有的参数
GH_TOKEN: ${{ inputs.token }}
LABELS: ${{ inputs.label }}
DEBUG: ${{ inputs.debug }}
Expand All @@ -127,203 +145,14 @@ runs:
UPDATE: ${{ inputs.update }}
REVIEWER: ${{ inputs.reviewer }}
TOKEN: ${{ github.token }}
AUTHOR_NAME: ${{ inputs.author_name }}
AUTHOR_EMAIL: ${{ inputs.author_email }}
BASE_BRANCH: ${{ inputs.base_branch }}
# 生成时还需要的参数
BASIC_LINK: ${{ inputs.basic_link }}
FILE_TYPE: ${{ inputs.file_type }}
IGNORE_FILE: ${{ inputs.ignore_file }}
WEBSITE_PATH: ${{ inputs.website_path }}
run: |
# 后面都要用的
# 获取当前日期和时间
DATE_TIME=$(date '+%Y/%m/%d %H:%M')

# 参数处理
# 格式化更新方式 - 默认 PR
UPDATE_WAY=$(echo "$UPDATE" | tr '[:upper:]' '[:lower:]' | sed "s/[\"\'\`-]//g; s/[[:space:]]//g")
# 根据输入值设置对应的更新方式
case "$UPDATE_WAY" in
"pr"|"pullrequest"|"pullrequests"|"prs"|"拉取请求")
UPDATE_WAY="PR"
if [[ "$DEBUG" ]]; then
echo "[DEBUG] 更新方式: 创建拉取请求"
fi

# 如果 AUTO_MERGE 为空字符串,则不做任何操作
if [[ -z "$AUTO_MERGE" ]]; then
if [[ "$DEBUG" ]]; then
echo "[DEBUG] 不启用自动合并,因为自动合并方式为空"
fi
CLEAN_AUTO_MERGE=""
else
# 格式化自动合并方式
CLEAN_AUTO_MERGE=$(echo "$AUTO_MERGE" | tr '[:upper:]' '[:lower:]' | sed "s/[\"\'\`-]//g")

case "$CLEAN_AUTO_MERGE" in
"s"|"squash"|"压缩"|"压缩合并"|"压缩自动合并")
CLEAN_AUTO_MERGE="squash"
;;
"m"|"merge"|"合并"|"合并提交"|"提交")
CLEAN_AUTO_MERGE="merge"
;;
"r"|"rebase"|"变基"|"变基合并"|"变基自动合并")
CLEAN_AUTO_MERGE="rebase"
;;
*)
echo "[ERROR] 未知的自动合并方式: $AUTO_MERGE"
echo "[TIP] 可用的自动合并方式: 压缩、合并、变基"
exit 1
;;
esac
fi

if [[ ("$AUTO_MERGE" != "$CLEAN_AUTO_MERGE") && ("$DEBUG") ]]; then
echo "[DEBUG] 已格式化自动合并方式: $AUTO_MERGE -> $CLEAN_AUTO_MERGE"
fi

# 格式化标签
CLEAN_LABELS=$(echo "$LABELS" | sed "s/[\"\'\`]*//g")
if [[ ("$LABELS" != "$CLEAN_LABELS") && ("$DEBUG") ]]; then
echo "[DEBUG] 标签包含特殊字符,已移除: $LABELS -> $CLEAN_LABELS"
fi

# 校验审查者
CLEAN_REVIEWER=$(echo "$REVIEWER" | sed "s/[\"\'\`]*//g")
if [[ ("$REVIEWER" != "$CLEAN_REVIEWER") && ("$DEBUG") ]]; then
echo "[DEBUG] 审查者信息包含特殊字符,已移除: $REVIEWER -> $CLEAN_REVIEWER"
fi

if [[ -n $CLEAN_REVIEWER ]]; then
IFS=',' read -r -a reviewers <<< "$CLEAN_REVIEWER"
# 遍历每个用户名并检查是否是协作者
for reviewer in "${reviewers[@]}"; do
# 使用 curl 发送请求,获取协作者信息
response=$(curl -s -w "%{http_code}" -o response.json \
-H "Authorization: token $TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/collaborators")

# 获取响应的状态码
status_code=$(tail -n1 <<< "$response")

# 处理不同的 HTTP 状态码
case $status_code in
200|201)
# 请求成功,检查是否有该审查者
if ! jq -e ".[] | select(.login == \"$reviewer\")" response.json > /dev/null; then
echo "[ERROR] $reviewer 不是仓库的协作者"
if [[ "$DEBUG" ]]; then
echo "[DEBUG] GitHub API 请求返回:"
while IFS= read -r line; do
echo "[DEBUG] $line"
done < response.json
exit 1
fi
elif [[ "$DEBUG" ]]; then
echo "[DEBUG] 审查者 $reviewer 鉴权成功"
fi
;;
401)
echo "[ERROR] 验证审查者时出错: 鉴权失败 (401):"
while IFS= read -r line; do
echo "[DEBUG] $line"
done < response.json
exit 1
;;
403)
echo "[ERROR] 验证审查者时出错: 没有权限或达到速率限制 (403)"
while IFS= read -r line; do
echo "[DEBUG] $line"
done < response.json
exit 1
;;
404)
echo "[ERROR] 验证审查者时出错: 没有权限或仓库不存在 (404)"
while IFS= read -r line; do
echo "[DEBUG] $line"
done < response.json
exit 1
;;
*)
echo "[ERROR] 验证审查者时出错: 未命中的非成功状态码 ($status_code)"
while IFS= read -r line; do
echo "[DEBUG] $line"
done < response.json
exit 1
;;
esac
done
fi

# 签出分支
BRANCH_NAME="sitemap-update-$(date +%Y%m%d%H%M%S)"
git checkout -b $BRANCH_NAME
echo "[INFO] 已创建新分支: $BRANCH_NAME"

# 生成工作流 URL
WORKFLOW_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
;;
"commit"|"提交"|"直接提交"|"directcommit"|"commitdirectly")
UPDATE_WAY="Commit"
if [[ "$DEBUG" ]]; then
echo "[DEBUG] 更新方式: 直接提交到主分支"
fi
# 不得同时使用的参数
params=("LABELS" "AUTO_MERGE")

# 遍历参数名称数组,检查冲突
for param_name in "${params[@]}"; do
param_value="${!param_name}"
if [[ -n "$param_value" ]]; then
echo "[ERROR] 错误的参数传递"
echo "[TIP] $param_name 参数不得与更新方式“提交”共存"
exit 1
fi
done
;;
*)
echo "[ERROR] 未知的更新方式: $AUTO_MERGE"
echo "[TIP] 可用的更新方式: 提交、拉取请求"
exit 1
;;
esac

# 前面做完都要做的

# 配置 Git 用户
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"

# 提交并推送 sitemap.xml
git add "$LOCATION"
git commit -m "[${DATE_TIME}] 自动更新网站地图"
git config --global push.autoSetupRemote true
git push

# 拉取请求更新后续还要做的
if [[ "$UPDATE_WAY" == "PR" ]]; then
# 创建拉取请求
PR_URL=$(gh pr create --title "[${DATE_TIME}] 自动更新网站地图" \
--body "此拉取请求通过 [工作流](${WORKFLOW_URL}) 使用 [Sitemap Creator](https://github.com/DuckDuckStudio/Sitemap_Creator) 创建。" \
--base ${{ inputs.base_branch }} \
--head $BRANCH_NAME)
echo "[INFO] 已创建拉取请求: $PR_URL"

# 判断是否有清理后的标签并添加到 PR
if [[ -n "$CLEAN_LABELS" ]]; then
gh pr edit "$PR_URL" --add-label "$CLEAN_LABELS"
echo "[INFO] 已为创建的拉取请求添加标签: $CLEAN_LABELS"
elif [[ "$DEBUG" ]]; then
echo "[DEBUG] 没有有效标签,跳过添加标签"
fi

# 判断是否有清理后的审查者并添加到 PR
if [[ -n "$CLEAN_REVIEWER" ]]; then
gh pr edit "$PR_URL" --add-reviewer "$CLEAN_REVIEWER"
echo "[INFO] 已为创建的拉取请求添加审查者: $CLEAN_REVIEWER"
elif [[ "$DEBUG" ]]; then
echo "[DEBUG] 没有有效审查者,跳过添加审查者"
fi

# 判断是否启用自动合并
# 如果 CLEAN_AUTO_MERGE 有值(即设置了自动合并方式),进行后续处理
if [[ -n "$CLEAN_AUTO_MERGE" ]]; then
gh pr merge "$PR_URL" --$CLEAN_AUTO_MERGE --auto
echo "[INFO] 已为拉取请求启用 $CLEAN_AUTO_MERGE 合并"
elif [[ "$DEBUG" ]]; then
echo "[DEBUG] 没有有效自动合并方式,跳过启用自动合并"
fi
fi
node Sitemap_Creator.mjs
rm Sitemap_Creator.mjs
Loading