2025-01-05 20:29:41 +00:00
name : AI Bot
on :
issues :
types : [ opened]
issue_comment :
types : [ created]
pull_request_review_comment :
types : [ created]
2025-01-05 20:35:55 +00:00
pull_request :
types : [ opened, edited, synchronize]
2025-01-05 20:29:41 +00:00
jobs :
respond-to-commands :
runs-on : ubuntu-latest
if : |
2025-01-05 20:35:55 +00:00
(github.actor == 'f') &&
((github.event_name == 'issues' && contains(github.event.issue.body, '/ai')) ||
2025-01-05 20:29:41 +00:00
(github.event_name == 'issue_comment' && contains(github.event.comment.body, '/ai')) ||
2025-01-05 20:35:55 +00:00
(github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '/ai')) ||
(github.event_name == 'pull_request' && contains(github.event.pull_request.body, '/ai')))
2025-01-05 20:29:41 +00:00
permissions :
contents : write
pull-requests : write
issues : write
steps :
- uses : actions/checkout@v3
with :
fetch-depth : 0
- name : Setup Node.js
uses : actions/setup-node@v3
with :
node-version : '18'
- name : Install dependencies
run : npm install openai@^4.0.0 @octokit/rest@^19.0.0
- name : Process command
id : process
env :
2025-01-05 20:35:55 +00:00
GH_SECRET : ${{ secrets.GH_SECRET }}
2025-01-05 20:29:41 +00:00
OPENAI_API_KEY : ${{ secrets.OPENAI_API_KEY }}
run : |
node << 'EOF'
const OpenAI = require('openai');
const { Octokit } = require('@octokit/rest');
async function main() {
const openai = new OpenAI({
apiKey : process.env.OPENAI_API_KEY
});
const octokit = new Octokit({
2025-01-05 20:35:55 +00:00
auth : process.env.GH_SECRET
2025-01-05 20:29:41 +00:00
});
const eventName = process.env.GITHUB_EVENT_NAME;
const eventPath = process.env.GITHUB_EVENT_PATH;
const event = require(eventPath);
2025-01-05 20:35:55 +00:00
// Double check user authorization
const actor = event.sender?.login || event.pull_request?.user?.login || event.issue?.user?.login;
if (actor !== 'f') {
console.log('Unauthorized user attempted to use the bot:', actor);
return;
}
2025-01-05 20:29:41 +00:00
// Get command and context
let command = '';
let issueNumber = null;
2025-01-05 20:35:55 +00:00
let isPullRequest = false;
2025-01-05 20:29:41 +00:00
if (eventName === 'issues') {
command = event.issue.body;
issueNumber = event.issue.number;
} else if (eventName === 'issue_comment') {
command = event.comment.body;
issueNumber = event.issue.number;
2025-01-05 20:35:55 +00:00
isPullRequest = !!event.issue.pull_request;
2025-01-05 20:29:41 +00:00
} else if (eventName === 'pull_request_review_comment') {
command = event.comment.body;
issueNumber = event.pull_request.number;
2025-01-05 20:35:55 +00:00
isPullRequest = true;
} else if (eventName === 'pull_request') {
command = event.pull_request.body;
issueNumber = event.pull_request.number;
isPullRequest = true;
2025-01-05 20:29:41 +00:00
}
if (!command.startsWith('/ai')) {
return;
}
// Extract the actual command after /ai
const aiCommand = command.substring(3).trim();
2025-01-05 20:35:55 +00:00
2025-01-05 20:46:18 +00:00
// Handle resolve conflicts command
if (aiCommand === 'resolve' || aiCommand === 'fix conflicts') {
if (!isPullRequest) {
2025-01-05 21:24:07 +00:00
console.log('Command rejected : Not a pull request');
2025-01-05 20:46:18 +00:00
await octokit.issues.createComment({
owner : event.repository.owner.login,
repo : event.repository.name,
issue_number : issueNumber,
body : '❌ The resolve command can only be used on pull requests.'
});
return;
}
2025-01-05 21:02:16 +00:00
try {
2025-01-05 21:24:07 +00:00
console.log('Starting resolve command execution...');
2025-01-05 21:02:16 +00:00
// Get PR details
2025-01-05 21:24:07 +00:00
console.log('Fetching PR details...');
2025-01-05 21:02:16 +00:00
const { data: pr } = await octokit.pulls.get({
owner : event.repository.owner.login,
repo : event.repository.name,
pull_number : issueNumber
});
2025-01-05 21:24:07 +00:00
console.log(`PR found : #${issueNumber} from branch ${pr.head.ref}`);
2025-01-05 21:02:16 +00:00
// Get the PR diff to extract the new prompt
2025-01-05 21:24:07 +00:00
console.log('Fetching PR file changes...');
2025-01-05 21:02:16 +00:00
const { data: files } = await octokit.pulls.listFiles({
owner : event.repository.owner.login,
repo : event.repository.name,
pull_number : issueNumber
});
2025-01-05 21:24:07 +00:00
console.log(`Found ${files.length} changed files`);
2025-01-05 21:02:16 +00:00
// Extract prompt from changes
2025-01-05 21:24:07 +00:00
console.log('Analyzing changes to extract prompt information...');
2025-01-05 21:02:16 +00:00
let newPrompt = '';
let actName = '';
let contributorInfo = '';
for (const file of files) {
2025-01-05 21:24:07 +00:00
console.log(`Processing file : ${file.filename}`);
2025-01-05 21:02:16 +00:00
if (file.filename === 'README.md') {
const patch = file.patch || '';
const addedLines = patch.split('\n')
.filter(line => line.startsWith('+'))
.map(line => line.substring(1))
.join('\n');
2025-01-05 21:24:07 +00:00
console.log('Attempting to extract prompt from README changes...');
2025-01-05 21:02:16 +00:00
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();
2025-01-05 21:24:07 +00:00
console.log(`Found prompt : "${actName}" `);
2025-01-05 21:02:16 +00:00
const contributorLine = addedLines.match(/Contributed by : \[@([^\]]+)\]\(https:\/\/github\.com\/([^\)]+)\)/);
if (contributorLine) {
contributorInfo = `Contributed by : [ @${contributorLine[1]}](https://github.com/${contributorLine[2]})`;
2025-01-05 21:24:07 +00:00
console.log(`Found contributor info : ${contributorInfo}`);
} else {
console.log('No contributor info found');
2025-01-05 21:02:16 +00:00
}
2025-01-05 20:53:06 +00:00
}
2025-01-05 20:46:18 +00:00
}
}
2025-01-05 21:02:16 +00:00
if (!actName || !newPrompt) {
2025-01-05 21:24:07 +00:00
console.log('Failed to extract prompt information');
2025-01-05 21:02:16 +00:00
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;
2025-01-05 20:46:18 +00:00
}
2025-01-05 21:15:22 +00:00
// Get content from main branch as reference
2025-01-05 21:24:07 +00:00
console.log('Fetching current content from main branch...');
2025-01-05 21:02:16 +00:00
const { data: readmeFile } = await octokit.repos.getContent({
2025-01-05 20:46:18 +00:00
owner : event.repository.owner.login,
repo : event.repository.name,
2025-01-05 21:02:16 +00:00
path : 'README.md' ,
2025-01-05 21:15:22 +00:00
ref : 'main'
2025-01-05 20:46:18 +00:00
});
2025-01-05 21:24:07 +00:00
console.log('README.md content fetched');
2025-01-05 20:46:18 +00:00
2025-01-05 21:02:16 +00:00
const { data: csvFile } = await octokit.repos.getContent({
2025-01-05 20:46:18 +00:00
owner : event.repository.owner.login,
repo : event.repository.name,
2025-01-05 21:02:16 +00:00
path : 'prompts.csv' ,
2025-01-05 21:15:22 +00:00
ref : 'main'
2025-01-05 20:46:18 +00:00
});
2025-01-05 21:24:07 +00:00
console.log('prompts.csv content fetched');
2025-01-05 20:46:18 +00:00
2025-01-05 21:10:51 +00:00
// Format the new prompt section
2025-01-05 21:24:07 +00:00
console.log('Preparing content updates...');
2025-01-05 21:17:51 +00:00
const newSection = `## ${actName}\n${contributorInfo ? contributorInfo + '\n' : ''}\n> ${newPrompt}\n\n`;
2025-01-05 21:10:51 +00:00
2025-01-05 21:15:22 +00:00
// 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) {
2025-01-05 21:24:07 +00:00
console.log('Contributors section not found, appending to end');
readmeContent += newSection;
2025-01-05 21:15:22 +00:00
} else {
2025-01-05 21:24:07 +00:00
console.log('Inserting before Contributors section');
2025-01-05 21:15:22 +00:00
readmeContent = readmeContent.slice(0, contributorsIndex) + newSection + readmeContent.slice(contributorsIndex);
}
2025-01-05 21:20:17 +00:00
// Prepare CSV content
2025-01-05 21:24:07 +00:00
console.log('Preparing CSV content...');
2025-01-05 21:20:17 +00:00
const csvContent = Buffer.from(csvFile.content, 'base64').toString('utf-8') +
`\n"${actName.replace(/"/g, '""')}","${newPrompt.replace(/"/g, '""')}"`;
2025-01-05 21:27:40 +00:00
console.log('Creating Git operations in fork...');
console.log(`Fork owner : ${pr.head.user.login}`);
console.log(`Fork repo : ${pr.head.repo.name}`);
console.log(`Branch : ${pr.head.ref}`);
2025-01-05 21:15:22 +00:00
2025-01-05 21:27:40 +00:00
// Create blobs in fork
console.log('Creating file blobs in fork...');
2025-01-05 21:20:17 +00:00
const [readmeBlob, csvBlob] = await Promise.all([
octokit.git.createBlob({
2025-01-05 21:27:40 +00:00
owner : pr.head.user.login,
repo : pr.head.repo.name,
2025-01-05 21:20:17 +00:00
content : Buffer.from(readmeContent).toString('base64'),
encoding : 'base64'
}),
octokit.git.createBlob({
2025-01-05 21:27:40 +00:00
owner : pr.head.user.login,
repo : pr.head.repo.name,
2025-01-05 21:20:17 +00:00
content : Buffer.from(csvContent).toString('base64'),
encoding : 'base64'
})
] );
2025-01-05 21:27:40 +00:00
console.log('File blobs created in fork');
2025-01-05 21:20:17 +00:00
2025-01-05 21:27:40 +00:00
// 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
console.log('Creating new tree in fork...');
2025-01-05 21:20:17 +00:00
const { data: newTree } = await octokit.git.createTree({
2025-01-05 21:27:40 +00:00
owner : pr.head.user.login,
repo : pr.head.repo.name,
base_tree : currentTree.sha,
2025-01-05 21:20:17 +00:00
tree : [
{
path : 'README.md' ,
mode : '100644' ,
type : 'blob' ,
sha : readmeBlob.data.sha
},
{
path : 'prompts.csv' ,
mode : '100644' ,
type : 'blob' ,
sha : csvBlob.data.sha
}
]
2025-01-05 21:15:22 +00:00
});
2025-01-05 21:27:40 +00:00
console.log('New tree created in fork');
2025-01-05 21:15:22 +00:00
2025-01-05 21:27:40 +00:00
// Create a commit in fork
console.log('Creating commit in fork...');
2025-01-05 21:20:17 +00:00
const { data: newCommit } = await octokit.git.createCommit({
2025-01-05 21:27:40 +00:00
owner : pr.head.user.login,
repo : pr.head.repo.name,
2025-01-05 21:20:17 +00:00
message: `feat : Add "${actName}" to prompts`,
tree : newTree.sha,
parents : [ pr.head.sha]
2025-01-05 21:02:16 +00:00
});
2025-01-05 21:27:40 +00:00
console.log(`New commit created in fork : ${newCommit.sha}`);
2025-01-05 20:46:18 +00:00
2025-01-05 21:27:40 +00:00
// Update the reference in fork
console.log(`Updating branch ${pr.head.ref} in fork...`);
2025-01-05 21:20:17 +00:00
await octokit.git.updateRef({
2025-01-05 21:27:40 +00:00
owner : pr.head.user.login,
repo : pr.head.repo.name,
2025-01-05 21:20:17 +00:00
ref : `heads/${pr.head.ref}`,
sha : newCommit.sha
2025-01-05 21:02:16 +00:00
});
2025-01-05 21:27:40 +00:00
console.log('Branch updated successfully in fork');
2025-01-05 21:02:16 +00:00
2025-01-05 21:24:07 +00:00
console.log('Adding success comment to PR...');
2025-01-05 21:02:16 +00:00
await octokit.issues.createComment({
owner : event.repository.owner.login,
repo : event.repository.name,
issue_number : issueNumber,
2025-01-05 21:20:17 +00:00
body : `✨ Added "${actName}" to both files`
2025-01-05 21:02:16 +00:00
});
2025-01-05 21:24:07 +00:00
console.log('Process completed successfully');
2025-01-05 20:46:18 +00:00
} catch (error) {
2025-01-05 21:24:07 +00:00
console.error('Error details:', error);
2025-01-05 20:46:18 +00:00
await octokit.issues.createComment({
owner : event.repository.owner.login,
repo : event.repository.name,
issue_number : issueNumber,
2025-01-05 21:24:07 +00:00
body : `❌ Error while trying to update files:\n\`\`\`\n${error.message}\n\`\`\``
2025-01-05 20:46:18 +00:00
});
}
return;
}
2025-01-05 20:35:55 +00:00
// Handle rename command specifically
if (aiCommand.startsWith('rename') || aiCommand === 'suggest title') {
if (!isPullRequest) {
await octokit.issues.createComment({
owner : event.repository.owner.login,
repo : event.repository.name,
issue_number : issueNumber,
body : '❌ The rename command can only be used on pull requests.'
});
return;
}
// Get PR details for context
const { data: pr } = await octokit.pulls.get({
owner : event.repository.owner.login,
repo : event.repository.name,
pull_number : issueNumber
});
2025-01-05 20:39:17 +00:00
// Get the list of files changed in the PR
const { data: files } = await octokit.pulls.listFiles({
2025-01-05 20:35:55 +00:00
owner : event.repository.owner.login,
repo : event.repository.name,
2025-01-05 20:39:17 +00:00
pull_number : issueNumber
2025-01-05 20:35:55 +00:00
});
2025-01-05 20:39:17 +00:00
// Process file changes
const fileChanges = await Promise.all(files.map(async file => {
if (file.status === 'removed') {
return `Deleted : ${file.filename}`;
}
// Get file content for added or modified files
if (file.status === 'added' || file.status === 'modified') {
const patch = file.patch || '';
return `${file.status === 'added' ? 'Added' : 'Modified'}: ${file.filename}\nChanges:\n${patch}`;
}
return `${file.status}: ${file.filename}`;
}));
2025-01-05 20:35:55 +00:00
const completion = await openai.chat.completions.create({
model : "gpt-3.5-turbo" ,
messages : [
{
role : "system" ,
2025-01-05 20:42:16 +00:00
content : "You are a helpful assistant that generates clear and concise pull request titles. Follow these rules:\n1. Use conventional commit style (feat:, fix:, docs:, etc.)\n2. Focus on WHAT changed, not HOW or WHERE\n3. Keep it short and meaningful\n4. Don't mention file names or technical implementation details\n5. Return ONLY the new title, nothing else\n\nGood examples:\n- feat: Add \"Act as a Career Coach\"\n- fix: Correct typo in Linux Terminal prompt\n- docs: Update installation instructions\n- refactor: Improve error handling"
2025-01-05 20:35:55 +00:00
},
{
role : "user" ,
2025-01-05 20:42:16 +00:00
content : `Based on these file changes, generate a concise PR title:\n\n${fileChanges.join('\n\n')}`
2025-01-05 20:35:55 +00:00
}
] ,
2025-01-05 20:42:16 +00:00
temperature : 0.5 ,
2025-01-05 20:35:55 +00:00
max_tokens : 60
});
const newTitle = completion.choices[0].message.content.trim();
// Update PR title
await octokit.pulls.update({
owner : event.repository.owner.login,
repo : event.repository.name,
pull_number : issueNumber,
title : newTitle
});
// Add comment about the rename
await octokit.issues.createComment({
owner : event.repository.owner.login,
repo : event.repository.name,
issue_number : issueNumber,
2025-01-05 20:39:17 +00:00
body: `✨ Updated PR title to : "${newTitle}" \n\nBased on the following changes:\n\`\`\`diff\n${fileChanges.join('\n')}\n\`\`\``
2025-01-05 20:35:55 +00:00
});
return;
}
2025-01-05 20:29:41 +00:00
2025-01-05 20:35:55 +00:00
// Handle other commands
2025-01-05 20:29:41 +00:00
const completion = await openai.chat.completions.create({
model : "gpt-3.5-turbo" ,
messages : [
{
role : "system" ,
content : "You are a helpful AI assistant that helps with GitHub repositories. You can suggest code changes, fix issues, and improve code quality."
},
{
role : "user" ,
content : aiCommand
}
] ,
temperature : 0.7 ,
max_tokens : 2000
});
const response = completion.choices[0].message.content;
// If response contains code changes, create a new branch and PR
if (response.includes('```')) {
const branchName = `ai-bot/fix-${issueNumber}`;
// Create new branch
const defaultBranch = event.repository.default_branch;
const ref = await octokit.git.getRef({
owner : event.repository.owner.login,
repo : event.repository.name,
ref : `heads/${defaultBranch}`
});
await octokit.git.createRef({
owner : event.repository.owner.login,
repo : event.repository.name,
ref : `refs/heads/${branchName}`,
sha : ref.data.object.sha
});
// Extract code changes and file paths from response
const codeBlocks = response.match(/```[\s\S]*?```/g);
for (const block of codeBlocks) {
const [_, filePath, ...codeLines] = block.split('\n');
const content = Buffer.from(codeLines.join('\n')).toString('base64');
await octokit.repos.createOrUpdateFileContents({
owner : event.repository.owner.login,
repo : event.repository.name,
path : filePath,
message: `AI Bot : Apply suggested changes for #${issueNumber}`,
content,
branch : branchName
});
}
// Create PR
await octokit.pulls.create({
owner : event.repository.owner.login,
repo : event.repository.name,
title: `AI Bot : Fix for #${issueNumber}`,
body : `This PR was automatically generated in response to #${issueNumber}\n\nChanges proposed:\n${response}`,
head : branchName,
base : defaultBranch
});
}
// Add comment with response
await octokit.issues.createComment({
owner : event.repository.owner.login,
repo : event.repository.name,
issue_number : issueNumber,
body : `AI Bot Response:\n\n${response}`
});
}
main().catch(error => {
console.error('Error:', error);
process.exit(1);
});
EOF
- name : Handle errors
if : failure()
uses : actions/github-script@v6
with :
script : |
const issueNumber = context.issue.number || context.payload.pull_request.number;
await github.rest.issues.createComment({
owner : context.repo.owner,
repo : context.repo.repo,
issue_number : issueNumber,
body : '❌ Sorry, there was an error processing your command. Please try again or contact the repository maintainers.'
});