Skip to content

Commit

Permalink
Merge pull request #8 from lowply/fix-year-bug
Browse files Browse the repository at this point in the history
Fix week numbering bug
  • Loading branch information
lowply authored Jan 3, 2020
2 parents 15639d2 + d7130d2 commit 6ac512d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
go test -v .
41 changes: 41 additions & 0 deletions src/date.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"time"

"github.com/jinzhu/now"
)

type date struct {
Year string
WeekStartDate string
WeekEndDate string
WeekNumber string
WeekNumberYear string
Dates [7]string
}

func NewDate(t time.Time) *date {
d := &date{}

nc := &now.Config{
WeekStartDay: time.Monday,
}
n := nc.With(t)

d.Year = n.BeginningOfYear().Format("2006")
d.WeekEndDate = n.EndOfSunday().Format("01/02")
d.WeekStartDate = n.BeginningOfWeek().Format("01/02")
_, isoweek := n.Monday().ISOWeek()
d.WeekNumber = fmt.Sprintf("%02d", isoweek)
for j := range d.Dates {
d.Dates[j] = n.Monday().AddDate(0, 0, j).Format("01/02")
}
// Thursday of the week, should be used with the week number
// e.g. "2020 Week 01".
// See https://en.wikipedia.org/wiki/ISO_week_date#First_week
// for the ISO 8601 first week definition
d.WeekNumberYear = n.BeginningOfWeek().AddDate(0, 0, 3).Format("2006")
return d
}
45 changes: 45 additions & 0 deletions src/date_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"testing"
"time"
)

type testCase struct {
now string
should string
}

func TestNewDate(t *testing.T) {
testCases := []testCase{
// Monday when Jan 1st is Monday
{now: "2018-01-01T00:00:00Z", should: "2018 Week 01"},
// Monday when Jan 1st is Tuesday
{now: "2018-12-31T00:00:00Z", should: "2019 Week 01"},
// Monday when Jan 1st is Wednesday
{now: "2019-12-30T00:00:00Z", should: "2020 Week 01"},
// Monday when Jan 1st is Thursday
{now: "2025-12-29T00:00:00Z", should: "2026 Week 01"},
// Monday when Jan 1st is Friday
{now: "2020-12-28T00:00:00Z", should: "2020 Week 53"},
// Monday when Jan 1st is Saturday
{now: "2021-12-27T00:00:00Z", should: "2021 Week 52"},
// Monday when Jan 1st is Saturday and it's a leap year
{now: "2032-12-27T00:00:00Z", should: "2032 Week 53"},
// Monday when Jan 1st is Sunday
{now: "2022-12-26T00:00:00Z", should: "2022 Week 52"},
}

for _, v := range testCases {
t.Logf("Testing %v...", v.now)
now, err := time.Parse(time.RFC3339, v.now)
if err != nil {
t.Fatal(err)
}
d := NewDate(now)
current := d.WeekNumberYear + " Week " + d.WeekNumber
if current != v.should {
t.Errorf("Actual: %v, Should: %v\n", current, v.should)
}
}
}
2 changes: 1 addition & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/lowply/issue-from-template
go 1.12

require (
github.com/jinzhu/now v1.0.0
github.com/jinzhu/now v1.1.1
gopkg.in/yaml.v2 v2.2.2
)
4 changes: 2 additions & 2 deletions src/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/jinzhu/now v1.0.0 h1:6WV8LvwPpDhKjo5U9O6b4+xdG/jTXNPwlDme/MTo8Ns=
github.com/jinzhu/now v1.0.0/go.mod h1:oHTiXerJ20+SfYcrdlBO7rzZRJWGwSTQ0iUY2jI6Gfc=
github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
Expand Down
28 changes: 5 additions & 23 deletions src/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,33 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"time"

"github.com/jinzhu/now"
yaml "gopkg.in/yaml.v2"
)

type issue struct {
*request
*date
endpoint string
template string
}

func NewIssue() *issue {
r := NewRequest(201)
i := &issue{request: r}
i := &issue{}
i.request = NewRequest(201)
i.date = NewDate(time.Now())
i.endpoint = "https://api.github.com/repos/" + os.Getenv("GITHUB_REPOSITORY") + "/issues"
i.template = filepath.Join(os.Getenv("GITHUB_WORKSPACE"), ".github", "ISSUE_TEMPLATE", os.Getenv("IFT_TEMPLATE_NAME"))
return i
}

func (i *issue) parseTemplate() (string, error) {
d := &struct {
Year string
WeekStartDate string
WeekEndDate string
WeekNumber string
Dates [7]string
}{}

now.WeekStartDay = time.Monday
d.Year = now.BeginningOfYear().Format("2006")
d.WeekEndDate = now.EndOfSunday().Format("01/02")
d.WeekStartDate = now.BeginningOfWeek().Format("01/02")
_, isoweek := now.Monday().ISOWeek()
d.WeekNumber = fmt.Sprintf("%02d", isoweek)
for i, _ := range d.Dates {
d.Dates[i] = now.Monday().AddDate(0, 0, i).Format("01/02")
}

file, err := ioutil.ReadFile(i.template)
if err != nil {
return "", err
Expand All @@ -60,7 +42,7 @@ func (i *issue) parseTemplate() (string, error) {
}

b := new(bytes.Buffer)
err = t.Execute(b, d)
err = t.Execute(b, i.date)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 6ac512d

Please sign in to comment.