Skip to content

Conversation

@kkaarreell
Copy link
Collaborator

@kkaarreell kkaarreell commented Jan 20, 2026

Introduces a new CLI option -C/--copy-state-dir that allows users to copy YAML files from an existing state directory to a newly created state-dir. This is useful for reusing state from a different machine or shared location without modifying the original state-dir.

Key features:

  • Copies all YAML files from the specified source directory
  • Creates a new state directory with proper initialization
  • Validates source directory existence before copying
  • Cannot be used together with --extract-state-dir (mutual exclusivity check)
  • Logs copying progress with info and debug messages

🤖 Generated with Claude Code

Summary by Sourcery

Add support for initializing a new state directory by copying YAML files from an existing state directory, with safeguards around usage.

New Features:

  • Introduce --copy-state-dir (-C) CLI option to copy YAML files from an existing state directory into a newly created state-dir.

Enhancements:

  • Enforce mutual exclusivity between --copy-state-dir and --extract-state-dir to prevent conflicting state initialization modes.

Documentation:

  • Document the new --copy-state-dir (-C) option with usage examples and clarify the short option for --extract-state-dir (-E) in the README.

@kkaarreell kkaarreell self-assigned this Jan 20, 2026
@sourcery-ai
Copy link

sourcery-ai bot commented Jan 20, 2026

Reviewer's Guide

Adds a new -C/--copy-state-dir CLI option to copy YAML files from an existing state directory into a newly initialized state directory, enforces mutual exclusivity with --extract-state-dir, and documents the new behavior in the README.

Flow diagram for handling --extract-state-dir and --copy-state-dir options

flowchart TD
    Start[Start newa CLI] --> ParseOptions[Parse CLI options]
    ParseOptions --> CheckMutualExcl[Check extract_state_dir and copy_state_dir]

    CheckMutualExcl -->|both set| Error[Raise ClickException: Cannot use both options]
    CheckMutualExcl -->|only extract_state_dir| DoExtract[Extract YAML from archive to state_dirpath]
    CheckMutualExcl -->|only copy_state_dir| DoCopy[Copy YAML from source state dir to new state_dirpath]
    CheckMutualExcl -->|neither set| Continue[Continue with existing or new state_dirpath]

    DoExtract --> ForceFlag[Set ctx.force true]
    ForceFlag --> OpenArchive[Open archive via tarfile]
    OpenArchive --> ExtractMembers[Extract YAML files into ctx.state_dirpath]
    ExtractMembers --> InitDirExtract[initialize_state_dir ctx]
    InitDirExtract --> NextStepsExtract[Proceed with further CLI actions]

    DoCopy --> ResolveSource[Resolve and validate source_dir path]
    ResolveSource -->|not exists or not dir| ErrorSource[Raise ClickException for invalid source_dir]
    ResolveSource --> InitDirCopy[initialize_state_dir ctx]
    InitDirCopy --> FindYaml[List *.yaml files in source_dir]
    FindYaml -->|none found| WarnNone[Log warning: no YAML files]
    FindYaml -->|one or more| CopyLoop[Copy each YAML file to ctx.state_dirpath]
    CopyLoop --> LogCopied[Log number of copied YAML files]
    WarnNone --> NextStepsCopy[Proceed with further CLI actions]
    LogCopied --> NextStepsCopy[Proceed with further CLI actions]

    Continue --> NextStepsNoStateOp[Proceed with further CLI actions]
Loading

File-Level Changes

Change Details Files
Introduce --copy-state-dir/-C option to copy YAML files from an existing state directory into a new state directory.
  • Add click option definition for --copy-state-dir/-C with default empty string and help text.
  • Extend main() parameters to accept copy_state_dir.
  • Resolve and validate the source directory path, ensuring it exists and is a directory before proceeding.
  • Initialize the destination state directory prior to copying.
  • Copy all *.yaml files from the source directory to the destination using shutil.copy2, logging info/debug messages and warning when no YAML files are found.
newa/cli/main.py
Enforce mutual exclusivity between state extraction and state copying options and keep extract path behavior consistent.
  • Add a runtime check that raises click.ClickException when both --extract-state-dir and --copy-state-dir are provided.
  • Ensure initialization of state directory happens in the appropriate branches for extract and copy flows.
newa/cli/main.py
Document the new copy-state-dir behavior and update extract-state-dir option docs.
  • Update README section header to show the short option -E for --extract-state-dir.
  • Add a new README subsection describing --copy-state-dir/-C semantics, mutual exclusivity with --extract-state-dir, and usage examples including a network share scenario.
