You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

template.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package template
  4. import (
  5. "fmt"
  6. "net/url"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "code.gitea.io/gitea/modules/container"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "gitea.com/go-chi/binding"
  13. )
  14. // Validate checks whether an IssueTemplate is considered valid, and returns the first error
  15. func Validate(template *api.IssueTemplate) error {
  16. if err := validateMetadata(template); err != nil {
  17. return err
  18. }
  19. if template.Type() == api.IssueTemplateTypeYaml {
  20. if err := validateYaml(template); err != nil {
  21. return err
  22. }
  23. }
  24. return nil
  25. }
  26. func validateMetadata(template *api.IssueTemplate) error {
  27. if strings.TrimSpace(template.Name) == "" {
  28. return fmt.Errorf("'name' is required")
  29. }
  30. if strings.TrimSpace(template.About) == "" {
  31. return fmt.Errorf("'about' is required")
  32. }
  33. return nil
  34. }
  35. func validateYaml(template *api.IssueTemplate) error {
  36. if len(template.Fields) == 0 {
  37. return fmt.Errorf("'body' is required")
  38. }
  39. ids := make(container.Set[string])
  40. for idx, field := range template.Fields {
  41. if err := validateID(field, idx, ids); err != nil {
  42. return err
  43. }
  44. if err := validateLabel(field, idx); err != nil {
  45. return err
  46. }
  47. position := newErrorPosition(idx, field.Type)
  48. switch field.Type {
  49. case api.IssueFormFieldTypeMarkdown:
  50. if err := validateStringItem(position, field.Attributes, true, "value"); err != nil {
  51. return err
  52. }
  53. case api.IssueFormFieldTypeTextarea:
  54. if err := validateStringItem(position, field.Attributes, false,
  55. "description",
  56. "placeholder",
  57. "value",
  58. "render",
  59. ); err != nil {
  60. return err
  61. }
  62. case api.IssueFormFieldTypeInput:
  63. if err := validateStringItem(position, field.Attributes, false,
  64. "description",
  65. "placeholder",
  66. "value",
  67. ); err != nil {
  68. return err
  69. }
  70. if err := validateBoolItem(position, field.Validations, "is_number"); err != nil {
  71. return err
  72. }
  73. if err := validateStringItem(position, field.Validations, false, "regex"); err != nil {
  74. return err
  75. }
  76. case api.IssueFormFieldTypeDropdown:
  77. if err := validateStringItem(position, field.Attributes, false, "description"); err != nil {
  78. return err
  79. }
  80. if err := validateBoolItem(position, field.Attributes, "multiple"); err != nil {
  81. return err
  82. }
  83. if err := validateOptions(field, idx); err != nil {
  84. return err
  85. }
  86. case api.IssueFormFieldTypeCheckboxes:
  87. if err := validateStringItem(position, field.Attributes, false, "description"); err != nil {
  88. return err
  89. }
  90. if err := validateOptions(field, idx); err != nil {
  91. return err
  92. }
  93. default:
  94. return position.Errorf("unknown type")
  95. }
  96. if err := validateRequired(field, idx); err != nil {
  97. return err
  98. }
  99. }
  100. return nil
  101. }
  102. func validateLabel(field *api.IssueFormField, idx int) error {
  103. if field.Type == api.IssueFormFieldTypeMarkdown {
  104. // The label is not required for a markdown field
  105. return nil
  106. }
  107. return validateStringItem(newErrorPosition(idx, field.Type), field.Attributes, true, "label")
  108. }
  109. func validateRequired(field *api.IssueFormField, idx int) error {
  110. if field.Type == api.IssueFormFieldTypeMarkdown || field.Type == api.IssueFormFieldTypeCheckboxes {
  111. // The label is not required for a markdown or checkboxes field
  112. return nil
  113. }
  114. return validateBoolItem(newErrorPosition(idx, field.Type), field.Validations, "required")
  115. }
  116. func validateID(field *api.IssueFormField, idx int, ids container.Set[string]) error {
  117. if field.Type == api.IssueFormFieldTypeMarkdown {
  118. // The ID is not required for a markdown field
  119. return nil
  120. }
  121. position := newErrorPosition(idx, field.Type)
  122. if field.ID == "" {
  123. // If the ID is empty in yaml, template.Unmarshal will auto autofill it, so it cannot be empty
  124. return position.Errorf("'id' is required")
  125. }
  126. if binding.AlphaDashPattern.MatchString(field.ID) {
  127. return position.Errorf("'id' should contain only alphanumeric, '-' and '_'")
  128. }
  129. if !ids.Add(field.ID) {
  130. return position.Errorf("'id' should be unique")
  131. }
  132. return nil
  133. }
  134. func validateOptions(field *api.IssueFormField, idx int) error {
  135. if field.Type != api.IssueFormFieldTypeDropdown && field.Type != api.IssueFormFieldTypeCheckboxes {
  136. return nil
  137. }
  138. position := newErrorPosition(idx, field.Type)
  139. options, ok := field.Attributes["options"].([]any)
  140. if !ok || len(options) == 0 {
  141. return position.Errorf("'options' is required and should be a array")
  142. }
  143. for optIdx, option := range options {
  144. position := newErrorPosition(idx, field.Type, optIdx)
  145. switch field.Type {
  146. case api.IssueFormFieldTypeDropdown:
  147. if _, ok := option.(string); !ok {
  148. return position.Errorf("should be a string")
  149. }
  150. case api.IssueFormFieldTypeCheckboxes:
  151. opt, ok := option.(map[string]any)
  152. if !ok {
  153. return position.Errorf("should be a dictionary")
  154. }
  155. if label, ok := opt["label"].(string); !ok || label == "" {
  156. return position.Errorf("'label' is required and should be a string")
  157. }
  158. if required, ok := opt["required"]; ok {
  159. if _, ok := required.(bool); !ok {
  160. return position.Errorf("'required' should be a bool")
  161. }
  162. }
  163. }
  164. }
  165. return nil
  166. }
  167. func validateStringItem(position errorPosition, m map[string]any, required bool, names ...string) error {
  168. for _, name := range names {
  169. v, ok := m[name]
  170. if !ok {
  171. if required {
  172. return position.Errorf("'%s' is required", name)
  173. }
  174. return nil
  175. }
  176. attr, ok := v.(string)
  177. if !ok {
  178. return position.Errorf("'%s' should be a string", name)
  179. }
  180. if strings.TrimSpace(attr) == "" && required {
  181. return position.Errorf("'%s' is required", name)
  182. }
  183. }
  184. return nil
  185. }
  186. func validateBoolItem(position errorPosition, m map[string]any, names ...string) error {
  187. for _, name := range names {
  188. v, ok := m[name]
  189. if !ok {
  190. return nil
  191. }
  192. if _, ok := v.(bool); !ok {
  193. return position.Errorf("'%s' should be a bool", name)
  194. }
  195. }
  196. return nil
  197. }
  198. type errorPosition string
  199. func (p errorPosition) Errorf(format string, a ...any) error {
  200. return fmt.Errorf(string(p)+": "+format, a...)
  201. }
  202. func newErrorPosition(fieldIdx int, fieldType api.IssueFormFieldType, optionIndex ...int) errorPosition {
  203. ret := fmt.Sprintf("body[%d](%s)", fieldIdx, fieldType)
  204. if len(optionIndex) > 0 {
  205. ret += fmt.Sprintf(", option[%d]", optionIndex[0])
  206. }
  207. return errorPosition(ret)
  208. }
  209. // RenderToMarkdown renders template to markdown with specified values
  210. func RenderToMarkdown(template *api.IssueTemplate, values url.Values) string {
  211. builder := &strings.Builder{}
  212. for _, field := range template.Fields {
  213. f := &valuedField{
  214. IssueFormField: field,
  215. Values: values,
  216. }
  217. if f.ID == "" {
  218. continue
  219. }
  220. f.WriteTo(builder)
  221. }
  222. return builder.String()
  223. }
  224. type valuedField struct {
  225. *api.IssueFormField
  226. url.Values
  227. }
  228. func (f *valuedField) WriteTo(builder *strings.Builder) {
  229. if f.Type == api.IssueFormFieldTypeMarkdown {
  230. // markdown blocks do not appear in output
  231. return
  232. }
  233. // write label
  234. if !f.HideLabel() {
  235. _, _ = fmt.Fprintf(builder, "### %s\n\n", f.Label())
  236. }
  237. blankPlaceholder := "_No response_\n"
  238. // write body
  239. switch f.Type {
  240. case api.IssueFormFieldTypeCheckboxes:
  241. for _, option := range f.Options() {
  242. checked := " "
  243. if option.IsChecked() {
  244. checked = "x"
  245. }
  246. _, _ = fmt.Fprintf(builder, "- [%s] %s\n", checked, option.Label())
  247. }
  248. case api.IssueFormFieldTypeDropdown:
  249. var checkeds []string
  250. for _, option := range f.Options() {
  251. if option.IsChecked() {
  252. checkeds = append(checkeds, option.Label())
  253. }
  254. }
  255. if len(checkeds) > 0 {
  256. _, _ = fmt.Fprintf(builder, "%s\n", strings.Join(checkeds, ", "))
  257. } else {
  258. _, _ = fmt.Fprint(builder, blankPlaceholder)
  259. }
  260. case api.IssueFormFieldTypeInput:
  261. if value := f.Value(); value == "" {
  262. _, _ = fmt.Fprint(builder, blankPlaceholder)
  263. } else {
  264. _, _ = fmt.Fprintf(builder, "%s\n", value)
  265. }
  266. case api.IssueFormFieldTypeTextarea:
  267. if value := f.Value(); value == "" {
  268. _, _ = fmt.Fprint(builder, blankPlaceholder)
  269. } else if render := f.Render(); render != "" {
  270. quotes := minQuotes(value)
  271. _, _ = fmt.Fprintf(builder, "%s%s\n%s\n%s\n", quotes, f.Render(), value, quotes)
  272. } else {
  273. _, _ = fmt.Fprintf(builder, "%s\n", value)
  274. }
  275. }
  276. _, _ = fmt.Fprintln(builder)
  277. }
  278. func (f *valuedField) Label() string {
  279. if label, ok := f.Attributes["label"].(string); ok {
  280. return label
  281. }
  282. return ""
  283. }
  284. func (f *valuedField) HideLabel() bool {
  285. if label, ok := f.Attributes["hide_label"].(bool); ok {
  286. return label
  287. }
  288. return false
  289. }
  290. func (f *valuedField) Render() string {
  291. if render, ok := f.Attributes["render"].(string); ok {
  292. return render
  293. }
  294. return ""
  295. }
  296. func (f *valuedField) Value() string {
  297. return strings.TrimSpace(f.Get(fmt.Sprintf("form-field-" + f.ID)))
  298. }
  299. func (f *valuedField) Options() []*valuedOption {
  300. if options, ok := f.Attributes["options"].([]any); ok {
  301. ret := make([]*valuedOption, 0, len(options))
  302. for i, option := range options {
  303. ret = append(ret, &valuedOption{
  304. index: i,
  305. data: option,
  306. field: f,
  307. })
  308. }
  309. return ret
  310. }
  311. return nil
  312. }
  313. type valuedOption struct {
  314. index int
  315. data any
  316. field *valuedField
  317. }
  318. func (o *valuedOption) Label() string {
  319. switch o.field.Type {
  320. case api.IssueFormFieldTypeDropdown:
  321. if label, ok := o.data.(string); ok {
  322. return label
  323. }
  324. case api.IssueFormFieldTypeCheckboxes:
  325. if vs, ok := o.data.(map[string]any); ok {
  326. if v, ok := vs["label"].(string); ok {
  327. return v
  328. }
  329. }
  330. }
  331. return ""
  332. }
  333. func (o *valuedOption) IsChecked() bool {
  334. switch o.field.Type {
  335. case api.IssueFormFieldTypeDropdown:
  336. checks := strings.Split(o.field.Get(fmt.Sprintf("form-field-%s", o.field.ID)), ",")
  337. idx := strconv.Itoa(o.index)
  338. for _, v := range checks {
  339. if v == idx {
  340. return true
  341. }
  342. }
  343. return false
  344. case api.IssueFormFieldTypeCheckboxes:
  345. return o.field.Get(fmt.Sprintf("form-field-%s-%d", o.field.ID, o.index)) == "on"
  346. }
  347. return false
  348. }
  349. var minQuotesRegex = regexp.MustCompilePOSIX("^`{3,}")
  350. // minQuotes return 3 or more back-quotes.
  351. // If n back-quotes exists, use n+1 back-quotes to quote.
  352. func minQuotes(value string) string {
  353. ret := "```"
  354. for _, v := range minQuotesRegex.FindAllString(value, -1) {
  355. if len(v) >= len(ret) {
  356. ret = v + "`"
  357. }
  358. }
  359. return ret
  360. }