-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
shaorongqiang
committed
Feb 12, 2025
1 parent
9eb3092
commit 279286c
Showing
3 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: 'Code Style Checker' | ||
description: 'Check code style for different programming languages.' | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
run-script: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Install dependencies | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install jq | ||
- name: Run the script | ||
env: | ||
INPUT_URL: 'https://openrouter.ai/api/v1/chat/completions' | ||
INPUT_API_KEY: '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' | ||
INPUT_MODEL: 'deepseek/deepseek-chat:free' | ||
INPUT_REPO_PATH: ${{ github.workspace }} | ||
INPUT_STYLE: 'rust' # 根据需要修改 | ||
INPUT_LANGUAGE: 'rust' # 根据需要修改 | ||
run: bash ./checker.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#!/bin/bash | ||
|
||
|
||
url=${INPUT_URL} | ||
api_key=${INPUT_API_KEY} | ||
model=${INPUT_MODEL} | ||
repo_path=${INPUT_REPO_PATH} | ||
style=${INPUT_STYLE} | ||
language=${INPUT_LANGUAGE} | ||
|
||
echo "url: $url" | ||
echo "api_key: $api_key" | ||
echo "model: $model" | ||
echo "repo_path: $repo_path" | ||
echo "style: $style" | ||
echo "language: $language" | ||
|
||
if [ ! $api_key ]; then | ||
echo "api key is required" | ||
exit -1 | ||
fi | ||
|
||
if [ ! -d "$repo_path" ]; then | ||
echo "repo path does not exist" | ||
exit -2 | ||
fi | ||
|
||
if [ ! $language ]; then | ||
echo "language is required" | ||
exit -3 | ||
fi | ||
|
||
if [ ! $style ]; then | ||
echo "style is required" | ||
exit -4 | ||
fi | ||
|
||
if [ ! $model ]; then | ||
echo "model is required" | ||
exit -5 | ||
fi | ||
|
||
files="" | ||
language=$(echo $language | tr '[:upper:]' '[:lower:]') | ||
case $language in | ||
"rust"|"rs") | ||
language="Rust" | ||
files=$(git ls-files '*.rs') | ||
;; | ||
"golang"|"go") | ||
language="Golang" | ||
files=$(git ls-files '*.go') | ||
;; | ||
"python"|"py") | ||
language="Python" | ||
files=$(git ls-files '*.py') | ||
;; | ||
"javascript"|"js") | ||
language="JavaScript" | ||
files=$(git ls-files '*.js') | ||
;; | ||
"typescript"|"ts") | ||
language="TypeScript" | ||
files=$(git ls-files '*.ts') | ||
;; | ||
"solidity"|"sol") | ||
language="Solidity" | ||
files=$(git ls-files '*.sol') | ||
;; | ||
*) | ||
echo "Invalid language" | ||
exit -6 | ||
;; | ||
esac | ||
|
||
for file in ${files[*]} | ||
do | ||
code=$(cat "$file") | ||
system_message='You are a '${language}' code style evaluator that checks code against style guidelines. | ||
EXAMPLE JSON OUTPUT: | ||
{ | ||
\"violations\": [ | ||
{ \"line number\": \"reason\" }, | ||
{ \"line number\": \"reason\" }, | ||
], | ||
\"score\":0 | ||
}' | ||
|
||
user_message="Style Guide: | ||
${style} | ||
Code to evaluate: | ||
${code}" | ||
user_message=$(echo $user_message | sed 's/{/\\{/g' | sed 's/}/\\}/g' | sed 's/\"/\\"/g' | sed 's/\\\\"/\\"/g') | ||
|
||
json='{ | ||
"response_format": {"type": "json_object"}, | ||
"model": "'$model'", | ||
"messages": [ | ||
{"role": "system", "content": "'$system_message'"}, | ||
{"role": "user", "content": "'$user_message'"} | ||
], | ||
"stream": false | ||
}' | ||
|
||
echo "curl -s -X POST \"$url\" \ | ||
-H \"Content-Type: application/json\" \ | ||
-H \"Authorization: Bearer $api_key\" \ | ||
--data \"${json}\"" | ||
|
||
response=$(curl -s -X POST "$url" \ | ||
-H "Content-Type: application/json" \ | ||
-H "Authorization: Bearer $api_key" \ | ||
--data "${json}") | ||
|
||
echo $file | ||
# 检查是否有错误 | ||
error=$(echo $response | jq -r '.error // empty') | ||
if [ ! -z "$error" ]; then | ||
echo "Error: $error" | ||
continue | ||
fi | ||
|
||
# 提取内容 | ||
content=$(echo $response | jq -r '.choices[0].message.content // "No content found"') | ||
echo "$content" | ||
done | ||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#![deny(warnings)] | ||
|
||
mod checker; | ||
mod common; | ||
|
||
use anyhow::Result; | ||
use clap::Parser; | ||
use common::{check_code, get_code_files, get_language}; | ||
|
||
#[derive(Parser)] | ||
pub struct Command { | ||
#[clap(short, long)] | ||
url: String, | ||
|
||
#[clap(short, long)] | ||
model: String, | ||
|
||
#[clap(short, long)] | ||
api_key: String, | ||
|
||
#[clap(short, long)] | ||
style: String, | ||
|
||
#[clap(short, long)] | ||
language: String, | ||
|
||
#[clap(short, long)] | ||
repo_local_path: String, | ||
} | ||
|
||
impl Command { | ||
pub async fn execute(self) -> Result<()> { | ||
let language = match get_language(&self.language) { | ||
Some(language) => language, | ||
None => return Err(anyhow::anyhow!("Invalid language")), | ||
}; | ||
|
||
let files = get_code_files(&self.repo_local_path, &language)?; | ||
|
||
for file in files { | ||
match check_code( | ||
&file, | ||
&self.style, | ||
language.clone(), | ||
&self.url, | ||
&self.api_key, | ||
&self.model, | ||
) | ||
.await | ||
{ | ||
Ok(content) => println!("\n{}: \n{}", file.display(), content), | ||
Err(e) => println!("\n{}: \n{}", file.display(), e), | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
env_logger::init(); | ||
Command::parse().execute().await | ||
} |