| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:41:14 +01:00
										 |  |  | require "rubocops/extend/formula" | 
					
						
							| 
									
										
										
										
											2018-09-02 23:30:07 +02:00
										 |  |  | require "extend/string" | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     module FormulaAudit | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |       # This cop audits patches in Formulae. | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |       class Patches < FormulaCop | 
					
						
							|  |  |  |         def audit_formula(_node, _class_node, _parent_class_node, body) | 
					
						
							|  |  |  |           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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def patch_problems(patch) | 
					
						
							|  |  |  |           patch_url = string_content(patch) | 
					
						
							| 
									
										
										
										
											2017-08-25 20:07:45 +05:30
										 |  |  |           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) | 
					
						
							| 
									
										
										
										
											2019-10-13 19:26:39 +01:00
										 |  |  |             unless patch_url.match?(/\?full_index=\w+$/) | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |               problem <<~EOS | 
					
						
							| 
									
										
										
										
											2017-08-25 20:07:45 +05:30
										 |  |  |                 GitHub patches should use the full_index parameter: | 
					
						
							|  |  |  |                   #{patch_url}?full_index=1 | 
					
						
							|  |  |  |               EOS | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |           gh_patch_patterns = Regexp.union([%r{/raw\.github\.com/}, | 
					
						
							|  |  |  |                                             %r{gist\.github\.com/raw}, | 
					
						
							|  |  |  |                                             %r{gist\.github\.com/.+/raw}, | 
					
						
							|  |  |  |                                             %r{gist\.githubusercontent\.com/.+/raw}]) | 
					
						
							|  |  |  |           if regex_match_group(patch, gh_patch_patterns) | 
					
						
							| 
									
										
										
										
											2019-10-13 19:26:39 +01:00
										 |  |  |             unless patch_url.match?(/[a-fA-F0-9]{40}/) | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |               problem <<~EOS.chomp | 
					
						
							| 
									
										
										
										
											2017-08-17 11:41:58 +05:30
										 |  |  |                 GitHub/Gist patches should specify a revision: | 
					
						
							| 
									
										
										
										
											2019-04-01 16:02:13 -04:00
										 |  |  |                   #{patch_url} | 
					
						
							| 
									
										
										
										
											2017-08-17 11:41:58 +05:30
										 |  |  |               EOS | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |           gh_patch_diff_pattern = | 
					
						
							|  |  |  |             %r{https?://patch-diff\.githubusercontent\.com/raw/(.+)/(.+)/pull/(.+)\.(?:diff|patch)} | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |           if match_obj = regex_match_group(patch, gh_patch_diff_pattern) | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |             problem <<~EOS | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |               use GitHub pull request URLs: | 
					
						
							|  |  |  |                 https://github.com/#{match_obj[1]}/#{match_obj[2]}/pull/#{match_obj[3]}.patch | 
					
						
							|  |  |  |               Rather than patch-diff: | 
					
						
							|  |  |  |                 #{patch_url} | 
					
						
							|  |  |  |             EOS | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if regex_match_group(patch, %r{macports/trunk}) | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |             problem <<~EOS.chomp | 
					
						
							| 
									
										
										
										
											2017-08-17 11:41:58 +05:30
										 |  |  |               MacPorts patches should specify a revision instead of trunk: | 
					
						
							| 
									
										
										
										
											2019-04-01 16:02:13 -04:00
										 |  |  |                 #{patch_url} | 
					
						
							| 
									
										
										
										
											2017-08-17 11:41:58 +05:30
										 |  |  |             EOS | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if regex_match_group(patch, %r{^http://trac\.macports\.org}) | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |             problem <<~EOS.chomp | 
					
						
							| 
									
										
										
										
											2017-08-17 11:41:58 +05:30
										 |  |  |               Patches from MacPorts Trac should be https://, not http: | 
					
						
							| 
									
										
										
										
											2019-04-01 16:02:13 -04:00
										 |  |  |                 #{patch_url} | 
					
						
							| 
									
										
										
										
											2017-08-17 11:41:58 +05:30
										 |  |  |             EOS | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           return unless regex_match_group(patch, %r{^http://bugs\.debian\.org}) | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |           problem <<~EOS.chomp | 
					
						
							| 
									
										
										
										
											2017-08-17 11:41:58 +05:30
										 |  |  |             Patches from Debian should be https://, not http: | 
					
						
							| 
									
										
										
										
											2019-04-01 16:02:13 -04:00
										 |  |  |               #{patch_url} | 
					
						
							| 
									
										
										
										
											2017-08-17 11:41:58 +05:30
										 |  |  |           EOS | 
					
						
							| 
									
										
										
										
											2017-08-01 20:59:17 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |