mirror of
https://git.ferricyanide.solutions/A_D/irc-webhooks.git
synced 2024-12-22 18:37:06 +00:00
147 lines
4.2 KiB
Go
147 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
const INDENT = " "
|
|
|
|
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"`
|
|
Committer Person `json:"committer"`
|
|
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(config Config) {
|
|
http.HandleFunc("/", handleWebHook)
|
|
|
|
err := http.ListenAndServe(config.BindHost+":"+config.BindPort, nil)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
}
|