From cc51c8adb7a535482c7f219c10d5a611748da782 Mon Sep 17 00:00:00 2001 From: freeziyou <80776877@qq.com> Date: Tue, 20 Feb 2024 16:32:27 +0800 Subject: [PATCH 1/3] add issue creater --- server/tasks/lark/issue.py | 82 ++++++++++++++++++++++++++++++--- server/utils/github/model.py | 1 + server/utils/lark/issue_card.py | 14 ++++++ 3 files changed, 90 insertions(+), 7 deletions(-) diff --git a/server/tasks/lark/issue.py b/server/tasks/lark/issue.py index c23368bb..a374a6cb 100644 --- a/server/tasks/lark/issue.py +++ b/server/tasks/lark/issue.py @@ -80,8 +80,29 @@ def get_assignees_by_issue(issue, team): return assignees +def get_creater_by_issue(issue, team): + code_name = issue.extra["user"].get("login", 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, + ) + .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_issue(issue, team) tags = [i["name"] for i in issue.extra.get("labels", [])] status = issue.extra.get("state", "opened") if status == "closed": @@ -110,6 +131,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 +194,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 +213,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 +231,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 +274,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 +293,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 +344,10 @@ def send_issue_card(issue_id): db.session.commit() assignees = get_assignees_by_issue(issue, team) + creater, _ = get_creater_by_issue(issue, team) + if creater not in assignees and creater: + assignees.append(creater) + users = ( "".join( [f'' for open_id in assignees] @@ -297,7 +359,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 +693,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/utils/github/model.py b/server/utils/github/model.py index c47ff8f0..08a748b0 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 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", ), From 68a71bf9c9cdd2f672b9574c513cf7972fcd4c44 Mon Sep 17 00:00:00 2001 From: freeziyou <80776877@qq.com> Date: Tue, 20 Feb 2024 17:07:19 +0800 Subject: [PATCH 2/3] add pr creater --- server/tasks/lark/pull_request.py | 90 ++++++++++++++++++++++++++++--- server/utils/github/model.py | 2 +- server/utils/lark/pr_card.py | 42 ++++++++++----- 3 files changed, 113 insertions(+), 21 deletions(-) diff --git a/server/tasks/lark/pull_request.py b/server/tasks/lark/pull_request.py index 09572613..20a8a6b9 100644 --- a/server/tasks/lark/pull_request.py +++ b/server/tasks/lark/pull_request.py @@ -93,8 +93,29 @@ def get_assignees_by_pr(pr, team): return assignees +def get_creater_by_pr(pr, team): + code_name = pr.extra["user"].get("login", 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, + ) + .scalar() + ) + return creater, code_name + + 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_pr(pr, team) reviewers = pr.extra.get("requested_reviewers", []) if len(reviewers): @@ -142,6 +163,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 +195,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 +214,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 +255,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 +274,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 +302,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 +371,10 @@ def send_pull_request_card(pull_request_id: str): db.session.commit() assignees = get_assignees_by_pr(pr, team) + creater, _ = get_creater_by_pr(pr, team) + if creater not in assignees and creater: + assignees.append(creater) + users = ( "".join( [f'' for open_id in assignees] @@ -325,7 +387,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 +792,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 08a748b0..b5ed46d0 100644 --- a/server/utils/github/model.py +++ b/server/utils/github/model.py @@ -107,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/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", From 99941be9c63828f666acbcda4c4e3e21c7353ff2 Mon Sep 17 00:00:00 2001 From: freeziyou <80776877@qq.com> Date: Tue, 20 Feb 2024 19:45:33 +0800 Subject: [PATCH 3/3] fix creater --- server/tasks/lark/issue.py | 11 ++++++----- server/tasks/lark/pull_request.py | 25 +++---------------------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/server/tasks/lark/issue.py b/server/tasks/lark/issue.py index a374a6cb..6ebc4c6f 100644 --- a/server/tasks/lark/issue.py +++ b/server/tasks/lark/issue.py @@ -80,9 +80,9 @@ def get_assignees_by_issue(issue, team): return assignees -def get_creater_by_issue(issue, team): - code_name = issue.extra["user"].get("login", None) - +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) @@ -94,6 +94,7 @@ def get_creater_by_issue(issue, team): .filter( TeamMember.team_id == team.id, CodeUser.name == code_name, + CodeUser.status == 0, ) .scalar() ) @@ -102,7 +103,7 @@ def get_creater_by_issue(issue, team): 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_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": @@ -344,7 +345,7 @@ def send_issue_card(issue_id): db.session.commit() assignees = get_assignees_by_issue(issue, team) - creater, _ = get_creater_by_issue(issue, team) + creater, _ = get_creater_by_item(issue, team) if creater not in assignees and creater: assignees.append(creater) diff --git a/server/tasks/lark/pull_request.py b/server/tasks/lark/pull_request.py index 20a8a6b9..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, @@ -93,29 +94,9 @@ def get_assignees_by_pr(pr, team): return assignees -def get_creater_by_pr(pr, team): - code_name = pr.extra["user"].get("login", 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, - ) - .scalar() - ) - return creater, code_name - - 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_pr(pr, team) + creater, code_name = get_creater_by_item(pr, team) reviewers = pr.extra.get("requested_reviewers", []) if len(reviewers): @@ -371,7 +352,7 @@ def send_pull_request_card(pull_request_id: str): db.session.commit() assignees = get_assignees_by_pr(pr, team) - creater, _ = get_creater_by_pr(pr, team) + creater, _ = get_creater_by_item(pr, team) if creater not in assignees and creater: assignees.append(creater)