From 89b54f76ca82d96e010adaa6fa1016521bb8f1b8 Mon Sep 17 00:00:00 2001 From: Sonlis Date: Tue, 19 Dec 2023 19:35:30 +0200 Subject: [PATCH] Add ci --- .github/workflows/build-and-publish.yaml | 29 ++++++++++++++++++++++++ .github/workflows/test.yaml | 11 +++++++++ Dockerfile | 13 +++++++++++ Makefile | 2 +- docker-compose.yaml | 1 + internal/handle/buttonHandlers.go | 2 -- internal/handle/formatting.go | 4 +--- internal/notifier/notifier.go | 1 + internal/notifier/notifier_test.go | 7 +++--- 9 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/build-and-publish.yaml create mode 100644 .github/workflows/test.yaml create mode 100644 Dockerfile diff --git a/.github/workflows/build-and-publish.yaml b/.github/workflows/build-and-publish.yaml new file mode 100644 index 0000000..e99951b --- /dev/null +++ b/.github/workflows/build-and-publish.yaml @@ -0,0 +1,29 @@ +name: BuildAndPublish +on: + workflow_dispatch: + inputs: + version: + description: Version to publish + required: true + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: '1.20.0' + - name: Install dependencies + run: go mod tidy + - name: Test with the Go CLI + run: make test + - name: Build binary + run: go build cmd/server/server.go -o server + - name: Build docker image + run: | + docker buildx build --push \ + --tag bastibast/athene-events-reminder:${{ github.event.inputs.version }} \ + --platform linux/amd64,linux/arm/v7,linux/arm64 . diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..3e847af --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,11 @@ +name: Test +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Test with the Go CLI + run: make test diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..896c294 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM alpine:3 as packages +RUN apk --update add ca-certificates +RUN apk add tzdata + +FROM scratch +WORKDIR /opt/app + +COPY --from=packages /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt +COPY --from=packages /usr/share/zoneinfo /usr/share/zoneinfo +COPY server server + +USER 1001 +ENTRYPOINT ["./server"] diff --git a/Makefile b/Makefile index cb921b3..6393929 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ gotest: compose: docker-compose up -d sleep 3 - PGPASSWORD=test_pass psql -h localhost -p 5447 -U test_user -d test -c "CREATE TABLE IF NOT EXISTS reminder(chat_id int, event_id int, reminder_time timestamp);" + PGPASSWORD=test_pass psql -h localhost -p 5447 -U test_user -d test -c "CREATE TABLE IF NOT EXISTS reminder(chat_id int, event_id int, reminder_time timestamptz);" .PHONY: clean clean: diff --git a/docker-compose.yaml b/docker-compose.yaml index 24481f4..9a6be55 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -6,5 +6,6 @@ services: POSTGRES_USER: test_user POSTGRES_PASSWORD: test_pass POSTGRES_DB: test + TZ: UTC ports: - 5447:5432 diff --git a/internal/handle/buttonHandlers.go b/internal/handle/buttonHandlers.go index ff938b3..200092a 100644 --- a/internal/handle/buttonHandlers.go +++ b/internal/handle/buttonHandlers.go @@ -2,7 +2,6 @@ package handle import ( "context" - "fmt" "strconv" "strings" @@ -21,7 +20,6 @@ func handleEventInfo(ctx context.Context, ilmo *event.Ilmo, query tgbotapi.Callb if err != nil { return text, markup, err } - fmt.Println(event.RegistrationStartDate) reminderSet, err := db.CheckReminder(ctx, message.Chat.ID, event.ID) if err != nil { return text, markup, err diff --git a/internal/handle/formatting.go b/internal/handle/formatting.go index af89dae..496a84f 100644 --- a/internal/handle/formatting.go +++ b/internal/handle/formatting.go @@ -13,9 +13,7 @@ var backButton = "Back" // buildEventsMarkup builds the markup structure returned when a single event is requested. func buildEventInfoMarkup(event event.Event, reminderSet bool) tgbotapi.InlineKeyboardMarkup { // Set the reminde to be 5 minutes before the event registration starts. - // Also remove 2 hours to the time, because the event is in UTC+2, while the db time is in UTC. - // Should be removed once the db timezone is correctly set. - reminderTime := event.RegistrationStartDate.Add(time.Duration(-5) * time.Minute).Add(time.Duration(-2) * time.Hour).Format("2006-01-02T15:04:05Z07:00") + reminderTime := event.RegistrationStartDate.Add(time.Duration(-5) * time.Minute).UTC().Format("2006-01-02T15:04:05Z07:00") if reminderSet { return tgbotapi.NewInlineKeyboardMarkup( tgbotapi.NewInlineKeyboardRow( diff --git a/internal/notifier/notifier.go b/internal/notifier/notifier.go index 7c0228d..9081a20 100644 --- a/internal/notifier/notifier.go +++ b/internal/notifier/notifier.go @@ -73,6 +73,7 @@ func getReminders(ctx context.Context, db *database.DB, i *event.Ilmo) ([]Remind if err := rows.Scan(&reminder.ChatId, &reminder.EventId, &reminder.ReminderTime); err != nil { return nil, err } + reminder.ReminderTime = reminder.ReminderTime.UTC() reminders = append(reminders, reminder) } if err := rows.Err(); err != nil { diff --git a/internal/notifier/notifier_test.go b/internal/notifier/notifier_test.go index 638878a..b90cd5c 100644 --- a/internal/notifier/notifier_test.go +++ b/internal/notifier/notifier_test.go @@ -32,8 +32,7 @@ func TestGetReminders(t *testing.T) { timeToInsert := time.Date(2023, 12, 8, 16, 0, 0, 0, time.UTC) - currentTime := time.Now().Add(time.Second * 30).Add(time.Duration(-2) * time.Hour) - + currentTime := time.Now().Add(time.Second * 30).UTC() err = db_conn.CreateReminder(context.Background(), 1, 1, timeToInsert) if err != nil { t.Errorf("Failed to create reminder: %v", err) @@ -48,11 +47,11 @@ func TestGetReminders(t *testing.T) { if err != nil { t.Errorf("Error while getting reminders: %v", err) } - want := Reminder{2, 2, currentTime.UTC().Add(time.Hour * 2)} + want := Reminder{2, 2, currentTime} if len(reminders) != 1 { t.Errorf("Expected 1 reminder, got %v", len(reminders)) } - if reminders[0] != want { + if !reminders[0].ReminderTime.Truncate(time.Second).Equal(want.ReminderTime.Truncate(time.Second)) { t.Errorf("Expected %v, got %v", want, reminders[0]) } }