2022-06-15 21:33:29 +02:00
|
|
|
// Copyright 2022 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 pipeline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-15 14:37:46 +02:00
|
|
|
"errors"
|
2022-06-15 21:33:29 +02:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2023-12-08 09:15:08 +02:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
|
|
|
forge_types "go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
2022-06-15 21:33:29 +02:00
|
|
|
)
|
|
|
|
|
2024-05-13 22:58:21 +02:00
|
|
|
// Restart a pipeline by creating a new one out of the old and start it.
|
2024-02-11 19:42:33 +02:00
|
|
|
func Restart(ctx context.Context, store store.Store, lastPipeline *model.Pipeline, user *model.User, repo *model.Repo, envs map[string]string) (*model.Pipeline, error) {
|
2024-04-16 08:04:55 +02:00
|
|
|
forge, err := server.Config.Services.Manager.ForgeFromRepo(repo)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("failure to load forge for repo '%s'", repo.FullName)
|
|
|
|
log.Error().Err(err).Str("repo", repo.FullName).Msg(msg)
|
|
|
|
return nil, fmt.Errorf(msg)
|
|
|
|
}
|
|
|
|
|
2023-05-14 14:18:43 +02:00
|
|
|
switch lastPipeline.Status {
|
2022-06-15 21:33:29 +02:00
|
|
|
case model.StatusDeclined,
|
|
|
|
model.StatusBlocked:
|
2023-05-14 14:18:43 +02:00
|
|
|
return nil, &ErrBadRequest{Msg: fmt.Sprintf("cannot restart a pipeline with status %s", lastPipeline.Status)}
|
2022-06-15 21:33:29 +02:00
|
|
|
}
|
|
|
|
|
2023-10-08 14:05:06 +02:00
|
|
|
// fetch the old pipeline config from the database
|
2023-05-14 14:18:43 +02:00
|
|
|
configs, err := store.ConfigsForPipeline(lastPipeline.ID)
|
2022-06-15 21:33:29 +02:00
|
|
|
if err != nil {
|
2024-01-10 21:57:12 +02:00
|
|
|
log.Error().Err(err).Msgf("failure to get pipeline config for %s", repo.FullName)
|
|
|
|
return nil, &ErrNotFound{Msg: fmt.Sprintf("failure to get pipeline config for %s. %s", repo.FullName, err)}
|
2022-06-15 21:33:29 +02:00
|
|
|
}
|
|
|
|
|
2024-02-11 19:42:33 +02:00
|
|
|
var pipelineFiles []*forge_types.FileMeta
|
2022-06-15 21:33:29 +02:00
|
|
|
for _, y := range configs {
|
2022-11-06 13:44:04 +02:00
|
|
|
pipelineFiles = append(pipelineFiles, &forge_types.FileMeta{Data: y.Data, Name: y.Name})
|
2022-06-15 21:33:29 +02:00
|
|
|
}
|
|
|
|
|
2024-02-11 19:42:33 +02:00
|
|
|
// If the config service is active we should refetch the config in case something changed
|
|
|
|
configService := server.Config.Services.Manager.ConfigServiceFromRepo(repo)
|
|
|
|
pipelineFiles, err = configService.Fetch(ctx, forge, user, repo, lastPipeline, pipelineFiles, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &ErrBadRequest{
|
|
|
|
Msg: fmt.Sprintf("On fetching external pipeline config: %s", err),
|
2022-06-15 21:33:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-14 14:18:43 +02:00
|
|
|
newPipeline := createNewOutOfOld(lastPipeline)
|
2024-05-06 11:47:02 +02:00
|
|
|
newPipeline.Parent = lastPipeline.Number
|
2022-06-15 21:33:29 +02:00
|
|
|
|
2023-05-14 14:18:43 +02:00
|
|
|
err = store.CreatePipeline(newPipeline)
|
2022-06-15 21:33:29 +02:00
|
|
|
if err != nil {
|
2022-10-18 03:24:12 +02:00
|
|
|
msg := fmt.Sprintf("failure to save pipeline for %s", repo.FullName)
|
2022-06-15 21:33:29 +02:00
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
return nil, fmt.Errorf(msg)
|
|
|
|
}
|
|
|
|
|
2022-08-15 14:37:46 +02:00
|
|
|
if len(configs) == 0 {
|
2024-06-04 08:30:54 +02:00
|
|
|
newPipeline, uErr := UpdateToStatusError(store, *newPipeline, errors.New("pipeline definition not found"))
|
|
|
|
if uErr != nil {
|
|
|
|
log.Debug().Err(uErr).Msg("failure to update pipeline status")
|
2023-05-14 14:18:43 +02:00
|
|
|
} else {
|
2024-04-16 08:04:55 +02:00
|
|
|
updatePipelineStatus(ctx, forge, newPipeline, repo, user)
|
2022-08-15 14:37:46 +02:00
|
|
|
}
|
2023-05-14 14:18:43 +02:00
|
|
|
return newPipeline, nil
|
2022-08-15 14:37:46 +02:00
|
|
|
}
|
2023-10-08 14:05:06 +02:00
|
|
|
if err := linkPipelineConfigs(store, configs, newPipeline.ID); err != nil {
|
2022-10-18 03:24:12 +02:00
|
|
|
msg := fmt.Sprintf("failure to persist pipeline config for %s.", repo.FullName)
|
2022-06-15 21:33:29 +02:00
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
return nil, fmt.Errorf(msg)
|
|
|
|
}
|
|
|
|
|
2024-04-16 08:04:55 +02:00
|
|
|
newPipeline, pipelineItems, err := createPipelineItems(ctx, forge, store, newPipeline, user, repo, pipelineFiles, envs)
|
2022-06-15 21:33:29 +02:00
|
|
|
if err != nil {
|
2023-11-03 12:44:03 +02:00
|
|
|
msg := fmt.Sprintf("failure to createPipelineItems for %s", repo.FullName)
|
2022-06-15 21:33:29 +02:00
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
return nil, fmt.Errorf(msg)
|
|
|
|
}
|
|
|
|
|
2024-04-16 08:04:55 +02:00
|
|
|
if err := prepareStart(ctx, forge, store, newPipeline, user, repo); err != nil {
|
2024-02-22 16:48:29 +02:00
|
|
|
msg := fmt.Sprintf("failure to prepare pipeline for %s", repo.FullName)
|
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
return nil, fmt.Errorf(msg)
|
|
|
|
}
|
|
|
|
|
2024-04-16 08:04:55 +02:00
|
|
|
newPipeline, err = start(ctx, forge, store, newPipeline, user, repo, pipelineItems)
|
2022-06-15 21:33:29 +02:00
|
|
|
if err != nil {
|
2022-10-18 03:24:12 +02:00
|
|
|
msg := fmt.Sprintf("failure to start pipeline for %s", repo.FullName)
|
2022-06-15 21:33:29 +02:00
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
return nil, fmt.Errorf(msg)
|
|
|
|
}
|
|
|
|
|
2023-05-14 14:18:43 +02:00
|
|
|
return newPipeline, nil
|
2022-06-15 21:33:29 +02:00
|
|
|
}
|
|
|
|
|
2023-10-08 14:05:06 +02:00
|
|
|
func linkPipelineConfigs(store store.Store, configs []*model.Config, pipelineID int64) error {
|
2022-06-15 21:33:29 +02:00
|
|
|
for _, conf := range configs {
|
2022-10-18 03:24:12 +02:00
|
|
|
pipelineConfig := &model.PipelineConfig{
|
|
|
|
ConfigID: conf.ID,
|
|
|
|
PipelineID: pipelineID,
|
2022-06-15 21:33:29 +02:00
|
|
|
}
|
2022-10-18 03:24:12 +02:00
|
|
|
err := store.PipelineConfigCreate(pipelineConfig)
|
2022-06-15 21:33:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
func createNewOutOfOld(old *model.Pipeline) *model.Pipeline {
|
2023-03-18 21:35:27 +02:00
|
|
|
newPipeline := *old
|
|
|
|
newPipeline.ID = 0
|
|
|
|
newPipeline.Number = 0
|
|
|
|
newPipeline.Status = model.StatusPending
|
|
|
|
newPipeline.Started = 0
|
|
|
|
newPipeline.Finished = 0
|
2023-11-03 12:44:03 +02:00
|
|
|
newPipeline.Errors = nil
|
2023-03-18 21:35:27 +02:00
|
|
|
return &newPipeline
|
2022-06-15 21:33:29 +02:00
|
|
|
}
|