Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
updater.Group CoolDown
Browse files Browse the repository at this point in the history
This is still vaporware, but with a new name.

I think it's more clear: check whenever, but if a PR was opened more
than this ago I don't want to see it.
  • Loading branch information
thepwagner committed Oct 8, 2020
1 parent de9929d commit e41197d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
58 changes: 45 additions & 13 deletions updater/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package updater
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"

"github.com/dependabot/gomodules-extracted/cmd/go/_internal_/semver"
)
Expand All @@ -17,30 +19,21 @@ type Group struct {

// Parameters that apply to members:
// Range is a comma separated list of allowed semver ranges
Range string `yaml:"range"`
Frequency Frequency `yaml:"frequency"`
Range string `yaml:"range"`
CoolDown string `yaml:"cooldown"`

compiledPattern *regexp.Regexp
}

type Frequency string

const (
FrequencyDaily Frequency = "daily"
FrequencyWeekly Frequency = "weekly"
)

func (g *Group) Validate() error {
if g.Name == "" {
return fmt.Errorf("groups must specify name")
}
if g.Pattern == "" {
return fmt.Errorf("groups must specify pattern")
}
switch g.Frequency {
case "", FrequencyDaily, FrequencyWeekly:
default:
return fmt.Errorf("frequency must be: [%s,%s]", FrequencyDaily, FrequencyWeekly)
if !durPattern.MatchString(g.CoolDown) {
return fmt.Errorf("invalid cooldown, expected ISO8601 duration: %q", g.CoolDown)
}

if strings.HasPrefix(g.Pattern, "/") && strings.HasSuffix(g.Pattern, "/") {
Expand Down Expand Up @@ -87,3 +80,42 @@ func cleanRange(rangeCond string, prefixLen int) string {
}
return s
}

var durPattern = regexp.MustCompile(`P?(((?P<year>\d+)Y)?((?P<month>\d+)M)?((?P<day>\d+)D)|(?P<week>\d+)W)?`)

const (
oneYear = 8766 * time.Hour
oneMonth = 730*time.Hour + 30*time.Minute
oneWeek = 7 * 24 * time.Hour
oneDay = 24 * time.Hour
)

func (g Group) CoolDownDuration() time.Duration {
m := durPattern.FindStringSubmatch(g.CoolDown)

var ret time.Duration
for i, name := range durPattern.SubexpNames() {
part := m[i]
if i == 0 || name == "" || part == "" {
continue
}

val, err := strconv.Atoi(part)
if err != nil {
return 0
}
valDur := time.Duration(val)
switch name {
case "year":
ret += valDur * oneYear
case "month":
ret += valDur * oneMonth
case "week":
ret += valDur * oneWeek
case "day":
ret += valDur * oneDay
}
}

return ret
}
21 changes: 21 additions & 0 deletions updater/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,32 @@ package updater_test
import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thepwagner/action-update/updater"
)

func TestGroup_CoolDownDuration(t *testing.T) {
g := updater.Group{Name: "test", Pattern: "test"}

cases := map[string]time.Duration{
"P1D": 24 * time.Hour,
"1D": 24 * time.Hour,
"1W": 7 * 24 * time.Hour,
}

for in, expected := range cases {
t.Run(in, func(t *testing.T) {
g.CoolDown = in
err := g.Validate()
require.NoError(t, err)
assert.Equal(t, expected, g.CoolDownDuration())
})
}
}

func TestGroup_InRange(t *testing.T) {
cases := map[string]struct {
included []string
Expand Down
8 changes: 3 additions & 5 deletions updater/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ func TestParseGroups(t *testing.T) {
frequency: weekly
range: ">=v1.4.0, <v2"`,
expected: updater.Groups{{
Name: "foo",
Pattern: "github.com/thepwagner",
Frequency: "weekly",
Range: ">=v1.4.0, <v2",
Name: "foo",
Pattern: "github.com/thepwagner",
Range: ">=v1.4.0, <v2",
}},
},
"multiple": {
Expand Down Expand Up @@ -83,7 +82,6 @@ func TestParseGroups(t *testing.T) {
for i, g := range groups {
assert.Equal(t, tc.expected[i].Name, g.Name)
assert.Equal(t, tc.expected[i].Pattern, g.Pattern)
assert.Equal(t, tc.expected[i].Frequency, g.Frequency)
assert.Equal(t, tc.expected[i].Range, g.Range)
}
}
Expand Down

0 comments on commit e41197d

Please sign in to comment.