added initial go files

This commit is contained in:
A_D 2018-11-16 18:43:18 +02:00
parent 629cb1cbbc
commit 3138ae1fbe
No known key found for this signature in database
GPG Key ID: C242F3DD220FA945
2 changed files with 109 additions and 0 deletions

5
main.go Normal file
View File

@ -0,0 +1,5 @@
package main
func main() {
ListenForWebhook()
}

104
webhooks.go Normal file
View File

@ -0,0 +1,104 @@
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type BaseWebHook struct {
Secret string `json:"secret"`
}
type Push struct {
BaseWebHook
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
CompareUrl string `json:"compare_url"`
Commits []Commit `json:"commits"`
Repository Repository `json:"repository"`
Pusher RepoUser `json:"pusher"`
Sender RepoUser `json:"sender"`
}
type RepoUser struct {
ID float64 `json:"id"`
Login string `json:"login"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarUrl string `json:"avatar_url"`
Language string `json:"language"`
Username string `json:"username"`
}
type Repository struct {
ID float64 `json:"id"`
Owner RepoUser `json:"owner"`
Name string `json:"name"`
FullName string `json:"full_name"`
Description string `json:"description"`
Empty bool `json:"empty"`
Private bool `json:"private"`
Fork bool `json:"fork"`
Parent string `json:"parent"`
Mirror bool `json:"mirror"`
Size float64 `json:"size"`
HtmlUrl string `json:"html_url"`
SshUrl string `json:"ssh_url"`
CloneUrl string `json:"clone_url"`
Website string `json:"website"`
StarsCount float64 `json:"stars_count"`
ForksCount float64 `json:"forks_count"`
WatchersCount float64 `json:"watchers_count"`
OpenIssuesCount float64 `json:"open_issues_count"`
DefaultBranch string `json:"default_branch"`
Archived bool `json:"archived"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Permissions RepoPermissions `json:"permissions"`
}
type RepoPermissions struct {
Admin bool
Push bool
Pull bool
}
type Commit struct {
Hash string `json:"id"`
Message string `json:"message"`
Author Person `json:"author"`
Commiter Person `json:"commiter"`
Verification interface{} `json:"verification"`
Timestamp string `json:"timestamp"`
}
type Person struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
}
func ListenForWebhook() {
http.HandleFunc(
"/", func(writer http.ResponseWriter, request *http.Request) {
data, err := ioutil.ReadAll(request.Body)
if err != nil {
panic(err)
}
push := Push{}
json.Unmarshal(data, &push)
fmt.Println(data)
fmt.Println(string(data))
fmt.Println(request)
fmt.Printf("%+v\n", push)
request.Body.Close()
},
)
err := http.ListenAndServe("0.0.0.0:5000", nil)
if err != nil {
fmt.Println(err)
}
}