<commit_before>package org.fakekoji.jobmanager.model;

import org.fakekoji.jobmanager.JenkinsJobTemplateBuilder;
import org.fakekoji.model.BuildProvider;
import org.fakekoji.model.JDKVersion;
import org.fakekoji.model.OToolVariable;
import org.fakekoji.model.Platform;
import org.fakekoji.model.Task;
import org.fakekoji.model.TaskVariant;
import org.fakekoji.model.TaskVariantValue;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.fakekoji.jobmanager.JenkinsJobTemplateBuilder.JenkinsTemplate.TASK_JOB_TEMPLATE;
import static org.fakekoji.jobmanager.JenkinsJobTemplateBuilder.RELEASE_SUFFIX_VAR;
import static org.fakekoji.jobmanager.JenkinsJobTemplateBuilder.XML_DECLARATION;
import static org.fakekoji.jobmanager.JenkinsJobTemplateBuilder.fillBuildPlatform;
import static org.fakekoji.jobmanager.JenkinsJobTemplateBuilder.loadTemplate;

public class TestJob extends TaskJob {

    private final Project.ProjectType projectType;
    private final Platform buildPlatform;
    private final Map<TaskVariant, TaskVariantValue> buildVariants;
    private final List<String> projectSubpackageBlacklist;
    private final List<String> projectSubpackageWhitelist;

    public TestJob(
            String platformProvider,
            String projectName,
            Project.ProjectType projectType,
            Product product,
            JDKVersion jdkVersion,
            Set<BuildProvider> buildProviders,
            Task task,
            Platform platform,
            Map<TaskVariant, TaskVariantValue> variants,
            Platform buildPlatform,
            Map<TaskVariant, TaskVariantValue> buildVariants,
            List<String> projectSubpackageBlacklist,
            List<String> projectSubpackageWhitelist,
            File scriptsRoot
    ) {
        super(platformProvider, projectName, product, jdkVersion, buildProviders, task, platform, variants, scriptsRoot);
        this.projectType = projectType;
        this.buildPlatform = buildPlatform;
        this.buildVariants = buildVariants;
        this.projectSubpackageBlacklist = projectSubpackageBlacklist;
        this.projectSubpackageWhitelist = projectSubpackageWhitelist;
    }

    public TestJob(
            String platformProvider,
            String projectName,
            Project.ProjectType projectType,
            Product product,
            JDKVersion jdkVersion,
            Set<BuildProvider> buildProviders,
            Task task,
            Platform platform,
            Map<TaskVariant, TaskVariantValue> variants,
            Platform buildPlatform,
            Map<TaskVariant, TaskVariantValue> buildVariants,
            File scriptsRoot
    ) {
        this(
                platformProvider,
                projectName,
                projectType,
                product,
                jdkVersion,
                buildProviders,
                task,
                platform,
                variants,
                buildPlatform,
                buildVariants,
                Collections.emptyList(),
                Collections.emptyList(),
                scriptsRoot
        );

    }

    public TestJob(
            BuildJob buildJob,
            Task task,
            Platform platform,
            Map<TaskVariant, TaskVariantValue> variants
    ) {
        this(
                buildJob.getPlatformProvider(),
                buildJob.getProjectName(),
                Project.ProjectType.JDK_PROJECT,
                buildJob.getProduct(),
                buildJob.getJdkVersion(),
                buildJob.getBuildProviders(),
                task,
                platform,
                variants,
                buildJob.getPlatform(),
                buildJob.getVariants(),
                buildJob.getScriptsRoot()
        );
    }

    @Override
    public String generateTemplate() throws IOException {
        switch (projectType) {
            case JDK_PROJECT:
                return generateOToolTemplate();
            case JDK_TEST_PROJECT:
                return generateKojiTemplate();
        }
        return "";
    }

    private String generateOToolTemplate() throws IOException {
        return XML_DECLARATION + new JenkinsJobTemplateBuilder(loadTemplate(TASK_JOB_TEMPLATE), this)
                .buildBuildProvidersTemplate(getBuildProviders())
                .buildFakeKojiXmlRpcApiTemplate(
                        getProjectName(),
                        getBuildVariants(),
                        fillBuildPlatform(getBuildPlatform(), getTask().getFileRequirements()),
                        true
                )
                .buildScriptTemplate(
                        getTask(),
                        getPlatformProvider(),
                        getPlatform(),
                        getScriptsRoot(),
                        getExportedVariables())
                .buildTriggerTemplate(getTask().getScmPollSchedule())
                .buildPostBuildTasks(getTask().getXmlTemplate())
                .prettyPrint();
    }

