Skip to content

Commit

Permalink
Merge pull request #224 from ConnectAI-E/freeziyou-022001
Browse files Browse the repository at this point in the history
添加 Issue 和 Pr 创建人
  • Loading branch information
lloydzhou authored Feb 21, 2024
2 parents f3978f8 + 99941be commit e4da753
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 28 deletions.
83 changes: 76 additions & 7 deletions server/tasks/lark/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,30 @@ def get_assignees_by_issue(issue, team):
return assignees


def get_creater_by_item(item, team):
code_name = item.extra["user"].get("login", None)
creater = None
if code_name:
creater = (
db.session.query(IMUser.openid)
.join(TeamMember, TeamMember.im_user_id == IMUser.id)
.join(
CodeUser,
CodeUser.id == TeamMember.code_user_id,
)
.filter(
TeamMember.team_id == team.id,
CodeUser.name == code_name,
CodeUser.status == 0,
)
.scalar()
)
return creater, code_name


def gen_issue_card_by_issue(bot, issue, repo_url, team, maunal=False):
assignees = get_assignees_by_issue(issue, team)
creater, code_name = get_creater_by_item(issue, team)
tags = [i["name"] for i in issue.extra.get("labels", [])]
status = issue.extra.get("state", "opened")
if status == "closed":
Expand Down Expand Up @@ -110,6 +132,8 @@ def gen_issue_card_by_issue(bot, issue, repo_url, team, maunal=False):
title=issue.title,
description=description,
status=status,
creater=creater if creater else code_name,
is_creater_outside=False if creater else True,
assignees=assignees,
tags=tags,
updated=issue.modified.strftime("%Y-%m-%d %H:%M:%S"),
Expand Down Expand Up @@ -171,7 +195,14 @@ def send_issue_url_message(
bot, application = get_bot_by_application_id(app_id)
if not application:
return send_issue_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 @@ -183,7 +214,14 @@ def send_issue_url_message(
)
if not team:
return send_issue_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 All @@ -194,7 +232,14 @@ def send_issue_url_message(
)
else:
return send_issue_failed_tip(
"找不到对应的项目", app_id, message_id, content, data, *args, bot=bot, **kwargs
"找不到对应的项目",
app_id,
message_id,
content,
data,
*args,
bot=bot,
**kwargs,
)
# 回复到话题内部
return bot.reply(message_id, message).json()
Expand Down Expand Up @@ -230,7 +275,14 @@ def send_issue_manual(app_id, message_id, content, data, *args, **kwargs):
bot, application = get_bot_by_application_id(app_id)
if not application:
return send_issue_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 @@ -242,7 +294,14 @@ def send_issue_manual(app_id, message_id, content, data, *args, **kwargs):
)
if not team:
return send_issue_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 @@ -286,6 +345,10 @@ def send_issue_card(issue_id):
db.session.commit()

assignees = get_assignees_by_issue(issue, team)
creater, _ = get_creater_by_item(issue, team)
if creater not in assignees and creater:
assignees.append(creater)

