Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/sources/template-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ The GitHub data source provides macros that add dynamic parts to your queries. M

| Macro | Syntax | Description | Example |
|-------|--------|-------------|---------|
| **multiVar** | `$__multiVar(qualifier,$var)` | Expands a multi-value variable into GitHub search qualifiers. Each selected value is combined with the qualifier name in `qualifier:value` format. | `$__multiVar(label,$labels)` expands to `label:first-label label:second-label` |
| **multiVar** | `$__multiVar(qualifier,$var)` | Expands a multi-value variable into GitHub search qualifiers. Each selected value is combined with the qualifier name in `qualifier:value` format. Values that contain spaces are wrapped in double quotes, for example `label:"good first issue"`. | `$__multiVar(label,$labels)` expands to `label:first-label label:second-label` |
| **toDay** | `$__toDay(diff)` | Returns the current date in UTC, formatted as `YYYY-MM-DD`. An optional `diff` parameter offsets the date by the specified number of days. | `created:$__toDay(-7)` on 2022-01-17 expands to `created:2022-01-10` |

### Use multiVar with "All" selection
Expand Down
11 changes: 10 additions & 1 deletion pkg/github/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ func trimAll(s []string) []string {
return r
}

// quoteQualifierValue wraps a qualifier value that contains whitespace in double quotes,
// so GitHub search treats it as one value (ex: label:"good first issue")
func quoteQualifierValue(v string) string {
if !strings.ContainsAny(v, " \t") || (len(v) > 1 && strings.HasPrefix(v, `"`) && strings.HasSuffix(v, `"`)) {
return v
}
return `"` + v + `"`
Comment thread
adamyeats marked this conversation as resolved.
Resolved
}

// InterPolateMacros interpolate macros on a given query string
func InterPolateMacros(query string) (string, error) {
macros := map[string]macroFunc{
Expand All @@ -45,7 +54,7 @@ func InterPolateMacros(query string) (string, error) {
prop = arg
continue
}
out = strings.Trim(fmt.Sprintf("%s %s:%s", out, prop, arg), " ")
out = strings.Trim(fmt.Sprintf("%s %s:%s", out, prop, quoteQualifierValue(arg)), " ")
}
return out, nil
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/github/macros_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ 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: "is:open $__multiVar(label,good first issue,bug)", want: `is:open label:"good first issue" label:bug`},
{query: `is:open $__multiVar(label,"help wanted")`, want: `is:open label:"help wanted"`},
{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")},
Expand Down
Loading