Skip to content

Commit 7a87533

Browse files
committed
chore: Use CI to automatically update data every month
1 parent f2e483c commit 7a87533

File tree

5 files changed

+84
-9
lines changed

5 files changed

+84
-9
lines changed

.github/workflows/gh-pages.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ on:
44
push:
55
branches:
66
- main
7+
workflow_run:
8+
workflows: ["Monthly Script Runner"]
9+
types:
10+
- completed
711

812
jobs:
913
build-deploy:
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: Monthly Script Runner
2+
3+
# 触发条件:每月的1号运行
4+
on:
5+
schedule:
6+
- cron: "0 0 0 * *" # 每月一号的00:00 UTC时间运行
7+
workflow_dispatch:
8+
9+
jobs:
10+
run-scripts:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
# 检出代码
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
18+
# 设置 Node.js 环境
19+
- name: Set up Node.js
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version: '20' # 使用 Node.js 18
23+
24+
# 运行第一个脚本 sync_xlab.js,从接口获取最新数据
25+
- name: Run sync_xlab.js
26+
run: node script/sync_xlab.js
27+
28+
# 运行第二个脚本 update_year_user.js, 更新 rankingList.json
29+
- name: Run update_year_user.js
30+
run: node script/update_year_user.js
31+
env:
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+
34+
# 运行第三个脚本 front-matter.js, 批量创建详情页
35+
- name: Run front-matter.js
36+
run: node script/front-matter.js
37+
38+
# 提交并推送更改到 main 分支
39+
- name: Commit and push changes
40+
run: |
41+
git config --local user.email "action@github.com"
42+
git config --local user.name "GitHub Action"
43+
git add .
44+
git commit -m "chore: Automated data update from scripts"
45+
git push origin main
46+
env:
47+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
48+
49+
# # 创建并推送 PR
50+
# - name: Create Pull Request
51+
# id: create_pr
52+
# uses: peter-evans/create-pull-request@v7
53+
# with:
54+
# commit-message: "chore: Updating data from xlab"
55+
# title: "chore: Updating data from xlab"
56+
# body: "Updating data from xlab,and create user homepages"
57+
# branch: update-ranking-data
58+
# base: "main"
59+
# delete-branch: true
60+
61+
# # 启用自动合并
62+
# - name: Enable auto-merge
63+
# uses: peter-evans/enable-pull-request-automerge@v3
64+
# with:
65+
# token: ${{ secrets.GITHUB_TOKEN }}
66+
# pull-request-number: ${{ steps.create_pr.outputs.pull-request-number }}
67+
# merge-method: merge

script/front-matter.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ async function updateFrontMatter(login, item) {
6767
}
6868

6969
// 主函数
70-
async function main(year) {
70+
async function main() {
71+
const year = new Date().getFullYear();
7172
try {
7273
const rankingsData = await readRankingData(year);
7374

@@ -90,4 +91,4 @@ async function main(year) {
9091
}
9192

9293
// 执行主程序
93-
main(2024);
94+
main();

script/sync_xlab.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ async function writeRankingData(data) {
4040
}
4141

4242
// 主函数
43-
async function main(year) {
43+
async function main() {
44+
const year = new Date().getFullYear();
45+
console.log(year);
4446
const rankingData = await readRankingData();
4547
const xlabData = await fetchXlab(year);
4648

@@ -49,7 +51,7 @@ async function main(year) {
4951
return;
5052
}
5153

52-
const yearIndex = rankingData.findIndex(v => v.year === year);
54+
const yearIndex = rankingData.findIndex(v => Number(v.year) === year);
5355
// 获取当前时间戳
5456
const update = `${new Date().getFullYear()}${new Date().getMonth() + 1} 月`;
5557
const ranking = {
@@ -58,7 +60,7 @@ async function main(year) {
5860
annualRanking: xlabData,
5961
};
6062

61-
if (yearIndex > 0) {
63+
if (yearIndex > -1) {
6264
rankingData[yearIndex] = ranking;
6365
} else {
6466
// 插入新的年份数据
@@ -71,4 +73,4 @@ async function main(year) {
7173
await writeRankingData(rankingData);
7274
}
7375

74-
main(2024);
76+
main();

script/update_year_user.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const fs = require('fs').promises;
99
const path = require('path');
1010

1111
// 运行此脚本 需要配置 github_token 否则会 API 拉取会被 github 限制
12-
const GITHUB_TOKEN = ''; // 替换为你的 GitHub 个人访问令牌
12+
const GITHUB_TOKEN = process.env.GITHUB_TOKEN; // 替换为你的 GitHub 个人访问令牌
1313
// 防止速度过快被 github 限制
1414
const TIME_DELAY = 600;
1515

@@ -96,7 +96,8 @@ async function enrichRankingData(data) {
9696
}
9797

9898
// 主函数
99-
async function main(year) {
99+
async function main() {
100+
const year = new Date().getFullYear();
100101
try {
101102
const rankingsData = await readRankingData();
102103
const yearData = rankingsData.find(yearData => yearData.year === year);
@@ -114,4 +115,4 @@ async function main(year) {
114115
}
115116
}
116117

117-
main(2024);
118+
main();

0 commit comments

Comments
 (0)