forked from CN-E-Learning/CN-e-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
154 lines (138 loc) · 5.65 KB
/
school-info-auto-append.yml
File metadata and controls
154 lines (138 loc) · 5.65 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
# 学校信息自动填报工作流
name: 学校信息自动填报
on:
issues:
types: [labeled]
# 设置必要的权限
permissions:
contents: write
issues: write
pull-requests: write
jobs:
append-school-info:
runs-on: ubuntu-latest
if: github.event.label.name == '审核通过' && contains(github.event.issue.labels.*.name, '学校信息填报')
steps:
- name: 检出代码
uses: actions/checkout@v4
- name: 追加学校信息到表格
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
const filePath = 'docs/school/index.md';
const issue = context.payload.issue;
// 验证issue是否存在
if (!issue || !issue.body) {
core.setFailed('Issue内容为空');
return;
}
// 只取每个字段下第一个非空行
function extractField(body, field) {
const reg = new RegExp(`### ${field}\\s*([\\s\\S]*?)(?=\\n###|$)`, 'm');
const match = body.match(reg);
if (!match) return '';
return match[1].split('\n').map(s => s.trim()).filter(Boolean)[0] || '';
}
const name = extractField(issue.body, '学校名称');
const location = extractField(issue.body, '地区');
const type = extractField(issue.body, '学校类型');
// 验证必要字段
if (!location || !type || !name) {
core.setFailed('缺少必要字段:school_location、school_type、school_name');
return;
}
// 验证文件是否存在
if (!fs.existsSync(filePath)) {
core.setFailed(`文件不存在: ${filePath}`);
return;
}
try {
let content = fs.readFileSync(filePath, 'utf8');
// 检查是否已存在相同学校
if (content.includes(`[${name}]`)) {
core.setOutput('result', '学校已存在,跳过添加');
return;
}
// 确保文件以换行结尾
if (!content.endsWith('\n')) {
content += '\n';
}
// 直接在文件末尾追加新行
const newRow = `| ${location} | ${type} | [${name}](/school/list/${name}) |\n`;
content += newRow;
fs.writeFileSync(filePath, content, 'utf8');
core.setOutput('result', '学校信息添加成功');
} catch (error) {
core.setFailed(`处理文件时出错: ${error.message}`);
}
- name: 新建学校详情文件
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
const issue = context.payload.issue;
// 字段提取函数,与前面保持一致
function extractField(body, field) {
const reg = new RegExp(`### ${field}\\s*([\\s\\S]*?)(?=\\n###|$)`, 'm');
const match = body.match(reg);
if (!match) return '';
return match[1].split('\n').map(s => s.trim()).filter(Boolean)[0] || '';
}
const name = extractField(issue.body, '学校名称');
if (!name) {
core.setFailed('未能提取学校名称,无法创建详情文件');
return;
}
const fileDir = 'docs/school/list';
const filePath = path.join(fileDir, `${name}.md`);
if (!fs.existsSync(fileDir)) {
fs.mkdirSync(fileDir, { recursive: true });
}
const content = `# ${name}\n\n${issue.body}`;
fs.writeFileSync(filePath, content, 'utf8');
- name: 提交更改
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: '自动追加学校信息到表格及新建详情文件'
file_pattern: 'docs/school/index.md docs/school/list/*.md'
commit_user_name: 'github-actions[bot]'
commit_user_email: 'github-actions[bot]@users.noreply.github.com'
commit_author: ${{ github.event.issue.user.name || github.event.issue.user.login }} <${{ github.event.issue.user.id }}+${{ github.event.issue.user.login }}@users.noreply.github.com>
- name: 更新标签并关闭 Issue
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const issue_number = context.payload.issue.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
// 添加“添加完成”标签
await github.rest.issues.addLabels({
owner,
repo,
issue_number,
labels: ['添加完成']
});
// 移除“等待添加”标签(如果有)
try {
await github.rest.issues.removeLabel({
owner,
repo,
issue_number,
name: '等待添加'
});
} catch (e) {
// 如果没有该标签则忽略
}
// 关闭 issue
await github.rest.issues.update({
owner,
repo,
issue_number,
state: 'closed'
});