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

initial commit

This commit is contained in:
Pablo Ovelleiro Corral 2019-04-13 12:31:28 +02:00
commit 1d15852006
No known key found for this signature in database
GPG Key ID: F7C1D57C8464E825
5 changed files with 333 additions and 0 deletions

2
.gitignore vendored Normal file

@ -0,0 +1,2 @@
tags
config.ini

20
config.ini.example Normal file

@ -0,0 +1,20 @@
[http]
# The path the listener will expect the post data
http_uri = "/post"
# The port the listener will listen on
http_port = "9000"
[matrix]
# The matrix server to connect to
matrix_host = "http://matrix.org"
# The matrix room to post to
matrix_room = "#my-awesome-room:matrix.org"
# User credentions of the bot for posting to the room
matrix_pass = "supersecretpass"
matrix_user = "my-awesome-bot"

100
listener.go Normal file

@ -0,0 +1,100 @@
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"text/template"
)
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
json.Unmarshal(body, &postData)
// fmt.Println(postData)
generateMessage(postData)
fmt.Fprint(w, "POST done")
} else {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
}
}
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) {
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")
default:
fmt.Println("Unknown action")
fmt.Println(data)
}
//TODO Events:
//Pushed x commits
// tmplPush, err := template.New("test").Parse("{{.User}} pushed {{.NumCommits}} to {{.Repo}}")
//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
}

78
main.go Normal file

@ -0,0 +1,78 @@
package main
import (
"fmt"
"github.com/go-ini/ini"
"os"
_ "github.com/matrix-org/gomatrix"
)
// import "net/http"
//TODO get this vals from config file
// var (
// flagPort is the open port the application listens on
// flagPort = flag.String("port", "9000", "Port to listen on")
// matrixPass = "oSaiNahqu5ahF5jieBe2UKok"
// matrixUser = "reminder-bot"
// matrixHost = "http://matrix.org"
// postURI = "/post"
// )
var cfg *ini.File
var err error
func init() {
cfg, err = ini.Load("config.ini")
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
}
func main() {
//TODO Check if already logged in
//TODO Check if In room
//TODO Send
// fmt.Println("Logging in")
// cli, _ := gomatrix.NewClient("http://matrix.org", "", "")
// resp, err := cli.Login(&gomatrix.ReqLogin{
// Type: "m.login.password",
// User: matrixUser,
// Password: matrixPass,
// })
// if err != nil {
// panic(err)
// }
// cli.SetCredentials(resp.UserID, resp.AccessToken)
// fmt.Println("Joining Room")
// roomID := ""
// if resp, err := cli.JoinRoom("#test-reminder-bot:matrix.org", "", nil); err != nil {
// panic(err)
// } else {
// roomID = resp.RoomID
// }
// fmt.Println("Sending a message")
// _, err = cli.SendText(roomID, "test message 0")
// if err != nil {
// panic(err)
// }
fmt.Println("Setting up POST-listener")
setupListener()
}

133
postdata.go Normal file

@ -0,0 +1,133 @@
package main
import (
"time"
)
type GiteaPostData struct {
Secret string `json:"secret"`
Action string `json:"action"`
Number int `json:"number"`
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
CompareURL string `json:"compare_url"`
Issue struct {
ID int `json:"id"`
URL string `json:"url"`
Number int `json:"number"`
User 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:"user"`
Title string `json:"title"`
Body string `json:"body"`
Labels []interface{} `json:"labels"`
Milestone interface{} `json:"milestone"`
Assignee interface{} `json:"assignee"`
Assignees interface{} `json:"assignees"`
State string `json:"state"`
Comments int `json:"comments"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ClosedAt interface{} `json:"closed_at"`
DueDate interface{} `json:"due_date"`
PullRequest interface{} `json:"pull_request"`
} `json:"issue"`
Commits []struct {
ID string `json:"id"`
Message string `json:"message"`
URL string `json:"url"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"author"`
Committer struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"committer"`
Verification interface{} `json:"verification"`
Timestamp time.Time `json:"timestamp"`
} `json:"commits"`
Comment struct {
ID int `json:"id"`
HTMLURL string `json:"html_url"`
PullRequestURL string `json:"pull_request_url"`
IssueURL string `json:"issue_url"`
User 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:"user"`
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
} `json:"comment"`
Repository struct {
ID int `json:"id"`
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"`
Name string `json:"name"`
FullName string `json:"full_name"`
Description string `json:"description"`
Empty bool `json:"empty"`
Private bool `json:"private"`
Fork bool `json:"fork"`
Parent interface{} `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 {
Admin bool `json:"admin"`
Push bool `json:"push"`
Pull bool `json:"pull"`
} `json:"permissions"`
} `json:"repository"`
Pusher 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:"pusher"`
Sender 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:"sender"`
}