Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions .github/workflows/check-removed-urls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,17 @@ jobs:
- name: Generate current URLs list
run: |
for dir in compare base; do
pushd ${dir}/docs
find ./_build/ -name '*.html' \
| sed 's|/_build||;s|/index.html$|/|;s|.html$||' \
| sort > urls.txt
popd
build_dir="${dir}/docs/_build"
urls_file="${dir}/docs/urls.txt"

if [ ! -d "${build_dir}" ]; then
echo "Expected docs build directory not found: ${build_dir}"
exit 1
fi

find "${build_dir}" -name '*.html' \
| sed "s|^${build_dir}||;s|^/html||;s|/index.html$|/|;s|.html$||" \
| sort > "${urls_file}"
done
- name: Compare URLs
run: |
BASE_URLS_PATH="base/docs/urls.txt"
COMPARE_URLS_PATH="compare/docs/urls.txt"
removed=$(comm -23 ${BASE_URLS_PATH} ${COMPARE_URLS_PATH} )
if [ -n "$removed" ]; then
echo "The following URLs were removed:"
echo "$removed"
echo "Please ensure removed pages are redirected"
exit 1
fi
run: python3 compare/docs/_dev/check_removed_urls.py
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Upcoming

* Prevent Vale from processing Markdown files in the build directory
* Update link to documenation in README
* Make removed URL check redirect-aware and add support for reusable workflow contexts
* Exclude utility directories from builds and checks
* Update link to documentation in README

Expand All @@ -11,6 +14,7 @@
* `docs/Makefile` [#605](https://github.com/canonical/sphinx-stack/pull/605), [#610](https://github.com/canonical/sphinx-stack/pull/610)
* `README.md` [#603](https://github.com/canonical/sphinx-stack/pull/603)
* `.github/workflows/cla-check.yml` [#606](https://github.com/canonical/sphinx-stack/pull/606)
* `.github/workflows/check-removed-urls.yml` [#PR_NUMBER](https://github.com/canonical/sphinx-stack/pull/#PR_NUMBER)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `.github/workflows/check-removed-urls.yml` [#PR_NUMBER](https://github.com/canonical/sphinx-stack/pull/#PR_NUMBER)
* `.github/workflows/check-removed-urls.yml` [#612](https://github.com/canonical/sphinx-stack/pull/612)

Leaving this comment so we don't forget to replace these before merge.


## 2.0

Expand Down
98 changes: 98 additions & 0 deletions docs/_dev/check_removed_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#! /usr/bin/env python

"""Check for removed URLs and verify if redirects exist."""

import csv
import io
import sys
from pathlib import Path

BASE_URLS = Path("base/docs/urls.txt")
COMPARE_URLS = Path("compare/docs/urls.txt")
REDIRECTS = Path("compare/docs/redirects.txt")


def read_urls(path):
return {
line.strip()
for line in path.read_text(encoding="utf-8").splitlines()
if line.strip()
}


def read_redirect_sources(path):
sources = set()

if not path.exists():
return sources

for raw_line in path.read_text(encoding="utf-8").splitlines():
line = raw_line.strip()
if not line or line.startswith("#"):
continue

fields = next(
csv.reader(
io.StringIO(line),
delimiter=" ",
quotechar='"',
skipinitialspace=True,
),
[],
)
if fields:
sources.add(fields[0])

return sources


def source_candidates_for_url(url):
clean_path = url.strip()
clean_path = clean_path.removeprefix("./")
clean_path = clean_path.removeprefix("/")
clean_path = clean_path.removesuffix(".html")
clean_path = clean_path.rstrip("/")

if not clean_path:
return {"index.md"}

# A removed dirhtml URL can map back to either a page file or an
# index file. Directory-level redirects are stored with a trailing
# slash, so include that form too.
return {
f"{clean_path}.md",
f"{clean_path}/index.md",
f"{clean_path}/",
}


def main():
if not BASE_URLS.exists():
print(f"Error: Base URLs file not found at {BASE_URLS}")
sys.exit(1)
if not COMPARE_URLS.exists():
print(f"Error: Compare URLs file not found at {COMPARE_URLS}")
sys.exit(1)

removed_urls = sorted(read_urls(BASE_URLS) - read_urls(COMPARE_URLS))
redirect_sources = read_redirect_sources(REDIRECTS)

missing_redirects = [
url
for url in removed_urls
if source_candidates_for_url(url).isdisjoint(redirect_sources)
]

if missing_redirects:
print("The following URLs were removed without redirects:")
print("\n".join(missing_redirects))
print("Please ensure removed pages are redirected")
sys.exit(1)

if removed_urls:
print("Removed URLs have redirects:")
print("\n".join(removed_urls))


if __name__ == "__main__":
main()
Loading