-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add task event consumer #1510
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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,23 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package task | ||
|
|
||
| // TaskUpdateUserAccessV2Data is the Task v2 update event payload under the | ||
| // standard Lark V2 event envelope. | ||
| type TaskUpdateUserAccessV2Data struct { | ||
| EventTypes []string `json:"event_types,omitempty" desc:"Task commit types included in this event" enum:"task_create,task_deleted,task_summary_update,task_desc_update,task_assignees_update,task_followers_update,task_reminders_update,task_start_due_update,task_completed_update"` | ||
| TaskGUID string `json:"task_guid,omitempty" desc:"Task GUID that changed" kind:"task_guid"` | ||
| } | ||
|
|
||
| var taskUpdateUserAccessCommitTypes = []string{ | ||
| "task_create", | ||
| "task_deleted", | ||
| "task_summary_update", | ||
| "task_desc_update", | ||
| "task_assignees_update", | ||
| "task_followers_update", | ||
| "task_reminders_update", | ||
| "task_start_due_update", | ||
| "task_completed_update", | ||
| } |
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,32 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package task | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/larksuite/cli/errs" | ||
| "github.com/larksuite/cli/internal/event" | ||
| ) | ||
|
|
||
| const taskSubscriptionPath = "/open-apis/task/v2/task_v2/task_subscription?user_id_type=open_id" | ||
|
|
||
| func taskSubscriptionPreConsume(ctx context.Context, rt event.APIClient, _ map[string]string) (func() error, error) { | ||
| if rt == nil { | ||
| return nil, errs.NewInternalError(errs.SubtypeUnknown, | ||
| "runtime API client is required for pre-consume subscription") | ||
| } | ||
|
|
||
| if _, err := rt.CallAPI(ctx, "POST", taskSubscriptionPath, nil); err != nil { | ||
| if _, ok := errs.ProblemOf(err); ok { | ||
| return nil, err | ||
| } | ||
| return nil, errs.NewNetworkError( | ||
| errs.SubtypeNetworkTransport, | ||
| "failed to subscribe task event", | ||
| ).WithCause(err) | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
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,119 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package task | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/larksuite/cli/errs" | ||
| ) | ||
|
|
||
| type stubAPIClient struct { | ||
| err error | ||
|
|
||
| method string | ||
| path string | ||
| body interface{} | ||
| calls int | ||
| } | ||
|
|
||
| func (s *stubAPIClient) CallAPI(_ context.Context, method, path string, body interface{}) (json.RawMessage, error) { | ||
| s.method = method | ||
| s.path = path | ||
| s.body = body | ||
| s.calls++ | ||
| if s.err != nil { | ||
| return nil, s.err | ||
| } | ||
| return json.RawMessage(`{"code":0,"msg":"success","data":{}}`), nil | ||
| } | ||
|
|
||
| func TestTaskSubscriptionPreConsumeCallsSubscribeAPI(t *testing.T) { | ||
| rt := &stubAPIClient{} | ||
| cleanup, err := taskSubscriptionPreConsume(context.Background(), rt, nil) | ||
| if err != nil { | ||
| t.Fatalf("taskSubscriptionPreConsume error = %v", err) | ||
| } | ||
| if cleanup != nil { | ||
| t.Fatal("cleanup = non-nil, want nil because task subscription has no unsubscribe API") | ||
| } | ||
| if rt.calls != 1 { | ||
| t.Fatalf("calls = %d, want 1", rt.calls) | ||
| } | ||
| if rt.method != "POST" { | ||
| t.Errorf("method = %q, want POST", rt.method) | ||
| } | ||
| if rt.path != taskSubscriptionPath { | ||
| t.Errorf("path = %q, want %q", rt.path, taskSubscriptionPath) | ||
| } | ||
| if rt.body != nil { | ||
| t.Errorf("body = %#v, want nil", rt.body) | ||
| } | ||
| } | ||
|
|
||
| func TestTaskSubscriptionPreConsumeRequiresRuntime(t *testing.T) { | ||
| _, err := taskSubscriptionPreConsume(context.Background(), nil, nil) | ||
| if err == nil { | ||
| t.Fatal("expected error") | ||
| } | ||
| p, ok := errs.ProblemOf(err) | ||
| if !ok { | ||
| t.Fatalf("expected typed error, got %T: %v", err, err) | ||
| } | ||
| if p.Category != errs.CategoryInternal { | ||
| t.Errorf("category = %s, want %s", p.Category, errs.CategoryInternal) | ||
| } | ||
| if p.Subtype != errs.SubtypeUnknown { | ||
| t.Errorf("subtype = %s, want %s", p.Subtype, errs.SubtypeUnknown) | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| func TestTaskSubscriptionPreConsumePassesThroughAPIError(t *testing.T) { | ||
| wantErr := errs.NewValidationError(errs.SubtypeFailedPrecondition, "subscription already exists") | ||
| rt := &stubAPIClient{err: wantErr} | ||
|
|
||
| _, err := taskSubscriptionPreConsume(context.Background(), rt, nil) | ||
| if err != wantErr { | ||
| t.Fatalf("err identity changed: got %T %v, want original %T %v", err, err, wantErr, wantErr) | ||
| } | ||
| if !errors.Is(err, wantErr) { | ||
| t.Fatalf("err = %v, want %v", err, wantErr) | ||
| } | ||
| p, ok := errs.ProblemOf(err) | ||
| if !ok { | ||
| t.Fatalf("expected typed error, got %T: %v", err, err) | ||
| } | ||
| if p.Category != errs.CategoryValidation { | ||
| t.Errorf("category = %s, want %s", p.Category, errs.CategoryValidation) | ||
| } | ||
| if p.Subtype != errs.SubtypeFailedPrecondition { | ||
| t.Errorf("subtype = %s, want %s", p.Subtype, errs.SubtypeFailedPrecondition) | ||
| } | ||
| } | ||
|
|
||
| func TestTaskSubscriptionPreConsumeWrapsUntypedAPIError(t *testing.T) { | ||
| cause := errors.New("connection reset") | ||
| rt := &stubAPIClient{err: cause} | ||
|
|
||
| _, err := taskSubscriptionPreConsume(context.Background(), rt, nil) | ||
| if err == nil { | ||
| t.Fatal("expected error") | ||
| } | ||
| if !errors.Is(err, cause) { | ||
| t.Fatalf("err = %v, want cause %v", err, cause) | ||
| } | ||
| p, ok := errs.ProblemOf(err) | ||
| if !ok { | ||
| t.Fatalf("expected typed error, got %T: %v", err, err) | ||
| } | ||
| if p.Category != errs.CategoryNetwork { | ||
| t.Errorf("category = %s, want %s", p.Category, errs.CategoryNetwork) | ||
| } | ||
| if p.Subtype != errs.SubtypeNetworkTransport { | ||
| t.Errorf("subtype = %s, want %s", p.Subtype, errs.SubtypeNetworkTransport) | ||
| } | ||
| } | ||
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,33 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| // Package task registers Task-domain EventKeys. | ||
| package task | ||
|
|
||
| import ( | ||
| "reflect" | ||
|
|
||
| "github.com/larksuite/cli/internal/event" | ||
| ) | ||
|
|
||
| const eventTypeTaskUpdateUserAccessV2 = "task.task.update_user_access_v2" | ||
|
|
||
| // Keys returns all Task-domain EventKey definitions. | ||
| func Keys() []event.KeyDefinition { | ||
| return []event.KeyDefinition{ | ||
| { | ||
| Key: eventTypeTaskUpdateUserAccessV2, | ||
| DisplayName: "Task updated", | ||
| Description: "Triggered when tasks visible to the current user or app are created, deleted, or updated", | ||
| EventType: eventTypeTaskUpdateUserAccessV2, | ||
| Schema: event.SchemaDef{ | ||
| Native: &event.SchemaSpec{Type: reflect.TypeOf(TaskUpdateUserAccessV2Data{})}, | ||
| }, | ||
| PreConsume: taskSubscriptionPreConsume, | ||
| Scopes: []string{"task:task:read"}, | ||
| AuthTypes: []string{"user", "bot"}, | ||
| RequiredConsoleEvents: []string{eventTypeTaskUpdateUserAccessV2}, | ||
| SingleConsumer: true, | ||
| }, | ||
| } | ||
| } |
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,95 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package task | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/larksuite/cli/internal/event" | ||
| "github.com/larksuite/cli/internal/event/schemas" | ||
| ) | ||
|
|
||
| func TestKeysTaskUpdateUserAccessMetadata(t *testing.T) { | ||
| keys := Keys() | ||
| if len(keys) != 1 { | ||
| t.Fatalf("len(Keys()) = %d, want 1", len(keys)) | ||
| } | ||
|
|
||
| def := keys[0] | ||
| if def.Key != eventTypeTaskUpdateUserAccessV2 { | ||
| t.Errorf("Key = %q, want %q", def.Key, eventTypeTaskUpdateUserAccessV2) | ||
| } | ||
| if def.EventType != eventTypeTaskUpdateUserAccessV2 { | ||
| t.Errorf("EventType = %q, want %q", def.EventType, eventTypeTaskUpdateUserAccessV2) | ||
| } | ||
| if def.Schema.Native == nil { | ||
| t.Fatal("Schema.Native is nil") | ||
| } | ||
| if def.Schema.Native.Type != reflect.TypeOf(TaskUpdateUserAccessV2Data{}) { | ||
| t.Errorf("native type = %v, want TaskUpdateUserAccessV2Data", def.Schema.Native.Type) | ||
| } | ||
| if def.Process != nil { | ||
| t.Fatal("Native Task EventKey must not set Process") | ||
| } | ||
| if def.PreConsume == nil { | ||
| t.Fatal("PreConsume is nil") | ||
| } | ||
| if !def.SingleConsumer { | ||
| t.Fatal("SingleConsumer = false, want true") | ||
| } | ||
| if !reflect.DeepEqual(def.Scopes, []string{"task:task:read"}) { | ||
| t.Errorf("Scopes = %#v", def.Scopes) | ||
| } | ||
| if !reflect.DeepEqual(def.AuthTypes, []string{"user", "bot"}) { | ||
| t.Errorf("AuthTypes = %#v", def.AuthTypes) | ||
| } | ||
| if !reflect.DeepEqual(def.RequiredConsoleEvents, []string{eventTypeTaskUpdateUserAccessV2}) { | ||
| t.Errorf("RequiredConsoleEvents = %#v", def.RequiredConsoleEvents) | ||
| } | ||
| } | ||
|
|
||
| func TestTaskUpdateUserAccessSchemaAnnotations(t *testing.T) { | ||
| raw := schemas.WrapV2Envelope(schemas.FromType(reflect.TypeOf(TaskUpdateUserAccessV2Data{}))) | ||
| var schema map[string]interface{} | ||
| if err := json.Unmarshal(raw, &schema); err != nil { | ||
| t.Fatalf("unmarshal schema: %v", err) | ||
| } | ||
|
|
||
| eventProps := schema["properties"].(map[string]interface{})["event"].(map[string]interface{})["properties"].(map[string]interface{}) | ||
| taskGUID := eventProps["task_guid"].(map[string]interface{}) | ||
| if got := taskGUID["format"]; got != "task_guid" { | ||
| t.Errorf("task_guid format = %v, want task_guid", got) | ||
| } | ||
|
|
||
| eventTypes := eventProps["event_types"].(map[string]interface{}) | ||
| items := eventTypes["items"].(map[string]interface{}) | ||
| rawEnum, ok := items["enum"].([]interface{}) | ||
| if !ok { | ||
| t.Fatalf("event_types item enum missing: %#v", items["enum"]) | ||
| } | ||
| got := make(map[string]bool, len(rawEnum)) | ||
| for _, v := range rawEnum { | ||
| got[v.(string)] = true | ||
| } | ||
| for _, want := range taskUpdateUserAccessCommitTypes { | ||
| if !got[want] { | ||
| t.Errorf("event_types enum missing %q; enum=%v", want, rawEnum) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestTaskUpdateUserAccessRegistersCleanly(t *testing.T) { | ||
| const key = eventTypeTaskUpdateUserAccessV2 | ||
| event.UnregisterKeyForTest(key) | ||
| t.Cleanup(func() { event.UnregisterKeyForTest(key) }) | ||
|
|
||
| for _, def := range Keys() { | ||
| event.RegisterKey(def) | ||
| } | ||
| if _, ok := event.Lookup(key); !ok { | ||
| t.Fatalf("event.Lookup(%q) not registered", key) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.