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"
"fmt"
"io/ioutil"
"log"
"net/http"
2019-04-13 15:25:50 +02:00
"strconv"
2019-04-13 12:31:28 +02:00
"text/template"
2019-04-13 19:19:24 +02:00
"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 . Printf ( "listening on port %s" , httpPort )
log . Fatal ( http . ListenAndServe ( ":" + httpPort , mux ) )
}
// PostHandler converts post request body to string
func PostHandler ( w http . ResponseWriter , r * http . Request ) {
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
2019-04-13 15:25:50 +02:00
//TODO check secret!
//TODO check repo
2019-04-13 12:31:28 +02:00
json . Unmarshal ( body , & postData )
2019-04-13 15:25:50 +02:00
fmt . Printf ( "%+v\n" , postData )
fmt . Println ( "=================================================" )
spew . Dump ( postData )
fmt . Println ( "=================================================" )
2019-04-13 20:21:58 +02:00
fmt . Println ( string ( body ) )
2019-04-13 19:19:24 +02:00
fmt . Println ( "=================================================" )
2019-04-14 20:43:16 +02:00
message := generateMessage ( postData , r . Header . Get ( "X-Gitea-Event" ) )
bot . SendMessageToRooms ( postData . Repository . Name , message )
2019-04-13 19:19:24 +02:00
fmt . Println ( "=================================================" )
2019-04-13 12:31:28 +02:00
fmt . Fprint ( w , "POST done" )
} 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
2019-04-13 19:19:24 +02:00
fmt . Println ( "======================" )
fmt . Println ( eventHeader )
fmt . Println ( data . Action )
fmt . Println ( "======================" )
2019-04-13 15:25:50 +02:00
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" :
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" :
2019-04-13 19:19:24 +02:00
templ . Parse ( "{{.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 12:31:28 +02:00
default :
2019-04-13 19:19:24 +02:00
fmt . Println ( "Unknown action: " + eventHeader + " " + data . Action )
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 {
panic ( 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
// tmplIssue, err := template.New("test").Parse("{{.User}} {{.Action}} Issue {{.IssueID}} in repository {{.Repo}}")
}