pull/787/head
f 2 weeks ago
parent d615b36bed
commit cd05562974

@ -99,6 +99,7 @@ jobs:
// Handle resolve conflicts command
if (aiCommand === 'resolve' || aiCommand === 'fix conflicts') {
if (!isPullRequest) {
console.log('Command rejected: Not a pull request');
await octokit.issues.createComment({
owner: event.repository.owner.login,
repo: event.repository.name,
@ -109,26 +110,34 @@ jobs:
}
try {
console.log('Starting resolve command execution...');
// Get PR details
console.log('Fetching PR details...');
const { data: pr } = await octokit.pulls.get({
owner: event.repository.owner.login,
repo: event.repository.name,
pull_number: issueNumber
});
console.log(`PR found: #${issueNumber} from branch ${pr.head.ref}`);
// Get the PR diff to extract the new prompt
console.log('Fetching PR file changes...');
const { data: files } = await octokit.pulls.listFiles({
owner: event.repository.owner.login,
repo: event.repository.name,
pull_number: issueNumber
});
console.log(`Found ${files.length} changed files`);
// Extract prompt from changes
console.log('Analyzing changes to extract prompt information...');
let newPrompt = '';
let actName = '';
let contributorInfo = '';
for (const file of files) {
console.log(`Processing file: ${file.filename}`);
if (file.filename === 'README.md') {
const patch = file.patch || '';
const addedLines = patch.split('\n')
@ -136,20 +145,26 @@ jobs:
.map(line => line.substring(1))
.join('\n');
console.log('Attempting to extract prompt from README changes...');
const promptMatch = addedLines.match(/## Act as (?:a |an )?([^\n]+)\n(?:Contributed by:[^\n]*\n)?(?:> )?([^#]+?)(?=\n\n|$)/);
if (promptMatch) {
actName = `Act as ${promptMatch[1].trim()}`;
newPrompt = promptMatch[2].trim();
console.log(`Found prompt: "${actName}"`);
const contributorLine = addedLines.match(/Contributed by: \[@([^\]]+)\]\(https:\/\/github\.com\/([^\)]+)\)/);
if (contributorLine) {
contributorInfo = `Contributed by: [@${contributorLine[1]}](https://github.com/${contributorLine[2]})`;
console.log(`Found contributor info: ${contributorInfo}`);
} else {
console.log('No contributor info found');
}
}
}
}
if (!actName || !newPrompt) {
console.log('Failed to extract prompt information');
await octokit.issues.createComment({
owner: event.repository.owner.login,
repo: event.repository.name,
@ -160,12 +175,14 @@ jobs:
}
// Get content from main branch as reference
console.log('Fetching current content from main branch...');
const { data: readmeFile } = await octokit.repos.getContent({
owner: event.repository.owner.login,
repo: event.repository.name,
path: 'README.md',
ref: 'main'
});
console.log('README.md content fetched');
const { data: csvFile } = await octokit.repos.getContent({
owner: event.repository.owner.login,
@ -173,24 +190,31 @@ jobs:
path: 'prompts.csv',
ref: 'main'
});
console.log('prompts.csv content fetched');
// Format the new prompt section
console.log('Preparing content updates...');
const newSection = `## ${actName}\n${contributorInfo ? contributorInfo + '\n' : ''}\n> ${newPrompt}\n\n`;
// Insert the new section before Contributors in README
let readmeContent = Buffer.from(readmeFile.content, 'base64').toString('utf-8');
const contributorsIndex = readmeContent.indexOf('## Contributors');
if (contributorsIndex === -1) {
readmeContent += newSection; // Append if Contributors section not found
console.log('Contributors section not found, appending to end');
readmeContent += newSection;
} else {
console.log('Inserting before Contributors section');
readmeContent = readmeContent.slice(0, contributorsIndex) + newSection + readmeContent.slice(contributorsIndex);
}
// Prepare CSV content
console.log('Preparing CSV content...');
const csvContent = Buffer.from(csvFile.content, 'base64').toString('utf-8') +
`\n"${actName.replace(/"/g, '""')}","${newPrompt.replace(/"/g, '""')}"`;
// Create a tree with both files
console.log('Creating Git tree...');
console.log(`Using PR head SHA: ${pr.head.sha}`);
const { data: mainTree } = await octokit.git.getTree({
owner: event.repository.owner.login,
repo: event.repository.name,
@ -199,6 +223,7 @@ jobs:
});
// Create blobs for both files
console.log('Creating file blobs...');
const [readmeBlob, csvBlob] = await Promise.all([
octokit.git.createBlob({
owner: event.repository.owner.login,
@ -213,8 +238,10 @@ jobs:
encoding: 'base64'
})
]);
console.log('File blobs created');
// Create a new tree
console.log('Creating new tree with updated files...');
const { data: newTree } = await octokit.git.createTree({
owner: event.repository.owner.login,
repo: event.repository.name,
@ -234,8 +261,10 @@ jobs:
}
]
});
console.log('New tree created');
// Create a commit
console.log('Creating commit...');
const { data: newCommit } = await octokit.git.createCommit({
owner: event.repository.owner.login,
repo: event.repository.name,
@ -243,29 +272,34 @@ jobs:
tree: newTree.sha,
parents: [pr.head.sha]
});
console.log(`New commit created: ${newCommit.sha}`);
// Update the reference
console.log(`Updating branch ${pr.head.ref}...`);
await octokit.git.updateRef({
owner: event.repository.owner.login,
repo: event.repository.name,
ref: `heads/${pr.head.ref}`,
sha: newCommit.sha
});
console.log('Branch updated successfully');
console.log('Adding success comment to PR...');
await octokit.issues.createComment({
owner: event.repository.owner.login,
repo: event.repository.name,
issue_number: issueNumber,
body: `✨ Added "${actName}" to both files`
});
console.log('Process completed successfully');
} catch (error) {
console.error('Error:', error);
console.error('Error details:', error);
await octokit.issues.createComment({
owner: event.repository.owner.login,
repo: event.repository.name,
issue_number: issueNumber,
body: `❌ Error while trying to update files: ${error.message}`
body: `❌ Error while trying to update files:\n\`\`\`\n${error.message}\n\`\`\``
});
}
return;

Loading…
Cancel
Save