You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

license.go 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package checks
  5. import (
  6. "regexp"
  7. "strings"
  8. "golang.org/x/tools/go/analysis"
  9. )
  10. var (
  11. header = regexp.MustCompile(`.*Copyright.*\d{4}.*(Gitea|Gogs)`)
  12. goGenerate = "//go:generate"
  13. buildTag = "// +build"
  14. )
  15. var License = &analysis.Analyzer{
  16. Name: "license",
  17. Doc: "check for a copyright header.",
  18. Run: runLicense,
  19. }
  20. func runLicense(pass *analysis.Pass) (interface{}, error) {
  21. for _, file := range pass.Files {
  22. if len(file.Comments) == 0 {
  23. pass.Reportf(file.Pos(), "Copyright not found")
  24. continue
  25. }
  26. if len(file.Comments[0].List) == 0 {
  27. pass.Reportf(file.Pos(), "Copyright not found or wrong")
  28. continue
  29. }
  30. commentGroup := 0
  31. if strings.HasPrefix(file.Comments[0].List[0].Text, goGenerate) {
  32. if len(file.Comments[0].List) > 1 {
  33. pass.Reportf(file.Pos(), "Must be an empty line between the go:generate and the Copyright")
  34. continue
  35. }
  36. commentGroup++
  37. }
  38. if strings.HasPrefix(file.Comments[0].List[0].Text, buildTag) {
  39. commentGroup++
  40. }
  41. if len(file.Comments) < commentGroup+1 {
  42. pass.Reportf(file.Pos(), "Copyright not found")
  43. continue
  44. }
  45. if len(file.Comments[commentGroup].List) < 1 {
  46. pass.Reportf(file.Pos(), "Copyright not found or wrong")
  47. continue
  48. }
  49. var check bool
  50. for _, comment := range file.Comments[commentGroup].List {
  51. if header.MatchString(comment.Text) {
  52. check = true
  53. }
  54. }
  55. if !check {
  56. pass.Reportf(file.Pos(), "Copyright did not match check")
  57. }
  58. }
  59. return nil, nil
  60. }