Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .devcontainer/post_create.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
#!/bin/bash
set -e

apt-get update && apt-get install -y vim git

# tools
go install -v golang.org/x/tools/gopls@latest
go install -v github.com/go-delve/delve/cmd/dlv@latest
echo export PATH="$PATH:$(go env GOPATH)/bin" >> ~/.bashrc

git config --local core.editor vim
git config --local pull.rebase false
echo "source /usr/share/bash-completion/completions/git" >> ~/.bashrc

# binary will be $(go env GOPATH)/bin/golangci-lint
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.61.0
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
rss_subscriptions.db
bot
.env
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ FROM --platform=$BUILDPLATFORM golang:1.23.1-bookworm AS vscode
WORKDIR /app
COPY . /app
# RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
RUN <<EOF
apt-get update
apt-get install -y vim git sqlite3
go install -v golang.org/x/tools/gopls@latest
go install -v github.com/go-delve/delve/cmd/dlv@latest
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.61.0
EOF


FROM --platform=$BUILDPLATFORM golang:1.23.1-bookworm AS build
Expand All @@ -15,6 +22,7 @@ RUN CGO_ENABLED=1 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o bot main.go


FROM --platform=$BUILDPLATFORM gcr.io/distroless/base-debian12:latest
# FROM --platform=$BUILDPLATFORM gcr.io/distroless/base-debian12:nonroot
# ARG USERNAME=nonroot
# ARG GROUPNAME=nonroot
# ENV TZ Asia/Tokyo
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ docker compose up -d
- `/subscribe <URL>`
- `/list`
- `/unsubscribe <ID>`
- `/check <URL>`

## Docker build
```console
Expand Down
1 change: 1 addition & 0 deletions domain/repository/rss_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import "github.com/dev-shimada/discord-rss-bot/domain/model"
type RssEnrtyRepository interface {
Create(entries []model.RssEntry) error
Find(entries []model.RssEntry) []model.RssEntry
FindByModels(entries []model.RssEntry) []model.RssEntry
}
6 changes: 6 additions & 0 deletions infrastructure/persistence/rss_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ func (r RssEntryPersistence) Find(entries []model.RssEntry) []model.RssEntry {
r.db.Find(&entries)
return entries
}

func (r RssEntryPersistence) FindByModels(entries []model.RssEntry) []model.RssEntry {
res := []model.RssEntry{}
r.db.Where(&entries).Find(&res)
return res
}
66 changes: 66 additions & 0 deletions infrastructure/persistence/rss_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/dev-shimada/discord-rss-bot/infrastructure/database"
"github.com/dev-shimada/discord-rss-bot/infrastructure/persistence"
"github.com/google/go-cmp/cmp"
"gorm.io/gorm"
)

func TestRssEntryPersistenceCreate(t *testing.T) {
Expand Down Expand Up @@ -116,3 +117,68 @@ func TestRssEntryPersistenceFind(t *testing.T) {
})
}
}
func TestRssEntryPersistenceFindByModels(t *testing.T) {
now := time.Now()
tests := []struct {
name string
args []model.RssEntry
create func(db *gorm.DB)
want []model.RssEntry
}{
{
name: "empty",
args: []model.RssEntry{},
create: func(db *gorm.DB) {},
want: []model.RssEntry{},
},
{
name: "multiple",
args: []model.RssEntry{{ID: 1, RSSURL: "https://example.com/", EntryTitle: "title1", EntryLink: "https://example.com/entry1", PublishedAt: now}},
create: func(db *gorm.DB) {
db.Create(&model.RssEntry{ID: 1, RSSURL: "https://example.com/", EntryTitle: "title1", EntryLink: "https://example.com/entry1", PublishedAt: now})
db.Create(&model.RssEntry{ID: 2, RSSURL: "https://example.com/", EntryTitle: "title1", EntryLink: "https://example.com/entry1", PublishedAt: now})
},
want: []model.RssEntry{
{ID: 1, RSSURL: "https://example.com/", EntryTitle: "title1", EntryLink: "https://example.com/entry1", PublishedAt: now},
},
},
{
name: "no match",
args: []model.RssEntry{{ID: 1, RSSURL: "https://example.com/", EntryTitle: "title1", EntryLink: "https://example.com/entry1", PublishedAt: now}},
create: func(db *gorm.DB) {
db.Create(&model.RssEntry{ID: 2, RSSURL: "https://example.com/", EntryTitle: "title1", EntryLink: "https://example.com/entry1", PublishedAt: now})
},
want: []model.RssEntry{},
},
}

bfDbPath := os.Getenv("DB_PATH")
os.Setenv("DB_PATH", "testdata/test.db")
defer os.Setenv("DB_PATH", bfDbPath)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// setup
os.Remove("testdata/test.db")
db := database.NewDB()
defer database.CloseDB(db)
r := persistence.NewRssEntryPersistence(db)

// prepare
tt.create(db)

// test
got := r.FindByModels(tt.args)

// remove CreatedAt
for i := range got {
got[i].CreatedAt = time.Time{}
}

// assert
if !cmp.Equal(got, tt.want) {
t.Errorf("diff: %v", cmp.Diff(got, tt.want))
}
})
}
}
2 changes: 1 addition & 1 deletion usecase/rss_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (f RssEntriesUsecase) CheckNewEntries(s []model.Subscription) []model.RssEn
cpRes := make([]model.RssEntry, len(s))
copy(cpRes, res)

existingEntries := f.rr.Find(cpRes)
existingEntries := f.rr.FindByModels(cpRes)
newEntries := diff(res, existingEntries)
uniqueNewEntries := unique(newEntries)

Expand Down
5 changes: 4 additions & 1 deletion usecase/rss_entries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/dev-shimada/discord-rss-bot/domain/model"
"github.com/dev-shimada/discord-rss-bot/domain/repository"
"github.com/dev-shimada/discord-rss-bot/infrastructure/database"
"github.com/dev-shimada/discord-rss-bot/infrastructure/persistence"
"github.com/dev-shimada/discord-rss-bot/usecase"
Expand All @@ -24,7 +25,9 @@ func (m mockRss) Fetch(rssURL string) ([]*gofeed.Item, error) {
}

// mockRssEnrtyRepository is a mock of RssEnrtyRepository interface
type mockRssEnrtyRepository struct{}
type mockRssEnrtyRepository struct {
repository.RssEnrtyRepository
}

func (r mockRssEnrtyRepository) Create(_ []model.RssEntry) error { return nil }
func (r mockRssEnrtyRepository) Find(_ []model.RssEntry) []model.RssEntry { return nil }
Expand Down