Skip to content

Search generated_reports and other improvements to report file paths and dev setup#1155

Merged
jeremylenz merged 2 commits intotheforeman:developfrom
jeremylenz:improve-report-file-paths
Feb 25, 2026
Merged

Search generated_reports and other improvements to report file paths and dev setup#1155
jeremylenz merged 2 commits intotheforeman:developfrom
jeremylenz:improve-report-file-paths

Conversation

@jeremylenz
Copy link
Collaborator

@jeremylenz jeremylenz commented Feb 23, 2026

What are the changes introduced in this pull request?

  • Updates report_file_paths to search for reports in priority order: generated_reports → uploads → done, ensuring downloads return the most recent/complete report. Previously, generated_reports was not searched at all.
  • Adds generated_reports_file_path helper method for consistency
  • Fixes base_folder to use parent directory in dev environments, keeping generated files in /home/vagrant/red_hat_inventory/ instead of polluting the Foreman repo directory
  • Fixes typo in comment: /ver/lib/foreman/var/lib/foreman

Considerations taken when implementing this change?

The priority order reflects the report lifecycle:

  1. generated - Reports end up here if the user chose not to upload them, or if the upload fails completely
  2. uploads - Reports end up here if the upload is in progress or interrupted
  3. done - Reports end up here if the upload is complete

The report files are moved (not copied) from generated > uploaded > done, and overwrite any file with the same name in the destination folder. For this reason:

  • if the most recent report was generated only, it will be in the generated folder
  • if the most recent report was uploaded, it will probably be in the done folder, and there will be nothing in the generated folder.

Hence the ordering above.

This ensures hammer CLI and the frontend download button serve the most up-to-date report available.

Change 2 - Dev environment report location

For dev environments, using File.dirname(Dir.getwd) instead of Dir.getwd moves the red_hat_inventory/ folder location to keep report files outside the git repository, avoiding clutter in git status.

What are the testing steps for this pull request?

  • Generate a report for an organization
  • Verify the report appears in generated_reports/ folder
  • Download the report via hammer CLI: hammer insights inventory download-report --organization-id 1 --path /tmp
  • Verify the correct report file is downloaded
  • In dev environment, verify reports are created in /home/vagrant/red_hat_inventory/ instead of /home/vagrant/foreman/red_hat_inventory/

🤖 Generated with Claude Code

Summary by Sourcery

Adjust report storage and lookup to prioritize completed reports and keep development artifacts out of the Foreman repo directory.

New Features:

  • Add a helper to build file paths for generated reports for consistent report handling.

Bug Fixes:

  • Correct the base folder path comment from /ver/lib/foreman to /var/lib/foreman.

Enhancements:

  • Change the base folder resolution in development to use the parent of the current working directory so generated files are stored outside the Foreman repository.
  • Update report file path discovery to search done, uploads, then generated_reports folders in priority order to return the most complete report available.

@sourcery-ai
Copy link

sourcery-ai bot commented Feb 23, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors how report file paths are resolved and adjusts the base storage folder behavior, prioritizing completed and uploaded reports while keeping dev-generated files outside the Foreman repo, plus a minor comment typo fix.

Sequence diagram for resolving report file paths during download

sequenceDiagram
  actor User
  participant HammerCLI
  participant ForemanServer
  participant ForemanInventoryUpload
  participant Filesystem

  User->>HammerCLI: inventory download-report --organization-id
  HammerCLI->>ForemanServer: HTTP request for latest report
  ForemanServer->>ForemanInventoryUpload: report_file_paths(organization_id)
  activate ForemanInventoryUpload
  ForemanInventoryUpload->>ForemanInventoryUpload: facts_archive_name(organization_id)
  ForemanInventoryUpload->>ForemanInventoryUpload: done_file_path(filename)
  ForemanInventoryUpload->>ForemanInventoryUpload: uploads_file_path(filename)
  ForemanInventoryUpload->>ForemanInventoryUpload: generated_reports_file_path(filename)
  ForemanInventoryUpload->>Filesystem: Dir[done, uploads, generated_reports]
  Filesystem-->>ForemanInventoryUpload: matching file list (priority order)
  deactivate ForemanInventoryUpload
  ForemanInventoryUpload-->>ForemanServer: ordered report paths
  ForemanServer->>Filesystem: read first existing file
  Filesystem-->>ForemanServer: report archive
  ForemanServer-->>HammerCLI: report file content
  HammerCLI-->>User: save report to requested path
Loading

Class diagram for updated ForemanInventoryUpload module

classDiagram
  class ForemanInventoryUpload {
    +base_folder()
    +done_folder()
    +done_file_path(filename)
    +uploads_folder()
    +uploads_file_path(filename)
    +generated_reports_folder()
    +generated_reports_file_path(filename)
    +report_file_paths(organization_id)
    +outputs_folder()
    +facts_archive_name(organization_id)
  }
Loading

Flow diagram for selecting base_folder in different environments

flowchart TD
  A[start] --> B[Check for /var/lib/foreman using Dir.glob]
  B -->|found| C[use /var/lib/foreman as root]
  B -->|not found| D["use File.dirname(Dir.getwd) as root"]
  C --> E[Append red_hat_inventory/]
  D --> E[Append red_hat_inventory/]
  E --> F[Return base_folder path]
  F --> G[end]
Loading

File-Level Changes

