forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dangerfile
88 lines (73 loc) · 2.68 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
require 'open3'
# Warn when there is a big PR
warn('Big PR') if git.lines_of_code > 500
# Ensure a clean commit history
if git.commits.any? { |c| c.message =~ /^Merge branch '#{github.branch_for_base}'/ }
fail('Please rebase to get rid of the merge commits in this PR')
end
modified_files = git.modified_files + git.added_files
# Sometimes its a README fix, or something like that - which isn't relevant for
# including in a CHANGELOG for example
has_app_changes = !modified_files.grep(/Source/).empty?
has_test_changes = !modified_files.grep(/Tests/).empty?
has_danger_changes = !modified_files.grep(/Dangerfile|tools\/oss-check|Gemfile/).empty?
has_package_changes = !modified_files.grep(/Package\.swift/).empty?
has_bazel_changes = !modified_files.grep(/\.bazelrc|\.bazelversion|WORKSPACE|bazel\/|BUILD|MODULE\.bazel/).empty?
# Add a CHANGELOG entry for app changes
if !modified_files.include?('CHANGELOG.md') && has_app_changes
warn("If this is a user-facing change, please include a CHANGELOG entry to credit yourself! \nYou can find it at [CHANGELOG.md](https://github.com/realm/SwiftLint/blob/main/CHANGELOG.md).")
markdown <<-MARKDOWN
Here's an example of your CHANGELOG entry:
```markdown
* #{github.pr_title}.#{' '}
[#{github.pr_author}](https://github.com/#{github.pr_author})
[#issue_number](https://github.com/realm/SwiftLint/issues/issue_number)
```
*note*: There are two invisible spaces after the entry's text.
MARKDOWN
end
return unless has_app_changes || has_danger_changes || has_package_changes || has_bazel_changes
# Non-trivial amounts of app changes without tests
if git.lines_of_code > 50 && has_app_changes && !has_test_changes
warn 'This PR may need tests.'
end
# Run OSSCheck if there were app changes
def non_empty_lines(lines)
lines.split(/\n+/).reject(&:empty?)
end
def parse_line(line)
line.split(':', 2).last.strip
end
file = Tempfile.new('violations')
force_flag = has_danger_changes ? "--force" : ""
begin
Open3.popen3("tools/oss-check -v #{force_flag} 2> #{file.path}") do |_, stdout, _, _|
while char = stdout.getc
print char
end
end
rescue SignalException => e
if e.signo == Signal.list["TERM"]
# Post a nicer message here
fail "OSSCheck was cancelled"
else
raise e
end
rescue => e
# Handle other exceptions
fail "OSSCheck error: #{e.message}"
end
lines = file.read.chomp
file.close
file.unlink
non_empty_lines(lines).each do |line|
if line.include? 'Permanently added the RSA host key for IP address'
# Don't report to Danger
elsif line.start_with? 'Message:'
message parse_line(line)
elsif line.start_with? 'Warning:'
warn parse_line(line)
elsif line.start_with? 'Error:'
fail parse_line(line)
end
end