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

feat: Add workspaceId, mode, name to webhook #9820

Merged
merged 11 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions e2e_tests/tests/cluster/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def test_slack_webhook() -> None:
url=f"http://localhost:{port}",
webhookType=bindings.v1WebhookType.SLACK,
triggers=[webhook_trigger],
mode=bindings.v1WebhookMode.WORKSPACE,
name="",
workspaceId=None,
)

result = bindings.post_PostWebhook(sess, body=webhook_request)
Expand Down Expand Up @@ -103,6 +106,9 @@ def test_log_pattern_send_webhook(should_match: bool) -> None:
url=f"http://localhost:{port}{slack_path}",
webhookType=bindings.v1WebhookType.SLACK,
triggers=[webhook_trigger],
mode=bindings.v1WebhookMode.WORKSPACE,
name="",
workspaceId=None,
),
)

Expand All @@ -113,6 +119,9 @@ def test_log_pattern_send_webhook(should_match: bool) -> None:
url=f"http://localhost:{port}{default_path}",
webhookType=bindings.v1WebhookType.DEFAULT,
triggers=[webhook_trigger],
mode=bindings.v1WebhookMode.WORKSPACE,
name="",
workspaceId=None,
),
)

Expand Down
27 changes: 27 additions & 0 deletions harness/determined/common/api/bindings.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions master/internal/webhooks/postgres_webhook_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,19 @@ func TestWebhooks(t *testing.T) {
})

t.Run("webhook creation should work", func(t *testing.T) {
workspaceID := int32(1)
testWebhookOne.Triggers = testTriggersOne
testWebhookOne.Mode = WebhookModeSpecific
testWebhookOne.Name = "test-name"
testWebhookOne.WorkspaceID = &workspaceID
err := AddWebhook(ctx, &testWebhookOne)
require.NoError(t, err, "failed to create webhook")
webhooks, err := GetWebhooks(ctx)
require.NoError(t, err, "unable to get webhooks")
webhookOneResponse := getWebhookByID(webhooks, testWebhookOne.ID)
require.Equal(t, "test-name", webhookOneResponse.Name)
require.Equal(t, workspaceID, *webhookOneResponse.WorkspaceID)
require.Equal(t, WebhookModeSpecific, testWebhookOne.Mode)
})

t.Run("webhook creation with multiple triggers should work", func(t *testing.T) {
Expand Down Expand Up @@ -124,6 +134,7 @@ func TestWebhookScanLogs(t *testing.T) {
Triggers: Triggers{
{TriggerType: TriggerTypeTaskLog, Condition: map[string]any{"regex": r0}},
},
Mode: WebhookModeWorkspace,
gt2345 marked this conversation as resolved.
Show resolved Hide resolved
}
w1 := &Webhook{
WebhookType: WebhookTypeDefault,
Expand All @@ -132,13 +143,15 @@ func TestWebhookScanLogs(t *testing.T) {
{TriggerType: TriggerTypeTaskLog, Condition: map[string]any{"regex": r0}},
{TriggerType: TriggerTypeTaskLog, Condition: map[string]any{"regex": r1}},
},
Mode: WebhookModeWorkspace,
}
w2 := &Webhook{
WebhookType: WebhookTypeDefault,
URL: uuid.New().String(),
Triggers: Triggers{
{TriggerType: TriggerTypeTaskLog, Condition: map[string]any{"regex": r2}},
},
Mode: WebhookModeWorkspace,
}

require.NoError(t, manager.addWebhook(ctx, w0))
Expand Down Expand Up @@ -388,26 +401,31 @@ var (
ID: 1000,
URL: "http://testwebhook.com",
WebhookType: WebhookTypeSlack,
Mode: WebhookModeWorkspace,
}
testWebhookTwo = Webhook{
ID: 2000,
URL: "http://testwebhooktwo.com",
WebhookType: WebhookTypeDefault,
Mode: WebhookModeWorkspace,
}
testWebhookThree = Webhook{
ID: 3000,
URL: "http://testwebhookthree.com",
WebhookType: WebhookTypeSlack,
Mode: WebhookModeWorkspace,
}
testWebhookFour = Webhook{
ID: 6000,
URL: "http://twebhook.com",
WebhookType: WebhookTypeSlack,
Mode: WebhookModeWorkspace,
}
testWebhookFive = Webhook{
ID: 7000,
URL: "http://twebhooktwo.com",
WebhookType: WebhookTypeDefault,
Mode: WebhookModeWorkspace,
}
testWebhookFourTrigger = Trigger{
ID: 6001,
Expand Down Expand Up @@ -484,6 +502,7 @@ func mockWebhook() *Webhook {
return &Webhook{
URL: uuid.New().String(),
WebhookType: WebhookTypeDefault,
Mode: WebhookModeWorkspace,
}
}

Expand Down Expand Up @@ -511,6 +530,7 @@ func TestDequeueEvents(t *testing.T) {
},
},
WebhookType: WebhookTypeDefault,
Mode: WebhookModeWorkspace,
}))

