fix lint errors

This commit is contained in:
bobo.yang 2024-11-14 15:04:27 +08:00
parent 2c1a10c4ce
commit b0e7369929
4 changed files with 34 additions and 23 deletions

View File

@ -46,7 +46,9 @@ def get_issue_or_task(task):
issue = read_issue_by_url(task.strip()) issue = read_issue_by_url(task.strip())
assert_exit(not issue, "Failed to read issue.", exit_code=-1) assert_exit(not issue, "Failed to read issue.", exit_code=-1)
return json.dumps({"id": issue["iid"], "title": issue["title"], "description": issue["description"]}) return json.dumps(
{"id": issue["iid"], "title": issue["title"], "description": issue["description"]}
)
else: else:
return task return task
@ -84,7 +86,9 @@ def main():
print("issue id:", issue_id, end="\n\n") print("issue id:", issue_id, end="\n\n")
issue = get_issue_json(issue_id, task) issue = get_issue_json(issue_id, task)
assert_exit(not issue["description"], f"Failed to retrieve issue with ID: {issue_id}", exit_code=-1) assert_exit(
not issue["description"], f"Failed to retrieve issue with ID: {issue_id}", exit_code=-1
)
# Generate 5 branch names # Generate 5 branch names
print("Generating code task summary ...", end="\n\n", flush=True) print("Generating code task summary ...", end="\n\n", flush=True)

View File

