-
Notifications
You must be signed in to change notification settings - Fork 94
Validate website field #1219
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
Open
AdwaithaV
wants to merge
7
commits into
Qiskit:main
Choose a base branch
from
AdwaithaV:validate-website-field
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−7
Open
Validate website field #1219
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
629da0f
Add validation for duplicate website URLs
AdwaithaV 64172e5
Add validation for duplicate website URLs
AdwaithaV 4bd0aff
Format URL validation with black
AdwaithaV 9051555
Improve duplicate website URL validation
AdwaithaV b43ded9
Update ecosystem/validation/test_url.py
AdwaithaV edc752d
Address review comments for website URL validation
AdwaithaV a93f249
Update resources/checks.toml
AdwaithaV 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
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,8 @@ | |||||||
|
|
||||||||
| """URL Validations""" | ||||||||
|
|
||||||||
| from urllib.parse import urlparse | ||||||||
|
|
||||||||
| import pytest | ||||||||
|
|
||||||||
| # pylint: disable=missing-function-docstring, missing-class-docstring | ||||||||
|
|
@@ -20,7 +22,7 @@ | |||||||
| class TestURLs: | ||||||||
| @classmethod | ||||||||
| def get_all_urls(cls, member): | ||||||||
| """recursevly search for URLs in the member data""" | ||||||||
| """recursively search for URLs in the member data""" | ||||||||
| for key, value in member.to_dict().items(): | ||||||||
| if isinstance(value, str) and value.startswith("http"): | ||||||||
| yield getattr(member, key) | ||||||||
|
|
@@ -29,33 +31,114 @@ def get_all_urls(cls, member): | |||||||
| else: | ||||||||
| continue | ||||||||
|
|
||||||||
| @staticmethod | ||||||||
| def normalize_url(url): | ||||||||
| """Normalize URL for comparison.""" | ||||||||
|
|
||||||||
| if url is None: | ||||||||
| return None, None | ||||||||
|
|
||||||||
| parsed = urlparse(str(url)) | ||||||||
|
|
||||||||
| hostname = (parsed.hostname or "").lower() | ||||||||
|
|
||||||||
| if hostname.startswith("www."): | ||||||||
| hostname = hostname[4:] | ||||||||
|
|
||||||||
| path = parsed.path.rstrip("/") | ||||||||
|
|
||||||||
| return hostname, path | ||||||||
|
|
||||||||
| @staticmethod | ||||||||
| def is_same_or_subpath(candidate, reference): | ||||||||
|
Comment on lines
+52
to
+53
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it can be implemented as an assertion:
Suggested change
|
||||||||
| """ | ||||||||
| Check whether candidate duplicates or points inside reference. | ||||||||
| """ | ||||||||
|
|
||||||||
| candidate_host, candidate_path = TestURLs.normalize_url(candidate) | ||||||||
|
|
||||||||
| reference_host, reference_path = TestURLs.normalize_url(reference) | ||||||||
|
|
||||||||
| if candidate_host != reference_host: | ||||||||
| return False | ||||||||
|
|
||||||||
| return candidate_path == reference_path or candidate_path.startswith( | ||||||||
| reference_path + "/" | ||||||||
| ) | ||||||||
|
|
||||||||
| def test_http(self, member): | ||||||||
| for url in TestURLs.get_all_urls(member): | ||||||||
| assert not str(url).startswith("http:"), f"{url} is not HTTPS" | ||||||||
|
|
||||||||
| def test_025(self, member): | ||||||||
| """Documentation link has redundant suffix""" | ||||||||
|
|
||||||||
| fields = ["url", "documentation", "reference_paper"] | ||||||||
|
|
||||||||
| for field in fields: | ||||||||
| url = getattr(member, field) | ||||||||
|
|
||||||||
| if url is None: | ||||||||
| continue | ||||||||
| if url.hostname.endswith("readthedocs.io"): | ||||||||
| suffixes = ["en/latest/", "en/latest", "en"] | ||||||||
|
|
||||||||
| if url.hostname.lower().endswith("readthedocs.io"): | ||||||||
| suffixes = [ | ||||||||
| "en/latest/", | ||||||||
| "en/latest", | ||||||||
| "en", | ||||||||
| ] | ||||||||
|
|
||||||||
| for suffix in suffixes: | ||||||||
| assert not url.path.endswith( | ||||||||
| suffix | ||||||||
| ), f"{url} has redundant suffix: {suffix}" | ||||||||
|
|
||||||||
| def test_026(self, member): | ||||||||
| """The /README.md is not documentation""" | ||||||||
|
|
||||||||
| documentation_url = getattr(member, "documentation") | ||||||||
|
|
||||||||
| if documentation_url is None: | ||||||||
| pytest.skip("No member.documentation") | ||||||||
| if documentation_url.hostname.endswith("github.com"): | ||||||||
| suffixes = ["main/README.md"] | ||||||||
|
|
||||||||
| if documentation_url.hostname.lower().endswith("github.com"): | ||||||||
| suffixes = [ | ||||||||
| "main/README.md", | ||||||||
| "blob/main/README.md", | ||||||||
| "tree/main", | ||||||||
| ] | ||||||||
|
|
||||||||
| for suffix in suffixes: | ||||||||
| assert not documentation_url.path.endswith(suffix), ( | ||||||||
| "The field `member.documentation` can be empty. " | ||||||||
| "It does not have to be a link to the `README.md`." | ||||||||
| "The field `member.documentation` can be " | ||||||||
| "empty. It does not have to be a link to " | ||||||||
| "the repository's `README.md` or root." | ||||||||
| ) | ||||||||
|
|
||||||||
| def test_027(self, member): | ||||||||
|
1ucian0 marked this conversation as resolved.
|
||||||||
| """Website should not duplicate GitHub or PyPI URLs""" | ||||||||
|
|
||||||||
| website_url = getattr(member, "website") | ||||||||
|
|
||||||||
| if website_url is None: | ||||||||
| pytest.skip("No member.website") | ||||||||
|
|
||||||||
| hostname = website_url.hostname or "" | ||||||||
|
|
||||||||
| # Reject GitHub repository URLs as website | ||||||||
| repository_url = getattr(member, "url") | ||||||||
|
|
||||||||
| if repository_url is not None and repository_url.hostname.lower().endswith( | ||||||||
| "github.com" | ||||||||
| ): | ||||||||
| assert not TestURLs.is_same_or_subpath( | ||||||||
| website_url, | ||||||||
| repository_url, | ||||||||
| ), ( | ||||||||
| "The field `member.website` should not " | ||||||||
| "duplicate the GitHub repository URL." | ||||||||
| ) | ||||||||
|
|
||||||||
| assert not ( | ||||||||
| hostname.endswith("pypi.org") or hostname.endswith("pypi.python.org") | ||||||||
| ), ("The field `member.website` should not " "point to a PyPI project URL.") | ||||||||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you can bake these normalizatoins inside
get_all_urlsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thaks for the suggestion .Since
get_all_urls()is also used bytest_http(), would you prefer inlining the normalization intois_same_or_subpath(), or did you have a different approach in mind forget_all_urls()