1
0
mirror of https://github.com/hustcer/deepseek-review.git synced 2026-05-13 05:16:05 +08:00
Files
deepseek-review/nu/review.nu
2025-01-30 11:33:36 +08:00

205 lines
7.2 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env nu
# Author: hustcer
# Created: 2025/01/29 13:02:15
# TODO:
# [√] Deepseek code review for GitHub PRs
# [√] Deepseek code review for local commit changes
# [√] Debug mode
# [√] Output token usage info
# [ ] Add more action outputs
# Description: A script to do code review by deepseek
# Env vars:
# GITHUB_TOKEN: Your GitHub API token
# DEEPSEEK_TOKEN: Your Deepseek API token
# BASE_URL: Deepseek API base URL
# SYSTEM_PROMPT: System prompt message
# USER_PROMPT: User prompt message
# Usage:
# 1. Local: just cr
# 2. Local: just cr -f HEAD~1 --debug
#
# Commonly used exit codes
const ECODE = {
SUCCESS: 0,
OUTDATED: 1,
MISSING_BINARY: 2,
MISSING_DEPENDENCY: 3,
CONDITION_NOT_SATISFIED: 5,
SERVER_ERROR: 6,
INVALID_PARAMETER: 7,
AUTH_FAILED: 8,
}
const DEFAULT_OPTIONS = {
MODEL: 'deepseek-chat',
BASE_URL: 'https://api.deepseek.com',
USER_PROMPT: 'Please review the following code changes:',
SYS_PROMPT: 'You are a professional code review assistant responsible for analyzing code changes in GitHub Pull Requests. Identify potential issues such as code style violations, logical errors, security vulnerabilities, and provide improvement suggestions. Clearly list the problems and recommendations in a concise manner.',
}
# Use Deepseek AI to review code changes
export def --env deepseek-review [
token?: string, # Your Deepseek API token, fallback to DEEPSEEK_TOKEN
--debug(-d), # Debug mode
--repo(-r): string, # GitHub repository name, e.g. hustcer/deepseek-review
--pr-number(-n): string, # 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(-m): string = $DEFAULT_OPTIONS.MODEL, # Model name, deepseek-chat by default
--base-url: string = $DEFAULT_OPTIONS.BASE_URL,
--sys-prompt(-s): string = $DEFAULT_OPTIONS.SYS_PROMPT,
--user-prompt(-u): string = $DEFAULT_OPTIONS.USER_PROMPT,
] {
let is_action = ($env.GITHUB_ACTIONS? == 'true')
let token = $token | default $env.DEEPSEEK_TOKEN?
$env.GH_TOKEN = $gh_token | default $env.GITHUB_TOKEN?
if ($token | is-empty) {
print $'(ansi r)Please provide your Deepseek API token by setting `DEEPSEEK_TOKEN` or passing it as an argument.(ansi reset)'
return
}
if $is_action and not (is-installed gh) {
print $'(ansi r)Please install GitHub CLI from https://cli.github.com (ansi reset)'
return
}
let hint = if not $is_action {
$'🚀 Initiate the code review by Deepseek AI for local changes ...'
} else {
$'🚀 Initiate the code review by Deepseek AI for PR (ansi g)#($pr_number)(ansi reset) in (ansi g)($repo)(ansi reset) ...'
}
print $hint; print -n (char nl)
let diff_content = get-diff --pr-number $pr_number --repo $repo --diff-to $diff_to --diff-from $diff_from
let payload = {
model: $model,
stream: false,
messages: [
{ role: 'system', content: $sys_prompt },
{ role: 'user', content: $"($user_prompt):\n($diff_content)" }
]
}
if $debug {
print $'Code Changes:'; hr-line; print $diff_content
}
let header = [Authorization $'Bearer ($token)']
let url = $'($base_url)/chat/completions'
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)'
exit $ECODE.SERVER_ERROR
return
}
if $debug {
print $'Deepseek Response:'; hr-line
$response | table -e | print
}
if ($response | describe) == 'string' {
print $'❌ Code review failedError: '; hr-line; print $response
exit $ECODE.SERVER_ERROR
return
}
let review = $response | get -i choices.0.message.content
if not $is_action {
print $'Code Review Result:'; hr-line
print $review
} else {
gh pr comment $pr_number --body $review --repo $repo
print $'✅ Code review finishedPR (ansi g)#($pr_number)(ansi reset) review result was posted as a comment.'
}
print $'(char nl)Token Usage Info:'; hr-line
$response.usage | table -e | print
}
# Get the diff content from GitHub PR or local git changes
export def get-diff [
--pr-number: string, # GitHub PR number
--repo: string, # GitHub repository name
--diff-to: string, # Diff to git ref
--diff-from: string, # Diff from git ref
] {
let diff_content = if ($pr_number | is-not-empty) {
if ($repo | is-empty) {
print $'(ansi r)Please provide the GitHub repository name by `--repo` option.(ansi reset)'
exit $ECODE.INVALID_PARAMETER
}
gh pr diff $pr_number --repo $repo | str trim
} else if ($diff_from | is-not-empty) {
if not (has-ref $diff_from) {
print $'(ansi r)The specified git ref ($diff_from) does not exist, please check it again.(ansi reset)'
exit $ECODE.INVALID_PARAMETER
}
if ($diff_to | is-not-empty) and not (has-ref $diff_to) {
print $'(ansi r)The specified git ref ($diff_to) does not exist, please check it again.(ansi reset)'
exit $ECODE.INVALID_PARAMETER
}
git diff $diff_from ($diff_to | default HEAD)
} else if not (git-check (pwd) --check-repo=1) {
print $'Current directory (pwd) is (ansi r)NOT(ansi reset) a git repo, bye...(char nl)'
exit $ECODE.CONDITION_NOT_SATISFIED
} else { git diff }
if ($diff_content | is-empty) {
print $'(ansi g)Nothing to review.(ansi reset)'
exit $ECODE.SUCCESS
}
$diff_content
}
# Check if some command available in current shell
export def is-installed [ app: string ] {
(which $app | length) > 0
}
# Check if git was installed and if current directory is a git repo
export def git-check [
dest: string, # The dest dir to check
--check-repo: int, # Check if current directory is a git repo
] {
cd $dest
if not (is-installed git) {
print $'You should (ansi r)INSTALL git(ansi reset) first to run this command, bye...'
exit $ECODE.MISSING_BINARY
}
# If we don't need repo check just quit now
if ($check_repo != 0) {
let checkRepo = (do -i { git rev-parse --is-inside-work-tree } | complete)
if not ($checkRepo.stdout =~ 'true') {
print $'Current directory is (ansi r)NOT(ansi reset) a git repo, bye...(char nl)'
exit $ECODE.CONDITION_NOT_SATISFIED
}
}
}
# Check if a git repo has the specified ref: could be a branch or tag, etc.
export def has-ref [
ref: string # The git ref to check
] {
let checkRepo = (do -i { git rev-parse --is-inside-work-tree } | complete)
if not ($checkRepo.stdout =~ 'true') { return false }
# Brackets were required here, or error will occur
let parse = (do -i { git rev-parse --verify -q $ref } | complete)
if ($parse.stdout | is-empty) { false } else { true }
}
export def hr-line [
width?: int = 90,
--color(-c): string = 'g',
--blank-line(-b),
--with-arrow(-a),
] {
# Create a line by repeating the unit with specified times
def build-line [
times: int,
unit: string = '-',
] {
0..<$times | reduce -f '' { |i, acc| $unit + $acc }
}
print $'(ansi $color)(build-line $width)(if $with_arrow {'>'})(ansi reset)'
if $blank_line { char nl }
}
alias main = deepseek-review