Skip to content

fix(nats): derive CloudEvents subject from metadata instead of hardcoded TODO#6269

Open
TRIVENI206 wants to merge 9 commits intomindersec:mainfrom
TRIVENI206:fix-nats-subject
Open

fix(nats): derive CloudEvents subject from metadata instead of hardcoded TODO#6269
TRIVENI206 wants to merge 9 commits intomindersec:mainfrom
TRIVENI206:fix-nats-subject

Conversation

@TRIVENI206
Copy link
Copy Markdown

…ded TODO

Summary

This PR replaces the hardcoded CloudEvents subject ("TODO") in the NATS eventing driver with a dynamically derived value from message metadata.

The subject is now determined using the following priority:

  • entity_id
  • repository_id or artifact_id
  • project_id

If none of these values are present, a fallback value ("minder") is used.

This improves event filtering in NATS JetStream and aligns with the CloudEvents specification, while keeping the change minimal and avoiding unrelated refactoring for easier review.

Fixes #6250

Testing

  • Built the NATS module using:
    go build ./internal/events/nats/...

  • Ran tests for the NATS module:
    go test ./internal/events/nats/...

  • Verified that:

    • The code compiles successfully
    • No tests in the NATS module are broken
    • The subject is now set dynamically instead of being hardcoded

…ded TODO

Signed-off-by: TRIVENI206 <trivenireddy206@gmail.com>
@TRIVENI206 TRIVENI206 requested a review from a team as a code owner April 3, 2026 19:21
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 3, 2026

CLA assistant check
All committers have signed the CLA.

@evankanderson
Copy link
Copy Markdown
Member

This seems to be conflicting with the work discussed in #6251 (and runs into the same question as to whether we can get by with simply entity_id given our simplification and standardization on entities last year.

@coveralls
Copy link
Copy Markdown

coveralls commented Apr 4, 2026

Coverage Status

Coverage is 58.618%TRIVENI206:fix-nats-subject into mindersec:main. No base build found for mindersec:main.

@TRIVENI206
Copy link
Copy Markdown
Author

Thanks for the feedback!

I based the implementation on multiple metadata fields for broader compatibility, but I see the point about standardizing on entity_id.

Would you prefer simplifying this to use only entity_id (with a fallback if missing), or keeping the current priority-based approach?

Happy to update the PR accordingly.

@evankanderson
Copy link
Copy Markdown
Member

I'd like to use only entity_ids, but only if that is correct. I haven't yet pulled in enough of the surrounding code to know that for sure. Some analysis and/or example cases from a running Minder instance would help.

@TRIVENI206
Copy link
Copy Markdown
Author

Thanks for the guidance!

I looked into how metadata is constructed across the codebase and found that entity_id is consistently used as the primary identifier. There are also indications (e.g., comments about replacing other identifiers) that the system is standardizing around entity_id.

Based on this, I’ve simplified the implementation to rely solely on entity_id (with a fallback), keeping the change aligned with the current design direction.

I’ve also verified that the NATS module builds successfully and tests pass locally .

Please let me know if this matches your expectations.

evankanderson
evankanderson previously approved these changes Apr 6, 2026
Copy link
Copy Markdown
Member

@evankanderson evankanderson left a comment

Choose a reason for hiding this comment

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

I agree with your assessment -- looking through the internal/providers/github/webhook code, it appears that things tend to bottom out in HandleEntityAndDoMessage, which has an entity and an originator.

It appears that there are also messages around app installation which won't have an entity ID associated with them, but the fixed "minder" string should work just as well as the fixed "TODO" string.

Comment on lines +209 to +217
subject := ""

if val, ok := msg.Metadata["entity_id"]; ok && val != "" {
subject = val
} else {
subject = "minder"
}

event.SetSubject(subject)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a stupid little Go trick, but you can use the default-zero return value from map lookup combined with cmp.Or (return first non-zero value) to write this as:

Suggested change
subject := ""
if val, ok := msg.Metadata["entity_id"]; ok && val != "" {
subject = val
} else {
subject = "minder"
}
event.SetSubject(subject)
event.SetSubject(cmp.Or(msg.Metadata["entity_id"], "minder"))

@evankanderson
Copy link
Copy Markdown
Member

(You don't have to do the "stupid Go trick", I just wanted to show some of the interesting language patterns)

@TRIVENI206
Copy link
Copy Markdown
Author

Thanks for the suggestion — that’s a neat Go pattern!

I’m happy to update the code to use cmp.Or if you’d prefer that style, or keep the current version for readability. Please let me know which you’d like.

Gitjay11 and others added 5 commits April 7, 2026 14:55
…c#6222)

* refactor: extract shared label filtering to internal/labels (mindersec#4982)

* refactor: address review feedback and test warnings

* refactor: remove error return from ParseLabelFilter and LabelsFromFilter
* fixed duplicate repo name in cli after filter

Signed-off-by: DharunMR <maddharun56@gmail.com>

* fixed duplicate repo name in cli after filter

Signed-off-by: DharunMR <maddharun56@gmail.com>

---------

Signed-off-by: DharunMR <maddharun56@gmail.com>
added a name flag to delete ruletype

Signed-off-by: DharunMR <maddharun56@gmail.com>
…sec#6296)

Update documentation

Co-authored-by: evankanderson <evankanderson@users.noreply.github.com>
Signed-off-by: TRIVENI206 <trivenireddy206@gmail.com>
@evankanderson
Copy link
Copy Markdown
Member

I'm fine merging this as-is after CI completes; I'm headed out on a run, but will do a pass this evening (maybe two)

Copy link
Copy Markdown
Member

@evankanderson evankanderson left a comment

Choose a reason for hiding this comment

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

It looks like you may need to fix a bad merge on this PR; after that, I'm happy to approve.

Comment thread internal/db/domain.go
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This file looks like it might be a bad merge -- try git checkout upstream/main internal/db/domain.go

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this has been fixed at head; try git pull upstream main after reverting this change.

Signed-off-by: TRIVENI206 <trivenireddy206@gmail.com>
Copy link
Copy Markdown
Member

@evankanderson evankanderson left a comment

Choose a reason for hiding this comment

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

You still seem to have some merge changes to clean up. You may want to record your changes, and then reset to head and cherry-pick your changes in.

event.SetSubject("TODO") // This *should* represent the entity, but we don't have a standard field for it yet.
subject := ""

if val, ok := msg.Metadata["entity_id"]; ok && val != "" {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You don't need to check for presence if you're already checking that val is non-zero-valued.

Suggested change
if val, ok := msg.Metadata["entity_id"]; ok && val != "" {
if val := msg.Metadata["entity_id"]; val != "" {

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.

Hardcoded "TODO" subject in CloudEvents NATS driver

6 participants