-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dangerfile
119 lines (92 loc) · 3.52 KB
/
Dangerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# frozen_string_literal: true
# See http://danger.systems/reference.html for a reference.
# Various checks
def big_pr?
git.lines_of_code > 200
end
def work_in_progress?
github.pr_title.include?('WIP') ||
github.pr_labels.include?('wip') ||
github.pr_labels.include?('work-in-progress') ||
github.pr_labels.include?('work in progress') ||
github.pr_labels.include?('in progress')
end
def test_changes?
git.modified_files.grep(/(spec|test)/).any? ||
git.added_files.grep(/(spec|test)/).any?
end
# TODO: Check if the project uses rspec or minitest and add checks for minitest
def focused_spec_exists?
`grep -r 'focus: true' spec/`.length > 1
end
def schema_changed?
git.modified_files.grep('schema.rb').any?
end
def short_summary?
github.pr_body.length < 5
end
def coffee_script_added?
git.added_files.any? { |file| file.end_with?('coffee') }
end
def controller_specs_added?
git.added_files.any? { |file| file.include?('controllers') && file.end_with?('.rb') }
end
def vcr_cassettes_changed?
git.modified_files.any? { |file| file.include?('vcr_cassettes') } ||
git.added_files.any? { |file| file.include?('vcr_cassettes') }
end
def short_commit_messages?
git.commits.any? { |c| c.message.split(' ').length < 3 }
end
def message_table(checks)
message_string = "### Pull Request Checklist for reviewer\n\n"
link = 'https://github.com/thoughtbot/guides/tree/master/code-review'
message_string += "Be sure to follow follow [good code review guidelines](#{link}).\n"
checks.shuffle.each do |check|
message_string += "* [ ] #{check}\n"
end
message_string
end
# allows to add labels to an issue
def add_labels(labels:)
repo = github.pr_json['head']['repo']['full_name']
id = github.pr_json['id']
github.api.add_labels_to_an_issue(repo, id, labels)
end
# Print our PR checklist
checks = [
'Tests for new features or bug fixes have been added',
'There are no known bugs',
"Commit messages are explanatory (no 'fixed', 'try', etc.)"
]
# Prevent merging of WIP Pull Requests
failure('PR is classed as Work in Progress. Do not merge.') if work_in_progress?
# Don't let testing shortcuts get into master by accident
failure("'focus: true' left in specs") if focused_spec_exists?
# We do not want to have new CoffeeScript files. We should prefer plain Javascript for now.
failure('Please use Javascript over CoffeeScript') if coffee_script_added?
# Controller specs are deprecated
failure('Please use request specs over controller specs') if controller_specs_added?
if big_pr?
checks << 'Added [yard](http://www.rubydoc.info/gems/yard/file/docs/GettingStarted.md) documentation for new features'
warn('Big PR. Please review carefully.')
end
if schema_changed?
warn('Changes to config/schema.db detected. Please review changes to this file carefully.')
end
# Mainly to encourage writing up some reasoning about the PR, rather than
# just leaving a title
warn('Please provide a summary in the Pull Request description') if short_summary?
# Ensure git commit messages are longer than 3 words
if short_commit_messages?
warn(
'Some of the commit messages look really short. '\
'They probably could be made more descriptive for other people.'
)
end
# Warn on missing tests
warn('Tests were not updated', sticky: false) unless test_changes?
# vcr_cassettes are generated automatically, most of the time, no need to review them.
message(":eyes: You don't have to review the vcr_cassettes changes.") if vcr_cassettes_changed?
markdown(message_table(checks))
rubocop.lint(force_exclusion: true, report_danger: true)