users = (
"".join(
[f'<at user_id="{open_id}"></at>' for open_id in assignees]
Expand All @@ -297,7 +360,7 @@ def send_issue_card(issue_id):
message_id,
# 第一条话题消息,直接放repo_url
FeishuTextMessage(
users + f" {repo_url}/issues/{issue.issue_number}"
f"{users} {repo_url}/issues/{issue.issue_number}"
),
reply_in_thread=True,
).json()
Expand Down Expand Up @@ -631,7 +694,13 @@ def get_github_name_by_openid(

if not code_user_id:
return send_issue_failed_tip(
"找不到对应的 GitHub 用户", app_id, message_id, content, data, *args, **kwargs
"找不到对应的 GitHub 用户",
app_id,
message_id,
content,
data,
*args,
**kwargs,
)

# 第三步:如果找到了 code_user_id,使用它在 bind_user 表中查询 name
Expand Down
71 changes: 63 additions & 8 deletions server/tasks/lark/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from model.team import get_assignees_by_openid
from tasks.lark.issue import (
gen_comment_post_message,
get_creater_by_item,
get_github_name_by_openid,
replace_im_name_to_github_name,
replace_images_with_keys,
Expand Down Expand Up @@ -95,6 +96,7 @@ def get_assignees_by_pr(pr, team):

def gen_pr_card_by_pr(pr: PullRequest, repo_url, team, maunal=False):
assignees = get_assignees_by_pr(pr, team)
creater, code_name = get_creater_by_item(pr, team)
reviewers = pr.extra.get("requested_reviewers", [])

if len(reviewers):
Expand Down Expand Up @@ -142,6 +144,8 @@ def gen_pr_card_by_pr(pr: PullRequest, repo_url, team, maunal=False):
status=status,
merged=merged,
persons=[], # TODO:应该是所有有写权限的人
creater=creater if creater else code_name,
is_creater_outside=False if creater else True,
assignees=assignees,
reviewers=reviewers,
labels=labels,
Expand Down Expand Up @@ -172,7 +176,14 @@ def send_pull_request_manual(app_id, message_id, content, data, *args, **kwargs)
bot, application = get_bot_by_application_id(app_id)
if not application:
return send_pull_request_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 @@ -184,7 +195,14 @@ def send_pull_request_manual(app_id, message_id, content, data, *args, **kwargs)
)
if not team:
return send_pull_request_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 @@ -218,7 +236,14 @@ def send_pull_request_url_message(
bot, application = get_bot_by_application_id(app_id)
if not application:
return send_pull_request_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 @@ -230,7 +255,14 @@ def send_pull_request_url_message(
)
if not team:
return send_pull_request_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 All @@ -251,7 +283,14 @@ def send_pull_request_url_message(
)
else:
return send_pull_request_failed_tip(
"找不到对应的项目", app_id, message_id, content, data, *args, bot=bot, **kwargs
"找不到对应的项目",
app_id,
message_id,
content,
data,
*args,
bot=bot,
**kwargs,
)
# 回复到话题内部
return bot.reply(message_id, message).json()
Expand Down Expand Up @@ -313,6 +352,10 @@ def send_pull_request_card(pull_request_id: str):
db.session.commit()

assignees = get_assignees_by_pr(pr, team)
creater, _ = get_creater_by_item(pr, team)
if creater not in assignees and creater:
assignees.append(creater)

users = (
"".join(
[f'<at user_id="{open_id}"></at>' for open_id in assignees]
Expand All @@ -325,7 +368,7 @@ def send_pull_request_card(pull_request_id: str):
message_id,
# TODO 第一条话题消息,直接放repo_url
FeishuTextMessage(
f"{users}{repo_url}/pull/{pr.pull_request_number}"
f"{users} {repo_url}/pull/{pr.pull_request_number}"
),
reply_in_thread=True,
).json()
Expand Down Expand Up @@ -730,10 +773,22 @@ def change_pull_request_reviewer(
)
if "id" not in response:
return send_pull_request_failed_tip(
"更新 Pull Request 审核人失败", app_id, message_id, content, data, *args, **kwargs
"更新 Pull Request 审核人失败",
app_id,
message_id,
content,
data,
*args,
**kwargs,
)
else:
send_pull_request_success_tip(
"更新 Pull Request 审核人成功", app_id, message_id, content, data, *args, **kwargs
"更新 Pull Request 审核人成功",
app_id,
message_id,
content,
data,
*args,
**kwargs,
)
return response
3 changes: 2 additions & 1 deletion server/utils/github/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Issue(BaseModel):
comments: int
created_at: str
updated_at: str
user: User
assignee: Optional[User] = None
assignees: Optional[list[User]] = []
pull_request: Optional[PRInIssue] = None
Expand Down Expand Up @@ -106,7 +107,7 @@ class PullRequest(BaseModel):
assignees: Optional[list[User]] = []
base: Branch
head: Branch

user: User
comments: int
review_comments: int
commits: int
Expand Down
14 changes: 14 additions & 0 deletions server/utils/lark/issue_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def __init__(
status="待完成",
persons=[],
assignees=[],
creater=None,
is_creater_outside=False,
tags=[],
updated="2022年12月23日 16:32",
):
Expand All @@ -22,6 +24,9 @@ def __init__(
if len(assignees) > 0
else "**<font color='red'>待分配</font>**"
)
creater = (
f"{creater}(组织外用户)" if is_creater_outside else f"<at id={creater}></at>"
)
labels = "、".join(tags) if len(tags) > 0 else "**<font color='red'>待补充</font>**"
action_button = (
FeishuMessageButton("重新打开", type="primary", value={"command": f"/reopen"})
Expand Down Expand Up @@ -76,6 +81,15 @@ def __init__(
weight=1,
vertical_align="top",
),
FeishuMessageColumn(
FeishuMessageMarkdown(
f"🧔 <font color='grey'>**创建人**</font>\n{creater}",
text_align="left",
),
width="weighted",
weight=1,
vertical_align="top",
),
flex_mode="bisect",
background_style="grey",
),
Expand Down
Loading

0 comments on commit e4da753

Please sign in to comment.