-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyncer.go
123 lines (97 loc) · 2.09 KB
/
syncer.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package ghsync
import (
"github.com/facebookgo/httpdown"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/levels"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"net/http"
"net/http/httputil"
"os"
)
const (
HookName = "ghsync_hook"
hookPath = "/webhook"
)
var httpConfig = httpdown.HTTP{}
type Config struct {
Token string
Organization string
ListenAddr string
PublicAddr string
}
func New(conf Config, data DataStore) *Syncer {
ts := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: conf.Token,
})
tc := oauth2.NewClient(oauth2.NoContext, ts)
return &Syncer{
conf: conf,
client: github.NewClient(tc),
data: data,
logger: levels.New(log.NewContext(log.NewLogfmtLogger(os.Stderr)).With("ts", log.DefaultTimestamp)),
}
}
// Syncer dog
type Syncer struct {
conf Config
client *github.Client
http httpdown.Server
data DataStore
logger levels.Levels
}
func (s *Syncer) Start() error {
if err := s.syncRepos(); err != nil {
return err
}
if err := s.syncMembers(); err != nil {
return err
}
if err := s.syncTeams(); err != nil {
return err
}
if err := s.syncIssues(); err != nil {
return err
}
if err := s.syncIssuesComments(); err != nil {
return err
}
if err := s.syncReviewComments(); err != nil {
return err
}
if err := s.syncCommitComments(); err != nil {
return err
}
srv := http.Server{
Addr: s.conf.ListenAddr,
Handler: http.HandlerFunc(s.handleHook),
}
hsrv, err := httpConfig.ListenAndServe(&srv)
if err != nil {
return err
}
s.logger.Info().Log("msg", "listening", "addr", s.conf.ListenAddr)
s.http = hsrv
return nil
}
func (s *Syncer) Wait() error {
return s.http.Wait()
}
func (s *Syncer) Stop() error {
return s.http.Stop()
}
func (s *Syncer) handleHook(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != hookPath {
http.NotFound(w, r)
return
}
var (
ev = r.Header.Get("X-Github-Event")
// dest interface{}
)
switch ev {
default:
b, _ := httputil.DumpRequest(r, true)
s.logger.Warn().Log("msg", "Unhandle event", "event", ev, "request", string(b))
}
}