Skip to content

Commit d2ca66b

Browse files
authored
fix(pubsub): Add payload to pubsub triggers. (spinnaker#221)
1 parent dfe3a4e commit d2ca66b

File tree

5 files changed

+35
-45
lines changed

5 files changed

+35
-45
lines changed

echo-model/src/main/java/com/netflix/spinnaker/echo/model/Trigger.java

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
@JsonDeserialize(builder = Trigger.TriggerBuilder.class)
3030
@Builder(toBuilder = true)
3131
@Wither
32-
@ToString(of = {"type", "master", "job", "cronExpression", "source", "project", "slug", "account", "repository", "tag", "parameters", "payloadConstraints", "attributeConstraints", "branch", "runAsUser", "subscriptionName", "pubsubSystem", "expectedArtifactIds"}, includeFieldNames = false)
32+
@ToString(of = {"type", "master", "job", "cronExpression", "source", "project", "slug", "account", "repository", "tag", "parameters", "payloadConstraints", "attributeConstraints", "branch", "runAsUser", "subscriptionName", "pubsubSystem", "expectedArtifactIds", "payload"}, includeFieldNames = false)
3333
@Value
3434
public class Trigger {
3535
public enum Type {
@@ -71,6 +71,7 @@ public String toString() {
7171
String tag;
7272
String digest;
7373
Map payloadConstraints;
74+
Map payload;
7475
Map attributeConstraints;
7576
String branch;
7677
String runAsUser;
@@ -90,10 +91,6 @@ public Trigger atBuildNumber(final int buildNumber) {
9091
.buildNumber(buildNumber)
9192
.hash(null)
9293
.tag(null)
93-
.payloadConstraints(null)
94-
.attributeConstraints(null)
95-
.subscriptionName(null)
96-
.pubsubSystem(null)
9794
.build();
9895
}
9996

@@ -102,22 +99,14 @@ public Trigger atHash(final String hash) {
10299
.buildNumber(null)
103100
.hash(hash)
104101
.tag(null)
105-
.payloadConstraints(null)
106-
.attributeConstraints(null)
107-
.subscriptionName(null)
108-
.pubsubSystem(null)
109102
.build();
110103
}
111104

112105
public Trigger atBranch(final String branch) {
113106
return this.toBuilder()
114107
.buildNumber(null)
115108
.tag(null)
116-
.payloadConstraints(null)
117-
.attributeConstraints(null)
118109
.branch(branch)
119-
.subscriptionName(null)
120-
.pubsubSystem(null)
121110
.build();
122111
}
123112

@@ -126,23 +115,18 @@ public Trigger atTag(final String tag) {
126115
.buildNumber(null)
127116
.hash(null)
128117
.tag(tag)
129-
.payloadConstraints(null)
130-
.attributeConstraints(null)
131-
.subscriptionName(null)
132-
.pubsubSystem(null)
133118
.build();
134119
}
135120

136-
public Trigger atPayloadConstraints(final Map parameters, final Map payloadConstraints) {
121+
public Trigger atPayload(final Map payload) {
122+
return this.toBuilder()
123+
.payload(payload)
124+
.build();
125+
}
126+
127+
public Trigger atParameters(final Map parameters) {
137128
return this.toBuilder()
138-
.buildNumber(null)
139-
.hash(null)
140-
.digest(null)
141129
.parameters(parameters)
142-
.payloadConstraints(payloadConstraints)
143-
.attributeConstraints(null)
144-
.subscriptionName(null)
145-
.pubsubSystem(null)
146130
.build();
147131
}
148132

@@ -152,8 +136,6 @@ public Trigger atSecret(final String secret) {
152136
.hash(null)
153137
.digest(null)
154138
.secret(secret)
155-
.subscriptionName(null)
156-
.pubsubSystem(null)
157139
.build();
158140
}
159141

echo-pipelinetriggers/src/main/java/com/netflix/spinnaker/echo/pipelinetriggers/monitor/PubsubEventMonitor.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import rx.Observable;
3535
import rx.functions.Action1;
3636

37+
import java.util.HashMap;
38+
import java.util.Map;
3739
import java.util.function.Function;
3840
import java.util.function.Predicate;
3941

@@ -84,10 +86,15 @@ protected boolean isSuccessfulTriggerEvent(final TriggerEvent event) {
8486
@Override
8587
protected Function<Trigger, Pipeline> buildTrigger(Pipeline pipeline, TriggerEvent event) {
8688
PubsubEvent pubsubEvent = (PubsubEvent) event;
89+
Map payload = pubsubEvent.getPayload();
90+
Map parameters = payload.containsKey("parameters") ? (Map) payload.get("parameters") : new HashMap();
8791
MessageDescription description = pubsubEvent.getContent().getMessageDescription();
8892
return trigger -> pipeline
8993
.withReceivedArtifacts(description.getArtifacts())
90-
.withTrigger(trigger.atMessageDescription(description.getSubscriptionName(), description.getPubsubSystem().toString()));
94+
.withTrigger(trigger
95+
.atMessageDescription(description.getSubscriptionName(), description.getPubsubSystem().toString())
96+
.atParameters(parameters)
97+
.atPayload(payload));
9198
}
9299

93100
@Override

echo-pipelinetriggers/src/main/java/com/netflix/spinnaker/echo/pipelinetriggers/monitor/WebhookEventMonitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected boolean isSuccessfulTriggerEvent(final TriggerEvent event) {
8484
protected Function<Trigger, Pipeline> buildTrigger(Pipeline pipeline, TriggerEvent event) {
8585
Map payload = event.getPayload();
8686
Map parameters = payload.containsKey("parameters") ? (Map) payload.get("parameters") : new HashMap();
87-
return trigger -> pipeline.withTrigger(trigger.atPayloadConstraints(parameters, payload));
87+
return trigger -> pipeline.withTrigger(trigger.atParameters(parameters).atPayload(payload));
8888
}
8989

9090
@Override

echo-pipelinetriggers/src/test/groovy/com/netflix/spinnaker/echo/pipelinetriggers/PubsubEventMonitorSpec.groovy

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ class PubsubEventMonitorSpec extends Specification implements RetrofitStubs {
9898
})
9999

100100
where:
101-
event | trigger
102-
createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", null) | enabledGooglePubsubTrigger
103-
createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", []) | enabledGooglePubsubTrigger
104-
createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", goodArtifacts) | enabledGooglePubsubTrigger.withExpectedArtifactIds(goodExpectedArtifacts*.id)
105-
createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", goodArtifacts) | enabledGooglePubsubTrigger.withExpectedArtifactIds(goodRegexExpectedArtifacts*.id)
106-
createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", goodArtifacts) | enabledGooglePubsubTrigger // Trigger doesn't care about artifacts.
101+
event | trigger
102+
createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", null, [:]) | enabledGooglePubsubTrigger
103+
createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", [], [:]) | enabledGooglePubsubTrigger
104+
createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", goodArtifacts, [:]) | enabledGooglePubsubTrigger.withExpectedArtifactIds(goodExpectedArtifacts*.id)
105+
createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", goodArtifacts, [:]) | enabledGooglePubsubTrigger.withExpectedArtifactIds(goodRegexExpectedArtifacts*.id)
106+
createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", goodArtifacts, [:]) | enabledGooglePubsubTrigger // Trigger doesn't care about artifacts.
107107
// TODO(jacobkiefer): Add Kafka cases when that is implemented.
108108
}
109109

@@ -123,7 +123,7 @@ class PubsubEventMonitorSpec extends Specification implements RetrofitStubs {
123123
disabledGooglePubsubTrigger | "disabled Google pubsub trigger"
124124

125125
pipeline = createPipelineWith(goodExpectedArtifacts, trigger)
126-
event = createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", [])
126+
event = createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", [], [:])
127127
// TODO(jacobkiefer): Add Kafka cases when that is implemented.
128128
}
129129

@@ -142,7 +142,7 @@ class PubsubEventMonitorSpec extends Specification implements RetrofitStubs {
142142
})
143143

144144
where:
145-
event = createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", [])
145+
event = createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", [], [:])
146146
pipeline = createPipelineWith(goodExpectedArtifacts, enabledGooglePubsubTrigger, disabledGooglePubsubTrigger)
147147
}
148148

@@ -164,7 +164,7 @@ class PubsubEventMonitorSpec extends Specification implements RetrofitStubs {
164164
enabledGooglePubsubTrigger.withPubsubSystem("noogle") | "different subscription name"
165165

166166
pipeline = createPipelineWith(goodExpectedArtifacts, trigger)
167-
event = createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", [])
167+
event = createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", [], [:])
168168
}
169169

170170
@Unroll
@@ -183,7 +183,7 @@ class PubsubEventMonitorSpec extends Specification implements RetrofitStubs {
183183
enabledGooglePubsubTrigger.withExpectedArtifactIds(badExpectedArtifacts*.id) | "non-matching artifact in message"
184184

185185
pipeline = createPipelineWith(goodExpectedArtifacts, trigger)
186-
event = createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", goodArtifacts)
186+
event = createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", goodArtifacts, [:])
187187
}
188188

189189
@Unroll
@@ -202,7 +202,7 @@ class PubsubEventMonitorSpec extends Specification implements RetrofitStubs {
202202
enabledGooglePubsubTrigger.withSubscriptionName(null) | "subscriptionName"
203203
enabledGooglePubsubTrigger.withPubsubSystem(null) | "pubsubSystem"
204204

205-
event = createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", [])
205+
event = createPubsubEvent(PubsubSystem.GOOGLE, "projects/project/subscriptions/subscription", [], [:])
206206
goodPipeline = createPipelineWith(goodExpectedArtifacts, enabledGooglePubsubTrigger)
207207
badPipeline = createPipelineWith(goodExpectedArtifacts, trigger)
208208
}
@@ -237,8 +237,8 @@ class PubsubEventMonitorSpec extends Specification implements RetrofitStubs {
237237
})
238238

239239
where:
240-
trigger | callCount
241-
enabledGooglePubsubTrigger | 1
240+
trigger | callCount
241+
enabledGooglePubsubTrigger | 1
242242
enabledGooglePubsubTrigger.withPayloadConstraints([key: 'value']) | 1
243243
enabledGooglePubsubTrigger.withPayloadConstraints([key: 'wrongValue']) | 0
244244
}
@@ -274,8 +274,8 @@ class PubsubEventMonitorSpec extends Specification implements RetrofitStubs {
274274
})
275275

276276
where:
277-
trigger | callCount
278-
enabledGooglePubsubTrigger | 1
277+
trigger | callCount
278+
enabledGooglePubsubTrigger | 1
279279
enabledGooglePubsubTrigger.withAttributeConstraints([key: 'value']) | 1
280280
enabledGooglePubsubTrigger.withAttributeConstraints([key: 'wrongValue']) | 0
281281
}

echo-pipelinetriggers/src/test/groovy/com/netflix/spinnaker/echo/test/RetrofitStubs.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ trait RetrofitStubs {
8787
return res
8888
}
8989

90-
PubsubEvent createPubsubEvent(PubsubSystem pubsubSystem, String subscriptionName, List<Artifact> artifacts) {
90+
PubsubEvent createPubsubEvent(PubsubSystem pubsubSystem, String subscriptionName, List<Artifact> artifacts, Map payload) {
9191
def res = new PubsubEvent()
9292

9393
def description = MessageDescription.builder()
@@ -102,6 +102,7 @@ trait RetrofitStubs {
102102

103103
res.details = new Metadata([type: PubsubEventMonitor.PUBSUB_TRIGGER_TYPE])
104104
res.content = content
105+
res.payload = payload
105106
return res
106107
}
107108

0 commit comments

Comments
 (0)