-
Notifications
You must be signed in to change notification settings - Fork 0
feat: CLI command + gha workflow to contribute patches back to source #9
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
21 commits
Select commit
Hold shift + click to select a range
560dba1
feat: workflow to contribute patches back to source
kelly-sovacool 7819a8b
feat: contribute CLI command
kelly-sovacool 83e81a6
fix: fallback to gh auth token if github token isn't set
kelly-sovacool 83cb208
Merge branch 'main' into contribute
kelly-sovacool d0b6ded
fix: support ssh git URLs
kelly-sovacool fce50e8
fix: require patch file to exist as file
kelly-sovacool 743b8cd
feat: mark patch status
kelly-sovacool 2f3eafe
chore: Merge branch 'contribute' of github.com:CCBR/syncweaver into c…
kelly-sovacool 70aeb5d
fix(security): do not pass github token in url
kelly-sovacool 3880d36
chore: cleanup unneeded annotate-rejected subcmd
kelly-sovacool a11eaa1
fix: resolved_patch_path must remain in host repo
kelly-sovacool 5cc971b
docs: update raises section
kelly-sovacool 46a8370
fix: handle newlines for github output
kelly-sovacool 0d4f19c
fix: sanitize branch stub
kelly-sovacool 8457bf0
fix: stay within host repo for patch resolution
kelly-sovacool 3b51429
test: remove duplicate line
kelly-sovacool 099d5df
style: enforce py instructions
kelly-sovacool 07cf838
chore: Merge branch 'contribute' of github.com:CCBR/syncweaver into c…
kelly-sovacool bd79f6b
fix: missing docstring close
kelly-sovacool a0673f6
chore: Merge branch 'main' into contribute
kelly-sovacool 7cad376
fix: make sure patch exists before opening PR
kelly-sovacool 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 |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| """CLI command for contributing host patches back to source repositories.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import pathlib | ||
|
|
||
| import click | ||
|
|
||
| from syncweaver.contribute_patch import ( | ||
| contribute_patch, | ||
| resolve_contribute_patch_metadata, | ||
| ) | ||
| from syncweaver.git import resolve_github_token | ||
| from syncweaver.lockfile import load_existing_lockfile | ||
| from syncweaver.patch import mark_patch_status | ||
|
|
||
|
|
||
| @click.command("contribute") | ||
| @click.option( | ||
| "--path", | ||
| "source_path", | ||
| default="", | ||
| show_default=False, | ||
| help=( | ||
| "Tracked source path in the host repository, e.g. code/package1. " | ||
| "Resolved automatically from lockfile when not provided." | ||
| ), | ||
| ) | ||
| @click.option( | ||
| "--repo-url", | ||
| default="", | ||
| show_default=False, | ||
| help=( | ||
| "Source repository URL or OWNER/REPO shorthand. " | ||
| "Used to disambiguate when multiple sources are tracked." | ||
| ), | ||
| ) | ||
| @click.option( | ||
| "--source-repository", | ||
| default="", | ||
| show_default=False, | ||
| help=( | ||
| "Source repository in OWNER/REPO format. " | ||
| "Derived from lockfile repo_url when not provided." | ||
| ), | ||
| ) | ||
| @click.option( | ||
| "--patch", | ||
| "patch_path", | ||
| default="", | ||
| show_default=False, | ||
| type=click.Path(), | ||
| help=( | ||
| "Path to the patch file to contribute. " | ||
| "Resolved from lockfile patch entry when not provided." | ||
| ), | ||
| ) | ||
| @click.option( | ||
| "--base-ref", | ||
| "source_base_ref", | ||
| default="", | ||
| show_default=False, | ||
| help=( | ||
| "Base branch or ref in the source repository to target. " | ||
| "Defaults to lockfile ref when not provided." | ||
| ), | ||
| ) | ||
| @click.option( | ||
| "--lockfile", | ||
| default=".syncweaver-lock.json", | ||
| show_default=True, | ||
| type=click.Path(path_type=pathlib.Path), | ||
| help="Path to .syncweaver-lock.json in the host repository.", | ||
| ) | ||
| @click.option( | ||
| "--token", | ||
| envvar="GITHUB_TOKEN", | ||
| default="", | ||
| show_default=False, | ||
| help=( | ||
| "GitHub token with push access to the source repository. " | ||
| "May also be set via the GITHUB_TOKEN environment variable or resolved from `gh auth token` " | ||
| "when not provided." | ||
| ), | ||
| ) | ||
| @click.option( | ||
| "--run-id", | ||
| default="", | ||
| show_default=False, | ||
| help="Optional identifier appended to the branch name for uniqueness.", | ||
| ) | ||
| @click.option( | ||
| "--debug", | ||
| is_flag=True, | ||
| default=False, | ||
| help="Print resolved metadata and verbose git output.", | ||
| ) | ||
| def contribute_cmd( | ||
| source_path: str, | ||
| repo_url: str, | ||
| source_repository: str, | ||
| patch_path: str, | ||
| source_base_ref: str, | ||
| lockfile: pathlib.Path, | ||
| token: str, | ||
| run_id: str, | ||
| debug: bool, | ||
| ) -> None: | ||
| """Contribute a tracked host patch back to the source repository. | ||
|
|
||
| Clones the source repository, applies the patch to a new branch, pushes | ||
| it, and opens a pull request. Runs from the host repository directory. | ||
| """ | ||
| cwd = pathlib.Path.cwd() | ||
| resolved_lockfile = cwd / lockfile | ||
| try: | ||
| resolved = resolve_contribute_patch_metadata( | ||
| lockfile=resolved_lockfile, | ||
| host_cwd=cwd, | ||
| source_path=source_path, | ||
| repo_url=repo_url, | ||
| source_repository=source_repository, | ||
| patch_path=patch_path, | ||
| source_base_ref=source_base_ref, | ||
| ) | ||
| except ( | ||
| FileNotFoundError, | ||
| KeyError, | ||
| ValueError, | ||
| json.JSONDecodeError, | ||
| OSError, | ||
| ) as exc: | ||
| raise click.ClickException(str(exc)) from exc | ||
|
|
||
| if debug: | ||
| click.echo("Resolved metadata:") | ||
| click.echo(f" source_path: {resolved['source_path']}") | ||
| click.echo(f" repo_url: {resolved['repo_url']}") | ||
| click.echo(f" source_repository: {resolved['source_repository']}") | ||
| click.echo(f" patch_path: {resolved['patch_path']}") | ||
| click.echo(f" source_base_ref: {resolved['source_base_ref']}") | ||
|
|
||
| try: | ||
| resolved_token = resolve_github_token(token) | ||
| except RuntimeError as exc: | ||
| raise click.ClickException(str(exc)) from exc | ||
|
|
||
| try: | ||
| lock_data = load_existing_lockfile(resolved_lockfile) | ||
| patch_key = resolved["patch_path"] | ||
| patch_found = False | ||
| for source_entry in lock_data.get("sources", {}).values(): | ||
| if source_entry.get("patch") == patch_key: | ||
| patch_found = True | ||
| break | ||
| if not patch_found: | ||
| raise KeyError(f"Patch path is not tracked in lockfile: {patch_key}") | ||
| except (FileNotFoundError, KeyError, json.JSONDecodeError, OSError) as exc: | ||
| raise click.ClickException(str(exc)) from exc | ||
|
|
||
| try: | ||
| pr_url = contribute_patch( | ||
| resolved=resolved, | ||
| host_cwd=cwd, | ||
| github_token=resolved_token, | ||
| run_id=run_id, | ||
| debug=debug, | ||
| ) | ||
| mark_patch_status( | ||
| patch_path=pathlib.Path(resolved["patch_path"]), | ||
| status="open", | ||
| pr_url=pr_url, | ||
| lockfile_path=resolved_lockfile, | ||
| ) | ||
| except (FileNotFoundError, KeyError, ValueError, RuntimeError, OSError) as exc: | ||
| raise click.ClickException(str(exc)) from exc | ||
|
|
||
| click.echo(f"Pull request opened: {pr_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
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
Oops, something went wrong.
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.