Skip to content

fix(cnb): pin login to CNB_HOST so it ignores the current dir git remote - #530

Merged
jeff-r2026 merged 1 commit into
mainfrom
fix/cnb-login-host
Sep 12, 2026
Merged

jeff-r2026 merged 1 commit into
mainfrom
fix/cnb-login-host

Conversation

@jeff-r2026

Copy link
Copy Markdown
Collaborator

What & why

cnb login(@cnbcool/cnb-cli)在未显式指定 --host 时,会从当前目录第一个 git remote 推断平台地址(resolvePlatformUrl → getGitRemoteHosts() 跑 git remote -v)。优先级为:--host > git remote > CNB_API_ENDPOINT > 默认 cnb.cool。

因此在一个 remote 指向非 CNB 平台(如内网 git 服务器)的仓库里跑 cnb login,device-auth 请求会被打到那个 host 并返回 401;而在没有该 remote 的目录里同一命令却成功——表现为「同一命令换个目录就登录失败」。

teamai 的 cnbLogin() 此前调 cnb login 未传 --host,在这种仓库里首次 teamai init(provider=cnb、且未登录过)会踩同样的坑。本 provider 其他所有操作(clone / create-repo / PR)都已以 CNB_HOST 为唯一真相源,唯独 login 例外。

What changed

  • src/providers/cnb/cnb-cli.ts:cnbLogin() 改为 cnb login --host ${CNB_HOST}(CNB_HOST = TEAMAI_CNB_HOST ?? cnb.cool),与 provider 其他操作同源;自托管实例自动带上其域名。
  • 新增单测 src/__tests__/cnb-login-host.test.ts:断言默认与 TEAMAI_CNB_HOST 两种情况下的 argv。
  • docs/providers.md:手动 cnb login 示例改为带 --host cnb.cool,并说明原因与 teamai 自动带 host 的行为。

CI 路径(CNB_TOKEN)根本不触发 login,不受影响。

Test plan / report

环境:macOS,真实 @cnbcool/cnb-cli v1.15.20,在 remote 指向 git.woa.com(非 CNB)的仓库目录内。

  • npx tsc --noEmit — pass

  • npx vitest run — 221 files / 3040 tests passed

  • npm run build — pass(dist 内含 login", "--host", CNB_HOST)

  • 真实 CLI 复现 + 修复验证(cnb ... --debug,密钥已打码):

    场景 Platform URL 结果
    不带 --host(推断 git remote) https://git.woa.com status=401 → 登录失败: status=401 body=(复现报错)
    带 --host cnb.cool(修复) https://cnb.cool device-auth status=200,进入 token 轮询(authorization_pending,正常等待浏览器授权)
  • 验证 teamai 实际发出的 argv(用记录参数的假 cnb shim 驱动真实 cnbLogin()):

    • 默认:login --host cnb.cool
    • TEAMAI_CNB_HOST=cnb.internal.example.com:login --host cnb.internal.example.com

The cnb CLI infers its platform URL from the first git remote of the
current directory when --host is not given. Running `cnb login` inside a
repo whose remote points at a non-CNB host (e.g. an internal git server)
therefore sends the OAuth2 device-auth request there and fails with 401,
while the same command succeeds in a directory with no such remote.

Pass --host ${CNB_HOST} explicitly. CNB_HOST (TEAMAI_CNB_HOST, default
cnb.cool) is already the single source of truth for every other CNB
operation (clone / create-repo / PR), so anchoring login to it keeps the
provider's auth consistent and self-hosted-friendly.
@jeff-r2026
jeff-r2026 merged commit c593801 into main Sep 12, 2026
7 checks passed
@jeff-r2026
jeff-r2026 deleted the fix/cnb-login-host branch September 12, 2026 05:35
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
…ote (Tencent#530)

The cnb CLI infers its platform URL from the first git remote of the
current directory when --host is not given. Running `cnb login` inside a
repo whose remote points at a non-CNB host (e.g. an internal git server)
therefore sends the OAuth2 device-auth request there and fails with 401,
while the same command succeeds in a directory with no such remote.

Pass --host ${CNB_HOST} explicitly. CNB_HOST (TEAMAI_CNB_HOST, default
cnb.cool) is already the single source of truth for every other CNB
operation (clone / create-repo / PR), so anchoring login to it keeps the
provider's auth consistent and self-hosted-friendly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant