You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: pipeline_helper.go
    path of file: ./repos/incubator-devlake/backend/server/services
    
    the code of the file until where you have to start completion: /*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 services

import (
	"fmt"
	"time"

	"github.com/apache/incubator-devlake/core/dal"
	"github.com/apache/incubator-devlake/core/errors"
	"github.com/apache/incubator-devlake/core/models"
	"github.com/apache/incubator-devlake/helpers/dbhelper"
)

// CreateDbPipeline returns a NewPipeline
func CreateDbPipeline(newPipeline *models.NewPipeline) (pipeline *models.Pipeline, err errors.Error) {
	pipeline = &models.Pipeline{}
	txHelper := dbhelper.NewTxHelper(basicRes, &err)
	defer txHelper.End()
	tx := txHelper.Begin()
	errors.Must(txHelper.LockTablesTimeout(2*time.Second,
		dal.LockTables{
			{Table: "_devlake_pipelines", Exclusive: true},
			{Table: "_devlake_pipeline_labels", Exclusive: true},
			{Table: "_devlake_tasks", Exclusive: true},
		},
	))
	if err !=
    
func CreateDbPipeline(newPipeline *models.NewPipeline) (pipeline *models.Pipeline, err errors.Error) {
	pipeline = &models.Pipeline{}
	txHelper := dbhelper.NewTxHelper(basicRes, &err)
	defer txHelper.End()
	tx := txHelper.Begin()
	errors.Must(txHelper.LockTablesTimeout(2*time.Second,
		dal.LockTables{
			{Table: "_devlake_pipelines", Exclusive: true},
			{Table: "_devlake_pipeline_labels", Exclusive: true},
			{Table: "_devlake_tasks", Exclusive: true},
		},
	))
	if err != nil {
		err = errors.BadInput.Wrap(err, "failed to lock pipeline table, is there any pending pipeline or deletion?")
		return
	}
	if newPipeline.BlueprintId > 0 {
		count := errors.Must1(tx.Count(
			dal.From(&models.Pipeline{}),
			dal.Where("blueprint_id = ? AND status IN ?", newPipeline.BlueprintId, models.PendingTaskStatus),
		))
		// some pipeline is ruunning , get the detail and output them.
		if count > 0 {
			return nil, errors.BadInput.New("there are pending pipelines of current blueprint already")
		}
	}
	// create pipeline object from posted data
	dbPipeline := &models.Pipeline{
		Name:          newPipeline.Name,
		FinishedTasks: 0,
		Status:        models.TASK_CREATED,
		Message:       "",
		SpentSeconds:  0,
		Plan:          newPipeline.Plan,
		SyncPolicy:    newPipeline.SyncPolicy,
	}
	if newPipeline.BlueprintId != 0 {
		dbPipeline.BlueprintId = newPipeline.BlueprintId
	}

	// save pipeline to database
	errors.Must(tx.Create(dbPipeline))
	labels := make([]models.DbPipelineLabel, 0)
	for _, label := range newPipeline.Labels {
		labels = append(labels, models.DbPipelineLabel{
			PipelineId: dbPipeline.ID,
			Name:       label,
		})
	}
	if len(newPipeline.Labels) > 0 {
		errors.Must(tx.Create(&labels))
	}

	// create tasks accordingly
	for i := range newPipeline.Plan {
		for j := range newPipeline.Plan[i] {
			logger.Debug(fmt.Sprintf("plan[%d][%d] is %+v\n", i, j, newPipeline.Plan[i][j]))
			pipelineTask := newPipeline.Plan[i][j]
			newTask := &models.NewTask{
				PipelineTask: pipelineTask,
				PipelineId:   dbPipeline.ID,
				PipelineRow:  i + 1,
				PipelineCol:  j + 1,
			}
			_ = errors.Must1(createTask(newTask, tx))
			// sync task state back to pipeline
			dbPipeline.TotalTasks += 1
		}
	}
	if dbPipeline.TotalTasks == 0 {
		err = errors.BadInput.New("no task to run")
		return
	}

	// update tasks state
	errors.Must(tx.Update(dbPipeline))
	dbPipeline.Labels = newPipeline.Labels
	return dbPipeline, nil
}