Skip to content

fix: ignore 404s and dup inserts in template advisory sync - #2283

Merged
xbhouse merged 1 commit into
RedHatInsights:masterfrom
xbhouse:handle-404s-and-dup-inserts
Jul 29, 2026
Merged

fix: ignore 404s and dup inserts in template advisory sync#2283
xbhouse merged 1 commit into
RedHatInsights:masterfrom
xbhouse:handle-404s-and-dup-inserts

Conversation

@xbhouse

@xbhouse xbhouse commented Jul 28, 2026

Copy link
Copy Markdown
Contributor
  • Skip sync if a template isn't found in content sources (it may have already been deleted)
  • Use ON CONFLICT DO NOTHING on template advisory inserts

Secure Coding Practices Checklist GitHub Link

Secure Coding Checklist

  • Input Validation
  • Output Encoding
  • Authentication and Password Management
  • Session Management
  • Access Control
  • Cryptographic Practices
  • Error Handling and Logging
  • Data Protection
  • Communication Security
  • System Configuration
  • Database Security
  • File Management
  • Memory Management
  • General Coding Practices

Summary by Sourcery

Handle missing templates and duplicate inserts gracefully in template advisory synchronization with content sources.

Bug Fixes:

  • Skip template advisory sync when the template is missing in content sources to avoid failing on deleted templates.
  • Prevent duplicate template advisory insert errors by using a no-op on-conflict insert strategy.

Enhancements:

  • Introduce a dedicated not-found error for content sources templates to standardize 404 handling in the listener.

Tests:

  • Add a test to verify that template advisory sync is skipped when content sources returns 404 for a template.

@sourcery-ai

sourcery-ai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Reviewer's Guide

Handle missing content-source templates during template advisory sync and prevent duplicate rows by using no-op-on-conflict bulk insert, with new coverage in tests and mocks.

File-Level Changes

Change Details Files
Skip template advisory sync when the template is missing in content-sources (404) and treat it as a non-error.
  • Add a sentinel error value for 'content sources template not found' and use it in error handling paths
  • Update httpCallCSTemplateAdvisories to map HTTP 404 responses into the sentinel error while returning an empty response body
  • Update syncTemplateAdvisories to detect the sentinel error, log a warning, and return success without modifying database state
listener/listener.go
listener/template_advisories.go
Avoid duplicate template advisory rows during sync by making bulk inserts idempotent.
  • Wrap the DB transaction used for bulk insert with a GORM OnConflict DoNothing clause before calling BulkInsert
  • Ensure existing advisory links are preserved even when content-sources returns overlapping IDs
listener/template_advisories.go
Extend tests and content-sources mock server to cover the new 404 behavior and shared org ID constant.
  • Introduce a shared orgID test constant and reuse it across template advisory tests
  • Add a test that verifies syncTemplateAdvisories returns nil and leaves DB state unchanged when content-sources responds 404 for a template UUID
  • Extend the template advisory IDs mock handler to return HTTP 404 for a specific UUID used in tests
listener/template_advisories_test.go
platform/content_sources.go

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

@xbhouse xbhouse changed the title fix: ignore 404s in CS and duplicate inserts in template advisory sync fix: ignore 404s and dup inserts in template advisory sync Jul 28, 2026
@xbhouse
xbhouse marked this pull request as ready for review July 28, 2026 21:42
@xbhouse
xbhouse requested a review from a team as a code owner July 28, 2026 21:42

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

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:

  • The 404 handling in httpCallCSTemplateAdvisories relies on err != nil for a 404 response, which may be brittle depending on how api.Client.Request reports non-2xx status codes; consider keying off httpResp.StatusCode alone when it’s non-nil.
  • The hard-coded 404 UUID string used in both the test and the mock handler (40499999-9999-9999-9999-999999999404) should be extracted to a shared constant to avoid drift between the test and mock implementation.
  • In the LogWarn call for errContentSourcesTemplateNotFound, the error is logged as a bare value (err.Error()), which may not align with structured logging expectations; consider logging it with an explicit key like "error", err.Error().
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The 404 handling in `httpCallCSTemplateAdvisories` relies on `err != nil` for a 404 response, which may be brittle depending on how `api.Client.Request` reports non-2xx status codes; consider keying off `httpResp.StatusCode` alone when it’s non-nil.
- The hard-coded 404 UUID string used in both the test and the mock handler (`40499999-9999-9999-9999-999999999404`) should be extracted to a shared constant to avoid drift between the test and mock implementation.
- In the `LogWarn` call for `errContentSourcesTemplateNotFound`, the error is logged as a bare value (`err.Error()`), which may not align with structured logging expectations; consider logging it with an explicit key like `"error", err.Error()`.

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.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.10%. Comparing base (66d6952) to head (be52e3e).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2283      +/-   ##
==========================================
+ Coverage   59.04%   59.10%   +0.06%     
==========================================
  Files         147      147              
  Lines        9324     9332       +8     
==========================================
+ Hits         5505     5516      +11     
+ Misses       3245     3244       -1     
+ Partials      574      572       -2     
Flag Coverage Δ
unittests 59.10% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TenSt TenSt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm!

@TenSt TenSt self-assigned this Jul 29, 2026
@xbhouse
xbhouse merged commit 9e7fdaf into RedHatInsights:master Jul 29, 2026
8 of 9 checks passed
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