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

net.nemerosa.ontrack.kdsl.spec.PromotionLevelExtensions.kt Maven / Gradle / Ivy

There is a newer version: 4.4.5
Show newest version
package net.nemerosa.ontrack.kdsl.spec

import com.fasterxml.jackson.databind.JsonNode
import net.nemerosa.ontrack.json.asJson
import net.nemerosa.ontrack.kdsl.connector.Connected
import net.nemerosa.ontrack.kdsl.connector.graphql.convert
import net.nemerosa.ontrack.kdsl.connector.graphql.schema.PromotionLevelDeletePropertyMutation
import net.nemerosa.ontrack.kdsl.connector.graphql.schema.PromotionLevelGetPropertyQuery
import net.nemerosa.ontrack.kdsl.connector.graphql.schema.PromotionLevelSetPropertyMutation
import net.nemerosa.ontrack.kdsl.connector.graphql.schema.fragment.PromotionLevelFragment
import net.nemerosa.ontrack.kdsl.connector.graphqlConnector

/**
 * Creates a [PromotionLevel] from a GraphQL [PromotionLevelFragment].
 */
fun PromotionLevelFragment.toPromotionLevel(connected: Connected) = PromotionLevel(
    connector = connected.connector,
    id = id().toUInt(),
    name = name()!!,
    description = description(),
)

/**
 * Sets a generic property on a [PromotionLevel].
 *
 * @param type FQCN of the property type
 * @param data Property raw data (will be converted into JSON)
 */
fun PromotionLevel.setProperty(
    type: String,
    data: Any,
): PromotionLevel {
    graphqlConnector.mutate(
        PromotionLevelSetPropertyMutation(
            id.toInt(),
            type,
            data.asJson()
        )
    ) {
        it?.setPromotionLevelPropertyById()?.fragments()?.payloadUserErrors()?.convert()
    }
    return this
}

/**
 * Deletes a generic property on a [PromotionLevel].
 *
 * @param type FQCN of the property type
 */
fun PromotionLevel.deleteProperty(
    type: String,
): PromotionLevel {
    graphqlConnector.mutate(
        PromotionLevelDeletePropertyMutation(
            id.toInt(),
            type
        )
    ) {
        it?.setPromotionLevelPropertyById()?.fragments()?.payloadUserErrors()?.convert()
    }
    return this
}

/**
 * Gets a generic property on a [PromotionLevel].
 *
 * @param type FQCN of the property type
 * @return Property raw data as JSON
 */
fun PromotionLevel.getProperty(
    type: String,
): JsonNode? =
    graphqlConnector.query(
        PromotionLevelGetPropertyQuery(id.toInt(), type)
    )?.promotionLevel()?.properties()?.firstOrNull()?.value()?.asJson()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy