All Downloads are FREE. Search and download functionalities are using the official Maven repository.

.bamboo.specs.extension.bamboo-specs-extension.1.3.18.source-code.Specs.kt Maven / Gradle / Ivy

The newest version!
package com.atlassian.bamboo.specs.extension

import com.atlassian.bamboo.specs.api.builders.RootEntityPropertiesBuilder
import com.atlassian.bamboo.specs.api.builders.deployment.Deployment
import com.atlassian.bamboo.specs.api.builders.deployment.Environment
import com.atlassian.bamboo.specs.api.builders.permission.DeploymentPermissions
import com.atlassian.bamboo.specs.api.builders.permission.EnvironmentPermissions
import com.atlassian.bamboo.specs.api.builders.permission.Permissions
import com.atlassian.bamboo.specs.api.builders.permission.PlanPermissions
import com.atlassian.bamboo.specs.api.builders.permission.ProjectPermissions
import com.atlassian.bamboo.specs.api.builders.plan.Plan
import com.atlassian.bamboo.specs.api.builders.project.Project
import com.atlassian.bamboo.specs.util.BambooServer
import com.atlassian.bamboo.specs.util.BambooSpecSerializer
import java.io.Writer

object Specs {
    private val entities = mutableSetOf>()
    private val mapEnvsToDeployments = mutableMapOf()

    fun addToEnvMap(env: Environment, deployment: Deployment) {
        mapEnvsToDeployments[env] = deployment
    }

    operator fun invoke(init: Specs.() -> Unit) = this.apply(init)

    fun project(key: String, name: String, description: String? = null, init: SpecsDsl = {}): Project {
        val project = Project()
        project.key(key)
        project.name(name)
        if (description != null) {
            project.description(description)
        }
        project.init()
        return project
    }

    fun projectPermissions(name: String, applyToPlans: Boolean, init: SpecsDsl = {}): ProjectPermissions {
        val p = ProjectPermissions(name)
        p.projectPermissions.init()
        if (applyToPlans) {
            p.projectPlanPermissions.init()
        }
        entities.add(p)
        return p
    }

    fun Project.permissions(
        applyToPlans: Boolean = true,
        init: SpecsDsl = {},
    ): ProjectPermissions = projectPermissions(this.name, applyToPlans, init)

    @JvmName("projectPlan")
    fun plan(
        project: Project,
        key: String,
        name: String,
        description: String? = null,
        init: SpecsDsl = {},
    ): Plan {
        val plan = Plan(project, name, key)
        this.entities.add(plan)
        if (description != null) {
            plan.description(description)
        }
        plan.init()
        return plan
    }

    fun Project.plan(key: String, name: String, description: String? = null, init: SpecsDsl = {}): Plan =
        plan(this, key, name, description, init)

    fun planPermissions(plan: Plan, setDefaults: Boolean = true, init: SpecsDsl = {}): PlanPermissions {
        val planPermissions = PlanPermissions(plan.identifier)
        this.entities.add(planPermissions)
        if (setDefaults) {
            planPermissions.addDefaultPermissions()
        }
        planPermissions.permissions.init()
        return planPermissions
    }

    fun Plan.permissions(setDefaults: Boolean = true, init: SpecsDsl = {}): PlanPermissions =
        planPermissions(this, setDefaults, init)

    @JvmName("planDeployment")
    fun deployment(plan: Plan, name: String, description: String? = null, init: SpecsDsl = {}): Deployment {
        val deployment = Deployment(plan.identifier, name)
        this.entities.add(deployment)
        if (description != null) {
            deployment.description(description)
        }
        deployment.init()
        return deployment
    }

    @JvmName("deploymentPermissions")
    fun deploymentPermissions(deployment: Deployment, init: SpecsDsl = {}): DeploymentPermissions {
        val deploymentPermissions = DeploymentPermissions(deployment.name)

        val permissions = Permissions()
        permissions.init()
        deploymentPermissions.permissions(permissions)

        entities.add(deploymentPermissions)
        return deploymentPermissions
    }

    fun Deployment.permissions(init: SpecsDsl = {}) = deploymentPermissions(this, init)

    fun environmentPermissions(
        deploymentProjectName: String,
        environmentName: String,
        init: SpecsDsl = {},
    ): EnvironmentPermissions {
        val environmentPermissions = EnvironmentPermissions(deploymentProjectName, environmentName)

        val permissions = Permissions()
        permissions.init()
        environmentPermissions.permissions(permissions)

        entities.add(environmentPermissions)
        return environmentPermissions
    }

    @Deprecated(
        "Kotlin Specs now grabs the deployment and environment names from the containing objects and doesn't need to be manually specified",
        ReplaceWith(
            "environmentPermissions(init)",
            "com.atlassian.bamboo.specs.extension.Specs.environmentPermissions",
        ),
    )
    fun Environment.permissions(
        deploymentProjectName: String,
        environmentName: String,
        init: SpecsDsl = {},
    ) = environmentPermissions(deploymentProjectName, environmentName, init)

    fun Environment.permissions(init: SpecsDsl = {}) =
        environmentPermissions(this.getDeploymentProjectName(), this.name, init)

    private fun Environment.getDeploymentProjectName(): String {
        return [email protected](this).name
    }

    fun Plan.deployment(name: String, description: String? = null, init: SpecsDsl = {}): Deployment =
        deployment(this, name, description, init)

    fun publish(bambooServer: BambooServer) = entities.forEach { bambooServer.publish(it) }

    fun printYaml(writer: Writer) = entities.forEach { BambooSpecSerializer.dump(it, writer) }

    fun toYaml(): String = entities.joinToString("\n") { BambooSpecSerializer.dump(it) }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy