Skip to content

Commit 11cc7af

Browse files
author
shaorongqiang
committed
fix action
1 parent 9eb3092 commit 11cc7af

File tree

4 files changed

+314
-0
lines changed

4 files changed

+314
-0
lines changed

.github/workflows/check-style.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: 'Code Style Checker'
2+
description: 'Check code style for different programming languages.'
3+
4+
on:
5+
push:
6+
branches:
7+
- master
8+
pull_request:
9+
branches:
10+
- master
11+
12+
jobs:
13+
run-script:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v2
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v2
21+
with:
22+
python-version: '3.x'
23+
24+
- name: Install dependencies
25+
run: |
26+
sudo apt-get update
27+
sudo apt-get install jq
28+
pip3 install requests
29+
30+
- name: Run the script
31+
env:
32+
INPUT_URL: 'https://openrouter.ai/api/v1/chat/completions'
33+
INPUT_API_KEY: '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'
34+
INPUT_MODEL: 'deepseek/deepseek-chat:free'
35+
INPUT_REPO_PATH: ${{ github.workspace }}
36+
INPUT_STYLE: 'rust' # 根据需要修改
37+
INPUT_LANGUAGE: 'rust' # 根据需要修改
38+
run: bash ./checker.sh

checker.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import os
2+
import subprocess
3+
import json
4+
import requests
5+
6+
# 环境变量
7+
url = os.getenv('INPUT_URL')
8+
api_key = os.getenv('INPUT_API_KEY')
9+
model = os.getenv('INPUT_MODEL')
10+
repo_path = os.getenv('INPUT_REPO_PATH')
11+
style = os.getenv('INPUT_STYLE')
12+
language = os.getenv('INPUT_LANGUAGE')
13+
14+
# 检查必要的环境变量
15+
if not api_key:
16+
print("api key is required")
17+
exit(-1)
18+
19+
if not os.path.isdir(repo_path):
20+
print("repo path does not exist")
21+
exit(-2)
22+
23+
if not language:
24+
print("language is required")
25+
exit(-3)
26+
27+
if not style:
28+
print("style is required")
29+
exit(-4)
30+
31+
if not model:
32+
print("model is required")
33+
exit(-5)
34+
35+
# 文件搜索和语言处理
36+
language = language.lower()
37+
file_extensions = {
38+
"rust": "*.rs",
39+
"rs": "*.rs",
40+
"golang": "*.go",
41+
"go": "*.go",
42+
"python": "*.py",
43+
"py": "*.py",
44+
"javascript": "*.js",
45+
"js": "*.js",
46+
"typescript": "*.ts",
47+
"ts": "*.ts",
48+
"solidity": "*.sol",
49+
"sol": "*.sol"
50+
}
51+
52+
if language not in file_extensions:
53+
print("Invalid language")
54+
exit(-6)
55+
56+
files = subprocess.run(['git', 'ls-files', file_extensions[language]], cwd=repo_path, text=True, capture_output=True).stdout.split()
57+
58+
# 处理每个文件
59+
for file in files:
60+
with open(os.path.join(repo_path, file), 'r') as f:
61+
code = f.read()
62+
63+
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}}'
64+
65+
user_message = f"Style Guide: \n\n{style} \n\nCode to evaluate: \n\n{code}"
66+
user_message = json.dumps(user_message) # 处理特殊字符
67+
68+
json_payload = {
69+
"response_format": {"type": "json_object"},
70+
"model": model,
71+
"messages": [
72+
{"role": "system", "content": system_message},
73+
{"role": "user", "content": user_message}
74+
],
75+
"stream": False
76+
}
77+
78+
headers = {
79+
"Content-Type": "application/json",
80+
"Authorization": f"Bearer {os.getenv('INPUT_API_KEY')}"
81+
}
82+
83+
response = requests.post(url, headers=headers, json=json_payload)
84+
response_data = response.json()
85+
86+
print(file)
87+
if 'error' in response_data:
88+
print(f"Error: {response_data['error']}")
89+
continue
90+
91+
content = response_data.get('choices', [{}])[0].get('message', {}).get('content', "No content found")
92+
print(content)
93+
94+
exit(0)

