<commit_before>package clusterscan

import (
	"net/http"
	"strconv"

	"github.com/rancher/norman/api/access"
	"github.com/rancher/norman/types"
	"github.com/rancher/norman/types/convert"
	"github.com/rancher/rancher/pkg/clustermanager"
	"github.com/rancher/rancher/pkg/ref"
	"github.com/rancher/security-scan/pkg/kb-summarizer/report"
	corev1 "github.com/rancher/types/apis/core/v1"
	mgmtv3 "github.com/rancher/types/apis/management.cattle.io/v3"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Formatter(apiContext *types.APIContext, resource *types.RawResource) {
	if err := apiContext.AccessControl.CanDo(mgmtv3.ClusterScanGroupVersionKind.Group, mgmtv3.ClusterScanResource.Name, "read", apiContext, resource.Values, apiContext.Schema); err == nil {
		s, ok := resource.Values["status"]
		if !ok {
			return
		}
		status := convert.ToMapInterface(s)
		failed := false
		completed := false
		for _, cond := range convert.ToMapSlice(status["conditions"]) {
			if cond["type"] == string(mgmtv3.ClusterScanConditionCompleted) && cond["status"] == "True" {
				completed = true
			}
			if cond["type"] == string(mgmtv3.ClusterScanConditionFailed) && cond["status"] == "True" {
				failed = true
			}
		}
		if failed {
			resource.Values["state"] = "error"
			return
		}
		if completed {
			resource.Links["report"] = apiContext.URLBuilder.Link("report", resource)
		}
	}<commit_msg>propagate the scan result to the state<commit_after>package clusterscan

import (
	"net/http"
	"strconv"

	"github.com/rancher/norman/api/access"
	"github.com/rancher/norman/types"
	"github.com/rancher/norman/types/convert"
	"github.com/rancher/rancher/pkg/clustermanager"
	"github.com/rancher/rancher/pkg/ref"
	"github.com/rancher/security-scan/pkg/kb-summarizer/report"
	corev1 "github.com/rancher/types/apis/core/v1"
	mgmtv3 "github.com/rancher/types/apis/management.cattle.io/v3"
	"github.com/sirupsen/logrus"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
	errorState  = "error"
	failedState = "fail"
	passedState = "pass"
)

func Formatter(apiContext *types.APIContext, resource *types.RawResource) {
	if err := apiContext.AccessControl.CanDo(mgmtv3.ClusterScanGroupVersionKind.Group, mgmtv3.ClusterScanResource.Name, "read", apiContext, resource.Values, apiContext.Schema); err == nil {
		s, ok := resource.Values["status"]
		if !ok {
			return
		}
		status := convert.ToMapInterface(s)
		failed := false
		completed := false
		for _, cond := range convert.ToMapSlice(status["conditions"]) {
			if cond["type"] == string(mgmtv3.ClusterScanConditionCompleted) && cond["status"] == "True" {
				completed = true
			}
			if cond["type"] == string(mgmtv3.ClusterScanConditionFailed) && cond["status"] == "True" {
				failed = true
			}
		}
		if failed {
			resource.Values["state"] = errorState
			return
		}
		if completed {
			resource.Links["report"] = apiContext.URLBuilder.Link("report", resource)
			cisScanStatusInterface, ok := status["cisScanStatus"]
			if !ok {
				resource.Values["state"] = errorState
				return
			}
			cisScanStatus := convert.ToMapInterface(cisScanStatusInterface)
			failInterface := cisScanStatus["fail"]
			fail, err := convert.ToNumber(failInterface)
			if err != nil {
				logrus.Errorf("error converting failInterface to int: %v", err)
				resource.Values["state"] = errorState
			}
			if fail > 0 {
				resource.Values["state"] = failedState
			} else {
				resource.Values["state"] = passedState
			}
		}
	}
{'fraction_non_alphanumeric': 0.14733178654292342, 'fraction_numerical': 0.006670533642691415, 'mean_word_length': 4.3390092879256965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '5257', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1214, 'n_words': 193}