This commit is contained in:
f 2025-01-05 23:35:55 +03:00
parent dcdd20bfaa
commit 2507582478
1 changed files with 86 additions and 5 deletions

View File

@ -7,14 +7,18 @@ on:
types: [created] types: [created]
pull_request_review_comment: pull_request_review_comment:
types: [created] types: [created]
pull_request:
types: [opened, edited, synchronize]
jobs: jobs:
respond-to-commands: respond-to-commands:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: | if: |
(github.event_name == 'issues' && contains(github.event.issue.body, '/ai')) || (github.actor == 'f') &&
((github.event_name == 'issues' && contains(github.event.issue.body, '/ai')) ||
(github.event_name == 'issue_comment' && contains(github.event.comment.body, '/ai')) || (github.event_name == 'issue_comment' && contains(github.event.comment.body, '/ai')) ||
(github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '/ai')) (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')))
permissions: permissions:
contents: write contents: write
pull-requests: write pull-requests: write
@ -36,7 +40,7 @@ jobs:
- name: Process command - name: Process command
id: process id: process
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GH_SECRET: ${{ secrets.GH_SECRET }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: | run: |
node << 'EOF' node << 'EOF'
@ -49,16 +53,24 @@ jobs:
}); });
const octokit = new Octokit({ const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN auth: process.env.GH_SECRET
}); });
const eventName = process.env.GITHUB_EVENT_NAME; const eventName = process.env.GITHUB_EVENT_NAME;
const eventPath = process.env.GITHUB_EVENT_PATH; const eventPath = process.env.GITHUB_EVENT_PATH;
const event = require(eventPath); const event = require(eventPath);
// 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;
}
// Get command and context // Get command and context
let command = ''; let command = '';
let issueNumber = null; let issueNumber = null;
let isPullRequest = false;
if (eventName === 'issues') { if (eventName === 'issues') {
command = event.issue.body; command = event.issue.body;
@ -66,9 +78,15 @@ jobs:
} else if (eventName === 'issue_comment') { } else if (eventName === 'issue_comment') {
command = event.comment.body; command = event.comment.body;
issueNumber = event.issue.number; issueNumber = event.issue.number;
isPullRequest = !!event.issue.pull_request;
} else if (eventName === 'pull_request_review_comment') { } else if (eventName === 'pull_request_review_comment') {
command = event.comment.body; command = event.comment.body;
issueNumber = event.pull_request.number; issueNumber = event.pull_request.number;
isPullRequest = true;
} else if (eventName === 'pull_request') {
command = event.pull_request.body;
issueNumber = event.pull_request.number;
isPullRequest = true;
} }
if (!command.startsWith('/ai')) { if (!command.startsWith('/ai')) {
@ -77,8 +95,71 @@ jobs:
// Extract the actual command after /ai // Extract the actual command after /ai
const aiCommand = command.substring(3).trim(); const aiCommand = command.substring(3).trim();
// 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;
}
// Generate response using OpenAI // Get PR details for context
const { data: pr } = await octokit.pulls.get({
owner: event.repository.owner.login,
repo: event.repository.name,
pull_number: issueNumber
});
// Get the PR diff for better context
const { data: diff } = await octokit.pulls.get({
owner: event.repository.owner.login,
repo: event.repository.name,
pull_number: issueNumber,
mediaType: { format: 'diff' }
});
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: "You are a helpful assistant that generates clear, concise, and descriptive pull request titles. Follow conventional commit message style. Return ONLY the new title, nothing else."
},
{
role: "user",
content: `Based on this pull request content, generate a better title.\n\nCurrent title: ${pr.title}\n\nDescription: ${pr.body}\n\nChanges:\n${diff}`
}
],
temperature: 0.7,
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,
body: `✨ Updated PR title to: "${newTitle}"`
});
return;
}
// Handle other commands
const completion = await openai.chat.completions.create({ const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo", model: "gpt-3.5-turbo",
messages: [ messages: [