    private String generateKojiTemplate() throws IOException {

        final List<String> subpackageBlacklist = Stream.of(
                projectSubpackageBlacklist,
                getTask().getRpmLimitation().getBlacklist(),
                getBuildVariants()
                        .values()
                        .stream()
                        .map(TaskVariantValue::getSubpackageBlacklist)
                        .flatMap(List::stream)
                        .collect(Collectors.toList())
        )
                .flatMap(List::stream)
                .distinct()
                .collect(Collectors.toList());

        final List<String> subpackageWhitelist = Stream.of(
                projectSubpackageWhitelist,
                getTask().getRpmLimitation().getWhitelist(),
                getBuildVariants()
                        .values()
                        .stream()
                        .map(TaskVariantValue::getSubpackageWhitelist)
                        .flatMap(List::stream)
                        .collect(Collectors.toList())
        )
                .flatMap(List::stream)
                .distinct()
                .collect(Collectors.toList());

        return XML_DECLARATION + new JenkinsJobTemplateBuilder(loadTemplate(TASK_JOB_TEMPLATE), this)
                .buildBuildProvidersTemplate(getBuildProviders())
                .buildKojiXmlRpcApiTemplate(
                        getProduct().getPackageName(),
                        getBuildPlatform(),
                        subpackageBlacklist,
                        subpackageWhitelist
                )
                .buildScriptTemplate(
                        getTask(),
                        getPlatformProvider(),
                        getPlatform(),
                        getScriptsRoot(),
                        getExportedVariables())
                .buildTriggerTemplate(getTask().getScmPollSchedule())
                .buildPostBuildTasks(getTask().getXmlTemplate())
                .prettyPrint();
    }

