Skip to content

Commit

Permalink
Improve link check and add back to build
Browse files Browse the repository at this point in the history
  • Loading branch information
hakanensari committed Sep 20, 2024
1 parent 31631d9 commit 2d2a862
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 37 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@ jobs:
bundler-cache: true
- run: bundle exec rubocop

test:
verify-links:
needs: [lint]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: "3.3"
- run: bin/verify-links

test:
needs: [verify-links]
strategy:
matrix:
ruby: ["3.2", "3.3"]
Expand Down
36 changes: 0 additions & 36 deletions bin/crawl-links

This file was deleted.

50 changes: 50 additions & 0 deletions bin/verify-links
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "open3"
require "net/http"
require "logger"

logger = Logger.new($stdout)

# Search for URLs in the README file
search_command = "grep -Eo 'https?://[^ ]+' README"
output, = Open3.capture3(search_command)
readme_matches = output.split("\n")

# Search for URLs in comments in other files
search_command = "grep -EoR '#.*https?://[^ ]+' lib"
output, = Open3.capture3(search_command)
lib_matches = output.split("\n")

# Extract only the URLs
matches = (readme_matches + lib_matches).map do |line|
line.match(%r{https?://[^ )>,\"]+})[0]
end

matches.uniq!

failed = false

threads = matches.map do |url|
Thread.new do
response = Net::HTTP.get_response(URI(url))
case response
when Net::HTTPSuccess
logger.info("✅ #{response.code} #{url}")
when Net::HTTPFound, Net::HTTPMovedPermanently
logger.warn("👀 #{response.code} #{url}")
else
failed = true
logger.error("❌ #{response.code} #{url}")
end
rescue URI::InvalidURIError => e
logger.error("❌ Invalid URL #{url}: #{e.message}")
end
end
threads.each(&:join)

if failed
logger.warn("Please fix broken documentation links.")
exit 1
end

0 comments on commit 2d2a862

Please sign in to comment.