<commit_before>package fixpkg

import (
	"encoding/json"
	"fmt"
	"path/filepath"
	"strings"

	"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/fix/v1alpha1"
	"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/fix/v1alpha2"
	"k8s.io/kube-openapi/pkg/validation/spec"
	"sigs.k8s.io/kustomize/kyaml/errors"
	"sigs.k8s.io/kustomize/kyaml/fieldmeta"
	"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
	"sigs.k8s.io/kustomize/kyaml/kio"
	"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
	"sigs.k8s.io/kustomize/kyaml/openapi"
	"sigs.k8s.io/kustomize/kyaml/sets"
	"sigs.k8s.io/kustomize/kyaml/setters2"
	"sigs.k8s.io/kustomize/kyaml/yaml"
)

var _ kio.Filter = &Fix{}

// Fix migrates the resources in v1alpha1 format to v1alpha2 format
type Fix struct {
	// pkgPathToPkgFilePaths key: package path relative to root package path
	// value: list of file paths in the package, relative to root package
	// the list doesn't include the file paths of subpackages
	pkgPathToPkgFilePaths map[string]sets.String

	// pkgFileToPkgPath key: file path(relative to root package)
	// value: path to package(relative to root package) to which the file belongs to
	pkgFileToPkgPath map[string]string

	// settersSchema is the schema equivalent of openAPI section in Kptfile
	// this must be updated while processing each resources so that the visitor
	// interface methods have access to it
	settersSchema *spec.Schema

	// Results are the results of fixing packages
	Results []*Result
}

// Result holds result of fixing packages
type Result struct {
	// FilePath is the file path of the resource
	FilePath string

	// Message is the result message
	Message string
}

// Filter implements Fix as a yaml.Filter
func (s *Fix) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
	// group the resources based on the packages they belong to
	// and populate Fix struct maps
	if err := s.groupPathsInPkgs(nodes); err != nil {
		return nodes, fmt.Errorf("unable to group resources by packages, %q", err.Error())
	}

	// each kpt package has a Kptfile with OpenAPI section(could be empty)
	// get the map of pkgPath to OpenAPI schema as a pre-processing step
	pkgPathToSettersSchema, err := getPkgPathToSettersSchema(nodes)
	if err != nil {
		return nodes, err
	}

	kfFound := false
	for i := range nodes {
		meta, err := nodes[i].GetMeta()
		if err != nil {
			return nodes, err
		}

		// check if there is Kptfile in root package
		if meta.Kind == v1alpha2.KptFileName {
			kfFound = true
		}

		if meta.Kind == v1alpha2.KptFileName {
			// this node is Kptfile node
			// migrate Kptfile from v1alpha1 to v1alpha2
			pkgPath := filepath.Dir(meta.Annotations[kioutil.PathAnnotation])
			functions := s.FunctionsInPkg(nodes, pkgPath)
			kNode, err := s.FixKptfile(nodes[i], functions)<commit_msg>Dummy PR to update unstable image (#402)<commit_after>package fixpkg

import (
	"encoding/json"
	"fmt"
	"path/filepath"
	"strings"

	"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/fix/v1alpha1"
	"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/fix/v1alpha2"
	"k8s.io/kube-openapi/pkg/validation/spec"
	"sigs.k8s.io/kustomize/kyaml/errors"
	"sigs.k8s.io/kustomize/kyaml/fieldmeta"
	"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
	"sigs.k8s.io/kustomize/kyaml/kio"
	"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
	"sigs.k8s.io/kustomize/kyaml/openapi"
	"sigs.k8s.io/kustomize/kyaml/sets"
	"sigs.k8s.io/kustomize/kyaml/setters2"
	"sigs.k8s.io/kustomize/kyaml/yaml"
)

var _ kio.Filter = &Fix{}

// Fix migrates the resources in v1alpha1 format to v1alpha2 format
type Fix struct {
	// pkgPathToPkgFilePaths key: package path relative to root package path
	// value: list of file paths in the package, relative to root package
	// the list doesn't include the file paths of subpackages
	pkgPathToPkgFilePaths map[string]sets.String

	// pkgFileToPkgPath key: file path(relative to root package)
	// value: path to package(relative to root package) to which the file belongs to
	pkgFileToPkgPath map[string]string

	// settersSchema is the schema equivalent of openAPI section in Kptfile
	// this must be updated while processing each resources so that the visitor
	// interface methods have access to it
	settersSchema *spec.Schema

	// Results are the results of fixing packages
	Results []*Result
}

// Result holds result of fixing packages
type Result struct {
	// FilePath is the file path of the resource
	FilePath string

	// Message is the result message
	Message string
}

// Filter implements Fix as a yaml.Filter
func (s *Fix) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
	// group the resources based on the packages they belong to and
	// populate Fix struct maps
	if err := s.groupPathsInPkgs(nodes); err != nil {
		return nodes, fmt.Errorf("unable to group resources by packages, %q", err.Error())
	}

	// each kpt package has a Kptfile with OpenAPI section(could be empty)
	// get the map of pkgPath to OpenAPI schema as a pre-processing step
	pkgPathToSettersSchema, err := getPkgPathToSettersSchema(nodes)
	if err != nil {
		return nodes, err
	}

	kfFound := false
	for i := range nodes {
		meta, err := nodes[i].GetMeta()
		if err != nil {
			return nodes, err
		}

		// check if there is Kptfile in root package
		if meta.Kind == v1alpha2.KptFileName {
			kfFound = true
		}

		if meta.Kind == v1alpha2.KptFileName {
			// this node is Kptfile node
			// migrate Kptfile from v1alpha1 to v1alpha2
			pkgPath := filepath.Dir(meta.Annotations[kioutil.PathAnnotation])
			functions := s.FunctionsInPkg(nodes, pkgPath)
			kNode, err := s.FixKptfile(nodes[i], functions)
{'fraction_non_alphanumeric': 0.10314219776318126, 'fraction_numerical': 0.010118941949227765, 'mean_word_length': 3.9464442493415275, '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': '6249928', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1929, 'n_words': 565}