* Refactor Logger
Refactor Logger to make a logger interface and make it possible to
wrap loggers for specific purposes.
* Refactor Doctor
Move the gitea doctor functions into its own module.
Use a logger for its messages instead of returning a results string[]
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Update modules/doctor/misc.go
Co-authored-by: 6543 <6543@obermui.de>
* Update modules/doctor/misc.go
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
return[]string{"authorized_keys is out of date, attempting regeneration"},models.RewriteAllPublicKeys()
}
returnnil,fmt.Errorf(`authorized_keys is out of date and should be regenerated with "gitea admin regenerate keys" or "gitea doctor --run authorized_keys --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)
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)
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)
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)
returnresults,nil
}
}
pr.MergeBase=strings.TrimSpace(pr.MergeBase)
ifpr.MergeBase!=oldMergeBase{
ifctx.Bool("fix"){
iferr:=pr.UpdateCols("merge_base");err!=nil{
returnresults,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++
}
returnresults,nil
})
})
ifctx.Bool("fix"){
results=append(results,fmt.Sprintf("%d PR mergebases updated of %d PRs total in %d repos",numPRsUpdated,numPRs,numRepos))
}else{
ifnumPRsUpdated>0&&err==nil{
returnresults,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))
returnnil,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 labels without existing repo or org
count,err:=models.CountOrphanedLabels()
iferr!=nil{
returnnil,err
}
ifcount>0{
ifctx.Bool("fix"){
iferr=models.DeleteOrphanedLabels();err!=nil{
returnnil,err
}
results=append(results,fmt.Sprintf("%d labels without existing repository/organisation deleted",count))
}else{
results=append(results,fmt.Sprintf("%d labels without existing repository/organisation",count))
}
}
//find issues without existing repository
count,err=models.CountOrphanedIssues()
iferr!=nil{
returnnil,err
}
ifcount>0{
ifctx.Bool("fix"){
iferr=models.DeleteOrphanedIssues();err!=nil{
returnnil,err
}
results=append(results,fmt.Sprintf("%d issues without existing repository deleted",count))
}else{
results=append(results,fmt.Sprintf("%d issues without existing repository",count))
logger.Critical("Unable to regenerate authorized_keys file. ERROR: %v",err)
returnfmt.Errorf("Unable to regenerate authorized_keys file. ERROR: %v",err)
}
scanner=bufio.NewScanner(regenerated)
forscanner.Scan(){
line:=scanner.Text()
ifstrings.HasPrefix(line,tplCommentPrefix){
continue
}
ifok:=linesInAuthorizedKeys[line];ok{
continue
}
if!autofix{
logger.Critical(
"authorized_keys file %q is out of date.\nRegenerate it with:\n\t\"%s\"\nor\n\t\"%s\"",
fPath,
"gitea admin regenerate keys",
"gitea doctor --run authorized_keys --fix")
returnfmt.Errorf(`authorized_keys is out of date and should be regenerated with "gitea admin regenerate keys" or "gitea doctor --run authorized_keys --fix"`)
}
logger.Warn("authorized_keys is out of date. Attempting rewrite...")
err=models.RewriteAllPublicKeys()
iferr!=nil{
logger.Critical("Unable to rewrite authorized_keys file. ERROR: %v",err)
returnfmt.Errorf("Unable to rewrite authorized_keys file. ERROR: %v",err)
}
}
returnnil
}
funcinit(){
Register(&Check{
Title:"Check if OpenSSH authorized_keys file is up-to-date",
logger.Critical("Model version on the database does not match the current Gitea version. Model consistency will not be checked until the database is upgraded")
logger.Warn("Unable to get merge base for PR ID %d, #%d onto %s in %s/%s. Error: %v & %v",pr.ID,pr.Index,pr.BaseBranch,pr.BaseRepo.OwnerName,pr.BaseRepo.Name,err,err2)
logger.Warn("Unable to get parents for merged PR ID %d, #%d onto %s in %s/%s. Error: %v",pr.ID,pr.Index,pr.BaseBranch,pr.BaseRepo.OwnerName,pr.BaseRepo.Name,err)
logger.Warn("Unable to get merge base for merged PR ID %d, #%d onto %s in %s/%s. Error: %v",pr.ID,pr.Index,pr.BaseBranch,pr.BaseRepo.OwnerName,pr.BaseRepo.Name,err)
returnnil
}
}
pr.MergeBase=strings.TrimSpace(pr.MergeBase)
ifpr.MergeBase!=oldMergeBase{
ifautofix{
iferr:=pr.UpdateCols("merge_base");err!=nil{
logger.Critical("Failed to update merge_base. ERROR: %v",err)
returnfmt.Errorf("Failed to update merge_base. ERROR: %v",err)
}
}else{
logger.Info("#%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++
}
returnnil
})
})
ifautofix{
logger.Info("%d PR mergebases updated of %d PRs total in %d repos",numPRsUpdated,numPRs,numRepos)
}else{
ifnumPRsUpdated>0&&err==nil{
logger.Critical("%d PRs with incorrect mergebases of %d PRs total in %d repos",numPRsUpdated,numPRs,numRepos)
returnfmt.Errorf("%d PRs with incorrect mergebases of %d PRs total in %d repos",numPRsUpdated,numPRs,numRepos)
}
logger.Warn("%d PRs with incorrect mergebases of %d PRs total in %d repos",numPRsUpdated,numPRs,numRepos)