Skip to content

Commit

Permalink
Reset task upon POST request from entrello (#12)
Browse files Browse the repository at this point in the history
* parse labels & cell name from archived card

* temp

* get rid of all glados-related code & mark habit upon entrello request

* add missing return statement
  • Loading branch information
utkuufuk authored Apr 3, 2022
1 parent f15d8c9 commit e520114
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.16
go-version: 1.18

- name: Check gofmt
run: test -z "$(gofmt -s -d .)"
Expand Down
122 changes: 62 additions & 60 deletions cmd/habit-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"

"github.com/utkuufuk/habit-service/internal/config"
"github.com/utkuufuk/habit-service/internal/glados"
"github.com/utkuufuk/habit-service/internal/habit"
"github.com/utkuufuk/habit-service/internal/logger"
"github.com/utkuufuk/habit-service/internal/service"
Expand All @@ -28,74 +29,75 @@ func main() {
}

http.HandleFunc("/entrello", handleEntrelloRequest)
http.HandleFunc("/glados", handleGladosCommand)
http.ListenAndServe(fmt.Sprintf(":%d", config.HttpPort), nil)
}

func handleEntrelloRequest(w http.ResponseWriter, req *http.Request) {
// @fixme: make this its own handler, doing the same thing as what handleGladosCommand currently does
if req.Method == http.MethodPost {
w.WriteHeader(http.StatusOK)
return
}

if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

action := service.FetchHabitsAsTrelloCardsAction{}
cards, err := action.Run(req.Context(), client)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, fmt.Sprintf("could not fetch new cards: %v", err))
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(cards)
}

func handleGladosCommand(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

type response struct {
Message string `json:"message"`
}
w.Header().Set("Content-Type", "application/json")

body, err := ioutil.ReadAll(req.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(response{fmt.Sprintf("Could not read HTTP request body: %v", err)})
if req.Method == http.MethodGet {
action := service.FetchHabitsAsTrelloCardsAction{}
cards, err := action.Run(req.Context(), client)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, fmt.Sprintf("could not fetch new cards: %v", err))
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(cards)
return
}

var request struct {
Args []string `json:"args"`
}
if err = json.Unmarshal(body, &request); err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(response{fmt.Sprintf("Could not decode HTTP request body: %v", err)})
return
}

action, err := glados.ParseCommand(request.Args)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(response{fmt.Sprintf("Could not parse Glados command: %v", err)})
return
}
if req.Method == http.MethodPost {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
logger.Error("Could not read request body: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}

var card struct {
Desc string `json:"desc"`
Labels []struct {
Name string `json:"name"`
} `json:"labels"`
}
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)
if err != nil {
logger.Error("Could not mark habit at cell '%s' as %s: %v", cell, symbol, err)
w.WriteHeader(http.StatusInternalServerError)
return
}

message, err := action.Run(req.Context(), client)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(response{err.Error()})
w.WriteHeader(http.StatusOK)
return
}

json.NewEncoder(w).Encode(response{message})
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
22 changes: 21 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/utkuufuk/habit-service

go 1.14
// +heroku goVersion go1.18
go 1.18

require (
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
Expand All @@ -11,3 +12,22 @@ require (
golang.org/x/oauth2 v0.0.0-20210220000619-9bb904979d93
google.golang.org/api v0.40.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
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/net v0.0.0-20201209123823-ac852fbbde11 // indirect
golang.org/x/sys v0.0.0-20210104204734-6f8348627aad // indirect
golang.org/x/text v0.3.6 // 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
gopkg.in/yaml.v2 v2.4.0 // indirect
)
22 changes: 0 additions & 22 deletions internal/glados/glados.go

This file was deleted.

0 comments on commit e520114

Please sign in to comment.