Skip to content
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

gateway: add api to get run webhook deliveries #452

Merged
merged 5 commits into from
Nov 16, 2023

Conversation

alessandro-sorint
Copy link
Contributor

@alessandro-sorint alessandro-sorint commented Oct 23, 2023

This patch adds an api to get run webhook deliveries by project ref.
The RunWebhook type add ProjectID field.

fix part of #424

@alessandro-sorint alessandro-sorint force-pushed the api-runwebhookdeliveries branch 2 times, most recently from fbb24a6 to 2dc532f Compare October 24, 2023 11:56
@alessandro-sorint alessandro-sorint changed the title gateway: add api to get run webhook deliveries gateway: add api to get run webhooks Oct 24, 2023
@alessandro-sorint
Copy link
Contributor Author

@sgotti can you do the review please?

@alessandro-sorint alessandro-sorint changed the title gateway: add api to get run webhooks gateway: add api to get run webhook deliveries Oct 26, 2023
internal/services/gateway/gateway.go Outdated Show resolved Hide resolved
internal/services/gateway/action/action.go Outdated Show resolved Hide resolved
const (
SortDirectionAsc SortDirection = "asc"
SortDirectionDesc SortDirection = "desc"
)
Copy link
Member

Choose a reason for hiding this comment

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

missing end of line?

internal/services/config/config.go Show resolved Hide resolved
internal/services/config/config_test.go Show resolved Hide resolved
@@ -69,3 +70,41 @@ func (d *DB) migrateV3(tx *sql.Tx) error {

return nil
}

func (d *DB) migrateV4(tx *sql.Tx) error {
var dmlGeneric = "DELETE FROM runwebhookdelivery"
Copy link
Member

Choose a reason for hiding this comment

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

so the migration will delete all the current webhook data?

Copy link
Member

Choose a reason for hiding this comment

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

Can you be more detailed?

Since the notification service changes are done in this development cycle could just drop all the notification service data. But if we follow this line we could just also remove all the notifications service migrations until the official next release (people using master should manually clean the notification service db).

Instead if we're doing migrations then we should correctly migrate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok @sgotti we can do migration, I can get ProjectID from Payload and populate the new column using a temporary table.

HasMore bool
}

