-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDangerfile
73 lines (57 loc) · 2.98 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
github.dismiss_out_of_range_messages
# Warn for large PRs to encourage smaller, more focused changes
warn("Large PR detected, consider splitting it into smaller ones!") if git.lines_of_code > 250
# Warn if the PR title contains [WIP]
warn("This PR is marked as Work in Progress, do not merge!") if github.pr_title.include?("[WIP]")
# Warn if there are merge conflicts that need to be resolved
merge_conflicts = []
git.modified_files.each do |file|
next unless File.exist?(file) # Ensure the file exists before reading
File.readlines(file).each_with_index do |line, index|
if line.include?('<<<<<<')
# Attempt to extract the class name by looking for common class declarations (Kotlin/Java/other)
class_name = File.readlines(file).grep(/class\s+(\w+)/).first&.strip || "Unknown Class"
merge_conflicts << { file: file, line: index + 1, class: class_name, content: line.strip }
end
end
end
if merge_conflicts.any?
message = "This PR has unresolved merge conflicts. Please resolve them:\n\n" +
merge_conflicts.map do |conflict|
"- File: #{conflict[:file]}, Line: #{conflict[:line]}, Class: #{conflict[:class]} \n Conflict Line: #{conflict[:content]}"
end.join("\n")
fail(message)
end
# Ensure a PR description exists
fail("Please provide a meaningful description for the PR.") if github.pr_body.nil? || github.pr_body.strip.empty?
# Ensure that unit tests are updated or added for the changes made
test_files = git.modified_files.grep(%r{src/test/.*\.kt$})
implementation_files = git.modified_files.grep(%r{src/main/.*\.kt$})
if implementation_files.any? && test_files.empty?
warn("The following implementation files were modified, but no corresponding test files were updated or added. Please consider adding or updating tests:\n\n" +
implementation_files.map { |file| "- #{file}" }.join("\n"))
end
# Android lint integration
android_lint.report_file = "app/build/reports/lint-results-debug.xml"
android_lint.lint
# Fail if there are any TODOs in the modified code and list their file paths and line numbers
todo_locations = []
git.modified_files.each do |file|
next unless File.exist?(file) # Ensure the file exists before reading
File.readlines(file).each_with_index do |line, index|
if line.include?("TODO")
todo_locations << { file: file, line: index + 1, content: line.strip }
end
end
end
if todo_locations.any?
message = "This PR contains TODO comments. Please address them:\n\n" +
todo_locations.map { |todo| "- #{todo[:file]}:#{todo[:line]} - #{todo[:content]}" }.join("\n")
fail(message)
end
# Warn and list which build.gradle or build.gradle.kts files were modified
modified_build_files = git.modified_files.select { |file| file.include?('build.gradle') || file.include?('build.gradle.kts') }
if modified_build_files.any?
warn("Dependencies were modified in the following files. Ensure they are necessary and properly tested:\n\n" +
modified_build_files.map { |file| "- #{file}" }.join("\n"))
end