2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2021 Woodpecker 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 datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2023-07-07 11:10:16 +00:00
|
|
|
"xorm.io/builder"
|
2022-10-18 01:24:12 +00:00
|
|
|
"xorm.io/xorm"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2022-10-18 01:24:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s storage) GetPipeline(id int64) (*model.Pipeline, error) {
|
|
|
|
pipeline := &model.Pipeline{}
|
|
|
|
return pipeline, wrapGet(s.engine.ID(id).Get(pipeline))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) GetPipelineNumber(repo *model.Repo, num int64) (*model.Pipeline, error) {
|
2023-07-07 11:10:16 +00:00
|
|
|
pipeline := new(model.Pipeline)
|
|
|
|
return pipeline, wrapGet(s.engine.Where(
|
|
|
|
builder.Eq{"pipeline_repo_id": repo.ID, "pipeline_number": num},
|
|
|
|
).Get(pipeline))
|
2022-10-18 01:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) GetPipelineLast(repo *model.Repo, branch string) (*model.Pipeline, error) {
|
2023-07-07 11:10:16 +00:00
|
|
|
pipeline := new(model.Pipeline)
|
|
|
|
return pipeline, wrapGet(s.engine.
|
|
|
|
Desc("pipeline_number").
|
|
|
|
Where(builder.Eq{"pipeline_repo_id": repo.ID, "pipeline_branch": branch, "pipeline_event": model.EventPush}).
|
|
|
|
Get(pipeline))
|
2022-10-18 01:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) GetPipelineLastBefore(repo *model.Repo, branch string, num int64) (*model.Pipeline, error) {
|
2023-07-07 11:10:16 +00:00
|
|
|
pipeline := new(model.Pipeline)
|
2022-10-18 01:24:12 +00:00
|
|
|
return pipeline, wrapGet(s.engine.
|
2022-10-22 13:54:43 +00:00
|
|
|
Desc("pipeline_number").
|
2023-07-07 11:10:16 +00:00
|
|
|
Where(builder.Lt{"pipeline_id": num}.
|
|
|
|
And(builder.Eq{"pipeline_repo_id": repo.ID, "pipeline_branch": branch})).
|
2022-10-18 01:24:12 +00:00
|
|
|
Get(pipeline))
|
|
|
|
}
|
|
|
|
|
2024-04-25 07:37:42 +00:00
|
|
|
func (s storage) GetPipelineList(repo *model.Repo, p *model.ListOptions, f *model.PipelineFilter) ([]*model.Pipeline, error) {
|
2023-06-12 23:07:52 +00:00
|
|
|
pipelines := make([]*model.Pipeline, 0, 16)
|
2024-04-25 07:37:42 +00:00
|
|
|
|
|
|
|
cond := builder.NewCond().And(builder.Eq{"pipeline_repo_id": repo.ID})
|
|
|
|
|
|
|
|
if f != nil {
|
|
|
|
if f.After != 0 {
|
2024-04-28 08:32:31 +00:00
|
|
|
cond = cond.And(builder.Gt{"pipeline_created": f.After})
|
2024-04-25 07:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if f.Before != 0 {
|
2024-04-28 08:32:31 +00:00
|
|
|
cond = cond.And(builder.Lt{"pipeline_created": f.Before})
|
2024-04-25 07:37:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pipelines, s.paginate(p).Where(cond).
|
2022-10-22 13:54:43 +00:00
|
|
|
Desc("pipeline_number").
|
2022-10-18 01:24:12 +00:00
|
|
|
Find(&pipelines)
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// GetActivePipelineList get all pipelines that are pending, running or blocked.
|
2023-04-30 01:40:13 +00:00
|
|
|
func (s storage) GetActivePipelineList(repo *model.Repo) ([]*model.Pipeline, error) {
|
|
|
|
pipelines := make([]*model.Pipeline, 0)
|
2022-10-18 01:24:12 +00:00
|
|
|
query := s.engine.
|
2022-10-22 13:54:43 +00:00
|
|
|
Where("pipeline_repo_id = ?", repo.ID).
|
|
|
|
In("pipeline_status", model.StatusPending, model.StatusRunning, model.StatusBlocked).
|
|
|
|
Desc("pipeline_number")
|
2022-10-18 01:24:12 +00:00
|
|
|
return pipelines, query.Find(&pipelines)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) GetPipelineCount() (int64, error) {
|
|
|
|
return s.engine.Count(new(model.Pipeline))
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
func (s storage) CreatePipeline(pipeline *model.Pipeline, stepList ...*model.Step) error {
|
2022-10-18 01:24:12 +00:00
|
|
|
sess := s.engine.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-21 23:34:11 +00:00
|
|
|
repoExist, err := sess.Where("repo_id = ?", pipeline.RepoID).Exist(&model.Repo{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !repoExist {
|
|
|
|
return ErrorRepoNotExist{RepoID: pipeline.RepoID}
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// calc pipeline number
|
|
|
|
var number int64
|
2023-07-07 11:10:16 +00:00
|
|
|
if _, err := sess.Select("MAX(pipeline_number)").
|
|
|
|
Table(new(model.Pipeline)).
|
|
|
|
Where("pipeline_repo_id = ?", pipeline.RepoID).
|
|
|
|
Get(&number); err != nil {
|
2022-10-18 01:24:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
pipeline.Number = number + 1
|
|
|
|
|
|
|
|
pipeline.Created = time.Now().UTC().Unix()
|
|
|
|
// only Insert set auto created ID back to object
|
|
|
|
if _, err := sess.Insert(pipeline); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
for i := range stepList {
|
|
|
|
stepList[i].PipelineID = pipeline.ID
|
2022-10-18 01:24:12 +00:00
|
|
|
// only Insert set auto created ID back to object
|
2022-10-28 15:38:53 +00:00
|
|
|
if _, err := sess.Insert(stepList[i]); err != nil {
|
2022-10-18 01:24:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) UpdatePipeline(pipeline *model.Pipeline) error {
|
|
|
|
_, err := s.engine.ID(pipeline.ID).AllCols().Update(pipeline)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-12 20:30:52 +00:00
|
|
|
func (s storage) DeletePipeline(pipeline *model.Pipeline) error {
|
|
|
|
return s.deletePipeline(s.engine.NewSession(), pipeline.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s storage) deletePipeline(sess *xorm.Session, pipelineID int64) error {
|
2024-03-18 19:07:45 +00:00
|
|
|
if err := s.workflowsDelete(sess, pipelineID); err != nil {
|
2024-01-18 21:50:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var confIDs []int64
|
|
|
|
if err := sess.Table(new(model.PipelineConfig)).Select("config_id").Where("pipeline_id = ?", pipelineID).Find(&confIDs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, confID := range confIDs {
|
|
|
|
exist, err := sess.Where(builder.Eq{"config_id": confID}.And(builder.Neq{"pipeline_id": pipelineID})).Exist(new(model.PipelineConfig))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !exist {
|
|
|
|
// this config is only used for this pipeline. so delete it
|
|
|
|
if _, err := sess.Where(builder.Eq{"config_id": confID}).Delete(new(model.Config)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-22 13:54:43 +00:00
|
|
|
if _, err := sess.Where("pipeline_id = ?", pipelineID).Delete(new(model.PipelineConfig)); err != nil {
|
2022-10-18 01:24:12 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-09-02 11:31:10 +00:00
|
|
|
return wrapDelete(sess.ID(pipelineID).Delete(new(model.Pipeline)))
|
2022-10-18 01:24:12 +00:00
|
|
|
}
|