feat: Add interactive base branch input for PR creation

- Introduce functions to read and save last base branch from config
- Modify PR creation and update commands to use dynamic base branch
- Enable user interaction to edit base branch during PR operations
This commit is contained in:
bobo 2024-06-30 15:42:39 +08:00
parent 5595097aed
commit 3a15d11026
4 changed files with 62 additions and 7 deletions

View File

@ -411,3 +411,36 @@ def update_pr(pr_number, title, body, repo_name):
else:
print("Failed to update PR.")
return None
def get_last_base_branch(default_branch):
""" read last base branch from config file """
def read_config(config_path, item):
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config = json.load(f)
return config.get(item)
return None
project_config_path = os.path.join(os.getcwd(), ".chat", ".workflow_config.json")
last_base_branch = read_config(project_config_path, "last_base_branch")
if last_base_branch:
return last_base_branch
return default_branch
def save_last_base_branch(save_branch=None):
""" save last base branch to config file """
def save_config(config_path, item, value):
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config = json.load(f)
else:
config = {}
config[item] = value
with open(config_path, "w", encoding="utf-8") as f:
json.dump(config, f, indent=4)
if not save_branch:
base_branch = get_current_branch()
project_config_path = os.path.join(os.getcwd(), ".chat", ".workflow_config.json")
save_config(project_config_path, "last_base_branch", base_branch)

View File

@ -12,6 +12,7 @@ from git_api import ( # noqa: E402
create_and_checkout_branch,
is_issue_url,
read_issue_by_url,
save_last_base_branch,
)
# Function to generate a random branch name
@ -79,6 +80,9 @@ def main():
# Select branch name
selected_branch = select_branch_name(branch_names)
# save base branch name
save_last_base_branch()
# create and checkout branch
print(f"Creating and checking out branch: {selected_branch}")
create_and_checkout_branch(selected_branch)

View File

@ -14,10 +14,10 @@ from git_api import ( # noqa: E402
get_current_branch,
get_github_repo,
get_issue_info,
get_last_base_branch,
save_last_base_branch,
)
BASH_BRANCH = "main"
# 从分支名称中提取issue id
def extract_issue_id(branch_name):
@ -57,6 +57,9 @@ def generate_pr_content(issue, commit_messages, user_input):
def edit_pr(title, body):
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": "", "body": ""}
@ -76,6 +79,12 @@ def get_issue_json(issue_id):
def main():
print("start new_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_github_repo()
branch_name = get_current_branch()
issue_id = extract_issue_id(branch_name)
@ -86,7 +95,7 @@ def main():
print("issue id:", issue_id, end="\n\n")
issue = get_issue_json(issue_id)
commit_messages = get_commit_messages(BASH_BRANCH)
commit_messages = get_commit_messages(base_branch)
print("generating pr title and body ...", end="\n\n", flush=True)
user_input = sys.argv[1]
@ -99,7 +108,7 @@ def main():
is_push_success = auto_push()
assert_exit(not is_push_success, "Failed to push changes.", exit_code=-1)
pr = create_pull_request(pr_title, pr_body, branch_name, BASH_BRANCH, repo_name)
pr = create_pull_request(pr_title, pr_body, branch_name, base_branch, repo_name)
assert_exit(not pr, "Failed to create PR.", exit_code=-1)
print(f"PR created successfully: {pr['html_url']}")

View File

@ -17,10 +17,10 @@ from git_api import ( # noqa: E402
get_issue_info,
get_recently_pr,
update_pr,
get_last_base_branch,
save_last_base_branch,
)
BASH_BRANCH = "main"
# 从分支名称中提取issue id
def extract_issue_id(branch_name):
@ -57,6 +57,9 @@ def generate_pr_content(issue, commit_messages):
def edit_pr(title, body):
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": "", "body": ""}
@ -76,6 +79,12 @@ def get_issue_json(issue_id):
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_github_repo()
branch_name = get_current_branch()
issue_id = extract_issue_id(branch_name)
@ -86,7 +95,7 @@ def main():
print("issue id:", issue_id, end="\n\n")
issue = get_issue_json(issue_id)
commit_messages = get_commit_messages(BASH_BRANCH)
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)