mirror of
				https://gitea.com/gitea/tea.git
				synced 2025-10-31 01:05:26 +01:00 
			
		
		
		
	Update gitea-vet v0.2.0 (#164)
tagged version v0.2.0 make vendor Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/164 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
		
							
								
								
									
										46
									
								
								vendor/code.gitea.io/gitea-vet/checks/imports.go
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								vendor/code.gitea.io/gitea-vet/checks/imports.go
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| // 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 checks | ||||
|  | ||||
| import ( | ||||
| 	"strings" | ||||
|  | ||||
| 	"golang.org/x/tools/go/analysis" | ||||
| ) | ||||
|  | ||||
| var Imports = &analysis.Analyzer{ | ||||
| 	Name: "imports", | ||||
| 	Doc:  "check for import order", | ||||
| 	Run:  runImports, | ||||
| } | ||||
|  | ||||
| func runImports(pass *analysis.Pass) (interface{}, error) { | ||||
| 	for _, file := range pass.Files { | ||||
| 		level := 0 | ||||
| 		for _, im := range file.Imports { | ||||
| 			var lvl int | ||||
| 			val := im.Path.Value | ||||
| 			switch { | ||||
| 			case importHasPrefix(val, "code.gitea.io"): | ||||
| 				lvl = 2 | ||||
| 			case strings.Contains(val, "."): | ||||
| 				lvl = 3 | ||||
| 			default: | ||||
| 				lvl = 1 | ||||
| 			} | ||||
|  | ||||
| 			if lvl < level { | ||||
| 				pass.Reportf(file.Pos(), "Imports are sorted wrong") | ||||
| 				break | ||||
| 			} | ||||
| 			level = lvl | ||||
| 		} | ||||
| 	} | ||||
| 	return nil, nil | ||||
| } | ||||
|  | ||||
| func importHasPrefix(s, p string) bool { | ||||
| 	return strings.HasPrefix(s, "\""+p) | ||||
| } | ||||
							
								
								
									
										73
									
								
								vendor/code.gitea.io/gitea-vet/checks/license.go
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								vendor/code.gitea.io/gitea-vet/checks/license.go
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,73 @@ | ||||
| // 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 checks | ||||
|  | ||||
| import ( | ||||
| 	"regexp" | ||||
| 	"strings" | ||||
|  | ||||
| 	"golang.org/x/tools/go/analysis" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	header     = regexp.MustCompile(`.*Copyright.*\d{4}.*(Gitea|Gogs)`) | ||||
| 	goGenerate = "//go:generate" | ||||
| 	buildTag   = "// +build" | ||||
| ) | ||||
|  | ||||
| var License = &analysis.Analyzer{ | ||||
| 	Name: "license", | ||||
| 	Doc:  "check for a copyright header", | ||||
| 	Run:  runLicense, | ||||
| } | ||||
|  | ||||
| func runLicense(pass *analysis.Pass) (interface{}, error) { | ||||
| 	for _, file := range pass.Files { | ||||
| 		if len(file.Comments) == 0 { | ||||
| 			pass.Reportf(file.Pos(), "Copyright not found") | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		if len(file.Comments[0].List) == 0 { | ||||
| 			pass.Reportf(file.Pos(), "Copyright not found or wrong") | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		commentGroup := 0 | ||||
| 		if strings.HasPrefix(file.Comments[0].List[0].Text, goGenerate) { | ||||
| 			if len(file.Comments[0].List) > 1 { | ||||
| 				pass.Reportf(file.Pos(), "Must be an empty line between the go:generate and the Copyright") | ||||
| 				continue | ||||
| 			} | ||||
| 			commentGroup++ | ||||
| 		} | ||||
|  | ||||
| 		if strings.HasPrefix(file.Comments[0].List[0].Text, buildTag) { | ||||
| 			commentGroup++ | ||||
| 		} | ||||
|  | ||||
| 		if len(file.Comments) < commentGroup+1 { | ||||
| 			pass.Reportf(file.Pos(), "Copyright not found") | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		if len(file.Comments[commentGroup].List) < 1 { | ||||
| 			pass.Reportf(file.Pos(), "Copyright not found or wrong") | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		var check bool | ||||
| 		for _, comment := range file.Comments[commentGroup].List { | ||||
| 			if header.MatchString(comment.Text) { | ||||
| 				check = true | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		if !check { | ||||
| 			pass.Reportf(file.Pos(), "Copyright did not match check") | ||||
| 		} | ||||
| 	} | ||||
| 	return nil, nil | ||||
| } | ||||
							
								
								
									
										59
									
								
								vendor/code.gitea.io/gitea-vet/checks/migrations.go
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								vendor/code.gitea.io/gitea-vet/checks/migrations.go
									
									
									
										generated
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | ||||
| // 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 checks | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"os/exec" | ||||
| 	"strings" | ||||
|  | ||||
| 	"golang.org/x/tools/go/analysis" | ||||
| ) | ||||
|  | ||||
| var Migrations = &analysis.Analyzer{ | ||||
| 	Name: "migrations", | ||||
| 	Doc:  "check migrations for black-listed packages.", | ||||
| 	Run:  checkMigrations, | ||||
| } | ||||
|  | ||||
| var migrationBlacklist = []string{ | ||||
| 	"code.gitea.io/gitea/models", | ||||
| 	"code.gitea.io/gitea/modules/structs", | ||||
| } | ||||
|  | ||||
| func checkMigrations(pass *analysis.Pass) (interface{}, error) { | ||||
| 	if !strings.EqualFold(pass.Pkg.Path(), "code.gitea.io/gitea/models/migrations") { | ||||
| 		return nil, nil | ||||
| 	} | ||||
|  | ||||
| 	if _, err := exec.LookPath("go"); err != nil { | ||||
| 		return nil, errors.New("go was not found in the PATH") | ||||
| 	} | ||||
|  | ||||
| 	depsCmd := exec.Command("go", "list", "-f", `{{join .Deps "\n"}}`, "code.gitea.io/gitea/models/migrations") | ||||
| 	depsOut, err := depsCmd.Output() | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	deps := strings.Split(string(depsOut), "\n") | ||||
| 	for _, dep := range deps { | ||||
| 		if stringInSlice(dep, migrationBlacklist) { | ||||
| 			pass.Reportf(0, "code.gitea.io/gitea/models/migrations cannot depend on the following packages: %s", migrationBlacklist) | ||||
| 			return nil, nil | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return nil, nil | ||||
| } | ||||
|  | ||||
| func stringInSlice(needle string, haystack []string) bool { | ||||
| 	for _, h := range haystack { | ||||
| 		if strings.EqualFold(needle, h) { | ||||
| 			return true | ||||
| 		} | ||||
| 	} | ||||
| 	return false | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 6543
					6543