2022-06-15 19:33:29 +00: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 12:37:46 +00:00
|
|
|
"errors"
|
2022-06-15 19:33:29 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2022-08-15 12:37:46 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml"
|
2022-06-15 19:33:29 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2022-11-06 11:44:04 +00:00
|
|
|
forge_types "github.com/woodpecker-ci/woodpecker/server/forge/types"
|
2022-06-15 19:33:29 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
|
|
|
)
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// Restart a pipeline by creating a new one out of the old and start it
|
|
|
|
func Restart(ctx context.Context, store store.Store, lastBuild *model.Pipeline, user *model.User, repo *model.Repo, envs map[string]string) (*model.Pipeline, error) {
|
2022-06-15 19:33:29 +00:00
|
|
|
switch lastBuild.Status {
|
|
|
|
case model.StatusDeclined,
|
|
|
|
model.StatusBlocked:
|
2022-10-18 01:24:12 +00:00
|
|
|
return nil, ErrBadRequest{Msg: fmt.Sprintf("cannot restart a pipeline with status %s", lastBuild.Status)}
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
var pipelineFiles []*forge_types.FileMeta
|
2022-06-15 19:33:29 +00:00
|
|
|
|
|
|
|
// fetch the old pipeline config from database
|
2022-10-18 01:24:12 +00:00
|
|
|
configs, err := store.ConfigsForPipeline(lastBuild.ID)
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil {
|
2022-10-18 01:24:12 +00:00
|
|
|
msg := fmt.Sprintf("failure to get pipeline config for %s. %s", repo.FullName, err)
|
2022-06-15 19:33:29 +00:00
|
|
|
log.Error().Msgf(msg)
|
|
|
|
return nil, ErrNotFound{Msg: msg}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, y := range configs {
|
2022-11-06 11:44:04 +00:00
|
|
|
pipelineFiles = append(pipelineFiles, &forge_types.FileMeta{Data: y.Data, Name: y.Name})
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If config extension is active we should refetch the config in case something changed
|
|
|
|
if server.Config.Services.ConfigService != nil && server.Config.Services.ConfigService.IsConfigured() {
|
2022-11-06 11:44:04 +00:00
|
|
|
currentFileMeta := make([]*forge_types.FileMeta, len(configs))
|
2022-06-15 19:33:29 +00:00
|
|
|
for i, cfg := range configs {
|
2022-11-06 11:44:04 +00:00
|
|
|
currentFileMeta[i] = &forge_types.FileMeta{Name: cfg.Name, Data: cfg.Data}
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newConfig, useOld, err := server.Config.Services.ConfigService.FetchConfig(ctx, repo, lastBuild, currentFileMeta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrBadRequest{
|
2022-10-18 01:24:12 +00:00
|
|
|
Msg: fmt.Sprintf("On fetching external pipeline config: %s", err),
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !useOld {
|
|
|
|
pipelineFiles = newConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
newBuild := createNewOutOfOld(lastBuild)
|
2022-06-15 19:33:29 +00:00
|
|
|
newBuild.Parent = lastBuild.ID
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
err = store.CreatePipeline(newBuild)
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil {
|
2022-10-18 01:24:12 +00:00
|
|
|
msg := fmt.Sprintf("failure to save pipeline for %s", repo.FullName)
|
2022-06-15 19:33:29 +00:00
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
return nil, fmt.Errorf(msg)
|
|
|
|
}
|
|
|
|
|
2022-08-15 12:37:46 +00:00
|
|
|
if len(configs) == 0 {
|
2022-11-06 11:44:04 +00:00
|
|
|
newBuild, uerr := UpdateToStatusError(store, *newBuild, errors.New("pipeline definition not found"))
|
2022-08-15 12:37:46 +00:00
|
|
|
if uerr != nil {
|
|
|
|
log.Debug().Err(uerr).Msg("failure to update pipeline status")
|
|
|
|
}
|
|
|
|
return newBuild, nil
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if err := persistPipelineConfigs(store, configs, newBuild.ID); err != nil {
|
|
|
|
msg := fmt.Sprintf("failure to persist pipeline config for %s.", repo.FullName)
|
2022-06-15 19:33:29 +00:00
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
return nil, fmt.Errorf(msg)
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
newBuild, pipelineItems, err := createPipelineItems(ctx, store, newBuild, user, repo, pipelineFiles, envs)
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil {
|
2022-08-15 12:37:46 +00:00
|
|
|
if errors.Is(err, &yaml.PipelineParseError{}) {
|
|
|
|
return newBuild, nil
|
|
|
|
}
|
2022-06-15 19:33:29 +00:00
|
|
|
msg := fmt.Sprintf("failure to createBuildItems for %s", repo.FullName)
|
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
return nil, fmt.Errorf(msg)
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
newBuild, err = start(ctx, store, newBuild, user, repo, pipelineItems)
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil {
|
2022-10-18 01:24:12 +00:00
|
|
|
msg := fmt.Sprintf("failure to start pipeline for %s", repo.FullName)
|
2022-06-15 19:33:29 +00:00
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
return nil, fmt.Errorf(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newBuild, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: reuse at create.go too
|
2022-10-18 01:24:12 +00:00
|
|
|
func persistPipelineConfigs(store store.Store, configs []*model.Config, pipelineID int64) error {
|
2022-06-15 19:33:29 +00:00
|
|
|
for _, conf := range configs {
|
2022-10-18 01:24:12 +00:00
|
|
|
pipelineConfig := &model.PipelineConfig{
|
|
|
|
ConfigID: conf.ID,
|
|
|
|
PipelineID: pipelineID,
|
2022-06-15 19:33:29 +00:00
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
err := store.PipelineConfigCreate(pipelineConfig)
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
func createNewOutOfOld(old *model.Pipeline) *model.Pipeline {
|
2022-06-15 19:33:29 +00:00
|
|
|
new := *old
|
|
|
|
new.ID = 0
|
|
|
|
new.Number = 0
|
|
|
|
new.Status = model.StatusPending
|
|
|
|
new.Started = 0
|
|
|
|
new.Finished = 0
|
|
|
|
new.Enqueued = time.Now().UTC().Unix()
|
|
|
|
new.Error = ""
|
|
|
|
return &new
|
|
|
|
}
|