Skip to content

Commit

Permalink
feat(managed-delivery): Add support for other types of code triggers (s…
Browse files Browse the repository at this point in the history
  • Loading branch information
luispollo authored and mergify[bot] committed Jan 8, 2020
1 parent 9207710 commit bfbcf2f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ data class GitTrigger
override var isRebake: Boolean = false,
override var isDryRun: Boolean = false,
override var isStrategy: Boolean = false,
val hash: String,
val source: String,
val project: String,
val branch: String,
val slug: String,
override val hash: String,
override val source: String,
override val project: String,
override val branch: String,
override val slug: String,
val action: String
) : Trigger {
) : Trigger, SourceCodeTrigger {
override var other: Map<String, Any> = mutableMapOf()
override var resolvedExpectedArtifacts: List<ExpectedArtifact> = mutableListOf()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2020 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.orca.pipeline.model

/**
* Defines properties that are common across different types of source code triggers.
*/
interface SourceCodeTrigger {
val source: String
val project: String
val branch: String
val slug: String
val hash: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import com.netflix.spinnaker.orca.pipeline.model.NexusTrigger
import com.netflix.spinnaker.orca.pipeline.model.PipelineTrigger
import com.netflix.spinnaker.orca.pipeline.model.Trigger

internal class TriggerDeserializer :
class TriggerDeserializer :
StdDeserializer<Trigger>(Trigger::class.java) {

companion object {
val customTriggerSuppliers: MutableList<CustomTriggerDeserializerSupplier> = mutableListOf()
val customTriggerSuppliers: MutableSet<CustomTriggerDeserializerSupplier> = mutableSetOf()
}

override fun deserialize(parser: JsonParser, context: DeserializationContext): Trigger =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.netflix.spinnaker.orca.KeelService
import com.netflix.spinnaker.orca.RetryableTask
import com.netflix.spinnaker.orca.TaskResult
import com.netflix.spinnaker.orca.igor.ScmService
import com.netflix.spinnaker.orca.pipeline.model.GitTrigger
import com.netflix.spinnaker.orca.pipeline.model.SourceCodeTrigger
import com.netflix.spinnaker.orca.pipeline.model.Stage
import com.netflix.spinnaker.orca.pipeline.model.Trigger
import org.slf4j.LoggerFactory
Expand All @@ -46,7 +46,7 @@ constructor(
private val log = LoggerFactory.getLogger(javaClass)

override fun execute(stage: Stage): TaskResult {
val context = objectMapper.convertValue<PublishDeliveryConfigContext>(stage.context)
val context = objectMapper.convertValue<ImportDeliveryConfigContext>(stage.context)
val trigger = stage.execution.trigger
val user = trigger.user ?: "anonymous"
val manifestLocation = processDeliveryConfigLocation(trigger, context)
Expand All @@ -61,23 +61,19 @@ constructor(

TaskResult.builder(ExecutionStatus.SUCCEEDED).context(emptyMap<String, Any?>()).build()
} catch (e: RetrofitError) {
handleFailure(e, context)
handleRetryableFailures(e, context)
} catch (e: Exception) {
log.error("Unexpected exception while executing {}, aborting.", javaClass.simpleName, e)
buildError(e.message)
}
}

private fun processDeliveryConfigLocation(trigger: Trigger, context: PublishDeliveryConfigContext): String {
if (trigger is GitTrigger) {
// if the pipeline has a git trigger, infer what context we can from the trigger
// FIXME: GitTrigger props are non-null, but IIUC branch and hash would be mutually exclusive, so what to do here?
if (trigger.hash != null && context.ref == null) {
private fun processDeliveryConfigLocation(trigger: Trigger, context: ImportDeliveryConfigContext): String {
if (trigger is SourceCodeTrigger) {
// if the pipeline has a source code trigger (git, etc.), infer what context we can from the trigger
if (context.ref == null) {
context.ref = trigger.hash
log.debug("Inferred context.ref from trigger: ${context.ref}")
} else if (trigger.branch != null && context.ref == null) {
context.ref = "refs/heads/${trigger.branch}"
log.debug("Inferred context.ref from trigger: ${context.ref}")
}
if (context.repoType == null) {
context.repoType = trigger.source
Expand All @@ -92,7 +88,7 @@ constructor(
log.debug("Inferred context.repository from trigger: ${context.repositorySlug}")
}
} else {
// otherwise, apply defaults where possible, or fail
// otherwise, apply defaults where possible, or fail if there's not enough information in the context
if (context.ref == null) {
context.ref = "refs/heads/master"
}
Expand All @@ -106,7 +102,7 @@ constructor(
?: ""}/${context.manifest}@${context.ref}"
}

private fun handleFailure(e: RetrofitError, context: PublishDeliveryConfigContext): TaskResult {
private fun handleRetryableFailures(e: RetrofitError, context: ImportDeliveryConfigContext): TaskResult {
return when {
e.kind == RetrofitError.Kind.NETWORK -> {
// retry if unable to connect
Expand All @@ -127,7 +123,7 @@ constructor(
}
}

private fun buildRetry(context: PublishDeliveryConfigContext): TaskResult {
private fun buildRetry(context: ImportDeliveryConfigContext): TaskResult {
context.incrementAttempt()
return if (context.attempt > context.maxRetries!!) {
val error = "Maximum number of retries exceeded (${context.maxRetries})"
Expand All @@ -152,7 +148,7 @@ constructor(
"$message: ${cause?.message ?: ""}"
}

data class PublishDeliveryConfigContext(
data class ImportDeliveryConfigContext(
var repoType: String? = null,
var projectKey: String? = null,
var repositorySlug: String? = null,
Expand All @@ -163,8 +159,8 @@ constructor(
val maxRetries: Int? = MAX_RETRIES
)

fun PublishDeliveryConfigContext.incrementAttempt() = this.also { attempt += 1 }
fun PublishDeliveryConfigContext.toMap() = objectMapper.convertValue<Map<String, Any?>>(this)
fun ImportDeliveryConfigContext.incrementAttempt() = this.also { attempt += 1 }
fun ImportDeliveryConfigContext.toMap() = objectMapper.convertValue<Map<String, Any?>>(this)

companion object {
const val MAX_RETRIES = 5
Expand Down

0 comments on commit bfbcf2f

Please sign in to comment.