-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow sources to declare custom periods to be queried (#13)
* refactor & update configuration to contain custom query periods * update readme * polish readme * implement period check & update existing tests * add unit test cases for period-checking * add unit test for config package * fix test name * fix bug * polish
- Loading branch information
Showing
16 changed files
with
661 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/utkuufuk/entrello/internal/config" | ||
"github.com/utkuufuk/entrello/internal/github" | ||
"github.com/utkuufuk/entrello/internal/tododock" | ||
"github.com/utkuufuk/entrello/internal/trello" | ||
) | ||
|
||
// Source defines an interface for a Trello card source | ||
type Source interface { | ||
// IsEnabled returns true if the source is enabled by configuration. | ||
IsEnabled() bool | ||
|
||
// GetName returns a human-readable name of the source | ||
GetName() string | ||
|
||
// GetLabel returns the corresponding card label ID for the source | ||
GetLabel() string | ||
|
||
// GetPeriod returns the period in minutes that the source should be checked | ||
GetPeriod() config.Period | ||
|
||
// FetchNewCards returns a list of Trello cards to be inserted into the board from the source | ||
FetchNewCards() ([]trello.Card, error) | ||
} | ||
|
||
// getEnabledSourcesAndLabels returns a list of enabled sources & all relevant label IDs | ||
func getEnabledSourcesAndLabels(cfg config.Sources) (sources []Source, labels []string) { | ||
arr := []Source{ | ||
github.GetSource(context.Background(), cfg.GithubIssues), | ||
tododock.GetSource(cfg.TodoDock), | ||
} | ||
now := time.Now() | ||
|
||
for _, src := range arr { | ||
if ok, err := shouldQuery(src, now); !ok { | ||
if err != nil { | ||
// @todo: send telegram notification instead if enabled | ||
log.Printf("[-] could not check if '%s' should be queried or not, skipping", src.GetName()) | ||
} | ||
continue | ||
} | ||
sources = append(sources, src) | ||
labels = append(labels, src.GetLabel()) | ||
} | ||
return sources, labels | ||
} | ||
|
||
// shouldQuery checks if the given source should be queried at the given time | ||
func shouldQuery(src Source, now time.Time) (bool, error) { | ||
if !src.IsEnabled() { | ||
return false, nil | ||
} | ||
|
||
interval := src.GetPeriod().Interval | ||
if interval < 0 { | ||
return false, fmt.Errorf("period interval must be a positive integer, got: '%d'", interval) | ||
} | ||
|
||
switch src.GetPeriod().Type { | ||
case config.PERIOD_TYPE_DEFAULT: | ||
return true, nil | ||
case config.PERIOD_TYPE_DAY: | ||
if interval > 31 { | ||
return false, fmt.Errorf("daily interval cannot be more than 14, got: '%d'", interval) | ||
} | ||
return now.Day()%interval == 0 && now.Hour() == 0 && now.Minute() == 0, nil | ||
case config.PERIOD_TYPE_HOUR: | ||
if interval > 23 { | ||
return false, fmt.Errorf("hourly interval cannot be more than 23, got: '%d'", interval) | ||
} | ||
return now.Hour()%interval == 0 && now.Minute() == 0, nil | ||
case config.PERIOD_TYPE_MINUTE: | ||
if interval > 60 { | ||
return false, fmt.Errorf("minute interval cannot be more than 60, got: '%d'", interval) | ||
} | ||
return now.Minute()%interval == 0, nil | ||
} | ||
|
||
return false, fmt.Errorf("unrecognized source period type: '%s'", src.GetPeriod().Type) | ||
} |
Oops, something went wrong.