    @Override
    List<OToolVariable> getExportedVariables() {<commit_msg>BINARIES now  effectively overwrite blacklist and whitelist to eat all<commit_after>package org.fakekoji.jobmanager.model;

import org.fakekoji.jobmanager.JenkinsJobTemplateBuilder;
import org.fakekoji.model.BuildProvider;
import org.fakekoji.model.JDKVersion;
import org.fakekoji.model.OToolVariable;
import org.fakekoji.model.Platform;
import org.fakekoji.model.Task;
import org.fakekoji.model.TaskVariant;
import org.fakekoji.model.TaskVariantValue;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.fakekoji.jobmanager.JenkinsJobTemplateBuilder.JenkinsTemplate.TASK_JOB_TEMPLATE;
import static org.fakekoji.jobmanager.JenkinsJobTemplateBuilder.RELEASE_SUFFIX_VAR;
import static org.fakekoji.jobmanager.JenkinsJobTemplateBuilder.XML_DECLARATION;
import static org.fakekoji.jobmanager.JenkinsJobTemplateBuilder.fillBuildPlatform;
import static org.fakekoji.jobmanager.JenkinsJobTemplateBuilder.loadTemplate;

public class TestJob extends TaskJob {

    private final Project.ProjectType projectType;
    private final Platform buildPlatform;
    private final Map<TaskVariant, TaskVariantValue> buildVariants;
    private final List<String> projectSubpackageBlacklist;
    private final List<String> projectSubpackageWhitelist;

    public TestJob(
            String platformProvider,
            String projectName,
            Project.ProjectType projectType,
            Product product,
            JDKVersion jdkVersion,
            Set<BuildProvider> buildProviders,
            Task task,
            Platform platform,
            Map<TaskVariant, TaskVariantValue> variants,
            Platform buildPlatform,
            Map<TaskVariant, TaskVariantValue> buildVariants,
            List<String> projectSubpackageBlacklist,
            List<String> projectSubpackageWhitelist,
            File scriptsRoot
    ) {
        super(platformProvider, projectName, product, jdkVersion, buildProviders, task, platform, variants, scriptsRoot);
        this.projectType = projectType;
        this.buildPlatform = buildPlatform;
        this.buildVariants = buildVariants;
        this.projectSubpackageBlacklist = projectSubpackageBlacklist;
        this.projectSubpackageWhitelist = projectSubpackageWhitelist;
    }

    public TestJob(
            String platformProvider,
            String projectName,
            Project.ProjectType projectType,
            Product product,
            JDKVersion jdkVersion,
            Set<BuildProvider> buildProviders,
            Task task,
            Platform platform,
            Map<TaskVariant, TaskVariantValue> variants,
            Platform buildPlatform,
            Map<TaskVariant, TaskVariantValue> buildVariants,
            File scriptsRoot
    ) {
        this(
                platformProvider,
                projectName,
                projectType,
                product,
                jdkVersion,
                buildProviders,
                task,
                platform,
                variants,
                buildPlatform,
                buildVariants,
                Collections.emptyList(),
                Collections.emptyList(),
                scriptsRoot
        );

    }

    public TestJob(
            BuildJob buildJob,
            Task task,
            Platform platform,
            Map<TaskVariant, TaskVariantValue> variants
    ) {
        this(
                buildJob.getPlatformProvider(),
                buildJob.getProjectName(),
                Project.ProjectType.JDK_PROJECT,
                buildJob.getProduct(),
                buildJob.getJdkVersion(),
                buildJob.getBuildProviders(),
                task,
                platform,
                variants,
                buildJob.getPlatform(),
                buildJob.getVariants(),
                buildJob.getScriptsRoot()
        );
    }

    @Override
    public String generateTemplate() throws IOException {
        switch (projectType) {
            case JDK_PROJECT:
                return generateOToolTemplate();
            case JDK_TEST_PROJECT:
                return generateKojiTemplate();
        }
        return "";
    }

    private String generateOToolTemplate() throws IOException {
        return XML_DECLARATION + new JenkinsJobTemplateBuilder(loadTemplate(TASK_JOB_TEMPLATE), this)
                .buildBuildProvidersTemplate(getBuildProviders())
                .buildFakeKojiXmlRpcApiTemplate(
                        getProjectName(),
                        getBuildVariants(),
                        fillBuildPlatform(getBuildPlatform(), getTask().getFileRequirements()),
                        true
                )
                .buildScriptTemplate(
                        getTask(),
                        getPlatformProvider(),
                        getPlatform(),
                        getScriptsRoot(),
                        getExportedVariables())
                .buildTriggerTemplate(getTask().getScmPollSchedule())
                .buildPostBuildTasks(getTask().getXmlTemplate())
                .prettyPrint();
    }

    private String generateKojiTemplate() throws IOException {
        final List<String> subpackageBlacklist;
        final List<String> subpackageWhitelist;
        if (getTask().getFileRequirements().getBinary().equals(Task.BinaryRequirements.BINARIES)) {
            //this is workaround
            //only one task is known to require binaries, and that requires them all
            //if ever listing will need filtering also on BINARIES
            //BINARIES and BINARIES_ALL mayb need to be introduced
            subpackageBlacklist = Arrays.asList();
            subpackageWhitelist = Arrays.asList(".*");
        } else {
            subpackageBlacklist = Stream.of(
                    projectSubpackageBlacklist,
                    getTask().getRpmLimitation().getBlacklist(),
                    getBuildVariants()
                            .values()
                            .stream()
                            .map(TaskVariantValue::getSubpackageBlacklist)
                            .flatMap(List::stream)
                            .collect(Collectors.toList())
            )
                    .flatMap(List::stream)
                    .distinct()
                    .collect(Collectors.toList());

            subpackageWhitelist = Stream.of(
                    projectSubpackageWhitelist,
                    getTask().getRpmLimitation().getWhitelist(),
                    getBuildVariants()
                            .values()
                            .stream()
                            .map(TaskVariantValue::getSubpackageWhitelist)
                            .flatMap(List::stream)
                            .collect(Collectors.toList())
            )
                    .flatMap(List::stream)
                    .distinct()
                    .collect(Collectors.toList());
        }

        return XML_DECLARATION + new JenkinsJobTemplateBuilder(loadTemplate(TASK_JOB_TEMPLATE), this)
                .buildBuildProvidersTemplate(getBuildProviders())
                .buildKojiXmlRpcApiTemplate(
                        getProduct().getPackageName(),
                        getBuildPlatform(),
                        subpackageBlacklist,
                        subpackageWhitelist
                )
                .buildScriptTemplate(
                        getTask(),
                        getPlatformProvider(),
                        getPlatform(),
                        getScriptsRoot(),
                        getExportedVariables())
                .buildTriggerTemplate(getTask().getScmPollSchedule())
                .buildPostBuildTasks(getTask().getXmlTemplate())
                .prettyPrint();
    }

    @Override
    List<OToolVariable> getExportedVariables() {
{'fraction_non_alphanumeric': 0.07268850552432642, 'fraction_numerical': 0.0, 'mean_word_length': 1.7668930997497319, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '5711884', 'n_tokens_mistral': 3766, 'n_tokens_neox': 3524, 'n_words': 653}