2019-03-14 18:42:15 +03:00
|
|
|
package g2i
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-15 16:56:16 +03:00
|
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-github/github"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-03-16 16:24:35 +03:00
|
|
|
EVENTS_UPDATER_SLEEP = 60 * 10
|
|
|
|
ISSUES_UPDATER_SLEEP = 60 * 10
|
2019-03-14 18:42:15 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
GHClient *GithubClient
|
|
|
|
IDECClient *IDECClient
|
2019-03-15 16:56:16 +03:00
|
|
|
config *Config
|
2019-03-14 18:42:15 +03:00
|
|
|
}
|
|
|
|
|
2019-03-15 16:56:16 +03:00
|
|
|
func (c *Config) NewClient(ctx context.Context) *Client {
|
2019-03-14 18:42:15 +03:00
|
|
|
client := &Client{}
|
|
|
|
ghc := NewGithubClient(c, ctx)
|
2019-03-15 16:56:16 +03:00
|
|
|
ic := NewIDECClient(c)
|
|
|
|
client.GHClient = ghc
|
|
|
|
client.IDECClient = ic
|
|
|
|
client.config = c
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Run() {
|
|
|
|
// open db first
|
|
|
|
err := c.config.openDB()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer c.config.Data.db.Close()
|
|
|
|
|
|
|
|
evetsCH := make(chan github.Event)
|
|
|
|
// Run issues updater
|
|
|
|
go c.issuesUpdater()
|
|
|
|
// Run IDEC messager
|
2019-03-16 16:24:35 +03:00
|
|
|
go c.idecEventMessenger(evetsCH)
|
|
|
|
// Run events updater
|
|
|
|
c.eventsUpdater(evetsCH)
|
2019-03-15 16:56:16 +03:00
|
|
|
}
|
|
|
|
|
2019-03-16 16:24:35 +03:00
|
|
|
func (c *Client) idecEventMessenger(ch chan github.Event) {
|
|
|
|
if !c.config.isHelloSent() {
|
|
|
|
c.IDECClient.PostHello()
|
|
|
|
c.config.storeHello()
|
|
|
|
}
|
2019-03-15 16:56:16 +03:00
|
|
|
for {
|
|
|
|
event := <-ch
|
2019-03-16 16:24:35 +03:00
|
|
|
err := c.IDECClient.PostComment(event)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2019-03-15 16:56:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) eventsUpdater(ch chan github.Event) {
|
|
|
|
log.Info("Events updater is running")
|
|
|
|
eventsURL, err := c.GHClient.EventsURL()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
for {
|
2019-03-16 16:24:35 +03:00
|
|
|
log.Info("Get repository events")
|
2019-03-15 16:56:16 +03:00
|
|
|
events, err := c.GHClient.GetEvents(eventsURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
time.Sleep(time.Second * EVENTS_UPDATER_SLEEP)
|
|
|
|
}
|
|
|
|
|
|
|
|
var newEvents []github.Event
|
|
|
|
for i, _ := range events {
|
|
|
|
if c.config.IsEventProcessable(*events[i].Type) {
|
|
|
|
newEvents = append(newEvents, events[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prevEvents, err := c.config.getEvents()
|
2019-03-16 16:24:35 +03:00
|
|
|
for i := len(newEvents) - 1; i >= 0; i-- {
|
2019-03-15 16:56:16 +03:00
|
|
|
if err == nil {
|
|
|
|
if idNotInEvents(newEvents[i].GetID(), prevEvents) {
|
|
|
|
ch <- newEvents[i]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ch <- newEvents[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := c.config.storeEvents(newEvents); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second * 60 * 60)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func idNotInEvents(id string, events []github.Event) bool {
|
|
|
|
for i, _ := range events {
|
|
|
|
if events[i].GetID() == id {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) issuesUpdater() {
|
2019-03-16 16:24:35 +03:00
|
|
|
log.Info("Issues updater is running")
|
2019-03-15 16:56:16 +03:00
|
|
|
for {
|
|
|
|
issues, err := c.GHClient.GetIssues()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
} else {
|
|
|
|
if err := c.config.storeIssues(issues); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second * ISSUES_UPDATER_SLEEP)
|
|
|
|
}
|
2019-03-14 18:42:15 +03:00
|
|
|
}
|