import json import os import sys sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")) from common_util import assert_exit, ui_edit # noqa: E402 from devchat.llm import ( # noqa: E402 chat_json, ) from git_api import ( # noqa: E402 auto_push, get_commit_messages, get_current_branch, get_gitlab_issue_repo, get_issue_info, get_last_base_branch, get_recently_pr, save_last_base_branch, update_pr, ) # 从分支名称中提取issue id def extract_issue_id(branch_name): if "#" in branch_name: return branch_name.split("#")[-1] return None # 使用LLM模型生成PR内容 PROMPT = ( "Create a pull request title and description based on " "the following issue and commit messages, if there is an " "issue, close that issue in PR description as /#issue_id:\n" "Issue: {issue}\n" "Commits:\n{commit_messages}\n" "The response result should format as JSON object as following:\n" '{{"title": "pr title", "description": "pr description"}}' ) @chat_json(prompt=PROMPT) def generate_pr_content_llm(issue, commit_messages): pass def generate_pr_content(issue, commit_messages): response = generate_pr_content_llm(issue=json.dumps(issue), commit_messages=commit_messages) assert_exit(not response, "Failed to generate PR content.", exit_code=-1) return response.get("title"), response.get("description") @ui_edit(ui_type="editor", description="Edit PR title:") @ui_edit(ui_type="editor", description="Edit PR description:") def edit_pr(title, description): pass @ui_edit(ui_type="editor", description="Edit base branch:") def edit_base_branch(base_branch): pass def get_issue_json(issue_id): issue = {"id": "no issue id", "title": "", "description": ""} if issue_id: issue = get_issue_info(issue_id) assert_exit(not issue, f"Failed to retrieve issue with ID: {issue_id}", exit_code=-1) issue = { "id": issue_id, "web_url": issue["web_url"], "title": issue["title"], "description": issue["description"], } return issue # 主函数 def main(): print("start update_pr ...", end="\n\n", flush=True) base_branch = get_last_base_branch("main") base_branch = edit_base_branch(base_branch) if isinstance(base_branch, list) and len(base_branch) > 0: base_branch = base_branch[0] save_last_base_branch(base_branch) repo_name = get_gitlab_issue_repo() branch_name = get_current_branch() issue_id = extract_issue_id(branch_name) # print basic info, repo_name, branch_name, issue_id print("repo name:", repo_name, end="\n\n") print("branch name:", branch_name, end="\n\n") print("issue id:", issue_id, end="\n\n") issue = get_issue_json(issue_id) commit_messages = get_commit_messages(base_branch) recent_pr = get_recently_pr(repo_name) assert_exit(not recent_pr, "Failed to get recent PR.", exit_code=-1) print("generating pr title and description ...", end="\n\n", flush=True) pr_title, pr_body = generate_pr_content(issue, commit_messages) assert_exit(not pr_title, "Failed to generate PR content.", exit_code=-1) pr_title, pr_body = edit_pr(pr_title, pr_body) assert_exit(not pr_title, "PR creation cancelled.", exit_code=0) is_push_success = auto_push() assert_exit(not is_push_success, "Failed to push changes.", exit_code=-1) pr = update_pr(recent_pr["iid"], pr_title, pr_body, repo_name) assert_exit(not pr, "Failed to update PR.", exit_code=-1) print(f"PR updated successfully: {pr['web_url']}") if __name__ == "__main__": main()