README.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 4 issues, and left some high level feedback:

  • The initialize_state_dir helper is now called from both the --extract-state-dir and --copy-state-dir branches but the local import inside the extract branch was removed; ensure it is imported at module scope (or otherwise available) so both code paths work correctly.
  • The copy logic only includes *.yaml files; if your state dirs may contain .yml files or YAML in subdirectories, consider whether those should also be copied and adjust the globbing accordingly.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `initialize_state_dir` helper is now called from both the `--extract-state-dir` and `--copy-state-dir` branches but the local import inside the extract branch was removed; ensure it is imported at module scope (or otherwise available) so both code paths work correctly.
- The copy logic only includes `*.yaml` files; if your state dirs may contain `.yml` files or YAML in subdirectories, consider whether those should also be copied and adjust the globbing accordingly.

## Individual Comments

### Comment 1
<location> `newa/cli/main.py:220` </location>
<code_context>

+    # copy YAML files from the given state directory to a new state-dir
+    if copy_state_dir:
+        source_dir = Path(os.path.expandvars(copy_state_dir))
+        if not source_dir.exists():
+            raise click.ClickException(
</code_context>

<issue_to_address>
**suggestion:** Consider also expanding `~` in paths, not just environment variables.

`os.path.expandvars` only handles `$VAR` placeholders, so values like `~/state-dir` won’t resolve. Consider wrapping with `os.path.expanduser` as well, e.g. `Path(os.path.expanduser(os.path.expandvars(copy_state_dir)))`, to support both env vars and home-directory shortcuts.

Suggested implementation:

```python
        source_dir = Path(os.path.expanduser(os.path.expandvars(copy_state_dir)))

```

If `os` is not already imported at the top of `newa/cli/main.py`, add:

```python
import os
```

near the other imports so that `os.path.expanduser` and `os.path.expandvars` are available.
</issue_to_address>

### Comment 2
<location> `README.md:727` </location>
<code_context>
-#### Option `--extract-state-dir`
+#### Option `--extract-state-dir`, `-E`

 Similar to `--state-dir`, however in this case the argument is URL of an archive containing NEWA YAML metadata files. For example, it could be used to follow up on a state-dir created and shared by an automation.

</code_context>

<issue_to_address>
**suggestion (typo):** Consider adding "the" before "URL" for smoother grammar.

Please update the sentence to: "the argument is the URL of an archive containing NEWA YAML metadata files."

```suggestion
Similar to `--state-dir`, however in this case the argument is the URL of an archive containing NEWA YAML metadata files. For example, it could be used to follow up on a state-dir created and shared by an automation.
```
</issue_to_address>

### Comment 3
<location> `README.md:750-752` </location>
<code_context>
+$ newa --copy-state-dir /mnt/shared/newa-state-dir/run-456 jira --issue-config config.yaml schedule execute report
+```
+
 #### Option `--context, -c`

 Allows custom `tmt` context definition on a cmdline. Such a context can be used in issue-config YAML file through Jinja template through `CONTEXT.<name>`. Option can be used multiple times.
</code_context>

<issue_to_address>
**suggestion (typo):** Wording "on a cmdline" is a bit awkward; consider "on the command line".

This small change will improve readability of the documentation for users.
</issue_to_address>

### Comment 4
<location> `README.md:752` </location>
<code_context>
+
 #### Option `--context, -c`

 Allows custom `tmt` context definition on a cmdline. Such a context can be used in issue-config YAML file through Jinja template through `CONTEXT.<name>`. Option can be used multiple times.
</code_context>

<issue_to_address>
**issue (typo):** Duplicated "through" in this sentence looks like a typo.

The phrase "through Jinja template through `CONTEXT.<name>`" repeats "through". Consider changing it to something like "in an issue-config YAML file via a Jinja template using `CONTEXT.<name>`" or otherwise removing the duplicate word.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Introduces a new CLI option -C/--copy-state-dir that allows users to copy
YAML files from an existing state directory to a newly created state-dir.
This is useful for reusing state from a different machine or shared location
without modifying the original state-dir.

Key features:
- Copies all YAML files from the specified source directory
- Creates a new state directory with proper initialization
- Validates source directory existence before copying
- Cannot be used together with --extract-state-dir (mutual exclusivity check)
- Logs copying progress with info and debug messages

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@kkaarreell kkaarreell force-pushed the ks_cli_copy branch 5 times, most recently from 18689a1 to 888d4a0 Compare January 20, 2026 08:40
@kkaarreell kkaarreell merged commit e629551 into main Jan 20, 2026
14 checks passed
@kkaarreell kkaarreell deleted the ks_cli_copy branch January 20, 2026 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants