Skip to content

Commit

Permalink
feat(webhooks): Add support for webhook that doesn monitor only (#3485)
Browse files Browse the repository at this point in the history
Sometimes, it's useful to simply poll a URL without ever doing a "create" on it.
If that URL returns 400's, the monitorWebhook handles it nicely while the createWebhook does not.
This change adds a `monitorOnly` flag to the stage context that allows user to skip the createWebhook part

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
marchello2000 and mergify[bot] authored Mar 4, 2020
1 parent f64cfa2 commit e31df93
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.netflix.spinnaker.orca.webhook.pipeline

import com.fasterxml.jackson.annotation.JsonFormat
import com.netflix.spinnaker.kork.exceptions.UserException
import com.netflix.spinnaker.orca.CancellableStage
import com.netflix.spinnaker.orca.pipeline.StageDefinitionBuilder
import com.netflix.spinnaker.orca.pipeline.TaskNode
Expand Down Expand Up @@ -52,7 +53,13 @@ class WebhookStage implements StageDefinitionBuilder {
void taskGraph(Stage stage, TaskNode.Builder builder) {
StageData stageData = stage.mapTo(StageData)

builder.withTask("createWebhook", CreateWebhookTask)
if (stageData.monitorOnly && !stageData.waitForCompletion) {
throw new UserException("Can't specify monitorOnly = true and waitForCompletion = false at the same time")
}

if (!stageData.monitorOnly) {
builder.withTask("createWebhook", CreateWebhookTask)
}
if (stageData.waitForCompletion) {
if (stageData.waitBeforeMonitor > 0) {
stage.context.putIfAbsent("waitTime", stageData.waitBeforeMonitor)
Expand Down Expand Up @@ -80,6 +87,7 @@ class WebhookStage implements StageDefinitionBuilder {
public Boolean waitForCompletion
public WebhookProperties.StatusUrlResolution statusUrlResolution
public String statusUrlJsonPath
public Boolean monitorOnly

@JsonFormat(with = [JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES])
public HttpMethod method = HttpMethod.POST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.netflix.spinnaker.orca.webhook.pipeline

import com.netflix.spinnaker.kork.exceptions.UserException
import com.netflix.spinnaker.orca.pipeline.TaskNode
import com.netflix.spinnaker.orca.pipeline.model.Execution
import com.netflix.spinnaker.orca.pipeline.model.Stage
Expand All @@ -41,31 +42,48 @@ class WebhookStageSpec extends Specification {
"webhook",
[
waitForCompletion: waitForCompletion,
waitBeforeMonitor: waitTime
waitBeforeMonitor: waitTime,
monitorOnly: monitorOnly
])

when:
webhookStage.taskGraph(stage, builder)

then:
1 * builder.withTask("createWebhook", CreateWebhookTask)

then:
expectedCreateTaskCount * builder.withTask("createWebhook", CreateWebhookTask)
expectedWaitTaskCount * builder.withTask("waitBeforeMonitorWebhook", WaitTask)

then:
expectedMonitorTaskCount * builder.withTask("monitorWebhook", MonitorWebhookTask)

stage.context.waitTime == expectedWaitTimeInContext

where:
waitForCompletion | waitTime || expectedWaitTimeInContext | expectedWaitTaskCount | expectedMonitorTaskCount
true | 10 || 10 | 1 | 1
true | "2" || 2 | 1 | 1
"true" | 0 || null | 0 | 1
true | -1 || null | 0 | 1
false | 10 || null | 0 | 0
false | 0 || null | 0 | 0
waitForCompletion | monitorOnly | waitTime || expectedWaitTimeInContext | expectedWaitTaskCount | expectedMonitorTaskCount | expectedCreateTaskCount
true | false | 10 || 10 | 1 | 1 | 1
true | null | "2" || 2 | 1 | 1 | 1
"true" | "false" | 0 || null | 0 | 1 | 1
true | false | -1 || null | 0 | 1 | 1
false | false | 10 || null | 0 | 0 | 1
false | false | 0 || null | 0 | 0 | 1
true | true | 10 || 10 | 1 | 1 | 0
true | "true" | "2" || 2 | 1 | 1 | 0
"true" | true | 0 || null | 0 | 1 | 0
true | "true" | -1 || null | 0 | 1 | 0
}

def "Should throw on invalid input"() {
given:
def stage = new Stage(
Execution.newPipeline("orca"),
"webhook",
[
waitForCompletion: false,
monitorOnly: true
])

when:
webhookStage.taskGraph(stage, builder)

then:
thrown(UserException)
}
}

0 comments on commit e31df93

Please sign in to comment.