11use crate :: common:: get_combined_instructions;
22use crate :: config:: Config ;
33use crate :: context:: {
4- ChangeType , CommitContext , GeneratedMessage , GeneratedReview , ProjectMetadata , RecentCommit ,
5- StagedFile ,
4+ ChangeType , CommitContext , GeneratedMessage , GeneratedReview , ProjectMetadata ,
5+ QualityDimension , RecentCommit , StagedFile ,
66} ;
77use crate :: gitmoji:: { apply_gitmoji, get_gitmoji_list} ;
88
@@ -282,6 +282,7 @@ pub fn process_commit_message(message: String, use_gitmoji: bool) -> String {
282282}
283283
284284/// Creates a system prompt for code review generation
285+ #[ allow( clippy:: too_many_lines) ]
285286pub fn create_review_system_prompt ( config : & Config ) -> anyhow:: Result < String > {
286287 let review_schema = schemars:: schema_for!( GeneratedReview ) ;
287288 let review_schema_str = serde_json:: to_string_pretty ( & review_schema) ?;
@@ -317,77 +318,15 @@ pub fn create_review_system_prompt(config: &Config) -> anyhow::Result<String> {
317318 - Acknowledge good practices and improvements
318319
319320 5. Analyze the following specific dimensions of code quality:
321+ " ) ;
320322
321- **Unnecessary Complexity**
322- - Overly complex algorithms or functions
323- - Unnecessary abstraction layers
324- - Convoluted control flow
325- - Functions/methods that are too long or have too many parameters
326- - Nesting levels that are too deep
327-
328- **Poor Abstractions**
329- - Inappropriate use of design patterns
330- - Missing abstractions where needed
331- - Leaky abstractions that expose implementation details
332- - Overly generic abstractions that add complexity
333- - Unclear separation of concerns
334-
335- **Unintended Code Deletion**
336- - Critical functionality removed without replacement
337- - Incomplete removal of deprecated code
338- - Breaking changes to public APIs
339- - Removed error handling or validation
340- - Missing edge case handling present in original code
341-
342- **Hallucinated Components**
343- - References to non-existent functions, classes, or modules
344- - Assumptions about available libraries or APIs
345- - Inconsistent or impossible behavior expectations
346- - References to frameworks or patterns not used in the project
347- - Creation of interfaces that don't align with the codebase
348-
349- **Style Inconsistencies**
350- - Deviation from project coding standards
351- - Inconsistent naming conventions
352- - Inconsistent formatting or indentation
353- - Inconsistent comment styles or documentation
354- - Mixing of different programming paradigms
355-
356- **Security Vulnerabilities**
357- - Injection vulnerabilities (SQL, Command, etc.)
358- - Insecure data handling or storage
359- - Authentication or authorization flaws
360- - Exposure of sensitive information
361- - Unsafe dependencies or API usage
362-
363- **Performance Issues**
364- - Inefficient algorithms or data structures
365- - Unnecessary computations or operations
366- - Resource leaks (memory, file handles, etc.)
367- - Excessive network or disk operations
368- - Blocking operations in asynchronous code
369-
370- **Code Duplication**
371- - Repeated logic or functionality
372- - Copy-pasted code with minor variations
373- - Duplicate functionality across different modules
374- - Redundant validation or error handling
375- - Parallel hierarchies or structures
376-
377- **Incomplete Error Handling**
378- - Missing try-catch blocks for risky operations
379- - Overly broad exception handling
380- - Swallowed exceptions without proper logging
381- - Unclear error messages or codes
382- - Inconsistent error recovery strategies
383-
384- **Test Coverage Gaps**
385- - Missing unit tests for critical functionality
386- - Uncovered edge cases or error paths
387- - Brittle tests that make inappropriate assumptions
388- - Missing integration or system tests
389- - Tests that don't verify actual requirements
323+ // Add each dimension's description
324+ for dimension in QualityDimension :: all ( ) {
325+ prompt. push_str ( dimension. description ( ) ) ;
326+ }
390327
328+ prompt. push_str (
329+ "
391330 For each dimension, identify specific issues with:
392331 - A severity level (Critical, High, Medium, Low)
393332 - Line number references or specific location in the code
@@ -399,45 +338,70 @@ pub fn create_review_system_prompt(config: &Config) -> anyhow::Result<String> {
399338
400339 prompt. push_str ( get_combined_instructions ( config) . as_str ( ) ) ;
401340
402- prompt. push_str ( "
341+ prompt. push_str (
342+ "
403343 Your response must be a valid JSON object with the following structure:
404344
405345 {
406346 \" summary\" : \" A brief summary of the changes and their quality\" ,
407347 \" code_quality\" : \" An assessment of the overall code quality\" ,
408348 \" suggestions\" : [\" Suggestion 1\" , \" Suggestion 2\" , ...],
409349 \" issues\" : [\" Issue 1\" , \" Issue 2\" , ...],
410- \" positive_aspects\" : [\" Positive aspect 1\" , \" Positive aspect 2\" , ...],
411- \" complexity\" : {
350+ \" positive_aspects\" : [\" Positive aspect 1\" , \" Positive aspect 2\" , ...]," ,
351+ ) ;
352+
353+ // Add each dimension to the JSON schema
354+ let mut is_first = true ;
355+ for dimension in QualityDimension :: all ( ) {
356+ let dim_name = match dimension {
357+ QualityDimension :: Complexity => "complexity" ,
358+ QualityDimension :: Abstraction => "abstraction" ,
359+ QualityDimension :: Deletion => "deletion" ,
360+ QualityDimension :: Hallucination => "hallucination" ,
361+ QualityDimension :: Style => "style" ,
362+ QualityDimension :: Security => "security" ,
363+ QualityDimension :: Performance => "performance" ,
364+ QualityDimension :: Duplication => "duplication" ,
365+ QualityDimension :: ErrorHandling => "error_handling" ,
366+ QualityDimension :: Testing => "testing" ,
367+ QualityDimension :: BestPractices => "best_practices" ,
368+ } ;
369+
370+ if is_first {
371+ is_first = false ;
372+ prompt. push_str ( & format ! (
373+ "
374+ \" {dim_name}\" : {{
412375 \" issues_found\" : true/false,
413376 \" issues\" : [
414- {
377+ {{
415378 \" description\" : \" Brief description\" ,
416379 \" severity\" : \" Critical/High/Medium/Low\" ,
417380 \" location\" : \" Line number or code location\" ,
418381 \" explanation\" : \" Detailed explanation of the issue\" ,
419382 \" recommendation\" : \" Specific suggestion for improvement\"
420- },
383+ }} ,
421384 ...
422385 ]
423- },
424- \" abstraction\" : { ... similar structure ... },
425- \" deletion\" : { ... similar structure ... },
426- \" hallucination\" : { ... similar structure ... },
427- \" style\" : { ... similar structure ... },
428- \" security\" : { ... similar structure ... },
429- \" performance\" : { ... similar structure ... },
430- \" duplication\" : { ... similar structure ... },
431- \" error_handling\" : { ... similar structure ... },
432- \" testing\" : { ... similar structure ... }
386+ }}"
387+ ) ) ;
388+ } else {
389+ prompt. push_str ( & format ! (
390+ ",
391+ \" {dim_name}\" : {{ ... similar structure ... }}"
392+ ) ) ;
393+ }
394+ }
395+
396+ prompt. push_str ( "
433397 }
434398
435399 Follow these steps to generate the code review:
436400
437401 1. Analyze the provided context, including staged changes and project metadata.
438402 2. Evaluate the code quality, looking for potential issues, improvements, and good practices.
439403 3. Create a concise summary of the changes and their quality.
440- 4. Analyze each of the 10 code quality dimensions.
404+ 4. Analyze each of the code quality dimensions.
441405 5. For each dimension with issues, list them with appropriate severity, location, explanation, and recommendation.
442406 6. Provide overall suggestions for improvements.
443407 7. Identify specific issues found across all dimensions.
0 commit comments