Skip to content

feat(core): support custom brands via extraBrands in config.json #1149

Open
fengb3 wants to merge 4 commits into
larksuite:mainfrom
fengb3:feat/extra-brands
Open

feat(core): support custom brands via extraBrands in config.json #1149
fengb3 wants to merge 4 commits into
larksuite:mainfrom
fengb3:feat/extra-brands

Conversation

@fengb3
Copy link
Copy Markdown

@fengb3 fengb3 commented May 28, 2026

Summary

支持在 config.json 中通过 extraBrands 字段配置自定义brand(私有化部署),让 lark-cli 能够连接非
feishu/lark 的自建飞书平台。

Changes

  • internal/core/types.go: 重构 endpoint 解析为 registry 模式(brandRegistry + RegisterBrand +
    MergeEndpointOverrides),支持运行时注册自定义品牌;为 Endpoints 字段添加 json tag 修复反序列化问题
  • internal/core/config.go: LoadMultiAppConfig 加载配置时自动调用 RegisterBrand 注册 extraBrands
    中的自定义品牌
  • internal/core/types_test.go: 添加 RegisterBrand、MergeEndpointOverrides、ResolveEndpoints 的单元测试

Usage

~/.lark-cli/config.json 中添加 extraBrands 字段,设置自定义 endpoint base url。未指定的字段会自动继承 feishu 的默认值。

{

  "extraBrands": {
    "your-brand-name": {
      "open": "https://your-private-hosted-url",
      // "accounts": "",
      // "mcp": "",
      // "applink": "",
    }
  },
  "apps": [
    {
      "appId": "cli_xxx",
      "appSecret": "xxx",
      "brand": "your-brand-name"
    }
  ]
}

// brand 字段引用 extraBrands 中定义的 key,CLI 会自动使用该品牌对应的 endpoint 进行 API 调用。

Test Plan

  • Unit tests pass (internal/core/types_test.go)
  • lark-cli doctor 验证自定义 brand 的 endpoint 正确解析

Related Issues

Summary by CodeRabbit

  • New Features

    • Configure additional brand endpoints in app settings to add or override brand-specific service URLs for greater flexibility.
  • Tests

    • Added unit tests covering endpoint merging, brand registration, and override behavior to ensure reliable endpoint resolution.

Review Change Stack

fengb3 added 3 commits May 28, 2026 12:30
Add brandRegistry map and RegisterBrand/MergeEndpointOverrides to
types.go. LoadMultiAppConfig registers extraBrands entries on load.
ResolveEndpoints signature unchanged — all callers zero-modified.
Add brandRegistry map and RegisterBrand/MergeEndpointOverrides to
types.go. LoadMultiAppConfig registers extraBrands entries on load.
ResolveEndpoints signature unchanged — all callers zero-modified.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 28, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 28, 2026

📝 Walkthrough

Walkthrough

The PR refactors endpoint resolution from hardcoded switch logic to a registry-based system. Endpoints struct fields gain JSON tags, a brandRegistry map stores custom brand configurations, RegisterBrand and MergeEndpointOverrides helpers manage brand registration and partial overrides, ResolveEndpoints now performs registry lookups with Feishu fallback, and MultiAppConfig gains an ExtraBrands section that is applied during config load.

Changes

Custom Brand Endpoint Registry

Layer / File(s) Summary
Endpoints type contract and serialization
internal/core/types.go
Endpoints struct fields (Open, Accounts, MCP, AppLink) receive JSON struct tags to enable unmarshalling from config.
Brand registry, merge helpers, and tests
internal/core/types.go, internal/core/types_test.go
Package-level brandRegistry map is initialized with built-in feishu and lark defaults; RegisterBrand adds or updates custom brands by merging partial overrides on top of Feishu defaults; MergeEndpointOverrides performs non-empty field merge logic; tests validate nil override passthrough, partial field overrides, full overrides, and that built-in brands are not overwritten.
Endpoint resolution refactoring
internal/core/types.go
ResolveEndpoints switches from hardcoded switch branching to registry map lookup with Feishu defaults fallback for unregistered brands.
Config-driven brand registration
internal/core/config.go
MultiAppConfig struct adds ExtraBrands field (map[string]*Endpoints); LoadMultiAppConfig iterates over ExtraBrands entries and calls RegisterBrand to apply custom endpoint configurations during config unmarshalling.

Sequence Diagram

