From 5a89b377c5969d9ab8bde399ca19929fb58bcf47 Mon Sep 17 00:00:00 2001
From: Jared Kosanovic <jared.kosanovic@wisc.edu>
Date: Fri, 26 Jun 2020 19:48:13 +0000
Subject: [PATCH] Check for broken links - INPLATFORM 219

---
 .gitlab-ci.yml                   | 11 +++++++++++
 scripts/README.md                |  8 ++++++++
 scripts/link-checker-config.json |  7 +++++++
 scripts/link-checker.sh          | 20 ++++++++++++++++++++
 4 files changed, 46 insertions(+)
 create mode 100644 .gitlab-ci.yml
 create mode 100644 scripts/README.md
 create mode 100644 scripts/link-checker-config.json
 create mode 100755 scripts/link-checker.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..00f29e8
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,11 @@
+check-links:
+  stage: test
+  tags:
+    - docker
+  image: "node:14"
+  before_script:
+    - npm install -g markdown-link-check
+  script:
+    - cd scripts
+    - ./link-checker.sh
+
diff --git a/scripts/README.md b/scripts/README.md
new file mode 100644
index 0000000..8507d04
--- /dev/null
+++ b/scripts/README.md
@@ -0,0 +1,8 @@
+# Scripts
+
+## Link Checker
+
+Files:
+
+- [link-checker.sh](./link-checker.sh): Main script to check each markdown file for broken external links and relative links. Uses [markdown-link-check](https://www.npmjs.com/package/markdown-link-check).
+- [link-checker-config.json](./link-checker-config.json): Configuration file for [markdown-link-check](https://www.npmjs.com/package/markdown-link-check). Used to exclude certain links so that they are not checked. This could be done for example links or links to websites that are sometimes down or timeout.
\ No newline at end of file
diff --git a/scripts/link-checker-config.json b/scripts/link-checker-config.json
new file mode 100644
index 0000000..461e321
--- /dev/null
+++ b/scripts/link-checker-config.json
@@ -0,0 +1,7 @@
+{
+    "ignorePatterns": [
+        {
+            "pattern": "interop.it.wisc.edu"
+        }
+    ]
+}
diff --git a/scripts/link-checker.sh b/scripts/link-checker.sh
new file mode 100755
index 0000000..3c2a0da
--- /dev/null
+++ b/scripts/link-checker.sh
@@ -0,0 +1,20 @@
+exit_code=0
+
+# Find all markdown files
+markdown_files=$(find ../ -name \*.md)
+
+for markdown_file in $markdown_files; do
+    # For each markdown file, check the links. link-checker-config.json excludes certain links (like example.com)
+    # If broken links are found, output them along with the file path.
+    if markdown-link-check -q -c link-checker-config.json $markdown_file > links.log; then
+        # Do nothing if the file doesn't have any broken links.
+        :
+    else
+        # If broken links are found, output them along with the file path.
+        cat links.log
+        exit_code=1
+    fi
+    rm links.log
+done
+
+exit $exit_code
-- 
GitLab