Skip to content

Commit

Permalink
Support for issues and pushes
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Aug 6, 2022
1 parent 3ed793d commit f30b696
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 7 deletions.
66 changes: 66 additions & 0 deletions events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

type Event struct {
Ref string `json:"ref,omitempty"`
// The user who pushed the commits.
Pusher Pusher `json:"pusher,omitempty"`
// The action performed. Can be created, edited, or deleted.
Action string `json:"action,omitempty"`
// The commit comment resource.
Comment Comment `json:"comment,omitempty"`
// The discussion resource.
Discussion Discussion `json:"discussion,omitempty"`
// The issue the comment belongs to.
Issue Issue `json:"issue,omitempty"`
// The repository where the event occurred.
Repository Repository `json:"repository"`
// The user that triggered the event.
Sender User `json:"sender"`
// URL that shows the changes in this ref update, from the before commit to the after commit.
// For a newly created ref that is directly based on the default branch, this is the comparison between the head of the default branch and the after commit.
// Otherwise, this shows all commits until the after commit.
Compare string `json:"compare,omitempty"`
}

type Pusher struct {
Name string `json:"name"`
Email string `json:"email,omitempty"`
}

type PullRequest struct {
Title string `json:"title"`
Url string `json:"html_url"`
IssueUrl string `json:"issue_url,omitempty"`
Number int `json:"number"`
}

type Issue struct {
Title string `json:"title"`
Url string `json:"html_url"`
Number int `json:"number"`
User User `json:"user,omitempty"`
}

type Repository struct {
Name string `json:"name"`
Private bool `json:"private"`
Url string `json:"html_url"`
}

type Discussion struct {
Title string `json:"title"`
Url string `json:"html_url"`
Number int `json:"number"`
User User `json:"user,omitempty"`
}

type Comment struct {
Url string `json:"html_url,omitempty"`
User User `json:"user,omitempty"`
}

type User struct {
Name string `json:"login,omitempty"`
Url string `json:"html_url,omitempty"`
Type string `json:"type,omitempty"`
}
Binary file added gigafeed
Binary file not shown.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/gigauserbot/gigafeed

go 1.18

require github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.9
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.9 h1:0pEtHIqipAj62qnGjg+sFRNw8+iR9LXP1cmF2YLZ+Ac=
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.9/go.mod h1:r815fYWTudnU9JhtsJAxUtuV7QrSgKpChJkfTSMFpfg=
113 changes: 106 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,126 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"

"github.com/PaulSonOfLars/gotgbot/v2"
)

const GIGA_FEED_CHAT_ID = -1001799797732
const BOT_TOKEN = "hard code it here"

func main() {
b, err := gotgbot.NewBot(BOT_TOKEN, &gotgbot.BotOpts{})
if err != nil {
panic("failed to create bot: " + err.Error())
}
webhookListener(b)
}

func webhookListener(b *gotgbot.Bot) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/", processUpdate(b))
server := &http.Server{
Addr: "0.0.0.0:3455",
Handler: mux,
ReadTimeout: time.Second * 2,
}
server.ListenAndServe()
}

func processUpdate(bot *gotgbot.Bot) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println("en error occured:", err.Error())
return
}
fmt.Println(string(b))
var event = Event{}
err = json.Unmarshal(b, &event)
if err != nil {
fmt.Println("failed to process update:", err.Error())
return
}
handleUpdate(bot, &event)
}
}

const PUSH_TEMPL = `
<b><u><a href="github.com/gigauserbot">THE GIGA PROJECT</a></u></b>
<b><u>New Push</u></b>
<b>Repository</b>: <code>%s</code>
<b>Ref</b>: <code>%s</code>
<b>Changes</b>: <a href="%s">click here</a>
<b>Pusher's Name</b>: %s
<b>Pusher's Email</b>: %s
`

const ISSUE_TEMPL = `
<b><u><a href="github.com/gigauserbot">THE GIGA PROJECT</a></u></b>
<b><u>New Issue Update</u></b>
<b>Repository</b>: <code>%s</code>
<b>Action</b>: <code>%s</code>
<b>Issue</b>: <a href="%s">%s</a>
<b>By</b>: %s
`

func handleUpdate(b *gotgbot.Bot, event *Event) {
fmt.Println("ok")
if event.Repository.Private {
// Don't log info about private repos
return
}
switch {
case event.Ref != "":
send(b,
fmt.Sprintf(
PUSH_TEMPL,
event.Repository.Name,
event.Ref,
event.Compare,
event.Pusher.Name,
event.Pusher.Email,
),
&gotgbot.InlineKeyboardMarkup{
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
{{Text: "Repository", Url: event.Repository.Url}},
},
},
)
case event.Issue.Number != 0:
fmt.Println("issue number")
send(b,
fmt.Sprintf(
ISSUE_TEMPL,
event.Repository.Name,
event.Action,
event.Issue.Url,
event.Issue.Title,
event.Issue.User.Name,
),
&gotgbot.InlineKeyboardMarkup{
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
{{Text: "Repository", Url: event.Repository.Url}},
},
},
)
}
}

func send(b *gotgbot.Bot, text string, markup *gotgbot.InlineKeyboardMarkup) {
_, err := b.SendMessage(GIGA_FEED_CHAT_ID, text, &gotgbot.SendMessageOpts{
ParseMode: "html",
ReplyMarkup: markup,
DisableWebPagePreview: true,
})
server := &http.Server{
Addr: "0.0.0.0:3455",
Handler: mux,
ReadTimeout: time.Second * 2,
if err != nil {
fmt.Println(err.Error())
}
server.ListenAndServe()
}

0 comments on commit f30b696

Please sign in to comment.