Skip to content

Commit e1764bf

Browse files
committed
feat: 添加Pull Request和New Issue按钮,支持直接提交到GitHub
1 parent 53b4965 commit e1764bf

File tree

1 file changed

+95
-9
lines changed

1 file changed

+95
-9
lines changed

src/components/ChallengeContributePage/components/YamlPreviewSection.tsx

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import * as React from 'react';
2-
import { Button, Modal } from 'antd';
3-
import { CopyOutlined } from '@ant-design/icons';
2+
import { Button, Modal, Space, message } from 'antd';
3+
import { CopyOutlined, GithubOutlined, BugOutlined } from '@ant-design/icons';
44
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter';
55
import yaml from 'react-syntax-highlighter/dist/esm/languages/hljs/yaml';
66
import { vs } from 'react-syntax-highlighter/dist/esm/styles/hljs';
77

88
// 注册YAML语言
99
SyntaxHighlighter.registerLanguage('yaml', yaml);
1010

11+
// GitHub仓库信息
12+
const GITHUB_REPO = 'JSREP/crawler-leetcode';
13+
const GITHUB_BASE_URL = `https://github.com/${GITHUB_REPO}`;
14+
1115
// 自定义高亮主题
1216
const highlightTheme = {
1317
...vs,
@@ -48,16 +52,98 @@ const YamlPreviewSection: React.FC<YamlPreviewSectionProps> = ({
4852
setIsModalVisible(false);
4953
};
5054

55+
// 提取挑战名称,用于PR和Issue标题
56+
const extractChallengeName = (): string => {
57+
try {
58+
// 尝试从YAML中提取name字段
59+
const match = yamlOutput.match(/name:\s*(.+)/);
60+
if (match && match[1]) {
61+
return match[1].trim();
62+
}
63+
} catch (e) {
64+
console.error('无法提取挑战名称', e);
65+
}
66+
return '新挑战';
67+
};
68+
69+
// 创建Pull Request
70+
const createPullRequest = () => {
71+
if (!yamlOutput) {
72+
message.error('请先预览YAML生成内容');
73+
return;
74+
}
75+
76+
const challengeName = extractChallengeName();
77+
78+
// 准备PR内容
79+
const title = encodeURIComponent(`新增题目: ${challengeName}`);
80+
const body = encodeURIComponent(
81+
`## 新增题目\n\n` +
82+
`提交一个新的挑战题目。\n\n` +
83+
`### YAML代码\n\n` +
84+
`\`\`\`yaml\n${yamlOutput}\n\`\`\``
85+
);
86+
87+
// 构建PR创建URL
88+
const prUrl = `${GITHUB_BASE_URL}/compare/main...?quick_pull=1&title=${title}&body=${body}`;
89+
90+
// 在新标签页中打开
91+
window.open(prUrl, '_blank');
92+
};
93+
94+
// 创建Issue
95+
const createIssue = () => {
96+
if (!yamlOutput) {
97+
message.error('请先预览YAML生成内容');
98+
return;
99+
}
100+
101+
const challengeName = extractChallengeName();
102+
103+
// 准备Issue内容
104+
const title = encodeURIComponent(`题目请求: ${challengeName}`);
105+
const body = encodeURIComponent(
106+
`## 题目请求\n\n` +
107+
`请求添加以下挑战题目。\n\n` +
108+
`### YAML代码\n\n` +
109+
`\`\`\`yaml\n${yamlOutput}\n\`\`\``
110+
);
111+
112+
// 构建Issue创建URL
113+
const issueUrl = `${GITHUB_BASE_URL}/issues/new?title=${title}&body=${body}`;
114+
115+
// 在新标签页中打开
116+
window.open(issueUrl, '_blank');
117+
};
118+
51119
return (
52120
<>
53121
<div style={{ marginBottom: 16, marginTop: 24 }}>
54-
<Button
55-
type="primary"
56-
onClick={showModal}
57-
icon={<CopyOutlined />}
58-
>
59-
预览YAML
60-
</Button>
122+
<Space size="middle">
123+
<Button
124+
type="primary"
125+
onClick={showModal}
126+
icon={<CopyOutlined />}
127+
>
128+
预览YAML
129+
</Button>
130+
131+
<Button
132+
type="default"
133+
onClick={createPullRequest}
134+
icon={<GithubOutlined />}
135+
>
136+
Pull Request
137+
</Button>
138+
139+
<Button
140+
type="default"
141+
onClick={createIssue}
142+
icon={<BugOutlined />}
143+
>
144+
New Issue
145+
</Button>
146+
</Space>
61147
</div>
62148

63149
<Modal

0 commit comments

Comments
 (0)