Files
devstar/services/devstar_devcontainer/api_services/OpenDevcontainerAPIService.go
戴明辰 4b6f1b9cb5 !6 k8s Agent for DevStar DevContainer
* DELETE /api/devcontainer?repoId=${repoId} 删除 DevContainer
* refactor
* GET /api/devcontainer?repoId=${repoId}&wait=true 阻塞式等待打开就绪的 DevContainer
* POST /api/devcontainer 创建 DevContainer
* refactored the code
* Updated context usage with cancel function
* 预留接口,适配单机版 DevStar DevContainer
* bugFix: context canceled while deleting k8s CRD DevcontainerApp
* 用户界面删除 k8s CRD DevContainer
* 用户界面创建 DevContainer 并更新 NodePort
* 完成用户界面创建 DevContainer
* transplant test code into DevStar Studio
* refactored API router to /routers/api
* 更改 DevContainer Doc
* 更改 DevContainer namespace
* 特殊仓库重定向
* [Doc] 更新 Kubernetes 部署 DevStar Studio 文档说明,特别是 namespace 管理
* [Doc] 更新 CI脚本说明
* Revert "optimized CI workflow"
* optimized CI workflow
* fix typo
* [feature test]: 测试 Pod 内使用 Kubernetes Operator 功能
* [Optimization] error msg for archived repo
* [Optimization]: display detailed err msg on creating devContainer for …
2024-09-30 06:48:01 +00:00

69 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package api_services
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/repo"
DevcontainersVO "code.gitea.io/gitea/routers/api/devcontainer/vo"
gitea_web_context "code.gitea.io/gitea/services/context"
DevcontainersService "code.gitea.io/gitea/services/devstar_devcontainer"
devcontainer_service_errors "code.gitea.io/gitea/services/devstar_devcontainer/errors"
devcontainer_service_options "code.gitea.io/gitea/services/devstar_devcontainer/options"
"context"
)
// OpenDevcontainerAPIService API 专用获取 DevContainer Service
func OpenDevcontainerAPIService(ctx *gitea_web_context.Context, opts *devcontainer_service_options.AbstractOpenDevcontainerOptions) (*DevcontainersVO.RepoDevContainerVO, error) {
// 0. 检查用户传入参数
if ctx == nil || opts == nil || opts.Actor == nil || opts.RepoId <= 0 {
return nil, devcontainer_service_errors.ErrIllegalParams{
FieldNameList: []string{"ctx", "opts", "opts.Actor", "opts.RepoId"},
}
}
var devcontainerDetails DevcontainersVO.RepoDevContainerVO
// 1. 开启数据库事务,查询 某用户在某仓库的 DevContainer
errTxn := db.WithTx(ctx, func(ctx context.Context) error {
// 1.1 调用 model层查询数据库将 repoId 变换为 Repository 对象
repositoryInDB, err := repo.GetRepositoryByID(ctx, opts.RepoId)
if err != nil || repositoryInDB == nil {
return devcontainer_service_errors.ErrIllegalParams{
FieldNameList: []string{"opts.RepoId"},
}
}
// 1.2 检查该用户在该仓库 是否已经创建过 DevContainer
optsRepoDevcontainer := &DevcontainersVO.RepoDevcontainerOptions{
Actor: opts.Actor,
Repository: repositoryInDB,
}
devcontainerDetails, err = DevcontainersService.GetRepoDevcontainerDetails(ctx, optsRepoDevcontainer)
if err != nil || devcontainerDetails.DevContainerId <= 0 {
return devcontainer_service_errors.ErrDevcontainerNotFound{
Actor: opts.Actor,
Repository: repositoryInDB,
}
}
// 1.3 查询 DevContainer 成功,结束数据库事务
return nil
})
if errTxn != nil {
return nil, errTxn
}
// 2. 调用抽象层获取 DevContainer 最新状态(需要根据用户传入的 wait 参数决定是否要阻塞等待 DevContainer 就绪)
optsOpenDevcontainer := &devcontainer_service_options.OpenDevcontainerAppDispatcherOptions{
Name: devcontainerDetails.DevContainerName,
Wait: opts.Wait,
}
openDevcontainerAbstractAgentVO, err := DevcontainersService.OpenDevcontainerService(ctx, optsOpenDevcontainer)
if err != nil {
return nil, err
}
// 3. 更新VO合并成为真正的 SSH连接信息
devcontainerDetails.DevContainerPort = openDevcontainerAbstractAgentVO.NodePortAssigned
return &devcontainerDetails, nil
}