forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
150 lines (130 loc) · 4.83 KB
/
Copy pathcite-lesson.yml
File metadata and controls
150 lines (130 loc) · 4.83 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
name: 知识引用追踪
on:
issues:
types: [opened, edited]
workflow_dispatch:
jobs:
cite-lesson:
# Only run for issues with 'usage' label; skip workflow_dispatch gracefully
if: >
github.event_name == 'issues' &&
contains(github.event.issue.labels.*.name, 'usage')
runs-on: ubuntu-latest
permissions:
issues: read
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Parse usage report and cite lessons
id: cite
run: |
python3 << 'PYEOF'
import json
import re
import os
import sys
from datetime import datetime
issue = json.loads(sys.argv[1])
body = issue.get('body', '')
node = issue.get('title', '?').replace('usage:', '').strip()
user = issue.get('user', {}).get('login', '?')
issue_url = issue.get('html_url', '')
issue_num = issue.get('number', 0)
date = datetime.utcnow().strftime('%Y-%m-%d')
# 解析 used 字段,提取 lesson ID
# 格式: "- lesson-id — 标题" 或 "- lesson-id"
used_section = ''
in_used = False
for line in body.split('\n'):
if 'used' in line.lower() or '本次使用' in line:
in_used = True
continue
if in_used:
if line.strip().startswith('- '):
used_section += line + '\n'
else:
break
lesson_ids = []
for line in used_section.split('\n'):
line = line.strip()
if not line.startswith('- '):
continue
# 提取 lesson ID(第一个 `—` 或 `-` 之前的部分)
content = line[2:].strip()
# 支持 "id — 标题" 和纯 "id" 两种格式
parts = re.split(r'[—–-]', content, maxsplit=1)
lesson_id = parts[0].strip()
if lesson_id:
lesson_ids.append(lesson_id)
print(f"NODE={node}", file=sys.stderr)
print(f"USER={user}", file=sys.stderr)
print(f"ISSUE_URL={issue_url}", file=sys.stderr)
print(f"LESSON_IDS={','.join(lesson_ids)}", file=sys.stderr)
# 输出给后续 steps 使用
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
f.write(f"node={node}\n")
f.write(f"user={user}\n")
f.write(f"issue_url={issue_url}\n")
f.write(f"issue_num={issue_num}\n")
f.write(f"date={date}\n")
f.write(f"lesson_ids={','.join(lesson_ids)}\n")
# 保存 lesson_ids 供后续 step 使用
with open('lesson_ids.txt', 'w') as f:
f.write('\n'.join(lesson_ids))
PYEOF
env:
ISSUE_JSON: ${{ toJson(github.event.issue) }}
- name: Add citation records to lessons
if: steps.cite.outputs.lesson_ids != ''
run: |
python3 << 'PYEOF'
import os
import sys
# 读取 cite step 输出
with open('lesson_ids.txt') as f:
lesson_ids = [l.strip() for l in f if l.strip()]
node = os.environ.get('NODE', '?')
user = os.environ.get('USER', '?')
issue_url = os.environ.get('ISSUE_URL', '')
date = os.environ.get('DATE', '')
issue_num = os.environ.get('ISSUE_NUM', '')
lessons_dir = 'lessons'
cited = []
skipped = []
for lesson_id in lesson_ids:
lesson_path = os.path.join(lessons_dir, f'{lesson_id}.md')
if not os.path.exists(lesson_path):
skipped.append(lesson_id)
continue
# 构建引用记录
citation = f"""
---
> 📖 被节点 [{node}]({issue_url}) 引用 | {date}
"""
with open(lesson_path, 'a', encoding='utf-8') as f:
f.write(citation)
cited.append(lesson_id)
print(f" ✓ cited {lesson_id}")
for lid in skipped:
print(f" ✗ not found: {lid}")
print(f"\nTotal: {len(cited)} cited, {len(skipped)} skipped")
PYEOF
env:
NODE: ${{ steps.cite.outputs.node }}
USER: ${{ steps.cite.outputs.user }}
ISSUE_URL: ${{ steps.cite.outputs.issue_url }}
DATE: ${{ steps.cite.outputs.date }}
ISSUE_NUM: ${{ steps.cite.outputs.issue_num }}
- name: Commit and push citations
if: steps.cite.outputs.lesson_ids != ''
run: |
git config user.name "misakanet-bot"
git config user.email "bot@misakanet.dev"
git add lessons/
if git diff --cached --quiet; then
echo "No changes to commit"
else
git commit -m "cite: record usage citations from #${{ steps.cite.outputs.issue_num }}"
git push
fi