-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(approval): support approval event consumption #1924
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package approval | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/larksuite/cli/errs" | ||
| "github.com/larksuite/cli/internal/event" | ||
| ) | ||
|
|
||
| type approvalEventType string | ||
| type approvalSubscriptionPath string | ||
|
|
||
| type approvalSubscriptionConfig struct { | ||
| eventType approvalEventType | ||
| subscribePath approvalSubscriptionPath | ||
| } | ||
|
|
||
| func approvalSubscriptionPreConsume(cfg approvalSubscriptionConfig) func(context.Context, event.APIClient, map[string]string) (func() error, error) { | ||
| return func(ctx context.Context, rt event.APIClient, params map[string]string) (func() error, error) { | ||
| if rt == nil { | ||
| return nil, errs.NewInternalError(errs.SubtypeUnknown, | ||
| "runtime API client is required for pre-consume subscription") | ||
| } | ||
|
|
||
| eventType := string(cfg.eventType) | ||
| subscribePath := string(cfg.subscribePath) | ||
| subscriptionTypes, err := approvalSubscriptionTypes(eventType, params) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| registered := make([]string, 0, len(subscriptionTypes)) | ||
| for _, subscriptionType := range subscriptionTypes { | ||
| body := map[string]string{"subscription_type": subscriptionType} | ||
| if _, err := rt.CallAPI(ctx, "POST", subscribePath, body); err != nil { | ||
| return nil, approvalSubscriptionRegistrationError(eventType, registered, subscriptionType, err) | ||
| } | ||
| registered = append(registered, subscriptionType) | ||
| } | ||
|
|
||
| // Approval subscriptions are durable user-auth relations. Consuming events | ||
| // should not cancel that relation when this local process exits. | ||
| return nil, nil | ||
| } | ||
| } | ||
|
|
||
| func approvalSubscriptionTypes(eventType string, params map[string]string) ([]string, error) { | ||
| raw := strings.TrimSpace(params["subscription_type"]) | ||
| if raw == "" { | ||
| return append([]string(nil), approvalAllSubscriptionTypes...), nil | ||
| } | ||
|
|
||
| values, err := parseApprovalSubscriptionTypeValues(raw) | ||
| if err != nil { | ||
| return nil, invalidApprovalSubscriptionTypeError(eventType, raw) | ||
| } | ||
|
|
||
| selected := make(map[string]bool, len(values)) | ||
| for _, value := range values { | ||
| value = strings.TrimSpace(value) | ||
| switch value { | ||
| case approvalSubscriptionTypeInvolved, approvalSubscriptionTypeManaged: | ||
| selected[value] = true | ||
| default: | ||
| return nil, invalidApprovalSubscriptionTypeError(eventType, value) | ||
| } | ||
| } | ||
|
|
||
| result := make([]string, 0, len(selected)) | ||
| for _, value := range approvalAllSubscriptionTypes { | ||
| if selected[value] { | ||
| result = append(result, value) | ||
| } | ||
| } | ||
| if len(result) == 0 { | ||
| return nil, invalidApprovalSubscriptionTypeError(eventType, raw) | ||
| } | ||
| return result, nil | ||
| } | ||
|
|
||
| func parseApprovalSubscriptionTypeValues(raw string) ([]string, error) { | ||
| if strings.HasPrefix(raw, "[") { | ||
| var values []string | ||
| if err := json.Unmarshal([]byte(raw), &values); err != nil { | ||
| return nil, err | ||
| } | ||
| return values, nil | ||
| } | ||
| return strings.Split(raw, ","), nil | ||
| } | ||
|
|
||
| func approvalSubscriptionRegistrationError(eventType string, registered []string, failed string, err error) error { | ||
| if err == nil { | ||
| return nil | ||
| } | ||
|
|
||
| msg := fmt.Sprintf( | ||
| "approval subscription pre-consume failed for EventKey %s: failed subscription_type %s", | ||
| eventType, | ||
| failed, | ||
| ) | ||
| hint := fmt.Sprintf( | ||
| "no approval subscription relation was registered for EventKey %s; fix the cause and retry", | ||
| eventType, | ||
| ) | ||
| if len(registered) > 0 { | ||
| msg = fmt.Sprintf( | ||
| "approval subscription pre-consume partially completed for EventKey %s: registered subscription_type(s) [%s], failed subscription_type %s", | ||
| eventType, | ||
| strings.Join(registered, ", "), | ||
| failed, | ||
| ) | ||
| hint = fmt.Sprintf( | ||
| "server-side approval subscription relation(s) already registered for EventKey %s: %s; after fixing the cause, retry with --param subscription_type=%s to register the failed relation", | ||
| eventType, | ||
| strings.Join(registered, ", "), | ||
| failed, | ||
| ) | ||
| } | ||
|
|
||
| if p, ok := errs.ProblemOf(err); ok { | ||
| if upstream := strings.TrimSpace(p.Message); upstream != "" { | ||
| p.Message = msg + ": " + upstream | ||
| } else { | ||
| p.Message = msg | ||
| } | ||
| if upstreamHint := strings.TrimSpace(p.Hint); upstreamHint != "" { | ||
| p.Hint = upstreamHint + "\n" + hint | ||
| } else { | ||
| p.Hint = hint | ||
| } | ||
| return err | ||
| } | ||
| return errs.NewInternalError(errs.SubtypeSDKError, "%s: %v", msg, err). | ||
| WithHint("%s", hint). | ||
| WithCause(err) | ||
| } | ||
|
|
||
| func invalidApprovalSubscriptionTypeError(eventType, value string) error { | ||
| return errs.NewValidationError(errs.SubtypeInvalidArgument, | ||
| "invalid subscription_type for EventKey %s: %q", eventType, value). | ||
| WithParam("--param"). | ||
| WithHint("omit subscription_type to register both approval subscription relations, or pass --param subscription_type=%s, --param subscription_type=%s, or --param subscription_type=%s,%s; run `lark-cli event schema %s` for details", | ||
| approvalSubscriptionTypeInvolved, | ||
| approvalSubscriptionTypeManaged, | ||
| approvalSubscriptionTypeInvolved, | ||
| approvalSubscriptionTypeManaged, | ||
| eventType) | ||
| } |
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.