/
source.go
70 lines (57 loc) · 1.39 KB
/
source.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
package jiracloud
import (
"encoding/json"
"fmt"
"github.com/ImpactInsights/valuestream/eventsources"
"github.com/opentracing/opentracing-go"
"github.com/urfave/cli"
"io/ioutil"
"net/http"
)
const (
sourceName string = "jiracloud"
)
type Source struct {
tracer opentracing.Tracer
}
func (s Source) Name() string {
return sourceName
}
func (s *Source) Tracer() opentracing.Tracer {
return s.tracer
}
func (s *Source) SecretKey() []byte {
return nil
}
func (s *Source) ValidatePayload(r *http.Request, secretKey []byte) ([]byte, error) {
if secretKey == nil {
return ioutil.ReadAll(r.Body)
}
return nil, fmt.Errorf("does not support signing right now")
}
func (s *Source) Event(r *http.Request, payload []byte) (eventsources.Event, error) {
var e Event
if err := json.Unmarshal(payload, &e); err != nil {
return nil, err
}
// get the specific event type from the wrapper even
switch e.Type() {
case sprintEvent:
var se SprintEvent
err := json.Unmarshal(payload, &se)
return se, err
case issueEvent:
var ie IssueEvent
err := json.Unmarshal(payload, &ie)
return ie, err
}
return nil, fmt.Errorf("event type: %q, not supported", e.WebhookEvent)
}
func NewSource(tracer opentracing.Tracer) (*Source, error) {
return &Source{
tracer: tracer,
}, nil
}
func NewFromCLI(c *cli.Context, tracer opentracing.Tracer) (eventsources.EventSource, error) {
return NewSource(tracer)
}