loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Support setting the `default` attribute of the issue template dropdown field (#31045)

Fix #31044

According to [GitHub issue template
documentation](https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/syntax-for-githubs-form-schema#attributes-for-dropdown),
the `default` attribute can be used to specify the preselected option
for a dropdown field.

(cherry picked from commit 7ab0988af140aa3e0204979765f75961f1dc9c11)

authored by

Zettat123 and committed by
Earl Warren
df15abd0 d6e454c3

+118 -1
+25
modules/issue/template/template.go
··· 91 91 if err := validateOptions(field, idx); err != nil { 92 92 return err 93 93 } 94 + if err := validateDropdownDefault(position, field.Attributes); err != nil { 95 + return err 96 + } 94 97 case api.IssueFormFieldTypeCheckboxes: 95 98 if err := validateStringItem(position, field.Attributes, false, "description"); err != nil { 96 99 return err ··· 246 249 return position.Errorf("'%s' should be a bool", name) 247 250 } 248 251 } 252 + return nil 253 + } 254 + 255 + func validateDropdownDefault(position errorPosition, attributes map[string]any) error { 256 + v, ok := attributes["default"] 257 + if !ok { 258 + return nil 259 + } 260 + defaultValue, ok := v.(int) 261 + if !ok { 262 + return position.Errorf("'default' should be an int") 263 + } 264 + 265 + options, ok := attributes["options"].([]any) 266 + if !ok { 267 + // should not happen 268 + return position.Errorf("'options' is required and should be a array") 269 + } 270 + if defaultValue < 0 || defaultValue >= len(options) { 271 + return position.Errorf("the value of 'default' is out of range") 272 + } 273 + 249 274 return nil 250 275 } 251 276
+92
modules/issue/template/template_test.go
··· 356 356 wantErr: "body[0](checkboxes), option[1]: can not require a hidden checkbox", 357 357 }, 358 358 { 359 + name: "dropdown default is not an integer", 360 + content: ` 361 + name: "test" 362 + about: "this is about" 363 + body: 364 + - type: dropdown 365 + id: "1" 366 + attributes: 367 + label: Label of dropdown 368 + description: Description of dropdown 369 + multiple: true 370 + options: 371 + - Option 1 of dropdown 372 + - Option 2 of dropdown 373 + - Option 3 of dropdown 374 + default: "def" 375 + validations: 376 + required: true 377 + `, 378 + wantErr: "body[0](dropdown): 'default' should be an int", 379 + }, 380 + { 381 + name: "dropdown default is out of range", 382 + content: ` 383 + name: "test" 384 + about: "this is about" 385 + body: 386 + - type: dropdown 387 + id: "1" 388 + attributes: 389 + label: Label of dropdown 390 + description: Description of dropdown 391 + multiple: true 392 + options: 393 + - Option 1 of dropdown 394 + - Option 2 of dropdown 395 + - Option 3 of dropdown 396 + default: 3 397 + validations: 398 + required: true 399 + `, 400 + wantErr: "body[0](dropdown): the value of 'default' is out of range", 401 + }, 402 + { 403 + name: "dropdown without default is valid", 404 + content: ` 405 + name: "test" 406 + about: "this is about" 407 + body: 408 + - type: dropdown 409 + id: "1" 410 + attributes: 411 + label: Label of dropdown 412 + description: Description of dropdown 413 + multiple: true 414 + options: 415 + - Option 1 of dropdown 416 + - Option 2 of dropdown 417 + - Option 3 of dropdown 418 + validations: 419 + required: true 420 + `, 421 + want: &api.IssueTemplate{ 422 + Name: "test", 423 + About: "this is about", 424 + Fields: []*api.IssueFormField{ 425 + { 426 + Type: "dropdown", 427 + ID: "1", 428 + Attributes: map[string]any{ 429 + "label": "Label of dropdown", 430 + "description": "Description of dropdown", 431 + "multiple": true, 432 + "options": []any{ 433 + "Option 1 of dropdown", 434 + "Option 2 of dropdown", 435 + "Option 3 of dropdown", 436 + }, 437 + }, 438 + Validations: map[string]any{ 439 + "required": true, 440 + }, 441 + Visible: []api.IssueFormFieldVisible{api.IssueFormFieldVisibleForm, api.IssueFormFieldVisibleContent}, 442 + }, 443 + }, 444 + FileName: "test.yaml", 445 + }, 446 + wantErr: "", 447 + }, 448 + { 359 449 name: "valid", 360 450 content: ` 361 451 name: Name ··· 399 489 - Option 1 of dropdown 400 490 - Option 2 of dropdown 401 491 - Option 3 of dropdown 492 + default: 1 402 493 validations: 403 494 required: true 404 495 - type: checkboxes ··· 475 566 "Option 2 of dropdown", 476 567 "Option 3 of dropdown", 477 568 }, 569 + "default": 1, 478 570 }, 479 571 Validations: map[string]any{ 480 572 "required": true,
+1 -1
templates/repo/issue/fields/dropdown.tmpl
··· 2 2 {{template "repo/issue/fields/header" .}} 3 3 {{/* FIXME: required validation */}} 4 4 <div class="ui fluid selection dropdown {{if .item.Attributes.multiple}}multiple clearable{{end}}"> 5 - <input type="hidden" name="form-field-{{.item.ID}}" value="0"> 5 + <input type="hidden" name="form-field-{{.item.ID}}" value="{{.item.Attributes.default}}"> 6 6 {{svg "octicon-triangle-down" 14 "dropdown icon"}} 7 7 {{if not .item.Validations.required}} 8 8 {{svg "octicon-x" 14 "remove icon"}}