mirror of
https://github.com/hustcer/deepseek-review.git
synced 2026-05-13 05:16:05 +08:00
feat: Add local code changes code review support
This commit is contained in:
68
nu/review.nu
68
nu/review.nu
@@ -4,11 +4,15 @@
|
||||
# TODO:
|
||||
# [√] Deepseek code reivew for Github PRs
|
||||
# [√] Deepseek code reivew for local commit changes
|
||||
# [√] Debug mode
|
||||
# [√] Output usage info
|
||||
# Description: A script to do code review by deepseek
|
||||
# Env vars:
|
||||
# GITHUB_TOKEN: Your Github API token
|
||||
# DEEPSEEK_TOKEN: Your Deepseek API token
|
||||
# Usage:
|
||||
# 1. Local: just cr
|
||||
# 2. Local: just cr -f HEAD~1 --debug
|
||||
#
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
@@ -18,12 +22,15 @@ const DEFAULT_OPTIONS = {
|
||||
SYS_PROMPT: '你是一个专业的代码审查助手,负责分析GitHub Pull Request的代码变更,指出潜在的问题,如代码风格、逻辑错误、安全漏洞,并提供改进建议。请用简洁明了的语言列出问题及建议。',
|
||||
}
|
||||
|
||||
# Use Deepseek AI to review code changes
|
||||
export def deepseek-review [
|
||||
token?: string, # Your Deepseek API token, fallback to DEEPSEEK_TOKEN
|
||||
--diff: string, # Diff content, e.g. `git diff` output
|
||||
--debug(-d), # Debug mode
|
||||
--repo: string, # Github repository name, e.g. hustcer/deepseek-review
|
||||
--pr-number: int, # Github PR number
|
||||
--gh-token: string, # Your Github token, GITHUB_TOKEN by default
|
||||
--diff-to(-t): string, # Diff to git ref
|
||||
--diff-from(-f): string, # Diff from git ref
|
||||
--model: string = $DEFAULT_OPTIONS.MODEL, # Model name, deepseek-chat by default
|
||||
--base-url: string = $DEFAULT_OPTIONS.BASE_URL,
|
||||
--sys-prompt: string = $DEFAULT_OPTIONS.SYS_PROMPT,
|
||||
@@ -32,38 +39,63 @@ export def deepseek-review [
|
||||
|
||||
let token = $token | default $env.DEEPSEEK_TOKEN?
|
||||
if ($token | is-empty) {
|
||||
print 'Please provide your Deepseek API token by setting DEEPSEEK_TOKEN or passing it as an argument.'
|
||||
print $'(ansi r)Please provide your Deepseek API token by setting `DEEPSEEK_TOKEN` or passing it as an argument.(ansi reset)'
|
||||
return
|
||||
}
|
||||
$env.GITHUB_TOKEN = $gh_token | default $env.GITHUB_TOKEN?
|
||||
let diff_content = if ($diff | is-empty) {
|
||||
let diff_content = if ($pr_number | is-not-empty) {
|
||||
gh pr diff $pr_number --repo $repo | str trim
|
||||
} else { $diff }
|
||||
} else if ($diff_from | is-not-empty) {
|
||||
git diff $diff_from ($diff_to | default HEAD)
|
||||
} else { git diff }
|
||||
if ($diff_content | is-empty) {
|
||||
print $'(ansi r)Please provide the diff content by passing `--diff` or `--pr-number`.(ansi reset)'
|
||||
return
|
||||
}
|
||||
let payload = {
|
||||
model: $model,
|
||||
stream: 'false',
|
||||
stream: false,
|
||||
messages: [
|
||||
{ role: 'system', content: $sys_prompt },
|
||||
{ role: 'user', content: $"($user_prompt):\n($diff_content)" }
|
||||
]
|
||||
}
|
||||
print $'🚀 Start code review for PR #($pr_number) in ($repo) by Deepseek AI ...'; hr-line
|
||||
let hint = if ($env.GITHUB_ACTIONS? != 'true') {
|
||||
$'🚀 Start code review for local changes by Deepseek AI ...'
|
||||
} else {
|
||||
$'🚀 Start code review for PR #($pr_number) in ($repo) by Deepseek AI ...'
|
||||
}
|
||||
|
||||
print $hint; print -n (char nl)
|
||||
if $debug {
|
||||
print $'Code Changes:'; hr-line; print $diff_content
|
||||
}
|
||||
let header = [Authorization $'Bearer ($token)']
|
||||
let url = $'($base_url)/chat/completions'
|
||||
let response = http post -H $header -t application/json $url $payload
|
||||
let review = $response | get choices.0.message.content
|
||||
if ($response | get status) != 200 {
|
||||
print $'❌ Code review failed!Error: ($response | get content)'
|
||||
print $'(char nl)(ansi g)Waiting for response from Deepseek ...(ansi reset)'
|
||||
let response = http post -e -H $header -t application/json $url $payload
|
||||
if ($response | is-empty) {
|
||||
print $'(ansi r)Oops, No response returned from Deepseek API.(ansi reset)'
|
||||
return
|
||||
}
|
||||
gh pr comment $pr_number --body $review --repo $repo
|
||||
print $'✅ Code review finished!PR #($pr_number) review result was posted as a comment.'
|
||||
}
|
||||
|
||||
# If current host is Windows
|
||||
export def windows? [] {
|
||||
# Windows / Darwin / Linux
|
||||
(sys host | get name) == 'Windows'
|
||||
if $debug {
|
||||
print $'Deepseek Response:'; hr-line
|
||||
$response | table -e | print
|
||||
}
|
||||
if ($response | describe) == 'string' {
|
||||
print $'❌ Code review failed!Error: '; hr-line; print $response
|
||||
return
|
||||
}
|
||||
let review = $response | get -i choices.0.message.content
|
||||
if ($env.GITHUB_ACTIONS? != 'true') {
|
||||
print $'Code review result:'; hr-line
|
||||
print $review
|
||||
} else {
|
||||
gh pr comment $pr_number --body $review --repo $repo
|
||||
print $'✅ Code review finished!PR #($pr_number) review result was posted as a comment.'
|
||||
}
|
||||
print '(char nl)Usage Info:'; hr-line
|
||||
$response.usage | table -e | print
|
||||
}
|
||||
|
||||
# Check if some command available in current shell
|
||||
|
||||
Reference in New Issue
Block a user