Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change chat_group table schema #163

Merged
merged 16 commits into from
Feb 1, 2024
11 changes: 5 additions & 6 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ jobs:
steps:
- name: Check out the repo
uses: actions/checkout@v2

- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_ACCOUNT }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Build and push main app Docker image
uses: docker/build-push-action@v2
with:
Expand All @@ -38,26 +38,25 @@ jobs:
uses: actions/checkout@v2
with:
submodules: 'true'

- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_ACCOUNT }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Build and push website Docker image
uses: docker/build-push-action@v2
with:
context: .
file: deploy/Dockerfile.proxy
push: true
tags: connectai/gitmaya-proxy:latest

- name: Build and push website Docker image-SaaS
uses: docker/build-push-action@v2
with:
context: .
file: deploy/Dockerfile.proxy.saas
push: true
tags: connectai/gitmaya-proxy:saas

65 changes: 61 additions & 4 deletions server/model/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ class Repo(Base):
owner_bind_id = db.Column(
ObjID(12), ForeignKey("bind_user.id"), nullable=True, comment="项目所有者"
)
chat_group_id = db.Column(
ObjID(12), ForeignKey("chat_group_v1.id"), nullable=True, comment="项目群ID"
)
repo_id = db.Column(db.String(128), nullable=True, comment="repo_id")
name = db.Column(db.String(128), nullable=True, comment="名称")
description = db.Column(db.String(1024), nullable=True, comment="描述")
Expand Down Expand Up @@ -262,10 +265,7 @@ class IMApplication(Base):


class ChatGroup(Base):
__tablename__ = "chat_group"
repo_id = db.Column(
ObjID(12), ForeignKey("repo.id"), nullable=True, comment="属于哪一个项目"
)
__tablename__ = "chat_group_v1"
im_application_id = db.Column(
ObjID(12), ForeignKey("im_application.id"), nullable=True, comment="哪一个项目创建的"
)
Expand Down Expand Up @@ -351,6 +351,63 @@ def create():
if "exist" in str(e):
db.create_all()

try:
db.session.query(ChatGroup).first()
except Exception as e:
if "exist" in str(e):
db.create_all()
db.session.execute(
text(
"alter table repo add chat_group_id binary(12) after owner_bind_id"
)
)

class ChatGroupOld(Base):
__tablename__ = "chat_group"
repo_id = db.Column(
ObjID(12), ForeignKey("repo.id"), nullable=True, comment="属于哪一个项目"
)

im_application_id = db.Column(
ObjID(12),
ForeignKey("im_application.id"),
nullable=True,
comment="哪一个项目创建的",
)
chat_id = db.Column(db.String(128), nullable=True, comment="chat_id")
name = db.Column(db.String(128), nullable=True, comment="群名称")
description = db.Column(db.String(256), nullable=True, comment="群描述")
extra = db.Column(
JSONStr(2048),
nullable=True,
server_default=text("'{}'"),
comment="其他字段",
)

for group in db.session.query(ChatGroupOld).all():
if (
not db.session.query(ChatGroup)
.filter(ChatGroup.id == group.id)
.limit(1)
.scalar()
):
db.session.add(
ChatGroup(
id=group.id,
im_application_id=group.im_application_id,
chat_id=group.chat_id,
name=group.name,
description=group.description,
extra=group.extra,
created=group.created,
modified=group.modified,
)
)
db.session.query(Repo).filter(Repo.id == group.repo_id).update(
dict(chat_group_id=group.id)
)
db.session.commit()