checker.sh

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/bin/bash
2+
3+
4+
url=${INPUT_URL:-"https://openrouter.ai/api/v1/chat/completions"}
5+
api_key=${INPUT_API_KEY:-"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"}
6+
model=${INPUT_MODEL:-"deepseek/deepseek-chat:free"}
7+
repo_path=${INPUT_REPO_PATH:-"."}
8+
style=${INPUT_STYLE:-"rust"}
9+
language=${INPUT_LANGUAGE:-"rust"}
10+
11+
if [ ! $api_key ]; then
12+
echo "api key is required"
13+
exit -1
14+
fi
15+
16+
if [ ! -d "$repo_path" ]; then
17+
echo "repo path does not exist"
18+
exit -2
19+
fi
20+
21+
if [ ! $language ]; then
22+
echo "language is required"
23+
exit -3
24+
fi
25+
26+
if [ ! $style ]; then
27+
echo "style is required"
28+
exit -4
29+
fi
30+
31+
if [ ! $model ]; then
32+
echo "model is required"
33+
exit -5
34+
fi
35+
36+
files=""
37+
language=$(echo $language | tr '[:upper:]' '[:lower:]')
38+
case $language in
39+
"rust"|"rs")
40+
language="Rust"
41+
files=$(git ls-files '*.rs')
42+
;;
43+
"golang"|"go")
44+
language="Golang"
45+
files=$(git ls-files '*.go')
46+
;;
47+
"python"|"py")
48+
language="Python"
49+
files=$(git ls-files '*.py')
50+
;;
51+
"javascript"|"js")
52+
language="JavaScript"
53+
files=$(git ls-files '*.js')
54+
;;
55+
"typescript"|"ts")
56+
language="TypeScript"
57+
files=$(git ls-files '*.ts')
58+
;;
59+
"solidity"|"sol")
60+
language="Solidity"
61+
files=$(git ls-files '*.sol')
62+
;;
63+
*)
64+
echo "Invalid language"
65+
exit -6
66+
;;
67+
esac
68+
69+
for file in ${files[*]}
70+
do
71+
code=$(cat "$file")
72+
system_message='You are a '${language}' code style evaluator that checks code against style guidelines.
73+
EXAMPLE JSON OUTPUT:
74+
{
75+
\"violations\": [
76+
{ \"line number\": \"reason\" },
77+
{ \"line number\": \"reason\" },
78+
],
79+
\"score\":0
80+
}'
81+
82+
user_message="Style Guide:
83+
84+
${style}
85+
86+
Code to evaluate:
87+
88+
${code}"
89+
user_message=$(echo $user_message | sed 's/{/\\{/g' | sed 's/}/\\}/g' | sed 's/\"/\\"/g' | sed 's/\\\\"/\\"/g')
90+
91+
json='{
92+
"response_format": {"type": "json_object"},
93+
"model": "'$model'",
94+
"messages": [
95+
{"role": "system", "content": "'$system_message'"},
96+
{"role": "user", "content": "'$user_message'"}
97+
],
98+
"stream": false
99+
}'
100+
101+
response=$(curl -s -X POST "$url" \
102+
-H "Content-Type: application/json" \
103+
-H "Authorization: Bearer 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" \
104+
--data "${json}")
105+
106+
echo $file
107+
# 检查是否有错误
108+
error=$(echo $response | jq -r '.error // empty')
109+
if [ ! -z "$error" ]; then
110+
echo "Error: $error"
111+
continue
112+
fi
113+
114+
# 提取内容
115+
content=$(echo $response | jq -r '.choices[0].message.content // "No content found"')
116+
echo "$content"
117+
done
118+
exit 0

main.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#![deny(warnings)]
2+
3+
mod checker;
4+
mod common;
5+
6+
use anyhow::Result;
7+
use clap::Parser;
8+
use common::{check_code, get_code_files, get_language};
9+
10+
#[derive(Parser)]
11+
pub struct Command {
12+
#[clap(short, long)]
13+
url: String,
14+
15+
#[clap(short, long)]
16+
model: String,
17+
18+
#[clap(short, long)]
19+
api_key: String,
20+
21+
#[clap(short, long)]
22+
style: String,
23+
24+
#[clap(short, long)]
25+
language: String,
26+
27+
#[clap(short, long)]
28+
repo_local_path: String,
29+
}
30+
31+
impl Command {
32+
pub async fn execute(self) -> Result<()> {
33+
let language = match get_language(&self.language) {
34+
Some(language) => language,
35+
None => return Err(anyhow::anyhow!("Invalid language")),
36+
};
37+
38+
let files = get_code_files(&self.repo_local_path, &language)?;
39+
40+
for file in files {
41+
match check_code(
42+
&file,
43+
&self.style,
44+
language.clone(),
45+
&self.url,
46+
&self.api_key,
47+
&self.model,
48+
)
49+
.await
50+
{
51+
Ok(content) => println!("\n{}: \n{}", file.display(), content),
52+
Err(e) => println!("\n{}: \n{}", file.display(), e),
53+
}
54+
}
55+
56+
Ok(())
57+
}
58+
}
59+
60+
#[tokio::main]
61+
async fn main() -> Result<()> {
62+
env_logger::init();
63+
Command::parse().execute().await
64+
}

0 commit comments

Comments
 (0)