/
server.go
164 lines (148 loc) · 4.39 KB
/
server.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package handlers
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/golang/glog"
"github.com/google/go-github/github"
"github.com/spf13/pflag"
"golang.org/x/oauth2"
"github.com/huawei-cloudnative/ci-bot/handlers/repository"
)
//Syncronization Flag for IssueComment and PR event Handling
//A Comment in PR section will lead Github to throw 2 webhook events which are
//1.The Webhook event name is "issue_comment"
//2.The Webhook event name is "pull_request" sequentially.
//let the handling only be with issue_comment event not pull_request event
var IsIssueCommentHandling = false
//Github client
var ClientRepo *github.Client
var c = Config{}
// Server implements http.Handler. It validates incoming GitHub webhooks and
// then dispatches them to the handlers accordingly.
type Server struct {
Config Config
GithubClient *github.Client
Repository repository.Interface
Context context.Context
}
//config structure
type Config struct {
Repo string `json:"repo"`
GitHubToken string `json:"git_hub_token"`
WebhookSecret string `json:"webhook_secret"`
TravisCIToken string `json:"travis_ci_token"`
TravisRepoName string `json:"travis_ci_repoaccount"`
}
//webhook server
type WebHookServer struct {
Address string
Port int64
ConfigFile string
}
//webhook handler
func NewWebHookServer() *WebHookServer {
s := WebHookServer{
Address: "0.0.0.0",
Port: 3000,
}
return &s
}
//function to add flags
func AddFlags(fs *pflag.FlagSet, s *WebHookServer) {
fs.StringVar(&s.Address, "address", s.Address, "IP address to serve, 0.0.0.0 by default")
fs.Int64Var(&s.Port, "port", s.Port, "Port to listen on, 3000 by default")
fs.StringVar(&c.Repo, "repo", c.Repo, "Refers to the project repo address")
fs.StringVar(&c.GitHubToken, "github-token", c.GitHubToken, "Contains the githubtoken info")
fs.StringVar(&c.WebhookSecret, "webhook-secret", c.WebhookSecret, "Contains the webhooksecret key")
fs.StringVar(&c.TravisCIToken, "travis-ci-token", c.TravisCIToken, "Contains Travis-CI access token to trigger the PR build")
fs.StringVar(&c.TravisRepoName, "repoName", c.TravisRepoName, "Contains repo name of CI build Ex: kubeedge/kubeedge")
fs.Parse(os.Args[1:])
}
// ServeHTTP validates an incoming webhook and invoke its handler.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
payload, err := github.ValidatePayload(r, []byte(c.WebhookSecret))
if err != nil {
glog.Errorf("Invalid payload: %v", err)
return
}
event, err := github.ParseWebHook(github.WebHookType(r), payload)
if err != nil {
glog.Errorf("Failed to parse webhook")
return
}
fmt.Fprint(w, "Received a webhook event")
var client http.Client
client.Do(r)
switch event.(type) {
case *github.IssueEvent:
go s.handleIssueEvent(payload)
case *github.IssueCommentEvent:
// Comments on PRs belong to IssueCommentEvent
IsIssueCommentHandling = true
go s.handleIssueCommentEvent(payload, s.GithubClient, s.Repository)
case *github.PullRequestEvent:
if !IsIssueCommentHandling {
go s.handlePullRequestEvent(payload, ClientRepo)
}
//Fall Back to original state
IsIssueCommentHandling = false
case *github.PullRequestComment:
go s.handlePullRequestCommentEvent(payload)
}
}
//function to run
func Run(s *WebHookServer) {
oauthSecret := c.GitHubToken
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: string(oauthSecret)},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
ClientRepo = client
// new repository instance
repository, err := repository.NewRepository(client, c.Repo)
if err != nil {
log.Println(err)
}
// init repository
err = repository.Init()
if err != nil {
log.Println(err)
}
// catch exit signal
sigs := make(chan os.Signal, 1)
signal.Notify(sigs)
go func() {
for sig := range sigs {
if sig == syscall.SIGKILL ||
sig == syscall.SIGQUIT ||
sig == syscall.SIGHUP ||
sig == syscall.SIGTERM ||
sig == syscall.SIGINT {
// clear repository
repository.Clear()
os.Exit(0)
}
}
}()
webHookHandler := Server{
Config: c,
GithubClient: ClientRepo,
Repository: repository,
Context: ctx,
}
//setting handler
http.HandleFunc("/hook", webHookHandler.ServeHTTP)
address := s.Address + ":" + strconv.FormatInt(s.Port, 10)
//starting server
if err := http.ListenAndServe(address, nil); err != nil {
log.Println(err)
}
}