forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.go
94 lines (84 loc) · 2.79 KB
/
generic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package generic
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/build/webhook"
)
// WebHookPlugin used for processing manual(or other) webhook requests.
type WebHookPlugin struct{}
// New returns a generic webhook plugin.
func New() *WebHookPlugin {
return &WebHookPlugin{}
}
// Extract services generic webhooks.
func (p *WebHookPlugin) Extract(buildCfg *api.BuildConfig, secret, path string, req *http.Request) (revision *api.SourceRevision, proceed bool, err error) {
trigger, ok := webhook.FindTriggerPolicy(api.GenericWebHookBuildTriggerType, buildCfg)
if !ok {
err = webhook.ErrHookNotEnabled
return
}
glog.V(4).Infof("Checking if the provided secret for BuildConfig %s/%s matches", buildCfg.Namespace, buildCfg.Name)
if trigger.GenericWebHook.Secret != secret {
err = webhook.ErrSecretMismatch
return
}
glog.V(4).Infof("Verifying build request for BuildConfig %s/%s", buildCfg.Namespace, buildCfg.Name)
if err = verifyRequest(req); err != nil {
return
}
git := buildCfg.Spec.Source.Git
if git == nil {
glog.V(4).Infof("No source defined for BuildConfig %s/%s, but triggering anyway", buildCfg.Namespace, buildCfg.Name)
return nil, true, nil
}
if req.Body != nil && req.Header.Get("Content-Type") == "application/json" {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, false, err
}
if len(body) == 0 {
return nil, true, nil
}
var data api.GenericWebHookEvent
if err = json.Unmarshal(body, &data); err != nil {
glog.V(4).Infof("Error unmarshaling json %v, but continuing", err)
return nil, true, nil
}
if data.Git == nil {
glog.V(4).Infof("No git information for the generic webhook found in %s/%s", buildCfg.Namespace, buildCfg.Name)
return nil, true, nil
}
if data.Git.Refs != nil {
for _, ref := range data.Git.Refs {
if webhook.GitRefMatches(ref.Ref, git.Ref) {
revision = &api.SourceRevision{
Type: api.BuildSourceGit,
Git: &ref.GitSourceRevision,
}
return revision, true, nil
}
}
glog.V(2).Infof("Skipping build for BuildConfig %s/%s. None of the supplied refs matched %q", buildCfg.Namespace, buildCfg, git.Ref)
return nil, false, nil
}
if !webhook.GitRefMatches(data.Git.Ref, git.Ref) {
glog.V(2).Infof("Skipping build for BuildConfig %s/%s. Branch reference from %q does not match configuration", buildCfg.Namespace, buildCfg.Name, data.Git.Ref)
return nil, false, nil
}
revision = &api.SourceRevision{
Type: api.BuildSourceGit,
Git: &data.Git.GitSourceRevision,
}
}
return revision, true, nil
}
func verifyRequest(req *http.Request) error {
if method := req.Method; method != "POST" {
return fmt.Errorf("Unsupported HTTP method %s", method)
}
return nil
}