Skip to content

Commit 41a79d0

Browse files
committed
Merge remote-tracking branch 'upstream/main'
* upstream/main: Avoid recursing into sub-sub-sub-docs folders when looking for READMEs. (go-gitea#23695) [skip ci] Updated translations via Crowdin Use auto-updating, natively hoverable, localized time elements (go-gitea#23988) Reserve ".png" suffix for user/org names (go-gitea#23992) Allow adding SSH keys even if SSH server is disabled (go-gitea#24025) Add placeholder and aria attributes to release and wiki edit page (go-gitea#24031) Add --quiet option to gitea dump (go-gitea#22969) Remove "inverted" class on creating new label and cancel buttons (go-gitea#24030) Use actions job link as commit status URL instead of run link (go-gitea#24023) Make label templates have consistent behavior and priority (go-gitea#23749) Add actions support to package auth verification (go-gitea#23729)
2 parents ac0dee9 + ef7fd78 commit 41a79d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+393
-517
lines changed

cmd/dump.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ It can be used for backup and capture Gitea server image to send to maintainer`,
112112
Name: "verbose, V",
113113
Usage: "Show process details",
114114
},
115+
cli.BoolFlag{
116+
Name: "quiet, q",
117+
Usage: "Only display warnings and errors",
118+
},
115119
cli.StringFlag{
116120
Name: "tempdir, t",
117121
Value: os.TempDir(),
@@ -192,12 +196,25 @@ func runDump(ctx *cli.Context) error {
192196
if _, err := setting.CfgProvider.Section("log.console").NewKey("STDERR", "true"); err != nil {
193197
fatal("Setting console logger to stderr failed: %v", err)
194198
}
199+
200+
// Set loglevel to Warn if quiet-mode is requested
201+
if ctx.Bool("quiet") {
202+
if _, err := setting.CfgProvider.Section("log.console").NewKey("LEVEL", "Warn"); err != nil {
203+
fatal("Setting console log-level failed: %v", err)
204+
}
205+
}
206+
195207
if !setting.InstallLock {
196208
log.Error("Is '%s' really the right config path?\n", setting.CustomConf)
197209
return fmt.Errorf("gitea is not initialized")
198210
}
199211
setting.LoadSettings() // cannot access session settings otherwise
200212

213+
verbose := ctx.Bool("verbose")
214+
if verbose && ctx.Bool("quiet") {
215+
return fmt.Errorf("--quiet and --verbose cannot both be set")
216+
}
217+
201218
stdCtx, cancel := installSignals()
202219
defer cancel()
203220

@@ -223,7 +240,6 @@ func runDump(ctx *cli.Context) error {
223240
return err
224241
}
225242

226-
verbose := ctx.Bool("verbose")
227243
var iface interface{}
228244
if fileName == "-" {
229245
iface, err = archiver.ByExtension(fmt.Sprintf(".%s", outType))

models/asymkey/ssh_key_parse.go

-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"strconv"
1717
"strings"
1818

19-
"code.gitea.io/gitea/models/db"
2019
"code.gitea.io/gitea/modules/log"
2120
"code.gitea.io/gitea/modules/process"
2221
"code.gitea.io/gitea/modules/setting"
@@ -158,10 +157,6 @@ func parseKeyString(content string) (string, error) {
158157
// CheckPublicKeyString checks if the given public key string is recognized by SSH.
159158
// It returns the actual public key line on success.
160159
func CheckPublicKeyString(content string) (_ string, err error) {
161-
if setting.SSH.Disabled {
162-
return "", db.ErrSSHDisabled{}
163-
}
164-
165160
content, err = parseKeyString(content)
166161
if err != nil {
167162
return "", err

models/user/user.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,8 @@ var (
537537
"gitea-actions",
538538
}
539539

540-
reservedUserPatterns = []string{"*.keys", "*.gpg", "*.rss", "*.atom"}
540+
// DON'T ADD ANY NEW STUFF, WE SOLVE THIS WITH `/user/{obj}` PATHS!
541+
reservedUserPatterns = []string{"*.keys", "*.gpg", "*.rss", "*.atom", "*.png"}
541542
)
542543

543544
// IsUsableUsername returns an error when a username is reserved

modules/label/parser.go

+20-28
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,23 @@ func IsErrTemplateLoad(err error) bool {
3030
}
3131

3232
func (err ErrTemplateLoad) Error() string {
33-
return fmt.Sprintf("Failed to load label template file '%s': %v", err.TemplateFile, err.OriginalError)
33+
return fmt.Sprintf("failed to load label template file %q: %v", err.TemplateFile, err.OriginalError)
3434
}
3535

36-
// GetTemplateFile loads the label template file by given name,
37-
// then parses and returns a list of name-color pairs and optionally description.
38-
func GetTemplateFile(name string) ([]*Label, error) {
39-
data, err := options.Labels(name + ".yaml")
40-
if err == nil && len(data) > 0 {
41-
return parseYamlFormat(name+".yaml", data)
42-
}
43-
44-
data, err = options.Labels(name + ".yml")
45-
if err == nil && len(data) > 0 {
46-
return parseYamlFormat(name+".yml", data)
47-
}
48-
49-
data, err = options.Labels(name)
36+
// LoadTemplateFile loads the label template file by given file name, returns a slice of Label structs.
37+
func LoadTemplateFile(fileName string) ([]*Label, error) {
38+
data, err := options.Labels(fileName)
5039
if err != nil {
51-
return nil, ErrTemplateLoad{name, fmt.Errorf("GetRepoInitFile: %w", err)}
40+
return nil, ErrTemplateLoad{fileName, fmt.Errorf("LoadTemplateFile: %w", err)}
5241
}
5342

54-
return parseLegacyFormat(name, data)
43+
if strings.HasSuffix(fileName, ".yaml") || strings.HasSuffix(fileName, ".yml") {
44+
return parseYamlFormat(fileName, data)
45+
}
46+
return parseLegacyFormat(fileName, data)
5547
}
5648

57-
func parseYamlFormat(name string, data []byte) ([]*Label, error) {
49+
func parseYamlFormat(fileName string, data []byte) ([]*Label, error) {
5850
lf := &labelFile{}
5951

6052
if err := yaml.Unmarshal(data, lf); err != nil {
@@ -65,19 +57,19 @@ func parseYamlFormat(name string, data []byte) ([]*Label, error) {
6557
for _, l := range lf.Labels {
6658
l.Color = strings.TrimSpace(l.Color)
6759
if len(l.Name) == 0 || len(l.Color) == 0 {
68-
return nil, ErrTemplateLoad{name, errors.New("label name and color are required fields")}
60+
return nil, ErrTemplateLoad{fileName, errors.New("label name and color are required fields")}
6961
}
7062
color, err := NormalizeColor(l.Color)
7163
if err != nil {
72-
return nil, ErrTemplateLoad{name, fmt.Errorf("bad HTML color code '%s' in label: %s", l.Color, l.Name)}
64+
return nil, ErrTemplateLoad{fileName, fmt.Errorf("bad HTML color code '%s' in label: %s", l.Color, l.Name)}
7365
}
7466
l.Color = color
7567
}
7668

7769
return lf.Labels, nil
7870
}
7971

80-
func parseLegacyFormat(name string, data []byte) ([]*Label, error) {
72+
func parseLegacyFormat(fileName string, data []byte) ([]*Label, error) {
8173
lines := strings.Split(string(data), "\n")
8274
list := make([]*Label, 0, len(lines))
8375
for i := 0; i < len(lines); i++ {
@@ -88,18 +80,18 @@ func parseLegacyFormat(name string, data []byte) ([]*Label, error) {
8880

8981
parts, description, _ := strings.Cut(line, ";")
9082

91-
color, name, ok := strings.Cut(parts, " ")
83+
color, labelName, ok := strings.Cut(parts, " ")
9284
if !ok {
93-
return nil, ErrTemplateLoad{name, fmt.Errorf("line is malformed: %s", line)}
85+
return nil, ErrTemplateLoad{fileName, fmt.Errorf("line is malformed: %s", line)}
9486
}
9587

9688
color, err := NormalizeColor(color)
9789
if err != nil {
98-
return nil, ErrTemplateLoad{name, fmt.Errorf("bad HTML color code '%s' in line: %s", color, line)}
90+
return nil, ErrTemplateLoad{fileName, fmt.Errorf("bad HTML color code '%s' in line: %s", color, line)}
9991
}
10092

10193
list = append(list, &Label{
102-
Name: strings.TrimSpace(name),
94+
Name: strings.TrimSpace(labelName),
10395
Color: color,
10496
Description: strings.TrimSpace(description),
10597
})
@@ -108,10 +100,10 @@ func parseLegacyFormat(name string, data []byte) ([]*Label, error) {
108100
return list, nil
109101
}
110102

111-
// LoadFormatted loads the labels' list of a template file as a string separated by comma
112-
func LoadFormatted(name string) (string, error) {
103+
// LoadTemplateDescription loads the labels from a template file, returns a description string by joining each Label.Name with comma
104+
func LoadTemplateDescription(fileName string) (string, error) {
113105
var buf strings.Builder
114-
list, err := GetTemplateFile(name)
106+
list, err := LoadTemplateFile(fileName)
115107
if err != nil {
116108
return "", err
117109
}

modules/repository/create.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
user_model "code.gitea.io/gitea/models/user"
2424
"code.gitea.io/gitea/models/webhook"
2525
"code.gitea.io/gitea/modules/git"
26-
"code.gitea.io/gitea/modules/label"
2726
"code.gitea.io/gitea/modules/log"
2827
"code.gitea.io/gitea/modules/setting"
2928
api "code.gitea.io/gitea/modules/structs"
@@ -190,7 +189,7 @@ func CreateRepository(doer, u *user_model.User, opts CreateRepoOptions) (*repo_m
190189

191190
// Check if label template exist
192191
if len(opts.IssueLabels) > 0 {
193-
if _, err := label.GetTemplateFile(opts.IssueLabels); err != nil {
192+
if _, err := LoadTemplateLabelsByDisplayName(opts.IssueLabels); err != nil {
194193
return nil, err
195194
}
196195
}

modules/repository/init.go

+69-48
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"fmt"
1010
"os"
11-
"path"
1211
"path/filepath"
1312
"sort"
1413
"strings"
@@ -27,6 +26,11 @@ import (
2726
asymkey_service "code.gitea.io/gitea/services/asymkey"
2827
)
2928

29+
type OptionFile struct {
30+
DisplayName string
31+
Description string
32+
}
33+
3034
var (
3135
// Gitignores contains the gitiginore files
3236
Gitignores []string
@@ -37,65 +41,73 @@ var (
3741
// Readmes contains the readme files
3842
Readmes []string
3943

40-
// LabelTemplates contains the label template files and the list of labels for each file
41-
LabelTemplates map[string]string
44+
// LabelTemplateFiles contains the label template files, each item has its DisplayName and Description
45+
LabelTemplateFiles []OptionFile
46+
labelTemplateFileMap = map[string]string{} // DisplayName => FileName mapping
4247
)
4348

49+
type optionFileList struct {
50+
all []string // all files provided by bindata & custom-path. Sorted.
51+
custom []string // custom files provided by custom-path. Non-sorted, internal use only.
52+
}
53+
54+
// mergeCustomLabelFiles merges the custom label files. Always use the file's main name (DisplayName) as the key to de-duplicate.
55+
func mergeCustomLabelFiles(fl optionFileList) []string {
56+
exts := map[string]int{"": 0, ".yml": 1, ".yaml": 2} // "yaml" file has the highest priority to be used.
57+
58+
m := map[string]string{}
59+
merge := func(list []string) {
60+
sort.Slice(list, func(i, j int) bool { return exts[filepath.Ext(list[i])] < exts[filepath.Ext(list[j])] })
61+
for _, f := range list {
62+
m[strings.TrimSuffix(f, filepath.Ext(f))] = f
63+
}
64+
}
65+
merge(fl.all)
66+
merge(fl.custom)
67+
68+
files := make([]string, 0, len(m))
69+
for _, f := range m {
70+
files = append(files, f)
71+
}
72+
sort.Strings(files)
73+
return files
74+
}
75+
4476
// LoadRepoConfig loads the repository config
45-
func LoadRepoConfig() {
46-
// Load .gitignore and license files and readme templates.
47-
types := []string{"gitignore", "license", "readme", "label"}
48-
typeFiles := make([][]string, 4)
77+
func LoadRepoConfig() error {
78+
types := []string{"gitignore", "license", "readme", "label"} // option file directories
79+
typeFiles := make([]optionFileList, len(types))
4980
for i, t := range types {
50-
files, err := options.Dir(t)
51-
if err != nil {
52-
log.Fatal("Failed to get %s files: %v", t, err)
81+
var err error
82+
if typeFiles[i].all, err = options.Dir(t); err != nil {
83+
return fmt.Errorf("failed to list %s files: %w", t, err)
5384
}
54-
if t == "label" {
55-
for i, f := range files {
56-
ext := strings.ToLower(filepath.Ext(f))
57-
if ext == ".yaml" || ext == ".yml" {
58-
files[i] = f[:len(f)-len(ext)]
59-
}
85+
sort.Strings(typeFiles[i].all)
86+
customPath := filepath.Join(setting.CustomPath, "options", t)
87+
if isDir, err := util.IsDir(customPath); err != nil {
88+
return fmt.Errorf("failed to check custom %s dir: %w", t, err)
89+
} else if isDir {
90+
if typeFiles[i].custom, err = util.StatDir(customPath); err != nil {
91+
return fmt.Errorf("failed to list custom %s files: %w", t, err)
6092
}
6193
}
62-
customPath := path.Join(setting.CustomPath, "options", t)
63-
isDir, err := util.IsDir(customPath)
64-
if err != nil {
65-
log.Fatal("Failed to get custom %s files: %v", t, err)
66-
}
67-
if isDir {
68-
customFiles, err := util.StatDir(customPath)
69-
if err != nil {
70-
log.Fatal("Failed to get custom %s files: %v", t, err)
71-
}
72-
73-
for _, f := range customFiles {
74-
if !util.SliceContainsString(files, f, true) {
75-
files = append(files, f)
76-
}
77-
}
78-
}
79-
typeFiles[i] = files
8094
}
8195

82-
Gitignores = typeFiles[0]
83-
Licenses = typeFiles[1]
84-
Readmes = typeFiles[2]
85-
LabelTemplatesFiles := typeFiles[3]
86-
sort.Strings(Gitignores)
87-
sort.Strings(Licenses)
88-
sort.Strings(Readmes)
89-
sort.Strings(LabelTemplatesFiles)
96+
Gitignores = typeFiles[0].all
97+
Licenses = typeFiles[1].all
98+
Readmes = typeFiles[2].all
9099

91100
// Load label templates
92-
LabelTemplates = make(map[string]string)
93-
for _, templateFile := range LabelTemplatesFiles {
94-
labels, err := label.LoadFormatted(templateFile)
101+
LabelTemplateFiles = nil
102+
labelTemplateFileMap = map[string]string{}
103+
for _, file := range mergeCustomLabelFiles(typeFiles[3]) {
104+
description, err := label.LoadTemplateDescription(file)
95105
if err != nil {
96-
log.Error("Failed to load labels: %v", err)
106+
return fmt.Errorf("failed to load labels: %w", err)
97107
}
98-
LabelTemplates[templateFile] = labels
108+
displayName := strings.TrimSuffix(file, filepath.Ext(file))
109+
labelTemplateFileMap[displayName] = file
110+
LabelTemplateFiles = append(LabelTemplateFiles, OptionFile{DisplayName: displayName, Description: description})
99111
}
100112

101113
// Filter out invalid names and promote preferred licenses.
@@ -111,6 +123,7 @@ func LoadRepoConfig() {
111123
}
112124
}
113125
Licenses = sortedLicenses
126+
return nil
114127
}
115128

116129
func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir, repoPath string, opts CreateRepoOptions) error {
@@ -344,7 +357,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
344357

345358
// InitializeLabels adds a label set to a repository using a template
346359
func InitializeLabels(ctx context.Context, id int64, labelTemplate string, isOrg bool) error {
347-
list, err := label.GetTemplateFile(labelTemplate)
360+
list, err := LoadTemplateLabelsByDisplayName(labelTemplate)
348361
if err != nil {
349362
return err
350363
}
@@ -370,3 +383,11 @@ func InitializeLabels(ctx context.Context, id int64, labelTemplate string, isOrg
370383
}
371384
return nil
372385
}
386+
387+
// LoadTemplateLabelsByDisplayName loads a label template by its display name
388+
func LoadTemplateLabelsByDisplayName(displayName string) ([]*label.Label, error) {
389+
if fileName, ok := labelTemplateFileMap[displayName]; ok {
390+
return label.LoadTemplateFile(fileName)
391+
}
392+
return nil, label.ErrTemplateLoad{TemplateFile: displayName, OriginalError: fmt.Errorf("label template %q not found", displayName)}
393+
}

modules/repository/init_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package repository
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestMergeCustomLabels(t *testing.T) {
13+
files := mergeCustomLabelFiles(optionFileList{
14+
all: []string{"a", "a.yaml", "a.yml"},
15+
custom: nil,
16+
})
17+
assert.EqualValues(t, []string{"a.yaml"}, files, "yaml file should win")
18+
19+
files = mergeCustomLabelFiles(optionFileList{
20+
all: []string{"a", "a.yaml"},
21+
custom: []string{"a"},
22+
})
23+
assert.EqualValues(t, []string{"a"}, files, "custom file should win")
24+
25+
files = mergeCustomLabelFiles(optionFileList{
26+
all: []string{"a", "a.yml", "a.yaml"},
27+
custom: []string{"a", "a.yml"},
28+
})
29+
assert.EqualValues(t, []string{"a.yml"}, files, "custom yml file should win if no yaml")
30+
}

0 commit comments

Comments
 (0)