<commit_before>package lib

import (
	"errors"
	"fmt"
	"io/ioutil"
	"strings"
	"time"

	"github.com/ForceCLI/force/desktop"
	. "github.com/ForceCLI/force/error"
)

// Creates a package that includes everything in the passed in string slice
// and then deploys the package to salesforce
func PushByPaths(fpaths []string, byName bool, namePaths map[string]string, opts *ForceDeployOptions) {
	pb := NewPushBuilder()

	var badPaths []string
	for _, fpath := range fpaths {
		// TODO: check for folder, if a folder, add all files in it
		name, err := pb.AddFile(fpath)
		if err != nil {
			fmt.Println(err.Error())
			badPaths = append(badPaths, fpath)
		} else {
			// Store paths by name for error messages
			namePaths[name] = fpath
		}
	}

	if len(badPaths) == 0 {
		fmt.Println("Deploying now...")
		t0 := time.Now()
		deployFiles(pb.ForceMetadataFiles(), byName, namePaths, opts)
		t1 := time.Now()
		fmt.Printf("The deployment took %v to run.\n", t1.Sub(t0))
	} else {
		ErrorAndExit("Could not add the following files:\n {}", strings.Join(badPaths, "\n"))
	}
}

func deployFiles(files ForceMetadataFiles, byName bool, namePaths map[string]string, opts *ForceDeployOptions) {
	force, _ := ActiveForce()
	result, err := force.Metadata.Deploy(files, *opts)
	err = processDeployResults(result, byName, namePaths, err)
	if err != nil {
		ErrorAndExit(err.Error())
	}
	return
}

