-
Notifications
You must be signed in to change notification settings - Fork 888
feat(webhook): Send updated entries to webhook on feed refresh #4235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
igortheclaw
wants to merge
2
commits into
miniflux:main
Choose a base branch
from
igortheclaw:fix/push-updated-entries-to-integrations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package webhook | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "miniflux.app/v2/internal/config" | ||
| "miniflux.app/v2/internal/model" | ||
| ) | ||
|
|
||
| // configureIntegrationAllowPrivateNetworksOption sets the global config option | ||
| // required to allow the webhook HTTP client to reach the httptest server on | ||
| // localhost (a private address). It restores the previous config on test cleanup. | ||
| func configureIntegrationAllowPrivateNetworksOption(t *testing.T) { | ||
| t.Helper() | ||
|
|
||
| t.Setenv("INTEGRATION_ALLOW_PRIVATE_NETWORKS", "1") | ||
|
|
||
| configParser := config.NewConfigParser() | ||
| parsedOptions, err := configParser.ParseEnvironmentVariables() | ||
| if err != nil { | ||
| t.Fatalf("Unable to configure test options: %v", err) | ||
| } | ||
|
|
||
| previousOptions := config.Opts | ||
| config.Opts = parsedOptions | ||
| t.Cleanup(func() { | ||
| config.Opts = previousOptions | ||
| }) | ||
| } | ||
|
|
||
| func testFeed() *model.Feed { | ||
| return &model.Feed{ | ||
| ID: 1, | ||
| UserID: 1, | ||
| Category: &model.Category{ | ||
| ID: 1, | ||
| Title: "Test", | ||
| }, | ||
| FeedURL: "https://example.org/feed.xml", | ||
| SiteURL: "https://example.org", | ||
| Title: "Test Feed", | ||
| CheckedAt: time.Now(), | ||
| } | ||
| } | ||
|
|
||
| func testEntries() model.Entries { | ||
| return model.Entries{ | ||
| {ID: 10, UserID: 1, FeedID: 1, URL: "https://example.org/post-1", Title: "Post 1"}, | ||
| } | ||
| } | ||
|
|
||
| // TestSendNewEntriesWebhookEventType verifies that SendNewEntriesWebhookEvent | ||
| // sends a request whose JSON body has event_type = "new_entries". | ||
| func TestSendNewEntriesWebhookEventType(t *testing.T) { | ||
| configureIntegrationAllowPrivateNetworksOption(t) | ||
|
|
||
| var gotBody []byte | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| gotBody, _ = io.ReadAll(r.Body) | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| client := NewClient(srv.URL, "") | ||
| if err := client.SendNewEntriesWebhookEvent(testFeed(), testEntries()); err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| var payload map[string]any | ||
| if err := json.Unmarshal(gotBody, &payload); err != nil { | ||
| t.Fatalf("unable to unmarshal payload: %v", err) | ||
| } | ||
|
|
||
| if got := payload["event_type"]; got != NewEntriesEventType { | ||
| t.Errorf("expected event_type %q, got %q", NewEntriesEventType, got) | ||
| } | ||
| } | ||
|
|
||
| // TestSendUpdatedEntriesWebhookEventType verifies that SendUpdatedEntriesWebhookEvent | ||
| // sends a request whose JSON body has event_type = "updated_entries". | ||
| func TestSendUpdatedEntriesWebhookEventType(t *testing.T) { | ||
| configureIntegrationAllowPrivateNetworksOption(t) | ||
|
|
||
| var gotBody []byte | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| gotBody, _ = io.ReadAll(r.Body) | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| client := NewClient(srv.URL, "") | ||
| if err := client.SendUpdatedEntriesWebhookEvent(testFeed(), testEntries()); err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| var payload map[string]any | ||
| if err := json.Unmarshal(gotBody, &payload); err != nil { | ||
| t.Fatalf("unable to unmarshal payload: %v", err) | ||
| } | ||
|
|
||
| if got := payload["event_type"]; got != UpdatedEntriesEventType { | ||
| t.Errorf("expected event_type %q, got %q", UpdatedEntriesEventType, got) | ||
| } | ||
| } | ||
|
|
||
| // TestSendUpdatedEntriesWebhookEventTypeHeader verifies that the | ||
| // X-Miniflux-Event-Type header is set to "updated_entries". | ||
| func TestSendUpdatedEntriesWebhookEventTypeHeader(t *testing.T) { | ||
| configureIntegrationAllowPrivateNetworksOption(t) | ||
|
|
||
| var gotHeader string | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| gotHeader = r.Header.Get("X-Miniflux-Event-Type") | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| client := NewClient(srv.URL, "") | ||
| if err := client.SendUpdatedEntriesWebhookEvent(testFeed(), testEntries()); err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| if gotHeader != UpdatedEntriesEventType { | ||
| t.Errorf("expected X-Miniflux-Event-Type header %q, got %q", UpdatedEntriesEventType, gotHeader) | ||
| } | ||
| } | ||
|
|
||
| // TestSendUpdatedEntriesWebhookNoEntriesIsNoop verifies that | ||
| // SendUpdatedEntriesWebhookEvent returns nil and makes no HTTP call when | ||
| // the entries slice is empty. | ||
| func TestSendUpdatedEntriesWebhookNoEntriesIsNoop(t *testing.T) { | ||
| called := false | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| called = true | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| client := NewClient(srv.URL, "") | ||
| if err := client.SendUpdatedEntriesWebhookEvent(testFeed(), model.Entries{}); err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| if called { | ||
| t.Error("expected no HTTP call for empty entries, but server was called") | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SendUpdatedEntriesWebhookEvent()builds the webhook payload using fields likeStatus,CreatedAt,ChangedAt,ShareCode, andStarredfrom the providedentries. For updated entries coming from feed parsing/refresh, these fields are typically unset (model.NewEntry leaves them as zero values), so theupdated_entriespayload can be misleading (blank status, zero timestamps, etc.). Consider ensuring the caller passes fully-hydrated entries from the DB (or adjust the storage refresh/update path to RETURNING/scan these columns) before constructing the webhook payload.