Skip to content

Commit

Permalink
Merge pull request #109 from devchat-ai/fix-format
Browse files Browse the repository at this point in the history
Fix format issues in /pr & /github
  • Loading branch information
yangbobo2021 authored May 19, 2024
2 parents bbc53e4 + 71a8def commit ccf6ba3
Show file tree
Hide file tree
Showing 18 changed files with 417 additions and 268 deletions.
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ setup-dev:
@pip install -r requirements-dev.txt
@echo "Done!"

T="."
check:
@echo ${div}
ruff check .
ruff format . --check
ruff check $(T)
ruff format $(T) --check
@echo "Done!"

fix:
@echo ${div}
ruff format .
ruff format $(T)
@echo ${div}
ruff check . --fix
ruff check $(T) --fix
@echo "Done!"
54 changes: 38 additions & 16 deletions merico/github/code_task_summary/command.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import json
import sys
import os
import sys

from devchat.llm import chat_json

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))

from common_util import ui_edit, assert_exit # noqa: E402
from git_api import auto_push, check_git_installed, create_pull_request, get_commit_messages, get_current_branch, get_github_repo, get_issue_info, is_issue_url, read_issue_by_url
from common_util import assert_exit, ui_edit # noqa: E402
from git_api import ( # noqa: E402
check_git_installed,
get_current_branch,
get_github_repo,
get_issue_info,
is_issue_url,
read_issue_by_url,
)


def extract_issue_id(branch_name):
if "#" in branch_name:
return branch_name.split("#")[-1]
return None


# Function to generate a random branch name
PROMPT = (
"You are a coding engineer, required to summarize the ISSUE description into a coding task description of no more than 50 words. \n"
"The ISSUE description is as follows: {issue_body}, please summarize the corresponding coding task description.\n"
"The coding task description should be output in JSON format, in the form of: {{\"summary\": \"code task summary\"}}\n"
"You are a coding engineer, required to summarize the ISSUE description into a coding task description of no more than 50 words. \n" # noqa: E501
"The ISSUE description is as follows: {issue_body}, please summarize the corresponding coding task description.\n" # noqa: E501
'The coding task description should be output in JSON format, in the form of: {{"summary": "code task summary"}}\n' # noqa: E501
)


@chat_json(prompt=PROMPT, model="gpt-4-1106-preview")
def generate_code_task_summary(issue_body):
pass
Expand All @@ -34,45 +45,56 @@ def get_issue_or_task(task):
if is_issue_url(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"]})
else:
return task


def get_issue_json(issue_id, task):
issue = {"id": "no issue id", "title": "", "body": task}
if issue_id:
issue = get_issue_info(issue_id)
assert_exit(not issue, "Failed to retrieve issue with ID: {issue_id}", exit_code=-1)
issue = {"id": issue_id, "html_url": issue["html_url"], "title": issue["title"], "body": issue["body"]}
issue = {
"id": issue_id,
"html_url": issue["html_url"],
"title": issue["title"],
"body": issue["body"],
}
return issue


# Main function
def main():
print("Start update code task summary ...", end="\n\n", flush=True)

is_git_installed = check_git_installed()
assert_exit(not is_git_installed, "Git is not installed.", exit_code=-1)

task = sys.argv[1]

repo_name = get_github_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, task)
assert_exit(not issue["body"], "Failed to retrieve issue with ID: {issue_id}", exit_code=-1)

# Generate 5 branch names
print("Generating code task summary ...", end="\n\n", flush=True)
code_task_summary = generate_code_task_summary(issue_body = issue["body"])
code_task_summary = generate_code_task_summary(issue_body=issue["body"])
assert_exit(not code_task_summary, "Failed to generate code task summary.", exit_code=-1)
assert_exit(not code_task_summary.get("summary", None), "Failed to generate code task summary, missing summary field in result.", exit_code=-1)
assert_exit(
not code_task_summary.get("summary", None),
"Failed to generate code task summary, missing summary field in result.",
exit_code=-1,
)
code_task_summary = code_task_summary["summary"]

# Select branch name
Expand All @@ -81,7 +103,7 @@ def main():
code_task_summary = code_task_summary[0]

# create and checkout branch
print(f"Updating code task summary to config:")
print("Updating code task summary to config:")
config_file = os.path.join(".chat", "complete.config")
if os.path.exists(config_file):
with open(config_file, "r") as f:
Expand All @@ -93,6 +115,6 @@ def main():
json.dump(config, f, indent=4)
print("Code task summary has updated")


if __name__ == "__main__":
main()

21 changes: 13 additions & 8 deletions merico/github/commit/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import subprocess
import sys

from devchat.llm import chat_completion_stream

from lib.chatmark import Checkbox, Form, TextEditor
from lib.ide_service import IDEService
from devchat.llm import chat_completion_stream

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))

from common_util import assert_exit # noqa: E402
from git_api import get_issue_info


diff_too_large_message_en = (
"Commit failed. The modified content is too long "
"and exceeds the model's length limit. "
Expand Down Expand Up @@ -264,9 +264,11 @@ def generate_commit_message_base_diff(user_input, diff, issue):
"""
global language
language_prompt = "You must response commit message in chinese。\n" if language == "zh" else ""
prompt = PROMPT_COMMIT_MESSAGE_BY_DIFF_USER_INPUT.replace("{__DIFF__}", f"{diff}").replace(
"{__USER_INPUT__}", f"{user_input + language_prompt}"
).replace("{__ISSUE__}", f"{issue}")
prompt = (
PROMPT_COMMIT_MESSAGE_BY_DIFF_USER_INPUT.replace("{__DIFF__}", f"{diff}")
.replace("{__USER_INPUT__}", f"{user_input + language_prompt}")
.replace("{__ISSUE__}", f"{issue}")
)

model_token_limit_error = (
diff_too_large_message_en if language == "en" else diff_too_large_message_zh
Expand Down Expand Up @@ -322,7 +324,12 @@ def get_issue_json(issue_id):
if issue_id:
issue = get_issue_info(issue_id)
assert_exit(not issue, "Failed to retrieve issue with ID: {issue_id}", exit_code=-1)
issue = {"id": issue_id, "html_url": issue["html_url"], "title": issue["title"], "body": issue["body"]}
issue = {
"id": issue_id,
"html_url": issue["html_url"],
"title": issue["title"],
"body": issue["body"],
}
return issue


Expand Down Expand Up @@ -392,8 +399,6 @@ def main():
if branch_name:
user_input += "\ncurrent repo branch name is:" + branch_name
commit_message = generate_commit_message_base_diff(user_input, diff, issue)



# TODO
# remove Closes #IssueNumber in commit message
Expand Down
27 changes: 12 additions & 15 deletions merico/github/common_util.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@

import functools
import sys
import os


from lib.chatmark import TextEditor, Form, Radio, Checkbox
from lib.chatmark import Checkbox, Form, Radio, TextEditor


def create_ui_objs(ui_decls, args):
Expand All @@ -24,7 +21,7 @@ def edit_form(uis, args):
ui_objs, editors = create_ui_objs(uis, args)
form = Form(editors)
form.render()

values = []
for obj in ui_objs:
if isinstance(obj, TextEditor):
Expand All @@ -35,47 +32,47 @@ def edit_form(uis, args):
# TODO
pass
return values


def editor(description):
def decorator_edit(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
uis = wrapper.uis[::-1]
return edit_form(uis, args)

if hasattr(func, "uis"):
wrapper.uis = func.uis
else:
wrapper.uis = []
wrapper.uis.append((TextEditor, description))
return wrapper

return decorator_edit


def ui_edit(ui_type, description):
def decorator_edit(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
uis = wrapper.uis[::-1]
return edit_form(uis, args)

if hasattr(func, "uis"):
wrapper.uis = func.uis
else:
wrapper.uis = []
ui_type_class = {
"editor": TextEditor,
"radio": Radio,
"checkbox": Checkbox
}[ui_type]
ui_type_class = {"editor": TextEditor, "radio": Radio, "checkbox": Checkbox}[ui_type]
wrapper.uis.append((ui_type_class, description))
return wrapper

return decorator_edit

def assert_exit(condition, message, exit_code = -1):

def assert_exit(condition, message, exit_code=-1):
if condition:
if exit_code == 0:
print(message, end="\n\n", flush=True)
else:
print(message, end="\n\n", file=sys.stderr, flush=True)
sys.exit(exit_code)
sys.exit(exit_code)
19 changes: 12 additions & 7 deletions merico/github/config/command.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import json
import os
import sys

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))

Expand All @@ -16,20 +16,22 @@ def read_issue_url():
return config_data["issue_repo"]
return ""


def save_issue_url(issue_url):
config_path = os.path.join(os.getcwd(), ".chat", ".workflow_config.json")
# make dirs
os.makedirs(os.path.dirname(config_path), exist_ok=True)

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["issue_repo"] = issue_url
with open(config_path, "w+", encoding="utf-8") as f:
json.dump(config_data, f, indent=4)


def read_github_token():
config_path = os.path.join(os.path.expanduser("~/.chat"), ".workflow_config.json")
if os.path.exists(config_path):
Expand All @@ -39,22 +41,25 @@ def read_github_token():
return config_data["github_token"]
return ""


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)

config_data["github_token"] = github_token
with open(config_path, "w+", encoding="utf-8") as f:
json.dump(config_data, f, indent=4)


@editor("Please specify the issue's repository, "
"If the issue is within this repository, no need to specify. "
"Otherwise, format as: username/repository-name")
@editor(
"Please specify the issue's repository, "
"If the issue is within this repository, no need to specify. "
"Otherwise, format as: username/repository-name"
)
@editor("Input your github TOKEN to access github api:")
def edit_issue(issue_url, github_token):
pass
Expand Down
Loading

0 comments on commit ccf6ba3

Please sign in to comment.