mirror of
https://github.com/pinpox/gitea-matrix-bot
synced 2024-11-26 08:43:47 +01:00
implement html messages
This commit is contained in:
parent
4ed15a856b
commit
f716061456
@ -18,4 +18,6 @@ matrix_user = "my-awesome-bot"
|
|||||||
[bot]
|
[bot]
|
||||||
# Path of the database to be used
|
# Path of the database to be used
|
||||||
db_path = "./tokens.db"
|
db_path = "./tokens.db"
|
||||||
|
# message type: 'html' or 'plain'
|
||||||
|
message_type = "html"
|
||||||
|
|
||||||
|
16
giteabot.go
16
giteabot.go
@ -55,7 +55,7 @@ func (gb *GiteaBot) handleCommandSecret(message, room, sender string) {
|
|||||||
//Check if a parameter was supplied (room for which to generate the token)
|
//Check if a parameter was supplied (room for which to generate the token)
|
||||||
msgParts := strings.Split(message, " ")
|
msgParts := strings.Split(message, " ")
|
||||||
if len(msgParts) != 3 {
|
if len(msgParts) != 3 {
|
||||||
gb.SendToRoom(room, "!gitea secert expects exactly on parameter, a room for which to request a token.\n Usage: !gitea secret <room id> \n\n e.g. !gitea secert !FoJFjcBoIJyKuPnDFf:matrix.org")
|
gb.SendTextToRoom(room, "!gitea secert expects exactly on parameter, a room for which to request a token.\n Usage: !gitea secret <room id> \n\n e.g. !gitea secert !FoJFjcBoIJyKuPnDFf:matrix.org")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,14 +63,14 @@ func (gb *GiteaBot) handleCommandSecret(message, room, sender string) {
|
|||||||
|
|
||||||
//Basic check if the roomID is properly formatted. The user might mistake it for the alias or address
|
//Basic check if the roomID is properly formatted. The user might mistake it for the alias or address
|
||||||
if !strings.HasPrefix(reqRoom, "!") {
|
if !strings.HasPrefix(reqRoom, "!") {
|
||||||
gb.SendToRoom(room, "Room IDs start with an exclamation mark\n\n e.g. !gitea secert !FoJFjcBoIJyKuPnDFf:matrix.org \n\n This is *not* the same as the rooms name or alias!")
|
gb.SendTextToRoom(room, "Room IDs start with an exclamation mark\n\n e.g. !gitea secert !FoJFjcBoIJyKuPnDFf:matrix.org \n\n This is *not* the same as the rooms name or alias!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check if room already has a token, if so display it.
|
//Check if room already has a token, if so display it.
|
||||||
if gb.Tokens[reqRoom] != "" {
|
if gb.Tokens[reqRoom] != "" {
|
||||||
gb.SendToRoom(room, "This room already has a token. Your secert token is:")
|
gb.SendTextToRoom(room, "This room already has a token. Your secert token is:")
|
||||||
gb.SendToRoom(room, gb.Tokens[reqRoom])
|
gb.SendTextToRoom(room, gb.Tokens[reqRoom])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,12 +80,12 @@ func (gb *GiteaBot) handleCommandSecret(message, room, sender string) {
|
|||||||
gb.db.Set(reqRoom, token)
|
gb.db.Set(reqRoom, token)
|
||||||
|
|
||||||
//Print the token and help to the room it was requested from
|
//Print the token and help to the room it was requested from
|
||||||
gb.SendToRoom(room, "Your secert token is:")
|
gb.SendTextToRoom(room, "Your secert token is:")
|
||||||
gb.SendToRoom(room, token)
|
gb.SendTextToRoom(room, token)
|
||||||
gb.SendToRoom(room, "Now, set up a weebhook in gitea with that token as secret")
|
gb.SendTextToRoom(room, "Now, set up a weebhook in gitea with that token as secret")
|
||||||
|
|
||||||
httpHost := cfg.Section("http").Key("http_host").String()
|
httpHost := cfg.Section("http").Key("http_host").String()
|
||||||
httpPort := cfg.Section("http").Key("http_port").String()
|
httpPort := cfg.Section("http").Key("http_port").String()
|
||||||
httpURI := cfg.Section("http").Key("http_uri").String()
|
httpURI := cfg.Section("http").Key("http_uri").String()
|
||||||
gb.SendToRoom(room, httpHost+":"+httpPort+httpURI+room)
|
gb.SendTextToRoom(room, httpHost+":"+httpPort+httpURI+room)
|
||||||
}
|
}
|
||||||
|
13
giteadb.go
13
giteadb.go
@ -29,6 +29,8 @@ func NewGiteaDB(path string) *GiteaDB {
|
|||||||
|
|
||||||
//Init initializes the db, if it exists in the path it will be overwritten
|
//Init initializes the db, if it exists in the path it will be overwritten
|
||||||
func (dbg *GiteaDB) Init() {
|
func (dbg *GiteaDB) Init() {
|
||||||
|
|
||||||
|
log.Debugf("Initializing DB in %s", dbg.path)
|
||||||
os.Remove(dbg.path)
|
os.Remove(dbg.path)
|
||||||
|
|
||||||
dbtmp, err := sql.Open("sqlite3", dbg.path)
|
dbtmp, err := sql.Open("sqlite3", dbg.path)
|
||||||
@ -51,6 +53,8 @@ func (dbg *GiteaDB) Init() {
|
|||||||
//GetToken returns the token for a room, if found
|
//GetToken returns the token for a room, if found
|
||||||
func (dbg *GiteaDB) GetToken(room string) string {
|
func (dbg *GiteaDB) GetToken(room string) string {
|
||||||
|
|
||||||
|
log.Debugf("Getting token for room %s", room)
|
||||||
|
|
||||||
db, err := sql.Open("sqlite3", dbg.path)
|
db, err := sql.Open("sqlite3", dbg.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -66,8 +70,11 @@ func (dbg *GiteaDB) GetToken(room string) string {
|
|||||||
var token string
|
var token string
|
||||||
err = stmt.QueryRow(room).Scan(&token)
|
err = stmt.QueryRow(room).Scan(&token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Debug("No token found!")
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Debugf("Found token: %s", token)
|
||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,6 +82,8 @@ func (dbg *GiteaDB) GetToken(room string) string {
|
|||||||
func (dbg *GiteaDB) GetAll() map[string]string {
|
func (dbg *GiteaDB) GetAll() map[string]string {
|
||||||
tokens := make(map[string]string)
|
tokens := make(map[string]string)
|
||||||
|
|
||||||
|
log.Debugf("Retrieving all tokens from %s", dbg.path)
|
||||||
|
|
||||||
db, err := sql.Open("sqlite3", dbg.path)
|
db, err := sql.Open("sqlite3", dbg.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -100,6 +109,8 @@ func (dbg *GiteaDB) GetAll() map[string]string {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Debugf("Found %v tokens", len(tokens))
|
||||||
|
|
||||||
return tokens
|
return tokens
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -96,53 +96,3 @@ func TestGiteaDB_GetAll(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGiteaDB_Unset(t *testing.T) {
|
|
||||||
type fields struct {
|
|
||||||
path string
|
|
||||||
}
|
|
||||||
type args struct {
|
|
||||||
room string
|
|
||||||
token string
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
fields fields
|
|
||||||
args args
|
|
||||||
}{
|
|
||||||
// TODO: Add test cases.
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
dbg := &GiteaDB{
|
|
||||||
path: tt.fields.path,
|
|
||||||
}
|
|
||||||
dbg.Unset(tt.args.room, tt.args.token)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGiteaDB_Set(t *testing.T) {
|
|
||||||
type fields struct {
|
|
||||||
path string
|
|
||||||
}
|
|
||||||
type args struct {
|
|
||||||
room string
|
|
||||||
token string
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
fields fields
|
|
||||||
args args
|
|
||||||
}{
|
|
||||||
// TODO: Add test cases.
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
dbg := &GiteaDB{
|
|
||||||
path: tt.fields.path,
|
|
||||||
}
|
|
||||||
dbg.Set(tt.args.room, tt.args.token)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
250
listener.go
250
listener.go
@ -3,12 +3,13 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"text/template"
|
"text/template"
|
||||||
// "github.com/davecgh/go-spew/spew"
|
// "github.com/davecgh/go-spew/spew"
|
||||||
// "os"
|
// "os"
|
||||||
@ -37,18 +38,31 @@ func PostHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.StatusInternalServerError)
|
http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unmarshall data and get room to post to
|
||||||
var postData GiteaPostData
|
var postData GiteaPostData
|
||||||
|
|
||||||
json.Unmarshal(body, &postData)
|
json.Unmarshal(body, &postData)
|
||||||
|
|
||||||
message := generateMessage(postData, r.Header.Get("X-Gitea-Event"))
|
|
||||||
|
|
||||||
args := strings.Split(r.URL.String(), "/")
|
args := strings.Split(r.URL.String(), "/")
|
||||||
room := args[len(args)-1]
|
room := args[len(args)-1]
|
||||||
|
|
||||||
|
// Check token
|
||||||
|
if mygiteabot.checkToken(room, postData.Secret) {
|
||||||
log.Debugf("Posting to room: %s", room)
|
log.Debugf("Posting to room: %s", room)
|
||||||
|
|
||||||
if mygiteabot.checkToken(room, postData.Secret) {
|
msgType := cfg.Section("bot").Key("message_type").String()
|
||||||
mygiteabot.SendToRoom(room, message)
|
|
||||||
|
//TODO check for configured message type
|
||||||
|
|
||||||
|
messageText := generateTextMessage(postData, r.Header.Get("X-Gitea-Event"))
|
||||||
|
switch msgType {
|
||||||
|
case "html":
|
||||||
|
messageHTML := generateHTMLMessage(postData, r.Header.Get("X-Gitea-Event"))
|
||||||
|
mygiteabot.SendHTMLToRoom(room, messageHTML, messageText)
|
||||||
|
case "plain":
|
||||||
|
mygiteabot.SendTextToRoom(room, messageText)
|
||||||
|
default:
|
||||||
|
log.Fatalf("Wrong message type %s. Supported 'html' and 'plain'", msgType)
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.Warningf("Wrong token %s for room: %s", postData.Secret, room)
|
log.Warningf("Wrong token %s for room: %s", postData.Secret, room)
|
||||||
}
|
}
|
||||||
@ -58,105 +72,159 @@ func PostHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//generateMessage generates the message string for a given event
|
//generateTextMessage generates the message string for a given event
|
||||||
func generateMessage(data GiteaPostData, eventHeader string) string {
|
func generateHTMLMessage(data GiteaPostData, eventHeader string) string {
|
||||||
|
|
||||||
|
templHeader := `<h3><a href="{{.Repository.HTMLURL}}"><b><span data-mx-color="#000000">[{{.Repository.FullName}}]</span></b></a></h3>`
|
||||||
|
|
||||||
|
mxc, err := mygiteabot.Client.UploadLink(data.Sender.AvatarURL)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Cound not upload user avatar")
|
||||||
|
log.Error(err)
|
||||||
|
templHeader = templHeader + `<img src='mxc://matrix.org/GZtGmjBmfljRasPdjvWdKKVe' alt='[gitea]' width='32' height='32' title='gitea'/> <span data-mx-color="#609926">{{.Sender.FullName}}</span> `
|
||||||
|
} else {
|
||||||
|
templHeader = templHeader + "<img src='" + mxc.ContentURI + `' alt='[gitea]' width='30' height='30' title='image title'/> <span data-mx-color="#609926">{{.Sender.FullName}}</span> `
|
||||||
|
}
|
||||||
|
|
||||||
templ := template.New("notification")
|
templ := template.New("notification")
|
||||||
var tpl bytes.Buffer
|
var tpl bytes.Buffer
|
||||||
|
|
||||||
|
mesgTemplates := map[string]string{
|
||||||
|
|
||||||
|
"push": "pushed " + strconv.Itoa(len(data.Commits)) + " commit(s) to {{.Repository.Name}}",
|
||||||
|
"issues.assigned": "assigned issue <b>#{{Issue.Number}}</b> <i>{{.Issue.Title}}</i> to {{}}",
|
||||||
|
"issues.closed": "closed issue <b>#{{Issue.Number}}</b> <i>{{.Issue.Title}}</i>",
|
||||||
|
"issues.demilestoned": "removed milestone TODO from issue <b>#{{Issue.Number}}</b> <i>{{.Issue.Title}}</i>",
|
||||||
|
"issues.edited": "edited issue <b>#{{Issue.Number}}</b> <i>{{.Issue.Title}}</i>",
|
||||||
|
"issues.label_cleared": "cleared labels from issue <b>#{{Issue.Number}}</b> <i>{{.Issue.Title}}</i>",
|
||||||
|
"issues.label_updated": "updated labels of issue <b>#{{Issue.Number}}</b> <i>{{.Issue.Title}}</i>",
|
||||||
|
"issues.milestoned": "added issue <b>#{{Issue.Number}}</b> <i>{{.Issue.Title}}</i> to milestone TODO",
|
||||||
|
"issues.opened": "opened issue <b>#{{Issue.Number}}</b> <i>{{.Issue.Title}}</i>",
|
||||||
|
"issues.reopened": "re-opened issue <b>#{{Issue.Number}}</b> <i>{{.Issue.Title}}</i>",
|
||||||
|
"issues.synchronized": "synchronized issue <b>#{{Issue.Number}}</b> <i>{{.Issue.Title}}</i>",
|
||||||
|
"issues.unassigned": "removed assignee from issue <b>#{{Issue.Number}}</b> <i>{{.Issue.Title}}</i>",
|
||||||
|
"fork": "forked repository {{.Repository.Parent.FullName}} to {{.Repository.FullName}}",
|
||||||
|
"pull_request.assigned": "assigned pull-request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i> to {{.PullRequest.Assignee.FullName}}",
|
||||||
|
"pull_request.closed": "closed pull-request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i>",
|
||||||
|
"pull_request.demilestoned": "removed milestone TODO from pull-request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i>",
|
||||||
|
"pull_request.edited": "edited pull-request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i>",
|
||||||
|
"pull_request.label_cleared": "removed labels from pull-request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i>",
|
||||||
|
"pull_request.label_updated": "updated labels from pull-request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i>",
|
||||||
|
"pull_request.milestoned": "added pull-request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i> to milestone TODO",
|
||||||
|
"pull_request.opened": "opened pull-request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i>",
|
||||||
|
"pull_request.reopened": "re-opened pull-request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i>",
|
||||||
|
"pull_request.synchronized": "synchronized pull-request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i>",
|
||||||
|
"pull_request.unassigned": "removed assinee from pull-request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i>",
|
||||||
|
"issue_comment.created": "commented on issue <b>#{{.Issue.Number}}</b> <i>{{.Issue.Title}}</i>:<br> <pre><code class='language-markdown'>{{.Comment.Body}}</code></pre>",
|
||||||
|
"issue_comment.deleted": "deleted commented on <b>#{{Issue.Number}}</b> <i>{{.Issue.Title}}</i>",
|
||||||
|
"repository.created": "created repository {{}}",
|
||||||
|
"repository.deleted": "deleted repository {{}}",
|
||||||
|
"release.published": "published release {{}}",
|
||||||
|
"release.updated": "updated release {{}}",
|
||||||
|
"release.deleted": "deleted release {{}}",
|
||||||
|
"pull_request_aproved": "approved pull request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i>",
|
||||||
|
"pull_request_rejected": "rejected pull request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i>",
|
||||||
|
"pull_request_comment": "commented on pull request <b>#{{PullRequest.Number}}</b> <i>#{{PullRequest.Title}}</i>: <pre><code class='language-markdown'>{{.Comment.Body}}</code><pre>",
|
||||||
|
}
|
||||||
|
|
||||||
switch eventHeader {
|
switch eventHeader {
|
||||||
|
|
||||||
case "push":
|
case "push":
|
||||||
templ.Parse("[{{.Repository.Fullname}}] {{.Pusher.FullName}} pushed " + strconv.Itoa(len(data.Commits)) + " commit(s) to {{.Repository.Name}}")
|
templ.Parse(templHeader + mesgTemplates["push"])
|
||||||
|
|
||||||
case "issues":
|
case "issues":
|
||||||
switch data.Action {
|
templ.Parse(templHeader + mesgTemplates["issues."+data.Action])
|
||||||
case "assigned":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} assigned issue #{{.Issue.Number}} {{.Issue.Title}} to {{}} ")
|
|
||||||
case "closed":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} closed issue #{{.Issue.Number}} {{.Issue.Title}}")
|
|
||||||
case "demilestoned":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed milestone TODO from issue #{{.Issue.Number}} {{.Issue.Title}}")
|
|
||||||
case "edited":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} edited issue #{{.Issue.Number}} {{.Issue.Title}}")
|
|
||||||
case "label_cleared":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} cleared labels from issue #{{.Issue.Number}} {{.Issue.Title}}")
|
|
||||||
case "label_updated":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} updated labels of issue #{{.Issue.Number}} {{.Issue.Title}}")
|
|
||||||
case "milestoned":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} added issue #{{.Issue.Number}} {{.Issue.Title}} to milestone TODO")
|
|
||||||
case "opened":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Issue.User.FullName}} opened issue #{{.Issue.Number}} {{.Issue.Title}}")
|
|
||||||
case "reopened":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} re-opened issue #{{.Issue.Number}} {{.Issue.Title}}")
|
|
||||||
case "synchronized":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} synchronized issue #{{.Issue.Number}} {{.Issue.Title}}")
|
|
||||||
case "unassigned":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed assignee from issue #{{.Issue.Number}} {{.Issue.Title}}")
|
|
||||||
}
|
|
||||||
|
|
||||||
case "fork":
|
case "fork":
|
||||||
templ.Parse("[{{.Repository.FullName}}] {{.Sender.FullName}} forked repository {{.Repository.Parent.FullName}} to {{.Repository.FullName}}")
|
templ.Parse(templHeader + mesgTemplates["fork"])
|
||||||
|
|
||||||
case "pull_request":
|
case "pull_request":
|
||||||
switch data.Action {
|
templ.Parse(templHeader + mesgTemplates["pull_request."+data.Action])
|
||||||
case "assigned":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} assigned pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\" to {{.PullRequest.Assignee.FullName}}")
|
|
||||||
case "closed":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} closed pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
|
|
||||||
case "demilestoned":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed milestone TODO from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
|
|
||||||
case "edited":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} edited pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
|
|
||||||
case "label_cleared":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed labels from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
|
|
||||||
case "label_updated":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} updated labels from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
|
|
||||||
case "milestoned":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} added pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\" to milestone TODO")
|
|
||||||
case "opened":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} opened pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
|
|
||||||
case "reopened":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} re-opened pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
|
|
||||||
case "synchronized":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} synchronized pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
|
|
||||||
case "unassigned":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed assinee from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
|
|
||||||
}
|
|
||||||
|
|
||||||
case "issue_comment":
|
case "issue_comment":
|
||||||
switch data.Action {
|
templ.Parse(templHeader + mesgTemplates["issue_comment."+data.Action])
|
||||||
case "created":
|
|
||||||
templ.Parse("{{.Sender.FullName}} commented on #{{.Issue.Number}} {{.Issue.Title}}: {{.Comment.Body}}")
|
|
||||||
case "deleted":
|
|
||||||
templ.Parse("{{.Sender.FullName}} deleted commented on #{{.Issue.Number}} {{.Issue.Title}}")
|
|
||||||
}
|
|
||||||
|
|
||||||
case "repository":
|
case "repository":
|
||||||
switch data.Action {
|
templ.Parse(templHeader + mesgTemplates["repository."+data.Action])
|
||||||
case "created":
|
|
||||||
templ.Parse("{{.Sender.FullName}} created repository {{}}")
|
|
||||||
case "deleted":
|
|
||||||
templ.Parse("{{.Sender.FullName}} deleted repository {{}}")
|
|
||||||
}
|
|
||||||
|
|
||||||
case "release":
|
case "release":
|
||||||
switch data.Action {
|
templ.Parse(templHeader + mesgTemplates["release."+data.Action])
|
||||||
case "published":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} published release {{}}")
|
|
||||||
case "updated":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} updated release {{}}")
|
|
||||||
case "deleted":
|
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} deleted release {{}}")
|
|
||||||
}
|
|
||||||
|
|
||||||
case "pull_request_approved":
|
case "pull_request_approved":
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} approved pull request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
|
templ.Parse(templHeader + mesgTemplates["pull_request_aproved"])
|
||||||
case "pull_request_rejected":
|
case "pull_request_rejected":
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} rejected pull request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"")
|
templ.Parse(templHeader + mesgTemplates["pull_request_rejected"])
|
||||||
case "pull_request_comment":
|
case "pull_request_comment":
|
||||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} commented on pull request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\": {{.Comment.Body}}")
|
templ.Parse(templHeader + mesgTemplates["pull_request_comment"])
|
||||||
|
default:
|
||||||
|
log.Warningf("Unknown action: %s for eventHeader %s", data.Action, eventHeader)
|
||||||
|
templ.Parse("Gitea did something unexpected, seriously wtf was that?! Event: " + eventHeader + " Action: " + data.Action)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := templ.Execute(&tpl, data); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tpl.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
//generateTextMessage generates the message string for a given event
|
||||||
|
func generateTextMessage(data GiteaPostData, eventHeader string) string {
|
||||||
|
|
||||||
|
templ := template.New("notification")
|
||||||
|
var tpl bytes.Buffer
|
||||||
|
|
||||||
|
mesgTemplates := map[string]string{
|
||||||
|
"push": "[{{.Repository.Fullname}}] {{.Pusher.FullName}} pushed " + strconv.Itoa(len(data.Commits)) + " commit(s) to {{.Repository.Name}}",
|
||||||
|
"issues.assigned": "[{{.Repository.FullName}}]: {{.Sender.FullName}} assigned issue #{{.Issue.Number}} {{.Issue.Title}} to {{}}",
|
||||||
|
"issues.closed": "[{{.Repository.FullName}}]: {{.Sender.FullName}} closed issue #{{.Issue.Number}} {{.Issue.Title}}",
|
||||||
|
"issues.demilestoned": "[{{.Repository.FullName}}]: {{.Sender.FullName}} removed milestone TODO from issue #{{.Issue.Number}} {{.Issue.Title}}",
|
||||||
|
"issues.edited": "[{{.Repository.FullName}}]: {{.Sender.FullName}} edited issue #{{.Issue.Number}} {{.Issue.Title}}",
|
||||||
|
"issues.label_cleared": "[{{.Repository.FullName}}]: {{.Sender.FullName}} cleared labels from issue #{{.Issue.Number}} {{.Issue.Title}}",
|
||||||
|
"issues.label_updated": "[{{.Repository.FullName}}]: {{.Sender.FullName}} updated labels of issue #{{.Issue.Number}} {{.Issue.Title}}",
|
||||||
|
"issues.milestoned": "[{{.Repository.FullName}}]: {{.Sender.FullName}} added issue #{{.Issue.Number}} {{.Issue.Title}} to milestone TODO",
|
||||||
|
"issues.opened": "[{{.Repository.FullName}}]: {{.Issue.User.FullName}} opened issue #{{.Issue.Number}} {{.Issue.Title}}",
|
||||||
|
"issues.reopened": "[{{.Repository.FullName}}]: {{.Sender.FullName}} re-opened issue #{{.Issue.Number}} {{.Issue.Title}}",
|
||||||
|
"issues.synchronized": "[{{.Repository.FullName}}]: {{.Sender.FullName}} synchronized issue #{{.Issue.Number}} {{.Issue.Title}}",
|
||||||
|
"issues.unassigned": "[{{.Repository.FullName}}]: {{.Sender.FullName}} removed assignee from issue #{{.Issue.Number}} {{.Issue.Title}}",
|
||||||
|
"fork": "[{{.Repository.FullName}}]: {{.Sender.FullName}} forked repository {{.Repository.Parent.FullName}} to {{.Repository.FullName}}",
|
||||||
|
"pull_request.assigned": "[{{.Repository.FullName}}]: {{.Sender.FullName}} assigned pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\" to {{.PullRequest.Assignee.FullName}}",
|
||||||
|
"pull_request.closed": "[{{.Repository.FullName}}]: {{.Sender.FullName}} closed pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"",
|
||||||
|
"pull_request.demilestoned": "[{{.Repository.FullName}}]: {{.Sender.FullName}} removed milestone TODO from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"",
|
||||||
|
"pull_request.edited": "[{{.Repository.FullName}}]: {{.Sender.FullName}} edited pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"",
|
||||||
|
"pull_request.label_cleared": "[{{.Repository.FullName}}]: {{.Sender.FullName}} removed labels from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"",
|
||||||
|
"pull_request.label_updated": "[{{.Repository.FullName}}]: {{.Sender.FullName}} updated labels from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"",
|
||||||
|
"pull_request.milestoned": "[{{.Repository.FullName}}]: {{.Sender.FullName}} added pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\" to milestone TODO",
|
||||||
|
"pull_request.opened": "[{{.Repository.FullName}}]: {{.Sender.FullName}} opened pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"",
|
||||||
|
"pull_request.reopened": "[{{.Repository.FullName}}]: {{.Sender.FullName}} re-opened pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"",
|
||||||
|
"pull_request.synchronized": "[{{.Repository.FullName}}]: {{.Sender.FullName}} synchronized pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"",
|
||||||
|
"pull_request.unassigned": "[{{.Repository.FullName}}]: {{.Sender.FullName}} removed assinee from pull-request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"",
|
||||||
|
"issue_comment.created": "{{.Sender.FullName}} commented on #{{.Issue.Number}} {{.Issue.Title}}: {{.Comment.Body}}",
|
||||||
|
"issue_comment.deleted": "{{.Sender.FullName}} deleted commented on #{{.Issue.Number}} {{.Issue.Title}}",
|
||||||
|
"repository.created": "{{.Sender.FullName}} created repository {{}}",
|
||||||
|
"repository.deleted": "{{.Sender.FullName}} deleted repository {{}}",
|
||||||
|
"release.published": "[{{.Repository.FullName}}]: {{.Sender.FullName}} published release {{}}",
|
||||||
|
"release.updated": "[{{.Repository.FullName}}]: {{.Sender.FullName}} updated release {{}}",
|
||||||
|
"release.deleted": "[{{.Repository.FullName}}]: {{.Sender.FullName}} deleted release {{}}",
|
||||||
|
"pull_request_aproved": "[{{.Repository.FullName}}]: {{.Sender.FullName}} approved pull request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"",
|
||||||
|
"pull_request_rejected": "[{{.Repository.FullName}}]: {{.Sender.FullName}} rejected pull request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\"",
|
||||||
|
"pull_request_comment": "[{{.Repository.FullName}}]: {{.Sender.FullName}} commented on pull request #{{.PullRequest.Number}} \"{{.PullRequest.Title}}\": {{.Comment.Body}}",
|
||||||
|
}
|
||||||
|
|
||||||
|
switch eventHeader {
|
||||||
|
case "push":
|
||||||
|
templ.Parse(mesgTemplates["push"])
|
||||||
|
case "issues":
|
||||||
|
templ.Parse(mesgTemplates["issues."+data.Action])
|
||||||
|
case "fork":
|
||||||
|
templ.Parse(mesgTemplates["fork"])
|
||||||
|
case "pull_request":
|
||||||
|
templ.Parse(mesgTemplates["pull_request."+data.Action])
|
||||||
|
case "issue_comment":
|
||||||
|
templ.Parse(mesgTemplates["issue_comment."+data.Action])
|
||||||
|
case "repository":
|
||||||
|
templ.Parse(mesgTemplates["repository."+data.Action])
|
||||||
|
case "release":
|
||||||
|
templ.Parse(mesgTemplates["release."+data.Action])
|
||||||
|
case "pull_request_approved":
|
||||||
|
templ.Parse(mesgTemplates["pull_request_aproved"])
|
||||||
|
case "pull_request_rejected":
|
||||||
|
templ.Parse(mesgTemplates["pull_request_rejected"])
|
||||||
|
case "pull_request_comment":
|
||||||
|
templ.Parse(mesgTemplates["pull_request_comment"])
|
||||||
default:
|
default:
|
||||||
log.Warningf("Unknown action: %s for eventHeader %s", data.Action, eventHeader)
|
log.Warningf("Unknown action: %s for eventHeader %s", data.Action, eventHeader)
|
||||||
templ.Parse("Gitea did something unexpected, seriously wtf was that?! Event: " + eventHeader + " Action: " + data.Action)
|
templ.Parse("Gitea did something unexpected, seriously wtf was that?! Event: " + eventHeader + " Action: " + data.Action)
|
||||||
|
Loading…
Reference in New Issue
Block a user