Skip to content

Commit

Permalink
feat: sync issue and pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydzhou committed Feb 1, 2024
1 parent c8f3c5b commit 4e307a7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
22 changes: 18 additions & 4 deletions server/tasks/lark/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
)
from model.team import get_code_users_by_openid
from sqlalchemy.orm import aliased
from tasks.github.issue import on_issue_opened
from tasks.github.pull_request import on_pull_request_opened
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 @@ -250,11 +252,13 @@ def sync_issue(
issue_id, issue_link, app_id, message_id, content, data, *args, **kwargs
):
repo_name = ""
is_pr = False
try:
if not issue_id:
path = urlparse(issue_link).path
issue_id = int(path.split("/").pop())
repo_name = path.split("/")[2]
is_pr = path.split("/")[3] == "pull"
except Exception as e:
logging.error(e)

Expand Down Expand Up @@ -324,17 +328,27 @@ def sync_issue(
)
openid = data["event"]["sender"]["sender_id"]["open_id"]
# 这里连三个表查询,所以一次性都查出来
code_users = get_code_users_by_openid([openid] + users)
code_users = get_code_users_by_openid([openid])
# 当前操作的用户
current_code_user_id = code_users[openid][0]

github_app = GitHubAppRepo(
code_application.installation_id, user_id=current_code_user_id
)
response = github_app.get_one_issue(team.name, repo.name, issue_id)
# TODO 先测试一下看这个结果是什么样子?

# 后面需要插入记录,再发卡片,创建话题
logging.debug("get_one_issue %r", response)
repository = github_app.get_repo_info_by_name(team.name, repo.name)
if is_pr:
pull_request = github_app.get_one_pull_requrst(team.name, repo.name, issue_id)
logging.debug("get_one_pull_requrst %r", pull_request)
on_pull_request_opened(
{"action": "opened", "pull_request": pull_request, "repository": repository}
)
else:
issue = github_app.get_one_issue(team.name, repo.name, issue_id)
logging.debug("get_one_issue %r", issue)
on_issue_opened({"action": "opened", "issue": issue, "repository": repository})

return send_chat_failed_tip(
"同步 issue 失败", app_id, message_id, content, data, *args, **kwargs
)
33 changes: 33 additions & 0 deletions server/utils/github/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ def get_repo_info(self, repo_id: str) -> dict | None:
if not team:
return None

return self.get_repo_info_by_name(repo.name)

def get_repo_info_by_name(self, repo_name: str) -> dict | None:
"""Get repo info by repo name.
Args:
repo_name (str): The repo name from GitHub.
Returns:
dict: Repo info.
"""
return self.base_github_rest_api(
f"https://api.github.com/repos/{team.name}/{repo.name}",
"GET",
Expand Down Expand Up @@ -285,6 +296,28 @@ def update_issue(
json=json,
)

def get_one_pull_request(
self,
repo_onwer: str,
repo_name: str,
pull_number: int,
) -> dict | None:
"""Get an issue
Args:
repo_onwer (str): The repo owner.
repo_name (str): The repo name.
pull_number (int): The pull_request id
Returns:
dict: The issue info.
"""

return self.base_github_rest_api(
f"https://api.github.com/repos/{repo_onwer}/{repo_name}/pulls/{pull_number}",
auth_type="install_token",
)

def requested_reviewers(
self,
repo_onwer: str,
Expand Down

0 comments on commit 4e307a7

Please sign in to comment.