diff --git a/server/tasks/lark/issue.py b/server/tasks/lark/issue.py index c23368bb..6ebc4c6f 100644 --- a/server/tasks/lark/issue.py +++ b/server/tasks/lark/issue.py @@ -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": @@ -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"), @@ -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 = ( @@ -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}" @@ -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() @@ -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 = ( @@ -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}" @@ -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'' for open_id in assignees] @@ -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() @@ -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 diff --git a/server/tasks/lark/pull_request.py b/server/tasks/lark/pull_request.py index 09572613..f4e90cc4 100644 --- a/server/tasks/lark/pull_request.py +++ b/server/tasks/lark/pull_request.py @@ -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, @@ -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): @@ -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, @@ -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 = ( @@ -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}" @@ -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 = ( @@ -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}" @@ -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() @@ -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'' for open_id in assignees] @@ -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() @@ -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 diff --git a/server/utils/github/model.py b/server/utils/github/model.py index c47ff8f0..b5ed46d0 100644 --- a/server/utils/github/model.py +++ b/server/utils/github/model.py @@ -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 @@ -106,7 +107,7 @@ class PullRequest(BaseModel): assignees: Optional[list[User]] = [] base: Branch head: Branch - + user: User comments: int review_comments: int commits: int diff --git a/server/utils/lark/issue_card.py b/server/utils/lark/issue_card.py index 286a0c87..f8a59434 100644 --- a/server/utils/lark/issue_card.py +++ b/server/utils/lark/issue_card.py @@ -11,6 +11,8 @@ def __init__( status="待完成", persons=[], assignees=[], + creater=None, + is_creater_outside=False, tags=[], updated="2022年12月23日 16:32", ): @@ -22,6 +24,9 @@ def __init__( if len(assignees) > 0 else "**待分配**" ) + creater = ( + f"{creater}(组织外用户)" if is_creater_outside else f"" + ) labels = "、".join(tags) if len(tags) > 0 else "**待补充**" action_button = ( FeishuMessageButton("重新打开", type="primary", value={"command": f"/reopen"}) @@ -76,6 +81,15 @@ def __init__( weight=1, vertical_align="top", ), + FeishuMessageColumn( + FeishuMessageMarkdown( + f"🧔 **创建人**\n{creater}", + text_align="left", + ), + width="weighted", + weight=1, + vertical_align="top", + ), flex_mode="bisect", background_style="grey", ), diff --git a/server/utils/lark/pr_card.py b/server/utils/lark/pr_card.py index 64280e48..30cbc73d 100644 --- a/server/utils/lark/pr_card.py +++ b/server/utils/lark/pr_card.py @@ -13,6 +13,8 @@ def __init__( persons=[], assignees=[], reviewers=[], + creater=None, + is_creater_outside=False, status="待合并", merged=False, labels=[], @@ -30,6 +32,9 @@ def __init__( if len(reviewers) > 0 else "**待分配**" ) + creater = ( + f"{creater}(组织外用户)" if is_creater_outside else f"" + ) label = ( "、".join(labels) if len(labels) > 0 else "**待补充**" ) @@ -97,6 +102,15 @@ def __init__( weight=1, vertical_align="top", ), + FeishuMessageColumn( + FeishuMessageMarkdown( + f"🧔 **创建人**\n{creater}", + text_align="left", + ), + width="weighted", + weight=1, + vertical_align="top", + ), flex_mode="bisect", background_style="grey", ), @@ -108,19 +122,23 @@ def __init__( background_style="grey", ), FeishuMessageAction( - FeishuMessageButton("已合并", type="default", value={"value": ""}) - if merged - else FeishuMessageButton( - "合并 PR", type="primary", value={"command": f"/merge"} + ( + FeishuMessageButton("已合并", type="default", value={"value": ""}) + if merged + else FeishuMessageButton( + "合并 PR", type="primary", value={"command": f"/merge"} + ) ), - FeishuMessageButton( - "重新打开 PR", - type="primary", - value={"command": "/deny" if merged else "/reopen"}, - ) - if status == "已关闭" - else FeishuMessageButton( - "关闭 PR", type="danger", value={"command": f"/close"} + ( + FeishuMessageButton( + "重新打开 PR", + type="primary", + value={"command": "/deny" if merged else "/reopen"}, + ) + if status == "已关闭" + else FeishuMessageButton( + "关闭 PR", type="danger", value={"command": f"/close"} + ) ), FeishuMessageButton( "查看 File Changed",