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

modified struct to fit subfields

This commit is contained in:
Pablo Ovelleiro Corral 2019-04-13 19:19:24 +02:00
parent 7d941bc51b
commit 313c3e10de
No known key found for this signature in database
GPG Key ID: F7C1D57C8464E825
2 changed files with 118 additions and 95 deletions

@ -4,12 +4,13 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/davecgh/go-spew/spew"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"text/template" "text/template"
"github.com/davecgh/go-spew/spew"
// "os" // "os"
) )
@ -44,15 +45,14 @@ func PostHandler(w http.ResponseWriter, r *http.Request) {
// fmt.Println(string(body)) // fmt.Println(string(body))
fmt.Println("=================================================")
fmt.Println(generateMessage(postData, r.Header.Get("X-Gitea-Event")))
fmt.Println("=================================================")
fmt.Printf("%+v\n", postData) fmt.Printf("%+v\n", postData)
fmt.Println("=================================================") fmt.Println("=================================================")
spew.Dump(postData) spew.Dump(postData)
fmt.Println("=================================================") fmt.Println("=================================================")
fmt.Println("=================================================")
fmt.Println(generateMessage(postData, r.Header.Get("X-Gitea-Event")))
fmt.Println("=================================================")
fmt.Fprint(w, "POST done") fmt.Fprint(w, "POST done")
} else { } else {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
@ -65,6 +65,13 @@ func generateMessage(data GiteaPostData, eventHeader string) string {
templ := template.New("notification") templ := template.New("notification")
var tpl bytes.Buffer var tpl bytes.Buffer
fmt.Println("======================")
fmt.Println(eventHeader)
fmt.Println(data.Action)
fmt.Println("======================")
switch eventHeader { switch eventHeader {
case "push": case "push":
@ -73,121 +80,102 @@ func generateMessage(data GiteaPostData, eventHeader string) string {
case "issues": case "issues":
switch data.Action { switch data.Action {
case "assigned": case "assigned":
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} assigned issue {{}} {{}} to {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} assigned issue #{{.Issue.Number}} {{.Issue.Title}} to TODO ")
case "closed": case "closed":
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} closed issue {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} closed issue #{{.Issue.Number}} {{.Issue.Title}}")
case "demilestoned": case "demilestoned":
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} removed milestone {{}} from issue {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed milestone TODO from issue #{{.Issue.Number}} {{.Issue.Title}}")
case "edited": case "edited":
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} edited issue {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} edited issue #{{.Issue.Number}} {{.Issue.Title}}")
case "label_cleared": case "label_cleared":
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} cleared labels from issue {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} cleared labels from issue #{{.Issue.Number}} {{.Issue.Title}}")
case "label_updated": case "label_updated":
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} updated labels of issue {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} updated labels of issue #{{.Issue.Number}} {{.Issue.Title}}")
case "milestoned": case "milestoned":
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} added issue {{}} {{}} to milestone {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} added issue #{{.Issue.Number}} {{.Issue.Title}} to milestone TODO")
case "opened": case "opened":
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} opened issue {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Issue.User.FullName}} opened issue #{{.Issue.Number}} {{.Issue.Title}}")
case "reopened": case "reopened":
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} re-opened issue {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} re-opened issue #{{.Issue.Number}} {{.Issue.Title}}")
case "synchronized": case "synchronized":
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} synchronized issue {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} synchronized issue #{{.Issue.Number}} {{.Issue.Title}}")
case "unassigned": case "unassigned":
templ.Parse("{{.Repository.FullName}}: {{.Sender.Fullname}} removed assignee from issue {{}} {{}}") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed assignee from issue #{{.Issue.Number}} {{.Issue.Title}}")
default:
fmt.Println("Unknown action: " + data.Action)
} }
case "fork": case "fork":
fmt.Println("Event: Fork") // switch data.Action {
switch data.Action { // case "created":
case "created": templ.Parse("{{.Sender.FullName}} forked repository {{.Repository.Parent.FullName}} to {{.Repository.FullName}}")
templ.Parse("") // case "edited":
case "edited": // templ.Parse("{{.Sender.FullName}} edited repository fork {{}}")
templ.Parse("") // case "deleted":
case "deleted": // templ.Parse("{{.Sender.FullName}} deleted repository fork {{}}")
templ.Parse("") // }
default:
fmt.Println("Unknown action: " + data.Action)
}
case "pull_request": case "pull_request":
fmt.Println("Event: Pull Request")
switch data.Action { switch data.Action {
case "assigned": case "assigned":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} assigned pull-request {{}} {{}} to {{}}")
case "closed": case "closed":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} closed pull-request {{}} {{}}")
case "demilestoned": case "demilestoned":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed milestone TODO from pull-request {{}} {{}}")
case "edited": case "edited":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} edited pull-request {{}} {{}}")
case "label_cleared": case "label_cleared":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed labels from pull-request {{}} {{}}")
case "label_updated": case "label_updated":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} updated labels from pull-request {{}} {{}}")
case "milestoned": case "milestoned":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} added pull-request {{}} {{}} to milestone TODO")
case "opened": case "opened":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} opened pull-request {{}} {{}}")
case "reopened": case "reopened":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} re-opened pull-request {{}} {{}}")
case "synchronized": case "synchronized":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} synchronized pull-request {{}} {{}}")
case "unassigned": case "unassigned":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} removed assinee {{}} from pull-request {{}} {{}}")
default:
fmt.Println("Unknown action: " + data.Action)
} }
case "issue_comment": case "issue_comment":
fmt.Println("Event: Issues comment")
templ.Parse("")
case "repository":
fmt.Println("Event: Repository")
switch data.Action { switch data.Action {
case "created": case "created":
templ.Parse("") templ.Parse("{{.Sender.FullName}} commented issue #{{.Issue.Number}} {{.Issue.Title}}: {{.Comment.Body}}")
case "deleted": case "deleted":
templ.Parse("") templ.Parse("{{.Sender.FullName}} deleted commented on issue #{{.Issue.Number}} {{.Issue.Title}}")
default: }
fmt.Println("Unknown action: " + data.Action)
case "repository":
switch data.Action {
case "created":
templ.Parse("{{.Sender.FullName}} created repository {{}}")
case "deleted":
templ.Parse("{{.Sender.FullName}} deleted repository {{}}")
} }
case "release": case "release":
fmt.Println("Event: Release")
switch data.Action { switch data.Action {
case "published": case "published":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} published release {{}}")
case "updated": case "updated":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} updated release {{}}")
case "deleted": case "deleted":
templ.Parse("") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} deleted release {{}}")
default:
fmt.Println("Unknown action: " + data.Action)
} }
case "pull_request_approved": case "pull_request_approved":
fmt.Println("Event: PR approoved") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} approved pull request {{}}{{}}")
fmt.Println("Unknown action: " + data.Action)
templ.Parse("")
case "pull_request_rejected": case "pull_request_rejected":
fmt.Println("Event: PR reject") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} rejected pull request {{}}{{}}")
fmt.Println("Unknown action: " + data.Action)
templ.Parse("")
case "pull_request_comment": case "pull_request_comment":
fmt.Println("Event: PR comment") templ.Parse("{{.Repository.FullName}}: {{.Sender.FullName}} commented on pull request {{}}{{}}: {{.Comment.Body}}")
fmt.Println("Unknown action: " + data.Action)
templ.Parse("")
default: default:
fmt.Println("Event: Unknown")
fmt.Println(eventHeader) fmt.Println("Unknown action: " + eventHeader + " " + data.Action)
fmt.Println("Unknown action: " + data.Action) templ.Parse("Gitea did something unexpected, seriously wtf was that?! Event: " + eventHeader + " Action: " + data.Action)
} }
if err := templ.Execute(&tpl, data); err != nil { if err := templ.Execute(&tpl, data); err != nil {

@ -85,27 +85,62 @@ type GiteaPostData struct {
Language string `json:"language"` Language string `json:"language"`
Username string `json:"username"` Username string `json:"username"`
} `json:"owner"` } `json:"owner"`
Name string `json:"name"` Name string `json:"name"`
FullName string `json:"full_name"` FullName string `json:"full_name"`
Description string `json:"description"` Description string `json:"description"`
Empty bool `json:"empty"` Empty bool `json:"empty"`
Private bool `json:"private"` Private bool `json:"private"`
Fork bool `json:"fork"` Fork bool `json:"fork"`
Parent interface{} `json:"parent"` Parent struct {
Mirror bool `json:"mirror"` CreatedAt time.Time `json:"created_at"`
Size int `json:"size"` Empty bool `json:"empty"`
HTMLURL string `json:"html_url"` Fork bool `json:"fork"`
SSHURL string `json:"ssh_url"` Private bool `json:"private"`
CloneURL string `json:"clone_url"` Archived bool `json:"archived"`
Website string `json:"website"` FullName string `json:"full_name"`
StarsCount int `json:"stars_count"` ForksCount int `json:"forks_count"`
ForksCount int `json:"forks_count"` ID int `json:"id"`
WatchersCount int `json:"watchers_count"` OpenIssuesCount int `json:"open_issues_count"`
OpenIssuesCount int `json:"open_issues_count"` Size int `json:"size"`
DefaultBranch string `json:"default_branch"` StarsCount int `json:"stars_count"`
Archived bool `json:"archived"` Name string `json:"name"`
CreatedAt time.Time `json:"created_at"` CloneURL string `json:"clone_url"`
UpdatedAt time.Time `json:"updated_at"` DefaultBranch string `json:"default_branch"`
SSHURL string `json:"ssh_url"`
WatchersCount int `json:"watchers_count"`
Description string `json:"description"`
UpdatedAt time.Time `json:"updated_at"`
Website string `json:"website"`
HTMLURL string `json:"html_url"`
Owner struct {
ID int `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"`
} `json:"owner"`
Permissions struct {
Admin bool `json:"admin"`
Push bool `json:"push"`
Pull bool `json:"pull"`
} `json:"permissions"`
} `json:"parent"`
Mirror bool `json:"mirror"`
Size int `json:"size"`
HTMLURL string `json:"html_url"`
SSHURL string `json:"ssh_url"`
CloneURL string `json:"clone_url"`
Website string `json:"website"`
StarsCount int `json:"stars_count"`
ForksCount int `json:"forks_count"`
WatchersCount int `json:"watchers_count"`
OpenIssuesCount int `json:"open_issues_count"`
DefaultBranch string `json:"default_branch"`
Archived bool `json:"archived"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Permissions struct { Permissions struct {
Admin bool `json:"admin"` Admin bool `json:"admin"`
Push bool `json:"push"` Push bool `json:"push"`