Skip to content

Commit a733458

Browse files
committed
Move init repository related functions to modules
1 parent a5f2894 commit a733458

File tree

15 files changed

+261
-269
lines changed

15 files changed

+261
-269
lines changed

models/error.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -754,22 +754,6 @@ func (err ErrIssueIsClosed) Error() string {
754754
return fmt.Sprintf("issue is closed [id: %d, repo_id: %d, index: %d]", err.ID, err.RepoID, err.Index)
755755
}
756756

757-
// ErrIssueLabelTemplateLoad represents a "ErrIssueLabelTemplateLoad" kind of error.
758-
type ErrIssueLabelTemplateLoad struct {
759-
TemplateFile string
760-
OriginalError error
761-
}
762-
763-
// IsErrIssueLabelTemplateLoad checks if an error is a ErrIssueLabelTemplateLoad.
764-
func IsErrIssueLabelTemplateLoad(err error) bool {
765-
_, ok := err.(ErrIssueLabelTemplateLoad)
766-
return ok
767-
}
768-
769-
func (err ErrIssueLabelTemplateLoad) Error() string {
770-
return fmt.Sprintf("Failed to load label template file '%s': %v", err.TemplateFile, err.OriginalError)
771-
}
772-
773757
// ErrNewIssueInsert is used when the INSERT statement in newIssue fails
774758
type ErrNewIssueInsert struct {
775759
OriginalError error

models/issue_label.go

Lines changed: 3 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -50,50 +50,6 @@ func init() {
5050
db.RegisterModel(new(IssueLabel))
5151
}
5252

53-
// GetLabelTemplateFile loads the label template file by given name,
54-
// then parses and returns a list of name-color pairs and optionally description.
55-
func GetLabelTemplateFile(name string) ([][3]string, error) {
56-
data, err := GetRepoInitFile("label", name)
57-
if err != nil {
58-
return nil, ErrIssueLabelTemplateLoad{name, fmt.Errorf("GetRepoInitFile: %v", err)}
59-
}
60-
61-
lines := strings.Split(string(data), "\n")
62-
list := make([][3]string, 0, len(lines))
63-
for i := 0; i < len(lines); i++ {
64-
line := strings.TrimSpace(lines[i])
65-
if len(line) == 0 {
66-
continue
67-
}
68-
69-
parts := strings.SplitN(line, ";", 2)
70-
71-
fields := strings.SplitN(parts[0], " ", 2)
72-
if len(fields) != 2 {
73-
return nil, ErrIssueLabelTemplateLoad{name, fmt.Errorf("line is malformed: %s", line)}
74-
}
75-
76-
color := strings.Trim(fields[0], " ")
77-
if len(color) == 6 {
78-
color = "#" + color
79-
}
80-
if !LabelColorPattern.MatchString(color) {
81-
return nil, ErrIssueLabelTemplateLoad{name, fmt.Errorf("bad HTML color code in line: %s", line)}
82-
}
83-
84-
var description string
85-
86-
if len(parts) > 1 {
87-
description = strings.TrimSpace(parts[1])
88-
}
89-
90-
fields[1] = strings.TrimSpace(fields[1])
91-
list = append(list, [3]string{fields[1], color, description})
92-
}
93-
94-
return list, nil
95-
}
96-
9753
// CalOpenIssues sets the number of open issues of a label based on the already stored number of closed issues.
9854
func (label *Label) CalOpenIssues() {
9955
label.NumOpenIssues = label.NumIssues - label.NumClosedIssues
@@ -191,70 +147,8 @@ func (label *Label) ForegroundColor() template.CSS {
191147
return template.CSS("#000")
192148
}
193149

194-
// .____ ___. .__
195-
// | | _____ \_ |__ ____ | |
196-
// | | \__ \ | __ \_/ __ \| |
197-
// | |___ / __ \| \_\ \ ___/| |__
198-
// >_______ (____ /___ /\___ >____/
199-
200-
func loadLabels(labelTemplate string) ([]string, error) {
201-
list, err := GetLabelTemplateFile(labelTemplate)
202-
if err != nil {
203-
return nil, err
204-
}
205-
206-
labels := make([]string, len(list))
207-
for i := 0; i < len(list); i++ {
208-
labels[i] = list[i][0]
209-
}
210-
return labels, nil
211-
}
212-
213-
// LoadLabelsFormatted loads the labels' list of a template file as a string separated by comma
214-
func LoadLabelsFormatted(labelTemplate string) (string, error) {
215-
labels, err := loadLabels(labelTemplate)
216-
return strings.Join(labels, ", "), err
217-
}
218-
219-
func initializeLabels(e db.Engine, id int64, labelTemplate string, isOrg bool) error {
220-
list, err := GetLabelTemplateFile(labelTemplate)
221-
if err != nil {
222-
return err
223-
}
224-
225-
labels := make([]*Label, len(list))
226-
for i := 0; i < len(list); i++ {
227-
labels[i] = &Label{
228-
Name: list[i][0],
229-
Description: list[i][2],
230-
Color: list[i][1],
231-
}
232-
if isOrg {
233-
labels[i].OrgID = id
234-
} else {
235-
labels[i].RepoID = id
236-
}
237-
}
238-
for _, label := range labels {
239-
if err = newLabel(e, label); err != nil {
240-
return err
241-
}
242-
}
243-
return nil
244-
}
245-
246-
// InitializeLabels adds a label set to a repository using a template
247-
func InitializeLabels(ctx context.Context, repoID int64, labelTemplate string, isOrg bool) error {
248-
return initializeLabels(db.GetEngine(ctx), repoID, labelTemplate, isOrg)
249-
}
250-
251-
func newLabel(e db.Engine, label *Label) error {
252-
_, err := e.Insert(label)
253-
return err
254-
}
255-
256150
// NewLabel creates a new label
257-
func NewLabel(label *Label) error {
151+
func NewLabel(ctx context.Context, label *Label) error {
258152
if !LabelColorPattern.MatchString(label.Color) {
259153
return fmt.Errorf("bad color code: %s", label.Color)
260154
}
@@ -275,7 +169,7 @@ func NewLabel(label *Label) error {
275169
label.Color = fmt.Sprintf("#%c%c%c%c%c%c", r, r, g, g, b, b)
276170
}
277171

278-
return newLabel(db.GetEngine(db.DefaultContext), label)
172+
return db.Insert(ctx, label)
279173
}
280174

281175
// NewLabels creates new labels
@@ -290,7 +184,7 @@ func NewLabels(labels ...*Label) error {
290184
if !LabelColorPattern.MatchString(label.Color) {
291185
return fmt.Errorf("bad color code: %s", label.Color)
292186
}
293-
if err := newLabel(db.GetEngine(ctx), label); err != nil {
187+
if err := db.Insert(ctx, label); err != nil {
294188
return err
295189
}
296190
}

models/issue_label_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ func TestNewLabels(t *testing.T) {
4242
{RepoID: 4, Name: "labelName4", Color: "ABCDEF"},
4343
{RepoID: 5, Name: "labelName5", Color: "DEF"},
4444
}
45-
assert.Error(t, NewLabel(&Label{RepoID: 3, Name: "invalid Color", Color: ""}))
46-
assert.Error(t, NewLabel(&Label{RepoID: 3, Name: "invalid Color", Color: "#45G"}))
47-
assert.Error(t, NewLabel(&Label{RepoID: 3, Name: "invalid Color", Color: "#12345G"}))
48-
assert.Error(t, NewLabel(&Label{RepoID: 3, Name: "invalid Color", Color: "45G"}))
49-
assert.Error(t, NewLabel(&Label{RepoID: 3, Name: "invalid Color", Color: "12345G"}))
45+
assert.Error(t, NewLabel(db.DefaultContext, &Label{RepoID: 3, Name: "invalid Color", Color: ""}))
46+
assert.Error(t, NewLabel(db.DefaultContext, &Label{RepoID: 3, Name: "invalid Color", Color: "#45G"}))
47+
assert.Error(t, NewLabel(db.DefaultContext, &Label{RepoID: 3, Name: "invalid Color", Color: "#12345G"}))
48+
assert.Error(t, NewLabel(db.DefaultContext, &Label{RepoID: 3, Name: "invalid Color", Color: "45G"}))
49+
assert.Error(t, NewLabel(db.DefaultContext, &Label{RepoID: 3, Name: "invalid Color", Color: "12345G"}))
5050
for _, label := range labels {
5151
unittest.AssertNotExistsBean(t, label)
5252
}

models/repo.go

Lines changed: 2 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"fmt"
1111
"os"
1212
"path"
13-
"sort"
1413
"strconv"
1514
"strings"
1615
"unicode/utf8"
@@ -27,7 +26,6 @@ import (
2726
"code.gitea.io/gitea/models/webhook"
2827
"code.gitea.io/gitea/modules/lfs"
2928
"code.gitea.io/gitea/modules/log"
30-
"code.gitea.io/gitea/modules/options"
3129
"code.gitea.io/gitea/modules/setting"
3230
"code.gitea.io/gitea/modules/storage"
3331
api "code.gitea.io/gitea/modules/structs"
@@ -36,90 +34,11 @@ import (
3634
"xorm.io/builder"
3735
)
3836

39-
var (
40-
// Gitignores contains the gitiginore files
41-
Gitignores []string
42-
43-
// Licenses contains the license files
44-
Licenses []string
45-
46-
// Readmes contains the readme files
47-
Readmes []string
48-
49-
// LabelTemplates contains the label template files and the list of labels for each file
50-
LabelTemplates map[string]string
51-
52-
// ItemsPerPage maximum items per page in forks, watchers and stars of a repo
53-
ItemsPerPage = 40
54-
)
55-
56-
// loadRepoConfig loads the repository config
57-
func loadRepoConfig() {
58-
// Load .gitignore and license files and readme templates.
59-
types := []string{"gitignore", "license", "readme", "label"}
60-
typeFiles := make([][]string, 4)
61-
for i, t := range types {
62-
files, err := options.Dir(t)
63-
if err != nil {
64-
log.Fatal("Failed to get %s files: %v", t, err)
65-
}
66-
customPath := path.Join(setting.CustomPath, "options", t)
67-
isDir, err := util.IsDir(customPath)
68-
if err != nil {
69-
log.Fatal("Failed to get custom %s files: %v", t, err)
70-
}
71-
if isDir {
72-
customFiles, err := util.StatDir(customPath)
73-
if err != nil {
74-
log.Fatal("Failed to get custom %s files: %v", t, err)
75-
}
76-
77-
for _, f := range customFiles {
78-
if !util.IsStringInSlice(f, files, true) {
79-
files = append(files, f)
80-
}
81-
}
82-
}
83-
typeFiles[i] = files
84-
}
85-
86-
Gitignores = typeFiles[0]
87-
Licenses = typeFiles[1]
88-
Readmes = typeFiles[2]
89-
LabelTemplatesFiles := typeFiles[3]
90-
sort.Strings(Gitignores)
91-
sort.Strings(Licenses)
92-
sort.Strings(Readmes)
93-
sort.Strings(LabelTemplatesFiles)
94-
95-
// Load label templates
96-
LabelTemplates = make(map[string]string)
97-
for _, templateFile := range LabelTemplatesFiles {
98-
labels, err := LoadLabelsFormatted(templateFile)
99-
if err != nil {
100-
log.Error("Failed to load labels: %v", err)
101-
}
102-
LabelTemplates[templateFile] = labels
103-
}
104-
105-
// Filter out invalid names and promote preferred licenses.
106-
sortedLicenses := make([]string, 0, len(Licenses))
107-
for _, name := range setting.Repository.PreferredLicenses {
108-
if util.IsStringInSlice(name, Licenses, true) {
109-
sortedLicenses = append(sortedLicenses, name)
110-
}
111-
}
112-
for _, name := range Licenses {
113-
if !util.IsStringInSlice(name, setting.Repository.PreferredLicenses, true) {
114-
sortedLicenses = append(sortedLicenses, name)
115-
}
116-
}
117-
Licenses = sortedLicenses
118-
}
37+
// ItemsPerPage maximum items per page in forks, watchers and stars of a repo
38+
var ItemsPerPage = 40
11939

12040
// NewRepoContext creates a new repository context
12141
func NewRepoContext() {
122-
loadRepoConfig()
12342
unit.LoadUnitConfig()
12443

12544
admin_model.RemoveAllWithNotice(db.DefaultContext, "Clean up temporary repository uploads", setting.Repository.Upload.TempPath)
@@ -440,35 +359,6 @@ type CreateRepoOptions struct {
440359
MirrorInterval string
441360
}
442361

443-
// GetRepoInitFile returns repository init files
444-
func GetRepoInitFile(tp, name string) ([]byte, error) {
445-
cleanedName := strings.TrimLeft(path.Clean("/"+name), "/")
446-
relPath := path.Join("options", tp, cleanedName)
447-
448-
// Use custom file when available.
449-
customPath := path.Join(setting.CustomPath, relPath)
450-
isFile, err := util.IsFile(customPath)
451-
if err != nil {
452-
log.Error("Unable to check if %s is a file. Error: %v", customPath, err)
453-
}
454-
if isFile {
455-
return os.ReadFile(customPath)
456-
}
457-
458-
switch tp {
459-
case "readme":
460-
return options.Readme(cleanedName)
461-
case "gitignore":
462-
return options.Gitignore(cleanedName)
463-
case "license":
464-
return options.License(cleanedName)
465-
case "label":
466-
return options.Labels(cleanedName)
467-
default:
468-
return []byte{}, fmt.Errorf("Invalid init file type")
469-
}
470-
}
471-
472362
// CreateRepository creates a repository for the user/organization.
473363
func CreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository, overwriteOrAdopt bool) (err error) {
474364
if err = repo_model.IsUsableRepoName(repo.Name); err != nil {

models/repo_generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func GenerateIssueLabels(ctx context.Context, templateRepo, generateRepo *repo_m
109109
Description: templateLabel.Description,
110110
Color: templateLabel.Color,
111111
}
112-
if err := newLabel(db.GetEngine(ctx), generateLabel); err != nil {
112+
if err := db.Insert(ctx, generateLabel); err != nil {
113113
return err
114114
}
115115
}

modules/repository/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func CreateRepository(doer, u *user_model.User, opts models.CreateRepoOptions) (
3333

3434
// Check if label template exist
3535
if len(opts.IssueLabels) > 0 {
36-
if _, err := models.GetLabelTemplateFile(opts.IssueLabels); err != nil {
36+
if _, err := GetLabelTemplateFile(opts.IssueLabels); err != nil {
3737
return nil, err
3838
}
3939
}
@@ -100,7 +100,7 @@ func CreateRepository(doer, u *user_model.User, opts models.CreateRepoOptions) (
100100

101101
// Initialize Issue Labels if selected
102102
if len(opts.IssueLabels) > 0 {
103-
if err = models.InitializeLabels(ctx, repo.ID, opts.IssueLabels, false); err != nil {
103+
if err = InitializeLabels(ctx, repo.ID, opts.IssueLabels, false); err != nil {
104104
rollbackRepo = repo
105105
rollbackRepo.OwnerID = u.ID
106106
return fmt.Errorf("InitializeLabels: %v", err)

0 commit comments

Comments
 (0)