<commit_before>package app

import (
	"net/http"

	"fmt"

	"reflect"

	"github.com/rancher/norman/api/access"
	"github.com/rancher/norman/httperror"
	"github.com/rancher/norman/parse"
	"github.com/rancher/norman/types"
	"github.com/rancher/norman/types/convert"
	"github.com/rancher/rancher/pkg/controllers/management/compose/common"
	hcommon "github.com/rancher/rancher/pkg/controllers/user/helm/common"
	"github.com/rancher/rancher/pkg/ref"
	"github.com/rancher/types/apis/management.cattle.io/v3"
	pv3 "github.com/rancher/types/apis/project.cattle.io/v3"
	projectschema "github.com/rancher/types/apis/project.cattle.io/v3/schema"
	projectv3 "github.com/rancher/types/client/project/v3"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type Wrapper struct {
	Clusters              v3.ClusterInterface
	TemplateVersionClient v3.TemplateVersionInterface
	KubeConfigGetter      common.KubeConfigGetter
	TemplateContentClient v3.TemplateContentInterface
	AppGetter             pv3.AppsGetter
}

const (
	appLabel       = "io.cattle.field/appId"
	activeState    = "active"
	deployingState = "deploying"
)

func Formatter(apiContext *types.APIContext, resource *types.RawResource) {
	resource.AddAction(apiContext, "upgrade")
	resource.AddAction(apiContext, "rollback")
	resource.Links["revision"] = apiContext.URLBuilder.Link("revision", resource)
	if _, ok := resource.Values["status"]; ok {
		if status, ok := resource.Values["status"].(map[string]interface{}); ok {
			delete(status, "lastAppliedTemplate")
		}
	}
	var workloads []projectv3.Workload
	if err := access.List(apiContext, &projectschema.Version, projectv3.WorkloadType, &types.QueryOptions{}, &workloads); err == nil {
		for _, w := range workloads {
			_, appID := ref.Parse(resource.ID)
			if w.WorkloadLabels[appLabel] == appID && w.State != activeState {
				resource.Values["state"] = deployingState
				resource.Values["transitioning"] = "yes"
				transitionMsg := convert.ToString(resource.Values["transitioningMessage"])
				if transitionMsg != "" {
					transitionMsg += "; "
				}
				resource.Values["transitioningMessage"] = transitionMsg + fmt.Sprintf("Workload %s: %s", w.Name, w.TransitioningMessage)
			}
		}
	}
	delete(resource.Values, "appliedFiles")
	delete(resource.Values, "files")
}

func (w Wrapper) Validator(request *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
	externalID := convert.ToString(data["externalId"])
	templateVersionID, err := hcommon.ParseExternalID(externalID)
	if err != nil {
		return err
	}
	templateVersion, err := w.TemplateVersionClient.Get(templateVersionID, metav1.GetOptions{})
	if err != nil {
		return err
	}<commit_msg>skip when externalId is empty<commit_after>package app

import (
	"net/http"

	"fmt"

	"reflect"

	"github.com/rancher/norman/api/access"
	"github.com/rancher/norman/httperror"
	"github.com/rancher/norman/parse"
	"github.com/rancher/norman/types"
	"github.com/rancher/norman/types/convert"
	"github.com/rancher/rancher/pkg/controllers/management/compose/common"
	hcommon "github.com/rancher/rancher/pkg/controllers/user/helm/common"
	"github.com/rancher/rancher/pkg/ref"
	"github.com/rancher/types/apis/management.cattle.io/v3"
	pv3 "github.com/rancher/types/apis/project.cattle.io/v3"
	projectschema "github.com/rancher/types/apis/project.cattle.io/v3/schema"
	projectv3 "github.com/rancher/types/client/project/v3"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type Wrapper struct {
	Clusters              v3.ClusterInterface
	TemplateVersionClient v3.TemplateVersionInterface
	KubeConfigGetter      common.KubeConfigGetter
	TemplateContentClient v3.TemplateContentInterface
	AppGetter             pv3.AppsGetter
}

const (
	appLabel       = "io.cattle.field/appId"
	activeState    = "active"
	deployingState = "deploying"
)

func Formatter(apiContext *types.APIContext, resource *types.RawResource) {
	resource.AddAction(apiContext, "upgrade")
	resource.AddAction(apiContext, "rollback")
	resource.Links["revision"] = apiContext.URLBuilder.Link("revision", resource)
	if _, ok := resource.Values["status"]; ok {
		if status, ok := resource.Values["status"].(map[string]interface{}); ok {
			delete(status, "lastAppliedTemplate")
		}
	}
	var workloads []projectv3.Workload
	if err := access.List(apiContext, &projectschema.Version, projectv3.WorkloadType, &types.QueryOptions{}, &workloads); err == nil {
		for _, w := range workloads {
			_, appID := ref.Parse(resource.ID)
			if w.WorkloadLabels[appLabel] == appID && w.State != activeState {
				resource.Values["state"] = deployingState
				resource.Values["transitioning"] = "yes"
				transitionMsg := convert.ToString(resource.Values["transitioningMessage"])
				if transitionMsg != "" {
					transitionMsg += "; "
				}
				resource.Values["transitioningMessage"] = transitionMsg + fmt.Sprintf("Workload %s: %s", w.Name, w.TransitioningMessage)
			}
		}
	}
	delete(resource.Values, "appliedFiles")
	delete(resource.Values, "files")
}

func (w Wrapper) Validator(request *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
	externalID := convert.ToString(data["externalId"])
	if externalID == "" {
		return nil
	}
	templateVersionID, err := hcommon.ParseExternalID(externalID)
	if err != nil {
		return err
	}
	templateVersion, err := w.TemplateVersionClient.Get(templateVersionID, metav1.GetOptions{})
	if err != nil {
		return err
	}
{'fraction_non_alphanumeric': 0.13874976813207196, 'fraction_numerical': 0.005935818957521795, 'mean_word_length': 4.754535752401281, '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': '2209211', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1835, 'n_words': 286}