1
0
mirror of https://github.com/hustcer/deepseek-review.git synced 2026-05-13 05:16:05 +08:00

feat: Add write code review result to file support (#172)

* feat: Add write code review result to file support

* feat: Add write code review result to file support

* feat: Add write code review result to file support

* feat: Add write code review result to file support
This commit is contained in:
Justin Ma
2025-04-11 11:39:47 +08:00
committed by GitHub
parent 9d6bb02502
commit 0298773233
4 changed files with 47 additions and 7 deletions

1
.gitignore vendored
View File

@@ -1,5 +1,6 @@
.env .env
.env.dev .env.dev
.env.local .env.local
review.md
config.yml config.yml
prompts.yaml prompts.yaml

2
cr
View File

@@ -27,6 +27,7 @@ def main [
--exclude(-x): string, # Comma separated file patterns to exclude in the code review --exclude(-x): string, # Comma separated file patterns to exclude in the code review
--temperature(-T): float, # Temperature for the model, between `0` and `2`, default value `1.0` --temperature(-T): float, # Temperature for the model, between `0` and `2`, default value `1.0`
--config(-C): string # Config file path, default to `config.yml` --config(-C): string # Config file path, default to `config.yml`
--output(-o): string, # Output file path
] { ] {
check-nushell check-nushell
@@ -36,6 +37,7 @@ def main [
deepseek-review $token deepseek-review $token
--repo=$repo --repo=$repo
--debug=$debug --debug=$debug
--output=$output
--model=$env.CHAT_MODEL --model=$env.CHAT_MODEL
--base-url=$base_url --base-url=$base_url
--chat-url=$chat_url --chat-url=$chat_url

View File

@@ -4,7 +4,7 @@
# Description: Diff command for DeepSeek-Review # Description: Diff command for DeepSeek-Review
use common.nu [GITHUB_API_BASE, ECODE, git-check, has-ref] use common.nu [GITHUB_API_BASE, ECODE, git-check, has-ref]
use util.nu [generate-include-regex, generate-exclude-regex, prepare-awk] use util.nu [generate-include-regex, generate-exclude-regex, prepare-awk, is-safe-git]
# If the PR title or body contains any of these keywords, skip the review # If the PR title or body contains any of these keywords, skip the review
const IGNORE_REVIEW_KEYWORDS = ['skip review' 'skip cr'] const IGNORE_REVIEW_KEYWORDS = ['skip review' 'skip cr']

View File

@@ -55,6 +55,7 @@ export def --env deepseek-review [
token?: string, # Your DeepSeek API token, fallback to CHAT_TOKEN env var token?: string, # Your DeepSeek API token, fallback to CHAT_TOKEN env var
--debug(-d), # Debug mode --debug(-d), # Debug mode
--repo(-r): string, # GitHub repo name, e.g. hustcer/deepseek-review, or local repo path / alias --repo(-r): string, # GitHub repo name, e.g. hustcer/deepseek-review, or local repo path / alias
--output(-o): string, # Output file path
--pr-number(-n): string, # GitHub PR number --pr-number(-n): string, # GitHub PR number
--gh-token(-k): string, # Your GitHub token, fallback to GITHUB_TOKEN env var --gh-token(-k): string, # Your GitHub token, fallback to GITHUB_TOKEN env var
--diff-to(-t): string, # Diff to git REF --diff-to(-t): string, # Diff to git REF
@@ -73,16 +74,20 @@ export def --env deepseek-review [
$env.config.table.mode = 'psql' $env.config.table.mode = 'psql'
let local_repo = $env.PWD let local_repo = $env.PWD
let write_file = ($output | is-not-empty)
let is_action = ($env.GITHUB_ACTIONS? == 'true') let is_action = ($env.GITHUB_ACTIONS? == 'true')
let stream = if $is_action { false } else { true }
let token = $token | default $env.CHAT_TOKEN? let token = $token | default $env.CHAT_TOKEN?
let repo = $repo | default $env.DEFAULT_GITHUB_REPO? let repo = $repo | default $env.DEFAULT_GITHUB_REPO?
let CHAT_HEADER = [Authorization $'Bearer ($token)'] let CHAT_HEADER = [Authorization $'Bearer ($token)']
let stream = if $is_action or $write_file { false } else { true }
let model = $model | default $env.CHAT_MODEL? | default $DEFAULT_OPTIONS.MODEL let model = $model | default $env.CHAT_MODEL? | default $DEFAULT_OPTIONS.MODEL
let base_url = $base_url | default $env.BASE_URL? | default $DEFAULT_OPTIONS.BASE_URL let base_url = $base_url | default $env.BASE_URL? | default $DEFAULT_OPTIONS.BASE_URL
let url = $chat_url | default $env.CHAT_URL? | default $'($base_url)/chat/completions' let url = $chat_url | default $env.CHAT_URL? | default $'($base_url)/chat/completions'
let max_length = try { $max_length | default ($env.MAX_LENGTH? | default 0 | into int) } catch { 0 } let max_length = try { $max_length | default ($env.MAX_LENGTH? | default 0 | into int) } catch { 0 }
let temperature = try { $temperature | default $env.TEMPERATURE? | default $DEFAULT_OPTIONS.TEMPERATURE | into float } catch { $DEFAULT_OPTIONS.TEMPERATURE } let temperature = try { $temperature | default $env.TEMPERATURE? | default $DEFAULT_OPTIONS.TEMPERATURE | into float } catch { $DEFAULT_OPTIONS.TEMPERATURE }
# Determine output mode
let output_mode = if $is_action { 'action' } else if ($output | is-not-empty) { 'file' } else { 'console' }
validate-temperature $temperature validate-temperature $temperature
let setting = { let setting = {
repo: $repo, repo: $repo,
@@ -155,18 +160,50 @@ export def --env deepseek-review [
exit $ECODE.SERVER_ERROR exit $ECODE.SERVER_ERROR
} }
let result = if ($reason | is-empty) { $review } else { $result } let result = if ($reason | is-empty) { $review } else { $result }
if not $is_action {
print $'Code Review Result:'; hr-line; print $result match $output_mode {
} else { 'action' => {
post-comments-to-pr $repo $pr_number $result post-comments-to-pr $repo $pr_number $result
print $'✅ Code review finishedPR (ansi g)#($pr_number)(ansi reset) review result was posted as a comment.' print $'✅ Code review finishedPR (ansi g)#($pr_number)(ansi reset) review result was posted as a comment.'
}
'file' => { write-review-to-file $output $setting $result $response }
_ => { print $'Code Review Result:'; hr-line; print $result }
} }
if ($response.usage? | is-not-empty) { if ($response.usage? | is-not-empty) {
print $'(char nl)Token Usage:'; hr-line print $'(char nl)Token Usage:'; hr-line
$response.usage? | table -e | print $response.usage? | table -e | print
} }
} }
# Write the code review result to a file
def write-review-to-file [
file: string, # Output file path
setting: record, # Review settings
result: string, # Review result
response: record, # DeepSeek API response
] {
let file = (if not ($file | str ends-with '.md') { $'($file).md' } else { $file })
let token_usage = if ($response.usage? | is-empty) { [] } else {
['## Token Usage', '', ($response.usage? | transpose key val | to md --pretty)]
}
# Generate content sections
let content_sections = [
'# DeepSeek Code Review Result', ''
$"Generated at: (date now | format date '%Y/%m/%d %H:%M:%S')", ''
'## Code Review Settings', ''
($setting | compact-record | reject -i repo | transpose key val | to md --pretty)
'', '## Review Detail', '', $result, '', ...$token_usage
]
try {
$content_sections | str join (char nl) | save --force $file
print $'Code Review Result saved to (ansi g)($file)(ansi reset)'
} catch {|err|
print $'(ansi r)Failed to save review result: (ansi reset)'
$err | table -e | print
}
}
# Validate the DeepSeek API token # Validate the DeepSeek API token
def validate-token [token?: string, --pr-number: string, --repo: string] { def validate-token [token?: string, --pr-number: string, --repo: string] {
if ($token | is-empty) { if ($token | is-empty) {