pull/787/head
f 4 weeks ago
parent 2cf9dd17ae
commit d615b36bed

@ -186,51 +186,77 @@ jobs:
readmeContent = readmeContent.slice(0, contributorsIndex) + newSection + readmeContent.slice(contributorsIndex); readmeContent = readmeContent.slice(0, contributorsIndex) + newSection + readmeContent.slice(contributorsIndex);
} }
// Get current files from PR branch to get their SHAs // Prepare CSV content
const { data: prReadmeFile } = await octokit.repos.getContent({ const csvContent = Buffer.from(csvFile.content, 'base64').toString('utf-8') +
`\n"${actName.replace(/"/g, '""')}","${newPrompt.replace(/"/g, '""')}"`;
// Create a tree with both files
const { data: mainTree } = await octokit.git.getTree({
owner: event.repository.owner.login, owner: event.repository.owner.login,
repo: event.repository.name, repo: event.repository.name,
path: 'README.md', tree_sha: pr.head.sha,
ref: pr.head.ref recursive: true
}); });
const { data: prCsvFile } = await octokit.repos.getContent({ // Create blobs for both files
const [readmeBlob, csvBlob] = await Promise.all([
octokit.git.createBlob({
owner: event.repository.owner.login,
repo: event.repository.name,
content: Buffer.from(readmeContent).toString('base64'),
encoding: 'base64'
}),
octokit.git.createBlob({
owner: event.repository.owner.login,
repo: event.repository.name,
content: Buffer.from(csvContent).toString('base64'),
encoding: 'base64'
})
]);
// Create a new tree
const { data: newTree } = await octokit.git.createTree({
owner: event.repository.owner.login, owner: event.repository.owner.login,
repo: event.repository.name, repo: event.repository.name,
path: 'prompts.csv', base_tree: mainTree.sha,
ref: pr.head.ref tree: [
{
path: 'README.md',
mode: '100644',
type: 'blob',
sha: readmeBlob.data.sha
},
{
path: 'prompts.csv',
mode: '100644',
type: 'blob',
sha: csvBlob.data.sha
}
]
}); });
// Update files in PR branch using PR's file SHAs // Create a commit
await octokit.repos.createOrUpdateFileContents({ const { data: newCommit } = await octokit.git.createCommit({
owner: event.repository.owner.login, owner: event.repository.owner.login,
repo: event.repository.name, repo: event.repository.name,
path: 'README.md', message: `feat: Add "${actName}" to prompts`,
message: `feat: Add "${actName}" to README`, tree: newTree.sha,
content: Buffer.from(readmeContent).toString('base64'), parents: [pr.head.sha]
branch: pr.head.ref,
sha: prReadmeFile.sha // Use PR's file SHA
}); });
// Update CSV in PR branch // Update the reference
const csvContent = Buffer.from(csvFile.content, 'base64').toString('utf-8') + await octokit.git.updateRef({
`\n"${actName.replace(/"/g, '""')}","${newPrompt.replace(/"/g, '""')}"`;
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', ref: `heads/${pr.head.ref}`,
message: `feat: Add "${actName}" to prompts.csv`, sha: newCommit.sha
content: Buffer.from(csvContent).toString('base64'),
branch: pr.head.ref,
sha: prCsvFile.sha // Use PR's file SHA
}); });
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: `✨ Updated files in your PR using main branch as base:\n1. Added "${actName}" to README.md\n2. Added the prompt to prompts.csv` body: `✨ Added "${actName}" to both files`
}); });
} catch (error) { } catch (error) {

Loading…
Cancel
Save