pull/787/head
f 2 weeks ago
parent c00f820312
commit ef592f1d92

@ -120,7 +120,7 @@ jobs:
repo: event.repository.name, repo: event.repository.name,
pull_number: issueNumber pull_number: issueNumber
}); });
console.log(`PR found: #${issueNumber} from branch ${pr.head.ref}`); console.log(`Original PR found: #${issueNumber} from ${pr.user.login}`);
// Get the PR diff to extract the new prompt // Get the PR diff to extract the new prompt
console.log('Fetching PR file changes...'); console.log('Fetching PR file changes...');
@ -158,7 +158,8 @@ jobs:
contributorInfo = `Contributed by: [@${contributorLine[1]}](https://github.com/${contributorLine[2]})`; contributorInfo = `Contributed by: [@${contributorLine[1]}](https://github.com/${contributorLine[2]})`;
console.log(`Found contributor info: ${contributorInfo}`); console.log(`Found contributor info: ${contributorInfo}`);
} else { } else {
console.log('No contributor info found'); contributorInfo = `Contributed by: [@${pr.user.login}](https://github.com/${pr.user.login})`;
console.log(`Using PR author as contributor: ${contributorInfo}`);
} }
} }
} }
@ -175,7 +176,7 @@ jobs:
return; return;
} }
// Get content from main branch as reference // Get content from main branch
console.log('Fetching current content from main branch...'); console.log('Fetching current content from main branch...');
const { data: readmeFile } = await octokit.repos.getContent({ const { data: readmeFile } = await octokit.repos.getContent({
owner: event.repository.owner.login, owner: event.repository.owner.login,
@ -183,7 +184,6 @@ jobs:
path: 'README.md', path: 'README.md',
ref: 'main' ref: 'main'
}); });
console.log('README.md content fetched');
const { data: csvFile } = await octokit.repos.getContent({ const { data: csvFile } = await octokit.repos.getContent({
owner: event.repository.owner.login, owner: event.repository.owner.login,
@ -191,11 +191,10 @@ jobs:
path: 'prompts.csv', path: 'prompts.csv',
ref: 'main' ref: 'main'
}); });
console.log('prompts.csv content fetched');
// Format the new prompt section // Prepare new content
console.log('Preparing content updates...'); console.log('Preparing content updates...');
const newSection = `## ${actName}\n${contributorInfo ? contributorInfo + '\n' : ''}\n> ${newPrompt}\n\n`; const newSection = `## ${actName}\n${contributorInfo}\n\n> ${newPrompt}\n\n`;
// Insert the new section before Contributors in README // Insert the new section before Contributors in README
let readmeContent = Buffer.from(readmeFile.content, 'base64').toString('utf-8'); let readmeContent = Buffer.from(readmeFile.content, 'base64').toString('utf-8');
@ -213,89 +212,95 @@ jobs:
const csvContent = Buffer.from(csvFile.content, 'base64').toString('utf-8') + const csvContent = Buffer.from(csvFile.content, 'base64').toString('utf-8') +
`\n"${actName.replace(/"/g, '""')}","${newPrompt.replace(/"/g, '""')}"`; `\n"${actName.replace(/"/g, '""')}","${newPrompt.replace(/"/g, '""')}"`;
console.log('Creating Git operations in fork...'); // Create new branch
console.log(`Fork owner: ${pr.head.user.login}`); const branchName = `prompt/${actName.toLowerCase().replace(/[^a-z0-9]+/g, '-')}`;
console.log(`Fork repo: ${pr.head.repo.name}`); console.log(`Creating new branch: ${branchName}`);
console.log(`Branch: ${pr.head.ref}`);
// Get main branch ref
// Create blobs in fork const { data: mainRef } = await octokit.git.getRef({
console.log('Creating file blobs in fork...'); owner: event.repository.owner.login,
const [readmeBlob, csvBlob] = await Promise.all([ repo: event.repository.name,
octokit.git.createBlob({ ref: 'heads/main'
owner: pr.head.user.login,
repo: pr.head.repo.name,
content: Buffer.from(readmeContent).toString('base64'),
encoding: 'base64'
}),
octokit.git.createBlob({
owner: pr.head.user.login,
repo: pr.head.repo.name,
content: Buffer.from(csvContent).toString('base64'),
encoding: 'base64'
})
]);
console.log('File blobs created in fork');
// Get current tree from fork
const { data: currentTree } = await octokit.git.getTree({
owner: pr.head.user.login,
repo: pr.head.repo.name,
tree_sha: pr.head.sha,
recursive: true
}); });
// Create a new tree in fork // Create new branch
console.log('Creating new tree in fork...'); await octokit.git.createRef({
const { data: newTree } = await octokit.git.createTree({ owner: event.repository.owner.login,
owner: pr.head.user.login, repo: event.repository.name,
repo: pr.head.repo.name, ref: `refs/heads/${branchName}`,
base_tree: currentTree.sha, sha: mainRef.object.sha
tree: [
{
path: 'README.md',
mode: '100644',
type: 'blob',
sha: readmeBlob.data.sha
},
{
path: 'prompts.csv',
mode: '100644',
type: 'blob',
sha: csvBlob.data.sha
}
]
}); });
console.log('New tree created in fork');
// Update files with correct author
// Create a commit in fork await octokit.repos.createOrUpdateFileContents({
console.log('Creating commit in fork...'); owner: event.repository.owner.login,
const { data: newCommit } = await octokit.git.createCommit({ repo: event.repository.name,
owner: pr.head.user.login, path: 'README.md',
repo: pr.head.repo.name, message: `feat: Add "${actName}" to README`,
message: `feat: Add "${actName}" to prompts`, content: Buffer.from(readmeContent).toString('base64'),
tree: newTree.sha, branch: branchName,
parents: [pr.head.sha] committer: {
name: pr.user.login,
email: `${pr.user.login}@users.noreply.github.com`
},
author: {
name: pr.user.login,
email: `${pr.user.login}@users.noreply.github.com`
}
}); });
console.log(`New commit created in fork: ${newCommit.sha}`);
await octokit.repos.createOrUpdateFileContents({
// Update the reference in fork owner: event.repository.owner.login,
console.log(`Updating branch ${pr.head.ref} in fork...`); repo: event.repository.name,
await octokit.git.updateRef({ path: 'prompts.csv',
owner: pr.head.user.login, message: `feat: Add "${actName}" to prompts.csv`,
repo: pr.head.repo.name, content: Buffer.from(csvContent).toString('base64'),
ref: `heads/${pr.head.ref}`, branch: branchName,
sha: newCommit.sha committer: {
name: pr.user.login,
email: `${pr.user.login}@users.noreply.github.com`
},
author: {
name: pr.user.login,
email: `${pr.user.login}@users.noreply.github.com`
}
});
// Create new PR
const { data: newPr } = await octokit.pulls.create({
owner: event.repository.owner.login,
repo: event.repository.name,
title: `feat: Add "${actName}"`,
head: branchName,
base: 'main',
body: `This PR supersedes #${issueNumber} with proper formatting.
Original PR by @${pr.user.login}
Changes:
- Added "${actName}" to README.md
- Added prompt to prompts.csv
- Preserved original attribution
`
}); });
console.log('Branch updated successfully in fork');
console.log('Adding success comment to PR...'); // Comment on original PR
await octokit.issues.createComment({ await octokit.issues.createComment({
owner: event.repository.owner.login, owner: event.repository.owner.login,
repo: event.repository.name, repo: event.repository.name,
issue_number: issueNumber, issue_number: issueNumber,
body: `✨ Added "${actName}" to both files` body: `I've created a new PR #${newPr.number} with your contribution properly formatted. This PR will be closed in favor of the new one.`
}); });
console.log('Process completed successfully');
// Close original PR
await octokit.pulls.update({
owner: event.repository.owner.login,
repo: event.repository.name,
pull_number: issueNumber,
state: 'closed'
});
console.log(`Created new PR #${newPr.number} and closed original PR #${issueNumber}`);
} catch (error) { } catch (error) {
console.error('Error details:', error); console.error('Error details:', error);
@ -303,7 +308,7 @@ jobs:
owner: event.repository.owner.login, owner: event.repository.owner.login,
repo: event.repository.name, repo: event.repository.name,
issue_number: issueNumber, issue_number: issueNumber,
body: `❌ Error while trying to update files:\n\`\`\`\n${error.message}\n\`\`\`` body: `❌ Error while trying to create new PR:\n\`\`\`\n${error.message}\n\`\`\``
}); });
} }
return; return;

Loading…
Cancel
Save