<commit_before>package no.skatteetaten.aurora.boober.service

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ObjectNode
import no.skatteetaten.aurora.boober.controller.security.UserDetailsProvider
import no.skatteetaten.aurora.boober.model.AuroraApplicationConfig
import no.skatteetaten.aurora.boober.model.Mount
import no.skatteetaten.aurora.boober.service.openshift.OpenShiftResourceClient
import no.skatteetaten.aurora.boober.utils.ensureStartWith
import org.apache.commons.lang.StringEscapeUtils
import org.apache.velocity.VelocityContext
import org.apache.velocity.app.VelocityEngine
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.io.StringWriter
import java.util.*

@Service
class OpenShiftObjectGenerator(
        val userDetailsProvider: UserDetailsProvider,
        val ve: VelocityEngine,
        val mapper: ObjectMapper,
        val openShiftTemplateProcessor: OpenShiftTemplateProcessor,
        val openShiftClient: OpenShiftResourceClient) {

    val logger: Logger = LoggerFactory.getLogger(OpenShiftObjectGenerator::class.java)

    fun generateBuildRequest(name: String): JsonNode {
        logger.debug("Generating build request for name $name")
        return mergeVelocityTemplate("buildrequest.json", mapOf("name" to name))

    }

    fun generateDeploymentRequest(name: String): JsonNode {
        logger.debug("Generating deploy request for name $name")
        return mergeVelocityTemplate("deploymentrequest.json", mapOf("name" to name))

    }

    fun generateObjects(auroraApplicationConfig: AuroraApplicationConfig, deployId: String): LinkedList<JsonNode> {


        val overrides = auroraApplicationConfig.deploy?.let {
            StringEscapeUtils.escapeJavaScript(mapper.writeValueAsString(it.overrideFiles))
        }


        val database = auroraApplicationConfig.deploy?.database?.map { it.spec }?.joinToString(",") ?: ""

        logger.debug("Database is $database")
        //In use in velocity template
        val routeName = auroraApplicationConfig.route?.let {
            if (it.route.isEmpty()) {
                null
            } else {
                it.route.first().let {
                    val host = it.host ?: "${auroraApplicationConfig.name}-${auroraApplicationConfig.namespace}"
                    "http://$host.${auroraApplicationConfig.cluster}.paas.skead.no${it.path ?: ""}"
                }
            }
        }


        val buildName = auroraApplicationConfig.build?.let {
            if (it.buildSuffix != null) {
                "${auroraApplicationConfig.name}-${it.buildSuffix}"
            } else {
                auroraApplicationConfig.name
            }
        }

        val mounts: List<Mount>? = auroraApplicationConfig.volume?.mounts?.map {
            if (it.exist) {
                it
            } else {
                it.copy(volumeName = it.volumeName.ensureStartWith(auroraApplicationConfig.name, "-"))
            }
        }
        val params = mapOf(
                "overrides" to overrides,
                "deployId" to deployId,
                "aac" to auroraApplicationConfig,
                "mounts" to mounts,
                "buildName" to buildName,
                "routeName" to routeName,
                "username" to userDetailsProvider.getAuthenticatedUser().username,
                "database" to database,
                "dbPath" to "/u01/secrets/app",
                "certPath" to "/u01/secrets/app/${auroraApplicationConfig.name}-cert"
        )

        val templatesToProcess = LinkedList(mutableListOf(
                "project.json",
                "deployer-rolebinding.json",
                "imagebuilder-rolebinding.json",
                "imagepuller-rolebinding.json",
                "admin-rolebinding.json"))

        if (auroraApplicationConfig.permissions.view != null) {
            templatesToProcess.add("view-rolebinding.json")<commit_msg>fix username to replace illegal value<commit_after>package no.skatteetaten.aurora.boober.service

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ObjectNode
import no.skatteetaten.aurora.boober.controller.security.UserDetailsProvider
import no.skatteetaten.aurora.boober.model.AuroraApplicationConfig
import no.skatteetaten.aurora.boober.model.Mount
import no.skatteetaten.aurora.boober.service.openshift.OpenShiftResourceClient
import no.skatteetaten.aurora.boober.utils.ensureStartWith
import org.apache.commons.lang.StringEscapeUtils
import org.apache.velocity.VelocityContext
import org.apache.velocity.app.VelocityEngine
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.io.StringWriter
import java.util.*

@Service
class OpenShiftObjectGenerator(
        val userDetailsProvider: UserDetailsProvider,
        val ve: VelocityEngine,
        val mapper: ObjectMapper,
        val openShiftTemplateProcessor: OpenShiftTemplateProcessor,
        val openShiftClient: OpenShiftResourceClient) {

    val logger: Logger = LoggerFactory.getLogger(OpenShiftObjectGenerator::class.java)

    fun generateBuildRequest(name: String): JsonNode {
        logger.debug("Generating build request for name $name")
        return mergeVelocityTemplate("buildrequest.json", mapOf("name" to name))

    }

    fun generateDeploymentRequest(name: String): JsonNode {
        logger.debug("Generating deploy request for name $name")
        return mergeVelocityTemplate("deploymentrequest.json", mapOf("name" to name))

    }

    fun generateObjects(auroraApplicationConfig: AuroraApplicationConfig, deployId: String): LinkedList<JsonNode> {


        val overrides = auroraApplicationConfig.deploy?.let {
            StringEscapeUtils.escapeJavaScript(mapper.writeValueAsString(it.overrideFiles))
        }


        val database = auroraApplicationConfig.deploy?.database?.map { it.spec }?.joinToString(",") ?: ""

        logger.debug("Database is $database")
        //In use in velocity template
        val routeName = auroraApplicationConfig.route?.let {
            if (it.route.isEmpty()) {
                null
            } else {
                it.route.first().let {
                    val host = it.host ?: "${auroraApplicationConfig.name}-${auroraApplicationConfig.namespace}"
                    "http://$host.${auroraApplicationConfig.cluster}.paas.skead.no${it.path ?: ""}"
                }
            }
        }


        val buildName = auroraApplicationConfig.build?.let {
            if (it.buildSuffix != null) {
                "${auroraApplicationConfig.name}-${it.buildSuffix}"
            } else {
                auroraApplicationConfig.name
            }
        }

        val mounts: List<Mount>? = auroraApplicationConfig.volume?.mounts?.map {
            if (it.exist) {
                it
            } else {
                it.copy(volumeName = it.volumeName.ensureStartWith(auroraApplicationConfig.name, "-"))
            }
        }
        val params = mapOf(
                "overrides" to overrides,
                "deployId" to deployId,
                "aac" to auroraApplicationConfig,
                "mounts" to mounts,
                "buildName" to buildName,
                "routeName" to routeName,
                "username" to userDetailsProvider.getAuthenticatedUser().username.replace(":", "-"),
                "database" to database,
                "dbPath" to "/u01/secrets/app",
                "certPath" to "/u01/secrets/app/${auroraApplicationConfig.name}-cert"
        )

        val templatesToProcess = LinkedList(mutableListOf(
                "project.json",
                "deployer-rolebinding.json",
                "imagebuilder-rolebinding.json",
                "imagepuller-rolebinding.json",
                "admin-rolebinding.json"))

        if (auroraApplicationConfig.permissions.view != null) {
            templatesToProcess.add("view-rolebinding.json")
{'fraction_non_alphanumeric': 0.09916338582677166, 'fraction_numerical': 0.0014763779527559055, 'mean_word_length': 2.560665790626369, 'pattern_counts': {'":': 1, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '1698113', 'n_tokens_mistral': 2241, 'n_tokens_neox': 2115, 'n_words': 376}