Skip to content

Commit f3645a6

Browse files
committed
update
1 parent f7c1996 commit f3645a6

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

.github/workflows/ai_bot.yml

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,150 @@ jobs:
9696
// Extract the actual command after /ai
9797
const aiCommand = command.substring(3).trim();
9898
99+
// Handle resolve conflicts command
100+
if (aiCommand === 'resolve' || aiCommand === 'fix conflicts') {
101+
if (!isPullRequest) {
102+
await octokit.issues.createComment({
103+
owner: event.repository.owner.login,
104+
repo: event.repository.name,
105+
issue_number: issueNumber,
106+
body: '❌ The resolve command can only be used on pull requests.'
107+
});
108+
return;
109+
}
110+
111+
// Get PR files
112+
const { data: files } = await octokit.pulls.listFiles({
113+
owner: event.repository.owner.login,
114+
repo: event.repository.name,
115+
pull_number: issueNumber
116+
});
117+
118+
let readmeContent = '';
119+
let csvContent = '';
120+
let readmeChanged = false;
121+
let csvChanged = false;
122+
let newPrompt = '';
123+
let actName = '';
124+
125+
// Analyze changes to extract prompt information
126+
for (const file of files) {
127+
if (file.filename === 'README.md' && (file.status === 'modified' || file.status === 'added')) {
128+
readmeChanged = true;
129+
const patch = file.patch || '';
130+
// Look for added lines in the patch
131+
const addedLines = patch.split('\n')
132+
.filter(line => line.startsWith('+'))
133+
.map(line => line.substring(1))
134+
.join('\n');
135+
136+
// Extract the new prompt section
137+
const promptMatch = addedLines.match(/### (.*?)\n([\s\S]*?)(?=\n###|$)/);
138+
if (promptMatch) {
139+
actName = promptMatch[1];
140+
newPrompt = promptMatch[2].trim();
141+
}
142+
}
143+
if (file.filename === 'prompts.csv' && (file.status === 'modified' || file.status === 'added')) {
144+
csvChanged = true;
145+
}
146+
}
147+
148+
if (!readmeChanged && !csvChanged) {
149+
await octokit.issues.createComment({
150+
owner: event.repository.owner.login,
151+
repo: event.repository.name,
152+
issue_number: issueNumber,
153+
body: '❌ No changes found in README.md or prompts.csv'
154+
});
155+
return;
156+
}
157+
158+
if (!actName || !newPrompt) {
159+
await octokit.issues.createComment({
160+
owner: event.repository.owner.login,
161+
repo: event.repository.name,
162+
issue_number: issueNumber,
163+
body: '❌ Could not extract prompt information from README.md'
164+
});
165+
return;
166+
}
167+
168+
// Create a new branch for fixes
169+
const branchName = `ai-bot/fix-conflicts-${issueNumber}`;
170+
const defaultBranch = event.repository.default_branch;
171+
172+
// Get current branch ref
173+
const { data: ref } = await octokit.git.getRef({
174+
owner: event.repository.owner.login,
175+
repo: event.repository.name,
176+
ref: `heads/${defaultBranch}`
177+
});
178+
179+
try {
180+
// Create new branch
181+
await octokit.git.createRef({
182+
owner: event.repository.owner.login,
183+
repo: event.repository.name,
184+
ref: `refs/heads/${branchName}`,
185+
sha: ref.object.sha
186+
});
187+
188+
// If CSV wasn't updated, update it
189+
if (!csvChanged) {
190+
// Get current CSV content
191+
const { data: currentCsv } = await octokit.repos.getContent({
192+
owner: event.repository.owner.login,
193+
repo: event.repository.name,
194+
path: 'prompts.csv',
195+
ref: branchName
196+
});
197+
198+
// Add new prompt to CSV
199+
const newCsvContent = Buffer.from(currentCsv.content, 'base64').toString('utf-8') +
200+
`\n"${actName.replace(/"/g, '""')}","${newPrompt.replace(/"/g, '""')}"`;
201+
202+
// Update CSV file
203+
await octokit.repos.createOrUpdateFileContents({
204+
owner: event.repository.owner.login,
205+
repo: event.repository.name,
206+
path: 'prompts.csv',
207+
message: `feat: Add "${actName}" to prompts.csv`,
208+
content: Buffer.from(newCsvContent).toString('base64'),
209+
branch: branchName,
210+
sha: currentCsv.sha
211+
});
212+
213+
// Create PR or update existing one
214+
await octokit.pulls.create({
215+
owner: event.repository.owner.login,
216+
repo: event.repository.name,
217+
title: `feat: Add "${actName}" to prompts.csv`,
218+
body: `This PR was automatically generated to sync prompts.csv with README.md changes.\n\nRelated to #${issueNumber}`,
219+
head: branchName,
220+
base: defaultBranch
221+
});
222+
}
223+
224+
await octokit.issues.createComment({
225+
owner: event.repository.owner.login,
226+
repo: event.repository.name,
227+
issue_number: issueNumber,
228+
body: '✨ Created a fix PR to sync README.md and prompts.csv'
229+
});
230+
231+
} catch (error) {
232+
console.error('Error:', error);
233+
await octokit.issues.createComment({
234+
owner: event.repository.owner.login,
235+
repo: event.repository.name,
236+
issue_number: issueNumber,
237+
body: `❌ Error while trying to fix conflicts: ${error.message}`
238+
});
239+
}
240+
return;
241+
}
242+
99243
// Handle rename command specifically
100244
if (aiCommand.startsWith('rename') || aiCommand === 'suggest title') {
101245
if (!isPullRequest) {

0 commit comments

Comments
 (0)