-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
patches.rb
124 lines (104 loc) 路 4.23 KB
/
patches.rb
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
120
121
122
123
124
# frozen_string_literal: true
require "rubocops/extend/formula"
require "extend/string"
module RuboCop
module Cop
module FormulaAudit
# This cop audits patches in Formulae.
class Patches < FormulaCop
def audit_formula(node, _class_node, _parent_class_node, body)
@full_source_content = source_buffer(node).source
external_patches = find_all_blocks(body, :patch)
external_patches.each do |patch_block|
url_node = find_every_method_call_by_name(patch_block, :url).first
url_string = parameters(url_node).first
patch_problems(url_string)
end
inline_patches = find_every_method_call_by_name(body, :patch)
inline_patches.each { |patch| inline_patch_problems(patch) }
if inline_patches.empty? && patch_end?
offending_patch_end_node(node)
problem "patch is missing 'DATA'"
end
patches_node = find_method_def(body, :patches)
return if patches_node.nil?
legacy_patches = find_strings(patches_node)
problem "Use the patch DSL instead of defining a 'patches' method"
legacy_patches.each { |p| patch_problems(p) }
end
private
def patch_problems(patch)
patch_url = string_content(patch)
gh_patch_param_pattern = %r{https?://github\.com/.+/.+/(?:commit|pull)/[a-fA-F0-9]*.(?:patch|diff)}
if regex_match_group(patch, gh_patch_param_pattern)
unless patch_url.match?(/\?full_index=\w+$/)
problem <<~EOS
GitHub patches should use the full_index parameter:
#{patch_url}?full_index=1
EOS
end
end
gh_patch_patterns = Regexp.union([%r{/raw\.github\.com/},
%r{/raw\.githubusercontent\.com/},
%r{gist\.github\.com/raw},
%r{gist\.github\.com/.+/raw},
%r{gist\.githubusercontent\.com/.+/raw}])
if regex_match_group(patch, gh_patch_patterns)
unless patch_url.match?(%r{/[a-fA-F0-9]{6,40}/})
problem <<~EOS.chomp
GitHub/Gist patches should specify a revision:
#{patch_url}
EOS
end
end
gh_patch_diff_pattern =
%r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)}
if match_obj = regex_match_group(patch, gh_patch_diff_pattern)
problem <<~EOS
use GitHub pull request URLs:
https://github.com/#{match_obj[1]}/#{match_obj[2]}/pull/#{match_obj[3]}.patch?full_index=1
Rather than patch-diff:
#{patch_url}
EOS
end
if regex_match_group(patch, %r{macports/trunk})
problem <<~EOS.chomp
MacPorts patches should specify a revision instead of trunk:
#{patch_url}
EOS
end
if regex_match_group(patch, %r{^http://trac\.macports\.org})
problem <<~EOS.chomp
Patches from MacPorts Trac should be https://, not http:
#{patch_url}
EOS
end
return unless regex_match_group(patch, %r{^http://bugs\.debian\.org})
problem <<~EOS.chomp
Patches from Debian should be https://, not http:
#{patch_url}
EOS
end
def inline_patch_problems(patch)
return unless patch_data?(patch) && !patch_end?
offending_node(patch)
problem "patch is missing '__END__'"
end
def_node_search :patch_data?, <<~AST
(send nil? :patch (:sym :DATA))
AST
def patch_end?
/^__END__$/.match?(@full_source_content)
end
def offending_patch_end_node(node)
@offensive_node = node
@source_buf = source_buffer(node)
@line_no = node.loc.last_line + 1
@column = 0
@length = 7 # "__END__".size
@offense_source_range = source_range(@source_buf, @line_no, @column, @length)
end
end
end
end
end