-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_rss_from_issue.py
More file actions
154 lines (129 loc) · 5.13 KB
/
add_rss_from_issue.py
File metadata and controls
154 lines (129 loc) · 5.13 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
import os
import sys
import yaml
import json
import requests
from github import Github
# 从环境变量获取GitHub Token
token = os.environ.get('GITHUB_TOKEN')
if not token:
print("未找到GITHUB_TOKEN环境变量")
sys.exit(1)
# 初始化GitHub客户端
g = Github(token)
# 获取Issue信息
issue_json = os.environ.get('GITHUB_EVENT_PATH')
if not issue_json:
print("未找到GITHUB_EVENT_PATH环境变量")
sys.exit(1)
with open(issue_json, 'r') as f:
issue_data = json.load(f)
# 获取Issue基本信息
repo_name = issue_data['repository']['full_name']
issue_number = issue_data['issue']['number']
issue_title = issue_data['issue']['title']
issue_body = issue_data['issue']['body']
issue_state = issue_data['issue']['state']
# 只有当Issue是打开状态且标题包含"添加RSS源"时才处理
if issue_state != 'open' or 'Add RSS Source' not in issue_title:
print(f"Issue #{issue_number} 不是打开状态或标题不包含'Add RSS Source',跳过处理")
sys.exit(0)
# 解析Issue内容
# GitHub表单式issue模板提交的内容是结构化的,包含各种字段
# 我们需要从issue数据中提取表单字段,而不是解析文本内容
try:
# 检查issue是否使用了模板
if 'labels' in issue_data['issue']:
labels = [label['name'] for label in issue_data['issue']['labels']]
# 只有带有add-rss标签的issue才处理
if 'add-rss' not in labels:
print(f"Issue #{issue_number} 没有add-rss标签,跳过处理")
sys.exit(0)
# 从issue事件数据中提取表单字段
# GitHub Actions事件数据中的issue.body是表单提交后的文本内容
# 但我们需要从event数据中直接获取结构化的表单字段
# 注意:GitHub Actions的issues事件不直接提供结构化的表单字段
# 我们需要解析issue.body中的Markdown表格或列表
body = issue_data['issue']['body']
website_name = None
rss_url = None
# 解析表单式issue模板生成的Markdown内容
# 表单式issue模板生成的内容格式如下:
# ### 网站名称
# 奇安信威胁情报中心
#
# ### RSS URL
# https://example.com/feed.xml
lines = body.strip().split('\n')
current_field = None
for line in lines:
line = line.strip()
if line.startswith('### '):
# 字段标题
current_field = line[4:].strip()
elif current_field and line:
# 字段值
if current_field == '网站名称':
website_name = line
elif current_field == 'RSS URL':
rss_url = line
# 重置当前字段,避免重复赋值
current_field = None
# 如果上面的解析方法失败,尝试另一种格式(使用冒号分隔)
if not website_name or not rss_url:
for line in lines:
line = line.strip()
if line.startswith('网站名称:'):
website_name = line.split(':', 1)[1].strip()
elif line.startswith('RSS URL:'):
rss_url = line.split(':', 1)[1].strip()
# 如果还是失败,尝试原始的中文冒号格式
if not website_name or not rss_url:
for line in lines:
line = line.strip()
if line.startswith('网站名称:'):
website_name = line.split(':')[1].strip()
elif line.startswith('RSS链接:') or line.startswith('RSS URL:'):
rss_url = line.split(':')[1].strip()
if not website_name or not rss_url:
raise ValueError("Issue内容格式不正确,无法提取网站名称或RSS链接")
print(f"解析到的网站名称:{website_name}")
print(f"解析到的RSS链接:{rss_url}")
except Exception as e:
print(f"解析Issue内容失败:{str(e)}")
# 回复Issue
repo = g.get_repo(repo_name)
issue = repo.get_issue(number=issue_number)
issue.create_comment(f"解析Issue内容失败:{str(e)}\n请按照正确格式提交:\n网站名称:XXX\nRSS链接:XXX")
sys.exit(1)
# 读取现有的rss.yaml文件
try:
with open('rss.yaml', 'r', encoding='utf-8') as f:
rss_config = yaml.safe_load(f) or {}
except Exception as e:
print(f"读取rss.yaml文件失败:{str(e)}")
sys.exit(1)
# 添加新的RSS源
rss_config[website_name] = {
'rss_url': rss_url,
'website_name': website_name
}
# 保存更新后的rss.yaml文件
try:
with open('rss.yaml', 'w', encoding='utf-8') as f:
yaml.dump(rss_config, f, allow_unicode=True, default_flow_style=False)
print(f"成功将新RSS源添加到rss.yaml")
except Exception as e:
print(f"保存rss.yaml文件失败:{str(e)}")
sys.exit(1)
# 回复并关闭Issue
try:
repo = g.get_repo(repo_name)
issue = repo.get_issue(number=issue_number)
issue.create_comment(f"成功添加RSS源:\n网站名称:{website_name}\nRSS链接:{rss_url}")
issue.edit(state='closed')
print(f"成功回复并关闭Issue #{issue_number}")
except Exception as e:
print(f"回复或关闭Issue失败:{str(e)}")
sys.exit(1)
print("处理完成!")