| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-20 10:22:39 -08:00
										 |  |  | require "rubocops/extend/formula_cop" | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     module FormulaAudit | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |       # This cop audits `patch`es in formulae. | 
					
						
							| 
									
										
										
										
											2021-01-12 16:54:53 +11:00
										 |  |  |       # TODO: Many of these could be auto-corrected. | 
					
						
							| 
									
										
										
										
											2023-02-20 18:10:59 -08:00
										 |  |  |       class Patches < FormulaCop | 
					
						
							| 
									
										
										
										
											2021-02-26 00:48:03 +05:30
										 |  |  |         extend AutoCorrector | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 01:10:52 +08:00
										 |  |  |         def audit_formula(node, _class_node, _parent_class_node, body) | 
					
						
							|  |  |  |           @full_source_content = source_buffer(node).source | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-05 04:17:50 +00:00
										 |  |  |           return if body.nil? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |           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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-27 03:13:50 +08:00
										 |  |  |           inline_patches = find_every_method_call_by_name(body, :patch) | 
					
						
							| 
									
										
										
										
											2020-06-27 01:10:52 +08:00
										 |  |  |           inline_patches.each { |patch| inline_patch_problems(patch) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if inline_patches.empty? && patch_end? | 
					
						
							|  |  |  |             offending_patch_end_node(node) | 
					
						
							| 
									
										
										
										
											2021-01-12 16:54:53 +11:00
										 |  |  |             add_offense(@offense_source_range, message: "patch is missing 'DATA'") | 
					
						
							| 
									
										
										
										
											2020-06-27 01:10:52 +08:00
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |           patches_node = find_method_def(body, :patches) | 
					
						
							|  |  |  |           return if patches_node.nil? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |           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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-26 00:48:03 +05:30
										 |  |  |         def patch_problems(patch_url_node) | 
					
						
							|  |  |  |           patch_url = string_content(patch_url_node) | 
					
						
							| 
									
										
										
										
											2020-07-24 20:49:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-26 00:48:03 +05:30
										 |  |  |           if regex_match_group(patch_url_node, %r{https://github.com/[^/]*/[^/]*/pull}) | 
					
						
							| 
									
										
										
										
											2020-07-24 20:49:26 -04:00
										 |  |  |             problem "Use a commit hash URL rather than an unstable pull request URL: #{patch_url}" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-26 00:48:03 +05:30
										 |  |  |           if regex_match_group(patch_url_node, %r{.*gitlab.*/merge_request.*}) | 
					
						
							| 
									
										
										
										
											2020-07-24 20:49:26 -04:00
										 |  |  |             problem "Use a commit hash URL rather than an unstable merge request URL: #{patch_url}" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-26 00:48:03 +05:30
										 |  |  |           if regex_match_group(patch_url_node, %r{https://github.com/[^/]*/[^/]*/commit/[a-fA-F0-9]*\.diff}) | 
					
						
							| 
									
										
										
										
											2021-04-17 01:44:28 +05:30
										 |  |  |             problem "GitHub patches should end with .patch, not .diff: #{patch_url}" | 
					
						
							| 
									
										
										
										
											2020-09-11 09:07:20 -07:00
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-02 19:10:58 +11:00
										 |  |  |           # Only .diff passes `--full-index` to `git diff` and there is no documented way | 
					
						
							|  |  |  |           # to get .patch to behave the same for GitLab. | 
					
						
							| 
									
										
										
										
											2021-04-19 11:19:35 -04:00
										 |  |  |           if regex_match_group(patch_url_node, %r{.*gitlab.*/commit/[a-fA-F0-9]*\.patch}) | 
					
						
							|  |  |  |             problem "GitLab patches should end with .diff, not .patch: #{patch_url}" | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2020-09-11 09:07:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-25 20:07:45 +05:30
										 |  |  |           gh_patch_param_pattern = %r{https?://github\.com/.+/.+/(?:commit|pull)/[a-fA-F0-9]*.(?:patch|diff)} | 
					
						
							| 
									
										
										
										
											2021-02-26 00:48:03 +05:30
										 |  |  |           if regex_match_group(patch_url_node, gh_patch_param_pattern) && !patch_url.match?(/\?full_index=\w+$/) | 
					
						
							| 
									
										
										
										
											2021-04-17 01:44:28 +05:30
										 |  |  |             problem "GitHub patches should use the full_index parameter: #{patch_url}?full_index=1" | 
					
						
							| 
									
										
										
										
											2017-08-25 20:07:45 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |           gh_patch_patterns = Regexp.union([%r{/raw\.github\.com/}, | 
					
						
							| 
									
										
										
										
											2020-03-08 20:13:36 +00:00
										 |  |  |                                             %r{/raw\.githubusercontent\.com/}, | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |                                             %r{gist\.github\.com/raw}, | 
					
						
							|  |  |  |                                             %r{gist\.github\.com/.+/raw}, | 
					
						
							|  |  |  |                                             %r{gist\.githubusercontent\.com/.+/raw}]) | 
					
						
							| 
									
										
										
										
											2021-02-26 00:48:03 +05:30
										 |  |  |           if regex_match_group(patch_url_node, gh_patch_patterns) && !patch_url.match?(%r{/[a-fA-F0-9]{6,40}/}) | 
					
						
							|  |  |  |             problem "GitHub/Gist patches should specify a revision: #{patch_url}" | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |           gh_patch_diff_pattern = | 
					
						
							|  |  |  |             %r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)} | 
					
						
							| 
									
										
										
										
											2021-02-26 00:48:03 +05:30
										 |  |  |           if regex_match_group(patch_url_node, gh_patch_diff_pattern) | 
					
						
							| 
									
										
										
										
											2020-07-24 20:49:26 -04:00
										 |  |  |             problem "Use a commit hash URL rather than patch-diff: #{patch_url}" | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-26 00:48:03 +05:30
										 |  |  |           if regex_match_group(patch_url_node, %r{macports/trunk}) | 
					
						
							|  |  |  |             problem "MacPorts patches should specify a revision instead of trunk: #{patch_url}" | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-26 00:48:03 +05:30
										 |  |  |           if regex_match_group(patch_url_node, %r{^http://trac\.macports\.org}) | 
					
						
							|  |  |  |             problem "Patches from MacPorts Trac should be https://, not http: #{patch_url}" do |corrector| | 
					
						
							|  |  |  |               correct = patch_url_node.source.gsub(%r{^http://}, "https://") | 
					
						
							|  |  |  |               corrector.replace(patch_url_node.source_range, correct) | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-26 00:48:03 +05:30
										 |  |  |           return unless regex_match_group(patch_url_node, %r{^http://bugs\.debian\.org}) | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-26 00:48:03 +05:30
										 |  |  |           problem "Patches from Debian should be https://, not http: #{patch_url}" do |corrector| | 
					
						
							|  |  |  |             correct = patch_url_node.source.gsub(%r{^http://}, "https://") | 
					
						
							|  |  |  |             corrector.replace(patch_url_node.source_range, correct) | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2020-06-27 01:10:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def inline_patch_problems(patch) | 
					
						
							| 
									
										
										
										
											2021-01-07 13:49:05 -08:00
										 |  |  |           return if !patch_data?(patch) || patch_end? | 
					
						
							| 
									
										
										
										
											2020-06-27 01:10:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           offending_node(patch) | 
					
						
							|  |  |  |           problem "patch is missing '__END__'" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def_node_search :patch_data?, <<~AST | 
					
						
							|  |  |  |           (send nil? :patch (:sym :DATA)) | 
					
						
							|  |  |  |         AST | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |         sig { returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2020-06-27 01:10:52 +08:00
										 |  |  |         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 | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |