Compare commits
200 Commits
dev_miroco
...
release/v1
Author | SHA1 | Date |
---|---|---|
|
34fe3d390b | 5 years ago |
|
ce51c2bdf6 | 5 years ago |
|
7937f1463a | 5 years ago |
|
dbe9c11238 | 5 years ago |
|
313ace93d0 | 5 years ago |
|
f79a2e193f | 5 years ago |
|
5d4251eb78 | 5 years ago |
|
88008b681d | 5 years ago |
|
5462fdcbbd | 5 years ago |
|
161e550200 | 5 years ago |
|
95af6096fb | 5 years ago |
|
801f4b9e7a | 5 years ago |
|
c0c3a533a0 | 5 years ago |
|
ed646078e1 | 5 years ago |
|
dc0ea133e1 | 5 years ago |
|
a854846f06 | 5 years ago |
|
b52e8de7de | 5 years ago |
|
1b62916393 | 5 years ago |
|
1d57c309ef | 5 years ago |
|
cf97e65b66 | 5 years ago |
|
42a46cff35 | 5 years ago |
|
2cb3db2d20 | 5 years ago |
|
04e480d477 | 5 years ago |
|
de9a96c4de | 5 years ago |
|
878434146f | 5 years ago |
|
d78be7ddf9 | 5 years ago |
|
83f8414e1e | 5 years ago |
|
0b216f40fd | 5 years ago |
|
dd6e604f8f | 5 years ago |
|
86863ae939 | 5 years ago |
|
f3a90057a5 | 5 years ago |
|
03fdd82d63 | 5 years ago |
|
cd7fa15d1d | 5 years ago |
|
79868d7096 | 5 years ago |
|
19626b93f8 | 5 years ago |
|
91e6a7f7ea | 5 years ago |
|
ff7eaa1eb4 | 5 years ago |
|
5131206aad | 5 years ago |
|
bfc25fcf40 | 5 years ago |
|
4a6765fba2 | 5 years ago |
|
dca8ef9407 | 5 years ago |
|
cebef5c871 | 5 years ago |
|
245d6ebda5 | 5 years ago |
|
d9875ff2e1 | 5 years ago |
|
cc2a6c1d30 | 5 years ago |
|
b5fd55de73 | 5 years ago |
|
e11b3a1076 | 5 years ago |
|
0c4be64345 | 5 years ago |
|
c34ad62eea | 5 years ago |
|
f7d7cf4e2d | 5 years ago |
|
99a364a9dc | 5 years ago |
|
3afbbfe921 | 5 years ago |
|
bfce841b04 | 5 years ago |
|
139fc7cfee | 5 years ago |
|
596eebb2b6 | 5 years ago |
|
1d5d745851 | 5 years ago |
|
3dabfd4933 | 5 years ago |
|
6ee6731290 | 5 years ago |
|
602fe45936 | 5 years ago |
|
e2da9cd21f | 5 years ago |
|
c0b917b7eb | 5 years ago |
|
54ea58ddf0 | 5 years ago |
|
0158725387 | 5 years ago |
|
f3cacf1332 | 5 years ago |
|
a15dc93011 | 5 years ago |
|
66b31786d3 | 5 years ago |
|
931ddfec6d | 5 years ago |
|
7e0a5b17db | 5 years ago |
|
07688231c2 | 5 years ago |
|
21eaeb8418 | 5 years ago |
|
9a929ad17f | 5 years ago |
|
c19ac41b34 | 5 years ago |
|
fd85d31cb4 | 5 years ago |
|
c9e4d7a564 | 5 years ago |
|
9990430e32 | 5 years ago |
|
6f5656ab0e | 5 years ago |
|
e4a876cee1 | 5 years ago |
|
abb534ba7a | 5 years ago |
|
65dceb6a40 | 5 years ago |
|
db26f0aca9 | 5 years ago |
|
76878fd69b | 5 years ago |
|
3444fa2dd7 | 5 years ago |
|
caa2aeaa52 | 5 years ago |
|
11300ee582 | 5 years ago |
|
c6b78c3d31 | 5 years ago |
|
4c40aa5be9 | 5 years ago |
|
50f2e90b76 | 5 years ago |
|
5d11ccc9e1 | 5 years ago |
|
93860af542 | 5 years ago |
|
7bf5834f2c | 5 years ago |
|
1fbdd9335f | 5 years ago |
|
e9061a537c | 5 years ago |
|
ed664a9e1d | 5 years ago |
|
4cb18601ff | 5 years ago |
|
3abb25166c | 5 years ago |
|
9e6ad64d48 | 5 years ago |
|
b51d7c459e | 5 years ago |
|
d3b6f001fe | 5 years ago |
|
e938f1d945 | 5 years ago |
|
7284327a00 | 5 years ago |
|
919f3f11e2 | 5 years ago |
|
3cee15e6f9 | 5 years ago |
|
34e3644ada | 5 years ago |
|
14bd120cdc | 5 years ago |
|
3e40f8bebc | 5 years ago |
|
df5f1d9dca | 5 years ago |
|
457ee1ab5a | 5 years ago |
|
4f64688902 | 5 years ago |
|
117dcf1c02 | 5 years ago |
|
4529a262c0 | 5 years ago |
|
ff24f81a05 | 5 years ago |
|
e3cb4f9d0e | 5 years ago |
|
9b7890f1cc | 5 years ago |
|
eb064dfda2 | 5 years ago |
|
f9e66e5a46 | 5 years ago |
|
ef89260cf1 | 5 years ago |
|
315d928626 | 5 years ago |
|
5525452bdf | 5 years ago |
|
987cd277f6 | 5 years ago |
|
5cdfde2ebf | 5 years ago |
|
1cd6233cef | 5 years ago |
|
cb81e39f7a | 5 years ago |
|
8efd6b32e2 | 5 years ago |
|
c95d9603ea | 5 years ago |
|
9169b39458 | 5 years ago |
|
80eb50655a | 5 years ago |
|
b16c555541 | 5 years ago |
|
b5b44364e3 | 5 years ago |
|
6af58022c8 | 5 years ago |
|
e48b460a0a | 5 years ago |
|
2cd2614eaa | 5 years ago |
|
0129e76ef5 | 5 years ago |
|
6896dad675 | 5 years ago |
|
1ed4323005 | 5 years ago |
|
049af0d3d0 | 5 years ago |
|
f5727d83dd | 5 years ago |
|
912ce27421 | 5 years ago |
|
b3549bb5ec | 5 years ago |
|
491cbeca67 | 5 years ago |
|
895d92ffe5 | 5 years ago |
|
4b11f967bd | 5 years ago |
|
1e73dd2446 | 5 years ago |
|
315026c2c5 | 5 years ago |
|
16dfd9ffbe | 5 years ago |
|
16f7b43903 | 5 years ago |
|
043febdbc9 | 5 years ago |
|
60f91d56f0 | 5 years ago |
|
16fc15ae6a | 5 years ago |
|
ef8f6d99f1 | 5 years ago |
|
4b688135f9 | 5 years ago |
|
e24861a546 | 5 years ago |
|
128cc34344 | 5 years ago |
|
f82a805478 | 5 years ago |
|
0dced15c1a | 5 years ago |
|
db9342c854 | 5 years ago |
|
79c1d48532 | 5 years ago |
|
05b9864086 | 5 years ago |
|
ff508c9c9b | 5 years ago |
|
f96c1a2c79 | 5 years ago |
|
ce756ee89f | 5 years ago |
|
f2e9d4b851 | 5 years ago |
|
e878d743f4 | 5 years ago |
|
3fa14d89a2 | 5 years ago |
|
bcb722daec | 5 years ago |
|
8add1dfacc | 5 years ago |
|
aa6ed1b7c1 | 5 years ago |
|
95cb921097 | 5 years ago |
|
6730df9e8c | 5 years ago |
|
b577500a54 | 5 years ago |
|
fe46185407 | 5 years ago |
|
69a2a29c33 | 5 years ago |
|
f766719895 | 5 years ago |
|
e2ddc42377 | 5 years ago |
|
3521177a34 | 5 years ago |
|
c8bb0ecf52 | 5 years ago |
|
dbe6136348 | 5 years ago |
|
6d1f7e90cf | 5 years ago |
|
42663a687c | 5 years ago |
|
73c90c26d4 | 5 years ago |
|
c579ad92b5 | 5 years ago |
|
602c5da953 | 5 years ago |
|
1980e59ac2 | 5 years ago |
|
28508792ba | 5 years ago |
|
3e23dad075 | 5 years ago |
|
b13b9d3dbd | 5 years ago |
|
4072f28e60 | 5 years ago |
|
dbeef6bb02 | 6 years ago |
|
fec35440db | 6 years ago |
|
f8ea50cc7a | 6 years ago |
|
0e53a16cca | 6 years ago |
|
7eaba6ba8a | 6 years ago |
|
ff16099c6d | 6 years ago |
|
a516a7ba0f | 6 years ago |
|
11bce6fd3d | 6 years ago |
|
3fb906dc02 | 6 years ago |
|
3a00a690c9 | 6 years ago |
|
a2b7cc1bb1 | 6 years ago |
|
04a77b1f42 | 6 years ago |
|
f523372d07 | 6 years ago |
|
e39c238ef4 | 6 years ago |
687 changed files with 23267 additions and 93815 deletions
@ -1,44 +1,57 @@
|
||||
# The full repository name |
||||
repo: go-gitea/gitea |
||||
|
||||
# Service type (gitea or github) |
||||
service: github |
||||
|
||||
# Base URL for Gitea instance if using gitea service type (optional) |
||||
# Default: https://gitea.com |
||||
base-url: |
||||
|
||||
# Changelog groups and which labeled PRs to add to each group |
||||
groups: |
||||
- |
||||
- |
||||
name: BREAKING |
||||
labels: |
||||
- kind/breaking |
||||
- |
||||
- |
||||
name: FEATURE |
||||
labels: |
||||
- kind/feature |
||||
- |
||||
name: SECURITY |
||||
labels: |
||||
- kind/security |
||||
- |
||||
name: BUGFIXES |
||||
labels: |
||||
- kind/bug |
||||
- |
||||
- |
||||
name: ENHANCEMENT |
||||
labels: |
||||
- kind/enhancement |
||||
- kind/refactor |
||||
- kind/ui |
||||
- |
||||
name: SECURITY |
||||
labels: |
||||
- kind/security |
||||
- |
||||
name: TESTING |
||||
labels: |
||||
- kind/testing |
||||
- |
||||
- |
||||
name: TRANSLATION |
||||
labels: |
||||
- kind/translation |
||||
- |
||||
- |
||||
name: BUILD |
||||
labels: |
||||
- kind/build |
||||
- kind/lint |
||||
- |
||||
- |
||||
name: DOCS |
||||
labels: |
||||
- kind/docs |
||||
- |
||||
- |
||||
name: MISC |
||||
default: true |
||||
default: true |
||||
|
||||
# regex indicating which labels to skip for the changelog |
||||
skip-labels: skip-changelog|backport\/.+ |
||||
|
@ -0,0 +1,529 @@
|
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package cmd |
||||
|
||||
import ( |
||||
"bufio" |
||||
"bytes" |
||||
"context" |
||||
"fmt" |
||||
"io/ioutil" |
||||
golog "log" |
||||
"os" |
||||
"os/exec" |
||||
"path/filepath" |
||||
"strings" |
||||
"text/tabwriter" |
||||
|
||||
"code.gitea.io/gitea/models" |
||||
"code.gitea.io/gitea/models/migrations" |
||||
"code.gitea.io/gitea/modules/git" |
||||
"code.gitea.io/gitea/modules/log" |
||||
"code.gitea.io/gitea/modules/options" |
||||
"code.gitea.io/gitea/modules/setting" |
||||
"xorm.io/builder" |
||||
|
||||
"github.com/urfave/cli" |
||||
) |
||||
|
||||
// CmdDoctor represents the available doctor sub-command.
|
||||
var CmdDoctor = cli.Command{ |
||||
Name: "doctor", |
||||
Usage: "Diagnose problems", |
||||
Description: "A command to diagnose problems with the current Gitea instance according to the given configuration.", |
||||
Action: runDoctor, |
||||
Flags: []cli.Flag{ |
||||
cli.BoolFlag{ |
||||
Name: "list", |
||||
Usage: "List the available checks", |
||||
}, |
||||
cli.BoolFlag{ |
||||
Name: "default", |
||||
Usage: "Run the default checks (if neither --run or --all is set, this is the default behaviour)", |
||||
}, |
||||
cli.StringSliceFlag{ |
||||
Name: "run", |
||||
Usage: "Run the provided checks - (if --default is set, the default checks will also run)", |
||||
}, |
||||
cli.BoolFlag{ |
||||
Name: "all", |
||||
Usage: "Run all the available checks", |
||||
}, |
||||
cli.BoolFlag{ |
||||
Name: "fix", |
||||
Usage: "Automatically fix what we can", |
||||
}, |
||||
cli.StringFlag{ |
||||
Name: "log-file", |
||||
Usage: `Name of the log file (default: "doctor.log"). Set to "-" to output to stdout, set to "" to disable`, |
||||
}, |
||||
}, |
||||
} |
||||
|
||||
type check struct { |
||||
title string |
||||
name string |
||||
isDefault bool |
||||
f func(ctx *cli.Context) ([]string, error) |
||||
abortIfFailed bool |
||||
skipDatabaseInit bool |
||||
} |
||||
|
||||
// checklist represents list for all checks
|
||||
var checklist = []check{ |
||||
{ |
||||
// NOTE: this check should be the first in the list
|
||||
title: "Check paths and basic configuration", |
||||
name: "paths", |
||||
isDefault: true, |
||||
f: runDoctorPathInfo, |
||||
abortIfFailed: true, |
||||
skipDatabaseInit: true, |
||||
}, |
||||
{ |
||||
title: "Check Database Version", |
||||
name: "check-db-version", |
||||
isDefault: true, |
||||
f: runDoctorCheckDBVersion, |
||||
abortIfFailed: true, |
||||
}, |
||||
{ |
||||
title: "Check if OpenSSH authorized_keys file is up-to-date", |
||||
name: "authorized_keys", |
||||
isDefault: true, |
||||
f: runDoctorAuthorizedKeys, |
||||
}, |
||||
{ |
||||
title: "Check if SCRIPT_TYPE is available", |
||||
name: "script-type", |
||||
isDefault: false, |
||||
f: runDoctorScriptType, |
||||
}, |
||||
{ |
||||
title: "Check if hook files are up-to-date and executable", |
||||
name: "hooks", |
||||
isDefault: false, |
||||
f: runDoctorHooks, |
||||
}, |
||||
{ |
||||
title: "Recalculate merge bases", |
||||
name: "recalculate_merge_bases", |
||||
isDefault: false, |
||||
f: runDoctorPRMergeBase, |
||||
}, |
||||
{ |
||||
title: "Check consistency of database", |
||||
name: "check-db-consistency", |
||||
isDefault: true, |
||||
f: runDoctorCheckDBConsistency, |
||||
}, |
||||
// more checks please append here
|
||||
} |
||||
|
||||
func runDoctor(ctx *cli.Context) error { |
||||
|
||||
// Silence the default loggers
|
||||
log.DelNamedLogger("console") |
||||
log.DelNamedLogger(log.DEFAULT) |
||||
|
||||
// Now setup our own
|
||||
logFile := ctx.String("log-file") |
||||
if !ctx.IsSet("log-file") { |
||||
logFile = "doctor.log" |
||||
} |
||||
|
||||
if len(logFile) == 0 { |
||||
log.NewLogger(1000, "doctor", "console", `{"level":"NONE","stacktracelevel":"NONE","colorize":"%t"}`) |
||||
} else if logFile == "-" { |
||||
log.NewLogger(1000, "doctor", "console", `{"level":"trace","stacktracelevel":"NONE"}`) |
||||
} else { |
||||
log.NewLogger(1000, "doctor", "file", fmt.Sprintf(`{"filename":%q,"level":"trace","stacktracelevel":"NONE"}`, logFile)) |
||||
} |
||||
|
||||
// Finally redirect the default golog to here
|
||||
golog.SetFlags(0) |
||||
golog.SetPrefix("") |
||||
golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT))) |
||||
|
||||
if ctx.IsSet("list") { |
||||
w := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0) |
||||
_, _ = w.Write([]byte("Default\tName\tTitle\n")) |
||||
for _, check := range checklist { |
||||
if check.isDefault { |
||||
_, _ = w.Write([]byte{'*'}) |
||||
} |
||||
_, _ = w.Write([]byte{'\t'}) |
||||
_, _ = w.Write([]byte(check.name)) |
||||
_, _ = w.Write([]byte{'\t'}) |
||||
_, _ = w.Write([]byte(check.title)) |
||||
_, _ = w.Write([]byte{'\n'}) |
||||
} |
||||
return w.Flush() |
||||
} |
||||
|
||||
var checks []check |
||||
if ctx.Bool("all") { |
||||
checks = checklist |
||||
} else if ctx.IsSet("run") { |
||||
addDefault := ctx.Bool("default") |
||||
names := ctx.StringSlice("run") |
||||
for i, name := range names { |
||||
names[i] = strings.ToLower(strings.TrimSpace(name)) |
||||
} |
||||
|
||||
for _, check := range checklist { |
||||
if addDefault && check.isDefault { |
||||
checks = append(checks, check) |
||||
continue |
||||
} |
||||
for _, name := range names { |
||||
if name == check.name { |
||||
checks = append(checks, check) |
||||
break |
||||
} |
||||
} |
||||
} |
||||
} else { |
||||
for _, check := range checklist { |
||||
if check.isDefault { |
||||
checks = append(checks, check) |
||||
} |
||||
} |
||||
} |
||||
|
||||
dbIsInit := false |
||||
|
||||
for i, check := range checks { |
||||
if !dbIsInit && !check.skipDatabaseInit { |
||||
// Only open database after the most basic configuration check
|
||||
setting.EnableXORMLog = false |
||||
if err := initDBDisableConsole(true); err != nil { |
||||
fmt.Println(err) |
||||
fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.") |
||||
return nil |
||||
} |
||||
dbIsInit = true |
||||
} |
||||
fmt.Println("[", i+1, "]", check.title) |
||||
messages, err := check.f(ctx) |
||||
for _, message := range messages { |
||||
fmt.Println("-", message) |
||||
} |
||||
if err != nil { |
||||
fmt.Println("Error:", err) |
||||
if check.abortIfFailed { |
||||
return nil |
||||
} |
||||
} else { |
||||
fmt.Println("OK.") |
||||
} |
||||
fmt.Println() |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func runDoctorPathInfo(ctx *cli.Context) ([]string, error) { |
||||
|
||||
res := make([]string, 0, 10) |
||||
|
||||
if fi, err := os.Stat(setting.CustomConf); err != nil || !fi.Mode().IsRegular() { |
||||
res = append(res, fmt.Sprintf("Failed to find configuration file at '%s'.", setting.CustomConf)) |
||||
res = append(res, fmt.Sprintf("If you've never ran Gitea yet, this is normal and '%s' will be created for you on first run.", setting.CustomConf)) |
||||
res = append(res, "Otherwise check that you are running this command from the correct path and/or provide a `--config` parameter.") |
||||
return res, fmt.Errorf("can't proceed without a configuration file") |
||||
} |
||||
|
||||
setting.NewContext() |
||||
|
||||
fail := false |
||||
|
||||
check := func(name, path string, is_dir, required, is_write bool) { |
||||
res = append(res, fmt.Sprintf("%-25s '%s'", name+":", path)) |
||||
fi, err := os.Stat(path) |
||||
if err != nil { |
||||
if os.IsNotExist(err) && ctx.Bool("fix") && is_dir { |
||||
if err := os.MkdirAll(path, 0777); err != nil { |
||||
res = append(res, fmt.Sprintf(" ERROR: %v", err)) |
||||
fail = true |
||||
return |
||||
} |
||||
fi, err = os.Stat(path) |
||||
} |
||||
} |
||||
if err != nil { |
||||
if required { |
||||
res = append(res, fmt.Sprintf(" ERROR: %v", err)) |
||||
fail = true |
||||
return |
||||
} |
||||
res = append(res, fmt.Sprintf(" NOTICE: not accessible (%v)", err)) |
||||
return |
||||
} |
||||
|
||||
if is_dir && !fi.IsDir() { |
||||
res = append(res, " ERROR: not a directory") |
||||
fail = true |
||||
return |
||||
} else if !is_dir && !fi.Mode().IsRegular() { |
||||
res = append(res, " ERROR: not a regular file") |
||||
fail = true |
||||
} else if is_write { |
||||
if err := runDoctorWritableDir(path); err != nil { |
||||
res = append(res, fmt.Sprintf(" ERROR: not writable: %v", err)) |
||||
fail = true |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Note print paths inside quotes to make any leading/trailing spaces evident
|
||||
check("Configuration File Path", setting.CustomConf, false, true, false) |
||||
check("Repository Root Path", setting.RepoRootPath, true, true, true) |
||||
check("Data Root Path", setting.AppDataPath, true, true, true) |
||||
check("Custom File Root Path", setting.CustomPath, true, false, false) |
||||
check("Work directory", setting.AppWorkPath, true, true, false) |
||||
check("Log Root Path", setting.LogRootPath, true, true, true) |
||||
|
||||
if options.IsDynamic() { |
||||
// Do not check/report on StaticRootPath if data is embedded in Gitea (-tags bindata)
|
||||
check("Static File Root Path", setting.StaticRootPath, true, true, false) |
||||
} |
||||
|
||||
if fail { |
||||
return res, fmt.Errorf("please check your configuration file and try again") |
||||
} |
||||
|
||||
return res, nil |
||||
} |
||||
|
||||
func runDoctorWritableDir(path string) error { |
||||
// There's no platform-independent way of checking if a directory is writable
|
||||
// https://stackoverflow.com/questions/20026320/how-to-tell-if-folder-exists-and-is-writable
|
||||
|
||||
tmpFile, err := ioutil.TempFile(path, "doctors-order") |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if err := os.Remove(tmpFile.Name()); err != nil { |
||||
fmt.Printf("Warning: can't remove temporary file: '%s'\n", tmpFile.Name()) |
||||
} |
||||
tmpFile.Close() |
||||
return nil |
||||
} |
||||
|
||||
const tplCommentPrefix = `# gitea public key` |
||||
|
||||
func runDoctorAuthorizedKeys(ctx *cli.Context) ([]string, error) { |
||||
if setting.SSH.StartBuiltinServer || !setting.SSH.CreateAuthorizedKeysFile { |
||||
return nil, nil |
||||
} |
||||
|
||||
fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys") |
||||
f, err := os.Open(fPath) |
||||
if err != nil { |
||||
if ctx.Bool("fix") { |
||||
return []string{fmt.Sprintf("Error whilst opening authorized_keys: %v. Attempting regeneration", err)}, models.RewriteAllPublicKeys() |
||||
} |
||||
return nil, err |
||||
} |
||||
defer f.Close() |
||||
|
||||
linesInAuthorizedKeys := map[string]bool{} |
||||
|
||||
scanner := bufio.NewScanner(f) |
||||
for scanner.Scan() { |
||||
line := scanner.Text() |
||||
if strings.HasPrefix(line, tplCommentPrefix) { |
||||
continue |
||||
} |
||||
linesInAuthorizedKeys[line] = true |
||||
} |
||||
f.Close() |
||||
|
||||
// now we regenerate and check if there are any lines missing
|
||||
regenerated := &bytes.Buffer{} |
||||
if err := models.RegeneratePublicKeys(regenerated); err != nil { |
||||
return nil, err |
||||
} |
||||
scanner = bufio.NewScanner(regenerated) |
||||
for scanner.Scan() { |
||||
line := scanner.Text() |
||||
if strings.HasPrefix(line, tplCommentPrefix) { |
||||
continue |
||||
} |
||||
if ok := linesInAuthorizedKeys[line]; ok { |
||||
continue |
||||
} |
||||
if ctx.Bool("fix") { |
||||
return []string{"authorized_keys is out of date, attempting regeneration"}, models.RewriteAllPublicKeys() |
||||
} |
||||
return nil, fmt.Errorf("authorized_keys is out of date and should be regenerated with gitea admin regenerate keys") |
||||
} |
||||
return nil, nil |
||||
} |
||||
|
||||
func runDoctorCheckDBVersion(ctx *cli.Context) ([]string, error) { |
||||
if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil { |
||||
if ctx.Bool("fix") { |
||||
return []string{fmt.Sprintf("WARN: Got Error %v during ensure up to date", err), "Attempting to migrate to the latest DB version to fix this."}, models.NewEngine(context.Background(), migrations.Migrate) |
||||
} |
||||
return nil, err |
||||
} |
||||
return nil, nil |
||||
} |
||||
|
||||
func iterateRepositories(each func(*models.Repository) ([]string, error)) ([]string, error) { |
||||
results := []string{} |
||||
err := models.Iterate( |
||||
models.DefaultDBContext(), |
||||
new(models.Repository), |
||||
builder.Gt{"id": 0}, |
||||
func(idx int, bean interface{}) error { |
||||
res, err := each(bean.(*models.Repository)) |
||||
results = append(results, res...) |
||||
return err |
||||
}, |
||||
) |
||||
return results, err |
||||
} |
||||
|
||||
func iteratePRs(repo *models.Repository, each func(*models.Repository, *models.PullRequest) ([]string, error)) ([]string, error) { |
||||
results := []string{} |
||||
err := models.Iterate( |
||||
models.DefaultDBContext(), |
||||
new(models.PullRequest), |
||||
builder.Eq{"base_repo_id": repo.ID}, |
||||
func(idx int, bean interface{}) error { |
||||
res, err := each(repo, bean.(*models.PullRequest)) |
||||
results = append(results, res...) |
||||
return err |
||||
}, |
||||
) |
||||
return results, err |
||||
} |
||||
|
||||
func runDoctorHooks(ctx *cli.Context) ([]string, error) { |
||||
// Need to iterate across all of the repositories
|
||||
return iterateRepositories(func(repo *models.Repository) ([]string, error) { |
||||
results, err := models.CheckDelegateHooks(repo.RepoPath()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if len(results) > 0 && ctx.Bool("fix") { |
||||
return []string{fmt.Sprintf("regenerated hooks for %s", repo.FullName())}, models.CreateDelegateHooks(repo.RepoPath()) |
||||
} |
||||
|
||||
return results, nil |
||||
}) |
||||
} |
||||
|
||||
func runDoctorPRMergeBase(ctx *cli.Context) ([]string, error) { |
||||
numRepos := 0 |
||||
numPRs := 0 |
||||
numPRsUpdated := 0 |
||||
results, err := iterateRepositories(func(repo *models.Repository) ([]string, error) { |
||||
numRepos++ |
||||
return iteratePRs(repo, func(repo *models.Repository, pr *models.PullRequest) ([]string, error) { |
||||
numPRs++ |
||||
results := []string{} |
||||
pr.BaseRepo = repo |
||||
repoPath := repo.RepoPath() |
||||
|
||||
oldMergeBase := pr.MergeBase |
||||
|
||||
if !pr.HasMerged { |
||||
var err error |
||||
pr.MergeBase, err = git.NewCommand("merge-base", "--", pr.BaseBranch, pr.GetGitRefName()).RunInDir(repoPath) |
||||
if err != nil { |
||||
var err2 error |
||||
pr.MergeBase, err2 = git.NewCommand("rev-parse", git.BranchPrefix+pr.BaseBranch).RunInDir(repoPath) |
||||
if err2 != nil { |
||||
results = append(results, fmt.Sprintf("WARN: Unable to get merge base for PR ID %d, #%d onto %s in %s/%s", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name)) |
||||
log.Error("Unable to get merge base for PR ID %d, Index %d in %s/%s. Error: %v & %v", pr.ID, pr.Index, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err, err2) |
||||
return results, nil |
||||
} |
||||
} |
||||
} else { |
||||
parentsString, err := git.NewCommand("rev-list", "--parents", "-n", "1", pr.MergedCommitID).RunInDir(repoPath) |
||||
if err != nil { |
||||
results = append(results, fmt.Sprintf("WARN: Unable to get parents for merged PR ID %d, #%d onto %s in %s/%s", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name)) |
||||
log.Error("Unable to get parents for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err) |
||||
return results, nil |
||||
} |
||||
parents := strings.Split(strings.TrimSpace(parentsString), " ") |
||||
if len(parents) < 2 { |
||||
return results, nil |
||||
} |
||||
|
||||
args := append([]string{"merge-base", "--"}, parents[1:]...) |
||||
args = append(args, pr.GetGitRefName()) |
||||
|
||||
pr.MergeBase, err = git.NewCommand(args...).RunInDir(repoPath) |
||||
if err != nil { |
||||
results = append(results, fmt.Sprintf("WARN: Unable to get merge base for merged PR ID %d, #%d onto %s in %s/%s", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name)) |
||||
log.Error("Unable to get merge base for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err) |
||||
return results, nil |
||||
} |
||||
} |
||||
pr.MergeBase = strings.TrimSpace(pr.MergeBase) |
||||
if pr.MergeBase != oldMergeBase { |
||||
if ctx.Bool("fix") { |
||||
if err := pr.UpdateCols("merge_base"); err != nil { |
||||
return results, err |
||||
} |
||||
} else { |
||||
results = append(results, fmt.Sprintf("#%d onto %s in %s/%s: MergeBase should be %s but is %s", pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, oldMergeBase, pr.MergeBase)) |
||||
} |
||||
numPRsUpdated++ |
||||
} |
||||
return results, nil |
||||
}) |
||||
}) |
||||
|
||||
if ctx.Bool("fix") { |
||||
results = append(results, fmt.Sprintf("%d PR mergebases updated of %d PRs total in %d repos", numPRsUpdated, numPRs, numRepos)) |
||||
} else { |
||||
if numPRsUpdated > 0 && err == nil { |
||||
return results, fmt.Errorf("%d PRs with incorrect mergebases of %d PRs total in %d repos", numPRsUpdated, numPRs, numRepos) |
||||
} |
||||
results = append(results, fmt.Sprintf("%d PRs with incorrect mergebases of %d PRs total in %d repos", numPRsUpdated, numPRs, numRepos)) |
||||
} |
||||
|
||||
return results, err |
||||
} |
||||
|
||||
func runDoctorScriptType(ctx *cli.Context) ([]string, error) { |
||||
path, err := exec.LookPath(setting.ScriptType) |
||||
if err != nil { |
||||
return []string{fmt.Sprintf("ScriptType %s is not on the current PATH", setting.ScriptType)}, err |
||||
} |
||||
return []string{fmt.Sprintf("ScriptType %s is on the current PATH at %s", setting.ScriptType, path)}, nil |
||||
} |
||||
|
||||
func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) { |
||||
var results []string |
||||
|
||||
// make sure DB version is uptodate
|
||||
if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil { |
||||
return nil, fmt.Errorf("model version on the database does not match the current Gitea version. Model consistency will not be checked until the database is upgraded") |
||||
} |
||||
|
||||
//find tracked times without existing issues/pulls
|
||||
count, err := models.CountOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id") |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if count > 0 { |
||||
if ctx.Bool("fix") { |
||||
if err = models.DeleteOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id"); err != nil { |
||||
return nil, err |
||||
} |
||||
results = append(results, fmt.Sprintf("%d tracked times without existing issue deleted", count)) |
||||
} else { |
||||
results = append(results, fmt.Sprintf("%d tracked times without existing issue", count)) |
||||
} |
||||
} |
||||
|
||||
return results, nil |
||||
} |
@ -0,0 +1,47 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package integrations |
||||
|
||||
import ( |
||||
"fmt" |
||||
"net/http" |
||||
"testing" |
||||
|
||||
"code.gitea.io/gitea/models" |
||||
"code.gitea.io/gitea/modules/structs" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestAPIIssuesMilestone(t *testing.T) { |
||||
defer prepareTestEnv(t)() |
||||
|
||||
milestone := models.AssertExistsAndLoadBean(t, &models.Milestone{ID: 1}).(*models.Milestone) |
||||
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: milestone.RepoID}).(*models.Repository) |
||||
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) |
||||
assert.Equal(t, int64(1), int64(milestone.NumIssues)) |
||||
assert.Equal(t, structs.StateOpen, milestone.State()) |
||||
|
||||
session := loginUser(t, owner.Name) |
||||
token := getTokenForLoggedInUser(t, session) |
||||
|
||||
// update values of issue
|
||||
milestoneState := "closed" |
||||
|
||||
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/milestones/%d?token=%s", owner.Name, repo.Name, milestone.ID, token) |
||||
req := NewRequestWithJSON(t, "PATCH", urlStr, structs.EditMilestoneOption{ |
||||
State: &milestoneState, |
||||
}) |
||||
resp := session.MakeRequest(t, req, http.StatusOK) |
||||
var apiMilestone structs.Milestone |
||||
DecodeJSON(t, resp, &apiMilestone) |
||||
assert.EqualValues(t, "closed", apiMilestone.State) |
||||
|
||||
req = NewRequest(t, "GET", urlStr) |
||||
resp = session.MakeRequest(t, req, http.StatusOK) |
||||
var apiMilestone2 structs.Milestone |
||||
DecodeJSON(t, resp, &apiMilestone2) |
||||
assert.EqualValues(t, "closed", apiMilestone2.State) |
||||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1 +1 @@
|
||||
0cf15c3f66ec8384480ed9c3cf87c9e97fbb0ec3 |
||||
423313fbd38093bb10d0c8387db9105409c6f196 |
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue