Skip to content

Commit

Permalink
Merge pull request #223 from ConnectAI-E/freeziyou-021901
Browse files Browse the repository at this point in the history
修复新建 Issue 时未正确显示@问题
  • Loading branch information
lloydzhou authored Feb 21, 2024
2 parents e4da753 + 6d013d4 commit 4cfd725
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 30 deletions.
62 changes: 56 additions & 6 deletions server/tasks/lark/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)
from model.team import get_code_users_by_openid
from sqlalchemy.orm import aliased
from tasks.lark.issue import replace_im_name_to_github_name
from utils.github.repo import GitHubAppRepo
from utils.lark.chat_manual import ChatManual, ChatView
from utils.lark.chat_tip_failed import ChatTipFailed
Expand Down Expand Up @@ -75,7 +76,14 @@ def send_chat_manual(app_id, message_id, content, data, *args, **kwargs):
bot, application = get_bot_by_application_id(app_id)
if not application:
return send_chat_failed_tip(
"找不到对应的应用", app_id, message_id, content, data, *args, bot=bot, **kwargs
"找不到对应的应用",
app_id,
message_id,
content,
data,
*args,
bot=bot,
**kwargs,
)

team = (
Expand All @@ -87,7 +95,14 @@ def send_chat_manual(app_id, message_id, content, data, *args, **kwargs):
)
if not team:
return send_chat_failed_tip(
"找不到对应的项目", app_id, message_id, content, data, *args, bot=bot, **kwargs
"找不到对应的项目",
app_id,
message_id,
content,
data,
*args,
bot=bot,
**kwargs,
)

message = ChatManual(
Expand All @@ -112,7 +127,14 @@ def send_chat_url_message(
bot, application = get_bot_by_application_id(app_id)
if not application:
return send_chat_failed_tip(
"找不到对应的应用", app_id, message_id, content, data, *args, bot=bot, **kwargs
"找不到对应的应用",
app_id,
message_id,
content,
data,
*args,
bot=bot,
**kwargs,
)

team = (
Expand All @@ -124,7 +146,14 @@ def send_chat_url_message(
)
if not team:
return send_chat_failed_tip(
"找不到对应的项目", app_id, message_id, content, data, *args, bot=bot, **kwargs
"找不到对应的项目",
app_id,
message_id,
content,
data,
*args,
bot=bot,
**kwargs,
)

repo_url = f"https://github.com/{team.name}/{repo_name}"
Expand Down Expand Up @@ -217,7 +246,13 @@ def create_issue(
)
if len(repos) > 1:
return send_chat_failed_tip(
"当前群有多个项目,无法唯一确定仓库", app_id, message_id, content, data, *args, **kwargs
"当前群有多个项目,无法唯一确定仓库",
app_id,
message_id,
content,
data,
*args,
**kwargs,
)

if len(repos) == 0:
Expand Down Expand Up @@ -277,6 +312,15 @@ def create_issue(
code_application.installation_id, user_id=current_code_user_id
)
assignees = [code_users[openid][1] for openid in users if openid in code_users]

# 判断 content 中是否有 at
if "mentions" in data["event"]["message"]:
# 替换 content 中的 im_name 为 code_name
body = replace_im_name_to_github_name(
app_id, message_id, {"text": body}, data, team, *args, **kwargs
)
body = body.replace("\n", "\r\n")

response = github_app.create_issue(
team.name, repo.name, title, body, assignees, labels
)
Expand Down Expand Up @@ -334,7 +378,13 @@ def sync_issue(

if len(repos) > 1:
return send_chat_failed_tip(
"当前群有多个项目,无法唯一确定仓库", app_id, message_id, content, data, *args, **kwargs
"当前群有多个项目,无法唯一确定仓库",
app_id,
message_id,
content,
data,
*args,
**kwargs,
)
if len(repos) == 0:
return send_chat_failed_tip(
Expand Down
73 changes: 49 additions & 24 deletions server/tasks/lark/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ def gen_issue_card_by_issue(bot, issue, repo_url, team, maunal=False):
tags=tags,
)

# 处理 description
# 处理 description 中的图片
description = replace_images_with_keys(
issue.description if issue.description else "", bot
)

# 处理 description 中的at
description = replace_code_name_to_im_name(description)

return IssueCard(
repo_url=repo_url,
id=issue.issue_number,
Expand Down Expand Up @@ -171,6 +175,18 @@ def replace_images_with_keys(text, bot):
return replaced_text.replace("![]()", "(请确认图片是否上传成功)")


def replace_code_name_to_im_name(text):
# 处理每行 at, 普通文本
def replacement_func(match):
code_name = match.group(1)
user_id = get_openid_by_code_name(code_name)
# 消息卡片的md和回复消息md的at格式不同
return f'<at id="{user_id}"></at>'

replaced_text = re.sub(r"@(\w+)", replacement_func, text)
return replaced_text


def send_issue_url_message(
app_id, message_id, content, data, *args, typ="view", **kwargs
):
Expand Down Expand Up @@ -587,28 +603,10 @@ def create_issue_comment(app_id, message_id, content, data, *args, **kwargs):

# 判断 content 中是否有 at
if "mentions" in data["event"]["message"]:
# 获得 mentions 中的 openid list
mentions = data["event"]["message"]["mentions"]
openid_list = [mention["id"]["open_id"] for mention in mentions]
code_name_list = []

for openid in openid_list:
# 通过 openid list 获得 code_name_list
code_name_list.append(
get_github_name_by_openid(
openid,
team.id,
app_id,
message_id,
content,
data,
*args,
**kwargs,
)
)

# 替换 content 中的 im_name 为 code_name
comment_text = replace_im_name_to_github_name(content["text"], code_name_list)
comment_text = replace_im_name_to_github_name(
app_id, message_id, content, data, team, *args, **kwargs
)

response = github_app.create_issue_comment(
team.name, repo.name, issue.issue_number, comment_text
Expand All @@ -620,9 +618,36 @@ def create_issue_comment(app_id, message_id, content, data, *args, **kwargs):
return response


def replace_im_name_to_github_name(content, code_name_list):
def replace_im_name_to_github_name(
app_id, message_id, content, data, team, *args, **kwargs
):
# 获得 mentions 中的 openid list
mentions = data["event"]["message"]["mentions"]
openid_list = [mention["id"]["open_id"] for mention in mentions]
code_name_list = []

for openid in openid_list:
# 通过 openid list 获得 code_name_list
code_name_list.append(
get_github_name_by_openid(
openid,
team.id,
app_id,
message_id,
content,
data,
*args,
**kwargs,
)
)

# 替换 content 中的 im_name 为 code_name
return replace_user_to_github_name(content["text"], code_name_list)


def replace_user_to_github_name(content, code_name_list):
"""
replace im name to github name
replace @_user_ to github name
Args:
content (str): content
Expand Down

0 comments on commit 4cfd725

Please sign in to comment.