func (h *ActionHandler) GetRunWebhookDeliveries(ctx context.Context, req *GetRunWebhookDeliveriesRequest) (*GetRunWebhooksDeliveriesResponse, error) {
Copy link
Member

Choose a reason for hiding this comment

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

It's runwebhook deliveries by project so the types and functions names should reflect this.

@alessandro-sorint alessandro-sorint force-pushed the api-runwebhookdeliveries branch 2 times, most recently from f31c5bb to 3f7917d Compare November 9, 2023 09:01
@@ -15,6 +15,14 @@ const (
DeliveryStatusDeliveryError DeliveryStatus = "deliveryError"
)

func DeliveryStatusFromStringSlice(slice []string) []DeliveryStatus {
Copy link
Member

Choose a reason for hiding this comment

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

A test for this function is probably needed.

@@ -0,0 +1,36 @@
package action
Copy link
Member

Choose a reason for hiding this comment

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

Probably actionHandler shouldn't belong to this commit but to the commit that add the apis handlers.

internal/services/notification/db/migrate.go Show resolved Hide resolved
internal/services/notification/webhooks.go Show resolved Hide resolved
@alessandro-sorint alessandro-sorint force-pushed the api-runwebhookdeliveries branch 3 times, most recently from 896e291 to 89e2c9e Compare November 10, 2023 09:24
@alessandro-sorint alessandro-sorint force-pushed the api-runwebhookdeliveries branch 4 times, most recently from c90272a to 587475c Compare November 13, 2023 15:08
Copy link
Member

Choose a reason for hiding this comment

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

I reversed engineered this commit and discovered that this change was done because the previous payload had a missing final curly bracket. Right? If so fix this before other changes in a dedicated commit so it's documented.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I changed also dbv1, dbv3 and import jsonc

Copy link
Member

Choose a reason for hiding this comment

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

Can you please detail the commit description explaining what has been changed since the base64 changes aren't self explaining?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the commit message (I looked better the json has an excess curly bracket and not missed)

{"exportMeta":{"kind":"RunWebhook"},"id":"ce98dd37-b7fd-4d0a-a744-8e1545de3a6c","creationTime":"2023-04-03T12:07:16.839654667Z","updateTime":"2023-04-03T12:07:16.839654667Z","payload":"eyJwcm9qZWN0X2luZm8iOnsicHJvamVjdF9pZCI6IjU3NTg4ZmU0LWI3YjgtNDdiOS1hNzhmLTdkOGUwNGRlMjU0NiJ9LCJydW4iOnsiaWQiOiIzNTU4OTllMi0xZDE3LTQ0YjMtYjMyYS04NDczZDQyYjE1NTEiLCJyZWZfdHlwZSI6ImJyYW5jaCIsInJlZiI6InJlZnMvaGVhZHMvbWFzdGVyIiwibmFtZSI6InJ1bjAxIiwiY291bnRlciI6MSwicGhhc2UiOiJxdWV1ZWQiLCJyZXN1bHQiOiJ1bmtub3duIiwidGFza3MiOnsiODhiNDBlNGEtNDY5MC00MGFkLWJlZGMtZmE3ZWE2ZWNlNmY0Ijp7ImlkIjoiODhiNDBlNGEtNDY5MC00MGFkLWJlZGMtZmE3ZWE2ZWNlNmY0IiwibmFtZSI6InRhc2swMSIsInN0YXR1cyI6Im5vdHN0YXJ0ZWQiLCJzZXR1cF9zdGVwIjp7InBoYXNlIjoibm90c3RhcnRlZCJ9LCJzdGVwcyI6W3sicGhhc2UiOiJub3RzdGFydGVkIn1dfX0sImVucXVldWVfdGltZSI6IjIwMjMtMDctMDNUMTE6MTA6MjYuMzA2Njk1OTk5KzAyOjAwIn0sIlZlcnNpb24iOjF9fQ=="}
{"exportMeta":{"kind":"RunWebhook"},"id":"d07ec67f-7a81-48e7-9d94-a87d29ca6ca3","creationTime":"2023-04-03T12:07:16.84108568Z","updateTime":"2023-04-03T12:07:16.84108568Z","payload":"eyJwcm9qZWN0X2luZm8iOnsicHJvamVjdF9pZCI6IjU3NTg4ZmU0LWI3YjgtNDdiOS1hNzhmLTdkOGUwNGRlMjU0NiJ9LCJydW4iOnsiaWQiOiIzNTU4OTllMi0xZDE3LTQ0YjMtYjMyYS04NDczZDQyYjE1NTEiLCJyZWZfdHlwZSI6ImJyYW5jaCIsInJlZiI6InJlZnMvaGVhZHMvbWFzdGVyIiwibmFtZSI6InJ1bjAxIiwiY291bnRlciI6MSwicGhhc2UiOiJxdWV1ZWQiLCJyZXN1bHQiOiJ1bmtub3duIiwidGFza3MiOnsiODhiNDBlNGEtNDY5MC00MGFkLWJlZGMtZmE3ZWE2ZWNlNmY0Ijp7ImlkIjoiODhiNDBlNGEtNDY5MC00MGFkLWJlZGMtZmE3ZWE2ZWNlNmY0IiwibmFtZSI6InRhc2swMSIsInN0YXR1cyI6Im5vdHN0YXJ0ZWQiLCJzZXR1cF9zdGVwIjp7InBoYXNlIjoibm90c3RhcnRlZCJ9LCJzdGVwcyI6W3sicGhhc2UiOiJub3RzdGFydGVkIn1dfX0sImVucXVldWVfdGltZSI6IjIwMjMtMDctMDNUMTE6MTA6MjYuMzA2Njk1OTk5KzAyOjAwIn0sIlZlcnNpb24iOjF9fQ=="}
{"exportMeta":{"kind":"RunWebhook"},"id":"ed2599f1-3cfc-4d09-afaf-934e2a4e1515","creationTime":"2023-04-03T12:07:16.839229324Z","updateTime":"2023-04-03T12:07:16.839229324Z","payload":"eyJwcm9qZWN0X2luZm8iOnsicHJvamVjdF9pZCI6IjU3NTg4ZmU0LWI3YjgtNDdiOS1hNzhmLTdkOGUwNGRlMjU0NiJ9LCJydW4iOnsiaWQiOiIzNTU4OTllMi0xZDE3LTQ0YjMtYjMyYS04NDczZDQyYjE1NTEiLCJyZWZfdHlwZSI6ImJyYW5jaCIsInJlZiI6InJlZnMvaGVhZHMvbWFzdGVyIiwibmFtZSI6InJ1bjAxIiwiY291bnRlciI6MSwicGhhc2UiOiJxdWV1ZWQiLCJyZXN1bHQiOiJ1bmtub3duIiwidGFza3MiOnsiODhiNDBlNGEtNDY5MC00MGFkLWJlZGMtZmE3ZWE2ZWNlNmY0Ijp7ImlkIjoiODhiNDBlNGEtNDY5MC00MGFkLWJlZGMtZmE3ZWE2ZWNlNmY0IiwibmFtZSI6InRhc2swMSIsInN0YXR1cyI6Im5vdHN0YXJ0ZWQiLCJzZXR1cF9zdGVwIjp7InBoYXNlIjoibm90c3RhcnRlZCJ9LCJzdGVwcyI6W3sicGhhc2UiOiJub3RzdGFydGVkIn1dfX0sImVucXVldWVfdGltZSI6IjIwMjMtMDctMDNUMTE6MTA6MjYuMzA2Njk1OTk5KzAyOjAwIn0sIlZlcnNpb24iOjF9fQ=="}
{"exportMeta":{"kind":"RunWebhook"},"id":"0f324898-442d-477c-93df-eb2229b3f922","creationTime":"2023-04-03T12:07:11.837436311Z","updateTime":"2023-04-03T12:07:11.837436311Z","payload":"eyJwcm9qZWN0X2luZm8iOnsicHJvamVjdF9pZCI6IjU3NTg4ZmU0LWI3YjgtNDdiOS1hNzhmLTdkOGUwNGRlMjU0NiJ9LCJydW4iOnsiaWQiOiIzNTU4OTllMi0xZDE3LTQ0YjMtYjMyYS04NDczZDQyYjE1NTEiLCJyZWZfdHlwZSI6ImJyYW5jaCIsInJlZiI6InJlZnMvaGVhZHMvbWFzdGVyIiwibmFtZSI6InJ1bjAxIiwiY291bnRlciI6MSwicGhhc2UiOiJxdWV1ZWQiLCJyZXN1bHQiOiJ1bmtub3duIiwidGFza3MiOnsiODhiNDBlNGEtNDY5MC00MGFkLWJlZGMtZmE3ZWE2ZWNlNmY0Ijp7ImlkIjoiODhiNDBlNGEtNDY5MC00MGFkLWJlZGMtZmE3ZWE2ZWNlNmY0IiwibmFtZSI6InRhc2swMSIsInN0YXR1cyI6Im5vdHN0YXJ0ZWQiLCJzZXR1cF9zdGVwIjp7InBoYXNlIjoibm90c3RhcnRlZCJ9LCJzdGVwcyI6W3sicGhhc2UiOiJub3RzdGFydGVkIn1dfX0sImVucXVldWVfdGltZSI6IjIwMjMtMDctMDNUMTE6MTA6MjYuMzA2Njk1OTk5KzAyOjAwIn0sIlZlcnNpb24iOjF9"}
Copy link
Member

Choose a reason for hiding this comment

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

To be completely correct in the base64 json "Version" should be lowercase "version" as defined in the json struct tag.

var runWebookDeliveries []*types.RunWebhookDelivery
err := h.d.Do(ctx, func(tx *sql.Tx) error {
var err error
runWebookDeliveries, err = h.d.GetRunWebhookDeliveriesAfterSequence(tx, req.StartSequence, req.ProjectID, req.DeliveryStatusFilter, limit, req.SortDirection)
Copy link
Member

Choose a reason for hiding this comment

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

GetProjectRun....

@@ -83,12 +83,30 @@ func mustSingleRow[T any](s []*T) (*T, error) {
return s[0], nil
}

func (d *DB) GetRunWebhookDeliveriesAfterSequence(tx *sql.Tx, afterSequence uint64, deliveryStatus types.DeliveryStatus, limit int) ([]*types.RunWebhookDelivery, error) {
func (d *DB) GetRunWebhookDeliveriesAfterSequence(tx *sql.Tx, afterSequence uint64, projectID string, deliveryStatusFilter []types.DeliveryStatus, limit int, sortDirection types.SortDirection) ([]*types.RunWebhookDelivery, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Use a new function to filter by project and use the old one for the global run webhooks (you could also remove deliveryStatus arg if not used.)

q.Where(q.E("delivery_status", deliveryStatus))

if projectID != "" {
q.Join("runwebhook", "runwebhook.id=runwebhookdelivery.run_webhook_id")
Copy link
Member

Choose a reason for hiding this comment

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

spaces between "="

@@ -90,7 +92,7 @@ func TestRunWebhookDelivery(t *testing.T) {

runWebhooks := make([]*types.RunWebhook, MaxRunWebhookDeliveriesQueryLimit+10)
for i := 0; i < len(runWebhooks); i++ {
runWebhooks[i] = createRunWebhook(t, ctx, ns)
runWebhooks[i] = createRunWebhook(t, ctx, ns, "projectidtest")
Copy link
Member

Choose a reason for hiding this comment

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

Use a const and call it "project01"

}

for i := 0; i < len(runWebhooks); i++ {
runWebhooks[i] = createRunWebhook(t, ctx, ns, "projectidtest02")
Copy link
Member

Choose a reason for hiding this comment

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

use a const

t.Fatalf("unexpected 10 run webhook deliveries, got %d", len(aresp.RunWebhookDeliveries))
}

// test get run webhook deliveries with deliverystatusfilter delivered
Copy link
Member

Choose a reason for hiding this comment

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

// test get run webhook deliveries with deliverystatusfilter = delivered

t.Fatalf("unexpected 20 run webhook deliveries, got %d", len(aresp.RunWebhookDeliveries))
}

// test get run webhook deliveries with cursor
Copy link
Member

Choose a reason for hiding this comment

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

There isn't any cursor concept in the notification service.

func TestGetProjectRunWebhookDeliveries(t *testing.T) {
t.Parallel()

t.Run("test get run webhook deliveries", func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Why don't you use the same logic (multiple t.Run) as the configstore tests?

services/notification/client/client.go Show resolved Hide resolved
services/notification/types/types.go Show resolved Hide resolved
@@ -161,7 +165,7 @@ func TestRunWebhookDelivery(t *testing.T) {

time.Sleep(1 * time.Second)

runWebhook := createRunWebhook(t, ctx, ns)
runWebhook := createRunWebhook(t, ctx, ns, "projectidtest")
Copy link
Member

Choose a reason for hiding this comment

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

const?

}

// test wrong deliverystatus
deliverystatus = []string{"notDelivered", "delivered", "deliveryError", "baddeliverystatus"}
Copy link
Member

Choose a reason for hiding this comment

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

should directly use the types

@alessandro-sorint alessandro-sorint force-pushed the api-runwebhookdeliveries branch 2 times, most recently from aee1ea3 to 35ded95 Compare November 14, 2023 15:08
tests/setup_test.go Outdated Show resolved Hide resolved
@alessandro-sorint alessandro-sorint force-pushed the api-runwebhookdeliveries branch 2 times, most recently from 49118b5 to aae8651 Compare November 15, 2023 15:57
Add web server to notification service.
fix payload fields since the json value has an excess curly bracket and Version must be lovercase.
add ProjectID to RunWebhook type
add an api to get run webhook deliveries by project id.
add an api to get run webhook deliveries by project ref.
@sgotti sgotti merged commit bfc34f0 into agola-io:master Nov 16, 2023
1 check passed
@alessandro-sorint alessandro-sorint deleted the api-runwebhookdeliveries branch November 16, 2023 14:39
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.

None yet

2 participants