diff --git a/Dockerfile b/Dockerfile index 2ade5db..d42d126 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ ENV GOOS=linux ENV GOARCH=amd64 WORKDIR /go/src COPY src . -RUN GO111MODULE=on go build -o /go/bin/main +RUN go build -o /go/bin/main FROM alpine RUN apk add --no-cache ca-certificates diff --git a/README.md b/README.md index 8a1f93f..984b8d5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Issue From Template -This action opens a new issue from an issue template. It parses the template's front matter and the body, then posts [an API request to open an issue](https://developer.github.com/v3/issues/#create-an-issue). Works best with a [scheduled workflow](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#scheduled-events-schedule) and the [Auto Closer](https://github.com/lowply/auto-closer) action. +This action opens a new issue from an issue template. It parses the template's front matter and the body, then posts [an API request to open an issue](https://docs.github.com/en/rest/issues/issues#create-an-issue). Works best with a [scheduled workflow](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule) and the [Auto Closer](https://github.com/lowply/auto-closer) action. ## Environment variables @@ -9,14 +9,18 @@ This action opens a new issue from an issue template. It parses the template's f ## Available template variables -- `.Year`: Year of the day when this action runs -- `.Month`: Month of the day when this action runs -- `.Day`: Day when this action runs -- `.WeekStartDate`: Date of Monday of the week (MM/DD) -- `.WeekEndDate`: Date of Sunday of the week (MM/DD) -- `.WeekNumber`: ISO week number -- `.WeekNumberYear`: Year of the Thursday of the week. Matches with [ISO week number](https://en.wikipedia.org/wiki/ISO_week_date#First_week) -- `.Dates`: Array of the dates of the week (Can be used as `{{ index .Dates 1 }}` in the template) +- `.Current`: The day when this action runs (time.Time) +- `.WeekStart`: Date of Monday of the week (time.Time) +- `.WeekEnd`: Date of Sunday of the week (time.Time) +- `.WeekNumber`: ISO week number (string) +- `.YearOfTheWeek`: Year of the Thursday of the week. Matches with [ISO week number](https://en.wikipedia.org/wiki/ISO_week_date#First_week) (string) +- `.Dates`: Array of the dates of the week (Can be used as `{{ index .Dates 1 }}` in the template, array of time.Time) + +For variables that are in the `time.Time` type, you can pick your preferred format in the template e.g. `.Format "2006-01-02"`. See https://pkg.go.dev/time#Time.Format for more details. + +### Date and time formatting layout + +If you're not familiar with Go's time.Time layouts, there are other resources you can use e.g. [Date and time format in Go (Golang) cheatsheet](https://gosamples.dev/date-time-format-cheatsheet/) but in short, **Monday, Jan 2nd, 2006** is the day used to express any formatting. So for example, if you want to format your date in `YYYY-MM-DD`, the format would be `2006-01-02`. ## Template example @@ -24,20 +28,20 @@ This action opens a new issue from an issue template. It parses the template's f --- name: Weekly Report about: This is an example -title: 'Report for Week {{ .WeekNumber }}, {{ .WeekNumberYear }} (Week of {{ .WeekStartDate }})' +title: 'Report for Week {{ .WeekNumber }}, {{ .YearOfTheWeek }} (Week of {{ .WeekStartDate.Format "2006/01/02" }})' labels: report assignees: lowply --- # This week's updates! -## {{ index .Dates 0 }} MON -## {{ index .Dates 1 }} TUE -## {{ index .Dates 2 }} WED -## {{ index .Dates 3 }} THU -## {{ index .Dates 4 }} FRI -## {{ index .Dates 5 }} SAT -## {{ index .Dates 6 }} SUN +## {{ (index .Dates 0).Format "01/02 Mon" }} +## {{ (index .Dates 1).Format "01/02 Mon" }} +## {{ (index .Dates 2).Format "01/02 Mon" }} +## {{ (index .Dates 3).Format "01/02 Mon" }} +## {{ (index .Dates 4).Format "01/02 Mon" }} +## {{ (index .Dates 5).Format "01/02 Mon" }} +## {{ (index .Dates 6).Format "01/02 Mon" }} ``` ## Default comments diff --git a/src/data.go b/src/data.go new file mode 100644 index 0000000..544124b --- /dev/null +++ b/src/data.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + "time" + + "github.com/jinzhu/now" +) + +type data struct { + Current time.Time + WeekStart time.Time + WeekEnd time.Time + WeekNumber string + YearOfTheWeek string + Dates [7]time.Time +} + +func NewData(t time.Time) *data { + d := &data{} + + nc := &now.Config{ + WeekStartDay: time.Monday, + } + n := nc.With(t) + + // https://github.com/jinzhu/now#mondaysunday + d.Current = t + d.WeekStart = n.Monday() + d.WeekEnd = n.Sunday() + + _, isoweek := n.Monday().ISOWeek() + d.WeekNumber = fmt.Sprintf("%02d", isoweek) + + // 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.YearOfTheWeek = n.BeginningOfWeek().AddDate(0, 0, 3).Format("2006") + + for j := range d.Dates { + d.Dates[j] = n.Monday().AddDate(0, 0, j) + } + return d +} diff --git a/src/date.go b/src/date.go deleted file mode 100644 index d5a85bc..0000000 --- a/src/date.go +++ /dev/null @@ -1,49 +0,0 @@ -package main - -import ( - "fmt" - "strconv" - "time" - - "github.com/jinzhu/now" -) - -type date struct { - Year string - Month string - Day 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 = strconv.Itoa(n.Year()) - d.Month = fmt.Sprintf("%02d", int(n.Month())) - d.Day = fmt.Sprintf("%02d", n.Day()) - - // https://github.com/jinzhu/now#mondaysunday - d.WeekStartDate = n.Monday().Format("01/02") - d.WeekEndDate = n.Sunday().Format("01/02") - - _, isoweek := n.Monday().ISOWeek() - d.WeekNumber = fmt.Sprintf("%02d", isoweek) - // 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") - for j := range d.Dates { - d.Dates[j] = n.Monday().AddDate(0, 0, j).Format("01/02") - } - return d -} diff --git a/src/date_test.go b/src/date_test.go index 1f4931f..d167f4d 100644 --- a/src/date_test.go +++ b/src/date_test.go @@ -39,8 +39,8 @@ func TestNewDate(t *testing.T) { if err != nil { t.Fatal(err) } - d := NewDate(now) - current := fmt.Sprintf("%v Week %v, Week of %v. Ends at %v. Year %v, Month %v, Day %v", d.WeekNumberYear, d.WeekNumber, d.WeekStartDate, d.WeekEndDate, d.Year, d.Month, d.Day) + d := NewData(now) + current := fmt.Sprintf("%v Week %v, Week of %v. Ends at %v. Year %v, Month %v, Day %v", d.YearOfTheWeek, d.WeekNumber, d.WeekStart.Format("01/02"), d.WeekEnd.Format("01/02"), d.Current.Format("2006"), d.Current.Format("01"), d.Current.Format("02")) if current != v.should { t.Fatalf("Actual: %v, Should: %v\n", current, v.should) } diff --git a/src/issue.go b/src/issue.go index 131ad19..4912b69 100644 --- a/src/issue.go +++ b/src/issue.go @@ -17,7 +17,7 @@ import ( type issue struct { *request - *date + *data endpoint string template string } @@ -26,13 +26,13 @@ func NewIssue() *issue { i := &issue{} i.request = NewRequest(201) if os.Getenv("ADD_DATES") == "" { - i.date = NewDate(time.Now()) + i.data = NewData(time.Now()) } else { dates, err := strconv.Atoi(os.Getenv("ADD_DATES")) if err != nil { return nil } - i.date = NewDate(time.Now().AddDate(0, 0, dates)) + i.data = NewData(time.Now().AddDate(0, 0, dates)) } 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")) @@ -51,7 +51,7 @@ func (i *issue) parseTemplate() (string, error) { } b := new(bytes.Buffer) - err = t.Execute(b, i.date) + err = t.Execute(b, i.data) if err != nil { return "", err }