gitea源码

init.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "context"
  6. "fmt"
  7. "path/filepath"
  8. "sort"
  9. "strings"
  10. issues_model "code.gitea.io/gitea/models/issues"
  11. "code.gitea.io/gitea/modules/label"
  12. "code.gitea.io/gitea/modules/options"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/util"
  15. )
  16. type OptionFile struct {
  17. DisplayName string
  18. Description string
  19. }
  20. var (
  21. // Gitignores contains the gitiginore files
  22. Gitignores []string
  23. // Licenses contains the license files
  24. Licenses []string
  25. // Readmes contains the readme files
  26. Readmes []string
  27. // LabelTemplateFiles contains the label template files, each item has its DisplayName and Description
  28. LabelTemplateFiles []OptionFile
  29. labelTemplateFileMap = map[string]string{} // DisplayName => FileName mapping
  30. )
  31. type optionFileList struct {
  32. all []string // all files provided by bindata & custom-path. Sorted.
  33. custom []string // custom files provided by custom-path. Non-sorted, internal use only.
  34. }
  35. // mergeCustomLabelFiles merges the custom label files. Always use the file's main name (DisplayName) as the key to de-duplicate.
  36. func mergeCustomLabelFiles(fl optionFileList) []string {
  37. exts := map[string]int{"": 0, ".yml": 1, ".yaml": 2} // "yaml" file has the highest priority to be used.
  38. m := map[string]string{}
  39. merge := func(list []string) {
  40. sort.Slice(list, func(i, j int) bool { return exts[filepath.Ext(list[i])] < exts[filepath.Ext(list[j])] })
  41. for _, f := range list {
  42. m[strings.TrimSuffix(f, filepath.Ext(f))] = f
  43. }
  44. }
  45. merge(fl.all)
  46. merge(fl.custom)
  47. files := make([]string, 0, len(m))
  48. for _, f := range m {
  49. files = append(files, f)
  50. }
  51. sort.Strings(files)
  52. return files
  53. }
  54. // LoadRepoConfig loads the repository config
  55. func LoadRepoConfig() error {
  56. types := []string{"gitignore", "license", "readme", "label"} // option file directories
  57. typeFiles := make([]optionFileList, len(types))
  58. for i, t := range types {
  59. var err error
  60. if typeFiles[i].all, err = options.AssetFS().ListFiles(t, true); err != nil {
  61. return fmt.Errorf("failed to list %s files: %w", t, err)
  62. }
  63. sort.Strings(typeFiles[i].all)
  64. customPath := filepath.Join(setting.CustomPath, "options", t)
  65. if isDir, err := util.IsDir(customPath); err != nil {
  66. return fmt.Errorf("failed to check custom %s dir: %w", t, err)
  67. } else if isDir {
  68. if typeFiles[i].custom, err = util.ListDirRecursively(customPath, &util.ListDirOptions{SkipCommonHiddenNames: true}); err != nil {
  69. return fmt.Errorf("failed to list custom %s files: %w", t, err)
  70. }
  71. }
  72. }
  73. Gitignores = typeFiles[0].all
  74. Licenses = typeFiles[1].all
  75. Readmes = typeFiles[2].all
  76. // Load label templates
  77. LabelTemplateFiles = nil
  78. labelTemplateFileMap = map[string]string{}
  79. for _, file := range mergeCustomLabelFiles(typeFiles[3]) {
  80. description, err := label.LoadTemplateDescription(file)
  81. if err != nil {
  82. return fmt.Errorf("failed to load labels: %w", err)
  83. }
  84. displayName := strings.TrimSuffix(file, filepath.Ext(file))
  85. labelTemplateFileMap[displayName] = file
  86. LabelTemplateFiles = append(LabelTemplateFiles, OptionFile{DisplayName: displayName, Description: description})
  87. }
  88. // Filter out invalid names and promote preferred licenses.
  89. sortedLicenses := make([]string, 0, len(Licenses))
  90. for _, name := range setting.Repository.PreferredLicenses {
  91. if util.SliceContainsString(Licenses, name, true) {
  92. sortedLicenses = append(sortedLicenses, name)
  93. }
  94. }
  95. for _, name := range Licenses {
  96. if !util.SliceContainsString(setting.Repository.PreferredLicenses, name, true) {
  97. sortedLicenses = append(sortedLicenses, name)
  98. }
  99. }
  100. Licenses = sortedLicenses
  101. return nil
  102. }
  103. // InitializeLabels adds a label set to a repository using a template
  104. func InitializeLabels(ctx context.Context, id int64, labelTemplate string, isOrg bool) error {
  105. list, err := LoadTemplateLabelsByDisplayName(labelTemplate)
  106. if err != nil {
  107. return err
  108. }
  109. labels := make([]*issues_model.Label, len(list))
  110. for i := range list {
  111. labels[i] = &issues_model.Label{
  112. Name: list[i].Name,
  113. Exclusive: list[i].Exclusive,
  114. ExclusiveOrder: list[i].ExclusiveOrder,
  115. Description: list[i].Description,
  116. Color: list[i].Color,
  117. }
  118. if isOrg {
  119. labels[i].OrgID = id
  120. } else {
  121. labels[i].RepoID = id
  122. }
  123. }
  124. for _, label := range labels {
  125. if err = issues_model.NewLabel(ctx, label); err != nil {
  126. return err
  127. }
  128. }
  129. return nil
  130. }
  131. // LoadTemplateLabelsByDisplayName loads a label template by its display name
  132. func LoadTemplateLabelsByDisplayName(displayName string) ([]*label.Label, error) {
  133. if fileName, ok := labelTemplateFileMap[displayName]; ok {
  134. return label.LoadTemplateFile(fileName)
  135. }
  136. return nil, label.ErrTemplateLoad{TemplateFile: displayName, OriginalError: fmt.Errorf("label template %q not found", displayName)}
  137. }