-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
188 lines (155 loc) · 5.61 KB
/
index.js
File metadata and controls
188 lines (155 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env node
/**
* AI Code Reviewer CLI
* Main entry point for the AI code review workflow
*/
const { Command } = require('commander');
const WebhookHandler = require('./src/github/webhookHandler');
const ConfigManager = require('./src/config/index');
const ConfigValidator = require('./src/config/validator');
const logger = require('./src/utils/logger');
const program = new Command();
program
.name('ai-code-reviewer')
.description('AI-powered code review for GitHub pull requests')
.version('1.0.22');
// Review command for GitHub Actions
program
.command('review')
.description('Run AI code review on a pull request')
.option('--pr <number>', 'Pull request number', parseInt)
.option('--repo <repo>', 'Repository name (owner/repo)')
.option('--config <file>', 'Configuration file path', 'ai-review-config.json')
.action(async (options) => {
try {
logger.info('Starting AI code review...');
// Load and validate configuration
const configManager = new ConfigManager();
configManager.configFile = options.config;
const config = configManager.loadConfig();
const validator = new ConfigValidator();
const validation = validator.validate(config);
if (!validation.isValid) {
logger.error('Configuration validation failed:');
validation.errors.forEach(error => logger.error(`- ${error}`));
process.exit(1);
}
// Validate environment
const envValidation = validator.validateEnvironment();
if (!envValidation.isValid) {
logger.error('Environment validation failed:');
envValidation.errors.forEach(error => logger.error(`- ${error}`));
process.exit(1);
}
// Create webhook handler and simulate PR event
const webhookHandler = new WebhookHandler(config);
// Simulate webhook payload for GitHub Actions
const payload = {
action: 'opened',
pull_request: {
number: options.pr,
title: `PR #${options.pr}`,
user: { login: 'BugBeaver' }
},
repository: {
full_name: options.repo,
owner: { login: options.repo.split('/')[0] },
name: options.repo.split('/')[1]
}
};
const result = await webhookHandler.handleWebhook(payload);
if (result.success) {
logger.info('AI code review completed successfully');
process.exit(0);
} else {
logger.error('AI code review failed:', result.error);
process.exit(1);
}
} catch (error) {
logger.error('Review command failed:', error);
process.exit(1);
}
});
// Webhook command for direct webhook handling
program
.command('webhook')
.description('Handle GitHub webhook payload')
.option('--payload <json>', 'Webhook payload as JSON string')
.option('--config <file>', 'Configuration file path', 'ai-review-config.json')
.action(async (options) => {
try {
logger.info('Processing webhook...');
// Load configuration
const configManager = new ConfigManager();
configManager.configFile = options.config;
const config = configManager.loadConfig();
// Parse payload
const payload = JSON.parse(options.payload);
// Create webhook handler
const webhookHandler = new WebhookHandler(config);
const result = await webhookHandler.handleWebhook(payload);
console.log(JSON.stringify(result, null, 2));
} catch (error) {
logger.error('Webhook command failed:', error);
console.error(JSON.stringify({ success: false, error: error.message }, null, 2));
process.exit(1);
}
});
// Validate command
program
.command('validate')
.description('Validate configuration')
.option('--config <file>', 'Configuration file path', 'ai-review-config.json')
.action(async (options) => {
try {
logger.info('Validating configuration...');
// Load configuration
const configManager = new ConfigManager();
configManager.configFile = options.config;
const config = configManager.loadConfig();
// Validate configuration
const validator = new ConfigValidator();
const validation = validator.validate(config);
const envValidation = validator.validateEnvironment();
const allErrors = [...validation.errors, ...envValidation.errors];
const allWarnings = [...validation.warnings, ...envValidation.warnings];
if (allErrors.length > 0) {
console.log('❌ Validation failed:');
allErrors.forEach(error => console.log(` - ${error}`));
process.exit(1);
} else {
console.log('✅ Configuration is valid');
if (allWarnings.length > 0) {
console.log('⚠️ Warnings:');
allWarnings.forEach(warning => console.log(` - ${warning}`));
}
process.exit(0);
}
} catch (error) {
logger.error('Validate command failed:', error);
process.exit(1);
}
});
// Test Discord webhook
program
.command('test-discord')
.description('Test Discord webhook connectivity')
.option('--webhook-url <url>', 'Discord webhook URL')
.action(async (options) => {
try {
const DiscordService = require('./src/services/discordService');
const discord = new DiscordService(options.webhookUrl);
const success = await discord.testWebhook();
if (success) {
console.log('✅ Discord webhook test successful');
process.exit(0);
} else {
console.log('❌ Discord webhook test failed');
process.exit(1);
}
} catch (error) {
logger.error('Test Discord command failed:', error);
process.exit(1);
}
});
program.parse();