@ -164,17 +164,17 @@ def get_gitlab_project_id():
if repo_url.startswith("git@"): if repo_url.startswith("git@"):
# Handle SSH URL format # Handle SSH URL format
parts = repo_url.split(':') parts = repo_url.split(":")
project_path = parts[1].replace(".git", "") project_path = parts[1].replace(".git", "")
elif repo_url.startswith("https://"): elif repo_url.startswith("https://"):
# Handle HTTPS URL format # Handle HTTPS URL format
parts = repo_url.split('/') parts = repo_url.split("/")
project_path = '/'.join(parts[3:]).replace(".git", "") project_path = "/".join(parts[3:]).replace(".git", "")
else: else:
raise ValueError(f"Unsupported Git URL format: {repo_url}") raise ValueError(f"Unsupported Git URL format: {repo_url}")
print(f"Extracted project path: {project_path}", file=sys.stderr) print(f"Extracted project path: {project_path}", file=sys.stderr)
encoded_project_path = requests.utils.quote(project_path, safe='') encoded_project_path = requests.utils.quote(project_path, safe="")
print(f"Encoded project path: {encoded_project_path}", file=sys.stderr) print(f"Encoded project path: {encoded_project_path}", file=sys.stderr)
return encoded_project_path return encoded_project_path
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
@ -184,6 +184,7 @@ def get_gitlab_project_id():
print(f"Error in get_gitlab_project_id: {e}", file=sys.stderr) print(f"Error in get_gitlab_project_id: {e}", file=sys.stderr)
return None return None
# parse sub tasks in issue description # parse sub tasks in issue description
def parse_sub_tasks(description): def parse_sub_tasks(description):
sub_tasks = [] sub_tasks = []
@ -243,27 +244,29 @@ def create_and_checkout_branch(branch_name):
def is_issue_url(task): def is_issue_url(task):
task = task.strip() task = task.strip()
# 使用正则表达式匹配 http 或 https 开头issues/数字 结尾的 URL # 使用正则表达式匹配 http 或 https 开头issues/数字 结尾的 URL
pattern = r'^(http|https)://.*?/issues/\d+$' pattern = r"^(http|https)://.*?/issues/\d+$"
is_issue = bool(re.match(pattern, task)) is_issue = bool(re.match(pattern, task))
# print(f"Task to check: {task}", file=sys.stderr) # print(f"Task to check: {task}", file=sys.stderr)
# print(f"Is issue URL: {is_issue}", file=sys.stderr) # print(f"Is issue URL: {is_issue}", file=sys.stderr)
return is_issue return is_issue
def read_issue_by_url(issue_url): def read_issue_by_url(issue_url):
# Extract the issue number and project path from the URL # Extract the issue number and project path from the URL
issue_url = issue_url.replace("/-/", "/") issue_url = issue_url.replace("/-/", "/")
parts = issue_url.split('/') parts = issue_url.split("/")
issue_number = parts[-1] issue_number = parts[-1]
project_path = '/'.join(parts[3:-2]) # Assumes URL format: https://gitlab.com/project/path/-/issues/number project_path = "/".join(
parts[3:-2]
) # Assumes URL format: https://gitlab.com/project/path/-/issues/number
# URL encode the project path # URL encode the project path
encoded_project_path = requests.utils.quote(project_path, safe='') encoded_project_path = requests.utils.quote(project_path, safe="")
# Construct the API endpoint URL # Construct the API endpoint URL
api_url = f"{GITLAB_API_URL}/projects/{encoded_project_path}/issues/{issue_number}" api_url = f"{GITLAB_API_URL}/projects/{encoded_project_path}/issues/{issue_number}"
@ -289,9 +292,9 @@ def get_gitlab_issue_repo(issue_repo=False):
if os.path.exists(config_path) and issue_repo: if os.path.exists(config_path) and issue_repo:
with open(config_path, "r", encoding="utf-8") as f: with open(config_path, "r", encoding="utf-8") as f:
config_data = json.load(f) config_data = json.load(f)
if "git_issue_repo" in config_data: if "git_issue_repo" in config_data:
issue_repo = requests.utils.quote(config_data["git_issue_repo"], safe='') issue_repo = requests.utils.quote(config_data["git_issue_repo"], safe="")
print( print(
"current issue repo:", "current issue repo:",
config_data["git_issue_repo"], config_data["git_issue_repo"],
@ -433,9 +436,9 @@ def create_pull_request(title, description, source_branch, target_branch, projec
"title": title, "title": title,
"description": description, "description": description,
"source_branch": source_branch, "source_branch": source_branch,
"target_branch": target_branch "target_branch": target_branch,
} }
response = requests.post(url, headers=headers, json=payload) response = requests.post(url, headers=headers, json=payload)
if response.status_code == 201: if response.status_code == 201:
response_json = response.json() response_json = response.json()
@ -446,8 +449,11 @@ def create_pull_request(title, description, source_branch, target_branch, projec
def get_recently_mr(project_id): def get_recently_mr(project_id):
project_id = requests.utils.quote(project_id, safe='') project_id = requests.utils.quote(project_id, safe="")
url = f"{GITLAB_API_URL}/projects/{project_id}/merge_requests?state=opened&order_by=updated_at&sort=desc" url = (
f"{GITLAB_API_URL}/projects/{project_id}/"
"merge_requests?state=opened&order_by=updated_at&sort=desc"
)
headers = { headers = {
"Private-Token": GITLAB_ACCESS_TOKEN, "Private-Token": GITLAB_ACCESS_TOKEN,
"Content-Type": "application/json", "Content-Type": "application/json",
@ -482,7 +488,7 @@ def run_command_with_retries(command, retries=3, delay=5):
def update_mr(project_id, mr_iid, title, description): def update_mr(project_id, mr_iid, title, description):
project_id = requests.utils.quote(project_id, safe='') project_id = requests.utils.quote(project_id, safe="")
url = f"{GITLAB_API_URL}/projects/{project_id}/merge_requests/{mr_iid}" url = f"{GITLAB_API_URL}/projects/{project_id}/merge_requests/{mr_iid}"
headers = {"Private-Token": GITLAB_ACCESS_TOKEN, "Content-Type": "application/json"} headers = {"Private-Token": GITLAB_ACCESS_TOKEN, "Content-Type": "application/json"}
payload = {"title": title, "description": description} payload = {"title": title, "description": description}
@ -495,6 +501,7 @@ def update_mr(project_id, mr_iid, title, description):
print("Failed to update MR.") print("Failed to update MR.")
return None return None
def check_unpushed_commits(): def check_unpushed_commits():
try: try:
# 获取当前分支的本地提交和远程提交的差异 # 获取当前分支的本地提交和远程提交的差异

View File

@ -41,7 +41,7 @@ def select_task(tasks):
def get_issue_json(issue_url): def get_issue_json(issue_url):
issue = get_issue_info_by_url(issue_url) issue = get_issue_info_by_url(issue_url)
assert_exit(not issue, f"Failed to retrieve issue with ID: {issue_id}", exit_code=-1) assert_exit(not issue, f"Failed to retrieve issue with ID: {issue_url}", exit_code=-1)
return { return {
"id": issue["iid"], "id": issue["iid"],
"web_url": issue["web_url"], "web_url": issue["web_url"],

View File

@ -54,7 +54,7 @@ def update_tasks_input(user_input):
def get_issue_json(issue_url): def get_issue_json(issue_url):
issue = get_issue_info_by_url(issue_url) issue = get_issue_info_by_url(issue_url)
assert_exit(not issue, f"Failed to retrieve issue with ID: {issue_id}", exit_code=-1) assert_exit(not issue, f"Failed to retrieve issue with ID: {issue_url}", exit_code=-1)
return { return {
"id": issue["iid"], "id": issue["iid"],
"web_url": issue["web_url"], "web_url": issue["web_url"],