-
Notifications
You must be signed in to change notification settings - Fork 0
282 lines (239 loc) · 10 KB
/
Copy pathdaily_report.yml
File metadata and controls
282 lines (239 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
name: Daily Report & Deploy
on:
schedule:
- cron: '0 0 * * *' # 每天 UTC 00:00
workflow_dispatch:
push:
branches: [main]
permissions:
contents: write
pages: write
id-token: write
concurrency:
group: pages
cancel-in-progress: false
env:
PYTHON_VERSION: '3.11'
NODE_VERSION: '20'
jobs:
# Job 1: 生成报告(仅定时或手动触发)
generate-report:
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: 📅 Get date
id: date
run: |
echo "date=$(date +%Y-%m-%d)" >> $GITHUB_OUTPUT
echo "year=$(date +%Y)" >> $GITHUB_OUTPUT
echo "month=$(date +%m)" >> $GITHUB_OUTPUT
- name: 🐍 Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: 📦 Install Python dependencies
run: pip install -r requirements.txt
- name: 🚀 Generate report
env:
LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
LLM_MODEL: ${{ secrets.LLM_MODEL }}
GITHUB_API_TOKEN: ${{ secrets.GITHUB_API_TOKEN }}
run: python main.py --local --no-push
- name: 📁 Move output to reports
run: |
DATE=${{ steps.date.outputs.date }}
YEAR=${{ steps.date.outputs.year }}
MONTH=${{ steps.date.outputs.month }}
mkdir -p reports/$YEAR/$MONTH data/$YEAR/$MONTH
cp output/$DATE.md reports/$YEAR/$MONTH/ 2>/dev/null || true
cp output/$DATE.json data/$YEAR/$MONTH/ 2>/dev/null || true
- name: 📤 Commit and push
run: |
git config user.name "wayyoungboy"
# git config user.email "github-actions[bot]@users.noreply.github.com"
git config user.email "1017761807@qq.com"
git add reports/ data/
git diff --staged --quiet || git commit -m "📈 Add report for ${{ steps.date.outputs.date }}"
git push
# Job 2: 构建部署网站
build-deploy:
runs-on: ubuntu-latest
needs: [generate-report]
if: always() && (needs.generate-report.result == 'success' || needs.generate-report.result == 'skipped')
steps:
- uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0
- name: 🔄 Pull latest changes
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git fetch origin main
git pull origin main --rebase
echo "✅ Latest changes pulled"
echo ""
echo "Latest 5 commits:"
git log --oneline -5
- name: 🟢 Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: 📦 Install dependencies
run: npm install
- name: 🔧 Generate sidebar
run: npm run generate-sidebar
- name: 📅 Find latest report date
id: latest-report
run: |
echo "🔍 Searching for latest report..."
echo ""
echo "Available years:"
ls reports/
echo ""
# 查找 reports 目录下最新的报告日期(按文件名排序,因为文件名就是日期)
LATEST_YEAR=$(ls reports/ | sort -r | head -1)
echo "Latest year: $LATEST_YEAR"
echo ""
echo "Available months in $LATEST_YEAR:"
ls reports/$LATEST_YEAR/
echo ""
LATEST_MONTH=$(ls reports/$LATEST_YEAR/ | sort -r | head -1)
echo "Latest month: $LATEST_MONTH"
echo ""
echo "Available reports in $LATEST_YEAR/$LATEST_MONTH:"
ls reports/$LATEST_YEAR/$LATEST_MONTH/
echo ""
# 按文件名(日期)降序排序,取第一个
LATEST_DATE=$(ls reports/$LATEST_YEAR/$LATEST_MONTH/ | sort -r | head -1 | sed 's/.md$//')
echo "Latest date: $LATEST_DATE"
echo ""
echo "year=$LATEST_YEAR" >> $GITHUB_OUTPUT
echo "month=$LATEST_MONTH" >> $GITHUB_OUTPUT
echo "date=$LATEST_DATE" >> $GITHUB_OUTPUT
echo "✅ Latest report found: $LATEST_YEAR/$LATEST_MONTH/$LATEST_DATE"
- name: 🔧 Update index.js with latest report link
run: |
echo "🔧 Updating index.js with latest report link..."
echo ""
echo "Current link in index.js:"
grep -A 1 "primaryButton" src/pages/index.js | grep "to="
echo ""
LATEST_REPORT_PATH="/reports/${{ steps.latest-report.outputs.year }}/${{ steps.latest-report.outputs.month }}/${{ steps.latest-report.outputs.date }}"
echo "📌 New link: $LATEST_REPORT_PATH"
echo ""
echo "Updating link..."
# 替换 index.js 中的链接
sed -i "s|to=\"/reports/[^\"]*\"|to=\"$LATEST_REPORT_PATH\"|g" src/pages/index.js
echo "✅ Link updated successfully!"
echo ""
echo "Updated link in index.js:"
grep -A 1 "primaryButton" src/pages/index.js | grep "to="
echo ""
- name: 🏗️ Build
run: npm run build
- name: 📤 Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: build
# Job 3: 部署到 GitHub Pages
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build-deploy
steps:
- name: 🚀 Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
# Job 4: 发送钉钉通知
send-dingtalk:
runs-on: ubuntu-latest
needs: [deploy]
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: 📅 Get date
id: date
run: |
echo "date=$(date +%Y-%m-%d)" >> $GITHUB_OUTPUT
echo "year=$(date +%Y)" >> $GITHUB_OUTPUT
echo "month=$(date +%m)" >> $GITHUB_OUTPUT
- name: Find latest report
id: find_report
run: |
DATE=${{ steps.date.outputs.date }}
YEAR=${{ steps.date.outputs.year }}
MONTH=${{ steps.date.outputs.month }}
# 从 data 目录查找今天的 JSON 报告
report_file="data/$YEAR/$MONTH/$DATE.json"
if [ -f "$report_file" ]; then
echo "report_file=$report_file" >> $GITHUB_OUTPUT
echo "找到今日报告: $report_file"
else
# 如果没有今天的报告,查找最新的报告
latest_file=$(find data -type f -name "*.json" | sort -r | head -n 1)
if [ -z "$latest_file" ]; then
echo "未找到报告文件"
exit 1
fi
echo "report_file=$latest_file" >> $GITHUB_OUTPUT
echo "使用最新报告: $latest_file"
fi
- name: Send to DingTalk
env:
DINGDING_TOKEN: ${{ secrets.DINGDING_TOKEN }}
REPORT_FILE: ${{ steps.find_report.outputs.report_file }}
DATE: ${{ steps.date.outputs.date }}
SITE_URL: "https://wayyoungboy.github.io/github-trending-reporter/"
REPO_URL: "https://github.com/${{ github.repository }}"
run: |
# 如果没有配置 token 就安全地跳过(避免在 workflow 校验阶段引用 secrets 导致错误)
if [ -z "$DINGDING_TOKEN" ]; then
echo "DINGDING_TOKEN 未配置,跳过发送"
exit 0
fi
python3 - <<'PY'
import json, os
report_file = os.environ['REPORT_FILE']
date = os.environ['DATE']
site_url = os.environ['SITE_URL']
repo_url = os.environ['REPO_URL']
# 读取 JSON 数据
with open(report_file, 'r', encoding='utf-8') as f:
data = json.load(f)
print(json.dumps(data, ensure_ascii=False, indent=2))
# 构建简洁的 Markdown 消息
content = ""
emojis = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟", "1️⃣1️⃣", "1️⃣2️⃣", "1️⃣3️⃣", "1️⃣4️⃣", "1️⃣5️⃣"]
for i, repo in enumerate(data['repositories'], 1):
emoji = emojis[i-1] if i <= len(emojis) else f"#{i}"
content += f"### {emoji} **{repo['full_name']}**\n"
content += f"- **语言**: {repo['language']}\n"
content += f"- **增长**: +{repo['stars_today']} ⭐ (今日)\n"
content += f"- **总计**: {repo['stars']} ⭐\n"
content += f"- **简介**: {repo.get('llm_summary') or repo['description']}\n\n"
# 构建消息(Markdown)
message = {
'msgtype': 'markdown',
'markdown': {
'title': f'每日GitHub趋势报告 - {date}',
'text': (
f'### 📈 GitHub趋势每日报告\n\n'
f'**📅 日期**: {date}\n'
f'**📊 项目总数**: {data["total_repos"]}个\n'
f'**🔥 最热门**: {data["repositories"][0]["full_name"]} (+{data["repositories"][0]["stars_today"]}⭐)\n\n'
f'**📱 今日热门项目**: \n{content}\n'
f'**[查看完整报告]({site_url})** | **[项目仓库]({repo_url})**'
)
}
}
print(json.dumps(message, ensure_ascii=False, indent=2))
open('dingtalk_message.json', 'w', encoding='utf-8').write(json.dumps(message, ensure_ascii=False))
PY
curl "https://oapi.dingtalk.com/robot/send?access_token=$DINGDING_TOKEN" \
-H 'Content-Type: application/json' \
-d @dingtalk_message.json