Skip to content

Commit

Permalink
Merge pull request #13 from lowply/lowply/more-variables
Browse files Browse the repository at this point in the history
Allow formatting in the template
  • Loading branch information
lowply authored Jul 25, 2022
2 parents 2fa0abb + bd13916 commit 8c529a0
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -9,35 +9,39 @@ 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

```
---
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
Expand Down
45 changes: 45 additions & 0 deletions src/data.go
Original file line number Diff line number Diff line change
@@ -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
}
49 changes: 0 additions & 49 deletions src/date.go

This file was deleted.

4 changes: 2 additions & 2 deletions src/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions src/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

type issue struct {
*request
*date
*data
endpoint string
template string
}
Expand All @@ -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"))
Expand All @@ -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
}
Expand Down

0 comments on commit 8c529a0

Please sign in to comment.