workflows/merico/pr/config_util.py

143 lines
4.6 KiB
Python
Raw Normal View History

2024-05-18 22:05:51 +08:00
import json
2024-05-19 15:34:26 +08:00
import os
2024-05-18 22:05:51 +08:00
from lib.chatmark import TextEditor
# 根据PR URL获取不同的仓库管理类型
# 支持的类型有github gitlab bitbucket bitbucket_server azure codecommit gerrit
def get_repo_type(url):
# 根据URL的特征判断仓库管理类型
if "github.com" in url:
return "github"
elif "gitlab.com" in url or "/gitlab/" in url:
return "gitlab"
elif "bitbucket.org" in url:
return "bitbucket"
elif "bitbucket-server" in url:
return "bitbucket_server"
elif "dev.azure.com" in url or "visualstudio.com" in url:
return "azure"
elif "codecommit" in url:
return "codecommit"
elif "gerrit" in url:
return "gerrit"
else:
return ""
2024-05-19 15:34:26 +08:00
2024-05-18 22:05:51 +08:00
def read_github_token():
config_path = os.path.join(os.path.expanduser("~/.chat"), ".workflow_config.json")
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config_data = json.load(f)
if "github_token" in config_data:
return config_data["github_token"]
return ""
2024-05-19 15:34:26 +08:00
2024-05-18 22:05:51 +08:00
def read_server_access_token(repo_type):
config_path = os.path.join(os.path.expanduser("~/.chat"), ".workflow_config.json")
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config_data = json.load(f)
if repo_type in config_data and "access_token" in config_data[repo_type]:
return config_data[repo_type]["access_token"]
return ""
2024-05-19 15:34:26 +08:00
def read_gitlab_host():
config_path = os.path.join(os.path.expanduser("~/.chat"), ".workflow_config.json")
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config_data = json.load(f)
if "gitlab_host" in config_data:
return config_data["gitlab_host"]
return ""
2024-05-18 22:05:51 +08:00
def save_github_token(github_token):
config_path = os.path.join(os.path.expanduser("~/.chat"), ".workflow_config.json")
config_data = {}
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config_data = json.load(f)
2024-05-19 15:34:26 +08:00
2024-05-18 22:05:51 +08:00
config_data["github_token"] = github_token
with open(config_path, "w+", encoding="utf-8") as f:
json.dump(config_data, f, indent=4)
2024-05-19 15:34:26 +08:00
def save_gitlab_host(github_token):
config_path = os.path.join(os.path.expanduser("~/.chat"), ".workflow_config.json")
config_data = {}
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config_data = json.load(f)
config_data["gitlab_host"] = github_token
with open(config_path, "w+", encoding="utf-8") as f:
json.dump(config_data, f, indent=4)
2024-05-18 22:05:51 +08:00
def save_server_access_token(repo_type, access_token):
config_path = os.path.join(os.path.expanduser("~/.chat"), ".workflow_config.json")
config_data = {}
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config_data = json.load(f)
2024-05-19 15:34:26 +08:00
2024-05-18 22:05:51 +08:00
if repo_type not in config_data:
config_data[repo_type] = {}
config_data[repo_type]["access_token"] = access_token
with open(config_path, "w+", encoding="utf-8") as f:
json.dump(config_data, f, indent=4)
2024-05-19 15:34:26 +08:00
2024-05-18 22:05:51 +08:00
def read_github_token_with_input():
github_token = read_github_token()
if not github_token:
# Input your github TOKEN to access github api:
2024-05-19 15:34:26 +08:00
github_token_editor = TextEditor("", "Please input your github TOKEN to access:")
2024-05-18 22:05:51 +08:00
github_token = github_token_editor.new_text
if not github_token:
return github_token
save_github_token(github_token)
return github_token
2024-05-19 15:34:26 +08:00
2024-05-18 22:05:51 +08:00
def read_server_access_token_with_input(pr_url):
repo_type = get_repo_type(pr_url)
if not repo_type:
return ""
server_access_token = read_server_access_token(repo_type)
if not server_access_token:
# Input your server access TOKEN to access server api:
server_access_token_editor = TextEditor(
2024-05-19 15:34:26 +08:00
"", f"Please input your {repo_type} access TOKEN to access:"
2024-05-18 22:05:51 +08:00
)
server_access_token_editor.render()
server_access_token = server_access_token_editor.new_text
if not server_access_token:
return server_access_token
save_server_access_token(repo_type, server_access_token)
return server_access_token
def gitlab_host():
host = read_gitlab_host()
if host:
return host
gitlab_host_editor = TextEditor(
"",
"Please input your gitlab host(for example: https://www.gitlab.com):"
)
gitlab_host_editor.render()
host = gitlab_host_editor.new_text
if host:
save_gitlab_host(host)
return host