Skip to content

Commit 11d4f92

Browse files
authored
Workflow
1 parent 4f00b6f commit 11d4f92

1 file changed

Lines changed: 113 additions & 94 deletions

File tree

β€Ž.github/workflows/gemini-code-assistant.ymlβ€Ž

Lines changed: 113 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,17 @@ jobs:
8787
BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.before || '' }}
8888
HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha || '' }}
8989
PR_AUTHOR: ${{ github.event.pull_request.user.login || github.actor }}
90-
PR_TITLE: ${{ github.event.pull_request.title || 'Code Analysis' }}
90+
PR_TITLE: ${{ github.event.pull_request.title || format('Push Analysis - {0}', github.ref_name) }}
9191
REPO_FULL_NAME: ${{ github.repository }}
92+
IS_PR: ${{ github.event_name == 'pull_request' }}
9293
run: |
9394
# Get context details from environment variables (secure)
9495
echo "pr-number=$PR_NUMBER" >> $GITHUB_OUTPUT
9596
echo "base-sha=$BASE_SHA" >> $GITHUB_OUTPUT
9697
echo "head-sha=$HEAD_SHA" >> $GITHUB_OUTPUT
9798
echo "pr-author=$PR_AUTHOR" >> $GITHUB_OUTPUT
9899
echo "pr-title=$PR_TITLE" >> $GITHUB_OUTPUT
100+
echo "is-pr=$IS_PR" >> $GITHUB_OUTPUT
99101
100102
# Get diff based on event type
101103
if [ "$EVENT_NAME" = "pull_request" ]; then
@@ -318,151 +320,168 @@ jobs:
318320
echo "analysis-success=false" >> $GITHUB_OUTPUT
319321
fi
320322
321-
- name: Post AI Review Comment
323+
- name: Output Analysis Results
322324
uses: actions/github-script@v7
323325
env:
324326
PR_NUMBER: ${{ steps.context-info.outputs.pr-number }}
325327
HEAD_SHA: ${{ steps.context-info.outputs.head-sha }}
326328
PR_AUTHOR: ${{ steps.context-info.outputs.pr-author }}
329+
PR_TITLE: ${{ steps.context-info.outputs.pr-title }}
327330
DIFF_AVAILABLE: ${{ steps.context-info.outputs.diff-available }}
328331
DIFF_TRUNCATED: ${{ steps.context-info.outputs.diff-truncated }}
329332
ANALYSIS_SUCCESS: ${{ steps.ai-analysis.outputs.analysis-success }}
333+
IS_PR: ${{ steps.context-info.outputs.is-pr }}
334+
EVENT_NAME: ${{ github.event_name }}
330335
with:
331336
script: |
332337
const fs = require('fs');
333338
const prNumber = process.env.PR_NUMBER;
334339
const headSha = process.env.HEAD_SHA;
335340
const prAuthor = process.env.PR_AUTHOR;
341+
const prTitle = process.env.PR_TITLE;
336342
const diffAvailable = process.env.DIFF_AVAILABLE === 'true';
337343
const diffTruncated = process.env.DIFF_TRUNCATED === 'true';
338344
const analysisSuccess = process.env.ANALYSIS_SUCCESS === 'true';
345+
const isPR = process.env.IS_PR === 'true';
346+
const eventName = process.env.EVENT_NAME;
339347
340348
// Read AI analysis results
341349
let aiAnalysis = '';
342350
try {
343351
aiAnalysis = fs.readFileSync('ai_analysis_result.txt', 'utf8');
344352
} catch (error) {
345-
aiAnalysis = '## ❌ Analysis Error\n\nFailed to read analysis results. Manual review required.';
353+
aiAnalysis = '## ❌ Analysis Error\\n\\nFailed to read analysis results. Manual review required.';
346354
}
347355
348356
// Create status indicators
349357
let statusIndicators = '';
350358
if (!diffAvailable) {
351-
statusIndicators += '⚠️ **Warning:** No diff content available for analysis\n';
359+
statusIndicators += '⚠️ **Warning:** No diff content available for analysis\\n';
352360
}
353361
if (diffTruncated) {
354-
statusIndicators += '⚠️ **Note:** Large diff was truncated for analysis\n';
362+
statusIndicators += '⚠️ **Note:** Large diff was truncated for analysis\\n';
355363
}
356364
if (!analysisSuccess) {
357-
statusIndicators += '❌ **Alert:** AI analysis encountered issues\n';
365+
statusIndicators += '❌ **Alert:** AI analysis encountered issues\\n';
358366
}
359367
360-
// Determine response type and format appropriately
361-
const analysisType = '${{ steps.analysis-type.outputs.type }}';
362-
363-
let reviewContent;
364-
if (analysisType === 'assistant') {
365-
reviewContent = `
366-
## πŸ€– AI WordPress Assistant Response
367-
368-
Hi @${prAuthor}! I've analyzed your request.
369-
370-
### πŸ“ Expert Analysis & Recommendations
371-
372-
${aiAnalysis}
373-
374-
### πŸ’‘ Available Commands
375-
Try these commands with @gemini-cli:
376-
- \`@gemini-cli review this code\` - Code review and analysis
377-
- \`@gemini-cli suggest improvements\` - Performance and structure suggestions
378-
- \`@gemini-cli check security\` - Security vulnerability analysis
379-
- \`@gemini-cli explain this function\` - Code explanation and documentation
380-
381-
**Analysis Date:** ${new Date().toISOString()}
382-
`;
383-
} else {
384-
reviewContent = `
385-
## πŸ€– AI-Powered Security & Code Review
386-
387-
Hi @${prAuthor}! I've completed an analysis of this ${analysisType === 'pr-review' ? 'pull request' : 'code'}.
388-
389-
### πŸ“Š Review Summary
390-
- **Project:** WordPress Plugin
391-
- **Commit:** \`${headSha.substring(0, 7)}\`
392-
- **WordPress Compatibility:** 6.5+
393-
- **PHP Compatibility:** 7.4+
394-
- **Analysis Status:** ${analysisSuccess ? 'βœ… Completed' : '⚠️ Partial/Failed'}
395-
396-
${statusIndicators}
397-
398-
---
368+
// Create unified content that works for both PRs and pushes
369+
const eventTypeLabel = isPR ? '(Pull Request)' : '(Push Event)';
370+
const analysisTarget = isPR ? 'pull request' : 'code push';
371+
const checklistTitle = isPR ? 'Review Checklist for Maintainers' : 'Code Quality Summary';
372+
const statusEmoji = analysisSuccess ? 'βœ… Completed' : '⚠️ Partial/Failed';
399373
400-
${aiAnalysis}
374+
let reviewContent = '## πŸ€– AI-Powered Code Analysis ' + eventTypeLabel + '\\n\\n';
375+
reviewContent += 'Hi @' + prAuthor + "! I've completed an analysis of this " + analysisTarget + '.\\n\\n';
376+
reviewContent += '### πŸ“Š Analysis Summary\\n';
377+
reviewContent += '- **Event Type:** ' + eventName + '\\n';
378+
reviewContent += '- **Title:** ' + prTitle + '\\n';
379+
reviewContent += '- **Project:** WordPress Plugin\\n';
380+
reviewContent += '- **Commit:** `' + headSha.substring(0, 7) + '`\\n';
381+
reviewContent += '- **WordPress Compatibility:** 6.5+\\n';
382+
reviewContent += '- **PHP Compatibility:** 7.4+\\n';
383+
reviewContent += '- **Analysis Status:** ' + statusEmoji + '\\n\\n';
384+
reviewContent += statusIndicators + '\\n';
385+
reviewContent += '---\\n\\n';
386+
reviewContent += aiAnalysis + '\\n\\n';
387+
reviewContent += '---\\n\\n';
388+
reviewContent += '### πŸ“‹ ' + checklistTitle + '\\n';
389+
reviewContent += '- [ ] Security vulnerabilities addressed\\n';
390+
reviewContent += '- [ ] WordPress coding standards followed\\n';
391+
reviewContent += '- [ ] Performance impact considered\\n';
392+
reviewContent += '- [ ] Documentation updated if needed\\n';
393+
reviewContent += '- [ ] Tests pass (if applicable)\\n\\n';
394+
reviewContent += '> πŸ”„ **Note:** This analysis was performed securely without executing untrusted code.\\n';
395+
reviewContent += '> \\n';
396+
reviewContent += '> 🎯 **Focus Areas:** Security, WordPress Standards, Performance, Code Quality\\n';
401397
402-
---
403398
404-
### πŸ“‹ Review Checklist for Maintainers
405-
- [ ] Security vulnerabilities addressed
406-
- [ ] WordPress coding standards followed
407-
- [ ] Performance impact considered
408-
- [ ] Documentation updated if needed
409-
- [ ] Tests pass (if applicable)
410-
411-
> πŸ”„ **Note:** This analysis was performed securely without executing untrusted code.
412-
>
413-
> 🎯 **Focus Areas:** Security, WordPress Standards, Performance, Code Quality
414-
`;
399+
// Handle output based on event type
400+
if (isPR && prNumber && prNumber !== '' && prNumber !== 'undefined') {
401+
// Post as PR comment
402+
const targetNumber = parseInt(prNumber, 10);
403+
if (!isNaN(targetNumber)) {
404+
console.log('πŸ” Posting analysis as comment on PR #' + targetNumber);
405+
await github.rest.issues.createComment({
406+
issue_number: targetNumber,
407+
owner: context.repo.owner,
408+
repo: context.repo.repo,
409+
body: reviewContent
410+
});
411+
console.log('βœ… PR comment posted successfully');
412+
} else {
413+
console.log('⚠️ Invalid PR number format - logging to workflow output');
414+
console.log('πŸ“„ Analysis Results:');
415+
console.log(reviewContent);
416+
}
417+
} else {
418+
// Log to workflow output for push events or when PR info unavailable
419+
console.log('πŸ“Š Analysis Results for ' + eventName + ' event:');
420+
console.log('='.repeat(60));
421+
console.log(reviewContent);
422+
console.log('='.repeat(60));
423+
console.log('βœ… Analysis logged to workflow output');
415424
}
416-
417-
const targetNumber = prNumber;
418-
419-
await github.rest.issues.createComment({
420-
issue_number: targetNumber,
421-
owner: context.repo.owner,
422-
repo: context.repo.repo,
423-
body: reviewContent
424-
});
425425
426426
- name: Handle Analysis Failure
427427
if: steps.ai-analysis.outputs.analysis-success != 'true'
428428
uses: actions/github-script@v7
429429
env:
430430
PR_NUMBER: ${{ steps.context-info.outputs.pr-number }}
431+
IS_PR: ${{ steps.context-info.outputs.is-pr }}
432+
EVENT_NAME: ${{ github.event_name }}
431433
WORKFLOW_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
432434
with:
433435
script: |
434436
const prNumber = process.env.PR_NUMBER;
437+
const isPR = process.env.IS_PR === 'true';
438+
const eventName = process.env.EVENT_NAME;
435439
const workflowUrl = process.env.WORKFLOW_URL;
436440
437-
// Create an issue for failed analysis
438-
const title = `🚨 AI Analysis Failed for PR #${prNumber}`;
439-
const body = `
440-
## AI Code Analysis Failure
441-
442-
The automated AI code analysis workflow failed for PR #${prNumber}.
441+
console.log('🚨 AI Analysis failed for ' + eventName + ' event');
443442
444-
**Pull Request:** #${prNumber}
445-
**Failure Time:** ${new Date().toISOString()}
446-
**Workflow Run:** ${workflowUrl}
447-
448-
### Possible Causes
449-
- API rate limits or temporary service issues
450-
- Large diff size exceeding analysis limits
451-
- Invalid file formats or encoding issues
452-
- GEMINI_API_KEY configuration problems
453-
454-
### Manual Actions Required
455-
1. πŸ” Review the failed workflow logs for specific error details
456-
2. πŸ”„ Re-run the analysis workflow if it was a temporary issue
457-
3. πŸ› οΈ Check GEMINI_API_KEY secret configuration
458-
4. πŸ‘₯ Proceed with manual code review for the PR
459-
460-
**Note:** This does not necessarily indicate issues with the PR code itself.
461-
`;
443+
if (isPR && prNumber && prNumber !== '' && prNumber !== 'undefined') {
444+
// Create an issue for failed PR analysis
445+
const title = '🚨 AI Analysis Failed for PR #' + prNumber;
446+
const body = `
447+
## AI Code Analysis Failure
448+
449+
The automated AI code analysis workflow failed for PR #${prNumber}.
450+
451+
**Pull Request:** #${prNumber}
452+
**Event Type:** ${eventName}
453+
**Failure Time:** ${new Date().toISOString()}
454+
**Workflow Run:** ${workflowUrl}
462455
463-
await github.rest.issues.create({
464-
owner: context.repo.owner,
465-
repo: context.repo.repo,
456+
457+
### Possible Causes
458+
- API rate limits or temporary service issues
459+
- Large diff size exceeding analysis limits
460+
- Invalid file formats or encoding issues
461+
- GEMINI_API_KEY configuration problems
462+
463+
### Manual Actions Required
464+
1. πŸ” Review the failed workflow logs for specific error details
465+
2. πŸ”„ Re-run the analysis workflow if it was a temporary issue
466+
3. πŸ› οΈ Check GEMINI_API_KEY secret configuration
467+
4. πŸ‘₯ Proceed with manual code review for the PR
468+
469+
**Note:** This does not necessarily indicate issues with the PR code itself.
470+
`;
471+
472+
await github.rest.issues.create({
473+
owner: context.repo.owner,
474+
repo: context.repo.repo,
475+
title: title,
476+
body: body
477+
});
478+
console.log('βœ… Created failure issue for PR #' + prNumber);
479+
} else {
480+
// Log failure for non-PR events
481+
console.log('⚠️ Analysis failed for ' + eventName + ' event');
482+
console.log('πŸ”— Workflow run: ' + workflowUrl);
483+
console.log('πŸ“‹ Manual review recommended - check workflow logs for details');
484+
}
466485
title: title,
467486
body: body,
468487
labels: ['ai-analysis', 'workflow-failure', 'needs-attention']

0 commit comments

Comments
Β (0)