@@ -2,23 +2,19 @@ package github
2
2
3
3
import (
4
4
"encoding/json"
5
- "errors"
6
5
"fmt"
7
6
"io/ioutil"
8
7
"mime"
9
8
"net/http"
10
9
11
10
kapi "k8s.io/kubernetes/pkg/api"
11
+ "k8s.io/kubernetes/pkg/api/errors"
12
12
13
13
"github.com/golang/glog"
14
14
"github.com/openshift/origin/pkg/build/api"
15
15
"github.com/openshift/origin/pkg/build/webhook"
16
16
)
17
17
18
- var (
19
- ErrNoGitHubEvent = errors .New ("missing X-GitHub-Event, X-Gogs-Event or X-Gitlab-Event" )
20
- )
21
-
22
18
// WebHook used for processing github webhook requests.
23
19
type WebHook struct {}
24
20
@@ -58,18 +54,18 @@ func (p *WebHook) Extract(buildCfg *api.BuildConfig, secret, path string, req *h
58
54
}
59
55
method := getEvent (req .Header )
60
56
if method != "ping" && method != "push" && method != "Push Hook" {
61
- return revision , envvars , proceed , fmt .Errorf ("Unknown X-GitHub-Event, X-Gogs-Event or X-Gitlab-Event %s" , method )
57
+ return revision , envvars , proceed , errors . NewBadRequest ( fmt .Sprintf ("Unknown X-GitHub-Event, X-Gogs-Event or X-Gitlab-Event %s" , method ) )
62
58
}
63
59
if method == "ping" {
64
60
return revision , envvars , proceed , err
65
61
}
66
62
body , err := ioutil .ReadAll (req .Body )
67
63
if err != nil {
68
- return revision , envvars , proceed , err
64
+ return revision , envvars , proceed , errors . NewBadRequest ( err . Error ())
69
65
}
70
66
var event pushEvent
71
67
if err = json .Unmarshal (body , & event ); err != nil {
72
- return revision , envvars , proceed , err
68
+ return revision , envvars , proceed , errors . NewBadRequest ( err . Error ())
73
69
}
74
70
if ! webhook .GitRefMatches (event .Ref , webhook .DefaultConfigRef , & buildCfg .Spec .Source ) {
75
71
glog .V (2 ).Infof ("Skipping build for BuildConfig %s/%s. Branch reference from '%s' does not match configuration" , buildCfg .Namespace , buildCfg , event )
@@ -89,18 +85,18 @@ func (p *WebHook) Extract(buildCfg *api.BuildConfig, secret, path string, req *h
89
85
90
86
func verifyRequest (req * http.Request ) error {
91
87
if method := req .Method ; method != "POST" {
92
- return fmt . Errorf ( "unsupported HTTP method %s" , method )
88
+ return webhook . MethodNotSupported
93
89
}
94
90
contentType := req .Header .Get ("Content-Type" )
95
91
mediaType , _ , err := mime .ParseMediaType (contentType )
96
92
if err != nil {
97
- return fmt .Errorf ("non-parseable Content-Type %s (%s)" , contentType , err )
93
+ return errors . NewBadRequest ( fmt .Sprintf ("non-parseable Content-Type %s (%s)" , contentType , err ) )
98
94
}
99
95
if mediaType != "application/json" {
100
- return fmt .Errorf ("unsupported Content-Type %s" , contentType )
96
+ return errors . NewBadRequest ( fmt .Sprintf ("unsupported Content-Type %s" , contentType ) )
101
97
}
102
98
if len (getEvent (req .Header )) == 0 {
103
- return ErrNoGitHubEvent
99
+ return errors . NewBadRequest ( "missing X-GitHub-Event, X-Gogs-Event or X-Gitlab-Event" )
104
100
}
105
101
return nil
106
102
}
0 commit comments