From 03d32b580cd2f5c9572a83aaa09010403f8ad154 Mon Sep 17 00:00:00 2001 From: zhanghan Date: Fri, 12 Sep 2025 11:39:20 +0800 Subject: [PATCH] Obtain scripts of different versions --- .../controller/script_version.go | 18 ++++ .../script_library/dao/script_version.go | 85 +++++++++++++++++++ .../script_library/model/script_version.go | 39 +++++++++ .../internal/module/script_library/router.go | 4 + .../script_library/service/script_version.go | 10 +++ 5 files changed, 156 insertions(+) create mode 100644 automation/server/internal/module/script_library/controller/script_version.go create mode 100644 automation/server/internal/module/script_library/dao/script_version.go create mode 100644 automation/server/internal/module/script_library/model/script_version.go create mode 100644 automation/server/internal/module/script_library/service/script_version.go diff --git a/automation/server/internal/module/script_library/controller/script_version.go b/automation/server/internal/module/script_library/controller/script_version.go new file mode 100644 index 00000000..10f2f07a --- /dev/null +++ b/automation/server/internal/module/script_library/controller/script_version.go @@ -0,0 +1,18 @@ +package controller + +import ( + "gitee.com/openeuler/PilotGo/sdk/response" + "github.com/gin-gonic/gin" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/script_library/service" +) + +func GetScriptVersions(c *gin.Context) { + script_id := c.Param("script_id") + + data, err := service.GetScriptVersions(script_id) + if err != nil { + response.Fail(c, nil, err.Error()) + return + } + response.Success(c, data, "success") +} diff --git a/automation/server/internal/module/script_library/dao/script_version.go b/automation/server/internal/module/script_library/dao/script_version.go new file mode 100644 index 00000000..d68ce5fe --- /dev/null +++ b/automation/server/internal/module/script_library/dao/script_version.go @@ -0,0 +1,85 @@ +package dao + +import ( + "encoding/json" + "fmt" + + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/global" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/script_library/model" +) + +func GetScriptVersions(scriptId string) (*model.ScriptVersionResponse, error) { + sql := ` +SELECT + s.id AS script_id, + s.name AS name, + s.script_type AS script_type, + s.description AS description, + s.is_public AS is_public, + CAST(( + SELECT COALESCE(JSON_ARRAYAGG( + JSON_OBJECT( + 'id', sv.id, + 'script_id', sv.script_id, + 'content', sv.content, + 'version', sv.version, + 'version_desc', sv.version_desc, + 'status', sv.status = 1, + 'creator', sv.creator, + 'created_at', sv.created_at, + 'last_modify_user', sv.last_modify_user, + 'last_modify_updated_at', sv.last_modify_updated_at + ) + ), JSON_ARRAY()) + FROM script_version sv + WHERE sv.script_id = s.id + ) AS CHAR) AS versions, + CAST(( + SELECT COALESCE(JSON_ARRAYAGG( + JSON_OBJECT( + 'id', t.id, + 'name', t.name, + 'description', t.description, + 'creator', t.creator, + 'created_at', t.created_at, + 'last_modify_user', t.last_modify_user, + 'last_modify_updated_at', t.last_modify_updated_at + ) + ), JSON_ARRAY()) + FROM tag t + WHERE FIND_IN_SET(t.name, s.tags) + ) AS CHAR) AS tags +FROM script s +WHERE s.id = ? +` + var row model.ScriptVersionRow + if err := global.App.MySQL.Raw(sql, scriptId).Scan(&row).Error; err != nil { + return &model.ScriptVersionResponse{}, fmt.Errorf("查询脚本版本失败: %w", err) + } + + var tags []model.Tag + if row.Tags == "" { + tags = []model.Tag{} + } else if err := json.Unmarshal([]byte(row.Tags), &tags); err != nil { + return &model.ScriptVersionResponse{}, fmt.Errorf("解析标签失败: %w", err) + } + + var scriptVersions []model.ScriptVersion + if row.Versions == "" { + scriptVersions = []model.ScriptVersion{} + } else if err := json.Unmarshal([]byte(row.Versions), &scriptVersions); err != nil { + return &model.ScriptVersionResponse{}, fmt.Errorf("解析版本失败: %w", err) + } + + resp := &model.ScriptVersionResponse{ + ID: row.ScriptID, + Name: row.Name, + ScriptType: row.ScriptType, + Description: row.Description, + IsPublic: row.IsPublic, + Tags: tags, + ScriptVersions: scriptVersions, + } + + return resp, nil +} diff --git a/automation/server/internal/module/script_library/model/script_version.go b/automation/server/internal/module/script_library/model/script_version.go new file mode 100644 index 00000000..42f93e83 --- /dev/null +++ b/automation/server/internal/module/script_library/model/script_version.go @@ -0,0 +1,39 @@ +package model + +type ScriptVersion struct { + ID int `json:"id" gorm:"primaryKey;type:int;not null;comment:'脚本版本ID'"` + ScriptID string `json:"script_id" gorm:"type:varchar(36);not null;comment:'脚本ID'"` + Content string `json:"content" gorm:"type:text;not null;comment:'脚本内容'"` + Version string `json:"version" gorm:"type:varchar(50);not null;comment:'脚本版本号'"` + VersionDesc string `json:"version_desc" gorm:"type:varchar(500);comment:'脚本版本描述'"` + Status bool `json:"status" gorm:"type:boolean;not null;default:false;comment:'脚本版本状态,true表示上线,false表示禁用'"` + Creator string `json:"creator" gorm:"type:varchar(100);not null;comment:'创建者'"` + CreatedAt string `json:"created_at" gorm:"comment:'创建时间'"` + LastModifyUser string `json:"last_modify_user" gorm:"type:varchar(100);not null;comment:'最后修改者'"` + LastModifyUpdatedAt string `json:"last_modify_updated_at" gorm:"comment:'最后修改时间'"` +} + +type ScriptVersionResponse struct { + ID string `json:"id"` + Name string `json:"name"` + ScriptType string `json:"script_type"` + Description string `json:"description"` + Tags []Tag `json:"tags"` + IsPublic bool `json:"is_public"` + ScriptVersions []ScriptVersion `json:"script_versions"` +} + +type ScriptVersionRow struct { + // Script 字段 + ScriptID string `json:"script_id"` + Name string `json:"name"` + ScriptType string `json:"script_type"` + Description string `json:"description"` + IsPublic bool `json:"is_public"` + + // JSON Script versions + Versions string `json:"versions"` + + // JSON tags + Tags string `json:"tags"` +} diff --git a/automation/server/internal/module/script_library/router.go b/automation/server/internal/module/script_library/router.go index 5aabcda9..93389212 100644 --- a/automation/server/internal/module/script_library/router.go +++ b/automation/server/internal/module/script_library/router.go @@ -11,6 +11,10 @@ func ScriptLibraryHandler(router *gin.RouterGroup) { api.POST("/add", controller.AddScriptHandler) api.GET("/list", controller.ScriptListHandler) + versionGroup := api.Group("/:script_id/scriptVersion") + { + versionGroup.GET("/list", controller.GetScriptVersions) + } } tag := router.Group("/tag") diff --git a/automation/server/internal/module/script_library/service/script_version.go b/automation/server/internal/module/script_library/service/script_version.go new file mode 100644 index 00000000..1eff661f --- /dev/null +++ b/automation/server/internal/module/script_library/service/script_version.go @@ -0,0 +1,10 @@ +package service + +import ( + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/script_library/dao" + "openeuler.org/PilotGo/PilotGo-plugin-automation/internal/module/script_library/model" +) + +func GetScriptVersions(scriptId string) (*model.ScriptVersionResponse, error) { + return dao.GetScriptVersions(scriptId) +} -- Gitee