forked from nisalgunawardhana/Github-models-starter-pro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample-code-review.js
More file actions
359 lines (301 loc) · 11.5 KB
/
sample-code-review.js
File metadata and controls
359 lines (301 loc) · 11.5 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* AUTOMATED CODE REVIEW & ANALYSIS DEMONSTRATION
*
* This file demonstrates GPT-5's advanced code analysis capabilities by providing
* comprehensive code review services. The application:
* 1. Analyzes code files for potential issues and improvements
* 2. Provides security vulnerability assessments
* 3. Suggests performance optimizations and best practices
* 4. Generates refactored code examples
* 5. Offers architectural recommendations and design patterns
* 6. Creates detailed documentation and code explanations
*
* Key concepts demonstrated:
* - Advanced code analysis using GPT-5's enhanced reasoning
* - Multi-dimensional code review (security, performance, maintainability)
* - Language-specific best practices and patterns
* - Automated documentation generation
* - Code quality scoring and improvement recommendations
* - Interactive code review workflow
*
* This showcases GPT-5's ability to understand complex code structures,
* identify potential issues, and provide actionable improvement suggestions
* across multiple programming languages and paradigms.
*/
import ModelClient, { isUnexpected } from "@azure-rest/ai-inference";
import { AzureKeyCredential } from "@azure/core-auth";
import { readFileSync, writeFileSync } from "node:fs";
import { join, extname, basename } from "node:path";
import readline from "readline";
// Configure API credentials and model
const token = process.env["GITHUB_TOKEN"];
const endpoint = "https://models.github.ai/inference";
const model = "openai/gpt-5";
// Create readline interface for user interaction
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Supported file extensions for code analysis
const supportedExtensions = {
'.js': 'JavaScript',
'.ts': 'TypeScript',
'.py': 'Python',
'.java': 'Java',
'.cpp': 'C++',
'.c': 'C',
'.cs': 'C#',
'.go': 'Go',
'.rs': 'Rust',
'.php': 'PHP',
'.rb': 'Ruby',
'.swift': 'Swift',
'.kt': 'Kotlin'
};
// Code review categories and scoring
const reviewCategories = {
security: 'Security & Vulnerability Assessment',
performance: 'Performance & Optimization',
maintainability: 'Code Maintainability & Readability',
bestPractices: 'Best Practices & Conventions',
architecture: 'Architecture & Design Patterns',
testing: 'Testing & Error Handling'
};
// Utility function for user input
function askQuestion(question) {
return new Promise((resolve) => {
rl.question(question, (answer) => {
resolve(answer.trim());
});
});
}
// Initialize AI client
function createClient() {
return ModelClient(
endpoint,
new AzureKeyCredential(token)
);
}
// Comprehensive code analysis
async function analyzeCode(code, language, filename) {
const client = createClient();
const analysisPrompt = `Perform a comprehensive code review and analysis of this ${language} code:
FILENAME: ${filename}
CODE:
\`\`\`${language.toLowerCase()}
${code}
\`\`\`
Provide a detailed analysis covering:
1. **SECURITY ANALYSIS** (Score: /10)
- Identify potential security vulnerabilities
- Check for input validation issues
- Look for injection attack vectors
- Assess authentication/authorization concerns
2. **PERFORMANCE ANALYSIS** (Score: /10)
- Identify performance bottlenecks
- Suggest optimization opportunities
- Check for inefficient algorithms or data structures
- Memory usage considerations
3. **CODE QUALITY** (Score: /10)
- Readability and maintainability assessment
- Code organization and structure
- Naming conventions and clarity
- Code complexity analysis
4. **BEST PRACTICES** (Score: /10)
- Language-specific best practices
- Design patterns usage
- Error handling implementation
- Code documentation quality
5. **IMPROVEMENT SUGGESTIONS**
- Prioritized list of specific improvements
- Code refactoring recommendations
- Architecture suggestions if applicable
6. **OVERALL SCORE**: X/40 with summary
Format your response clearly with markdown headers and provide specific line numbers when referencing issues.`;
const response = await client.path("/chat/completions").post({
body: {
messages: [
{
role: "system",
content: `You are a senior software engineer and code review expert with expertise in ${language} and software best practices. Provide thorough, constructive, and actionable code reviews. Be specific about issues and provide concrete improvement suggestions.`
},
{ role: "user", content: analysisPrompt }
],
model: model,
temperature: 0.3,
max_tokens: 1500
}
});
if (isUnexpected(response)) {
throw response.body.error;
}
return response.body.choices[0].message.content;
}
// Generate refactored code based on analysis
async function generateRefactoredCode(originalCode, language, analysisResults) {
const client = createClient();
const refactorPrompt = `Based on the following code analysis, provide a refactored version of the code that addresses the identified issues:
ORIGINAL CODE:
\`\`\`${language.toLowerCase()}
${originalCode}
\`\`\`
ANALYSIS RESULTS:
${analysisResults}
Please provide:
1. **REFACTORED CODE** - Complete improved version
2. **KEY CHANGES MADE** - Bulleted list of specific improvements
3. **RATIONALE** - Explanation of why each change improves the code
Focus on the most impactful improvements that address security, performance, and maintainability concerns.`;
const response = await client.path("/chat/completions").post({
body: {
messages: [
{
role: "system",
content: `You are an expert software engineer specializing in code refactoring and optimization. Provide clean, efficient, and well-documented refactored code that follows ${language} best practices.`
},
{ role: "user", content: refactorPrompt }
],
model: model,
temperature: 0.2,
max_tokens: 1200
}
});
if (isUnexpected(response)) {
throw response.body.error;
}
return response.body.choices[0].message.content;
}
// Generate comprehensive documentation
async function generateDocumentation(code, language, filename) {
const client = createClient();
const docPrompt = `Generate comprehensive technical documentation for this ${language} code:
FILENAME: ${filename}
CODE:
\`\`\`${language.toLowerCase()}
${code}
\`\`\`
Provide:
1. **OVERVIEW** - Purpose and functionality summary
2. **API DOCUMENTATION** - Functions, classes, and methods with parameters
3. **USAGE EXAMPLES** - Code examples showing how to use the module
4. **DEPENDENCIES** - Required libraries and imports
5. **CONFIGURATION** - Setup and configuration requirements
6. **TROUBLESHOOTING** - Common issues and solutions
Format as professional technical documentation with clear sections and examples.`;
const response = await client.path("/chat/completions").post({
body: {
messages: [
{
role: "system",
content: `You are a technical writer and software documentation expert. Create clear, comprehensive, and user-friendly documentation that helps developers understand and use the code effectively.`
},
{ role: "user", content: docPrompt }
],
model: model,
temperature: 0.4,
max_tokens: 1000
}
});
if (isUnexpected(response)) {
throw response.body.error;
}
return response.body.choices[0].message.content;
}
// Display supported file types
function displaySupportedTypes() {
console.log("\n🔍 Supported File Types for Code Analysis:");
console.log("=".repeat(50));
Object.entries(supportedExtensions).forEach(([ext, lang]) => {
console.log(`${ext.padEnd(6)} - ${lang}`);
});
console.log("=".repeat(50));
}
// Main code review function
export async function main() {
console.log("🔍 Advanced Code Review & Analysis Tool powered by GPT-5");
console.log("Comprehensive code analysis with security, performance, and quality insights\n");
try {
displaySupportedTypes();
const mode = await askQuestion("\nChoose mode:\n1. Analyze file from current directory\n2. Analyze code snippet\nEnter choice (1 or 2): ");
let code, language, filename;
if (mode === "1") {
// File analysis mode
filename = await askQuestion("Enter filename to analyze: ");
try {
code = readFileSync(filename, 'utf-8');
const ext = extname(filename).toLowerCase();
language = supportedExtensions[ext];
if (!language) {
console.log(`⚠️ Warning: ${ext} files are not explicitly supported, but I'll analyze as generic code.`);
language = "Generic";
}
} catch (error) {
console.error(`❌ Error reading file: ${error.message}`);
return;
}
} else {
// Code snippet mode
console.log("\nPaste your code snippet (end with 'END' on a new line):");
let codeLines = [];
let line;
while ((line = await askQuestion("")) !== "END") {
codeLines.push(line);
}
code = codeLines.join('\n');
filename = await askQuestion("Enter language (e.g., JavaScript, Python, Java): ");
language = filename;
filename = `snippet.${filename.toLowerCase()}`;
}
if (code.trim().length === 0) {
console.log("❌ No code provided for analysis.");
return;
}
console.log(`\n🔄 Analyzing ${language} code (${code.length} characters)...\n`);
// Perform comprehensive analysis
console.log("📊 Running comprehensive code analysis...");
const analysis = await analyzeCode(code, language, filename);
console.log("\n" + "=".repeat(80));
console.log("📋 CODE ANALYSIS RESULTS");
console.log("=".repeat(80));
console.log(analysis);
// Ask if user wants refactored code
const wantRefactor = await askQuestion("\n🛠️ Would you like to see refactored code? (y/n): ");
if (wantRefactor.toLowerCase() === 'y') {
console.log("\n🔄 Generating refactored code...");
const refactoredCode = await generateRefactoredCode(code, language, analysis);
console.log("\n" + "=".repeat(80));
console.log("🚀 REFACTORED CODE & IMPROVEMENTS");
console.log("=".repeat(80));
console.log(refactoredCode);
}
// Ask if user wants documentation
const wantDocs = await askQuestion("\n📚 Would you like to generate documentation? (y/n): ");
if (wantDocs.toLowerCase() === 'y') {
console.log("\n🔄 Generating documentation...");
const documentation = await generateDocumentation(code, language, filename);
console.log("\n" + "=".repeat(80));
console.log("📖 GENERATED DOCUMENTATION");
console.log("=".repeat(80));
console.log(documentation);
// Offer to save documentation
const saveDocs = await askQuestion("\n💾 Save documentation to file? (y/n): ");
if (saveDocs.toLowerCase() === 'y') {
const docFilename = `${basename(filename, extname(filename))}_documentation.md`;
writeFileSync(docFilename, documentation);
console.log(`✅ Documentation saved to: ${docFilename}`);
}
}
console.log("\n🎉 Code review complete! GPT-5 has analyzed your code for security, performance, and quality improvements.");
} catch (error) {
console.error("❌ Code analysis error:", error);
} finally {
rl.close();
}
}
// Execute the main function if this file is run directly
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch((err) => {
console.error("The code review sample encountered an error:", err);
process.exit(1);
});
}