sequenceDiagram
  participant ConfigLoader as Config Loader
  participant RegisterFn as RegisterBrand
  participant Registry as brandRegistry
  participant Resolver as ResolveEndpoints
  ConfigLoader->>RegisterFn: RegisterBrand(name, overrides)
  RegisterFn->>Registry: Merge overrides onto Feishu defaults
  Resolver->>Registry: Lookup brand by name
  Registry-->>Resolver: Return registered endpoints or Feishu fallback
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Suggested labels

size/L

Suggested reviewers

  • sang-neo03

Poem

In burrowed code I hopped and found,
Switches trimmed and registry crowned,
Brands now bloom from config seed,
Defaults hold fast where gaps still need,
A rabbit cheers: endpoints unbound! 🐇✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat(core): support custom brands via extraBrands in config.json' clearly summarizes the main change: adding support for custom brands through a new config field.
Description check ✅ Passed The PR description includes all required template sections (Summary, Changes, Test Plan, Related Issues) with substantial detail about implementation and usage examples.
Linked Issues check ✅ Passed The PR successfully addresses issues #31 and #14 by implementing custom brand registration via extraBrands config field with proper endpoint resolution and fallback to feishu defaults.
Out of Scope Changes check ✅ Passed All changes (registry-based endpoint resolution, extraBrands loading, JSON tagging, comprehensive tests) are directly scoped to supporting custom brands as required by linked issues.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot added the size/M Single-domain feat or fix with limited business impact label May 28, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (1)
internal/core/types_test.go (1)

93-116: ⚡ Quick win

Isolate brandRegistry mutations in tests.

These tests mutate package-global registry entries (staging, proxy) and do not clean them up, which can introduce order-coupling with future tests.

