diff --git a/README.md b/README.md index 9834103..988bcab 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/generate-branch-name/generate-branch-name-test.sh b/generate-branch-name/generate-branch-name-test.sh new file mode 100644 index 0000000..d31be8a --- /dev/null +++ b/generate-branch-name/generate-branch-name-test.sh @@ -0,0 +1,18 @@ +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +source $SCRIPT_DIR/generate-branch-name.sh + +# 主函数 +__main__() { + check_dependencies + commit_title="Fetch branch name from AI cost:" + echo "SCRIPT_DIR: $SCRIPT_DIR" + generate_id=$(echo -n $commit_title | md5sum | awk '{print $1}') + echo "generate_id: $generate_id" + generate_branch_name "$commit_title" "$BRAIN_AI_KEY" "$generate_id" + echo "branch name: $(cat /tmp/branch_name_$generate_id.txt)" + rm -f /tmp/branch_name_$generate_id.txt +} + +# 执行主函数,传递所有命令行参数 +__main__ \ No newline at end of file diff --git a/generate-branch-name/generate-branch-name.py b/generate-branch-name/generate-branch-name.py new file mode 100755 index 0000000..3f484f4 --- /dev/null +++ b/generate-branch-name/generate-branch-name.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 + +import requests +import json +import sys +import re + +def make_stream_request(input_text, api_key): + + if not input_text: + print("Input text is required") + return "" + if not api_key: + print("API key is required") + return "" + + url = 'http://openai-proxy.brain.loocaa.com/v1/chat/completions' + headers = { + 'Content-Type': 'application/json', + 'Authorization': f'Bearer {api_key}' + } + + payload = { + "model": "gpt-3.5-turbo", + "stream": True, + "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": f"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." + } + ] + } + + response = requests.post(url, headers=headers, json=payload, stream=True) + + # 检查响应状态码 + if response.status_code != 200: + return "" + + # 用于存储完整的响应 + full_response = "" + + for line in response.iter_lines(): + if line: + # 先解码字节字符串 + line = line.decode('utf-8') + if line.startswith('data: '): + line = line[6:] + + # 跳过心跳消息 + if line == '[DONE]': + break + + try: + json_response = json.loads(line) + if 'choices' in json_response: + content = json_response['choices'][0].get('delta', {}).get('content', '') + if content: + full_response += content + except json.JSONDecodeError: + continue + + return full_response + +def clean_branch_name(branch_name): + # 去除前后空格 + branch_name = branch_name.strip() + + # 转换为小写 + branch_name = branch_name.lower() + + # 替换空格为短横线 + branch_name = branch_name.replace(' ', '-') + + # 移除非字母数字和短横线的字符 + branch_name = re.sub(r'[^a-z0-9-]', '', branch_name) + + # 替换多个短横线为一个短横线 + branch_name = re.sub(r'-+', '-', branch_name) + + # 去除开头和结尾的短横线 + branch_name = branch_name.strip('-') + + return branch_name + +def save_branch_name(result, identifier): + file_path = f"/tmp/branch_name_{identifier}.txt" + with open(file_path, 'w') as file: + file.write(result) + print(f"Branch name saved to {file_path}") + +if __name__ == "__main__": + if len(sys.argv) > 2: + input_text = sys.argv[1] + api_key = sys.argv[2] + identifier = sys.argv[3] + else: + print("Usage: python stream_request.py ") + sys.exit(1) + + result = make_stream_request(input_text, api_key) + if result: + branch_name = clean_branch_name(result) + save_branch_name(branch_name, identifier) + sys.exit(0) + else: + print(f"Failed to get branch name {result}") + sys.exit(1) diff --git a/generate-branch-name/generate-branch-name.sh b/generate-branch-name/generate-branch-name.sh new file mode 100755 index 0000000..8994ad1 --- /dev/null +++ b/generate-branch-name/generate-branch-name.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# 检查依赖 +check_dependencies() { + # 检查 Python3 + if ! command -v python3 &> /dev/null; then + echo "错误:未安装 Python3" + exit 1 + fi + + # 检查 requests 包 + python3 -c "import requests" 2>/dev/null || { + echo "正在安装 requests 包..." + pip3 install requests + } +} + +# 执行 Python 脚本 +generate_branch_name() { + if [ ! -f "$SCRIPT_DIR/generate-branch-name.py" ]; then + echo "错误:找不到 generate-branch-name.py 文件" + fi + + # 将输入文本传递给 Python 脚本 + python3 $SCRIPT_DIR/generate-branch-name.py "$1" "$2" "$3" + + # 检查函数返回值 + if [ $? -ne 0 ]; then + echo -e $n "Generate branch name failed: $result" + fi +} \ No newline at end of file diff --git a/install.sh b/install.sh index 8b55e65..1d1b983 100755 --- a/install.sh +++ b/install.sh @@ -121,6 +121,20 @@ add_alias_if_not_exists "$PR_CREATE_ALIAS_NAME" "$PR_CREATE_ALIAS_COMMAND" # Add the pr-merge alias if it doesn't exist add_alias_if_not_exists "$PR_MERGE_ALIAS_NAME" "$PR_MERGE_ALIAS_COMMAND" +# 检查依赖 +check_dependencies() { + if ! command -v python3 &> /dev/null; then + echo "错误:未安装 Python3" + fi + + python3 -c "import requests" 2>/dev/null || { + echo "正在安装 requests 包..." + pip3 install requests + } +} + +check_dependencies + # Add environment variables for env_var in "${ENV_VARS[@]}"; do add_env_var_if_not_exists "$env_var" diff --git a/pr-create.sh b/pr-create.sh index 8052f71..4505f6e 100755 --- a/pr-create.sh +++ b/pr-create.sh @@ -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/generate-branch-name.sh jira_ticket=$1 if [ -z "$jira_ticket" ]; then @@ -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') @@ -68,6 +69,24 @@ 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 + start_time=$(date +%s.%N) + generate_id=$(echo -n $commit_title | md5sum | awk '{print $1}') + echo -e "Start fetch branch name from AI with ID: $generate_id" + generate_branch_name "$commit_title" "$BRAIN_AI_KEY" "$generate_id" + + result=$(cat /tmp/branch_name_$generate_id.txt) + echo -e $y Fetch branch name from AI success $result + rm -f /tmp/branch_name_$generate_id.txt + if [ -n "$result" ]; then + branch_name=$result + fi + + end_time=$(date +%s.%N) + duration=$(echo "$end_time - $start_time" | bc) + echo "Fetch branch name cost $duration seconds" +fi + if [ -n "${GH_BRANCH_PREFIX}" ]; then branch_name=${GH_BRANCH_PREFIX}/${branch_name} fi diff --git a/qk.sh b/qk.sh index 1f77598..f5f0245 100755 --- a/qk.sh +++ b/qk.sh @@ -19,6 +19,9 @@ JIRA_ID="$1" ACTION="$2" LOGS_DIR="$HOME/Downloads/logs_${JIRA_ID}" LOG_FILE="$LOGS_DIR/merged/flutter-api.log" +if [[ -n "${LOG_OUTPUT_FOLDER_NAME}" ]]; then + LOG_FILE="$LOGS_DIR/${LOG_OUTPUT_FOLDER_NAME}/flutter-api.log" +fi # Function to check if logs exist check_logs() { @@ -34,7 +37,7 @@ case "$ACTION" in # Call qklogs with full path "$QKLOGS_PATH" "$JIRA_ID" ;; - + "-f") check_logs # Use third argument if provided, otherwise prompt @@ -46,7 +49,7 @@ case "$ACTION" in fi "$QKFIND_PATH" "$LOG_FILE" "$REQUEST_ID" ;; - + "-s") check_logs # Use third argument if provided, otherwise prompt @@ -58,10 +61,10 @@ case "$ACTION" in fi "$QKSEARCH_PATH" "$LOG_FILE" "$SEARCH_TERM" ;; - + *) echo "❌ Invalid action: $ACTION" echo "Usage: qk [-d|-f|-s]" exit 1 ;; -esac \ No newline at end of file +esac \ No newline at end of file diff --git a/qklogs/qklogs.sh b/qklogs/qklogs.sh index 02eac9d..e697a0f 100755 --- a/qklogs/qklogs.sh +++ b/qklogs/qklogs.sh @@ -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:"