Change Details Files
Adjust base_folder resolution to keep dev environment files outside the Foreman git repo while preserving production behavior.
  • Keep using /var/lib/foreman when available, maintaining SELinux-friendly production behavior.
  • Change the dev fallback from Dir.getwd to File.dirname(Dir.getwd) so red_hat_inventory/ is created alongside the Foreman repo instead of inside it.
  • Clarify the accompanying comment to describe using the parent of the current working directory in dev setups.
lib/foreman_inventory_upload.rb
Extend and reorder report file path lookup to prefer completed and uploaded reports and include generated reports as a final fallback.
  • Introduce generated_reports_folder helper using ensure_folder under base_folder/generated_reports/.
  • Add generated_reports_file_path helper for consistent path construction to generated_reports files.
  • Update report_file_paths to search in priority order: done, uploads, then generated_reports using the filename from facts_archive_name(organization_id).
lib/foreman_inventory_upload.rb
Fix typo in documentation comment for the Foreman data directory path.
  • Correct /ver/lib/foreman to /var/lib/foreman in the comment above base_folder.
lib/foreman_inventory_upload.rb

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 left some high level feedback:

  • Using File.dirname(Dir.getwd) for the dev base_folder can produce surprising paths when the process is started from a subdirectory; consider anchoring this to a known project root (e.g., Rails.root or a configured base path) instead of the parent of the current working directory.
  • The report search priority (done → uploads → generated_reports) is now encoded in report_file_paths; consider adding a brief inline comment or extracting this ordering to a small constant to make the lifecycle assumptions and precedence explicit and easier to maintain.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Using `File.dirname(Dir.getwd)` for the dev `base_folder` can produce surprising paths when the process is started from a subdirectory; consider anchoring this to a known project root (e.g., Rails.root or a configured base path) instead of the parent of the current working directory.
- The report search priority (done → uploads → generated_reports) is now encoded in `report_file_paths`; consider adding a brief inline comment or extracting this ordering to a small constant to make the lifecycle assumptions and precedence explicit and easier to maintain.

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.

@jeremylenz jeremylenz force-pushed the improve-report-file-paths branch from c4ebe6f to 0f5b372 Compare February 23, 2026 19:56
@jeremylenz
Copy link
Collaborator Author

#1156 should fix the test failures.

@jeremylenz jeremylenz force-pushed the improve-report-file-paths branch from 0f5b372 to 9d265e3 Compare February 23, 2026 21:53
@LadislavVasina1
Copy link
Collaborator

@jeremylenz
With packit build, I have tried generating the report using
hammer insights inventory generate-report --organization-id=5 --no-upload
this command outputs:
Report generation started successfully (RC:0)
but the following command
hammer insights inventory download-report --organization-id=5 --path="/tmp" fails with

Failed to download report:
  ERF95-3648 [InventoryUpload::ReportActions::ReportMissingError]: The report file report_for_5.tar.xz doesn't exist

@jeremylenz
Copy link
Collaborator Author

@LadislavVasina1 I've temporarily cherry-picked the commit from #1158 here - see if it works better now.

Updates report_file_paths to search in priority order (done, uploads,
generated) ensuring downloads return the most recent report. Also fixes
base_folder to use parent directory in dev environments, keeping
generated files outside the Foreman repo directory.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@jeremylenz jeremylenz force-pushed the improve-report-file-paths branch 2 times, most recently from 1e17502 to 42c2937 Compare February 24, 2026 21:38
@jeremylenz
Copy link
Collaborator Author

rebased after the other PR was merged.

Explains the file lifecycle through generated → uploads → done
folders and why the search order prioritizes newest files first.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@jeremylenz jeremylenz force-pushed the improve-report-file-paths branch from 42c2937 to 33452b6 Compare February 24, 2026 21:41
@jeremylenz jeremylenz changed the title Improve report file paths and dev environment setup Search generated_reports and other improvements to report file paths and dev setup Feb 24, 2026
@jeremylenz
Copy link
Collaborator Author

updated the PR description to remove the AI slop :)

Copy link
Collaborator

@LadislavVasina1 LadislavVasina1 left a comment

Choose a reason for hiding this comment

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

ACK, report can be generated and downloaded, robottelo test testing this passes with packit.

@nofaralfasi nofaralfasi self-assigned this Feb 25, 2026
Copy link
Collaborator

@nofaralfasi nofaralfasi left a comment

Choose a reason for hiding this comment

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

LGTM!
Tested and works as expected.

  • Reports are correctly discovered in the correct folders
  • UI "Report saved to" path is showing the correct paths

@jeremylenz jeremylenz merged commit fb0e652 into theforeman:develop Feb 25, 2026
13 of 14 checks passed
zjhuntin pushed a commit to zjhuntin/foreman_rh_cloud that referenced this pull request Mar 4, 2026
…and dev setup (theforeman#1155)

* Improve report file paths and dev environment setup

Updates report_file_paths to search in priority order (done, uploads,
generated) ensuring downloads return the most recent report. Also fixes
base_folder to use parent directory in dev environments, keeping
generated files outside the Foreman repo directory.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* Add documentation for report_file_paths search order

Explains the file lifecycle through generated → uploads → done
folders and why the search order prioritizes newest files first.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
chris1984 pushed a commit that referenced this pull request Mar 4, 2026
…and dev setup (#1155)

* Improve report file paths and dev environment setup

Updates report_file_paths to search in priority order (done, uploads,
generated) ensuring downloads return the most recent report. Also fixes
base_folder to use parent directory in dev environments, keeping
generated files outside the Foreman repo directory.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* Add documentation for report_file_paths search order

Explains the file lifecycle through generated → uploads → done
folders and why the search order prioritizes newest files first.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
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.

3 participants