gitea源码

template.go 12KB

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