// Process and display the result of the push operation
func processDeployResults(result ForceCheckDeploymentStatusResult, byName bool, namePaths map[string]string, deployErr error) (err error) {
	if deployErr != nil {
		ErrorAndExit(deployErr.Error())
	}

	problems := result.Details.ComponentFailures
	successes := result.Details.ComponentSuccesses
	testFailures := result.Details.RunTestResult.TestFailures
	testSuccesses := result.Details.RunTestResult.TestSuccesses
	codeCoverageWarnings := result.Details.RunTestResult.CodeCoverageWarnings

	if len(problems) > 0 {
		fmt.Printf("\nFailures - %d\n", len(problems))
		for _, problem := range problems {
			if problem.FullName == "" {
				fmt.Println(problem.Problem)
			} else {
				if byName {
					fmt.Printf("ERROR with %s, line %d\n %s\n", problem.FullName, problem.LineNumber, problem.Problem)
				} else {
					fname, found := namePaths[problem.FullName]
					if !found {
						fname = problem.FullName
					}
					fmt.Printf("\"%s\", line %d: %s %s\n", fname, problem.LineNumber, problem.ProblemType, problem.Problem)
				}
			}
		}
	}

	if len(successes) > 0 {
		fmt.Printf("\nSuccesses - %d\n", len(successes)-1)
		for _, success := range successes {
			if success.FullName != "package.xml" {
				verb := "unchanged"
				if success.Changed {
					verb = "changed"
				} else if success.Deleted {
					verb = "deleted"
				} else if success.Created {
					verb = "created"
				}
				fmt.Printf("\t%s: %s\n", success.FullName, verb)
			}
		}
	}

	fmt.Printf("\nTest Successes - %d\n", len(testSuccesses))
	for _, failure := range testSuccesses {
		fmt.Printf("  [PASS]  %s::%s\n", failure.Name, failure.MethodName)
	}

	fmt.Printf("\nTest Failures - %d\n", len(testFailures))
	for _, failure := range testFailures {
		fmt.Printf("\n  [FAIL]  %s::%s: %s\n", failure.Name, failure.MethodName, failure.Message)
		fmt.Println(failure.StackTrace)
	}

	if len(codeCoverageWarnings) > 0 {
		fmt.Printf("\nCode Coverage Warnings - %d\n", len(codeCoverageWarnings))
		for _, warning := range codeCoverageWarnings {
			fmt.Printf("\n %s: %s\n", warning.Name, warning.Message)
		}
	}

	// Handle notifications
	desktop.NotifySuccess("push", len(problems) == 0)
	if len(problems) > 0 {
		err = errors.New("Some components failed deployment")<commit_msg>Display Errors After Successes When Deploying<commit_after>package lib

import (
	"errors"
	"fmt"
	"io/ioutil"
	"strings"
	"time"

	"github.com/ForceCLI/force/desktop"
	. "github.com/ForceCLI/force/error"
)

// Creates a package that includes everything in the passed in string slice
// and then deploys the package to salesforce
func PushByPaths(fpaths []string, byName bool, namePaths map[string]string, opts *ForceDeployOptions) {
	pb := NewPushBuilder()

	var badPaths []string
	for _, fpath := range fpaths {
		// TODO: check for folder, if a folder, add all files in it
		name, err := pb.AddFile(fpath)
		if err != nil {
			fmt.Println(err.Error())
			badPaths = append(badPaths, fpath)
		} else {
			// Store paths by name for error messages
			namePaths[name] = fpath
		}
	}

	if len(badPaths) == 0 {
		fmt.Println("Deploying now...")
		t0 := time.Now()
		deployFiles(pb.ForceMetadataFiles(), byName, namePaths, opts)
		t1 := time.Now()
		fmt.Printf("The deployment took %v to run.\n", t1.Sub(t0))
	} else {
		ErrorAndExit("Could not add the following files:\n {}", strings.Join(badPaths, "\n"))
	}
}

func deployFiles(files ForceMetadataFiles, byName bool, namePaths map[string]string, opts *ForceDeployOptions) {
	force, _ := ActiveForce()
	result, err := force.Metadata.Deploy(files, *opts)
	err = processDeployResults(result, byName, namePaths, err)
	if err != nil {
		ErrorAndExit(err.Error())
	}
	return
}

// Process and display the result of the push operation
func processDeployResults(result ForceCheckDeploymentStatusResult, byName bool, namePaths map[string]string, deployErr error) (err error) {
	if deployErr != nil {
		ErrorAndExit(deployErr.Error())
	}

	problems := result.Details.ComponentFailures
	successes := result.Details.ComponentSuccesses
	testFailures := result.Details.RunTestResult.TestFailures
	testSuccesses := result.Details.RunTestResult.TestSuccesses
	codeCoverageWarnings := result.Details.RunTestResult.CodeCoverageWarnings

	if len(successes) > 0 {
		fmt.Printf("\nSuccesses - %d\n", len(successes)-1)
		for _, success := range successes {
			if success.FullName != "package.xml" {
				verb := "unchanged"
				if success.Changed {
					verb = "changed"
				} else if success.Deleted {
					verb = "deleted"
				} else if success.Created {
					verb = "created"
				}
				fmt.Printf("\t%s: %s\n", success.FullName, verb)
			}
		}
	}

	fmt.Printf("\nTest Successes - %d\n", len(testSuccesses))
	for _, failure := range testSuccesses {
		fmt.Printf("  [PASS]  %s::%s\n", failure.Name, failure.MethodName)
	}

	if len(problems) > 0 {
		fmt.Printf("\nFailures - %d\n", len(problems))
		for _, problem := range problems {
			if problem.FullName == "" {
				fmt.Println(problem.Problem)
			} else {
				if byName {
					fmt.Printf("ERROR with %s, line %d\n %s\n", problem.FullName, problem.LineNumber, problem.Problem)
				} else {
					fname, found := namePaths[problem.FullName]
					if !found {
						fname = problem.FullName
					}
					fmt.Printf("\"%s\", line %d: %s %s\n", fname, problem.LineNumber, problem.ProblemType, problem.Problem)
				}
			}
		}
	}

	fmt.Printf("\nTest Failures - %d\n", len(testFailures))
	for _, failure := range testFailures {
		fmt.Printf("\n  [FAIL]  %s::%s: %s\n", failure.Name, failure.MethodName, failure.Message)
		fmt.Println(failure.StackTrace)
	}

	if len(codeCoverageWarnings) > 0 {
		fmt.Printf("\nCode Coverage Warnings - %d\n", len(codeCoverageWarnings))
		for _, warning := range codeCoverageWarnings {
			fmt.Printf("\n %s: %s\n", warning.Name, warning.Message)
		}
	}

	// Handle notifications
	desktop.NotifySuccess("push", len(problems) == 0)
	if len(problems) > 0 {
		err = errors.New("Some components failed deployment")
{'fraction_non_alphanumeric': 0.14127198917456021, 'fraction_numerical': 0.002976995940460081, 'mean_word_length': 3.805591677503251, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5164873', 'n_tokens_mistral': 2784, 'n_tokens_neox': 2519, 'n_words': 606}