Skip to content

Commit

Permalink
Merge pull request #126 from devchat-ai/update_workflows
Browse files Browse the repository at this point in the history
Refactor and Enhance PR Creation and Review Features
  • Loading branch information
kagami-l authored Jul 1, 2024
2 parents bf6e266 + 5f4ebe5 commit 7b19535
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 15 deletions.
37 changes: 37 additions & 0 deletions merico/github/git_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,40 @@ 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_item(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_item(project_config_path, "last_base_branch")
if last_base_branch:
return last_base_branch
return default_branch


def save_last_base_branch(base_branch=None):
"""save last base branch to config file"""

def save_config_item(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 base_branch:
base_branch = get_current_branch()
project_config_path = os.path.join(os.getcwd(), ".chat", ".workflow_config.json")
save_config_item(project_config_path, "last_base_branch", base_branch)
19 changes: 13 additions & 6 deletions merico/github/new_branch/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
create_and_checkout_branch,
is_issue_url,
read_issue_by_url,
save_last_base_branch,
)

# Function to generate a random branch name
PROMPT = (
"Give me 5 different git branch names, "
"mainly hoping to express: {task}, "
"Good branch name should looks like: <type>/<main content>-#<issue id>,"
"<issue id> is optional, add it only when you know the issue id clearly, "
"don't miss '#' before issue id. "
"Good branch name should looks like: <type>/<main content>,"
"the final result is output in JSON format, "
'as follows: {{"names":["name1", "name2", .. "name5"]}}\n'
)
Expand All @@ -47,9 +46,11 @@ def get_issue_or_task(task):
issue = read_issue_by_url(task.strip())
assert_exit(not issue, "Failed to read issue.", exit_code=-1)

return json.dumps({"id": issue["number"], "title": issue["title"], "body": issue["body"]})
return json.dumps(
{"id": issue["number"], "title": issue["title"], "body": issue["body"]}
), issue["number"]
else:
return task
return task, None


# Main function
Expand All @@ -67,17 +68,23 @@ def main():
)

# read issue by url
task = get_issue_or_task(task)
task, issue_id = get_issue_or_task(task)

# Generate 5 branch names
print("Generating branch names ...", end="\n\n", flush=True)
branch_names = generate_branch_name(task=task)
assert_exit(not branch_names, "Failed to generate branch names.", exit_code=-1)
branch_names = branch_names["names"]
for index, branch_name in enumerate(branch_names):
if issue_id:
branch_names[index] = f"{branch_name}-#{issue_id}"

# 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)
Expand Down
19 changes: 15 additions & 4 deletions merico/github/new_pr/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
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):
Expand Down Expand Up @@ -58,6 +58,11 @@ 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": ""}
if issue_id:
Expand All @@ -76,6 +81,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)
Expand All @@ -86,7 +97,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]
Expand All @@ -99,7 +110,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']}")
Expand Down
17 changes: 14 additions & 3 deletions merico/github/update_pr/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
get_current_branch,
get_github_repo,
get_issue_info,
get_last_base_branch,
get_recently_pr,
save_last_base_branch,
update_pr,
)

BASH_BRANCH = "main"


# 从分支名称中提取issue id
def extract_issue_id(branch_name):
Expand Down Expand Up @@ -58,6 +58,11 @@ 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": ""}
if issue_id:
Expand All @@ -76,6 +81,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)
Expand All @@ -86,7 +97,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)
Expand Down
4 changes: 3 additions & 1 deletion merico/pr/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def close(self):
from merico.pr.config_util import (
get_gitlab_host,
get_repo_type,
read_review_inline_config,
read_server_access_token_with_input,
)
from merico.pr.custom_suggestions_config import get_custom_suggestions_system_prompt
Expand Down Expand Up @@ -169,7 +170,8 @@ def close(self):
get_settings().pr_code_suggestions_prompt.system += language_prompt
get_settings().pr_review_prompt.system += language_prompt
get_settings().pr_description_prompt.system += language_prompt
# get_settings().pr_reviewer.inline_code_comments = True
if read_review_inline_config():
get_settings().pr_reviewer.inline_code_comments = True

# config for find similar issues
get_settings().set("PR_SIMILAR_ISSUE.VECTORDB", "lancedb")
Expand Down
72 changes: 72 additions & 0 deletions merico/pr/config/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import json
import os
import sys

from lib.chatmark import Checkbox

# Configuration items
CONFIG_ITEMS = {
"pr_review_inline": "PR Review Inline Enabled",
}

# Configuration file paths
GLOBAL_CONFIG_PATH = os.path.join(os.path.expanduser("~"), ".chat", ".workflow_config.json")


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


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)


def is_pre_review_inline_enabled(current_value=False):
print("\n\nEnable PR Review Inline:\n\n")
checkbox = Checkbox(
[
"PR Review Inline Enabled",
],
[current_value],
)
checkbox.render()

print(f"\n\ncheckbox.selections: {checkbox.selections}\n\n")
if len(checkbox.selections) > 0:
return True
if checkbox.selections is None:
return None
return False


def main():
print("Starting configuration of workflow settings...", end="\n\n", flush=True)
print(
"If you want to change access token or host url, "
"please edit the configuration file directly."
)
print("Configuration file is located at:", GLOBAL_CONFIG_PATH, end="\n\n", flush=True)

pr_review_inline_enable = read_config(GLOBAL_CONFIG_PATH, "pr_review_inline")

pr_review_inline_enable = is_pre_review_inline_enabled(pr_review_inline_enable or False)
if pr_review_inline_enable is not None:
save_config(GLOBAL_CONFIG_PATH, "pr_review_inline", pr_review_inline_enable)
print("Workflow settings configuration successful.")
sys.exit(0)


if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions merico/pr/config/command.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description: 'Config required settings for GIT workflows.'
steps:
- run: $devchat_python $command_path/command.py
10 changes: 10 additions & 0 deletions merico/pr/config_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ def read_gitlab_host():
return ""


def read_review_inline_config():
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 "pr_review_inline" in config_data:
return config_data["pr_review_inline"]
return False


def save_github_token(github_token):
config_path = os.path.join(os.path.expanduser("~/.chat"), ".workflow_config.json")

Expand Down
2 changes: 1 addition & 1 deletion merico/pr/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
git+https://gitee.com/imlaji/pr-agent.git@main
git+https://gitee.com/imlaji/pr-agent.git@ad276e206c7e462a689996ee3ada2769b35d5625
git+https://gitee.com/devchat-ai/devchat.git@main

0 comments on commit 7b19535

Please sign in to comment.