Skip to content

Commit 10ba6bd

Browse files
committed
tolerate yaml payloads for generic webhooks
1 parent 9c76951 commit 10ba6bd

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

examples/sample-app/application-template-stibuild.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@
108108
{
109109
"type": "Generic",
110110
"generic": {
111-
"secret": "secret101"
111+
"secret": "secret101",
112+
"allowEnv": true
112113
}
113114
},
114115
{

pkg/build/webhook/generic/generic.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/golang/glog"
1111

1212
kapi "k8s.io/kubernetes/pkg/api"
13+
"k8s.io/kubernetes/pkg/util/yaml"
1314

1415
"github.com/openshift/origin/pkg/build/api"
1516
"github.com/openshift/origin/pkg/build/webhook"
@@ -54,7 +55,7 @@ func (p *WebHookPlugin) Extract(buildCfg *api.BuildConfig, secret, path string,
5455
}
5556
}
5657

57-
if req.Body != nil && contentType == "application/json" {
58+
if req.Body != nil && (contentType == "application/json" || contentType == "application/yaml") {
5859
body, err := ioutil.ReadAll(req.Body)
5960
if err != nil {
6061
return nil, envvars, false, err
@@ -65,8 +66,15 @@ func (p *WebHookPlugin) Extract(buildCfg *api.BuildConfig, secret, path string,
6566
}
6667

6768
var data api.GenericWebHookEvent
69+
if contentType == "application/yaml" {
70+
body, err = yaml.ToJSON(body)
71+
if err != nil {
72+
glog.V(4).Infof("Error converting payload to json %v, but continuing with build", err)
73+
return nil, envvars, true, nil
74+
}
75+
}
6876
if err = json.Unmarshal(body, &data); err != nil {
69-
glog.V(4).Infof("Error unmarshaling json %v, but continuing", err)
77+
glog.V(4).Infof("Error unmarshalling payload %v, but continuing with build", err)
7078
return nil, envvars, true, nil
7179
}
7280
if len(data.Env) > 0 && trigger.AllowEnv {

pkg/build/webhook/generic/generic_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ func TestExtractWithUnmatchedGitRefsPayload(t *testing.T) {
482482
}
483483
}
484484

485-
func TestExtractWithKeyValuePairs(t *testing.T) {
485+
func TestExtractWithKeyValuePairsJSON(t *testing.T) {
486486
req := GivenRequestWithPayload(t, "push-generic-envs.json")
487487
buildConfig := &api.BuildConfig{
488488
Spec: api.BuildConfigSpec{
@@ -523,6 +523,47 @@ func TestExtractWithKeyValuePairs(t *testing.T) {
523523
}
524524
}
525525

526+
func TestExtractWithKeyValuePairsYAML(t *testing.T) {
527+
req := GivenRequestWithPayloadAndContentType(t, "push-generic-envs.yaml", "application/yaml")
528+
buildConfig := &api.BuildConfig{
529+
Spec: api.BuildConfigSpec{
530+
Triggers: []api.BuildTriggerPolicy{
531+
{
532+
Type: api.GenericWebHookBuildTriggerType,
533+
GenericWebHook: &api.WebHookTrigger{
534+
Secret: "secret100",
535+
AllowEnv: true,
536+
},
537+
},
538+
},
539+
CommonSpec: api.CommonSpec{
540+
Source: api.BuildSource{
541+
Git: &api.GitBuildSource{
542+
Ref: "master",
543+
},
544+
},
545+
Strategy: mockBuildStrategy,
546+
},
547+
},
548+
}
549+
plugin := New()
550+
revision, envvars, proceed, err := plugin.Extract(buildConfig, "secret100", "", req)
551+
552+
if err != nil {
553+
t.Errorf("Expected to be able to trigger a build without a payload error: %v", err)
554+
}
555+
if !proceed {
556+
t.Error("Expected 'proceed' return value to be 'true'")
557+
}
558+
if revision == nil {
559+
t.Error("Expected the 'revision' return value to not be nil")
560+
}
561+
562+
if len(envvars) == 0 {
563+
t.Error("Expected env vars to be set")
564+
}
565+
}
566+
526567
func TestExtractWithKeyValuePairsDisabled(t *testing.T) {
527568
req := GivenRequestWithPayload(t, "push-generic-envs.json")
528569
buildConfig := &api.BuildConfig{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
type: "Git"
3+
git:
4+
uri: "git://mygitserver/myrepo.git"
5+
ref: "refs/heads/master"
6+
commit: "9bdc3a26ff933b32f3e558636b58aea86a69f051"
7+
message: "Random act of kindness"
8+
author:
9+
name: "Jon Doe"
10+
11+
committer:
12+
name: "Jon Doe"
13+
14+
env:
15+
-
16+
name: "EXAMPLE"
17+
value: "sample-app"

0 commit comments

Comments
 (0)