-
Notifications
You must be signed in to change notification settings - Fork 2
/
issue-trans.py
134 lines (116 loc) · 4.71 KB
/
issue-trans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os, openai, argparse, tools
import dotenv
dotenv.load_dotenv(dotenv.find_dotenv())
parser = argparse.ArgumentParser(description="Translation")
parser.add_argument("--input", type=str, required=True, help="GitHub issue URL, for example, https://github.com/your-org/your-repository/issues/3692")
parser.add_argument("--token", type=str, required=False, help="GitHub access token, for example, github_pat_xxx_yyyyyy")
parser.add_argument("--proxy", type=str, required=False, help="OpenAI API proxy, for example, x.y.z")
parser.add_argument("--key", type=str, required=False, help="OpenAI API key, for example, xxxyyyzzz")
args = parser.parse_args()
tools.github_token_init(args.token)
tools.openai_init(args.key, args.proxy)
logs = []
logs.append(f"issue: {args.input}")
logs.append(f"token: {len(os.environ.get('GITHUB_TOKEN'))}B")
logs.append(f"proxy: {len(openai.api_base)}B")
logs.append(f"key: {len(openai.api_key)}B")
print(f"run with {', '.join(logs)}")
issue = tools.parse_issue_url(args.input)
j_issue_res = tools.query_issue(issue["owner"], issue["name"], issue["number"])
comments = j_issue_res['comments']
comment_trans_by_gpt = False
for index, j_res_c in enumerate(comments):
c_id = j_res_c["id"]
c_author = j_res_c["author"]["login"]
c_url = j_res_c["url"]
c_body = j_res_c["body"]
print("")
print(f"===============Comment(#{index+1})===============")
print(f"ID: {c_id}")
print(f"Author: {c_author}")
print(f"URL: {c_url}")
print(f"Body:\n{c_body}\n")
print(f"Updating......")
(c_body_trans, comment_trans_by_gpt, real_translated) = tools.gpt_translate(c_body, comment_trans_by_gpt)
if real_translated:
print(f"Body:\n{c_body_trans}\n")
try:
tools.update_issue_comment(c_id, tools.wrap_magic(c_body_trans))
print(f"Updated ok")
except tools.GithubGraphQLException as e:
if e.is_forbidden():
print(f"Warning!!! Ignore update comment {c_id} failed, forbidden, {e.errors}")
else:
raise e
id = j_issue_res["id"]
title = j_issue_res["title"]
body = j_issue_res["body"]
has_gpt_label = False
has_en_native_label = False
labels4print=[]
for label in j_issue_res["labels"]:
if label["name"] == tools.LABEL_TRANS_NAME:
has_gpt_label = True
if label["name"] == tools.LABEL_ENGLISH_NATIVE:
has_en_native_label = True
labels4print.append(f"{label['id']}({label['name']})")
print("")
print(f"===============ISSUE===============")
print(f"ID: {id}")
print(f"Url: {args.input}")
print(f"Title: {title}")
print(f"Labels: {', '.join(labels4print)}")
print(f"Body:\n{body}\n")
print(f"Updating......")
issue_changed = False
issue_trans_by_gpt = False
title_trans = title
body_trans = body
if tools.already_english(title):
print(f"Title is already english, skip")
else:
(title_trans, trans_by_gpt, real_translated) = tools.gpt_translate(title, issue_trans_by_gpt)
if trans_by_gpt:
issue_trans_by_gpt = True
if real_translated:
issue_changed = True
print(f"Title: {title_trans}")
if tools.TRANS_MAGIC in body:
issue_trans_by_gpt = True
print(f"Body is already translated, skip")
elif tools.already_english(body):
print(f"Body is already english, skip")
else:
(body_trans, trans_by_gpt, real_translated) = tools.gpt_translate(body, issue_trans_by_gpt)
if trans_by_gpt:
issue_trans_by_gpt = True
if real_translated:
issue_changed = True
print(f"Body:\n{body_trans}\n")
if not issue_changed:
print(f"Nothing changed, skip")
else:
try:
tools.update_issue(id, title_trans, tools.wrap_magic(body_trans))
print(f"Updated ok")
except tools.GithubGraphQLException as e:
if e.is_forbidden():
print(f"Warning!!! Ignore update issue {id} failed, forbidden, {e.errors}")
else:
raise e
any_by_gpt = comment_trans_by_gpt or issue_trans_by_gpt
if not any_by_gpt or has_gpt_label:
print(f"Label is already set, skip")
else:
print(f"Add label {tools.LABEL_TRANS_NAME}")
label_id = tools.query_label_id(issue["owner"], issue["name"], tools.LABEL_TRANS_NAME)
print(f"Query LABEL_TRANS_NAME={tools.LABEL_TRANS_NAME}, got LABEL_ID={label_id}")
tools.add_label(id, label_id)
print(f"Add label ok, {label_id}({tools.LABEL_TRANS_NAME})")
if not any_by_gpt and not has_gpt_label and not has_en_native_label:
print(f"Add label {tools.LABEL_ENGLISH_NATIVE}")
label_id = tools.query_label_id(issue["owner"], issue["name"], tools.LABEL_ENGLISH_NATIVE)
print(f"Query LABEL_ENGLISH_NATIVE={tools.LABEL_ENGLISH_NATIVE}, got LABEL_ID={label_id}")
tools.add_label(id, label_id)
print(f"Add label ok, {label_id}({tools.LABEL_ENGLISH_NATIVE})")
print("\nOK\n")