Skip to content

Commit

Permalink
Add a $__toDay() macro (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
armandgrillet authored Jan 3, 2023
1 parent 9436951 commit 11f2653
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Entries

## [1.3.2] - next
- Added a `$__toDay()` macro support

## [1.3.1] 2022-12-21

- Chore - Updated go version to latest (1.19.4)
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ You can reference them inside queries, allowing users to configure parameters su

You can use the following macros in your queries

| Macro Name | Syntax | Description | Example |
| ---------- | -------------------------- | -------------------------------------------------------------------- | ------------------------------------------------------------------------------------ |
| multiVar | `$__multiVar(prefix,$var)` | Expands a multi value variable into github query string | `$__multiVar(label,$labels)` will expand into `label:first-label label:second-label` |
| | | When using **all** in multi variable, use **\*** as custom all value | |
| Macro Name | Syntax | Description | Example |
| ---------- | -------------------------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ |
| multiVar | `$__multiVar(prefix,$var)` | Expands a multi value variable into github query string | `$__multiVar(label,$labels)` will expand into `label:first-label label:second-label` |
| | | When using **all** in multi variable, use **\*** as custom all value | |
| day | `$__toDay(diff)` | Returns the day according to UTC time, a difference in days can be added | `created:$__toDay(-7)` on 2022-01-17 will expand into `created:2022-01-10` |

## Access Token Permissions

Expand Down
14 changes: 14 additions & 0 deletions pkg/github/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
)

type macroFunc func(string, []string) (string, error)
Expand Down Expand Up @@ -47,6 +49,18 @@ func InterPolateMacros(query string) (string, error) {
}
return out, nil
},
"toDay": func(query string, args []string) (string, error) {
diff := 0
if args[0] != "" {
var err error
diff, err = strconv.Atoi(args[0])
if err != nil {
return query, errors.New("argument for day is not an integer")
}
}
expectedDay := time.Now().UTC().AddDate(0, 0, diff)
return expectedDay.Format("2006-01-02"), nil
},
}
for key, macro := range macros {
matches, err := getMatches(key, query)
Expand Down
9 changes: 8 additions & 1 deletion pkg/github/macros_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github_test
import (
"errors"
"testing"
"time"

"github.com/grafana/github-datasource/pkg/github"
"github.com/stretchr/testify/assert"
Expand All @@ -24,6 +25,12 @@ func TestInterPolateMacros(t *testing.T) {
{query: "hello $__multiVar(repo,*) world", want: "hello world"},
{query: "hello $__multiVar(repo,a,b,c)", want: "hello repo:a repo:b repo:c"},
{query: "hello $__multiVar(repo,a,b,c) $__multiVar(label,c,b,a) world", want: "hello repo:a repo:b repo:c label:c label:b label:a world"},
{query: "created:$__toDay(today)", wantErr: errors.New("argument for day is not an integer")},
{query: "created:$__toDay()", want: "created:" + time.Now().UTC().Format("2006-01-02")},
{query: "$__toDay(0)", want: time.Now().UTC().Format("2006-01-02")},
{query: "$__toDay(1)", want: time.Now().UTC().AddDate(0, 0, 1).Format("2006-01-02")},
{query: "$__toDay(-1)", want: time.Now().UTC().AddDate(0, 0, -1).Format("2006-01-02")},
{query: "$__toDay(-14)..$__toDay(-7)", want: time.Now().UTC().AddDate(0, 0, -14).Format("2006-01-02") + ".." + time.Now().UTC().AddDate(0, 0, -7).Format("2006-01-02")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -33,7 +40,7 @@ func TestInterPolateMacros(t *testing.T) {
return
}
assert.Nil(t, err)
assert.Equal(t, got, tt.want)
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 11f2653

Please sign in to comment.