<commit_before>package workflow

import (
	"bytes"
	"context"
	"fmt"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/fsamin/go-dump"
	"github.com/go-gorp/gorp"

	"github.com/ovh/cds/engine/api/cache"
	"github.com/ovh/cds/engine/api/observability"
	"github.com/ovh/cds/engine/api/repositoriesmanager"
	"github.com/ovh/cds/engine/api/services"
	"github.com/ovh/cds/sdk"
	"github.com/ovh/cds/sdk/log"
	"github.com/ovh/cds/sdk/luascript"
	"github.com/ovh/cds/sdk/tracingutils"
)

// processWorkflowRun triggers workflow node for every workflow.
// It contains all the logic for triggers and joins processing.
func processWorkflowRun(ctx context.Context, db gorp.SqlExecutor, store cache.Store, proj *sdk.Project, w *sdk.WorkflowRun, hookEvent *sdk.WorkflowNodeRunHookEvent, manual *sdk.WorkflowNodeRunManual, startingFromNode *int64) (*ProcessorReport, bool, error) {
	if w.Version == 2 {
		log.Debug("Manual run: %+v", manual)
		return processWorkflowDataRun(ctx, db, store, proj, w, hookEvent, manual, startingFromNode)
	}

	// Erase workflow data with old struct for ui compatibility
	if w.Workflow.Root.ID != w.Workflow.WorkflowData.Node.ID {
		data := w.Workflow.Migrate(true)
		w.Workflow.WorkflowData = &data
	}

	var end func()
	ctx, end = observability.Span(ctx, "workflow.processWorkflowRun",
		observability.Tag(observability.TagWorkflowRun, w.Number),
		observability.Tag(observability.TagWorkflow, w.Workflow.Name),
	)
	defer end()

	if w.Header == nil {
		w.Header = sdk.WorkflowRunHeaders{}
	}
	w.Header.Set(sdk.WorkflowRunHeader, strconv.FormatInt(w.Number, 10))
	w.Header.Set(sdk.WorkflowHeader, w.Workflow.Name)
	w.Header.Set(sdk.ProjectKeyHeader, proj.Key)

	// Push data in header to allow tracing
	if observability.Current(ctx).SpanContext().IsSampled() {
		w.Header.Set(tracingutils.SampledHeader, "1")
		w.Header.Set(tracingutils.TraceIDHeader, fmt.Sprintf("%v", observability.Current(ctx).SpanContext().TraceID))
	}

	report := new(ProcessorReport)<commit_msg>fix(api): migrate to new internal model (#3742)<commit_after>package workflow

import (
	"bytes"
	"context"
	"fmt"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/fsamin/go-dump"
	"github.com/go-gorp/gorp"

	"github.com/ovh/cds/engine/api/cache"
	"github.com/ovh/cds/engine/api/observability"
	"github.com/ovh/cds/engine/api/repositoriesmanager"
	"github.com/ovh/cds/engine/api/services"
	"github.com/ovh/cds/sdk"
	"github.com/ovh/cds/sdk/log"
	"github.com/ovh/cds/sdk/luascript"
	"github.com/ovh/cds/sdk/tracingutils"
)

// processWorkflowRun triggers workflow node for every workflow.
// It contains all the logic for triggers and joins processing.
func processWorkflowRun(ctx context.Context, db gorp.SqlExecutor, store cache.Store, proj *sdk.Project, w *sdk.WorkflowRun, hookEvent *sdk.WorkflowNodeRunHookEvent, manual *sdk.WorkflowNodeRunManual, startingFromNode *int64) (*ProcessorReport, bool, error) {
	if w.Version == 2 {
		log.Debug("Manual run: %+v", manual)
		return processWorkflowDataRun(ctx, db, store, proj, w, hookEvent, manual, startingFromNode)
	}

	// Erase workflow data with old struct for ui compatibility
	if w.Workflow.WorkflowData == nil || w.Workflow.Root.ID != w.Workflow.WorkflowData.Node.ID {
		data := w.Workflow.Migrate(true)
		w.Workflow.WorkflowData = &data
	}

	var end func()
	ctx, end = observability.Span(ctx, "workflow.processWorkflowRun",
		observability.Tag(observability.TagWorkflowRun, w.Number),
		observability.Tag(observability.TagWorkflow, w.Workflow.Name),
	)
	defer end()

	if w.Header == nil {
		w.Header = sdk.WorkflowRunHeaders{}
	}
	w.Header.Set(sdk.WorkflowRunHeader, strconv.FormatInt(w.Number, 10))
	w.Header.Set(sdk.WorkflowHeader, w.Workflow.Name)
	w.Header.Set(sdk.ProjectKeyHeader, proj.Key)

	// Push data in header to allow tracing
	if observability.Current(ctx).SpanContext().IsSampled() {
		w.Header.Set(tracingutils.SampledHeader, "1")
		w.Header.Set(tracingutils.TraceIDHeader, fmt.Sprintf("%v", observability.Current(ctx).SpanContext().TraceID))
	}

	report := new(ProcessorReport)
{'fraction_non_alphanumeric': 0.14122893954410307, 'fraction_numerical': 0.003964321110009911, 'mean_word_length': 4.910688140556369, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3284992', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1469, 'n_words': 267}