Proposed fix
 func TestRegisterBrand(t *testing.T) {
+	t.Cleanup(func() { delete(brandRegistry, "staging") })
 	RegisterBrand("staging", Endpoints{Open: "https://open-staging.feishu.cn"})
 	ep := ResolveEndpoints("staging")
@@
 func TestRegisterBrand_Full(t *testing.T) {
+	t.Cleanup(func() { delete(brandRegistry, "proxy") })
 	RegisterBrand("proxy", Endpoints{
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/core/types_test.go` around lines 93 - 116, The tests
TestRegisterBrand and TestRegisterBrand_Full mutate the package-global
brandRegistry via RegisterBrand and don't restore it; modify each test to
capture the current brandRegistry (or relevant entries) before calling
RegisterBrand and defer restoring the original registry state at the end of the
test so registry mutations are isolated; use ResolveEndpoints to verify behavior
as before but ensure brandRegistry is reset (restoring removed or previous
Endpoints for keys like "staging" and "proxy") in a deferred cleanup to avoid
test order coupling.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/core/config.go`:
- Around line 203-205: The loop over multi.ExtraBrands may dereference nil
pointers (ep) and panic; update the loop that iterates name, ep in
multi.ExtraBrands to guard against nil ep before calling RegisterBrand — e.g.,
if ep is nil then skip (or log a warning) and continue, otherwise call
RegisterBrand(name, *ep); ensure you reference the existing loop and
RegisterBrand call so only non-nil endpoints get dereferenced.

In `@internal/core/types.go`:
- Around line 137-140: The current fallback always returns
brandRegistry[string(BrandFeishu)] for any unknown brand, which hides typos;
change the logic so brandRegistry[string(BrandFeishu)] is returned only when
brand is empty (string(brand) == ""); if the brand is non-empty and not found in
brandRegistry, do not silently return Feishu — return a nil/zero endpoint (or an
explicit error/marker) so callers can detect and handle an unknown brand; update
the code around brandRegistry, BrandFeishu and the brand variable accordingly.
- Around line 84-133: There are duplicate top-level declarations for
brandRegistry, RegisterBrand and MergeEndpointOverrides; remove the redundant
copies and keep one consolidated implementation (keep the MergeEndpointOverrides
logic in the single RegisterBrand/brandRegistry file) so the package compiles.
Also update ResolveEndpoints to validate lookups instead of silently falling
back to Feishu: when a requested brand (e.g., LarkBrand or any name) is not
found in brandRegistry return an error (or a boolean ok) alongside the Endpoints
so callers can handle unknown-brand cases rather than implicitly defaulting to
Feishu.

---

Nitpick comments:
In `@internal/core/types_test.go`:
- Around line 93-116: The tests TestRegisterBrand and TestRegisterBrand_Full
mutate the package-global brandRegistry via RegisterBrand and don't restore it;
modify each test to capture the current brandRegistry (or relevant entries)
before calling RegisterBrand and defer restoring the original registry state at
the end of the test so registry mutations are isolated; use ResolveEndpoints to
verify behavior as before but ensure brandRegistry is reset (restoring removed
or previous Endpoints for keys like "staging" and "proxy") in a deferred cleanup
to avoid test order coupling.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 086c041b-7695-468e-a481-12e0e65d950a

📥 Commits

Reviewing files that changed from the base of the PR and between b91f6a2 and 3f756b5.

📒 Files selected for processing (3)
  • internal/core/config.go
  • internal/core/types.go
  • internal/core/types_test.go

Comment thread internal/core/config.go
Comment thread internal/core/types.go Outdated
Comment thread internal/core/types.go
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
internal/core/types_test.go (1)

122-134: ⚡ Quick win

Strengthen built-in immutability assertion for lark.

Line 131 only checks Open, so mutation of Accounts/MCP/AppLink could slip through. Snapshot and compare the full Endpoints struct for lark (same as feishu), and optionally assert empty brand key is not inserted.

🧪 Suggested test hardening
 func TestRegisterBrand_IgnoresBuiltIn(t *testing.T) {
-	original := brandRegistry[string(BrandFeishu)]
+	originalFeishu := brandRegistry[string(BrandFeishu)]
+	originalLark := brandRegistry[string(BrandLark)]
 	RegisterBrand("feishu", Endpoints{Open: "https://malicious.example.com"})
 	RegisterBrand("lark", Endpoints{Open: "https://malicious.example.com"})
 	RegisterBrand("", Endpoints{Open: "https://malicious.example.com"})

-	if brandRegistry[string(BrandFeishu)] != original {
+	if brandRegistry[string(BrandFeishu)] != originalFeishu {
 		t.Error("RegisterBrand should not overwrite built-in feishu brand")
 	}
-	if brandRegistry[string(BrandLark)].Open == "https://malicious.example.com" {
+	if brandRegistry[string(BrandLark)] != originalLark {
 		t.Error("RegisterBrand should not overwrite built-in lark brand")
 	}
+	if _, ok := brandRegistry[""]; ok {
+		t.Error("RegisterBrand should ignore empty brand name")
+	}
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/core/types_test.go` around lines 122 - 134, Update
TestRegisterBrand_IgnoresBuiltIn to assert the entire Endpoints struct for lark
is unchanged: capture the original endpoints (like originalFeishu :=
brandRegistry[string(BrandFeishu)] and originalLark :=
brandRegistry[string(BrandLark)]) before calling RegisterBrand, call
RegisterBrand("", ...) and RegisterBrand("feishu", ...)/RegisterBrand("lark",
...), then compare brandRegistry[string(BrandLark)] to originalLark (not just
the .Open field) using equality on the Endpoints value; also add an assertion
that brandRegistry does not contain an entry for the empty string key (e.g.,
brandRegistry[""] is absent or zero value) to ensure empty brand keys were not
inserted.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@internal/core/types_test.go`:
- Around line 122-134: Update TestRegisterBrand_IgnoresBuiltIn to assert the
entire Endpoints struct for lark is unchanged: capture the original endpoints
(like originalFeishu := brandRegistry[string(BrandFeishu)] and originalLark :=
brandRegistry[string(BrandLark)]) before calling RegisterBrand, call
RegisterBrand("", ...) and RegisterBrand("feishu", ...)/RegisterBrand("lark",
...), then compare brandRegistry[string(BrandLark)] to originalLark (not just
the .Open field) using equality on the Endpoints value; also add an assertion
that brandRegistry does not contain an entry for the empty string key (e.g.,
brandRegistry[""] is absent or zero value) to ensure empty brand keys were not
inserted.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0a812edd-8309-4708-8cfc-1da2683dcde2

📥 Commits

Reviewing files that changed from the base of the PR and between 3f756b5 and 7ed692b.

📒 Files selected for processing (3)
  • internal/core/config.go
  • internal/core/types.go
  • internal/core/types_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • internal/core/config.go
  • internal/core/types.go

@liangshuo-1
Copy link
Copy Markdown
Collaborator

Thanks for the PR 🙏

One thing to flag upfront: just exposing the endpoint baseURL isn't enough to make lark-cli work against a private deployment. Auth, the OpenAPI command surface, and most upper-layer capabilities don't have parity with the public cloud yet — so even with extraBrands pointing at a private host, most commands would still fail.

Private deployment support needs to be tackled as a whole, not via an endpoint override. So I'd rather not land this under the "private deployment" framing and set an expectation we can't meet.

If you're trying to reach a specific internal environment, happy to look at that case separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Single-domain feat or fix with limited business impact

Projects

None yet

3 participants