-
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 11cc7af
Showing
4 changed files
with
314 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,38 @@ | ||
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@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.x' | ||
|
||
- name: Install dependencies | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install jq | ||
pip3 install requests | ||
- 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,94 @@ | ||
import os | ||
import subprocess | ||
import json | ||
import requests | ||
|
||
# 环境变量 | ||
url = os.getenv('INPUT_URL') | ||
api_key = os.getenv('INPUT_API_KEY') | ||
model = os.getenv('INPUT_MODEL') | ||
repo_path = os.getenv('INPUT_REPO_PATH') | ||
style = os.getenv('INPUT_STYLE') | ||
language = os.getenv('INPUT_LANGUAGE') | ||
|
||
# 检查必要的环境变量 | ||
if not api_key: | ||
print("api key is required") | ||
exit(-1) | ||
|
||
if not os.path.isdir(repo_path): | ||
print("repo path does not exist") | ||
exit(-2) | ||
|
||
if not language: | ||
print("language is required") | ||
exit(-3) | ||
|
||
if not style: | ||
print("style is required") | ||
exit(-4) | ||
|
||
if not model: | ||
print("model is required") | ||
exit(-5) | ||
|
||
# 文件搜索和语言处理 | ||
language = language.lower() | ||
file_extensions = { | ||
"rust": "*.rs", | ||
"rs": "*.rs", | ||
"golang": "*.go", | ||
"go": "*.go", | ||
"python": "*.py", | ||
"py": "*.py", | ||
"javascript": "*.js", | ||
"js": "*.js", | ||
"typescript": "*.ts", | ||
"ts": "*.ts", | ||
"solidity": "*.sol", | ||
"sol": "*.sol" | ||
} | ||
|
||
if language not in file_extensions: | ||
print("Invalid language") | ||
exit(-6) | ||
|
||
files = subprocess.run(['git', 'ls-files', file_extensions[language]], cwd=repo_path, text=True, capture_output=True).stdout.split() | ||
|
||
# 处理每个文件 | ||
for file in files: | ||
with open(os.path.join(repo_path, file), 'r') as f: | ||
code = f.read() | ||
|
||
system_message = f'You are a {language.title()} code style evaluator that checks code against style guidelines.\nEXAMPLE JSON OUTPUT:\n{{\n "violations": [\n {{ "line number": "reason" }},\n {{ "line number": "reason" }},\n ],\n "score":0\n}}' | ||
|
||
user_message = f"Style Guide: \n\n{style} \n\nCode to evaluate: \n\n{code}" | ||
user_message = json.dumps(user_message) # 处理特殊字符 | ||
|
||
json_payload = { | ||
"response_format": {"type": "json_object"}, | ||
"model": model, | ||
"messages": [ | ||
{"role": "system", "content": system_message}, | ||
{"role": "user", "content": user_message} | ||
], | ||
"stream": False | ||
} | ||
|
||
headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {os.getenv('INPUT_API_KEY')}" | ||
} | ||
|
||
response = requests.post(url, headers=headers, json=json_payload) | ||
response_data = response.json() | ||
|
||
print(file) | ||
if 'error' in response_data: | ||
print(f"Error: {response_data['error']}") | ||
continue | ||
|
||
content = response_data.get('choices', [{}])[0].get('message', {}).get('content', "No content found") | ||
print(content) | ||
|
||
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,118 @@ | ||
#!/bin/bash | ||
|
||
|
||
url=${INPUT_URL:-"https://openrouter.ai/api/v1/chat/completions"} | ||
api_key=${INPUT_API_KEY:-"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"} | ||
model=${INPUT_MODEL:-"deepseek/deepseek-chat:free"} | ||
repo_path=${INPUT_REPO_PATH:-"."} | ||
style=${INPUT_STYLE:-"rust"} | ||
language=${INPUT_LANGUAGE:-"rust"} | ||
|
||
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 | ||
}' | ||
|
||
response=$(curl -s -X POST "$url" \ | ||
-H "Content-Type: application/json" \ | ||
-H "Authorization: Bearer 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" \ | ||
--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 | ||
} |