<commit_before>package org.rundeck.plugin.scm.git.imp.actions

import com.dtolabs.rundeck.core.plugins.views.BasicInputView
import com.dtolabs.rundeck.core.plugins.views.BasicInputViewBuilder
import com.dtolabs.rundeck.plugins.scm.JobImporter
import com.dtolabs.rundeck.plugins.scm.ScmExportResult
import com.dtolabs.rundeck.plugins.scm.ScmExportResultImpl
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.treewalk.TreeWalk
import org.rundeck.plugin.scm.git.BaseAction
import org.rundeck.plugin.scm.git.GitImportAction
import org.rundeck.plugin.scm.git.GitImportPlugin
import org.rundeck.plugin.scm.git.GitUtil

/**
 * Action to import selected jobs from git HEAD commit
 */
class ImportJobs extends BaseAction implements GitImportAction {
    ImportJobs(final String id, final String title, final String description, final String iconName) {
        super(id, title, description, iconName)
    }


    BasicInputView getInputView(GitImportPlugin plugin) {
        BasicInputViewBuilder.forActionId(id).with {
            title "Import remote Changes"
            description '''Import the modifications to Rundeck'''
            buttonTitle "Import"
            properties([])
            build()
        }
    }

    @Override
    ScmExportResult performAction(
            final GitImportPlugin plugin,
            final JobImporter importer,
            final List<String> selectedPaths,
            final Map<String, Object> input
    )
    {
        //perform git
        StringBuilder sb = new StringBuilder()
        boolean success=true

        //get head commit info
        def revCommit = GitUtil.getHead(plugin.repo)
        def meta = GitUtil.metaForCommit(revCommit)

        //walk the repo files and look for possible candidates
        plugin.walkTreePaths('HEAD^{tree}') { TreeWalk walk ->
            def path = walk.getPathString()
            if(!(plugin.isTrackedPath(path) && path in selectedPaths)){
                plugin.log.debug("skipping path ${path}")
                return
            }
            def objectId = walk.getObjectId(0)
            def size = plugin.repo.open(objectId, Constants.OBJ_BLOB).getSize()
            plugin.log.debug("import data: ${size} = ${objectId.name}")
            def bytes = plugin.repo.open(objectId, Constants.OBJ_BLOB).getBytes(Integer.MAX_VALUE)

            //plugin.log.debug("import data: "+new String(bytes))

            def importResult = importer.importFromStream(
                    walk.getNameString().endsWith(".xml") ? 'xml' : 'yaml',
                    new ByteArrayInputStream(bytes),
                    meta
            )
            if(!importResult.successful){
                success=false
                sb << ("Failed importing: ${walk.getPathString()}: " + importResult.errorMessage)
            }else{

                sb << ("Succeeded importing ${walk.getPathString()}: ${importResult}")
            }
        }
        def result = new ScmExportResultImpl()
        result.success = success
        result.message = sb.toString()
        return result

    }

}<commit_msg>use filtered treewalk, add tracking for imported file commits<commit_after>package org.rundeck.plugin.scm.git.imp.actions

import com.dtolabs.rundeck.core.plugins.views.BasicInputView
import com.dtolabs.rundeck.core.plugins.views.BasicInputViewBuilder
import com.dtolabs.rundeck.plugins.scm.JobImporter
import com.dtolabs.rundeck.plugins.scm.ScmExportResult
import com.dtolabs.rundeck.plugins.scm.ScmExportResultImpl
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.treewalk.TreeWalk
import org.rundeck.plugin.scm.git.BaseAction
import org.rundeck.plugin.scm.git.GitImportAction
import org.rundeck.plugin.scm.git.GitImportPlugin
import org.rundeck.plugin.scm.git.GitUtil

/**
 * Action to import selected jobs from git HEAD commit
 */
class ImportJobs extends BaseAction implements GitImportAction {
    ImportJobs(final String id, final String title, final String description, final String iconName) {
        super(id, title, description, iconName)
    }


    BasicInputView getInputView(GitImportPlugin plugin) {
        BasicInputViewBuilder.forActionId(id).with {
            title "Import remote Changes"
            description '''Import the modifications to Rundeck'''
            buttonTitle "Import"
            properties([])
            build()
        }
    }

    @Override
    ScmExportResult performAction(
            final GitImportPlugin plugin,
            final JobImporter importer,
            final List<String> selectedPaths,
            final Map<String, Object> input
    )
    {
        //perform git
        StringBuilder sb = new StringBuilder()
        boolean success=true

        //walk the repo files and look for possible candidates
        plugin.walkTreePaths('HEAD^{tree}',true) { TreeWalk walk ->
            def path = walk.getPathString()
            if(!(path in selectedPaths)){
                plugin.log.debug("not selected, skipping path ${path}")
                return
            }
            def objectId = walk.getObjectId(0)
            def size = plugin.repo.open(objectId, Constants.OBJ_BLOB).getSize()
            plugin.log.debug("import data: ${size} = ${path}")
            def bytes = plugin.repo.open(objectId, Constants.OBJ_BLOB).getBytes(Integer.MAX_VALUE)

            def commit = GitUtil.lastCommitForPath plugin.repo, plugin.git, path
            def meta = GitUtil.metaForCommit(commit)

            def importResult = importer.importFromStream(
                    walk.getNameString().endsWith(".xml") ? 'xml' : 'yaml',
                    new ByteArrayInputStream(bytes),
                    meta
            )
            if(!importResult.successful){
                success=false
                sb << ("Failed importing: ${walk.getPathString()}: " + importResult.errorMessage)
            }else{
                plugin.trackedImportedItems[(walk.getPathString())] = commit.name
                sb << ("Succeeded importing ${walk.getPathString()}: ${importResult}")
            }
        }
        def result = new ScmExportResultImpl()
        result.success = success
        result.message = sb.toString()
        return result

    }

}
{'fraction_non_alphanumeric': 0.0971373431971695, 'fraction_numerical': 0.000321646831778707, 'mean_word_length': 2.4114097641250685, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '7396382', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1651, 'n_words': 368}