Skip to content

HMS-9951: store template-advisory relationships on template update#2249

Draft
xbhouse wants to merge 1 commit into
RedHatInsights:masterfrom
xbhouse:HMS-9951
Draft

HMS-9951: store template-advisory relationships on template update#2249
xbhouse wants to merge 1 commit into
RedHatInsights:masterfrom
xbhouse:HMS-9951

Conversation

@xbhouse

@xbhouse xbhouse commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Summary

On template update events:

  • Calls content-sources API to fetch the current advisories for a template
  • Calculates the diff between returned and stored advisories
  • Stores updated relationships in the template_advisory table

On template delete events:

  • Removes the template_advisory entries for the deleted template

Testing steps

  1. Build and start the app: podman-compose up --build
  2. Send a template update event to the template topic: curl -X PUT http://localhost:9001/control/templates. There should be a template-updated message in the logs.
  3. The templates' advisories should be added to the template_advisory table:
podman exec -it db psql -d patchman -U admin

patchman=# select * from template_advisory;
 rh_account_id | template_id | advisory_id 
---------------+-------------+-------------
             1 |         112 |           1
             1 |         112 |           3
             1 |         113 |           1
             1 |         113 |           3
  1. Send the same update event. The template_advisory table should be unchanged.
  2. Send a template delete event: curl -X DELETE http://localhost:9001/control/templates. The templates' advisories should be removed from the template_advisory table.

Summary by Sourcery

Synchronize template advisory relationships with content-sources data on template lifecycle events.

New Features:

  • Integrate with the content-sources service to fetch advisory IDs for templates using authenticated requests.
  • Synchronize and persist template-to-advisory relationships in the database on template update events.
  • Expose a mock content-sources endpoint in the local platform for development and testing.

Enhancements:

  • Remove template_advisory records when templates are deleted to keep advisory relationships consistent.
  • Add helper utilities for encoding x-rh-identity headers and constructing identities from org IDs.
  • Extend listener metrics and configuration to support content-sources integration and correct event-type labeling.

Tests:

  • Add unit and integration tests covering advisory diffing, lookup, synchronization logic, and database helpers for template_advisory records.
  • Add tests for new identity utilities used in outbound content-sources requests.

@sourcery-ai

sourcery-ai Bot commented Jun 25, 2026

Copy link
Copy Markdown

Reviewer's Guide

Implements synchronization of template–advisory relationships with Content Sources on template update events, persists them in the template_advisory table, cleans them up on template deletion, and adds supporting utilities, configuration, and tests for the new behavior and Content Sources integration.

File-Level Changes

Change Details Files
Add DB test helpers for creating, checking, and deleting template_advisory relationships to support new advisory sync tests.
  • Introduce CreateTemplateAdvisories to insert template-advisory rows for a given account, template, and list of advisory IDs
  • Introduce CheckTemplateAdvisories to assert that all expected advisory IDs exist for a template
  • Introduce DeleteTemplateAdvisories to delete specific template-advisory rows and assert they were removed
base/database/testing.go
Wire template lifecycle handling to manage template_advisory rows and call advisory sync on updates, including proper metric labeling and identity handling.
  • Extend TemplateDelete to also delete template_advisory rows for the template and account before deleting the template itself
  • Change TemplateUpdate signature to accept eventType, use it for metrics, and on update events call syncTemplateAdvisories when CONTENT_SOURCES_ADDRESS is configured
  • Initialize a shared Content Sources client and base URL during listener configuration when template processing is enabled
listener/templates.go
listener/listener.go
Introduce identity utilities for constructing and encoding x-rh-identity headers used for Content Sources calls, with tests.
  • Add EncodeXRHID to JSON-marshal an identity.XRHID and base64-encode it
  • Add XRHIDForOrg to construct a basic identity.XRHID for a given org ID
  • Add unit tests validating encoding of identities and constructing XRHIDForOrg
base/utils/identity.go
base/utils/identity_test.go
Add configuration and mock platform integration for the Content Sources service.
  • Extend coreConfig and env/Clowder initialization to include CONTENT_SOURCES_ADDRESS and discover content-sources-backend
  • Print CONTENT_SOURCES_ADDRESS in service parameter logs
  • Initialize mock Content Sources endpoints in platformMock and implement a handler that returns fixed advisory IDs for a template
base/utils/config.go
platform/platform.go
platform/content_sources.go
conf/common.env
conf/local.env
Implement template advisory synchronization logic using Content Sources and advisory_metadata, including differential updates and associated tests.
  • Add syncTemplateAdvisories to fetch current advisories from Content Sources, diff them with stored template_advisory rows, delete obsolete relations, and bulk-insert new ones in a transaction
  • Add helpers to look up existing template advisories, compute diffs, resolve advisory names to advisory_metadata IDs, build rows for template_advisory, delete old rows, and make HTTP requests to Content Sources with retries and x-rh-identity header
  • Add tests covering the HTTP call to Content Sources, diff logic, DB lookups, row building (including skipping missing advisories), and end-to-end syncTemplateAdvisories behavior
listener/template_advisories.go
listener/template_advisories_test.go
Introduce a small Content Sources client abstraction for template advisory ID retrieval.
  • Define TemplateAdvisoryIDsResponse struct matching Content Sources response schema
  • Provide CreateContentSourcesClient helper that returns an api.Client configured with an HTTP client and debug flag based on log level
base/content_sources/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 feat: store template-advisory relationships on template update HMS-9951: store template-advisory relationships on template update Jun 25, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 65.62500% with 55 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.16%. Comparing base (b2d5a7d) to head (74db125).
⚠️ Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
listener/template_advisories.go 79.20% 11 Missing and 10 partials ⚠️
base/database/testing.go 0.00% 19 Missing ⚠️
base/content_sources/content_sources.go 0.00% 5 Missing ⚠️
listener/templates.go 64.28% 3 Missing and 2 partials ⚠️
base/utils/config.go 25.00% 3 Missing ⚠️
base/utils/identity.go 85.71% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2249      +/-   ##
==========================================
+ Coverage   59.06%   59.16%   +0.10%     
==========================================
  Files         138      140       +2     
  Lines        8848     9003     +155     
==========================================
+ Hits         5226     5327     +101     
- Misses       3076     3117      +41     
- Partials      546      559      +13     
Flag Coverage Δ
unittests 59.16% <65.62%> (+0.10%) ⬆️

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.

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