<commit_before>package api

import (
	"context"
	"net/http"
	"sort"

	"github.com/gorilla/mux"

	"github.com/ovh/cds/engine/api/project"
	"github.com/ovh/cds/engine/api/workflow"
	"github.com/ovh/cds/engine/service"
	"github.com/ovh/cds/sdk"
)

func (api *API) getWorkflowTriggerConditionHandler() service.Handler {
	return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
		vars := mux.Vars(r)
		key := vars["key"]
		name := vars["permWorkflowName"]

		id, errID := requestVarInt(r, "nodeID")
		if errID != nil {
			return errID
		}

		proj, errproj := project.Load(api.mustDB(), api.Cache, key, deprecatedGetUser(ctx), project.LoadOptions.WithVariables, project.LoadOptions.WithIntegrations)
		if errproj != nil {
			return sdk.WrapError(errproj, "getWorkflowTriggerConditionHandler> Unable to load project")
		}

		wf, errw := workflow.Load(ctx, api.mustDB(), api.Cache, proj, name, deprecatedGetUser(ctx), workflow.LoadOptions{})
		if errw != nil {
			return sdk.WrapError(errw, "getWorkflowTriggerConditionHandler> Unable to load workflow")
		}

		data := struct {
			Operators      map[string]string `json:"operators"`
			ConditionNames []string          `json:"names"`
		}{
			Operators: sdk.WorkflowConditionsOperators,
		}

		wr, errr := workflow.LoadLastRun(api.mustDB(), key, name, workflow.LoadRunOptions{})
		if errr != nil {
			if !sdk.ErrorIs(errr, sdk.ErrWorkflowNotFound) {
				return sdk.WrapError(errr, "getWorkflowTriggerConditionHandler> Unable to load last run workflow")
			}
		}

		refNode := wf.WorkflowData.NodeByID(id)
		if refNode == nil {
			return sdk.WrapError(sdk.ErrWorkflowNodeNotFound, "getWorkflowTriggerConditionHandler> Unable to load workflow node")
		}

		params := []sdk.Parameter{}
		// If there is a workflow run, try to get build parameter from it
		if wr != nil {
			var errp error
			params, errp = workflow.NodeBuildParametersFromRun(*wr, id)
			if errp != nil {
				return sdk.WrapError(errp, "getWorkflowTriggerConditionHandler> Unable to load build parameters from workflow run")
			}
		}

		// If node node found in last workflow run
		if len(params) == 0 {
			var errp error
			ancestorIds := refNode.Ancestors(wf.WorkflowData)
			params, errp = workflow.NodeBuildParametersFromWorkflow(ctx, api.mustDB(), api.Cache, proj, wf, refNode, ancestorIds)
			if errp != nil {
				return sdk.WrapError(errp, "getWorkflowTriggerConditionHandler> Unable to load build parameters from workflow")
			}
			sdk.AddParameter(&params, "cds.dest.pipeline", sdk.StringParameter, "")
			sdk.AddParameter(&params, "cds.status", sdk.StringParameter, "")
			sdk.AddParameter(&params, "cds.manual", sdk.StringParameter, "")

			if refNode.Context != nil && refNode.Context.ApplicationID != 0 {
				sdk.AddParameter(&params, "cds.dest.application", sdk.StringParameter, "")
			}
			if refNode.Context != nil && refNode.Context.EnvironmentID != 0 {
				sdk.AddParameter(&params, "cds.dest.environment", sdk.StringParameter, "")
			}
		}

		if refNode.IsLinkedToRepo(wf) {
			if sdk.ParameterFind(&params, "git.repository") == nil {
				data.ConditionNames = append(data.ConditionNames, "git.repository")
				data.ConditionNames = append(data.ConditionNames, "git.branch")
				data.ConditionNames = append(data.ConditionNames, "git.message")
				data.ConditionNames = append(data.ConditionNames, "git.author")
				data.ConditionNames = append(data.ConditionNames, "git.hash")
			}
		}

		for _, p := range params {
			data.ConditionNames = append(data.ConditionNames, p.Name)
		}

		sort.Strings(data.ConditionNames)
		return service.WriteJSON(w, data, http.StatusOK)
	}
}<commit_msg>fix(api): add git.tag in condition name (#4107)<commit_after>package api

import (
	"context"
	"net/http"
	"sort"

	"github.com/gorilla/mux"

	"github.com/ovh/cds/engine/api/project"
	"github.com/ovh/cds/engine/api/workflow"
	"github.com/ovh/cds/engine/service"
	"github.com/ovh/cds/sdk"
)

func (api *API) getWorkflowTriggerConditionHandler() service.Handler {
	return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
		vars := mux.Vars(r)
		key := vars["key"]
		name := vars["permWorkflowName"]

		id, errID := requestVarInt(r, "nodeID")
		if errID != nil {
			return errID
		}

		proj, errproj := project.Load(api.mustDB(), api.Cache, key, deprecatedGetUser(ctx), project.LoadOptions.WithVariables, project.LoadOptions.WithIntegrations)
		if errproj != nil {
			return sdk.WrapError(errproj, "getWorkflowTriggerConditionHandler> Unable to load project")
		}

		wf, errw := workflow.Load(ctx, api.mustDB(), api.Cache, proj, name, deprecatedGetUser(ctx), workflow.LoadOptions{})
		if errw != nil {
			return sdk.WrapError(errw, "getWorkflowTriggerConditionHandler> Unable to load workflow")
		}

		data := struct {
			Operators      map[string]string `json:"operators"`
			ConditionNames []string          `json:"names"`
		}{
			Operators: sdk.WorkflowConditionsOperators,
		}

		wr, errr := workflow.LoadLastRun(api.mustDB(), key, name, workflow.LoadRunOptions{})
		if errr != nil {
			if !sdk.ErrorIs(errr, sdk.ErrWorkflowNotFound) {
				return sdk.WrapError(errr, "getWorkflowTriggerConditionHandler> Unable to load last run workflow")
			}
		}

		refNode := wf.WorkflowData.NodeByID(id)
		if refNode == nil {
			return sdk.WrapError(sdk.ErrWorkflowNodeNotFound, "getWorkflowTriggerConditionHandler> Unable to load workflow node")
		}

		params := []sdk.Parameter{}
		// If there is a workflow run, try to get build parameter from it
		if wr != nil {
			var errp error
			params, errp = workflow.NodeBuildParametersFromRun(*wr, id)
			if errp != nil {
				return sdk.WrapError(errp, "getWorkflowTriggerConditionHandler> Unable to load build parameters from workflow run")
			}
		}

		// If node node found in last workflow run
		if len(params) == 0 {
			var errp error
			ancestorIds := refNode.Ancestors(wf.WorkflowData)
			params, errp = workflow.NodeBuildParametersFromWorkflow(ctx, api.mustDB(), api.Cache, proj, wf, refNode, ancestorIds)
			if errp != nil {
				return sdk.WrapError(errp, "getWorkflowTriggerConditionHandler> Unable to load build parameters from workflow")
			}
			sdk.AddParameter(&params, "cds.dest.pipeline", sdk.StringParameter, "")
			sdk.AddParameter(&params, "cds.status", sdk.StringParameter, "")
			sdk.AddParameter(&params, "cds.manual", sdk.StringParameter, "")

			if refNode.Context != nil && refNode.Context.ApplicationID != 0 {
				sdk.AddParameter(&params, "cds.dest.application", sdk.StringParameter, "")
			}
			if refNode.Context != nil && refNode.Context.EnvironmentID != 0 {
				sdk.AddParameter(&params, "cds.dest.environment", sdk.StringParameter, "")
			}
		}

		if refNode.IsLinkedToRepo(wf) {
			if sdk.ParameterFind(&params, "git.repository") == nil {
				data.ConditionNames = append(data.ConditionNames, "git.repository")
				data.ConditionNames = append(data.ConditionNames, "git.branch")
				data.ConditionNames = append(data.ConditionNames, "git.message")
				data.ConditionNames = append(data.ConditionNames, "git.author")
				data.ConditionNames = append(data.ConditionNames, "git.hash")
				data.ConditionNames = append(data.ConditionNames, "git.tag")
			}
		}

		for _, p := range params {
			data.ConditionNames = append(data.ConditionNames, p.Name)
		}

		sort.Strings(data.ConditionNames)
		return service.WriteJSON(w, data, http.StatusOK)
	}
}
{'fraction_non_alphanumeric': 0.131903921834713, 'fraction_numerical': 0.0013570362328674175, 'mean_word_length': 4.305975521958243, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '1338292', 'n_tokens_mistral': 2716, 'n_tokens_neox': 2379, 'n_words': 519}