| 
									
										
										
										
											2025-02-12 16:53:20 -08:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2024-06-17 19:11:26 +02:00
										 |  |  | require "rubocops/shared/url_helper" | 
					
						
							| 
									
										
										
										
											2017-07-19 01:14:14 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     module FormulaAudit | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |       # This cop audits `url`s and `mirror`s in formulae. | 
					
						
							| 
									
										
										
										
											2023-02-20 18:10:59 -08:00
										 |  |  |       class Urls < FormulaCop | 
					
						
							| 
									
										
										
										
											2024-06-17 19:11:26 +02:00
										 |  |  |         include UrlHelper | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-07 15:18:29 -04:00
										 |  |  |         sig { override.params(formula_nodes: FormulaNodes).void } | 
					
						
							|  |  |  |         def audit_formula(formula_nodes) | 
					
						
							|  |  |  |           return if (body_node = formula_nodes.body_node).nil? | 
					
						
							| 
									
										
										
										
											2022-11-05 04:17:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-10 03:22:28 +05:30
										 |  |  |           urls = find_every_func_call_by_name(body_node, :url) | 
					
						
							|  |  |  |           mirrors = find_every_func_call_by_name(body_node, :mirror) | 
					
						
							| 
									
										
										
										
											2017-07-19 01:14:14 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |           # Identify livecheck URLs, to skip some checks for them | 
					
						
							| 
									
										
										
										
											2020-05-22 15:18:01 +05:30
										 |  |  |           livecheck_url = if (livecheck = find_every_func_call_by_name(body_node, :livecheck).first) && | 
					
						
							|  |  |  |                              (livecheck_url = find_every_func_call_by_name(livecheck.parent, :url).first) | 
					
						
							|  |  |  |             string_content(parameters(livecheck_url).first) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-17 19:11:26 +02:00
										 |  |  |           audit_url(:formula, urls, mirrors, livecheck_url:) | 
					
						
							| 
									
										
										
										
											2019-09-02 10:50:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |           return if formula_tap != "homebrew-core" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           # Check for binary URLs | 
					
						
							| 
									
										
										
										
											2020-04-02 09:32:52 +01:00
										 |  |  |           audit_urls(urls, /(darwin|macos|osx)/i) do |match, url| | 
					
						
							| 
									
										
										
										
											2025-02-01 23:54:35 +00:00
										 |  |  |             next if T.must(@formula_name).include?(match.to_s.downcase) | 
					
						
							| 
									
										
										
										
											2020-04-02 09:32:52 +01:00
										 |  |  |             next if url.match?(/.(patch|diff)(\?full_index=1)?$/) | 
					
						
							| 
									
										
										
										
											2020-11-27 12:47:01 -05:00
										 |  |  |             next if tap_style_exception? :not_a_binary_url_prefix_allowlist | 
					
						
							| 
									
										
										
										
											2020-11-27 12:36:30 -05:00
										 |  |  |             next if tap_style_exception? :binary_bootstrap_formula_urls_allowlist | 
					
						
							| 
									
										
										
										
											2019-09-02 10:50:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 15:38:48 -05:00
										 |  |  |             problem "#{url} looks like a binary package, not a source archive; " \ | 
					
						
							|  |  |  |                     "homebrew/core is source-only." | 
					
						
							| 
									
										
										
										
											2019-09-02 10:50:49 +01:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-07-19 01:14:14 +05:30
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2017-07-30 12:57:57 +05:30
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-04-25 07:35:26 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |       # This cop makes sure that the correct format for PyPI URLs is used. | 
					
						
							| 
									
										
										
										
											2023-02-20 18:10:59 -08:00
										 |  |  |       class PyPiUrls < FormulaCop | 
					
						
							| 
									
										
										
										
											2024-07-07 15:18:29 -04:00
										 |  |  |         sig { override.params(formula_nodes: FormulaNodes).void } | 
					
						
							|  |  |  |         def audit_formula(formula_nodes) | 
					
						
							|  |  |  |           return if (body_node = formula_nodes.body_node).nil? | 
					
						
							| 
									
										
										
										
											2022-11-05 04:17:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-10 03:22:28 +05:30
										 |  |  |           urls = find_every_func_call_by_name(body_node, :url) | 
					
						
							|  |  |  |           mirrors = find_every_func_call_by_name(body_node, :mirror) | 
					
						
							| 
									
										
										
										
											2017-07-30 12:57:57 +05:30
										 |  |  |           urls += mirrors | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |           # Check pypi URLs | 
					
						
							| 
									
										
										
										
											2022-04-15 16:39:14 +01:00
										 |  |  |           pypi_pattern = %r{^https?://pypi\.python\.org/} | 
					
						
							| 
									
										
										
										
											2020-07-18 14:22:05 -04:00
										 |  |  |           audit_urls(urls, pypi_pattern) do |_, url| | 
					
						
							|  |  |  |             problem "use the `Source` url found on PyPI downloads page (`#{get_pypi_url(url)}`)" | 
					
						
							| 
									
										
										
										
											2017-07-30 12:57:57 +05:30
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-07-19 01:14:14 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |           # Require long files.pythonhosted.org URLs | 
					
						
							| 
									
										
										
										
											2022-04-15 16:39:14 +01:00
										 |  |  |           pythonhosted_pattern = %r{^https?://files\.pythonhosted\.org/packages/source/} | 
					
						
							| 
									
										
										
										
											2020-07-18 14:22:05 -04:00
										 |  |  |           audit_urls(urls, pythonhosted_pattern) do |_, url| | 
					
						
							|  |  |  |             problem "use the `Source` url found on PyPI downloads page (`#{get_pypi_url(url)}`)" | 
					
						
							| 
									
										
										
										
											2017-07-19 01:14:14 +05:30
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2020-07-18 14:22:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |         sig { params(url: String).returns(String) } | 
					
						
							| 
									
										
										
										
											2020-07-18 14:22:05 -04:00
										 |  |  |         def get_pypi_url(url) | 
					
						
							|  |  |  |           package_file = File.basename(url) | 
					
						
							| 
									
										
										
										
											2023-02-14 19:19:37 -08:00
										 |  |  |           package_name = T.must(package_file.match(/^(.+)-[a-z0-9.]+$/))[1] | 
					
						
							| 
									
										
										
										
											2020-07-18 14:22:05 -04:00
										 |  |  |           "https://pypi.org/project/#{package_name}/#files" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2017-07-19 01:14:14 +05:30
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-10-03 12:27:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |       # This cop makes sure that git URLs have a `revision`. | 
					
						
							| 
									
										
										
										
											2023-02-20 18:10:59 -08:00
										 |  |  |       class GitUrls < FormulaCop | 
					
						
							| 
									
										
										
										
											2024-07-07 15:18:29 -04:00
										 |  |  |         sig { override.params(formula_nodes: FormulaNodes).void } | 
					
						
							|  |  |  |         def audit_formula(formula_nodes) | 
					
						
							|  |  |  |           return if (body_node = formula_nodes.body_node).nil? | 
					
						
							| 
									
										
										
										
											2023-04-18 15:06:50 -07:00
										 |  |  |           return if formula_tap != "homebrew-core" | 
					
						
							| 
									
										
										
										
											2020-10-03 15:51:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-03 12:27:01 -04:00
										 |  |  |           find_method_calls_by_name(body_node, :url).each do |url| | 
					
						
							|  |  |  |             next unless string_content(parameters(url).first).match?(/\.git$/) | 
					
						
							| 
									
										
										
										
											2020-10-03 15:51:30 -04:00
										 |  |  |             next if url_has_revision?(parameters(url).last) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             offending_node(url) | 
					
						
							| 
									
										
										
										
											2020-07-29 17:31:11 -04:00
										 |  |  |             problem "Formulae in homebrew/core should specify a revision for git URLs" | 
					
						
							| 
									
										
										
										
											2020-10-03 15:51:30 -04:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def_node_matcher :url_has_revision?, <<~EOS | 
					
						
							|  |  |  |           (hash <(pair (sym :revision) str) ...>) | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     module FormulaAuditStrict | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |       # This cop makes sure that git URLs have a `tag`. | 
					
						
							| 
									
										
										
										
											2023-02-20 18:10:59 -08:00
										 |  |  |       class GitUrls < FormulaCop | 
					
						
							| 
									
										
										
										
											2024-07-07 15:18:29 -04:00
										 |  |  |         sig { override.params(formula_nodes: FormulaNodes).void } | 
					
						
							|  |  |  |         def audit_formula(formula_nodes) | 
					
						
							|  |  |  |           return if (body_node = formula_nodes.body_node).nil? | 
					
						
							| 
									
										
										
										
											2023-04-18 15:06:50 -07:00
										 |  |  |           return if formula_tap != "homebrew-core" | 
					
						
							| 
									
										
										
										
											2020-10-03 12:27:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-03 15:51:30 -04:00
										 |  |  |           find_method_calls_by_name(body_node, :url).each do |url| | 
					
						
							|  |  |  |             next unless string_content(parameters(url).first).match?(/\.git$/) | 
					
						
							|  |  |  |             next if url_has_tag?(parameters(url).last) | 
					
						
							| 
									
										
										
										
											2020-10-03 12:27:01 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             offending_node(url) | 
					
						
							| 
									
										
										
										
											2020-07-29 17:31:11 -04:00
										 |  |  |             problem "Formulae in homebrew/core should specify a tag for git URLs" | 
					
						
							| 
									
										
										
										
											2020-10-03 12:27:01 -04:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-03 15:51:30 -04:00
										 |  |  |         def_node_matcher :url_has_tag?, <<~EOS | 
					
						
							|  |  |  |           (hash <(pair (sym :tag) str) ...>) | 
					
						
							| 
									
										
										
										
											2020-10-03 12:27:01 -04:00
										 |  |  |         EOS | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-07-19 01:14:14 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |