From b74923b4147939c05e13b868ef57f05510df726b Mon Sep 17 00:00:00 2001 From: "m.huber" Date: Mon, 19 Feb 2024 23:03:13 +0100 Subject: [PATCH 01/32] format tables --- .../issue-pull-request-templates.en-us.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/content/usage/issue-pull-request-templates.en-us.md b/docs/content/usage/issue-pull-request-templates.en-us.md index 34475e346588..9bd3c17bb00b 100644 --- a/docs/content/usage/issue-pull-request-templates.en-us.md +++ b/docs/content/usage/issue-pull-request-templates.en-us.md @@ -291,15 +291,15 @@ contact_links: ### Possible Options -| Key | Description | Type | Default | -|----------------------|-------------------------------------------------------------------------------------------------------|--------------------|----------------| -| blank_issues_enabled | If set to false, the User is forced to use a Template | Boolean | true | -| contact_links | Custom Links to show in the Choose Box | Contact Link Array | Empty Array | +| Key | Description | Type | Default | +|----------------------|-------------------------------------------------------|--------------------|-------------| +| blank_issues_enabled | If set to false, the User is forced to use a Template | Boolean | true | +| contact_links | Custom Links to show in the Choose Box | Contact Link Array | Empty Array | ### Contact Link -| Key | Description | Type | Required | -|----------------------|-------------------------------------------------------------------------------------------------------|---------|----------| -| name | the name of your link | String | true | -| url | The URL of your Link | String | true | -| about | A short description of your Link | String | true | +| Key | Description | Type | Required | +|-------|----------------------------------|--------|----------| +| name | the name of your link | String | true | +| url | The URL of your Link | String | true | +| about | A short description of your Link | String | true | From f451c378f8e092fbfe54874a71fbb4b32a0db6e7 Mon Sep 17 00:00:00 2001 From: "m.huber" Date: Mon, 19 Feb 2024 23:14:19 +0100 Subject: [PATCH 02/32] document feature first :D --- .../issue-pull-request-templates.en-us.md | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/docs/content/usage/issue-pull-request-templates.en-us.md b/docs/content/usage/issue-pull-request-templates.en-us.md index 9bd3c17bb00b..411fa067361b 100644 --- a/docs/content/usage/issue-pull-request-templates.en-us.md +++ b/docs/content/usage/issue-pull-request-templates.en-us.md @@ -190,13 +190,15 @@ body: ### Markdown -You can use a `markdown` element to display Markdown in your form that provides extra context to the user, but is not submitted. +You can use a `markdown` element to display Markdown in your form that provides extra context to the user, but is not submitted by default. Attributes: -| Key | Description | Required | Type | Default | Valid values | -|-------|--------------------------------------------------------------|----------|--------|---------|--------------| -| value | The text that is rendered. Markdown formatting is supported. | Required | String | - | - | +| Key | Description | Required | Type | Default | Valid values | +|--------|--------------------------------------------------------------|----------|---------|---------|--------------| +| value | The text that is rendered. Markdown formatting is supported. | Required | String | - | - | +| submit | The text that is rendered, will be submitted. | Optional | Boolean | false | - | +| hide | Do not render in the from. | Optional | Boolean | false | - | ### Textarea @@ -264,11 +266,13 @@ You can use the `checkboxes` element to add a set of checkboxes to your form. Attributes: -| Key | Description | Required | Type | Default | Valid values | -|-------------|-------------------------------------------------------------------------------------------------------|----------|--------|--------------|--------------| -| label | A brief description of the expected user input, which is displayed in the form. | Required | String | - | - | -| description | A description of the set of checkboxes, which is displayed in the form. Supports Markdown formatting. | Optional | String | Empty String | - | -| options | An array of checkboxes that the user can select. For syntax, see below. | Required | Array | - | - | +| Key | Description | Required | Type | Default | Valid values | +|-------------|-------------------------------------------------------------------------------------------------------|----------|---------|--------------|--------------| +| label | A brief description of the expected user input, which is displayed in the form. | Required | String | - | - | +| description | A description of the set of checkboxes, which is displayed in the form. Supports Markdown formatting. | Optional | String | Empty String | - | +| options | An array of checkboxes that the user can select. For syntax, see below. | Required | Array | - | - | +| submit | The checkboxes are rendered, will be submitted. | Optional | Boolean | true | - | +| hide | Do not render in the from. | Optional | Boolean | false | - | For each value in the options array, you can set the following keys. @@ -276,6 +280,8 @@ For each value in the options array, you can set the following keys. |----------|------------------------------------------------------------------------------------------------------------------------------------------|----------|---------|---------|---------| | label | The identifier for the option, which is displayed in the form. Markdown is supported for bold or italic text formatting, and hyperlinks. | Required | String | - | - | | required | Prevents form submission until element is completed. | Optional | Boolean | false | - | +| submit | The specific checkbox that is rendered, will be submitted. | Optional | Boolean | true | - | +| hide | Do not render in the from. | Optional | Boolean | false | - | ## Syntax for issue config From a5e7a03850e2002ef0f6d70cc430fd60aab8f130 Mon Sep 17 00:00:00 2001 From: "m.huber" Date: Tue, 20 Feb 2024 00:09:24 +0100 Subject: [PATCH 03/32] fix docu and add support in validation --- .../issue-pull-request-templates.en-us.md | 35 ++++++++++++------- modules/issue/template/template.go | 12 +++++++ modules/issue/template/unmarshal.go | 15 ++++++++ modules/structs/issue.go | 2 ++ 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/docs/content/usage/issue-pull-request-templates.en-us.md b/docs/content/usage/issue-pull-request-templates.en-us.md index 411fa067361b..168e13ce8c1a 100644 --- a/docs/content/usage/issue-pull-request-templates.en-us.md +++ b/docs/content/usage/issue-pull-request-templates.en-us.md @@ -194,11 +194,12 @@ You can use a `markdown` element to display Markdown in your form that provides Attributes: -| Key | Description | Required | Type | Default | Valid values | -|--------|--------------------------------------------------------------|----------|---------|---------|--------------| -| value | The text that is rendered. Markdown formatting is supported. | Required | String | - | - | -| submit | The text that is rendered, will be submitted. | Optional | Boolean | false | - | -| hide | Do not render in the from. | Optional | Boolean | false | - | +| Key | Description | Required | Type | Default | Valid values | +|-------|--------------------------------------------------------------|----------|--------|---------|--------------| +| value | The text that is rendered. Markdown formatting is supported. | Required | String | - | - | + +Submit: Default is **false** +Hide: Default is **false** ### Textarea @@ -220,6 +221,9 @@ Validations: |----------|------------------------------------------------------|----------|---------|---------|--------------| | required | Prevents form submission until element is completed. | Optional | Boolean | false | - | +Submit: Default is **true** +Hide: Default is **false** + ### Input You can use an `input` element to add a single-line text field to your form. @@ -241,6 +245,9 @@ Validations: | is_number | Prevents form submission until element is filled with a number. | Optional | Boolean | false | - | | regex | Prevents form submission until element is filled with a value that match the regular expression. | Optional | String | - | a [regular expression](https://en.wikipedia.org/wiki/Regular_expression) | +Submit: Default is **true** +Hide: Default is **false** + ### Dropdown You can use a `dropdown` element to add a dropdown menu in your form. @@ -260,19 +267,20 @@ Validations: |----------|------------------------------------------------------|----------|---------|---------|--------------| | required | Prevents form submission until element is completed. | Optional | Boolean | false | - | +Submit: Default is **true** +Hide: Default is **false** + ### Checkboxes You can use the `checkboxes` element to add a set of checkboxes to your form. Attributes: -| Key | Description | Required | Type | Default | Valid values | -|-------------|-------------------------------------------------------------------------------------------------------|----------|---------|--------------|--------------| -| label | A brief description of the expected user input, which is displayed in the form. | Required | String | - | - | -| description | A description of the set of checkboxes, which is displayed in the form. Supports Markdown formatting. | Optional | String | Empty String | - | -| options | An array of checkboxes that the user can select. For syntax, see below. | Required | Array | - | - | -| submit | The checkboxes are rendered, will be submitted. | Optional | Boolean | true | - | -| hide | Do not render in the from. | Optional | Boolean | false | - | +| Key | Description | Required | Type | Default | Valid values | +|-------------|-------------------------------------------------------------------------------------------------------|----------|--------|--------------|--------------| +| label | A brief description of the expected user input, which is displayed in the form. | Required | String | - | - | +| description | A description of the set of checkboxes, which is displayed in the form. Supports Markdown formatting. | Optional | String | Empty String | - | +| options | An array of checkboxes that the user can select. For syntax, see below. | Required | Array | - | - | For each value in the options array, you can set the following keys. @@ -283,6 +291,9 @@ For each value in the options array, you can set the following keys. | submit | The specific checkbox that is rendered, will be submitted. | Optional | Boolean | true | - | | hide | Do not render in the from. | Optional | Boolean | false | - | +Submit: Default is **true** +Hide: Default is **false** + ## Syntax for issue config This is a example for a issue config file diff --git a/modules/issue/template/template.go b/modules/issue/template/template.go index 4e813fc91f78..85b6b01658bc 100644 --- a/modules/issue/template/template.go +++ b/modules/issue/template/template.go @@ -177,6 +177,18 @@ func validateOptions(field *api.IssueFormField, idx int) error { return position.Errorf("'required' should be a bool") } } + + if submit, ok := opt["submit"]; ok { + if _, ok := submit.(bool); !ok { + return position.Errorf("'submit' should be a bool") + } + } + + if hide, ok := opt["hide"]; ok { + if _, ok := hide.(bool); !ok { + return position.Errorf("'required' should be a bool") + } + } } } return nil diff --git a/modules/issue/template/unmarshal.go b/modules/issue/template/unmarshal.go index 8cae8d4c4299..44b2b553dfa1 100644 --- a/modules/issue/template/unmarshal.go +++ b/modules/issue/template/unmarshal.go @@ -128,9 +128,24 @@ func unmarshal(filename string, content []byte) (*api.IssueTemplate, error) { } } for i, v := range it.Fields { + // set default id value if v.ID == "" { v.ID = strconv.Itoa(i) } + // set default submit value + if v.Submit == nil { + submit := true + // markdown is not submitted by default + if v.Type == api.IssueFormFieldTypeMarkdown { + submit = false + } + v.Submit = &submit + } + // set default hide value + if v.Hide == nil { + hide := false + v.Hide = &hide + } } } diff --git a/modules/structs/issue.go b/modules/structs/issue.go index 34eae693299a..06c92f0198b5 100644 --- a/modules/structs/issue.go +++ b/modules/structs/issue.go @@ -145,6 +145,8 @@ type IssueFormField struct { ID string `json:"id" yaml:"id"` Attributes map[string]any `json:"attributes" yaml:"attributes"` Validations map[string]any `json:"validations" yaml:"validations"` + Submit *bool `json:"submit,omitempty" yaml:"submit,omitempty"` + Hide *bool `json:"hide,omitempty" yaml:"hide,omitempty"` } // IssueTemplate represents an issue template for a repository From 7913baede6baf99a9072d8722eb0299a7f39b2d3 Mon Sep 17 00:00:00 2001 From: "m.huber" Date: Tue, 20 Feb 2024 02:23:57 +0100 Subject: [PATCH 04/32] make checkbox able to hide and to not be subbmitted --- modules/issue/template/template.go | 26 +++++++++++++++++++-- templates/repo/issue/fields/checkboxes.tmpl | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/modules/issue/template/template.go b/modules/issue/template/template.go index 85b6b01658bc..b0cd02c9564d 100644 --- a/modules/issue/template/template.go +++ b/modules/issue/template/template.go @@ -265,8 +265,16 @@ type valuedField struct { } func (f *valuedField) WriteTo(builder *strings.Builder) { - if f.Type == api.IssueFormFieldTypeMarkdown { - // markdown blocks do not appear in output + // make sure submit is not nil + if f.Submit == nil { + submit := true + // markdown blocks do not appear in output by default + if f.Type == api.IssueFormFieldTypeMarkdown { + submit = false + } + f.Submit = &submit + } + if !*f.Submit { return } @@ -281,6 +289,9 @@ func (f *valuedField) WriteTo(builder *strings.Builder) { switch f.Type { case api.IssueFormFieldTypeCheckboxes: for _, option := range f.Options() { + if !option.Submit() { + continue + } checked := " " if option.IsChecked() { checked = "x" @@ -397,6 +408,17 @@ func (o *valuedOption) IsChecked() bool { return false } +func (o *valuedOption) Submit() bool { + if o.field.Type == api.IssueFormFieldTypeCheckboxes { + if vs, ok := o.data.(map[string]any); ok { + if v, ok := vs["submit"].(bool); ok { + return v + } + } + } + return true +} + var minQuotesRegex = regexp.MustCompilePOSIX("^`{3,}") // minQuotes return 3 or more back-quotes. diff --git a/templates/repo/issue/fields/checkboxes.tmpl b/templates/repo/issue/fields/checkboxes.tmpl index 237f2eb5dd93..c0bcc7b9308b 100644 --- a/templates/repo/issue/fields/checkboxes.tmpl +++ b/templates/repo/issue/fields/checkboxes.tmpl @@ -2,7 +2,7 @@ {{template "repo/issue/fields/header" .}} {{range $i, $opt := .item.Attributes.options}}
-
+
From e01a7c0dba594aab9d10f9dd5b38d9afa0e0a947 Mon Sep 17 00:00:00 2001 From: "m.huber" Date: Tue, 20 Feb 2024 02:44:19 +0100 Subject: [PATCH 05/32] make optional markdown submit work --- modules/issue/template/template.go | 31 ++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/modules/issue/template/template.go b/modules/issue/template/template.go index b0cd02c9564d..82edcc6f4f79 100644 --- a/modules/issue/template/template.go +++ b/modules/issue/template/template.go @@ -250,7 +250,7 @@ func RenderToMarkdown(template *api.IssueTemplate, values url.Values) string { IssueFormField: field, Values: values, } - if f.ID == "" { + if f.ID == "" || f.NoSubmit() { continue } f.WriteTo(builder) @@ -265,19 +265,6 @@ type valuedField struct { } func (f *valuedField) WriteTo(builder *strings.Builder) { - // make sure submit is not nil - if f.Submit == nil { - submit := true - // markdown blocks do not appear in output by default - if f.Type == api.IssueFormFieldTypeMarkdown { - submit = false - } - f.Submit = &submit - } - if !*f.Submit { - return - } - // write label if !f.HideLabel() { _, _ = fmt.Fprintf(builder, "### %s\n\n", f.Label()) @@ -325,6 +312,10 @@ func (f *valuedField) WriteTo(builder *strings.Builder) { } else { _, _ = fmt.Fprintf(builder, "%s\n", value) } + case api.IssueFormFieldTypeMarkdown: + if v, ok := f.Attributes["value"].(string); ok { + _, _ = fmt.Fprint(builder, v) + } } _, _ = fmt.Fprintln(builder) } @@ -336,7 +327,19 @@ func (f *valuedField) Label() string { return "" } +func (f *valuedField) NoSubmit() bool { + // handle default case + if f.Submit == nil { + // markdown blocks do not appear in output by default + return f.Type == api.IssueFormFieldTypeMarkdown + } + return !*f.Submit +} + func (f *valuedField) HideLabel() bool { + if f.Type == api.IssueFormFieldTypeMarkdown { + return true + } if label, ok := f.Attributes["hide_label"].(bool); ok { return label } From f73b4449b6060b1d604a54652b6501071b964757 Mon Sep 17 00:00:00 2001 From: "m.huber" Date: Tue, 20 Feb 2024 03:07:42 +0100 Subject: [PATCH 06/32] make hide option work --- modules/templates/helper.go | 9 +++++++++ templates/repo/issue/fields/checkboxes.tmpl | 2 +- templates/repo/issue/fields/dropdown.tmpl | 2 +- templates/repo/issue/fields/input.tmpl | 2 +- templates/repo/issue/fields/markdown.tmpl | 2 +- templates/repo/issue/fields/textarea.tmpl | 2 +- 6 files changed, 14 insertions(+), 5 deletions(-) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 6e42594b0b7d..cab164582941 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -176,9 +176,18 @@ func NewFuncMap() template.FuncMap { "FilenameIsImage": FilenameIsImage, "TabSizeClass": TabSizeClass, + "BoolPtrVal": PtrVal[bool], } } +// PtrVal return the value of an pointer or init a new empty one based on pointer type +func PtrVal[T any](ptr *T) T { + if ptr == nil { + ptr = new(T) + } + return *ptr +} + // Safe render raw as HTML func Safe(s any) template.HTML { switch v := s.(type) { diff --git a/templates/repo/issue/fields/checkboxes.tmpl b/templates/repo/issue/fields/checkboxes.tmpl index c0bcc7b9308b..015fac6cc42a 100644 --- a/templates/repo/issue/fields/checkboxes.tmpl +++ b/templates/repo/issue/fields/checkboxes.tmpl @@ -1,4 +1,4 @@ -
+
{{template "repo/issue/fields/header" .}} {{range $i, $opt := .item.Attributes.options}}
diff --git a/templates/repo/issue/fields/dropdown.tmpl b/templates/repo/issue/fields/dropdown.tmpl index 23aa373cd209..914da5d75488 100644 --- a/templates/repo/issue/fields/dropdown.tmpl +++ b/templates/repo/issue/fields/dropdown.tmpl @@ -1,4 +1,4 @@ -
+
{{template "repo/issue/fields/header" .}} {{/* FIXME: required validation */}}