Skip to content

support yaml payloads for generic webhooks #10031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/sample-app/application-template-stibuild.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@
{
"type": "Generic",
"generic": {
"secret": "secret101"
"secret": "secret101",
"allowEnv": true
}
},
{
Expand Down
12 changes: 10 additions & 2 deletions pkg/build/webhook/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/golang/glog"

kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/util/yaml"

"github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/build/webhook"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (p *WebHookPlugin) Extract(buildCfg *api.BuildConfig, secret, path string,
}
}

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

var data api.GenericWebHookEvent
if contentType == "application/yaml" {
body, err = yaml.ToJSON(body)
if err != nil {
glog.V(4).Infof("Error converting payload to json %v, but continuing with build", err)
return nil, envvars, true, nil
}
}
if err = json.Unmarshal(body, &data); err != nil {
glog.V(4).Infof("Error unmarshaling json %v, but continuing", err)
glog.V(4).Infof("Error unmarshalling payload %v, but continuing with build", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh... I didn't realize we continued in cases where the body was invalid. I could maybe see doing that if the body was empty, but it seems wrong to just ignore malformed content, especially if the data could change the behavior of the launched build

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't remember exactly why we made that choice, but i think we're mostly stuck with it now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's also unfortunate that all other content types get ignored and "successfully" do a generic build

return nil, envvars, true, nil
}
if len(data.Env) > 0 && trigger.AllowEnv {
Expand Down
43 changes: 42 additions & 1 deletion pkg/build/webhook/generic/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ func TestExtractWithUnmatchedGitRefsPayload(t *testing.T) {
}
}

func TestExtractWithKeyValuePairs(t *testing.T) {
func TestExtractWithKeyValuePairsJSON(t *testing.T) {
req := GivenRequestWithPayload(t, "push-generic-envs.json")
buildConfig := &api.BuildConfig{
Spec: api.BuildConfigSpec{
Expand Down Expand Up @@ -523,6 +523,47 @@ func TestExtractWithKeyValuePairs(t *testing.T) {
}
}

func TestExtractWithKeyValuePairsYAML(t *testing.T) {
req := GivenRequestWithPayloadAndContentType(t, "push-generic-envs.yaml", "application/yaml")
buildConfig := &api.BuildConfig{
Spec: api.BuildConfigSpec{
Triggers: []api.BuildTriggerPolicy{
{
Type: api.GenericWebHookBuildTriggerType,
GenericWebHook: &api.WebHookTrigger{
Secret: "secret100",
AllowEnv: true,
},
},
},
CommonSpec: api.CommonSpec{
Source: api.BuildSource{
Git: &api.GitBuildSource{
Ref: "master",
},
},
Strategy: mockBuildStrategy,
},
},
}
plugin := New()
revision, envvars, proceed, err := plugin.Extract(buildConfig, "secret100", "", req)

if err != nil {
t.Errorf("Expected to be able to trigger a build without a payload error: %v", err)
}
if !proceed {
t.Error("Expected 'proceed' return value to be 'true'")
}
if revision == nil {
t.Error("Expected the 'revision' return value to not be nil")
}

if len(envvars) == 0 {
t.Error("Expected env vars to be set")
}
}

func TestExtractWithKeyValuePairsDisabled(t *testing.T) {
req := GivenRequestWithPayload(t, "push-generic-envs.json")
buildConfig := &api.BuildConfig{
Expand Down
17 changes: 17 additions & 0 deletions pkg/build/webhook/generic/testdata/push-generic-envs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
type: "Git"
git:
uri: "git://mygitserver/myrepo.git"
ref: "refs/heads/master"
commit: "9bdc3a26ff933b32f3e558636b58aea86a69f051"
message: "Random act of kindness"
author:
name: "Jon Doe"
email: "[email protected]"
committer:
name: "Jon Doe"
email: "[email protected]"
env:
-
name: "EXAMPLE"
value: "sample-app"