Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

# 添加使用AI生成 branch_name/添加参数控制日志文件输出 #32

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ alias pr-merge=/Users/xxx/xxx/quick-workflow/pr-merge.sh
export JIRA_SERVICE_ADDRESS=https://xxx.xx # Your Jira network address
export JIRA_API_TOKEN=xxx

export BRAIN_AI_KEY=xxx # Your Brain AI key, Use to generate branch name

# Optional
# Generate the custom branch prefix name
# export GH_BRANCH_PREFIX=xx # xx/jira_ticket--desc
Expand Down
63 changes: 63 additions & 0 deletions generate-branch-name.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash

generate_branch_name_from_input() {
local input_text="$1"
local api_key="$2"

# OpenAI API 配置
local API_URL="http://openai-proxy.brain.loocaa.com/v1/chat/completions"

local json_data=$(cat <<EOF
{
"model": "gpt-3.5-turbo",
"stream": false,
"messages": [
{
"role": "system",
"content": "As a skilled linguist fluent in both English and Chinese, extract the key terms from the user's input (which may contain both Chinese and English) and generate a concise, descriptive branch name in English. Return only the branch name as a single string, and if other formats are present, convert them into a string."
},
{
"role": "user",
"content": "Based on the user's input '${input_text}', extract the key ideas and generate a concise branch name in English. The branch name should reflect the main concept of the suggestion without unnecessary detail."
}
]
}
EOF
)

# 发送请求到 OpenAI API
local response=$(curl -s --progress-bar --location --max-time 30 -w "%{http_code}" "$API_URL" \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $api_key" \
--data "$json_data")

# 提取状态码和响应内容
local http_code="${response: -3}"
local response_body="${response:0:${#response}-3}"

# 检查状态码
if [ "$http_code" -ne 200 ]; then
echo -e $n Fetch branch name from AI failed, code: $http_code
return 1
fi

# 从响应中提取分支名
local branch_name=$(echo "$response_body" | jq -r '.choices[0].message.content')

# 清理分支名
branch_name=$(echo "$branch_name" | \
sed 's/^[ \t]*//;s/[ \t]*$//' | \
tr '[:upper:]' '[:lower:]' | \
tr ' ' '-' | \
sed 's/[^a-z0-9-]//g' | \
sed 's/-\+/-/g' | \
sed 's/^-\|-$//g')

# 检查 branch_name 是否为空
if [ -z "$branch_name" ]; then
echo -e $n Fetch branch name from AI failed, branch_name is empty
return 1
fi
echo "$branch_name"
return 0
}
15 changes: 14 additions & 1 deletion pr-create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ source $script_dir/pr-body.sh
source $script_dir/multiselect.sh
source $script_dir/pr-jira.sh
source $script_dir/jira-status.sh
source $script_dir/generate-branch-name.sh

jira_ticket=$1
if [ -z "$jira_ticket" ]; then
Expand All @@ -27,7 +28,7 @@ if [ -n "${jira_ticket}" ]; then
status=$(read_status_pr_created $jira_ticket)
fi

if [ -n "${jira_ticket}" ]; then
if [ -n "${jira_ticket}" ] && [ -n "${OPENAI_KEY}" ]; then
issue_json=$(aiwflow issue-desc $jira_ticket)
issue_desc=$(echo "$issue_json" | jq -r '.issue_desc')
need_translate=$(echo "$issue_json" | jq -r '.need_translate')
Expand Down Expand Up @@ -68,6 +69,18 @@ if [ -z "${jira_ticket}" ]; then
branch_name=$(echo "$issue_desc" | sed 's/[^a-zA-Z0-9]/-/g')
fi

if [[ -n "${BRAIN_AI_KEY}" && -z "${OPENAI_KEY}" ]]; then
echo -e Start fetch branch name from AI...
branch_name_from_ai=$(generate_branch_name_from_input "$commit_title" "$BRAIN_AI_KEY")
# 检查函数是否执行成功
if [ $? -eq 0 ]; then
echo -e $y Fetch branch name from AI success $branch_name_from_ai
branch_name=$branch_name_from_ai
else
echo -e $n Fetch branch name from AI failed
fi
fi

if [ -n "${GH_BRANCH_PREFIX}" ]; then
branch_name=${GH_BRANCH_PREFIX}/${branch_name}
fi
Expand Down
18 changes: 17 additions & 1 deletion qklogs/qklogs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,26 @@ if ! merge_logs "$OUTPUT_DIR"; then
exit 1
fi

if [[ "${LOG_DELETE_WHEN_OPERATION_COMPLETED}" == 1 ]]; then
echo "ℹ️ Deleting downloaded files..."
# 删除原始日志文件
rm "$OUTPUT_DIR"/log.z*
fi

# Open the merged file
if [ -f "$OUTPUT_DIR/merged.zip" ]; then
echo "ℹ️ Opening merged file..."
unzip "$OUTPUT_DIR/merged.zip" -d "$OUTPUT_DIR/merged"

if [[ -n "${LOG_OUTPUT_FOLDER_NAME}" ]]; then
unzip "$OUTPUT_DIR/merged.zip" -d "$OUTPUT_DIR/${LOG_OUTPUT_FOLDER_NAME}"
else
unzip "$OUTPUT_DIR/merged.zip" -d "$OUTPUT_DIR/merged"
fi

if [[ "${LOG_DELETE_WHEN_OPERATION_COMPLETED}" == 1 ]]; then
echo "ℹ️ Deleting merged.zip ..."
rm "$OUTPUT_DIR/merged.zip"
fi

echo "✅ All done! Files are in: $OUTPUT_DIR"
echo "ℹ️ File list:"
Expand Down