Skip to content

Commit

Permalink
Automatically update all scores upon marking a habit (#20)
Browse files Browse the repository at this point in the history
* automatically update scores after marking a habit

* update docs and CD & release configs

* polish

* go mod tidy
  • Loading branch information
utkuufuk authored Apr 30, 2022
1 parent c942be8 commit b656ddd
Show file tree
Hide file tree
Showing 22 changed files with 659 additions and 531 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,5 @@ jobs:
- name: Build Progress Reporter
run: go build ./cmd/progress-report

- name: Build Score Updater
run: go build ./cmd/score-update

- name: Run tests
run: go test ./...
16 changes: 0 additions & 16 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,6 @@ builds:
- arm64
goarm: [6, 7]

- id: score-update
main: ./cmd/score-update
binary: score-update
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
goarch:
- amd64
- arm
- arm64
goarm: [6, 7]

archives:
- id: habit-service-archive
name_template: |-
Expand All @@ -60,7 +45,6 @@ archives:
builds:
- habit-service
- progress-report
- score-update
replacements:
386: i386
amd64: x86_64
Expand Down
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /bin/habit-service ./cmd/habit-service
RUN CGO_ENABLED=0 go build -o /bin/progress-report ./cmd/progress-report
RUN CGO_ENABLED=0 go build -o /bin/score-update ./cmd/score-update

FROM scratch
COPY --from=build /bin/habit-service /bin/habit-service
COPY --from=build /bin/progress-report /bin/progress-report
COPY --from=build /bin/score-update /bin/score-update
COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
WORKDIR /bin
Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ A new [Docker image](https://github.com/utkuufuk?tab=packages&repo_name=habit-se
--name habit-service \
ghcr.io/utkuufuk/habit-service/image:latest
# score update runner
docker run --rm \
--env-file </absolute/path/to/.env> \
ghcr.io/utkuufuk/habit-service/image:latest \
./score-update
# progress report runner
docker run --rm \
--env-file </absolute/path/to/.env> \
Expand Down
44 changes: 8 additions & 36 deletions cmd/habit-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ import (
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"

"github.com/utkuufuk/habit-service/internal/config"
"github.com/utkuufuk/habit-service/internal/habit"
"github.com/utkuufuk/habit-service/internal/logger"
"github.com/utkuufuk/habit-service/internal/service"
"github.com/utkuufuk/habit-service/internal/sheets"
)

var (
cfg config.ServerConfig
client habit.Client
client sheets.Client
)

func init() {
Expand All @@ -29,7 +28,7 @@ func init() {
os.Exit(1)
}

client, err = habit.GetClient(context.Background(), cfg.GoogleSheets)
client, err = sheets.GetClient(context.Background(), cfg.GoogleSheets)
if err != nil {
logger.Error("Could not create gsheets client for Habit Service: %v", err)
os.Exit(1)
Expand All @@ -48,10 +47,7 @@ func handleEntrelloRequest(w http.ResponseWriter, req *http.Request) {
}

if req.Method == http.MethodGet {
action := service.FetchHabitsAsTrelloCardsAction{
TimezoneLocation: cfg.TimezoneLocation,
}
cards, err := action.Run(req.Context(), client)
cards, err := service.FetchHabitCards(client, cfg.TimezoneLocation)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, fmt.Sprintf("could not fetch new cards: %v", err))
Expand All @@ -71,41 +67,17 @@ func handleEntrelloRequest(w http.ResponseWriter, req *http.Request) {
return
}

var card struct {
Desc string `json:"desc"`
Labels []struct {
Name string `json:"name"`
} `json:"labels"`
}
var card service.TrelloCard
cell := strings.Split(card.Desc, "\n")[0]
if err = json.Unmarshal(body, &card); err != nil {
logger.Warn("Invalid request body: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}

cell := strings.Split(card.Desc, "\n")[0]
matched, err := regexp.MatchString(`[a-zA-Z]{3} 202\d![A-Z][1-9][0-9]?$|^100$`, cell)
if err != nil || matched == false {
logger.Error("Invalid cell name '%s' in card description: %v", cell, err)
w.WriteHeader(http.StatusUnprocessableEntity)
return
}

symbol := "✔"
for _, c := range card.Labels {
if c.Name == "habit-skip" {
symbol = "–"
break
}
if c.Name == "habit-fail" {
symbol = "✘"
break
}
}

_, err = service.MarkHabitAction{Cell: cell, Symbol: symbol}.Run(req.Context(), client)
err = service.UpdateHabit(client, cfg.TimezoneLocation, cell, card.Labels)
if err != nil {
logger.Error("Could not mark habit at cell '%s' as %s: %v", cell, symbol, err)
logger.Error("Could not update habit at cell '%s': %v", cell, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down
20 changes: 9 additions & 11 deletions cmd/progress-report/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"os"

"github.com/utkuufuk/habit-service/internal/config"
"github.com/utkuufuk/habit-service/internal/habit"
"github.com/utkuufuk/habit-service/internal/logger"
"github.com/utkuufuk/habit-service/internal/service"
"github.com/utkuufuk/habit-service/internal/sheets"
)

func main() {
Expand All @@ -18,21 +18,19 @@ func main() {
}

ctx := context.Background()
client, err := habit.GetClient(ctx, cfg.GoogleSheets)
client, err := sheets.GetClient(ctx, cfg.GoogleSheets)
if err != nil {
logger.Error("Could not create gsheets client for Habit Service: %v", err)
os.Exit(1)
}

action := service.ReportProgressAction{
TimezoneLocation: cfg.TimezoneLocation,
SkipList: cfg.SkipList,
TelegramChatId: cfg.TelegramChatId,
TelegramToken: cfg.TelegramToken,
}

_, err = action.Run(ctx, client)
if err != nil {
if err = service.ReportProgress(
client,
cfg.TimezoneLocation,
cfg.SkipList,
cfg.TelegramChatId,
cfg.TelegramToken,
); err != nil {
logger.Error("Could not run Glados command: %v", err)
os.Exit(1)
}
Expand Down
32 changes: 0 additions & 32 deletions cmd/score-update/main.go

This file was deleted.

37 changes: 19 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,31 @@ go 1.18

require (
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/google/go-cmp v0.5.4
github.com/google/go-cmp v0.5.8
github.com/joho/godotenv v1.4.0
github.com/utkuufuk/entrello v0.1.1
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d
golang.org/x/oauth2 v0.0.0-20210220000619-9bb904979d93
google.golang.org/api v0.40.0
golang.org/x/exp v0.0.0-20220428152302-39d4317da171
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
google.golang.org/api v0.77.0
)

require (
cloud.google.com/go v0.74.0 // indirect
github.com/adlio/trello v1.8.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
cloud.google.com/go/compute v1.6.0 // indirect
github.com/adlio/trello v1.9.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/googleapis/gax-go/v2 v2.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
go.opencensus.io v0.22.5 // indirect
golang.org/x/exp v0.0.0-20220428152302-39d4317da171
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/text v0.3.6 // indirect
github.com/utkuufuk/entrello v1.1.2
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
golang.org/x/sys v0.0.0-20220429233432-b5fbb4746d32 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d // indirect
google.golang.org/grpc v1.34.0 // indirect
google.golang.org/protobuf v1.25.0 // indirect
google.golang.org/genproto v0.0.0-20220429170224-98d788798c3e // indirect
google.golang.org/grpc v1.46.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit b656ddd

Please sign in to comment.