Skip to content

Commit

Permalink
remove todo & today list ID configs & assign list for each source
Browse files Browse the repository at this point in the history
  • Loading branch information
utkuufuk committed Sep 23, 2021
1 parent 58e9dcf commit c24f985
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 197 deletions.
4 changes: 2 additions & 2 deletions cmd/entrello/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

// get a list of enabled sources and the corresponding labels for each source
sources, labels := getEnabledSources(cfg.Sources)
// get a list of sources and the corresponding labels for each source
sources, labels := getSources(cfg.Sources)
if len(sources) == 0 {
return
}
Expand Down
10 changes: 3 additions & 7 deletions cmd/entrello/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/utkuufuk/entrello/pkg/trello"
)

// getEnabledSources returns a slice of enabled sources & their labels as a separate slice
func getEnabledSources(cfg config.Sources) (sources []config.Source, labels []string) {
// getSources returns a slice of sources & their labels as a separate slice
func getSources(cfg config.Sources) (sources []config.Source, labels []string) {
arr := []config.Source{
cfg.GithubIssues,
cfg.TodoDock,
Expand All @@ -36,10 +36,6 @@ func getEnabledSources(cfg config.Sources) (sources []config.Source, labels []st

// shouldQuery checks if a the source should be queried at the given time
func shouldQuery(src config.Source, date time.Time) (bool, error) {
if !src.Enabled {
return false, nil
}

interval := src.Period.Interval
if interval < 0 {
return false, fmt.Errorf("period interval must be a positive integer, got: '%d'", interval)
Expand Down Expand Up @@ -98,7 +94,7 @@ func process(src config.Source, ctx context.Context, client trello.Client, wg *s

new, stale := client.FilterNewAndStale(cards, src.Label)
for _, c := range new {
if err := client.CreateCard(c, src.Label, now); err != nil {
if err := client.CreateCard(c, src.Label, src.List); err != nil {
logger.Errorf("could not create Trello card: %v", err)
continue
}
Expand Down
87 changes: 0 additions & 87 deletions cmd/entrello/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,9 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/utkuufuk/entrello/internal/config"
)

func TestGetEnabledSources(t *testing.T) {
period := config.Period{
Type: config.PERIOD_TYPE_DEFAULT,
Interval: 0,
}

tt := []struct {
name string
githubIssuesCfg config.Source
todoDockCfg config.Source
numResults int
labels []string
}{
{
name: "nothing enabled",
githubIssuesCfg: config.Source{Enabled: false, Period: period},
todoDockCfg: config.Source{Enabled: false, Period: period},
numResults: 0,
},
{
name: "only github issues enabled",
githubIssuesCfg: config.Source{
Enabled: true,
Period: period,
Label: "github-label",
},
todoDockCfg: config.Source{Enabled: false, Period: period},
numResults: 1,
labels: []string{"github-label"},
},
{
name: "only tododock enabled",
githubIssuesCfg: config.Source{Enabled: false, Period: period},
todoDockCfg: config.Source{
Enabled: true,
Period: period,
Label: "tododock-label",
},
numResults: 1,
labels: []string{"tododock-label"},
},
{
name: "all enabled",
githubIssuesCfg: config.Source{
Enabled: true,
Period: period,
Label: "github-label",
},
todoDockCfg: config.Source{
Enabled: true,
Period: period,
Label: "tododock-label",
},
numResults: 2,
labels: []string{"github-label", "tododock-label"},
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
cfg := config.Sources{
GithubIssues: tc.githubIssuesCfg,
TodoDock: tc.todoDockCfg,
}

sources, labels := getEnabledSources(cfg)
if len(sources) != tc.numResults {
t.Errorf("expected %d source(s); got %v", tc.numResults, len(sources))
}

if len(labels) != tc.numResults {
t.Errorf("expected %d label(s); got %v", tc.numResults, len(labels))
}

if tc.numResults == 0 {
return
}

if diff := cmp.Diff(labels, tc.labels); diff != "" {
t.Errorf("labels diff: %s", diff)
}
})
}
}

func TestShouldQuery(t *testing.T) {
tt := []struct {
name string
Expand Down Expand Up @@ -228,7 +142,6 @@ func TestShouldQuery(t *testing.T) {
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
src := config.Source{
Enabled: true,
Period: config.Period{
Type: tc.pType,
Interval: tc.pInterval,
Expand Down
8 changes: 3 additions & 5 deletions config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ trello:
api_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
api_token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
board_id: xxxxxxxxxxxxxxxxxxxxxxxx
todo_list_id: xxxxxxxxxxxxxxxxxxxxxxxx
today_list_id: xxxxxxxxxxxxxxxxxxxxxxxx

telegram:
enabled: true
Expand All @@ -17,29 +15,29 @@ sources:
github_issues:
name: "Github Issues"
endpoint: https://<domain>:<port>/entrello
enabled: true
strict: true
label_id: xxxxxxxxxxxxxxxxxxxxxxxx
list_id: xxxxxxxxxxxxxxxxxxxxxxxx
period:
type: minute
interval: 15

tododock:
name: "TodoDock"
endpoint: https://<domain>:<port>/entrello
enabled: true
strict: false
label_id: xxxxxxxxxxxxxxxxxxxxxxxx
list_id: xxxxxxxxxxxxxxxxxxxxxxxx
period:
type: hour
interval: 1

habits:
name: "Google Spreadsheet Habits"
endpoint: https://<domain>:<port>/entrello
enabled: true
strict: true
label_id: xxxxxxxxxxxxxxxxxxxxxxxx
list_id: xxxxxxxxxxxxxxxxxxxxxxxx
period:
type: hour
interval: 12
10 changes: 4 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ type Period struct {
type Source struct {
Name string `yaml:"name"`
Endpoint string `yaml:"endpoint"`
Enabled bool `yaml:"enabled"`
Strict bool `yaml:"strict"`
Label string `yaml:"label_id"`
List string `yaml:"list_id"`
Period Period `yaml:"period"`
}

Expand All @@ -35,11 +35,9 @@ type Sources struct {
}

type Trello struct {
ApiKey string `yaml:"api_key"`
ApiToken string `yaml:"api_token"`
BoardId string `yaml:"board_id"`
TodoListId string `yaml:"todo_list_id"`
TodayListId string `yaml:"today_list_id"`
ApiKey string `yaml:"api_key"`
ApiToken string `yaml:"api_token"`
BoardId string `yaml:"board_id"`
}

type Telegram struct {
Expand Down
29 changes: 2 additions & 27 deletions pkg/trello/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package trello

import (
"fmt"
"time"

"github.com/adlio/trello"
)
Expand All @@ -14,33 +13,9 @@ func (c Client) DeleteCard(card Card) error {
}

// CreateCard creates a Trello card using the the Trello API
func (c Client) CreateCard(card Card, label string, now time.Time) error {
func (c Client) CreateCard(card Card, label string, listId string) error {
card.IDLabels = []string{label}

if card.Due == nil {
card.IDList = c.todoListId
return c.api.CreateCard(card, trello.Defaults())
}

dueYear := card.Due.Year()
dueMonth := card.Due.Month()
dueDay := card.Due.Day()

thisYear := now.Year()
thisMonth := now.Month()
today := now.Day()

// insert into the todo list, unless the due date is sometime today
if dueYear > thisYear {
card.IDList = c.todoListId
} else if dueYear == thisYear && dueMonth > thisMonth {
card.IDList = c.todoListId
} else if dueYear == thisYear && dueMonth == thisMonth && dueDay > today {
card.IDList = c.todoListId
} else {
card.IDList = c.todayListId
}

card.IDList = listId
return c.api.CreateCard(card, trello.Defaults())
}

Expand Down
6 changes: 1 addition & 5 deletions pkg/trello/trello.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@ type Card *trello.Card
type Client struct {
api *trello.Client
boardId string
todoListId string
todayListId string
existingCards map[string][]Card
}

func NewClient(cfg config.Trello) (client Client, err error) {
if cfg.BoardId == "" || cfg.TodoListId == "" || cfg.TodayListId == "" || cfg.ApiKey == "" || cfg.ApiToken == "" {
if cfg.BoardId == "" || cfg.ApiKey == "" || cfg.ApiToken == "" {
return client, fmt.Errorf("could not create trello client, missing configuration parameter(s)")
}

return Client{
api: trello.NewClient(cfg.ApiKey, cfg.ApiToken),
boardId: cfg.BoardId,
todoListId: cfg.TodoListId,
todayListId: cfg.TodayListId,
existingCards: make(map[string][]Card),
}, nil
}
Expand Down
86 changes: 28 additions & 58 deletions pkg/trello/trello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,79 +49,49 @@ func TestNewClient(t *testing.T) {
str := "placeholder"

tt := []struct {
name string
boardId string
todoListId string
todayListId string
apiKey string
apiToken string
err bool
name string
boardId string
apiKey string
apiToken string
err bool
}{
{
name: "no errors",
boardId: str,
todoListId: str,
todayListId: str,
apiKey: str,
apiToken: str,
err: false,
},
{
name: "missing board ID",
boardId: "",
todoListId: str,
todayListId: str,
apiKey: str,
apiToken: str,
err: true,
},
{
name: "missing today list ID",
boardId: str,
todoListId: str,
todayListId: "",
apiKey: str,
apiToken: str,
err: true,
name: "no errors",
boardId: str,
apiKey: str,
apiToken: str,
err: false,
},
{
name: "missing todo list ID",
boardId: str,
todoListId: "",
todayListId: str,
apiKey: str,
apiToken: str,
err: true,
name: "missing board ID",
boardId: "",
apiKey: str,
apiToken: str,
err: true,
},
{
name: "missing api key",
boardId: str,
todoListId: str,
todayListId: str,
apiKey: "",
apiToken: str,
err: true,
name: "missing api key",
boardId: str,
apiKey: "",
apiToken: str,
err: true,
},
{
name: "missing api token",
boardId: str,
todoListId: str,
todayListId: str,
apiKey: str,
apiToken: "",
err: true,
name: "missing api token",
boardId: str,
apiKey: str,
apiToken: "",
err: true,
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
cfg := config.Config{
Trello: config.Trello{
ApiKey: tc.apiKey,
ApiToken: tc.apiToken,
BoardId: tc.boardId,
TodoListId: tc.todoListId,
TodayListId: tc.todayListId,
ApiKey: tc.apiKey,
ApiToken: tc.apiToken,
BoardId: tc.boardId,
},
Sources: config.Sources{},
}
Expand Down

0 comments on commit c24f985

Please sign in to comment.