pull/787/head
f 2 weeks ago
parent 6d21372c60
commit 3ef8f03ca9

@ -108,113 +108,110 @@ jobs:
return; return;
} }
// Get PR files try {
const { data: files } = await octokit.pulls.listFiles({ // Get PR details
owner: event.repository.owner.login, const { data: pr } = await octokit.pulls.get({
repo: event.repository.name, owner: event.repository.owner.login,
pull_number: issueNumber repo: event.repository.name,
}); pull_number: issueNumber
});
// Get PR details to know the branch
const { data: pr } = await octokit.pulls.get({
owner: event.repository.owner.login,
repo: event.repository.name,
pull_number: issueNumber
});
let readmeChanged = false; // Get the PR diff to extract the new prompt
let csvChanged = false; const { data: files } = await octokit.pulls.listFiles({
let newPrompt = ''; owner: event.repository.owner.login,
let actName = ''; repo: event.repository.name,
pull_number: issueNumber
});
// Analyze changes to extract prompt information // Extract prompt from changes
for (const file of files) { let newPrompt = '';
if (file.filename === 'README.md' && (file.status === 'modified' || file.status === 'added')) { let actName = '';
readmeChanged = true; let contributorInfo = '';
const patch = file.patch || '';
// Look for added lines in the patch for (const file of files) {
const addedLines = patch.split('\n') if (file.filename === 'README.md') {
.filter(line => line.startsWith('+')) const patch = file.patch || '';
.map(line => line.substring(1)) const addedLines = patch.split('\n')
.join('\n'); .filter(line => line.startsWith('+'))
.map(line => line.substring(1))
// Extract the new prompt section using the correct format .join('\n');
const promptMatch = addedLines.match(/## Act as (?:a |an )?([^\n]+)\n(?:Contributed by:[^\n]*\n)?(?:> )?([^#]+?)(?=\n\n|$)/);
if (promptMatch) { const promptMatch = addedLines.match(/## Act as (?:a |an )?([^\n]+)\n(?:Contributed by:[^\n]*\n)?(?:> )?([^#]+?)(?=\n\n|$)/);
actName = `Act as ${promptMatch[1].trim()}`; if (promptMatch) {
newPrompt = promptMatch[2].trim(); actName = `Act as ${promptMatch[1].trim()}`;
newPrompt = promptMatch[2].trim();
// Check if contributor line exists and is properly formatted
const contributorLine = addedLines.match(/Contributed by: \[@([^\]]+)\]\(https:\/\/github\.com\/([^\)]+)\)/); const contributorLine = addedLines.match(/Contributed by: \[@([^\]]+)\]\(https:\/\/github\.com\/([^\)]+)\)/);
if (!contributorLine) { if (contributorLine) {
// If no contributor line or improperly formatted, add a comment about it contributorInfo = `Contributed by: [@${contributorLine[1]}](https://github.com/${contributorLine[2]})`;
await octokit.issues.createComment({ }
owner: event.repository.owner.login,
repo: event.repository.name,
issue_number: issueNumber,
body: '⚠️ Note: Contributor line is missing or improperly formatted. Please add it in the format:\nContributed by: [@username](https://github.com/username)'
});
} }
} }
} }
if (file.filename === 'prompts.csv' && (file.status === 'modified' || file.status === 'added')) {
csvChanged = true; if (!actName || !newPrompt) {
await octokit.issues.createComment({
owner: event.repository.owner.login,
repo: event.repository.name,
issue_number: issueNumber,
body: '❌ Could not extract prompt information from changes'
});
return;
} }
}
if (!readmeChanged && !csvChanged) { // Get README from main branch
await octokit.issues.createComment({ const { data: readmeFile } = await octokit.repos.getContent({
owner: event.repository.owner.login, owner: event.repository.owner.login,
repo: event.repository.name, repo: event.repository.name,
issue_number: issueNumber, path: 'README.md',
body: '❌ No changes found in README.md or prompts.csv' ref: 'main'
}); });
return;
}
if (!actName || !newPrompt) { // Get CSV from main branch
await octokit.issues.createComment({ const { data: csvFile } = await octokit.repos.getContent({
owner: event.repository.owner.login, owner: event.repository.owner.login,
repo: event.repository.name, repo: event.repository.name,
issue_number: issueNumber, path: 'prompts.csv',
body: '❌ Could not extract prompt information from README.md' ref: 'main'
}); });
return;
}
try { // Prepare new README content
// If CSV wasn't updated, update it directly in the PR branch let readmeContent = Buffer.from(readmeFile.content, 'base64').toString('utf-8');
if (!csvChanged) { const newSection = `\n## ${actName}\n${contributorInfo ? contributorInfo + '\n' : ''}\n> ${newPrompt}\n`;
// Get current CSV content readmeContent += newSection;
const { data: currentCsv } = await octokit.repos.getContent({
owner: event.repository.owner.login,
repo: event.repository.name,
path: 'prompts.csv',
ref: pr.head.ref // Use PR's branch
});
// Add new prompt to CSV // Prepare new CSV content
const newCsvContent = Buffer.from(currentCsv.content, 'base64').toString('utf-8') + let csvContent = Buffer.from(csvFile.content, 'base64').toString('utf-8');
`\n"${actName.replace(/"/g, '""')}","${newPrompt.replace(/"/g, '""')}"`; csvContent += `\n"${actName.replace(/"/g, '""')}","${newPrompt.replace(/"/g, '""')}"`;
// Update CSV file directly in the PR branch // Update README in PR branch
await octokit.repos.createOrUpdateFileContents({ await octokit.repos.createOrUpdateFileContents({
owner: event.repository.owner.login, owner: event.repository.owner.login,
repo: event.repository.name, repo: event.repository.name,
path: 'prompts.csv', path: 'README.md',
message: `feat: Add "${actName}" to prompts.csv`, message: `feat: Add "${actName}" to README`,
content: Buffer.from(newCsvContent).toString('base64'), content: Buffer.from(readmeContent).toString('base64'),
branch: pr.head.ref, // Use PR's branch branch: pr.head.ref,
sha: currentCsv.sha sha: readmeFile.sha
}); });
await octokit.issues.createComment({ // Update CSV in PR branch
owner: event.repository.owner.login, await octokit.repos.createOrUpdateFileContents({
repo: event.repository.name, owner: event.repository.owner.login,
issue_number: issueNumber, repo: event.repository.name,
body: '✨ Updated prompts.csv in the PR with the new prompt' path: 'prompts.csv',
}); message: `feat: Add "${actName}" to prompts.csv`,
} content: Buffer.from(csvContent).toString('base64'),
branch: pr.head.ref,
sha: csvFile.sha
});
await octokit.issues.createComment({
owner: event.repository.owner.login,
repo: event.repository.name,
issue_number: issueNumber,
body: `✨ Updated both files:\n1. Added "${actName}" to README.md\n2. Added the prompt to prompts.csv`
});
} catch (error) { } catch (error) {
console.error('Error:', error); console.error('Error:', error);

Loading…
Cancel
Save