Dangerfile 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # TODO(drcamer): Danger supports a shared Dangerfile that we can reference
  2. # from our other repos (e.g. getsentry/danger)
  3. # see also: https://github.com/samdmarshall/danger/blob/master/Dangerfile
  4. # and: https://github.com/samdmarshall/pyconfig/blob/develop/Dangerfile
  5. # require changelog entry if number of lines changed is beyond this
  6. @S_CHANGE_LINES ||= 50
  7. # pattern list for included file paths
  8. @S_CHANGES_REQUIRED_PATTERNS ||= /^src\//
  9. # set the files to watch and warn about if there are changes made
  10. @S_BUILD_FILES ||= [
  11. "Makefile",
  12. # JavaScript
  13. ".eslintignore",
  14. ".eslintrc",
  15. # Python
  16. "setup.cfg",
  17. "setup.py",
  18. "tox.ini",
  19. # Danger
  20. "Dangerfile",
  21. # CI
  22. ".travis.yml",
  23. ]
  24. # set the files to watch and fail if there are changes
  25. @S_LICENSE_FILES ||= ["LICENSE"]
  26. # set the patterns to watch and warn about if they need security review
  27. @S_SECURITY_FILE_PATTERN ||= /Dangerfile|(auth|login|permission|email|twofactor|sudo).*\.py/
  28. # content changes within the diff
  29. @S_SECURITY_CONTENT_PATTERN ||= nil
  30. # dont ever match against changes in these files
  31. @S_SECURITY_EXCLUDE_FILES ||= /test_.*\.py|migrations|south_migrations|CHANGES|tests|yarn\.lock|\.html|\.jsx/
  32. @S_BACKPORTED_FILES ||= [
  33. "src/sentry/auth/password_validation.py",
  34. ]
  35. # warn if there are migrations
  36. @S_MIGRATIONS ||= /south_migrations/
  37. # determine if any of the files were modified
  38. def checkFiles(files_array)
  39. files_array.select { |f| git.modified_files.include?(f) }
  40. end
  41. def checkFilesPattern(pattern, exclude = nil)
  42. return [] unless pattern
  43. git.modified_files.select do |f|
  44. next(false) if exclude && exclude =~ f
  45. next(pattern =~ f)
  46. end
  47. end
  48. def checkContents(pattern, exclude = nil)
  49. return [] unless pattern
  50. git.modified_files.select do |f|
  51. next(false) if exclude && exclude =~ f
  52. next(git.diff_for_file(f).patch =~ pattern)
  53. end
  54. end
  55. # Warn about changes to dependencies or the build process
  56. warn("Changes to build requirements") if checkFiles(@S_BUILD_FILES).any?
  57. # Warn about changes to dependencies or the build process
  58. securityMatches = checkFilesPattern(@S_SECURITY_FILE_PATTERN, @S_SECURITY_EXCLUDE_FILES) + checkContents(@S_SECURITY_CONTENT_PATTERN, @S_SECURITY_EXCLUDE_FILES)
  59. if securityMatches.any?
  60. unless github.pr_labels.include?("Security")
  61. github.api.update_issue(github.pr_json["head"]["repo"]["full_name"], github.pr_json["number"], {
  62. :labels => github.pr_labels + ["Security"],
  63. })
  64. end
  65. # TODO(dcramer): when GitHub API actually exposes reviewers, we should
  66. # make this failing
  67. # securityTeam = github.api.organization_teams('getsentry')[0]
  68. # Make a note about contributors not in the organization
  69. # unless github.api.team_member?(securityTeam.id, github.pr_author
  70. warn("Changes require @getsentry/security sign-off")
  71. message = "### Security concerns found\n\n"
  72. securityMatches.to_set.each do |m|
  73. message << "- #{m}\n"
  74. end
  75. markdown(message)
  76. end
  77. # Make it more obvious that a PR is a work in progress and shouldn"t be merged yet
  78. warn("PR is classed as Work in Progress") if github.pr_title.include? "[WIP]" || github.pr_body.include?("#wip")
  79. # License is immutable
  80. fail("Do not modify the License") if @S_LICENSE_FILES && checkFiles(@S_LICENSE_FILES).any?
  81. # Notify about modifications to files that we've backported explicitly
  82. warn("This change includes modification to a file that was backported from newer Django.") if @S_BACKPORTED_FILES && checkFiles(@S_BACKPORTED_FILES).any?
  83. # Reasonable commits must update CHANGES
  84. if !github.pr_body.include?("#nochanges") && @S_CHANGE_LINES && git.lines_of_code > @S_CHANGE_LINES && !git.modified_files.include?("CHANGES") && checkFilesPattern(@S_CHANGES_REQUIRED_PATTERNS).any?
  85. warn("You should update CHANGES due to the size of this PR")
  86. end
  87. if git.added_files.grep(@S_MIGRATIONS).any?
  88. warn("PR includes migrations")
  89. markdown("## Migration Checklist\n\n" +
  90. "- [ ] new columns need to be nullable (unless table is new)\n" +
  91. "- [ ] migration with any new index needs to be done concurrently\n" +
  92. "- [ ] data migrations should not be done inside a transaction\n" +
  93. "- [ ] before merging, check to make sure there aren't conflicting migration ids\n"
  94. )
  95. end