gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package label
  4. import (
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/gitea/modules/options"
  9. "gopkg.in/yaml.v3"
  10. )
  11. type labelFile struct {
  12. Labels []*Label `yaml:"labels"`
  13. }
  14. // ErrTemplateLoad represents a "ErrTemplateLoad" kind of error.
  15. type ErrTemplateLoad struct {
  16. TemplateFile string
  17. OriginalError error
  18. }
  19. // IsErrTemplateLoad checks if an error is a ErrTemplateLoad.
  20. func IsErrTemplateLoad(err error) bool {
  21. _, ok := err.(ErrTemplateLoad)
  22. return ok
  23. }
  24. func (err ErrTemplateLoad) Error() string {
  25. return fmt.Sprintf("failed to load label template file %q: %v", err.TemplateFile, err.OriginalError)
  26. }
  27. // LoadTemplateFile loads the label template file by given file name, returns a slice of Label structs.
  28. func LoadTemplateFile(fileName string) ([]*Label, error) {
  29. data, err := options.Labels(fileName)
  30. if err != nil {
  31. return nil, ErrTemplateLoad{fileName, fmt.Errorf("LoadTemplateFile: %w", err)}
  32. }
  33. if strings.HasSuffix(fileName, ".yaml") || strings.HasSuffix(fileName, ".yml") {
  34. return parseYamlFormat(fileName, data)
  35. }
  36. return parseLegacyFormat(fileName, data)
  37. }
  38. func parseYamlFormat(fileName string, data []byte) ([]*Label, error) {
  39. lf := &labelFile{}
  40. if err := yaml.Unmarshal(data, lf); err != nil {
  41. return nil, err
  42. }
  43. // Validate label data and fix colors
  44. for _, l := range lf.Labels {
  45. l.Color = strings.TrimSpace(l.Color)
  46. if len(l.Name) == 0 || len(l.Color) == 0 {
  47. return nil, ErrTemplateLoad{fileName, errors.New("label name and color are required fields")}
  48. }
  49. color, err := NormalizeColor(l.Color)
  50. if err != nil {
  51. return nil, ErrTemplateLoad{fileName, fmt.Errorf("bad HTML color code '%s' in label: %s", l.Color, l.Name)}
  52. }
  53. l.Color = color
  54. }
  55. return lf.Labels, nil
  56. }
  57. func parseLegacyFormat(fileName string, data []byte) ([]*Label, error) {
  58. lines := strings.Split(string(data), "\n")
  59. list := make([]*Label, 0, len(lines))
  60. for i := range lines {
  61. line := strings.TrimSpace(lines[i])
  62. if len(line) == 0 {
  63. continue
  64. }
  65. parts, description, _ := strings.Cut(line, ";")
  66. color, labelName, ok := strings.Cut(parts, " ")
  67. if !ok {
  68. return nil, ErrTemplateLoad{fileName, fmt.Errorf("line is malformed: %s", line)}
  69. }
  70. color, err := NormalizeColor(color)
  71. if err != nil {
  72. return nil, ErrTemplateLoad{fileName, fmt.Errorf("bad HTML color code '%s' in line: %s", color, line)}
  73. }
  74. list = append(list, &Label{
  75. Name: strings.TrimSpace(labelName),
  76. Color: color,
  77. Description: strings.TrimSpace(description),
  78. })
  79. }
  80. return list, nil
  81. }
  82. // LoadTemplateDescription loads the labels from a template file, returns a description string by joining each Label.Name with comma
  83. func LoadTemplateDescription(fileName string) (string, error) {
  84. var buf strings.Builder
  85. list, err := LoadTemplateFile(fileName)
  86. if err != nil {
  87. return "", err
  88. }
  89. for i := range list {
  90. if i > 0 {
  91. buf.WriteString(", ")
  92. }
  93. buf.WriteString(list[i].Name)
  94. }
  95. return buf.String(), nil
  96. }