From e0e566bc2ea59d508bc97f2a6f6fbf0bd26fe62a Mon Sep 17 00:00:00 2001
From: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Date: Mon, 16 Oct 2023 14:19:45 +0200
Subject: [PATCH] [CI] Add link checker (#1875)

Adding link checker to the CI (closes
https://github.com/paritytech/polkadot-sdk/issues/993). It would be nice
to have the docs people own this and extend accordingly.
Currently all known-bad links are excluded, but we should one-by-one
include those as well until all are fixed.

This check now ensures that 1) no new broken links are introduced into
`.rs` files and 2) that no old links break unnoticed.

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
---
 .config/lychee.toml               | 50 +++++++++++++++++++++++++++++++
 .github/workflows/check-links.yml | 40 +++++++++++++++++++++++++
 .gitignore                        |  1 +
 3 files changed, 91 insertions(+)
 create mode 100644 .config/lychee.toml
 create mode 100644 .github/workflows/check-links.yml

diff --git a/.config/lychee.toml b/.config/lychee.toml
new file mode 100644
index 00000000000..9b2ae069931
--- /dev/null
+++ b/.config/lychee.toml
@@ -0,0 +1,50 @@
+# Config file for lychee link checker: <https://github.com/lycheeverse/lychee>
+# Run with `lychee -c .config/lychee.toml ./**/*.rs ./**/*.prdoc`
+
+cache = true
+max_cache_age = "1d"
+max_redirects = 10
+max_retries = 6
+
+# Exclude localhost et.al.
+exclude_all_private = true
+
+# Treat these codes as success condition:
+accept = [
+	# Ok
+	200,
+
+	# Rate limited - GitHub likes to throw this.
+	429
+]
+
+exclude_path = [ "./target" ]
+
+exclude = [
+  # Place holders (no need to fix these):
+  "http://visitme/",
+  "https://visitme/",
+  
+  # TODO <https://github.com/paritytech/polkadot-sdk/issues/134>
+  "https://docs.substrate.io/main-docs/build/custom-rpc/#public-rpcs",
+  "https://docs.substrate.io/rustdocs/latest/sp_api/macro.decl_runtime_apis.html",
+  "https://github.com/ipfs/js-ipfs-bitswap/blob/",
+  "https://github.com/paritytech/polkadot-sdk/substrate/frame/timestamp",
+  "https://github.com/paritytech/substrate/frame/fast-unstake",
+  "https://github.com/zkcrypto/bls12_381/blob/e224ad4ea1babfc582ccd751c2bf128611d10936/src/test-data/mod.rs",
+  "https://polkadot.network/the-path-of-a-parachain-block/",
+  "https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results",
+  "https://research.web3.foundation/en/latest/polkadot/block-production/Babe.html",
+  "https://research.web3.foundation/en/latest/polkadot/block-production/Babe.html#-6.-practical-results",
+  "https://research.web3.foundation/en/latest/polkadot/networking/3-avail-valid.html#topology",
+  "https://research.web3.foundation/en/latest/polkadot/NPoS/3.%20Balancing.html",
+  "https://research.web3.foundation/en/latest/polkadot/overview/2-token-economics.html",
+  "https://research.web3.foundation/en/latest/polkadot/overview/2-token-economics.html#inflation-model",
+  "https://research.web3.foundation/en/latest/polkadot/slashing/npos.html",
+  "https://research.web3.foundation/en/latest/polkadot/Token%20Economics.html#inflation-model",
+  "https://rpc.polkadot.io/",
+  "https://w3f.github.io/parachain-implementers-guide/node/approval/approval-distribution.html",
+  "https://w3f.github.io/parachain-implementers-guide/node/index.html",
+  "https://w3f.github.io/parachain-implementers-guide/protocol-chain-selection.html",
+  "https://w3f.github.io/parachain-implementers-guide/runtime/session_info.html",
+]
diff --git a/.github/workflows/check-links.yml b/.github/workflows/check-links.yml
new file mode 100644
index 00000000000..ec606f1b148
--- /dev/null
+++ b/.github/workflows/check-links.yml
@@ -0,0 +1,40 @@
+name: Check links
+
+on:
+  pull_request:
+    paths:
+      - "*.rs"
+      - "*.prdoc"
+      - ".github/workflows/check-links.yml"
+      - ".config/lychee.toml"
+    types: [opened, synchronize, reopened, ready_for_review]
+
+permissions:
+  packages: read
+
+jobs:
+  link-checker:
+    runs-on: ubuntu-latest
+    steps:
+      - name: Restore lychee cache
+        uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2 (7. Sep 2023)
+        with:
+          path: .lycheecache
+          key: cache-lychee-${{ github.sha }}
+          # This should restore from the most recent one:
+          restore-keys: cache-lychee-
+
+      - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 (22. Sep 2023)
+
+      - name: Lychee link checker
+        uses: lycheeverse/lychee-action@2ac9f030ccdea0033e2510a23a67da2a2da98492 # for v1.8.0 (15. May 2023)
+        with:
+          args: >-
+            --config .config/lychee.toml
+            --no-progress
+            './**/*.rs'
+            './**/*.prdoc'
+          fail: true
+        env:
+          # To bypass GitHub rate-limit:
+          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
diff --git a/.gitignore b/.gitignore
index 35e02e706b4..7feea8ada5c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,6 +5,7 @@
 .env*
 .idea
 .local
+.lycheecache
 .vscode
 .wasm-binaries
 *.adoc
-- 
GitLab