Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Skill Code Review

Skill Code Review

This project builds a code review Skill that automatically analyzes code quality, style issues, and potential bugs after the user uploads code files.

Goal: master toolchain integration and the generation of structured issue reports.


Project Requirements Analysis

Function Supported Languages Tool
Syntax Checking Python py_compile (built-in)
Code Style Checking Python flake8
Code Complexity Analysis Python radon
JavaScript Checking JS/TS eslint(npx)
Comprehensive Issue Report all Custom Aggregation Script

Directory structure



code-reviewer/

├── SKILL.md

└── scripts/

    ├── requirements.txt

    ├── check_python.py

    ├── check_js.py

    └── generate_report.py


Python Code Checking Script

Example

File path: scripts/check_python.py

import

subprocess

import

sys

import

json

import

os

def

ensure_tools

(

)

:

   

"""Ensure checking tools are installed"""

   

for

pkg

in

[

"flake8"

,

"radon"

]

:

       

try

:

           

import

(

pkg.

replace

(

"-"

,

"_"

)

)

       

except

ImportError

:

           

subprocess

.

run

(

[

sys

.

executable

,

"-m"

,

"pip"

,

"install"

,

pkg

,

                           

"--break-system-packages"

,

"--quiet"

]

)

def

check_syntax

(

file_path:

str

)

dict

:

   

"""Check Python syntax"""

    result

=

subprocess

.

run

(

       

[

sys

.

executable

,

"-m"

,

"py_compile"

,

file_path

]

,

        capture_output

=

True

,

text

=

True

   

)

   

if

result.

returncode

==

0

:

       

return

{

"passed"

:

True

,

"errors"

:

[

]

}

   

return

{

       

"passed"

:

False

,

       

"errors"

:

[

result.

stderr

.

strip

(

)

]

   

}

def

check_style

(

file_path:

str

)

list

:

   

"""Run flake8 to check code style, return a list of issues"""

    result

=

subprocess

.

run

(

       

[

"python"

,

"-m"

,

"flake8"

,

file_path

,

         

"--max-line-length=100"

,

         

"--format=%(row)d:%(col)d %(code)s %(text)s"

]

,

        capture_output

=

True

,

text

=

True

   

)

    issues

=

[

]

   

for

line

in

result.

stdout

.

strip

(

)

.

splitlines

(

)

:

        parts

=

line.

split

(

" "

,

2

)

       

if

len

(

parts

)

=

3

:

            loc

,

code

,

text

=

parts

            row

,

col

=

loc.

split

(

":"

)

            issues.

append

(

{

               

"line"

:

int

(

row

)

,

"col"

:

int

(

col

)

,

               

"code"

:

code

,

   

"message"

: text

           

}

)

   

return

issues

def

check_complexity

(

file_path:

str

)

list

:

   

"""Use radon to analyze function complexity, return functions with high complexity"""

    result

=

subprocess

.

run

(

       

[

"python"

,

"-m"

,

"radon"

,

"cc"

,

file_path

,

"-s"

,

"--json"

]

,

        capture_output

=

True

,

text

=

True

   

)

   

try

:

        data      

=

json.

loads

(

result.

stdout

)

        functions

=

data.

get

(

file_path

,

[

]

)

       

Only return functions with complexity grade C or above (complexity > 10)

       

return

[

           

{

"name"

: f

[

"name"

]

,

"complexity"

: f

[

"complexity"

]

,

             

"rank"

: f

[

"rank"

]

,

"line"

: f

[

"lineno"

]

}

           

for

f

in

functions

           

if

f.

get

(

"rank"

,

"A"

)

in

(

"C"

,

"D"

,

"E"

,

"F"

)

       

]

   

except

Exception

:

       

return

[

]

def

review_python

(

file_path:

str

)

dict

:

   

"""Perform a complete Python code review"""

   

if

not

os

.

path

.

exists

(

file_path

)

:

       

return

{

"status"

:

"error"

,

"message"

: f

"File does not exist: {file_path}"

}

    ensure_tools

(

)

    syntax    

=

check_syntax

(

file_path

)

    style      

=

check_style

(

file_path

)

if

syntax

[

"passed"

]

else

[

]

    complexity

=

check_complexity

(

file_path

)

if

syntax

[

"passed"

]

else

[

]

   

Calculate overall rating

    error_count  

=

len

(

[

i

for

i

in

style

if

i

[

"code"

]

.

startswith

(

"E"

)

]

)

    warning_count

=

len

(

[

i

for

i

in

style

if

i

[

"code"

]

.

startswith

(

"W"

)

]

)

   

if

not

syntax

[

"passed"

]

:

        grade

=

"F"

   

elif

error_count

10

:

        grade

=

"C"

   

elif

error_count

3

:

        grade

=

"B"

   

else

:

        grade

=

"A"

   

return

{

       

"status"

:          

"success"

,

       

"file"

:            

os

.

path

.

basename

(

file_path

)

,

       

"grade"

:           grade

,

       

"syntax_ok"

:       syntax

[

"passed"

]

,

       

"syntax_errors"

:   syntax

[

"errors"

]

,

       

"style_issues"

:    style

[

:

20

]

,

         

Show at most 20 items

       

"style_count"

:    

len

(

style

)

,

       

"error_count"

:     error_count

,

       

"warning_count"

:   warning_count

,

       

"complex_functions"

: complexity

   

}

if

name

==

"main"

:

    result

=

review_python

(

sys

.

argv

[

1

]

if

len

(

sys

.

argv

)

1

else

""

)

   

print

(

json.

dumps

(

result

,

ensure_ascii

=

False

,

indent

=

2

)

)

Output:



{

  "status": "success",

  "file": "runoob_app.py",

  "grade": "B",

  "syntax_ok": true,

  "style_issues": [

    {"line": 12, "col": 1, "code": "E302", "message": "expected 2 blank lines"},

    {"line": 34, "col": 80, "code": "E501", "message": "line too long (103 > 100)"}

  ],

  "style_count": 5,

  "error_count": 3,

  "warning_count": 2,

  "complex_functions": []

}


Full Content of SKILL.md



---

name: code-reviewer

version: 1.0.0

description: >

  自动审查用户上传的代码文件,检查语法错误、风格问题和代码复杂度,

  生成带评级的问题报告并给出改进建议。当用户需要代码审查、

  检查代码质量、查找 Bug、评估代码规范时触发。

---



# Code Review Assistant



## Execution Flow



### Step 1: Identify code files



获取用户上传的代码文件路径,识别编程语言(根据扩展名):

- .py → Python 审查

- .js / .ts → JavaScript/TypeScript 审查

- 其他格式 → 告知暂不支持,列出支持的格式



### Step 2: Run automated checks



Running a Python file:

```bash

cd scripts/

python check_python.py <文件路径>

Step 3: Generate and Interpret the Report

Based on the JSON output, present the results in a human-friendly way:

  1. Overall rating: A (excellent) / B (good) / C (needs improvement) / F (syntax error)
  2. Issue summary table: line number, problematic code, description
  3. Top 3 Priority Fixes: select the 3 most serious issues and provide fix suggestions and examples

If the file has no issues, give a brief positive comment:

Code check passed! X lines scanned in total, no syntax errors or obvious style issues found.



---



## Test Cases



## 实例



# File path: scripts/tests/test_sample.py (sample file for testing Skill review)



  

# Intentionally including some common issues to verify the review function



  

  

import



os



,



sys



 



# E401: Multiple imports on the same line (style issue)



  

import



json



  

  

def



processData



(



data



)



:  



# N802: Function names should be snake_case



  

    x



=



1



 



# F841: Local variable x assigned but never used



  

    result



=



[



]



 



# E225: missing whitespace around assignment operator



  

   



for



i



in



data:



  

       



if



i



>



0



:



  

            result.



append



(



i



)



  

       



elif



i



==



0



:



  

           



pass



  

       



else



:



  

            result.



append



(



-i



)



  

   



return



result



  

  

class



myClass:  



# N801: Class names should be CamelCase



  

   



pass



  

Run review:



cd scripts/ python check_python.py tests/test_sample.py



输出:



{ "status": "success", "file": "test_sample.py", "grade": "B", "syntax_ok": true, "style_count": 5, "error_count": 4, "warning_count": 1, "complex_functions": [] }