net.nemerosa.ontrack.kdsl.spec.BuildExtensions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ontrack-kdsl Show documentation
Show all versions of ontrack-kdsl Show documentation
Ontrack module: ontrack-kdsl
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.BuildDeletePropertyMutation
import net.nemerosa.ontrack.kdsl.connector.graphql.schema.BuildGetPropertyQuery
import net.nemerosa.ontrack.kdsl.connector.graphql.schema.BuildSetPropertyMutation
import net.nemerosa.ontrack.kdsl.connector.graphql.schema.fragment.BuildFragment
import net.nemerosa.ontrack.kdsl.connector.graphqlConnector
/**
* Creates a [Build] from a GraphQL [BuildFragment].
*/
fun BuildFragment.toBuild(connected: Connected) = Build(
connector = connected.connector,
branch = branch()?.fragments()?.branchFragment()?.toBranch(connected) ?: error("Missing parent branch"),
id = id().toUInt(),
name = name()!!,
description = description(),
)
/**
* Sets a generic property on a [Build].
*
* @param type FQCN of the property type
* @param data Property raw data (will be converted into JSON)
*/
fun Build.setProperty(
type: String,
data: Any,
): Build {
graphqlConnector.mutate(
BuildSetPropertyMutation(
id.toInt(),
type,
data.asJson()
)
) {
it?.setBuildPropertyById()?.fragments()?.payloadUserErrors()?.convert()
}
return this
}
/**
* Deletes a generic property on a [Build].
*
* @param type FQCN of the property type
*/
fun Build.deleteProperty(
type: String
): Build {
graphqlConnector.mutate(
BuildDeletePropertyMutation(
id.toInt(),
type
)
) {
it?.setBuildPropertyById()?.fragments()?.payloadUserErrors()?.convert()
}
return this
}
/**
* Gets a generic property on a [Build].
*
* @param type FQCN of the property type
* @return Property raw data as JSON
*/
fun Build.getProperty(
type: String,
): JsonNode? =
graphqlConnector.query(
BuildGetPropertyQuery(id.toInt(), type)
)?.builds()?.firstOrNull()?.properties()?.firstOrNull()?.value()?.asJson()
© 2015 - 2024 Weber Informatics LLC | Privacy Policy