first commit
This commit is contained in:
commit
ad18bb8ad3
9 changed files with 309 additions and 0 deletions
137
main.go
Normal file
137
main.go
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
"time"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
var version string
|
||||
|
||||
type PR struct {
|
||||
Action string `json:"action"`
|
||||
}
|
||||
|
||||
func prHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var pr PR
|
||||
defer r.Body.Close()
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
log.Printf("debug headers: %#v", r.Header)
|
||||
log.Printf("debug body: %s", string(body))
|
||||
err = json.Unmarshal(body, &pr)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
switch pr.Action {
|
||||
case "opened", "reopened", "synchronize":
|
||||
if r.Header.Get("X-Interseptor-Action") == "create" {
|
||||
w.Write(body)
|
||||
return
|
||||
}
|
||||
case "closed":
|
||||
if r.Header.Get("X-Interseptor-Action") == "delete" {
|
||||
w.Write(body)
|
||||
return
|
||||
}
|
||||
}
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
type Push struct {
|
||||
Ref string `json:"ref"`
|
||||
}
|
||||
|
||||
func pushHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var p Push
|
||||
defer r.Body.Close()
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
log.Printf("debug headers: %#v", r.Header)
|
||||
log.Printf("debug: %s", string(body))
|
||||
err = json.Unmarshal(body, &p)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if r.Header.Get("X-Interseptor-Ref") == p.Ref {
|
||||
w.Write(body)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
type CommentBody struct {
|
||||
Comment Comment `json:"comment"`
|
||||
}
|
||||
|
||||
type Comment struct {
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
func commentHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var c CommentBody
|
||||
defer r.Body.Close()
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
log.Printf("debug headers: %#v", r.Header)
|
||||
log.Printf("debug: %s", string(body))
|
||||
err = json.Unmarshal(body, &c)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if r.Header.Get("X-Interseptor-Comment") == c.Comment.Body {
|
||||
w.Write(body)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func dumpHandler(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
log.Printf("debug headers: %#v", r.Header)
|
||||
log.Printf("debug: %s", string(body))
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
event := r.Header.Get("X-GitHub-Event")
|
||||
log.Printf("%s %s %s: %s", r.RemoteAddr, r.Method, r.URL, event)
|
||||
switch event {
|
||||
case "pull_request":
|
||||
prHandler(w, r)
|
||||
case "push":
|
||||
pushHandler(w, r)
|
||||
//case "issue_comment":
|
||||
// commentHandler(w, r)
|
||||
default:
|
||||
dumpHandler(w, r)
|
||||
}
|
||||
})
|
||||
s := &http.Server{
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
Addr: ":8080",
|
||||
}
|
||||
log.Fatal(s.ListenAndServe())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue