-
Notifications
You must be signed in to change notification settings - Fork 215
Add script to update ty.json in schemastore
#65
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| """Update ty.json in schemastore. | ||
|
|
||
| This script will clone astral-sh/schemastore, update the schema and push the changes | ||
| to a new branch tagged with the ty git hash. You should see a URL to create the PR | ||
| to schemastore in the CLI. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import enum | ||
| import json | ||
| from pathlib import Path | ||
| from subprocess import check_call, check_output | ||
| from tempfile import TemporaryDirectory | ||
| from typing import NamedTuple, assert_never | ||
|
|
||
| TY_REPO = "https://github.com/astral-sh/ty" | ||
| TY_JSON = Path("schemas/json/ty.json") | ||
|
|
||
|
|
||
| class SchemastoreRepos(NamedTuple): | ||
| fork: str | ||
| upstream: str | ||
|
|
||
|
|
||
| class GitProtocol(enum.Enum): | ||
| SSH = "ssh" | ||
| HTTPS = "https" | ||
|
|
||
| def schemastore_repos(self) -> SchemastoreRepos: | ||
| match self: | ||
| case GitProtocol.SSH: | ||
| return SchemastoreRepos( | ||
| fork="git@github.com:astral-sh/schemastore.git", | ||
| upstream="git@github.com:SchemaStore/schemastore.git", | ||
| ) | ||
| case GitProtocol.HTTPS: | ||
| return SchemastoreRepos( | ||
| fork="https://github.com/astral-sh/schemastore.git", | ||
| upstream="https://github.com/SchemaStore/schemastore.git", | ||
| ) | ||
| case _: | ||
| assert_never(self) | ||
|
|
||
|
|
||
| def update_schemastore( | ||
| schemastore_path: Path, schemastore_repos: SchemastoreRepos, root: Path | ||
| ) -> None: | ||
| if not schemastore_path.is_dir(): | ||
| check_call( | ||
| ["git", "clone", schemastore_repos.fork, schemastore_path, "--depth=1"] | ||
| ) | ||
| check_call( | ||
| [ | ||
| "git", | ||
| "remote", | ||
| "add", | ||
| "upstream", | ||
| schemastore_repos.upstream, | ||
| ], | ||
| cwd=schemastore_path, | ||
| ) | ||
| # Create a new branch tagged with the current ty commit up to date with the latest | ||
| # upstream schemastore | ||
| check_call(["git", "fetch", "upstream"], cwd=schemastore_path) | ||
| current_sha = check_output(["git", "rev-parse", "HEAD"], text=True).strip() | ||
| branch = f"update-ty-{current_sha}" | ||
| check_call( | ||
| ["git", "switch", "-c", branch], | ||
| cwd=schemastore_path, | ||
| ) | ||
| check_call( | ||
| ["git", "reset", "--hard", "upstream/master"], | ||
| cwd=schemastore_path, | ||
| ) | ||
|
|
||
| # Run npm install | ||
| src = schemastore_path.joinpath("src") | ||
| check_call(["npm", "install"], cwd=schemastore_path) | ||
|
|
||
| # Update the schema and format appropriately | ||
| schema = json.loads(root.joinpath("ruff/ty.schema.json").read_text()) | ||
| schema["$id"] = "https://json.schemastore.org/ty.json" | ||
| src.joinpath(TY_JSON).write_text( | ||
| json.dumps(dict(schema.items()), indent=2, ensure_ascii=False), | ||
| ) | ||
| check_call( | ||
| [ | ||
| "../node_modules/prettier/bin/prettier.cjs", | ||
| "--plugin", | ||
| "prettier-plugin-sort-json", | ||
| "--write", | ||
| TY_JSON, | ||
| ], | ||
| cwd=src, | ||
| ) | ||
|
|
||
| # Check if the schema has changed | ||
| # https://stackoverflow.com/a/9393642/3549270 | ||
| if check_output(["git", "status", "-s"], cwd=schemastore_path).strip(): | ||
| # Schema has changed, commit and push | ||
| commit_url = f"{TY_REPO}/commit/{current_sha}" | ||
| commit_body = f"This updates ty's JSON schema to [{current_sha}]({commit_url})" | ||
| # https://stackoverflow.com/a/22909204/3549270 | ||
| check_call( | ||
| [ | ||
| "git", | ||
| "commit", | ||
| "-a", | ||
| "-m", | ||
| "Update ty's JSON schema", | ||
| "-m", | ||
| commit_body, | ||
| ], | ||
| cwd=schemastore_path, | ||
| ) | ||
| # This should show the link to create a PR | ||
| check_call( | ||
| ["git", "push", "--set-upstream", "origin", branch, "--force"], | ||
| cwd=schemastore_path, | ||
| ) | ||
| else: | ||
| print("No changes") | ||
|
|
||
|
|
||
| def determine_git_protocol(argv: list[str] | None = None) -> GitProtocol: | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
| ) | ||
| parser.add_argument( | ||
| "--proto", | ||
| choices=[proto.value for proto in GitProtocol], | ||
| default="https", | ||
| help="Protocol to use for git authentication", | ||
| ) | ||
| args = parser.parse_args(argv) | ||
| return GitProtocol(args.proto) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| root = Path( | ||
| check_output(["git", "rev-parse", "--show-toplevel"], text=True).strip(), | ||
| ) | ||
|
|
||
| expected_ruff_revision = check_output( | ||
| ["git", "ls-tree", "main", "--format", "%(objectname)", "ruff"] | ||
| ).strip() | ||
| actual_ruff_revision = check_output( | ||
| ["git", "-C", "ruff", "rev-parse", "HEAD"] | ||
| ).strip() | ||
|
|
||
| if expected_ruff_revision != actual_ruff_revision: | ||
| print( | ||
| f"The ruff submodule is at {expected_ruff_revision} but main expects {actual_ruff_revision}" | ||
| ) | ||
| match input( | ||
| "How do you want to proceed (u=reset submodule, n=abort, y=continue)? " | ||
| ): | ||
| case "u": | ||
| check_call( | ||
| ["git", "-C", "ruff", "reset", "--hard", expected_ruff_revision] | ||
| ) | ||
| case "n": | ||
| return | ||
| case "y": | ||
| ... | ||
| case command: | ||
| print(f"Invalid input '{command}', abort") | ||
| return | ||
|
|
||
| schemastore_repos = determine_git_protocol().schemastore_repos() | ||
| schemastore_existing = root.joinpath("schemastore") | ||
| if schemastore_existing.is_dir(): | ||
| update_schemastore(schemastore_existing, schemastore_repos, root) | ||
| else: | ||
| with TemporaryDirectory() as temp_dir: | ||
| update_schemastore( | ||
| Path(temp_dir).joinpath("schemastore"), schemastore_repos, root | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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.
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.
This is mostly just copy paste from the ruff repository. The only real addition is in
mainwhere we check the state of theruffsubmodule