# add command function to cli commands
app.cli.add_command(create)
12 changes: 9 additions & 3 deletions server/model/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class RepoWithUsers(Repo):
group = relationship(
ChatGroup,
primaryjoin=and_(
ChatGroup.repo_id == Repo.id,
ChatGroup.id == Repo.chat_group_id,
ChatGroup.status == 0,
),
viewonly=True,
Expand Down Expand Up @@ -202,6 +202,9 @@ def get_team_repo(team_id, user_id, page=1, size=20):
CodeApplication.status == 0,
RepoWithUsers.status == 0,
)
).order_by(
Repo.chat_group_id.desc(), # 已经关联的在前面
Repo.created.desc(), # 创建时间倒序
)
total = query.count()
if total == 0:
Expand Down Expand Up @@ -463,7 +466,7 @@ def create_repo_chat_group_by_repo_id(user_id, team_id, repo_id, chat_name=None)
chat_group = (
db.session.query(ChatGroup)
.filter(
ChatGroup.repo_id == repo.id,
ChatGroup.id == repo.chat_group_id,
ChatGroup.status == 0,
)
.first()
Expand Down Expand Up @@ -539,14 +542,17 @@ def create_repo_chat_group_by_repo_id(user_id, team_id, repo_id, chat_name=None)
chat_group_id = ObjID.new_id()
chat_group = ChatGroup(
id=chat_group_id,
repo_id=repo.id,
im_application_id=application.id,
chat_id=chat_id,
name=name,
description=description,
extra=result,
)
db.session.add(chat_group)
# 更改连表规则,创建新的群之后,需要更新repo.chat_group_id
db.session.query(Repo).filter(
Repo.id == repo.id,
).update(dict(chat_group_id=chat_group_id))
db.session.commit()
# send card message, and pin repo card
tasks.send_repo_to_chat_group.delay(repo.id, app_id, chat_id)
Expand Down
2 changes: 1 addition & 1 deletion server/tasks/github/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def on_push(data: dict | None) -> list:

# 发送 Commit Log 信息
chat_group = (
db.session.query(ChatGroup).filter(ChatGroup.repo_id == repo.id).first()
db.session.query(ChatGroup).filter(ChatGroup.id == repo.chat_group_id).first()
)
if not chat_group:
app.logger.info(f"ChatGroup not found: {repo.name}")
Expand Down
44 changes: 35 additions & 9 deletions server/tasks/lark/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .base import (
get_bot_by_application_id,
get_chat_group_by_chat_id,
get_git_object_by_message_id,
get_repo_name_by_repo_id,
with_authenticated_github,
)
Expand Down Expand Up @@ -61,7 +62,7 @@ def send_chat_manual(app_id, message_id, content, data, *args, **kwargs):
repo = (
db.session.query(Repo)
.filter(
Repo.id == chat_group.repo_id,
Repo.chat_group_id == chat_group.id,
Repo.status == 0,
)
.first()
Expand Down Expand Up @@ -187,19 +188,44 @@ def create_issue(
return send_chat_failed_tip(
"找不到项目群", app_id, message_id, content, data, *args, **kwargs
)
repo = (
db.session.query(Repo)
.filter(
Repo.id == chat_group.repo_id,
Repo.status == 0,
repos = []
try:
# 如果是在话题内运行命令(repo/issue/pull_request)尝试找到对应的repo
if len(repos) == 0:
root_id = data["event"]["message"].get("root_id", "")
if root_id:
repo, issue, pr = tasks.get_git_object_by_message_id(root_id)
if repo:
repos = [repo]
elif issue or pr:
repo_id = issue.repo_id if issue else pr.repo_id
repo = db.session.query(Repo).filter(Repo.id == repo_id).first()
if repo:
repos = [repo]
except Exception as e:
logging.error(e)

if len(repos) == 0:
repos = (
db.session.query(Repo)
.filter(
Repo.chat_group_id == chat_group.id,
Repo.status == 0,
)
.all()
)
.first()
)
if not repo:
if len(repos) > 1:
return send_chat_failed_tip(
"当前群有多个项目,无法唯一确定仓库", app_id, message_id, content, data, *args, **kwargs
)

if len(repos) == 0:
return send_chat_failed_tip(
"找不到项目", app_id, message_id, content, data, *args, **kwargs
)

repo = repos[0] # 能找到唯一的仓库才执行

code_application = (
db.session.query(CodeApplication)
.filter(
Expand Down
17 changes: 12 additions & 5 deletions server/tasks/lark/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,16 @@ def send_issue_card(issue_id):
"""
issue = db.session.query(Issue).filter(Issue.id == issue_id).first()
if issue:
repo = db.session.query(Repo).filter(Repo.id == issue.repo_id).first()
if not repo:
return False
chat_group = (
db.session.query(ChatGroup)
.filter(
ChatGroup.repo_id == issue.repo_id,
ChatGroup.id == repo.chat_group_id,
)
.first()
)
repo = db.session.query(Repo).filter(Repo.id == issue.repo_id).first()
if chat_group and repo:
bot, application = get_bot_by_application_id(chat_group.im_application_id)
team = db.session.query(Team).filter(Team.id == application.team_id).first()
Expand Down Expand Up @@ -303,10 +305,13 @@ def send_issue_comment(issue_id, comment, user_name: str):
"""
issue = db.session.query(Issue).filter(Issue.id == issue_id).first()
if issue:
repo = db.session.query(Repo).filter(Repo.id == issue.repo_id).first()
if not repo:
return False
chat_group = (
db.session.query(ChatGroup)
.filter(
ChatGroup.repo_id == issue.repo_id,
ChatGroup.id == repo.chat_group_id,
)
.first()
)
Expand Down Expand Up @@ -355,14 +360,16 @@ def update_issue_card(issue_id: str):

issue = db.session.query(Issue).filter(Issue.id == issue_id).first()
if issue:
repo = db.session.query(Repo).filter(Repo.id == issue.repo_id).first()
if not repo:
return False
chat_group = (
db.session.query(ChatGroup)
.filter(
ChatGroup.repo_id == issue.repo_id,
ChatGroup.id == repo.chat_group_id,
)
.first()
)
repo = db.session.query(Repo).filter(Repo.id == issue.repo_id).first()

if chat_group and repo:
bot, application = get_bot_by_application_id(chat_group.im_application_id)
Expand Down
Loading
Loading