-
Notifications
You must be signed in to change notification settings - Fork 81
/
detect.go
67 lines (57 loc) · 2.08 KB
/
detect.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
package bitbucketcloud
import (
"encoding/json"
"fmt"
"net/http"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketcloud/types"
"go.uber.org/zap"
)
func (v *Provider) Detect(req *http.Request, payload string, logger *zap.SugaredLogger) (bool, bool, *zap.SugaredLogger, string, error) {
isBitCloud := false
reqHeader := req.Header
event := reqHeader.Get("X-Event-Key")
if event == "" {
return false, false, logger, "", nil
}
eventInt, err := parsePayloadType(event, payload)
if err != nil || eventInt == nil {
return false, false, logger, "", err
}
// it is a Bitbucket cloud event
isBitCloud = true
setLoggerAndProceed := func(processEvent bool, reason string, err error) (bool, bool, *zap.SugaredLogger,
string, error,
) {
logger = logger.With("provider", "bitbucket-cloud", "event-id", reqHeader.Get("X-Request-Id"))
return isBitCloud, processEvent, logger, reason, err
}
_ = json.Unmarshal([]byte(payload), &eventInt)
switch e := eventInt.(type) {
case *types.PullRequestEvent:
if provider.Valid(event, []string{"pullrequest:created", "pullrequest:updated"}) {
return setLoggerAndProceed(true, "", nil)
}
if provider.Valid(event, []string{"pullrequest:comment_created"}) {
if provider.IsTestRetestComment(e.Comment.Content.Raw) {
return setLoggerAndProceed(true, "", nil)
}
if provider.IsOkToTestComment(e.Comment.Content.Raw) {
return setLoggerAndProceed(true, "", nil)
}
if provider.IsCancelComment(e.Comment.Content.Raw) {
return setLoggerAndProceed(true, "", nil)
}
}
return setLoggerAndProceed(false, fmt.Sprintf("not a valid gitops comment: \"%s\"", event), nil)
case *types.PushRequestEvent:
if provider.Valid(event, []string{"repo:push"}) {
if e.Push.Changes != nil {
return setLoggerAndProceed(true, "", nil)
}
}
return setLoggerAndProceed(false, fmt.Sprintf("invalid push event: \"%s\"", event), nil)
default:
return setLoggerAndProceed(false, "", fmt.Errorf("bitbucket-cloud: event \"%s\" is not supported", event))
}
}