-
Notifications
You must be signed in to change notification settings - Fork 583
feat: add drive create-folder shortcut and wiki node auto-grant #470
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
fangshuyu-768
merged 1 commit into
main
from
feat/drive-create-folder-wiki-node-auto-grant
Apr 16, 2026
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package drive | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/larksuite/cli/internal/output" | ||
| "github.com/larksuite/cli/internal/validate" | ||
| "github.com/larksuite/cli/shortcuts/common" | ||
| ) | ||
|
|
||
| type driveCreateFolderSpec struct { | ||
| Name string | ||
| FolderToken string | ||
| } | ||
|
|
||
| func newDriveCreateFolderSpec(runtime *common.RuntimeContext) driveCreateFolderSpec { | ||
| return driveCreateFolderSpec{ | ||
| Name: strings.TrimSpace(runtime.Str("name")), | ||
| FolderToken: strings.TrimSpace(runtime.Str("folder-token")), | ||
| } | ||
| } | ||
|
|
||
| func (s driveCreateFolderSpec) RequestBody() map[string]interface{} { | ||
| return map[string]interface{}{ | ||
| "name": s.Name, | ||
| "folder_token": s.FolderToken, | ||
| } | ||
| } | ||
|
|
||
| // DriveCreateFolder creates a new Drive folder under the specified parent | ||
| // folder, or under the caller's root folder when --folder-token is omitted. | ||
| var DriveCreateFolder = common.Shortcut{ | ||
| Service: "drive", | ||
| Command: "+create-folder", | ||
| Description: "Create a folder in Drive", | ||
| Risk: "write", | ||
| Scopes: []string{"space:folder:create"}, | ||
| AuthTypes: []string{"user", "bot"}, | ||
| Flags: []common.Flag{ | ||
| {Name: "name", Desc: "folder name", Required: true}, | ||
| {Name: "folder-token", Desc: "parent folder token (default: root folder)"}, | ||
| }, | ||
| Tips: []string{ | ||
| "Omit --folder-token to create the folder in the caller's root folder.", | ||
| }, | ||
| Validate: func(ctx context.Context, runtime *common.RuntimeContext) error { | ||
| return validateDriveCreateFolderSpec(newDriveCreateFolderSpec(runtime)) | ||
| }, | ||
| DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI { | ||
| spec := newDriveCreateFolderSpec(runtime) | ||
| dry := common.NewDryRunAPI(). | ||
| Desc("Create a folder in Drive"). | ||
| POST("/open-apis/drive/v1/files/create_folder"). | ||
| Desc("[1] Create folder"). | ||
| Body(spec.RequestBody()) | ||
| if runtime.IsBot() { | ||
| dry.Desc("After folder creation succeeds in bot mode, the CLI will also try to grant the current CLI user full_access (可管理权限) on the new folder.") | ||
| } | ||
| return dry | ||
| }, | ||
| Execute: func(ctx context.Context, runtime *common.RuntimeContext) error { | ||
| spec := newDriveCreateFolderSpec(runtime) | ||
|
|
||
| target := "root folder" | ||
| if spec.FolderToken != "" { | ||
| target = "folder " + common.MaskToken(spec.FolderToken) | ||
| } | ||
| fmt.Fprintf(runtime.IO().ErrOut, "Creating folder %q in %s...\n", spec.Name, target) | ||
|
|
||
| data, err := runtime.CallAPI( | ||
| "POST", | ||
| "/open-apis/drive/v1/files/create_folder", | ||
| nil, | ||
| spec.RequestBody(), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| folderToken := common.GetString(data, "token") | ||
| if folderToken == "" { | ||
| return output.Errorf(output.ExitAPI, "api_error", "drive create_folder succeeded but returned no folder token (data.token)") | ||
| } | ||
| out := map[string]interface{}{ | ||
| "created": true, | ||
| "name": spec.Name, | ||
| "folder_token": folderToken, | ||
| "parent_folder_token": spec.FolderToken, | ||
| } | ||
| if url := common.GetString(data, "url"); url != "" { | ||
| out["url"] = url | ||
| } | ||
| if grant := common.AutoGrantCurrentUserDrivePermission(runtime, folderToken, "folder"); grant != nil { | ||
| out["permission_grant"] = grant | ||
|
wittam-01 marked this conversation as resolved.
|
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| runtime.Out(out, nil) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| func validateDriveCreateFolderSpec(spec driveCreateFolderSpec) error { | ||
| if spec.Name == "" { | ||
| return output.ErrValidation("--name must not be empty") | ||
| } | ||
| if nameBytes := len([]byte(spec.Name)); nameBytes > 256 { | ||
| return output.ErrValidation("--name exceeds the maximum of 256 bytes (got %d)", nameBytes) | ||
| } | ||
| if spec.FolderToken != "" { | ||
| if err := validate.ResourceName(spec.FolderToken, "--folder-token"); err != nil { | ||
| return output.ErrValidation("%s", err) | ||
| } | ||
| } | ||
| return 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,266 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package drive | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/larksuite/cli/internal/cmdutil" | ||
| "github.com/larksuite/cli/internal/core" | ||
| "github.com/larksuite/cli/internal/httpmock" | ||
| "github.com/larksuite/cli/shortcuts/common" | ||
| ) | ||
|
|
||
| func TestValidateDriveCreateFolderSpecRejectsInvalidInputs(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| spec driveCreateFolderSpec | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "empty name", | ||
| spec: driveCreateFolderSpec{}, | ||
| wantErr: "--name must not be empty", | ||
| }, | ||
| { | ||
| name: "name too long", | ||
| spec: driveCreateFolderSpec{ | ||
| Name: strings.Repeat("a", 257), | ||
| }, | ||
| wantErr: "maximum of 256 bytes", | ||
| }, | ||
| { | ||
| name: "invalid folder token", | ||
| spec: driveCreateFolderSpec{ | ||
| Name: "Reports", | ||
| FolderToken: "../bad", | ||
| }, | ||
| wantErr: "--folder-token", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| err := validateDriveCreateFolderSpec(tt.spec) | ||
| if err == nil { | ||
| t.Fatal("expected validation error, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), tt.wantErr) { | ||
| t.Fatalf("error = %q, want substring %q", err.Error(), tt.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDriveCreateFolderDryRunIncludesCreateRequest(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cmd := &cobra.Command{Use: "drive +create-folder"} | ||
| cmd.Flags().String("name", "", "") | ||
| cmd.Flags().String("folder-token", "", "") | ||
| if err := cmd.Flags().Set("name", " Weekly Reports "); err != nil { | ||
| t.Fatalf("set --name: %v", err) | ||
| } | ||
| if err := cmd.Flags().Set("folder-token", " fld_parent "); err != nil { | ||
| t.Fatalf("set --folder-token: %v", err) | ||
| } | ||
|
|
||
| runtime := common.TestNewRuntimeContextWithIdentity(cmd, nil, core.AsBot) | ||
| dry := DriveCreateFolder.DryRun(context.Background(), runtime) | ||
| if dry == nil { | ||
| t.Fatal("DryRun returned nil") | ||
| } | ||
|
|
||
| data, err := json.Marshal(dry) | ||
| if err != nil { | ||
| t.Fatalf("marshal dry run: %v", err) | ||
| } | ||
|
|
||
| var got struct { | ||
| API []struct { | ||
| Method string `json:"method"` | ||
| URL string `json:"url"` | ||
| Body map[string]interface{} `json:"body"` | ||
| } `json:"api"` | ||
| } | ||
| if err := json.Unmarshal(data, &got); err != nil { | ||
| t.Fatalf("unmarshal dry run json: %v", err) | ||
| } | ||
| if len(got.API) != 1 { | ||
| t.Fatalf("expected 1 API call, got %d", len(got.API)) | ||
| } | ||
| if got.API[0].Method != "POST" || got.API[0].URL != "/open-apis/drive/v1/files/create_folder" { | ||
| t.Fatalf("unexpected dry-run API call: %#v", got.API[0]) | ||
| } | ||
| if got.API[0].Body["name"] != "Weekly Reports" { | ||
| t.Fatalf("name = %#v, want %q", got.API[0].Body["name"], "Weekly Reports") | ||
| } | ||
| if got.API[0].Body["folder_token"] != "fld_parent" { | ||
| t.Fatalf("folder_token = %#v, want %q", got.API[0].Body["folder_token"], "fld_parent") | ||
| } | ||
| } | ||
|
|
||
| func TestDriveCreateFolderBotAutoGrantSuccess(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| f, stdout, _, reg := cmdutil.TestFactory(t, drivePermissionGrantTestConfig(t, "ou_current_user")) | ||
|
|
||
| createStub := &httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/open-apis/drive/v1/files/create_folder", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "msg": "ok", | ||
| "data": map[string]interface{}{ | ||
| "token": "fld_created", | ||
| "url": "https://example.feishu.cn/drive/folder/fld_created", | ||
| }, | ||
| }, | ||
| } | ||
| reg.Register(createStub) | ||
|
|
||
| permStub := &httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/open-apis/drive/v1/permissions/fld_created/members", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "msg": "ok", | ||
| }, | ||
| } | ||
| reg.Register(permStub) | ||
|
|
||
| err := mountAndRunDrive(t, DriveCreateFolder, []string{ | ||
| "+create-folder", | ||
| "--name", " Weekly Reports ", | ||
| "--folder-token", " fld_parent ", | ||
| "--as", "bot", | ||
| }, f, stdout) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| body := decodeCapturedJSONBody(t, createStub) | ||
| if body["name"] != "Weekly Reports" { | ||
| t.Fatalf("name = %#v, want %q", body["name"], "Weekly Reports") | ||
| } | ||
| if body["folder_token"] != "fld_parent" { | ||
| t.Fatalf("folder_token = %#v, want %q", body["folder_token"], "fld_parent") | ||
| } | ||
|
|
||
| data := decodeDriveEnvelope(t, stdout) | ||
| if data["folder_token"] != "fld_created" { | ||
| t.Fatalf("folder_token = %#v, want %q", data["folder_token"], "fld_created") | ||
| } | ||
| if data["parent_folder_token"] != "fld_parent" { | ||
| t.Fatalf("parent_folder_token = %#v, want %q", data["parent_folder_token"], "fld_parent") | ||
| } | ||
| if data["name"] != "Weekly Reports" { | ||
| t.Fatalf("name = %#v, want %q", data["name"], "Weekly Reports") | ||
| } | ||
| if data["url"] != "https://example.feishu.cn/drive/folder/fld_created" { | ||
| t.Fatalf("url = %#v, want folder url", data["url"]) | ||
| } | ||
|
|
||
| grant, _ := data["permission_grant"].(map[string]interface{}) | ||
| if grant["status"] != common.PermissionGrantGranted { | ||
| t.Fatalf("permission_grant.status = %#v, want %q", grant["status"], common.PermissionGrantGranted) | ||
| } | ||
| if grant["user_open_id"] != "ou_current_user" { | ||
| t.Fatalf("permission_grant.user_open_id = %#v, want %q", grant["user_open_id"], "ou_current_user") | ||
| } | ||
| if grant["message"] != "Granted the current CLI user full_access (可管理权限) on the new folder." { | ||
| t.Fatalf("permission_grant.message = %#v", grant["message"]) | ||
| } | ||
|
|
||
| permBody := decodeCapturedJSONBody(t, permStub) | ||
| if permBody["member_type"] != "openid" || permBody["member_id"] != "ou_current_user" || permBody["perm"] != "full_access" || permBody["type"] != "user" { | ||
| t.Fatalf("unexpected permission request body: %#v", permBody) | ||
| } | ||
| } | ||
|
|
||
| func TestDriveCreateFolderUsesRootWhenParentIsOmitted(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| f, stdout, _, reg := cmdutil.TestFactory(t, drivePermissionGrantTestConfig(t, "ou_current_user")) | ||
|
|
||
| createStub := &httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/open-apis/drive/v1/files/create_folder", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "msg": "ok", | ||
| "data": map[string]interface{}{ | ||
| "token": "fld_root_child", | ||
| }, | ||
| }, | ||
| } | ||
| reg.Register(createStub) | ||
|
|
||
| err := mountAndRunDrive(t, DriveCreateFolder, []string{ | ||
| "+create-folder", | ||
| "--name", "Inbox", | ||
| "--as", "user", | ||
| }, f, stdout) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| body := decodeCapturedJSONBody(t, createStub) | ||
| if body["folder_token"] != "" { | ||
| t.Fatalf("folder_token = %#v, want empty string for root create", body["folder_token"]) | ||
| } | ||
|
|
||
| data := decodeDriveEnvelope(t, stdout) | ||
| if data["folder_token"] != "fld_root_child" { | ||
| t.Fatalf("folder_token = %#v, want %q", data["folder_token"], "fld_root_child") | ||
| } | ||
| if data["parent_folder_token"] != "" { | ||
| t.Fatalf("parent_folder_token = %#v, want empty string", data["parent_folder_token"]) | ||
| } | ||
| if _, ok := data["permission_grant"]; ok { | ||
| t.Fatalf("did not expect permission_grant in user mode output: %#v", data) | ||
| } | ||
| } | ||
|
|
||
| func TestDriveCreateFolderRejectsCreateResponseWithoutToken(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| f, stdout, _, reg := cmdutil.TestFactory(t, drivePermissionGrantTestConfig(t, "ou_current_user")) | ||
|
|
||
| reg.Register(&httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/open-apis/drive/v1/files/create_folder", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "msg": "ok", | ||
| "data": map[string]interface{}{ | ||
| "url": "https://example.feishu.cn/drive/folder/unknown", | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| err := mountAndRunDrive(t, DriveCreateFolder, []string{ | ||
| "+create-folder", | ||
| "--name", "Broken Folder", | ||
| "--as", "bot", | ||
| }, f, stdout) | ||
| if err == nil { | ||
| t.Fatal("expected error, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), "returned no folder token") { | ||
| t.Fatalf("err = %v, want missing folder token error", err) | ||
| } | ||
| if stdout.Len() != 0 { | ||
| t.Fatalf("stdout should be empty on error, got %s", stdout.String()) | ||
| } | ||
| } |
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.