t.Run("dequeueing and consuming a event should work", func(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions master/internal/webhooks/shipper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func TestShipper(t *testing.T) {
},
},
WebhookType: WebhookTypeDefault,
Mode: WebhookModeWorkspace,
}))
// And one that just fires once.
require.NoError(t, AddWebhook(ctx, &Webhook{
Expand All @@ -118,6 +119,7 @@ func TestShipper(t *testing.T) {
},
},
WebhookType: WebhookTypeDefault,
Mode: WebhookModeWorkspace,
}))

t.Log("build shipper")
Expand Down
48 changes: 48 additions & 0 deletions master/internal/webhooks/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,43 @@ type Webhook struct {
ID WebhookID `bun:"id,pk,autoincrement"`
WebhookType WebhookType `bun:"webhook_type,notnull"`
URL string `bun:"url,notnull"`
Mode WebhookMode `bun:"mode,notnull"`
WorkspaceID *int32 `bun:"workspace_id"`
Name string `bun:"name,notnull"`

Triggers Triggers `bun:"rel:has-many,join:id=webhook_id"`
}

// WebhookFromProto returns a model Webhook from a proto definition.
func WebhookFromProto(w *webhookv1.Webhook) Webhook {
var workspaceID *int32
if w.WorkspaceId != 0 {
workspaceID = &(w.WorkspaceId)
}
return Webhook{
URL: w.Url,
Triggers: TriggersFromProto(w.Triggers),
WebhookType: WebhookTypeFromProto(w.WebhookType),
Name: w.Name,
WorkspaceID: workspaceID,
Mode: WebhookModeFromProto(w.Mode),
}
}

// Proto converts a webhook to its protobuf representation.
func (w *Webhook) Proto() *webhookv1.Webhook {
var workspaceID int32
if w.WorkspaceID != nil {
workspaceID = *(w.WorkspaceID)
}
return &webhookv1.Webhook{
Id: int32(w.ID),
Url: w.URL,
Triggers: w.Triggers.Proto(),
WebhookType: w.WebhookType.Proto(),
Name: w.Name,
Mode: w.Mode.Proto(),
WorkspaceId: workspaceID,
}
}

Expand All @@ -61,6 +78,9 @@ type WebhookID int
// WebhookType is type for the WebhookType enum.
type WebhookType string

// WebhookMode is mode for the WebhookMode enum.
type WebhookMode string

// Triggers is a slice of Trigger objects—primarily useful for its methods.
type Triggers []*Trigger

Expand Down Expand Up @@ -145,6 +165,24 @@ const (
WebhookTypeSlack WebhookType = "SLACK"
)

const (
// WebhookModeWorkspace represents the webhook will be triggered by all experiment in the workspace.
WebhookModeWorkspace WebhookMode = "WORKSPACE"
// WebhookModeSpecific represents the webhook will only be triggered by experiment with
// matching configuration in the same workspace as the web hook.
WebhookModeSpecific WebhookMode = "SPECIFIC"
)

// WebhookModeFromProto returns a WebhookMode from a proto.
func WebhookModeFromProto(w webhookv1.WebhookMode) WebhookMode {
switch w {
case webhookv1.WebhookMode_WEBHOOK_MODE_SPECIFIC:
return WebhookModeSpecific
default:
return WebhookModeWorkspace
}
}

// WebhookTypeFromProto returns a WebhookType from a proto.
func WebhookTypeFromProto(w webhookv1.WebhookType) WebhookType {
switch w {
Expand Down Expand Up @@ -185,6 +223,16 @@ func (w WebhookType) Proto() webhookv1.WebhookType {
}
}

// Proto returns a proto from a WebhookMode.
func (w WebhookMode) Proto() webhookv1.WebhookMode {
switch w {
case WebhookModeSpecific:
return webhookv1.WebhookMode_WEBHOOK_MODE_SPECIFIC
default:
return webhookv1.WebhookMode_WEBHOOK_MODE_WORKSPACE
}
}

// Proto returns a proto from a TriggerType.
func (t TriggerType) Proto() webhookv1.TriggerType {
switch t {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TYPE public.webhook_mode AS ENUM (
'WORKSPACE',
'SPECIFIC'
);

ALTER table public.webhooks
ADD COLUMN workspace_id INT REFERENCES workspaces(id) DEFAULT NULL,
ADD COLUMN name text NOT NULL DEFAULT md5(random()::text),
ADD COLUMN mode public.webhook_mode NOT NULL DEFAULT 'WORKSPACE';

CREATE UNIQUE INDEX name_workspace_key on public.webhooks (name, workspace_id);
Loading
Loading