| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:41:14 +01:00
										 |  |  | require "rubocops/extend/formula" | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     module FormulaAudit | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |       # This cop audits the `homepage` URL in formulae. | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |       class Homepage < FormulaCop | 
					
						
							| 
									
										
										
										
											2021-01-12 15:58:52 +11:00
										 |  |  |         extend AutoCorrector | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-19 00:36:18 -04:00
										 |  |  |         def audit_formula(_node, _class_node, _parent_class_node, body_node) | 
					
						
							|  |  |  |           homepage_node = find_node_method_by_name(body_node, :homepage) | 
					
						
							| 
									
										
										
										
											2021-01-12 15:58:52 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |           if homepage_node.nil? | 
					
						
							|  |  |  |             problem "Formula should have a homepage." | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 15:58:52 +11:00
										 |  |  |           homepage_parameter_node = parameters(homepage_node).first | 
					
						
							|  |  |  |           offending_node(homepage_parameter_node) | 
					
						
							|  |  |  |           homepage = string_content(homepage_parameter_node) | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 15:58:52 +11:00
										 |  |  |           problem "Formula should have a homepage." if homepage.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           problem "The homepage should start with http or https." unless homepage.match?(%r{^https?://}) | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |           case homepage | 
					
						
							|  |  |  |           # Freedesktop is complicated to handle - It has SSL/TLS, but only on certain subdomains. | 
					
						
							|  |  |  |           # To enable https Freedesktop change the URL from http://project.freedesktop.org/wiki to | 
					
						
							|  |  |  |           # https://wiki.freedesktop.org/project_name. | 
					
						
							|  |  |  |           # "Software" is redirected to https://wiki.freedesktop.org/www/Software/project_name | 
					
						
							|  |  |  |           when %r{^http://((?:www|nice|libopenraw|liboil|telepathy|xorg)\.)?freedesktop\.org/(?:wiki/)?} | 
					
						
							| 
									
										
										
										
											2020-07-07 13:12:37 +01:00
										 |  |  |             if homepage.include?("Software") | 
					
						
							| 
									
										
										
										
											2022-06-28 10:09:59 +01:00
										 |  |  |               problem "Freedesktop homepages should be styled " \ | 
					
						
							| 
									
										
										
										
											2021-01-12 15:58:52 +11:00
										 |  |  |                       "`https://wiki.freedesktop.org/www/Software/project_name`" | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2021-01-12 15:58:52 +11:00
										 |  |  |               problem "Freedesktop homepages should be styled `https://wiki.freedesktop.org/project_name`" | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |             end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           # Google Code homepages should end in a slash | 
					
						
							|  |  |  |           when %r{^https?://code\.google\.com/p/[^/]+[^/]$} | 
					
						
							| 
									
										
										
										
											2021-01-12 15:58:52 +11:00
										 |  |  |             problem "Google Code homepages should end with a slash" do |corrector| | 
					
						
							|  |  |  |               corrector.replace(homepage_parameter_node.source_range, "\"#{homepage}/\"") | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |           when %r{^http://([^/]*)\.(sf|sourceforge)\.net(/|$)} | 
					
						
							| 
									
										
										
										
											2021-01-12 15:58:52 +11:00
										 |  |  |             fixed = "https://#{Regexp.last_match(1)}.sourceforge.io/" | 
					
						
							|  |  |  |             problem "Sourceforge homepages should be `#{fixed}`" do |corrector| | 
					
						
							|  |  |  |               corrector.replace(homepage_parameter_node.source_range, "\"#{fixed}\"") | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 22:37:02 -04:00
										 |  |  |           when /readthedocs\.org/ | 
					
						
							| 
									
										
										
										
											2021-01-12 15:58:52 +11:00
										 |  |  |             fixed = homepage.sub("readthedocs.org", "readthedocs.io") | 
					
						
							|  |  |  |             problem "Readthedocs homepages should be `#{fixed}`" do |corrector| | 
					
						
							|  |  |  |               corrector.replace(homepage_parameter_node.source_range, "\"#{fixed}\"") | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2020-08-03 22:37:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-01 00:03:48 -07:00
										 |  |  |           when %r{^https://github.com.*\.git$} | 
					
						
							| 
									
										
										
										
											2021-01-12 15:58:52 +11:00
										 |  |  |             problem "GitHub homepages should not end with .git" do |corrector| | 
					
						
							|  |  |  |               corrector.replace(homepage_parameter_node.source_range, "\"#{homepage.delete_suffix(".git")}\"") | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2020-08-03 22:37:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-13 17:21:51 +01:00
										 |  |  |           # People will run into mixed content sometimes, but we should enforce and then add | 
					
						
							|  |  |  |           # exemptions as they are discovered. Treat mixed content on homepages as a bug. | 
					
						
							|  |  |  |           # Justify each exemptions with a code comment so we can keep track here. | 
					
						
							|  |  |  |           # | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |           # Compact the above into this list as we're able to remove detailed notations, etc over time. | 
					
						
							| 
									
										
										
										
											2020-11-13 17:21:51 +01:00
										 |  |  |           when | 
					
						
							|  |  |  |                # Check for http:// GitHub homepage URLs, https:// is preferred. | 
					
						
							|  |  |  |                # Note: only check homepages that are repo pages, not *.github.com hosts | 
					
						
							| 
									
										
										
										
											2022-04-15 15:46:36 +01:00
										 |  |  |                %r{^http://github\.com/}, | 
					
						
							| 
									
										
										
										
											2020-11-13 17:21:51 +01:00
										 |  |  |                %r{^http://[^/]*\.github\.io/}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                # Savannah has full SSL/TLS support but no auto-redirect. | 
					
						
							|  |  |  |                # Doesn't apply to the download URLs, only the homepage. | 
					
						
							| 
									
										
										
										
											2022-04-15 15:46:36 +01:00
										 |  |  |                %r{^http://savannah\.nongnu\.org/}, | 
					
						
							| 
									
										
										
										
											2020-11-13 17:21:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                %r{^http://[^/]*\.sourceforge\.io/}, | 
					
						
							|  |  |  |                # There's an auto-redirect here, but this mistake is incredibly common too. | 
					
						
							|  |  |  |                # Only applies to the homepage and subdomains for now, not the FTP URLs. | 
					
						
							|  |  |  |                %r{^http://((?:build|cloud|developer|download|extensions|git|
 | 
					
						
							|  |  |  |                                glade|help|library|live|nagios|news|people| | 
					
						
							|  |  |  |                                projects|rt|static|wiki|www)\.)?gnome\.org}x, | 
					
						
							|  |  |  |                %r{^http://[^/]*\.apache\.org}, | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |                %r{^http://packages\.debian\.org}, | 
					
						
							|  |  |  |                %r{^http://wiki\.freedesktop\.org/}, | 
					
						
							|  |  |  |                %r{^http://((?:www)\.)?gnupg\.org/}, | 
					
						
							|  |  |  |                %r{^http://ietf\.org}, | 
					
						
							|  |  |  |                %r{^http://[^/.]+\.ietf\.org}, | 
					
						
							|  |  |  |                %r{^http://[^/.]+\.tools\.ietf\.org}, | 
					
						
							|  |  |  |                %r{^http://www\.gnu\.org/}, | 
					
						
							|  |  |  |                %r{^http://code\.google\.com/}, | 
					
						
							|  |  |  |                %r{^http://bitbucket\.org/}, | 
					
						
							|  |  |  |                %r{^http://(?:[^/]*\.)?archive\.org} | 
					
						
							| 
									
										
										
										
											2021-01-12 15:58:52 +11:00
										 |  |  |             problem "Please use https:// for #{homepage}" do |corrector| | 
					
						
							|  |  |  |               corrector.replace(homepage_parameter_node.source_range, "\"#{homepage.sub("http", "https")}\"") | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2020-08-03 22:37:02 -04:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2017-05-14 13:49:42 +05:30
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |