1
0
mirror of https://github.com/pinpox/gitea-matrix-bot synced 2024-11-22 19:31:58 +01:00
gitea-matrix-bot/listener.go

171 lines
6.9 KiB
Go
Raw Normal View History

2019-04-13 12:31:28 +02:00
package main
import (
2019-04-13 15:25:50 +02:00
"bytes"
2019-04-13 12:31:28 +02:00
"encoding/json"
log "github.com/sirupsen/logrus"
2019-04-13 12:31:28 +02:00
"io/ioutil"
"net/http"
2019-04-13 15:25:50 +02:00
"strconv"
2019-04-19 21:35:01 +02:00
"strings"
2019-04-13 19:19:24 +02:00
2019-04-19 21:35:01 +02:00
"text/template"
// "github.com/davecgh/go-spew/spew"
2019-04-13 15:25:50 +02:00
// "os"
2019-04-13 12:31:28 +02:00
)
func setupListener() {
httpURI := cfg.Section("http").Key("http_uri").String()
httpPort := cfg.Section("http").Key("http_port").String()
mux := http.NewServeMux()
mux.HandleFunc(httpURI, PostHandler)
log.Debugf("listening on port %s", httpPort)
2019-04-13 12:31:28 +02:00
log.Fatal(http.ListenAndServe(":"+httpPort, mux))
}
// PostHandler converts post request body to string
func PostHandler(w http.ResponseWriter, r *http.Request) {
2019-04-19 21:35:01 +02:00
2019-04-13 12:31:28 +02:00
if r.Method == "POST" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body",
http.StatusInternalServerError)
}
var postData GiteaPostData
json.Unmarshal(body, &postData)
message := generateMessage(postData, r.Header.Get("X-Gitea-Event"))
2019-04-19 21:35:01 +02:00
args := strings.Split(r.URL.String(), "/")
room := args[len(args)-1]
log.Debugf("Posting to room: %s", room)
2019-04-19 21:35:01 +02:00
if mygiteabot.checkToken(room, postData.Secret) {
mygiteabot.SendToRoom(room, message)
} else {
log.Warningf("Wrong token %s for room: %s", postData.Secret, room)
2019-04-19 21:35:01 +02:00
}
2019-04-13 12:31:28 +02:00
} else {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
}
}
2019-04-13 15:25:50 +02:00
//generateMessage generates the message string for a given event
func generateMessage(data GiteaPostData, eventHeader string) string {
templ := template.New("notification")
var tpl bytes.Buffer
switch eventHeader {
case "push":
templ.Parse("[{{.Repository.Fullname}}] {{.Pusher.FullName}} pushed " + strconv.Itoa(len(data.Commits)) + " commit(s) to {{.Repository.Name}}")
2019-04-13 15:25:50 +02:00
case "issues":
switch data.Action {
case "assigned":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} assigned issue #{{.Issue.Number}} {{.Issue.Title}} to {{}} ")
2019-04-13 15:25:50 +02:00
case "closed":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} closed issue #{{.Issue.Number}} {{.Issue.Title}}")
2019-04-13 15:25:50 +02:00
case "demilestoned":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed milestone TODO from issue #{{.Issue.Number}} {{.Issue.Title}}")
2019-04-13 15:25:50 +02:00
case "edited":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} edited issue #{{.Issue.Number}} {{.Issue.Title}}")
2019-04-13 15:25:50 +02:00
case "label_cleared":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} cleared labels from issue #{{.Issue.Number}} {{.Issue.Title}}")
2019-04-13 15:25:50 +02:00
case "label_updated":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} updated labels of issue #{{.Issue.Number}} {{.Issue.Title}}")
2019-04-13 15:25:50 +02:00
case "milestoned":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} added issue #{{.Issue.Number}} {{.Issue.Title}} to milestone TODO")
2019-04-13 15:25:50 +02:00
case "opened":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Issue.User.FullName}} opened issue #{{.Issue.Number}} {{.Issue.Title}}")
2019-04-13 15:25:50 +02:00
case "reopened":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} re-opened issue #{{.Issue.Number}} {{.Issue.Title}}")
2019-04-13 15:25:50 +02:00
case "synchronized":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} synchronized issue #{{.Issue.Number}} {{.Issue.Title}}")
2019-04-13 15:25:50 +02:00
case "unassigned":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed assignee from issue #{{.Issue.Number}} {{.Issue.Title}}")
2019-04-13 15:25:50 +02:00
}
2019-04-13 12:31:28 +02:00
2019-04-13 15:25:50 +02:00
case "fork":
templ.Parse("[{{.Repository.FullName}}] {{.Sender.FullName}} forked repository {{.Repository.Parent.FullName}} to {{.Repository.FullName}}")
2019-04-13 12:31:28 +02:00
2019-04-13 15:25:50 +02:00
case "pull_request":
switch data.Action {
case "assigned":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} assigned pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\" to {{.PullRequest.Assignee.FullName}}")
2019-04-13 15:25:50 +02:00
case "closed":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} closed pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
2019-04-13 15:25:50 +02:00
case "demilestoned":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed milestone TODO from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
2019-04-13 15:25:50 +02:00
case "edited":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} edited pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
2019-04-13 15:25:50 +02:00
case "label_cleared":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed labels from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
2019-04-13 15:25:50 +02:00
case "label_updated":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} updated labels from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
2019-04-13 15:25:50 +02:00
case "milestoned":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} added pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\" to milestone TODO")
2019-04-13 15:25:50 +02:00
case "opened":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} opened pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
2019-04-13 15:25:50 +02:00
case "reopened":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} re-opened pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
2019-04-13 15:25:50 +02:00
case "synchronized":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} synchronized pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
2019-04-13 15:25:50 +02:00
case "unassigned":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed assinee from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
2019-04-13 15:25:50 +02:00
}
2019-04-13 12:31:28 +02:00
2019-04-13 15:25:50 +02:00
case "issue_comment":
2019-04-13 19:19:24 +02:00
switch data.Action {
case "created":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Sender.FullName}} commented on #{{.Issue.Number}} {{.Issue.Title}}: {{.Comment.Body}}")
2019-04-13 19:19:24 +02:00
case "deleted":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Sender.FullName}} deleted commented on #{{.Issue.Number}} {{.Issue.Title}}")
2019-04-13 19:19:24 +02:00
}
2019-04-13 15:25:50 +02:00
case "repository":
switch data.Action {
case "created":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Sender.FullName}} created repository {{}}")
2019-04-13 15:25:50 +02:00
case "deleted":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Sender.FullName}} deleted repository {{}}")
2019-04-13 15:25:50 +02:00
}
case "release":
switch data.Action {
case "published":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} published release {{}}")
2019-04-13 15:25:50 +02:00
case "updated":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} updated release {{}}")
2019-04-13 15:25:50 +02:00
case "deleted":
2019-04-13 19:19:24 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} deleted release {{}}")
2019-04-13 15:25:50 +02:00
}
case "pull_request_approved":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} approved pull request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
2019-04-13 15:25:50 +02:00
case "pull_request_rejected":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} rejected pull request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
2019-04-13 15:25:50 +02:00
case "pull_request_comment":
2019-04-13 20:21:58 +02:00
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} commented on pull request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\": {{.Comment.Body}}")
2019-04-13 19:19:24 +02:00
default:
log.Warningf("Unknown action: %s for eventHeader %s", data.Action, eventHeader)
2019-04-13 19:19:24 +02:00
templ.Parse("Gitea did something unexpected, seriously wtf was that?! Event: " + eventHeader + " Action: " + data.Action)
2019-04-13 15:25:50 +02:00
}
if err := templ.Execute(&tpl, data); err != nil {
log.Fatal(err)
2019-04-13 12:31:28 +02:00
}
2019-04-13 15:25:50 +02:00
return tpl.String()
2019-04-13 12:31:28 +02:00
}