From 11f265396a4c7c430bfadaa561221c204f585c06 Mon Sep 17 00:00:00 2001 From: Armand Grillet <2117580+armandgrillet@users.noreply.github.com> Date: Tue, 3 Jan 2023 11:09:04 +0100 Subject: [PATCH] Add a $__toDay() macro (#195) --- CHANGELOG.md | 3 +++ README.md | 9 +++++---- pkg/github/macros.go | 14 ++++++++++++++ pkg/github/macros_test.go | 9 ++++++++- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef58e07c..f88fd350 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index 85f270b7..e3be8921 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/pkg/github/macros.go b/pkg/github/macros.go index 4c282bc6..d7b593d7 100644 --- a/pkg/github/macros.go +++ b/pkg/github/macros.go @@ -4,7 +4,9 @@ import ( "errors" "fmt" "regexp" + "strconv" "strings" + "time" ) type macroFunc func(string, []string) (string, error) @@ -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) diff --git a/pkg/github/macros_test.go b/pkg/github/macros_test.go index 4c4edde2..df45537f 100644 --- a/pkg/github/macros_test.go +++ b/pkg/github/macros_test.go @@ -3,6 +3,7 @@ package github_test import ( "errors" "testing" + "time" "github.com/grafana/github-datasource/pkg/github" "github.com/stretchr/testify/assert" @@ -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) { @@ -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) }) } }