From 3ad2c0de3fc9bb1442d1bc8e764c233745a12f35 Mon Sep 17 00:00:00 2001 From: Sho Mizutani Date: Thu, 2 Jan 2020 02:36:10 +0900 Subject: [PATCH 1/4] Update now to v1.1.1 --- src/go.mod | 2 +- src/go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/go.mod b/src/go.mod index 921bf71..814ac50 100644 --- a/src/go.mod +++ b/src/go.mod @@ -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 ) diff --git a/src/go.sum b/src/go.sum index 13e28bb..9d9c18e 100644 --- a/src/go.sum +++ b/src/go.sum @@ -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= From 4dfe7247db3be7608b7ba1595bf52d31db5400b1 Mon Sep 17 00:00:00 2001 From: Sho Mizutani Date: Thu, 2 Jan 2020 02:37:19 +0900 Subject: [PATCH 2/4] Separate date from issue.go --- src/date.go | 35 +++++++++++++++++++++++++++++++++++ src/issue.go | 28 +++++----------------------- 2 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 src/date.go diff --git a/src/date.go b/src/date.go new file mode 100644 index 0000000..819d31b --- /dev/null +++ b/src/date.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "time" + + "github.com/jinzhu/now" +) + +type date struct { + Year string + WeekStartDate string + WeekEndDate string + WeekNumber 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") + } + return d +} diff --git a/src/issue.go b/src/issue.go index 8bdc517..6e956e3 100644 --- a/src/issue.go +++ b/src/issue.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "errors" - "fmt" "io/ioutil" "os" "path/filepath" @@ -12,43 +11,26 @@ import ( "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 @@ -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 } From d5613f2269d11f275e600792bc4bd3e9504997a9 Mon Sep 17 00:00:00 2001 From: Sho Mizutani Date: Fri, 3 Jan 2020 13:21:29 +0900 Subject: [PATCH 3/4] Add WeekNumberYear --- src/date.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/date.go b/src/date.go index 819d31b..02a1ca1 100644 --- a/src/date.go +++ b/src/date.go @@ -8,11 +8,12 @@ import ( ) type date struct { - Year string - WeekStartDate string - WeekEndDate string - WeekNumber string - Dates [7]string + Year string + WeekStartDate string + WeekEndDate string + WeekNumber string + WeekNumberYear string + Dates [7]string } func NewDate(t time.Time) *date { @@ -31,5 +32,10 @@ func NewDate(t time.Time) *date { 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 } From d7130d25ec45cd67312236e5ff27a8d891871d18 Mon Sep 17 00:00:00 2001 From: Sho Mizutani Date: Fri, 3 Jan 2020 13:28:29 +0900 Subject: [PATCH 4/4] Add test for NewDate --- src/Makefile | 2 ++ src/date_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/Makefile create mode 100644 src/date_test.go diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..0ce7ec8 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,2 @@ +test: + go test -v . diff --git a/src/date_test.go b/src/date_test.go new file mode 100644 index 0000000..12eba42 --- /dev/null +++ b/src/date_test.go @@ -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) + } + } +}