<commit_before>package workflow

import (
	"fmt"

	"github.com/go-gorp/gorp"

	"github.com/ovh/cds/engine/api/application"
	"github.com/ovh/cds/engine/api/cache"
	"github.com/ovh/cds/engine/api/environment"
	"github.com/ovh/cds/engine/api/pipeline"
	"github.com/ovh/cds/sdk"
	"github.com/ovh/cds/sdk/log"
)

//Import is able to create a new workflow and all its components
func Import(db gorp.SqlExecutor, store cache.Store, proj *sdk.Project, w *sdk.Workflow, u *sdk.User, force bool, msgChan chan<- sdk.Message) error {
	w.ProjectKey = proj.Key
	w.ProjectID = proj.ID

	mError := new(sdk.MultiError)

	var pipelineLoader = func(n *sdk.WorkflowNode) {
		pip, err := pipeline.LoadPipeline(db, proj.Key, n.Pipeline.Name, true)
		if err != nil {
			log.Warning("workflow.Import> %s > Pipeline %s not found", w.Name, n.Pipeline.Name)
			mError.Append(sdk.NewError(sdk.ErrPipelineNotFound, fmt.Errorf("pipeline %s/%s not found", proj.Key, n.Pipeline.Name)))
			return
		}
		n.Pipeline = *pip
	}
	w.Visit(pipelineLoader)

	var applicationLoader = func(n *sdk.WorkflowNode) {
		if n.Context == nil || n.Context.Application == nil || n.Context.Application.Name == "" {
			return
		}
		app, err := application.LoadByName(db, store, proj.Key, n.Context.Application.Name, u)
		if err != nil {
			log.Warning("workflow.Import> %s > Application %s not found", w.Name, n.Context.Application.Name)
			mError.Append(sdk.NewError(sdk.ErrApplicationNotFound, fmt.Errorf("application %s/%s not found", proj.Key, n.Context.Application.Name)))
			return
		}
		n.Context.Application = app
	}
	w.Visit(applicationLoader)

	var envLoader = func(n *sdk.WorkflowNode) {
		if n.Context == nil || n.Context.Environment == nil || n.Context.Environment.Name == "" {
			return
		}
		env, err := environment.LoadEnvironmentByName(db, proj.Key, n.Context.Environment.Name)
		if err != nil {
			log.Warning("workflow.Import> %s > Environment %s not found", w.Name, n.Context.Environment.Name)
			mError.Append(sdk.NewError(sdk.ErrNoEnvironment, fmt.Errorf("environment %s/%s not found", proj.Key, n.Context.Environment.Name)))
			return
		}
		n.Context.Environment = env
	}
	w.Visit(envLoader)

	if !mError.IsEmpty() {
		return mError
	}

	doUpdate, errE := Exists(db, proj.Key, w.Name)
	if errE != nil {
		return sdk.WrapError(errE, "Import")
	}
<commit_msg>fix (api): import workflow hooks default config (#2034)<commit_after>package workflow

import (
	"fmt"

	"github.com/go-gorp/gorp"

	"github.com/ovh/cds/engine/api/application"
	"github.com/ovh/cds/engine/api/cache"
	"github.com/ovh/cds/engine/api/environment"
	"github.com/ovh/cds/engine/api/pipeline"
	"github.com/ovh/cds/sdk"
	"github.com/ovh/cds/sdk/log"
)

//Import is able to create a new workflow and all its components
func Import(db gorp.SqlExecutor, store cache.Store, proj *sdk.Project, w *sdk.Workflow, u *sdk.User, force bool, msgChan chan<- sdk.Message) error {
	w.ProjectKey = proj.Key
	w.ProjectID = proj.ID

	mError := new(sdk.MultiError)

	var pipelineLoader = func(n *sdk.WorkflowNode) {
		pip, err := pipeline.LoadPipeline(db, proj.Key, n.Pipeline.Name, true)
		if err != nil {
			log.Warning("workflow.Import> %s > Pipeline %s not found", w.Name, n.Pipeline.Name)
			mError.Append(sdk.NewError(sdk.ErrPipelineNotFound, fmt.Errorf("pipeline %s/%s not found", proj.Key, n.Pipeline.Name)))
			return
		}
		n.Pipeline = *pip
	}
	w.Visit(pipelineLoader)

	var applicationLoader = func(n *sdk.WorkflowNode) {
		if n.Context == nil || n.Context.Application == nil || n.Context.Application.Name == "" {
			return
		}
		app, err := application.LoadByName(db, store, proj.Key, n.Context.Application.Name, u)
		if err != nil {
			log.Warning("workflow.Import> %s > Application %s not found", w.Name, n.Context.Application.Name)
			mError.Append(sdk.NewError(sdk.ErrApplicationNotFound, fmt.Errorf("application %s/%s not found", proj.Key, n.Context.Application.Name)))
			return
		}
		n.Context.Application = app
	}
	w.Visit(applicationLoader)

	var envLoader = func(n *sdk.WorkflowNode) {
		if n.Context == nil || n.Context.Environment == nil || n.Context.Environment.Name == "" {
			return
		}
		env, err := environment.LoadEnvironmentByName(db, proj.Key, n.Context.Environment.Name)
		if err != nil {
			log.Warning("workflow.Import> %s > Environment %s not found", w.Name, n.Context.Environment.Name)
			mError.Append(sdk.NewError(sdk.ErrNoEnvironment, fmt.Errorf("environment %s/%s not found", proj.Key, n.Context.Environment.Name)))
			return
		}
		n.Context.Environment = env
	}
	w.Visit(envLoader)

	var hookLoad = func(n *sdk.WorkflowNode) {
		for i := range n.Hooks {
			h := &n.Hooks[i]
			m, err := LoadHookModelByName(db, h.WorkflowHookModel.Name)
			if err != nil {
				log.Warning("workflow.Import> %s > Hook %s not found", w.Name, h.WorkflowHookModel.Name)
				mError.Append(sdk.NewError(sdk.ErrNoEnvironment, fmt.Errorf("hook %s not found", h.WorkflowHookModel.Name)))
				return
			}
			h.WorkflowHookModel = *m
			h.WorkflowHookModelID = m.ID
			for k, v := range m.DefaultConfig {
				if _, has := h.Config[k]; !has {
					h.Config[k] = v
				}
			}
		}
	}
	w.Visit(hookLoad)

	if !mError.IsEmpty() {
		return mError
	}

	doUpdate, errE := Exists(db, proj.Key, w.Name)
	if errE != nil {
		return sdk.WrapError(errE, "Import")
	}

{'fraction_non_alphanumeric': 0.15348221044663135, 'fraction_numerical': 0.000757002271006813, 'mean_word_length': 3.8575367647058822, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6527220', 'n_tokens_mistral': 2068, 'n_tokens_neox': 1918, 'n_words': 397}