mirror of
https://github.com/pinpox/gitea-matrix-bot
synced 2024-11-22 19:31:58 +01:00
add first message templates
This commit is contained in:
parent
013b377a75
commit
7d941bc51b
195
listener.go
195
listener.go
@ -1,13 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"text/template"
|
||||
// "os"
|
||||
)
|
||||
|
||||
func setupListener() {
|
||||
@ -34,10 +37,21 @@ func PostHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var postData GiteaPostData
|
||||
|
||||
//TODO check secret!
|
||||
//TODO check repo
|
||||
|
||||
json.Unmarshal(body, &postData)
|
||||
|
||||
// fmt.Println(postData)
|
||||
generateMessage(postData)
|
||||
// fmt.Println(string(body))
|
||||
|
||||
fmt.Println("=================================================")
|
||||
fmt.Println(generateMessage(postData, r.Header.Get("X-Gitea-Event")))
|
||||
fmt.Println("=================================================")
|
||||
|
||||
fmt.Printf("%+v\n", postData)
|
||||
fmt.Println("=================================================")
|
||||
spew.Dump(postData)
|
||||
fmt.Println("=================================================")
|
||||
|
||||
fmt.Fprint(w, "POST done")
|
||||
} else {
|
||||
@ -45,56 +59,143 @@ func PostHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
type GiteaEvent int
|
||||
|
||||
const (
|
||||
ISSUE_OPEN = 0
|
||||
ISSUE_CLOSE = 1
|
||||
ISSUE_REOPEN = 2
|
||||
|
||||
COMMENT_ADD = 3
|
||||
COMMENT_EDIT = 4
|
||||
COMMENT_DEL = 5
|
||||
|
||||
PUSH = 6
|
||||
)
|
||||
|
||||
//determineAction figures out what the hell the user did
|
||||
func determineAction(data GiteaPostData) GiteaEvent {
|
||||
return 7
|
||||
}
|
||||
|
||||
//generateMessage generates the message string for a given event
|
||||
func generateMessage(data GiteaPostData) {
|
||||
func generateMessage(data GiteaPostData, eventHeader string) string {
|
||||
|
||||
switch determineAction(data) {
|
||||
case ISSUE_OPEN:
|
||||
fmt.Println("Issue Opened")
|
||||
case ISSUE_CLOSE:
|
||||
fmt.Println("Issue Closed")
|
||||
case ISSUE_REOPEN:
|
||||
fmt.Println("Issue Reopened")
|
||||
case COMMENT_ADD:
|
||||
fmt.Println("Comment added")
|
||||
case COMMENT_DEL:
|
||||
fmt.Println("Comment deleted")
|
||||
case COMMENT_EDIT:
|
||||
fmt.Println("Comment edited")
|
||||
templ := template.New("notification")
|
||||
var tpl bytes.Buffer
|
||||
|
||||
switch eventHeader {
|
||||
|
||||
case "push":
|
||||
templ.Parse("{{.Pusher.FullName}} pushed " + strconv.Itoa(len(data.Commits)) + " commit(s) to {{.Repository.Name}}")
|
||||
|
||||
case "issues":
|
||||
switch data.Action {
|
||||
case "assigned":
|
||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} assigned issue {{}} {{}} to {{}}")
|
||||
case "closed":
|
||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} closed issue {{}} {{}}")
|
||||
case "demilestoned":
|
||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} removed milestone {{}} from issue {{}} {{}}")
|
||||
case "edited":
|
||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} edited issue {{}} {{}}")
|
||||
case "label_cleared":
|
||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} cleared labels from issue {{}} {{}}")
|
||||
case "label_updated":
|
||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} updated labels of issue {{}} {{}}")
|
||||
case "milestoned":
|
||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} added issue {{}} {{}} to milestone {{}}")
|
||||
case "opened":
|
||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} opened issue {{}} {{}}")
|
||||
case "reopened":
|
||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} re-opened issue {{}} {{}}")
|
||||
case "synchronized":
|
||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} synchronized issue {{}} {{}}")
|
||||
case "unassigned":
|
||||
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} removed assignee from issue {{}} {{}}")
|
||||
default:
|
||||
fmt.Println("Unknown action: " + data.Action)
|
||||
}
|
||||
|
||||
case "fork":
|
||||
fmt.Println("Event: Fork")
|
||||
switch data.Action {
|
||||
case "created":
|
||||
templ.Parse("")
|
||||
case "edited":
|
||||
templ.Parse("")
|
||||
case "deleted":
|
||||
templ.Parse("")
|
||||
default:
|
||||
fmt.Println("Unknown action: " + data.Action)
|
||||
|
||||
}
|
||||
|
||||
case "pull_request":
|
||||
fmt.Println("Event: Pull Request")
|
||||
switch data.Action {
|
||||
case "assigned":
|
||||
templ.Parse("")
|
||||
case "closed":
|
||||
templ.Parse("")
|
||||
case "demilestoned":
|
||||
templ.Parse("")
|
||||
case "edited":
|
||||
templ.Parse("")
|
||||
case "label_cleared":
|
||||
templ.Parse("")
|
||||
case "label_updated":
|
||||
templ.Parse("")
|
||||
case "milestoned":
|
||||
templ.Parse("")
|
||||
case "opened":
|
||||
templ.Parse("")
|
||||
case "reopened":
|
||||
templ.Parse("")
|
||||
case "synchronized":
|
||||
templ.Parse("")
|
||||
case "unassigned":
|
||||
templ.Parse("")
|
||||
default:
|
||||
fmt.Println("Unknown action: " + data.Action)
|
||||
|
||||
}
|
||||
|
||||
case "issue_comment":
|
||||
fmt.Println("Event: Issues comment")
|
||||
templ.Parse("")
|
||||
|
||||
case "repository":
|
||||
fmt.Println("Event: Repository")
|
||||
switch data.Action {
|
||||
case "created":
|
||||
templ.Parse("")
|
||||
case "deleted":
|
||||
templ.Parse("")
|
||||
default:
|
||||
fmt.Println("Unknown action: " + data.Action)
|
||||
}
|
||||
|
||||
case "release":
|
||||
fmt.Println("Event: Release")
|
||||
switch data.Action {
|
||||
case "published":
|
||||
templ.Parse("")
|
||||
case "updated":
|
||||
templ.Parse("")
|
||||
case "deleted":
|
||||
templ.Parse("")
|
||||
|
||||
default:
|
||||
fmt.Println("Unknown action: " + data.Action)
|
||||
|
||||
}
|
||||
|
||||
case "pull_request_approved":
|
||||
fmt.Println("Event: PR approoved")
|
||||
fmt.Println("Unknown action: " + data.Action)
|
||||
templ.Parse("")
|
||||
case "pull_request_rejected":
|
||||
fmt.Println("Event: PR reject")
|
||||
fmt.Println("Unknown action: " + data.Action)
|
||||
templ.Parse("")
|
||||
case "pull_request_comment":
|
||||
fmt.Println("Event: PR comment")
|
||||
fmt.Println("Unknown action: " + data.Action)
|
||||
templ.Parse("")
|
||||
default:
|
||||
fmt.Println("Unknown action")
|
||||
fmt.Println(data)
|
||||
fmt.Println("Event: Unknown")
|
||||
fmt.Println(eventHeader)
|
||||
fmt.Println("Unknown action: " + data.Action)
|
||||
}
|
||||
|
||||
//TODO Events:
|
||||
//Pushed x commits
|
||||
// tmplPush, err := template.New("test").Parse("{{.User}} pushed {{.NumCommits}} to {{.Repo}}")
|
||||
if err := templ.Execute(&tpl, data); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return tpl.String()
|
||||
|
||||
//Actions: Opened, Closed, Repoened, Commented
|
||||
// tmplIssue, err := template.New("test").Parse("{{.User}} {{.Action}} Issue {{.IssueID}} in repository {{.Repo}}")
|
||||
//Issue Closed
|
||||
//Issue Opened
|
||||
//Issue Commented
|
||||
|
||||
//PR Stuff
|
||||
|
||||
}
|
||||
|
2
main.go
2
main.go
@ -67,6 +67,8 @@ func main() {
|
||||
// }
|
||||
// fmt.Println("Sending a message")
|
||||
// _, err = cli.SendText(roomID, "test message 0")
|
||||
// TODO maybe notice is better?
|
||||
// func (cli *Client) SendNotice(roomID, text string) (*RespSendEvent, error)
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user