-
Notifications
You must be signed in to change notification settings - Fork 77
ci: respect redirected pages in removed URL check #612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
03042a2
ci: make removed URL check aware of docs/redirects.txt
rajannpatel a1c6aa7
add check-removed-urls.yml to changelog
rajannpatel d57b2c9
Extract removed URL check into script
rajannpatel 093a29b
Merge branch 'main' into check-removed-urls
rajannpatel 31823da
resolve https://github.com/canonical/sphinx-stack/pull/612#discussion…
rajannpatel 19285f1
resolve https://github.com/canonical/sphinx-stack/pull/612#discussion…
rajannpatel ac42e8a
Scoped base_urls, compare_urls, and redirects locally inside main()
rajannpatel 38296e3
docs: update changelog
jahn-junior File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
|
jahn-junior marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| 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() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.