irc-webhooks/webhooks.go

147 lines
4.2 KiB
Go
Raw Normal View History

2018-11-16 16:43:18 +00:00
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
2018-11-16 20:34:13 +00:00
"strings"
2018-11-16 16:43:18 +00:00
)
2018-11-16 20:34:13 +00:00
const INDENT = " "
2018-11-16 16:43:18 +00:00
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"`
2018-11-16 20:34:13 +00:00
Committer Person `json:"committer"`
2018-11-16 16:43:18 +00:00
Verification interface{} `json:"verification"`
Timestamp string `json:"timestamp"`
}
type Person struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
}
2018-11-16 19:31:45 +00:00
func ListenForWebHook(config Config) {
2018-11-16 20:34:13 +00:00
http.HandleFunc("/", handleWebHook)
2018-11-16 19:31:45 +00:00
2018-11-16 20:34:13 +00:00
err := http.ListenAndServe(config.BindHost+":"+config.BindPort, nil)
2018-11-16 19:31:45 +00:00
2018-11-16 16:43:18 +00:00
if err != nil {
fmt.Println(err)
}
}
2018-11-16 20:34:13 +00:00
func handleWebHook(writer http.ResponseWriter, request *http.Request) {
defer request.Body.Close()
data, err := ioutil.ReadAll(request.Body)
if err != nil {
panic(err)
}
switch eventType := request.Header.Get("X-Gitea-Event"); eventType {
case "push":
handlePush(data)
case "":
// Not a gitea event.
fmt.Printf("Got a non-event HTTP request %+v\n", request)
return
default:
fmt.Printf("Unknown event type: %q\n", eventType)
return
}
}
func handlePush(data []byte) {
pushData := Push{}
json.Unmarshal(data, &pushData)
fmt.Println("---")
fmt.Printf(
"Got a push for repo %q owned by %q. Pushed by %q (%q)\n",
pushData.Repository.FullName,
pushData.Repository.Owner.Login,
pushData.Pusher.Login,
pushData.Pusher.Email,
)
for _, commit := range pushData.Commits {
fmt.Println(INDENT + "--")
fmt.Printf(
INDENT+"Commit added by %q (%q), made by %q (%q), with hash %q and message:\n",
commit.Committer.Name,
commit.Committer.Email,
commit.Author.Name,
commit.Author.Email,
commit.Hash,
)
msg := strings.Replace(commit.Message, "\n", strings.Repeat(INDENT, 2)+"\n", -1)
fmt.Println(strings.Repeat(INDENT, 2) + msg)
}
}