Skip to content

Commit abf4502

Browse files
committed
feat: add new issue command
1 parent 84cc241 commit abf4502

File tree

6 files changed

+165
-0
lines changed

6 files changed

+165
-0
lines changed

new-issue/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# new issue
2+
Help you to create github issue, with a certain format.
3+
4+
## Usage

new-issue/bug/command.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
description: 创建一个bug类型的issue
2+
hint: question
3+
input: required
4+
steps:
5+
- run: $devchat_python $command_path/../command.py ".devchat/information/issue/bug.txt$input"

new-issue/command.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import sys
2+
import os
3+
import json
4+
5+
from devchat.llm.chat import chat_json
6+
from github import Github
7+
from github import Auth
8+
# from common_util import editor
9+
10+
def read_config():
11+
config_path = os.path.join(os.getcwd(), '.devchat', 'information', 'config', 'config.json')
12+
if not os.path.exists(config_path):
13+
print('Config file does not exist')
14+
return None
15+
with open(config_path, 'r') as f:
16+
return json.load(f)
17+
18+
def write_config(repo_path, github_token):
19+
config_path = os.path.join(os.getcwd(), '.devchat', 'information', 'config', 'config.json')
20+
with open(config_path, 'w') as f:
21+
json.dump({'repo_path': repo_path, 'github_token': github_token}, f)
22+
23+
PROMPT= prompt = """
24+
pls return a JSON object without line break(the response must be parsed as json, not contain any redundant chars), including a string field Title and a markdown string field called Body.
25+
{information}
26+
{request}
27+
{context}
28+
"""
29+
@chat_json(prompt=PROMPT)
30+
def get_issue_content(information, request, context):
31+
pass
32+
33+
PROMPT= prompt = """
34+
I will request you to write an issue. The request is consists of two parts, Firstly, I will give a context list(organized by concept name). You may choose most related ones and return it as a json object(with key files) according to the requst content. Then I will ask you for issue content with the context you chosen. Now is the first request, pls only return an json object with context files you chosen. context files: {context_list}, issue content:
35+
`{request_content}`
36+
"""
37+
@chat_json(prompt=PROMPT)
38+
def get_related_context_file(context_list, request_content):
39+
pass
40+
41+
# @editor("Please specify the issue's repository, "
42+
# "If the issue is within this repository, no need to specify. "
43+
# "Otherwise, format as: username/repository-name")
44+
# @editor("Input your github TOKEN to access github api:")
45+
# def set_config(repo_path, github_token):
46+
# pass
47+
48+
def get_request(path):
49+
if not os.path.exists(path):
50+
return
51+
with open(path, 'r') as f:
52+
request = f.read()
53+
return request
54+
55+
def read_api_config():
56+
config = read_config()
57+
# set config by devlake ui
58+
# repo_path = ''
59+
# github_token = ''
60+
# if config != None:
61+
# repo_path = config['repo_path']
62+
# github_token = config['github_token']
63+
# else:
64+
# # request user config
65+
# repo_path, github_token = set_config(repo_path, github_token)
66+
67+
# write_config(repo_path, github_token)
68+
repo_path = config['repo_path']
69+
github_token = config['github_token']
70+
return repo_path, github_token
71+
def create_github_issue(github_token, repo_path, issue_content, issue_title):
72+
auth = Auth.Token(github_token)
73+
g = Github(auth=auth)
74+
repo = g.get_repo(repo_path)
75+
issue = repo.create_issue(title=issue_title, body=issue_content)
76+
print(issue, flush=True)
77+
78+
def main():
79+
# get user input
80+
user_input = sys.argv[1]
81+
params = user_input.strip().split(' ')
82+
user_content = params[1]
83+
print(f'reading request...\n', flush=True)
84+
request_path = os.path.join(os.getcwd(), params[0])
85+
request = get_request(request_path)
86+
if request == '':
87+
print('Request file does not exist', flush=True)
88+
return
89+
# get repository context
90+
information = ''
91+
# get request context
92+
print(f'reading context list\n', flush=True)
93+
# get context list
94+
context_list = os.listdir(os.path.join(os.getcwd(), '.devchat', 'information', 'context'))
95+
# print(context_list, flush=True)
96+
print(f'[ai]choosing context files...\n', flush=True)
97+
response = get_related_context_file(context_list=context_list, request_content=request + user_content)
98+
print(response)
99+
chosen_context_files = response['files']
100+
print(f'chosen context files: {chosen_context_files}\n', flush=True)
101+
print(f'reading context...\n', flush=True)
102+
for context_file in chosen_context_files:
103+
context_file_path = os.path.join(os.getcwd(), '.devchat', 'information', 'context', context_file)
104+
if not os.path.exists(context_file_path):
105+
print(f'context file {context_file} does not exist\n', flush=True)
106+
return
107+
with open(context_file_path, 'r') as f:
108+
information += f.read()
109+
print(f'context read\n', flush=True)
110+
111+
print(f'reading config...\n', flush=True)
112+
# check if github token is configured
113+
repo_path, github_token = read_api_config()
114+
print(f'[ai] generating issue content...\n', flush=True)
115+
# # debug
116+
# print(information)
117+
# print(request)
118+
# print(user_content)
119+
# get issue_content from ai
120+
issue_content=get_issue_content(information=information, request=request, context=user_content)
121+
# print issue content and wait for user confirmation
122+
print(issue_content, flush=True)
123+
# TODO:: add user confirmation
124+
print(f'creating issue...\n', flush=True)
125+
create_github_issue(github_token, repo_path, issue_content['Body'], issue_content['Title'])
126+
127+
# request github api to create issue
128+
print(f'issue created\n', flush=True)
129+
130+
131+
if __name__ == "__main__":
132+
main()

new-issue/command.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
description: 创建一个issue
2+
hint: question
3+
input: required
4+
steps:
5+
- run: $devchat_python $command_path/command.py "$input"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
description: 创建一个feature-request类型的issue
2+
hint: question
3+
input: required
4+
steps:
5+
- run: $devchat_python $command_path/../command.py ".devchat/information/issue/feature-request.txt$input"

new-issue/requirements.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
certifi==2024.2.2
2+
cffi==1.16.0
3+
charset-normalizer==3.3.2
4+
cryptography==42.0.5
5+
Deprecated==1.2.14
6+
idna==3.6
7+
pycparser==2.21
8+
PyGithub==2.2.0
9+
PyJWT==2.8.0
10+
PyNaCl==1.5.0
11+
requests==2.31.0
12+
typing_extensions==4.10.0
13+
urllib3==2.2.1
14+
wrapt==1.16.0

0 commit comments

Comments
 (0)