Skip to content

Commit 87521d5

Browse files
committed
Add workflow status latest service with CI/CD status optimization
1 parent 4b8d72f commit 87521d5

File tree

7 files changed

+765
-0
lines changed

7 files changed

+765
-0
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*
2+
* Copyright (c) 2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pipelineConfig
18+
19+
import (
20+
"github.com/devtron-labs/devtron/pkg/sql"
21+
"github.com/go-pg/pg"
22+
"go.uber.org/zap"
23+
)
24+
25+
type WorkflowStatusLatestRepository interface {
26+
// CI Workflow Status Latest methods
27+
SaveCiWorkflowStatusLatest(model *CiWorkflowStatusLatest) error
28+
UpdateCiWorkflowStatusLatest(model *CiWorkflowStatusLatest) error
29+
GetCiWorkflowStatusLatestByPipelineId(pipelineId int) (*CiWorkflowStatusLatest, error)
30+
GetCiWorkflowStatusLatestByAppId(appId int) ([]*CiWorkflowStatusLatest, error)
31+
DeleteCiWorkflowStatusLatestByPipelineId(pipelineId int) error
32+
33+
// CD Workflow Status Latest methods
34+
SaveCdWorkflowStatusLatest(model *CdWorkflowStatusLatest) error
35+
UpdateCdWorkflowStatusLatest(model *CdWorkflowStatusLatest) error
36+
GetCdWorkflowStatusLatestByPipelineIdAndWorkflowType(pipelineId int, workflowType string) (*CdWorkflowStatusLatest, error)
37+
GetCdWorkflowStatusLatestByAppId(appId int) ([]*CdWorkflowStatusLatest, error)
38+
GetCdWorkflowStatusLatestByPipelineId(pipelineId int) ([]*CdWorkflowStatusLatest, error)
39+
DeleteCdWorkflowStatusLatestByPipelineId(pipelineId int) error
40+
}
41+
42+
type WorkflowStatusLatestRepositoryImpl struct {
43+
dbConnection *pg.DB
44+
logger *zap.SugaredLogger
45+
}
46+
47+
func NewWorkflowStatusLatestRepositoryImpl(dbConnection *pg.DB, logger *zap.SugaredLogger) *WorkflowStatusLatestRepositoryImpl {
48+
return &WorkflowStatusLatestRepositoryImpl{
49+
dbConnection: dbConnection,
50+
logger: logger,
51+
}
52+
}
53+
54+
// CI Workflow Status Latest model
55+
type CiWorkflowStatusLatest struct {
56+
tableName struct{} `sql:"ci_workflow_status_latest" pg:",discard_unknown_columns"`
57+
Id int `sql:"id,pk"`
58+
PipelineId int `sql:"pipeline_id"`
59+
AppId int `sql:"app_id"`
60+
CiWorkflowId int `sql:"ci_workflow_id"`
61+
Status string `sql:"status"`
62+
sql.AuditLog
63+
}
64+
65+
// CD Workflow Status Latest model
66+
type CdWorkflowStatusLatest struct {
67+
tableName struct{} `sql:"cd_workflow_status_latest" pg:",discard_unknown_columns"`
68+
Id int `sql:"id,pk"`
69+
PipelineId int `sql:"pipeline_id"`
70+
AppId int `sql:"app_id"`
71+
EnvironmentId int `sql:"environment_id"`
72+
WorkflowType string `sql:"workflow_type"`
73+
WorkflowRunnerId int `sql:"workflow_runner_id"`
74+
Status string `sql:"status"`
75+
sql.AuditLog
76+
}
77+
78+
// CI Workflow Status Latest methods implementation
79+
func (impl *WorkflowStatusLatestRepositoryImpl) SaveCiWorkflowStatusLatest(model *CiWorkflowStatusLatest) error {
80+
err := impl.dbConnection.Insert(model)
81+
if err != nil {
82+
impl.logger.Errorw("error in saving ci workflow status latest", "err", err, "model", model)
83+
return err
84+
}
85+
return nil
86+
}
87+
88+
func (impl *WorkflowStatusLatestRepositoryImpl) UpdateCiWorkflowStatusLatest(model *CiWorkflowStatusLatest) error {
89+
_, err := impl.dbConnection.Model(model).WherePK().UpdateNotNull()
90+
if err != nil {
91+
impl.logger.Errorw("error in updating ci workflow status latest", "err", err, "model", model)
92+
return err
93+
}
94+
return nil
95+
}
96+
97+
func (impl *WorkflowStatusLatestRepositoryImpl) GetCiWorkflowStatusLatestByPipelineId(pipelineId int) (*CiWorkflowStatusLatest, error) {
98+
model := &CiWorkflowStatusLatest{}
99+
err := impl.dbConnection.Model(model).
100+
Where("pipeline_id = ?", pipelineId).
101+
Select()
102+
if err != nil {
103+
impl.logger.Errorw("error in getting ci workflow status latest by pipeline id", "err", err, "pipelineId", pipelineId)
104+
return nil, err
105+
}
106+
return model, nil
107+
}
108+
109+
func (impl *WorkflowStatusLatestRepositoryImpl) GetCiWorkflowStatusLatestByAppId(appId int) ([]*CiWorkflowStatusLatest, error) {
110+
var models []*CiWorkflowStatusLatest
111+
err := impl.dbConnection.Model(&models).
112+
Where("app_id = ?", appId).
113+
Select()
114+
if err != nil {
115+
impl.logger.Errorw("error in getting ci workflow status latest by app id", "err", err, "appId", appId)
116+
return nil, err
117+
}
118+
return models, nil
119+
}
120+
121+
func (impl *WorkflowStatusLatestRepositoryImpl) DeleteCiWorkflowStatusLatestByPipelineId(pipelineId int) error {
122+
_, err := impl.dbConnection.Model(&CiWorkflowStatusLatest{}).
123+
Where("pipeline_id = ?", pipelineId).
124+
Delete()
125+
if err != nil {
126+
impl.logger.Errorw("error in deleting ci workflow status latest by pipeline id", "err", err, "pipelineId", pipelineId)
127+
return err
128+
}
129+
return nil
130+
}
131+
132+
// CD Workflow Status Latest methods implementation
133+
func (impl *WorkflowStatusLatestRepositoryImpl) SaveCdWorkflowStatusLatest(model *CdWorkflowStatusLatest) error {
134+
err := impl.dbConnection.Insert(model)
135+
if err != nil {
136+
impl.logger.Errorw("error in saving cd workflow status latest", "err", err, "model", model)
137+
return err
138+
}
139+
return nil
140+
}
141+
142+
func (impl *WorkflowStatusLatestRepositoryImpl) UpdateCdWorkflowStatusLatest(model *CdWorkflowStatusLatest) error {
143+
_, err := impl.dbConnection.Model(model).WherePK().UpdateNotNull()
144+
if err != nil {
145+
impl.logger.Errorw("error in updating cd workflow status latest", "err", err, "model", model)
146+
return err
147+
}
148+
return nil
149+
}
150+
151+
func (impl *WorkflowStatusLatestRepositoryImpl) GetCdWorkflowStatusLatestByPipelineIdAndWorkflowType(pipelineId int, workflowType string) (*CdWorkflowStatusLatest, error) {
152+
model := &CdWorkflowStatusLatest{}
153+
err := impl.dbConnection.Model(model).
154+
Where("pipeline_id = ?", pipelineId).
155+
Where("workflow_type = ?", workflowType).
156+
Select()
157+
if err != nil {
158+
impl.logger.Errorw("error in getting cd workflow status latest by pipeline id and workflow type", "err", err, "pipelineId", pipelineId, "workflowType", workflowType)
159+
return nil, err
160+
}
161+
return model, nil
162+
}
163+
164+
func (impl *WorkflowStatusLatestRepositoryImpl) GetCdWorkflowStatusLatestByAppId(appId int) ([]*CdWorkflowStatusLatest, error) {
165+
var models []*CdWorkflowStatusLatest
166+
err := impl.dbConnection.Model(&models).
167+
Where("app_id = ?", appId).
168+
Select()
169+
if err != nil {
170+
impl.logger.Errorw("error in getting cd workflow status latest by app id", "err", err, "appId", appId)
171+
return nil, err
172+
}
173+
return models, nil
174+
}
175+
176+
func (impl *WorkflowStatusLatestRepositoryImpl) GetCdWorkflowStatusLatestByPipelineId(pipelineId int) ([]*CdWorkflowStatusLatest, error) {
177+
var models []*CdWorkflowStatusLatest
178+
err := impl.dbConnection.Model(&models).
179+
Where("pipeline_id = ?", pipelineId).
180+
Select()
181+
if err != nil {
182+
impl.logger.Errorw("error in getting cd workflow status latest by pipeline id", "err", err, "pipelineId", pipelineId)
183+
return nil, err
184+
}
185+
return models, nil
186+
}
187+
188+
func (impl *WorkflowStatusLatestRepositoryImpl) DeleteCdWorkflowStatusLatestByPipelineId(pipelineId int) error {
189+
_, err := impl.dbConnection.Model(&CdWorkflowStatusLatest{}).
190+
Where("pipeline_id = ?", pipelineId).
191+
Delete()
192+
if err != nil {
193+
impl.logger.Errorw("error in deleting cd workflow status latest by pipeline id", "err", err, "pipelineId", pipelineId)
194+
return err
195+
}
196+
return nil
197+
}

0 commit comments

Comments
 (0)