Skip to content

Commit

Permalink
Merge pull request #14 from Civil/dev
Browse files Browse the repository at this point in the history
Merge everything from dev branch to master
  • Loading branch information
Civil authored Jul 3, 2024
2 parents 15104a4 + 985809f commit 88ff885
Show file tree
Hide file tree
Showing 1,607 changed files with 1,035,142 additions and 44,742 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
config.yaml
github2telegram
github2telegram.db
github2telegram.sqlite3
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Changes

CHANGELOG
---------
**0.1.0**
- [Code] Upgrade all dependencies to their latest version
- [Code] Migrate to a different telegram bot library


**0.0.2**
- [Improvement] Improve logging, make it more consistent
- [Improvement] Add basic validation for repo name for a `new` command
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.15-alpine AS builder
FROM golang:1.21-alpine AS builder

# Set necessary environmet variables needed for our image
ENV CGO_ENABLED=1 \
Expand Down
8 changes: 5 additions & 3 deletions configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ type FiltersConfig struct {
}

type NotificationConfig struct {
Token string
Url string
Type string
Type string
Token string
WebhookURL string
WebhookPath string
WebhookListenAddress string
}

type NotificationEndpoints interface {
Expand Down
9 changes: 9 additions & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import (
"github.com/Civil/github2telegram/types"
"time"
)

Expand All @@ -12,16 +13,24 @@ type Database interface {
AddFeed(name, repo, filter, messagePattern string) (int, error)
GetFeed(name string) (*Feed, error)
ListFeeds() ([]*Feed, error)
RemoveFeed(name, repo, filter, messagePattern string) error

// Subscriptions
AddSubscribtion(endpoint, url, filter string, chatID int64) error
RemoveSubscribtion(endpoint, url, filter string, chatID int64) error

// Maintenance
UpdateChatID(oldChatID, newChatID int64) error

// Notification methods
GetNotificationMethods(url, filter string) ([]string, error)

// Endpoints
GetEndpointInfo(endpoint, url, filter string) ([]int64, error)

// Resend Queue
AddMessagesToResentQueue(messages []*types.NotificationMessage) error
GetMessagesFromResentQueue() ([]*types.NotificationMessage, error)
}

type Feed struct {
Expand Down
170 changes: 150 additions & 20 deletions db/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"database/sql"
"fmt"
"github.com/Civil/github2telegram/types"
"time"

"github.com/Civil/github2telegram/configs"
Expand All @@ -11,7 +12,7 @@ import (
)

const (
currentSchemaVersion = 2
currentSchemaVersion = 3
)

type SQLite struct {
Expand All @@ -25,7 +26,7 @@ func initSqlite() Database {
configs.Config.DB, err = sql.Open("sqlite3", configs.Config.DatabaseURL)
if err != nil {
logger.Fatal("unable to open database file",
zap.Any("config", configs.Config),
zap.String("file", configs.Config.DatabaseURL),
zap.Error(err),
)
}
Expand Down Expand Up @@ -67,11 +68,16 @@ func initSqlite() Database {
'message_pattern' VARCHAR(255) NOT NULL
);
INSERT INTO 'schema_version' (id, version) values (1, 2);
CREATE TABLE IF NOT EXISTS 'resend_queue' (
'id' INTEGER PRIMARY KEY AUTOINCREMENT,
'chat_id' Int64,
'message' TEXT NOT NULL
);
INSERT INTO 'schema_version' (id, version) values (1, 3);
`)
if err != nil {
logger.Fatal("failed to initialize database",
zap.Any("config", configs.Config),
zap.Error(err),
)
}
Expand All @@ -92,29 +98,68 @@ func initSqlite() Database {
}
rows.Close()

if schemaVersion != currentSchemaVersion {
switch schemaVersion {
case 1:
_, err = configs.Config.DB.Exec(`
if schemaVersion == 1 {
_, err = configs.Config.DB.Exec(`
ALTER TABLE last_version ADD COLUMN 'last_tag' VARCHAR(255) NOT NULL DEFAULT '';
UPDATE schema_version SET version = 2 WHERE id=1;
`)

if err != nil {
logger.Fatal("failed to migrate database",
zap.Int("databaseVersion", schemaVersion),
zap.Int("upgradingTo", currentSchemaVersion),
zap.Error(err),
)
}
// 'last_tag' VARCHAR(255) NOT NULL DEFAULT '',
default:
// Don't know how to migrate from this version
logger.Fatal("Unknown schema version specified",
zap.Int("version", schemaVersion),
if err != nil {
logger.Fatal("failed to migrate database",
zap.Int("databaseVersion", schemaVersion),
zap.Int("upgradingTo", currentSchemaVersion),
zap.Error(err),
)
}

_, err = configs.Config.DB.Exec(`
UPDATE schema_version SET version = 2 WHERE id=1;`)
if err != nil {
logger.Fatal("failed to migrate database",
zap.Int("databaseVersion", schemaVersion),
zap.Int("upgradingTo", currentSchemaVersion),
zap.Error(err),
)
}

// We've successfully upgraded to schema version 2.
schemaVersion = 2
}

if schemaVersion == 2 {
_, err = configs.Config.DB.Exec(` CREATE TABLE IF NOT EXISTS 'resend_queue' (
'id' INTEGER PRIMARY KEY AUTOINCREMENT,
'chat_id' Int64,
'message' TEXT NOT NULL
);`)
if err != nil {
logger.Fatal("failed to migrate database",
zap.Int("databaseVersion", schemaVersion),
zap.Int("upgradingTo", currentSchemaVersion),
zap.Error(err),
)
}

_, err = configs.Config.DB.Exec(`
UPDATE schema_version SET version = 3 WHERE id=1;`)
if err != nil {
logger.Fatal("failed to migrate database",
zap.Int("databaseVersion", schemaVersion),
zap.Int("upgradingTo", currentSchemaVersion),
zap.Error(err),
)
}

// We've successfully upgraded to schema version 3.
schemaVersion = 3
}

if schemaVersion != currentSchemaVersion {
// Don't know how to migrate from this version
logger.Fatal("Unknown schema version specified",
zap.Int("version", schemaVersion),
)
}
}

Expand Down Expand Up @@ -288,6 +333,36 @@ func (d *SQLite) ListFeeds() ([]*Feed, error) {
return result, nil
}

func (d *SQLite) RemoveFeed(name, repo, filter, messagePattern string) error {
logger := zapwriter.Logger("remove_feed")
stmt, err := d.db.Prepare("DELETE FROM 'feeds' WHERE name=? and repo=? and filter=? and message_pattern=?")
if err != nil {
logger.Error("error creating statement",
zap.Error(err),
)
return err
}

_, err = stmt.Exec(name, repo, filter, messagePattern)
if err != nil {
logger.Error("error removing subscription",
zap.Error(err),
)
}

return err
}

func (d *SQLite) UpdateChatID(oldChatID, newChatID int64) error {
stmt, err := d.db.Prepare("UPDATE 'subscriptions' SET chat_id=? WHERE chat_id=?")
if err != nil {
return err
}

_, err = stmt.Exec(newChatID, oldChatID)
return err
}

func (d *SQLite) AddSubscribtion(endpoint, url, filter string, chatID int64) error {
stmt, err := d.db.Prepare("SELECT chat_id FROM 'subscriptions' where endpoint=? and url=? and filter=? and chat_id=?;")
if err != nil {
Expand Down Expand Up @@ -446,3 +521,58 @@ func (d *SQLite) UpdateLastUpdateTime(url, filter, tag string, t time.Time) {
return
}
}

func (db *SQLite) AddMessagesToResentQueue(messages []*types.NotificationMessage) error {
logger := zapwriter.Logger("add_messages_to_resent_queue")
stmt, err := db.db.Prepare("INSERT INTO 'resend_queue' (chat_id, message) VALUES (?, ?)")
if err != nil {
logger.Error("error creating statement",
zap.Error(err),
)
return err
}

for _, m := range messages {
_, err = stmt.Exec(m.ChatID, m.Message)
if err != nil {
logger.Error("error updating data",
zap.Error(err),
)
return err
}
}
return nil
}

func (db *SQLite) GetMessagesFromResentQueue() ([]*types.NotificationMessage, error) {
logger := zapwriter.Logger("get_messages_from_resent_queue")
stmt, err := db.db.Prepare("SELECT chat_id, message FROM 'resend_queue'")
if err != nil {
logger.Error("error creating statement",
zap.Error(err),
)
return nil, err
}
rows, err := stmt.Query()
if err != nil {
logger.Error("error retrieving data",
zap.Error(err),
)
return nil, err
}
results := make([]*types.NotificationMessage, 0)
for rows.Next() {
res := &types.NotificationMessage{}
err = rows.Scan(&res.ChatID, &res.Message)
if err != nil {
logger.Error("error retrieving data",
zap.Error(err),
)
continue
}
results = append(results, res)
}
_ = rows.Close()

return nil, nil
}
6 changes: 6 additions & 0 deletions endpoints/parameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package endpoints

type ConfigParams struct {
Name string
Value string
}
Loading

0 comments on commit 88ff885

Please sign in to comment.