Skip to content

Commit

Permalink
Refactor axios token retrieval to use a function for better encapsula…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
swuecho committed Jun 30, 2024
1 parent 85c5127 commit 626730a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 3 deletions.
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10.4
99 changes: 99 additions & 0 deletions scripts/auto_commit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""
translate from https://github.com/zhufengme/GPTCommit/blob/main/gptcommit.sh
"""
import os
import subprocess
import requests

# 设置你的 OpenAI API 密钥
OPENAI_API_KEY = os.getenv('DEEPSEEK_API_KEY', '')
LLM_URL = "https://api.deepseek.com/v1/chat/completions"

# 设置你的 Proxy,默认使用HTTPS_PROXY环境变量
CURL_PROXY = os.getenv('HTTPS_PROXY', '')

def get_git_diff(diff_type):
try:
result = subprocess.check_output(['git', 'diff'] + diff_type, text=True)
return result
except subprocess.CalledProcessError as e:
print(f"Error getting git diff: {e}")
return ""

def generate_commit_message(diff):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}",
}
payload = {
"model": "deepseek-coder",
"messages": [
{
"role": "user",
"content": f"write a concise commit message:\n\n{diff}\n\nCommit message:",
}
],
"max_tokens": 100,
"temperature": 0.7,
}
proxies = {
"https": CURL_PROXY,
} if CURL_PROXY else {}

try:
response = requests.post(LLM_URL,
headers=headers,
proxies=proxies,
json=payload,
timeout=5)
response.raise_for_status()
return response.json()['choices'][0]['message']['content']
except requests.RequestException as e:
print(f"Error calling OpenAI API: {e}")
return ""

def main():
# 检查工作目录状态
print("检查工作目录状态...")
try:
subprocess.run(['git', 'status'], check=True)
except subprocess.CalledProcessError as e:
print(f"Error checking git status: {e}")
return

# 获取工作目录和暂存区之间的差异
working_diff = get_git_diff([])
# 获取暂存区和HEAD之间的差异
staged_diff = get_git_diff(['--cached'])

# 合并差异
diff = working_diff + staged_diff

# 如果没有差异,退出
if not diff.strip():
print("没有发现差异。")
return

# 获取生成的提交注释
commit_message = generate_commit_message(diff)
print(commit_message)

# git add
print("git add...")
try:
subprocess.run(['git', 'add', '.', '-A'], check=True)
except subprocess.CalledProcessError as e:
print(f"Error adding changes: {e}")
return

# 提交代码
print("提交代码...")
try:
subprocess.run(['git', 'commit', '-m', commit_message], check=True)
except subprocess.CalledProcessError as e:
print(f"Error committing changes: {e}")
return

if __name__ == "__main__":
main()
8 changes: 5 additions & 3 deletions web/src/plugins/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import Vue from 'vue';
import axios from "axios";

let jwtToken = localStorage.getItem('JWT_TOKEN')
console.log(jwtToken)
function getJwtToken() {
let jwtToken = localStorage.getItem('JWT_TOKEN')
return jwtToken
}

// Full config: https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
Expand All @@ -16,7 +18,7 @@ let config = {
// timeout: 60 * 1000, // Timeout
// withCredentials: true, // Check cross-site Access-Control
headers: {
'Authorization' : `Bearer ${jwtToken}`
'Authorization' : `Bearer ${getJwtToken()}`
}
};

Expand Down

0 comments on commit 626730a

Please sign in to comment.