Skip to content

Commit

Permalink
fix action
Browse files Browse the repository at this point in the history
  • Loading branch information
shaorongqiang committed Feb 12, 2025
1 parent 9eb3092 commit 46df91e
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/check-style.yml
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
123 changes: 123 additions & 0 deletions checker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/bin/bash


url=${INPUT_URL}
api_key="Authorization: Bearer ${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 [ ! $INPUT_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 \"$api_key\" --data \"${json}\""

response=$(curl -s -X POST "$url" -H "Content-Type: application/json" -H "$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
64 changes: 64 additions & 0 deletions main.rs
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
}

0 comments on commit 46df91e

Please sign in to comment.