Skip to content

Commit 9a2cce9

Browse files
committed
fix: station json check
1 parent 5a48ab8 commit 9a2cce9

4 files changed

Lines changed: 2504 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: Protect station.json
2+
3+
on:
4+
push:
5+
paths:
6+
- "resource/station.json"
7+
8+
jobs:
9+
protect:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
with:
15+
token: ${{ secrets.PAT_TOKEN }}
16+
fetch-depth: 0
17+
18+
- name: Check if committer is bot and revert if needed
19+
env:
20+
PAT_TOKEN: ${{ secrets.PAT_TOKEN }}
21+
run: |
22+
BRANCH="${{ github.ref_name }}"
23+
ACTOR="${{ github.actor }}"
24+
COMMITTER_EMAIL="${{ github.event.commits[0].author.email }}"
25+
COMMITTER_NAME="${{ github.event.commits[0].author.name }}"
26+
COMMIT_MESSAGE="${{ github.event.commits[0].message }}"
27+
COMMIT_SHA="${{ github.event.commits[0].sha }}"
28+
29+
echo "檢查分支: $BRANCH"
30+
31+
# 首先檢查是否在 main 分支上
32+
if [ "$BRANCH" != "main" ]; then
33+
echo "❌ 錯誤: 不允許直接修改 resource/station.json"
34+
exit 1
35+
fi
36+
37+
echo "檢查提交者信息:"
38+
echo " Actor: $ACTOR"
39+
echo " 提交者: $COMMITTER_NAME ($COMMITTER_EMAIL)"
40+
echo " 提交訊息: $COMMIT_MESSAGE"
41+
echo " 提交 SHA: $COMMIT_SHA"
42+
43+
# 檢查是否為 bot 提交
44+
IS_BOT=false
45+
46+
# 檢查 actor 是否為 bot
47+
if [[ "$ACTOR" == *"[bot]"* ]] || [[ "$ACTOR" == "github-actions[bot]" ]]; then
48+
IS_BOT=true
49+
fi
50+
51+
# 檢查提交者 email 是否為 GitHub Actions
52+
if [[ "$COMMITTER_EMAIL" == *"noreply@github.com"* ]] || \
53+
[[ "$COMMITTER_EMAIL" == "action@github.com" ]]; then
54+
IS_BOT=true
55+
fi
56+
57+
# 檢查提交者名稱
58+
if [[ "$COMMITTER_NAME" == "GitHub Action" ]] || \
59+
[[ "$COMMITTER_NAME" == *"[bot]"* ]]; then
60+
IS_BOT=true
61+
fi
62+
63+
# 檢查提交訊息是否包含 [skip ci](我們的 workflow 使用這個標記)
64+
if [[ "$COMMIT_MESSAGE" == *"[skip ci]"* ]]; then
65+
IS_BOT=true
66+
fi
67+
68+
if [ "$IS_BOT" = false ]; then
69+
echo "❌ 錯誤: resource/station.json 只能由 bot 或 GitHub Actions 更新"
70+
echo " 當前提交者: $COMMITTER_NAME ($COMMITTER_EMAIL)"
71+
echo " 操作者: $ACTOR"
72+
echo ""
73+
echo "正在 revert resource/station.json 檔案..."
74+
75+
# 配置 git
76+
git config --local user.email "action@github.com"
77+
git config --local user.name "GitHub Action"
78+
79+
# 設置 remote URL 包含 PAT token
80+
git remote set-url origin https://x-access-token:$PAT_TOKEN@github.com/${{ github.repository }}.git
81+
82+
# 只 revert station.json 檔案(從上一個提交恢復)
83+
git checkout HEAD~1 -- resource/station.json
84+
85+
# 提交恢復的檔案
86+
git add resource/station.json
87+
git commit -m "Revert unauthorized change to station.json [skip ci]"
88+
89+
# 強制推送
90+
git push origin $BRANCH --force
91+
92+
echo "✅ 已 revert resource/station.json 並強制推送"
93+
exit 1
94+
fi
95+
96+
echo "✅ 允許: 提交來自 bot 或 GitHub Actions"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Station JSON Check
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
paths:
9+
- "resource/station-dev.json"
10+
pull_request:
11+
types: [opened, synchronize, reopened]
12+
paths:
13+
- "resource/station-dev.json"
14+
15+
jobs:
16+
check:
17+
runs-on: ubuntu-latest
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
with:
23+
token: ${{ secrets.PAT_TOKEN }}
24+
fetch-depth: 0
25+
persist-credentials: true
26+
sparse-checkout: |
27+
resource/station-dev.json
28+
resource/station.json
29+
scripts/station-json-check.ts
30+
sparse-checkout-cone-mode: false
31+
32+
- name: Setup Bun
33+
uses: oven-sh/setup-bun@v2
34+
with:
35+
bun-version: latest
36+
37+
- name: Run station JSON check
38+
run: bun run scripts/station-json-check.ts
39+
40+
- name: Commit and push changes
41+
if: success() && github.event_name == 'push' && github.ref == 'refs/heads/main'
42+
env:
43+
PAT_TOKEN: ${{ secrets.PAT_TOKEN }}
44+
run: |
45+
git config --local user.email "action@github.com"
46+
git config --local user.name "GitHub Action"
47+
48+
# 設置 remote URL 包含 PAT token
49+
git remote set-url origin https://x-access-token:$PAT_TOKEN@github.com/${{ github.repository }}.git
50+
51+
git add resource/station.json
52+
git diff --staged --quiet || git commit -m "chore: update station.json from station-dev.json [skip ci]"
53+
git push origin main

0 commit comments

Comments
 (0)