Skip to content

Commit 7854959

Browse files
authored
Workflow
1 parent e10cd4c commit 7854959

1 file changed

Lines changed: 113 additions & 35 deletions

File tree

.github/workflows/gemini-issue-assistant.yml

Lines changed: 113 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,73 @@ jobs:
2626
with:
2727
node-version: '20'
2828

29-
- name: Install Gemini CLI
29+
- name: Install Google AI SDK and Create Analysis Script
3030
run: |
31-
npm install -g @google/generative-ai
32-
npm install -g @google/generative-ai-cli || echo "CLI install failed, using direct API"
31+
echo "🔧 Installing Google AI SDK..."
32+
npm install @google/generative-ai
33+
34+
echo "📦 Creating Gemini analysis script..."
35+
cat > gemini-analyze.js << 'SCRIPT_EOF'
36+
const { GoogleGenerativeAI } = require('@google/generative-ai');
37+
const fs = require('fs');
38+
39+
async function analyzeIssue() {
40+
try {
41+
const apiKey = process.env.GEMINI_API_KEY;
42+
if (!apiKey) {
43+
throw new Error('GEMINI_API_KEY environment variable not found');
44+
}
45+
46+
console.log('🔑 API key configured, length:', apiKey.length);
47+
console.log('🤖 Initializing Gemini AI...');
48+
49+
const genAI = new GoogleGenerativeAI(apiKey);
50+
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
51+
52+
const prompt = fs.readFileSync('analysis_prompt.txt', 'utf8');
53+
console.log('📝 Prompt loaded, size:', prompt.length, 'characters');
54+
55+
console.log('🚀 Generating analysis...');
56+
const result = await model.generateContent(prompt);
57+
const response = await result.response;
58+
const text = response.text();
59+
60+
fs.writeFileSync('gemini_response.txt', text);
61+
console.log('✅ Analysis completed successfully');
62+
console.log('📄 Result size:', text.length, 'characters');
63+
64+
} catch (error) {
65+
console.error('❌ Gemini analysis failed:', error.message);
66+
console.error('🔍 Full error details:', error);
67+
68+
const fallbackContent = [
69+
'## 🤖 AI Analysis Status',
70+
'',
71+
'The automated AI analysis encountered an issue: ' + error.message,
72+
'',
73+
'This may be due to:',
74+
'- API key configuration issues',
75+
'- Network connectivity problems',
76+
'- Gemini API rate limits or service issues',
77+
'- Invalid prompt format or size',
78+
'',
79+
'### Manual Review Recommended',
80+
'Please review this issue manually and check the repository for related code.',
81+
''
82+
].join('\n');
83+
84+
fs.writeFileSync('gemini_response.txt', fallbackContent);
85+
process.exit(1);
86+
}
87+
}
88+
89+
analyzeIssue().catch(error => {
90+
console.error('Fatal error:', error);
91+
process.exit(1);
92+
});
93+
SCRIPT_EOF
94+
95+
echo "✅ Analysis script created successfully"
3396
3497
- name: Determine analysis type
3598
id: analysis-type
@@ -51,6 +114,35 @@ jobs:
51114
echo "type=skip" >> $GITHUB_OUTPUT
52115
fi
53116
117+
- name: Scan Codebase for Context
118+
id: scan-code
119+
run: |
120+
echo "📋 Scanning codebase for relevant context..."
121+
122+
# Get recent commits for context
123+
echo "📜 Recent commits:" > codebase_context.txt
124+
git log --oneline -5 >> codebase_context.txt
125+
echo "" >> codebase_context.txt
126+
127+
# Get main plugin files for context
128+
echo "📁 Main plugin files:" >> codebase_context.txt
129+
find . -name "*.php" -path "./.*" -prune -o -name "*.php" -print | head -10 | while read file; do
130+
if [ -f "$file" ]; then
131+
echo "=== $file ===" >> codebase_context.txt
132+
head -30 "$file" >> codebase_context.txt
133+
echo "" >> codebase_context.txt
134+
fi
135+
done
136+
137+
# Check if we collected context
138+
if [ -s codebase_context.txt ]; then
139+
echo "✅ Codebase context collected: $(wc -l < codebase_context.txt) lines"
140+
echo "context-available=true" >> $GITHUB_OUTPUT
141+
else
142+
echo "⚠️ No codebase context found"
143+
echo "context-available=false" >> $GITHUB_OUTPUT
144+
fi
145+
54146
- name: Create analysis prompt
55147
env:
56148
ANALYSIS_TYPE: ${{ steps.analysis-type.outputs.type }}
@@ -59,6 +151,7 @@ jobs:
59151
ISSUE_AUTHOR: ${{ github.event.issue.user.login }}
60152
COMMENT_BODY: ${{ github.event.comment.body }}
61153
COMMENT_AUTHOR: ${{ github.event.comment.user.login }}
154+
CONTEXT_AVAILABLE: ${{ steps.scan-code.outputs.context-available }}
62155
run: |
63156
# Skip analysis if not relevant
64157
if [ "$ANALYSIS_TYPE" = "skip" ]; then
@@ -110,8 +203,16 @@ jobs:
110203
echo "" >> analysis_prompt.txt
111204
echo "REPOSITORY CONTEXT: WordPress plugin project (WordPress 6.5+, PHP 7.4+)" >> analysis_prompt.txt
112205
fi
206+
207+
# Add codebase context if available
208+
if [ "$CONTEXT_AVAILABLE" = "true" ]; then
209+
echo "" >> analysis_prompt.txt
210+
echo "CODEBASE CONTEXT FOR REFERENCE:" >> analysis_prompt.txt
211+
cat codebase_context.txt >> analysis_prompt.txt
212+
fi
113213
114-
- name: Run Gemini Analysis
214+
- name: Run AI Analysis
215+
id: ai-analysis
115216
env:
116217
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
117218
ANALYSIS_SKIPPED: ${{ steps.analysis-type.outputs.analysis-skipped }}
@@ -121,41 +222,18 @@ jobs:
121222
exit 0
122223
fi
123224
124-
echo "Starting Gemini analysis..."
225+
echo "🤖 Starting AI issue analysis with official Google SDK..."
226+
echo "📝 Prompt file size: $(wc -c < analysis_prompt.txt) bytes"
125227
126-
# Try different methods to run Gemini analysis
127-
if command -v gemini &> /dev/null; then
128-
echo "Using Gemini CLI..."
129-
gemini analyze --file analysis_prompt.txt --model gemini-pro > gemini_response.txt 2>&1 || {
130-
echo "Gemini CLI failed, trying direct API..."
131-
node -e "
132-
const { GoogleGenerativeAI } = require('@google/generative-ai');
133-
const fs = require('fs');
134-
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
135-
const model = genAI.getGenerativeModel({ model: 'gemini-pro' });
136-
const prompt = fs.readFileSync('analysis_prompt.txt', 'utf8');
137-
model.generateContent(prompt).then(result => {
138-
const response = result.response;
139-
console.log(response.text());
140-
}).catch(console.error);
141-
" > gemini_response.txt 2>&1
142-
}
228+
if node gemini-analyze.js; then
229+
echo "analysis-success=true" >> $GITHUB_OUTPUT
230+
echo "✅ AI analysis completed successfully"
143231
else
144-
echo "Using Node.js direct API..."
145-
node -e "
146-
const { GoogleGenerativeAI } = require('@google/generative-ai');
147-
const fs = require('fs');
148-
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
149-
const model = genAI.getGenerativeModel({ model: 'gemini-pro' });
150-
const prompt = fs.readFileSync('analysis_prompt.txt', 'utf8');
151-
model.generateContent(prompt).then(result => {
152-
const response = result.response;
153-
console.log(response.text());
154-
}).catch(console.error);
155-
" > gemini_response.txt 2>&1
232+
echo "analysis-success=false" >> $GITHUB_OUTPUT
233+
echo "❌ AI analysis failed - check logs for details"
156234
fi
157235
158-
# Output analysis results
236+
# Format the response
159237
echo "## 🤖 Gemini Issue Analysis" > formatted_response.txt
160238
echo "" >> formatted_response.txt
161239
if [ -s gemini_response.txt ]; then

0 commit comments

Comments
 (0)