|
| 1 | +# Adapted from https://github.com/capistrano/danger/blob/master/Dangerfile |
| 2 | +# Q: What is a Dangerfile, anyway? A: See http://danger.systems/ |
| 3 | + |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | +# Additional pull request data |
| 6 | +# ------------------------------------------------------------------------------ |
| 7 | +pr_number = github.pr_json["number"] |
| 8 | +pr_url = github.pr_json["_links"]["html"]["href"] |
| 9 | +# Sometimes its a README fix, or something like that - which isn't relevant for |
| 10 | +# including in a CHANGELOG for example |
| 11 | +declared_trivial = (github.pr_title + github.pr_body).include?("#trivial") |
| 12 | + |
| 13 | +# Just to let people know |
| 14 | +warn("PR is classed as Work in Progress") if github.pr_title.include? "[WIP]" |
| 15 | + |
| 16 | +# Ensure a clean commits history |
| 17 | +if git.commits.any? { |c| c.message =~ /^Merge branch '#{github.branch_for_base}'/ } |
| 18 | + fail('Please rebase to get rid of the merge commits in this PR') |
| 19 | +end |
| 20 | +commit_lint.check disable: [:subject_cap, :subject_period] |
| 21 | + |
| 22 | +# Check code style with clang-format |
| 23 | +code_style_validation.check file_extensions: ['.hpp', '.cpp', '.h'], ignore_file_patterns: [/^external\//] |
| 24 | + |
| 25 | +# ------------------------------------------------------------------------------ |
| 26 | +# What changed? |
| 27 | +# ------------------------------------------------------------------------------ |
| 28 | +# Were source files added? Were source files deleted? |
| 29 | +# We look for added and removed files in the api, include, src, tests and tools directories. |
| 30 | +# Files whose license header might need to be updated are kept here. |
| 31 | +has_added_files = !git.added_files.grep(/^(api|include|src|tests|tools)/).empty? |
| 32 | +has_deleted_files = !git.deleted_files.grep(/^(api|include|src|tests|tools)/).empty? |
| 33 | +# Was any code modified in the api, cmake, external, include, src and tools directories? |
| 34 | +has_code_changes = !git.modified_files.grep(/^(api|cmake|external|include|src|tools)/).empty? |
| 35 | +# Was any code modified in the tests directory? |
| 36 | +has_test_changes = !git.modified_files.grep(/^tests/).empty? |
| 37 | +# Was the CHANGELOG.md file modified? |
| 38 | +has_changelog_changes = git.modified_files.include?("CHANGELOG.md") |
| 39 | +# Was the .gitattributes file modified? |
| 40 | +# .gitattributes is used to keep track of license headers and has to be updated |
| 41 | +# if files are added or removed |
| 42 | +has_gitattributes_changes = git.modified_files.include?(".gitattributes") |
| 43 | +# Was documentation added? |
| 44 | +has_doc_changes = !git.modified_files.grep(/^doc/).empty? |
| 45 | + |
| 46 | +# ------------------------------------------------------------------------------ |
| 47 | +# You've made changes to api|cmake|external|include|src|tools, |
| 48 | +# but didn't write any tests? |
| 49 | +# ------------------------------------------------------------------------------ |
| 50 | +if has_code_changes && !has_test_changes |
| 51 | + if %w(tests).any? { |dir| Dir.exist?(dir) } |
| 52 | + warn("There are code changes, but no corresponding tests. "\ |
| 53 | + "Please include tests if this PR introduces any modifications in "\ |
| 54 | + "behavior.", |
| 55 | + :sticky => false) |
| 56 | + else |
| 57 | + markdown <<-MARKDOWN |
| 58 | +Thanks for the PR! This project lacks automated tests, which makes reviewing and approving PRs somewhat difficult. Please make sure that your contribution has not broken backwards compatibility or introduced any risky changes. |
| 59 | +
|
| 60 | +MARKDOWN |
| 61 | + end |
| 62 | +end |
| 63 | + |
| 64 | +# ------------------------------------------------------------------------------ |
| 65 | +# You've made nontrivial changes to api|cmake|external|include|src|tools, |
| 66 | +# but didn't write any docs? |
| 67 | +# ------------------------------------------------------------------------------ |
| 68 | +doc_changes_recommended = git.insertions > 15 |
| 69 | +if has_code_changes && !has_doc_changes && doc_changes_recommended && not_declared_trivial |
| 70 | + warn("Consider adding supporting documentation to this change. Documentation sources can be found in the `doc` directory.") |
| 71 | +end |
| 72 | + |
| 73 | +# ------------------------------------------------------------------------------ |
| 74 | +# Have you updated CHANGELOG.md? |
| 75 | +# ------------------------------------------------------------------------------ |
| 76 | +if !has_changelog_changes && has_code_changes |
| 77 | + markdown <<-MARKDOWN |
| 78 | +Here's an example of a CHANGELOG.md entry: |
| 79 | +
|
| 80 | +```markdown |
| 81 | +* [##{pr_number}](#{pr_url}): #{github.pr_title} - [@#{github.pr_author}](https://github.com/#{github.pr_author}) |
| 82 | +``` |
| 83 | +MARKDOWN |
| 84 | + warn("Please update CHANGELOG.md with a description of your changes. "\ |
| 85 | + "If this PR is not a user-facing change (e.g. just refactoring), "\ |
| 86 | + "you can disregard this.", :sticky => false) |
| 87 | +end |
| 88 | + |
| 89 | +# ------------------------------------------------------------------------------ |
| 90 | +# You've made changes to api|include|src|tests|tools, |
| 91 | +# but didn't update .gitattributes? |
| 92 | +# ------------------------------------------------------------------------------ |
| 93 | +if has_added_files && has_deleted_files && !has_gitattributes_changes |
| 94 | + warn("You have added source files without updating `.gitattributes`.", :sticky => false) |
| 95 | +end |
| 96 | +if has_deleted_files && !has_gitattributes_changes |
| 97 | + warn("You have removed source files without updating `.gitattributes`.", :sticky => false) |
| 98 | +end |
| 99 | + |
| 100 | +lgtm.check_lgtm |
0 commit comments