/
template_product.go
117 lines (98 loc) · 4.08 KB
/
template_product.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
Copyright 2021 The KodeRover Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package service
import (
"fmt"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models/template"
commonrepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb"
templaterepo "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/mongodb/template"
"github.com/koderover/zadig/v2/pkg/setting"
e "github.com/koderover/zadig/v2/pkg/tool/errors"
"go.uber.org/zap"
)
type PipelineResource struct {
Version string `json:"version"`
Kind string `json:"kind"`
}
type Features struct {
Features []string `json:"features"`
}
func GetProductTemplate(productName string, log *zap.SugaredLogger) (*template.Product, error) {
resp, err := templaterepo.NewProductColl().Find(productName)
if err != nil {
log.Errorf("GetProductTemplate error: %v", err)
return nil, e.ErrGetProduct.AddDesc(err.Error())
}
var totalServices []*models.Service
totalServices, err = commonrepo.NewServiceColl().ListMaxRevisionsByProduct(productName)
if err != nil {
return resp, fmt.Errorf("ListMaxRevisionsByProduct err : %s", err)
}
totalBuilds, err := commonrepo.NewBuildColl().List(&commonrepo.BuildListOption{ProductName: productName, IsSort: true})
if err != nil {
return resp, fmt.Errorf("Build.List err : %v", err)
}
totalTests, err := commonrepo.NewTestingColl().List(&commonrepo.ListTestOption{ProductName: productName, IsSort: true})
if err != nil {
return resp, fmt.Errorf("Testing.List err : %v", err)
}
totalEnvs, err := commonrepo.NewProductColl().List(&commonrepo.ProductListOptions{Name: productName, IsSortByUpdateTime: true})
if err != nil {
return resp, fmt.Errorf("Product.List err : %v", err)
}
totalWorkflows, err := commonrepo.NewWorkflowColl().List(&commonrepo.ListWorkflowOption{Projects: []string{productName}, IsSort: true})
if err != nil {
return resp, fmt.Errorf("Workflow.List err : %v", err)
}
totalPiplines, err := commonrepo.NewPipelineColl().List(&commonrepo.PipelineListOption{ProductName: productName, IsPreview: true})
if err != nil {
return resp, fmt.Errorf("Pipeline.List err : %v", err)
}
totalEnvTemplateServiceNum := 0
for _, services := range resp.Services {
totalEnvTemplateServiceNum += len(services)
}
resp.TotalServiceNum = len(totalServices)
if len(totalServices) > 0 {
serviceObj, err := GetServiceTemplate(totalServices[0].ServiceName, totalServices[0].Type, productName, setting.ProductStatusDeleting, totalServices[0].Revision, false, log)
if err != nil {
log.Errorf("GetServiceTemplate err : %s", err)
} else {
resp.LatestServiceUpdateTime = serviceObj.CreateTime
resp.LatestServiceUpdateBy = serviceObj.CreateBy
}
}
resp.TotalBuildNum = len(totalBuilds)
if len(totalBuilds) > 0 {
resp.LatestBuildUpdateTime = totalBuilds[0].UpdateTime
resp.LatestBuildUpdateBy = totalBuilds[0].UpdateBy
}
resp.TotalTestNum = len(totalTests)
if len(totalTests) > 0 {
resp.LatestTestUpdateTime = totalTests[0].UpdateTime
resp.LatestTestUpdateBy = totalTests[0].UpdateBy
}
resp.TotalEnvNum = len(totalEnvs)
if len(totalEnvs) > 0 {
resp.LatestEnvUpdateTime = totalEnvs[0].UpdateTime
resp.LatestEnvUpdateBy = totalEnvs[0].UpdateBy
}
resp.TotalWorkflowNum = len(totalWorkflows) + len(totalPiplines)
if len(totalWorkflows) > 0 {
resp.LatestWorkflowUpdateTime = totalWorkflows[0].UpdateTime
resp.LatestWorkflowUpdateBy = totalWorkflows[0].UpdateBy
}
resp.TotalEnvTemplateServiceNum = totalEnvTemplateServiceNum
return resp, nil
}