Skip to content

feat: add RuntimeContext.BotInfo() for lazy bot identity retrieval#409

Merged
liangshuo-1 merged 1 commit intomainfrom
feat/runtime-bot-info
Apr 11, 2026
Merged

feat: add RuntimeContext.BotInfo() for lazy bot identity retrieval#409
liangshuo-1 merged 1 commit intomainfrom
feat/runtime-bot-info

Conversation

@liangshuo-1
Copy link
Copy Markdown
Collaborator

@liangshuo-1 liangshuo-1 commented Apr 10, 2026

Summary

  • Add BotInfo() method on RuntimeContext that lazily fetches the current app's bot open_id and display name from /bot/v3/info, cached via sync.OnceValues
  • Add CanBot() on CliConfig to gate the call when bot identity is unavailable (e.g. external credential provider only injecting UAT)
  • Add fetchBotInfo() private method using DoAPIAsBot for consistent shortcut header injection (X-Cli-Shortcut, X-Cli-Execution-Id)
  • Add TestNewRuntimeContextWithBotInfo test helper for shortcut tests that need a pre-set bot identity

Test plan

  • TestFetchBotInfo_Success — full path via httpmock
  • TestFetchBotInfo_ShortcutHeaders — verifies X-Cli-Shortcut/X-Cli-Execution-Id injection
  • TestFetchBotInfo_OnceSemantics — single stub, two calls
  • TestFetchBotInfo_APICodeNonZero — code != 0 error branch
  • TestFetchBotInfo_EmptyOpenID — empty open_id error branch
  • TestFetchBotInfo_HTTP4xx — HTTP status code >= 400
  • TestFetchBotInfo_InvalidJSON — malformed response body
  • TestFetchBotInfo_CanBotFalse — SupportedIdentities=user-only pre-check
  • TestBotInfo_NilFunc — nil guard for uninitialized contexts
  • TestCliConfig_CanBot — 4 sub-cases for CanBot() bitflag logic

Summary by CodeRabbit

Release Notes

  • New Features
    • Added bot identity authentication capability support with validation to ensure bots are properly configured and authorized
    • Implemented bot metadata retrieval functionality allowing bots to securely fetch their open ID and application name

Add BotInfo() method on RuntimeContext that lazily fetches the current
app's bot open_id and display name from /bot/v3/info on first call,
cached via sync.OnceValues for the lifetime of the process.

- BotInfo struct (OpenID, AppName) in Identity section of runner.go
- fetchBotInfo() uses DoAPIAsBot for consistent header injection
- CanBot() on CliConfig gates the call when bot identity is unavailable
- Nil guard prevents panic in test contexts
- Full test coverage via httpmock.Registry + mounted shortcuts

Change-Id: I40ac710fb52d13939853f71827a5cbdbddd4f80f
@github-actions github-actions bot added the size/M Single-domain feat or fix with limited business impact label Apr 10, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 10, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 38275645-836e-4035-ad21-dc5f41e53cfe

📥 Commits

Reviewing files that changed from the base of the PR and between 8c799d5 and 2764540.

📒 Files selected for processing (5)
  • internal/core/config.go
  • internal/core/config_test.go
  • shortcuts/common/runner.go
  • shortcuts/common/runner_botinfo_test.go
  • shortcuts/common/testing.go

📝 Walkthrough

Walkthrough

This PR adds bot identity capability checking and introduces bot information retrieval functionality. A new CanBot() method checks if bot identity is supported in CLI config, while BotInfo() on RuntimeContext lazily fetches bot details via API with thread-safe caching, including validation of identity support, HTTP status, and JSON unmarshalling.

Changes

Cohort / File(s) Summary
Bot Identity Capability
internal/core/config.go, internal/core/config_test.go
Added CanBot() method and identityBotBit constant to enable capability checks for bot identity support in CLI configuration.
Bot Information Retrieval
shortcuts/common/runner.go, shortcuts/common/runner_botinfo_test.go
Introduced BotInfo struct and BotInfo() method on RuntimeContext with lazy, thread-safe initialization via sync.OnceValues. Includes fetchBotInfo() that validates bot capability, performs API request to /open-apis/bot/v3/info, and handles multiple error cases.
Testing Utilities
shortcuts/common/testing.go
Added TestNewRuntimeContextWithBotInfo helper function to construct RuntimeContext instances with mocked bot information for testing purposes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

size/M

Suggested reviewers

  • MaxHuang22
  • hugang-lark

Poem

🐰 A bot's true name, now fetched with care,
Through cached lanes with thread-safe flair,
Identity checked before we dare,
To call the APIs in the air! 🚀

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: add RuntimeContext.BotInfo() for lazy bot identity retrieval' clearly and specifically summarizes the main feature being added: a new method for lazy fetching of bot identity information.
Description check ✅ Passed The description fully addresses all template sections: provides a clear summary of changes, lists the main additions, documents comprehensive test coverage with checkmarks, and notes no related issues.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/runtime-bot-info

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

@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Apr 10, 2026

Greptile Summary

This PR adds a BotInfo() method on RuntimeContext that lazily fetches the bot's open_id and app_name from /open-apis/bot/v3/info, cached via sync.OnceValues, along with a CanBot() gate on CliConfig and a test helper TestNewRuntimeContextWithBotInfo. The implementation is solid and well-tested across all error branches.

Confidence Score: 5/5

Safe to merge; all findings are style-level P2 suggestions with no correctness or reliability impact.

All three comments are P2 (style/documentation): the BotInfo name collision is valid Go, the sync.OnceValues error-caching behaviour is intentional and consistent with the short RuntimeContext lifetime, and the identityBotBit drift risk is low given stable iota ordering. No P0/P1 issues found.

No files require special attention beyond the P2 style notes on runner.go and config.go.

Important Files Changed

Filename Overview
shortcuts/common/runner.go Adds BotInfo struct, BotInfo() method, and fetchBotInfo() helper; the struct/method name collision and sync.OnceValues error-caching semantics are worth documenting more clearly
internal/core/config.go Adds CanBot() and identityBotBit; the constant duplicates extension/credential.SupportsBot without a compile-time assertion to catch future drift
internal/core/config_test.go Adds TestCliConfig_CanBot with four sub-cases covering all bitflag combinations; straightforward and correct
shortcuts/common/runner_botinfo_test.go Comprehensive tests for all error branches and the Once semantics; well-structured with a shared helper
shortcuts/common/testing.go Adds TestNewRuntimeContextWithBotInfo test helper; clean and consistent with existing test context constructors

Sequence Diagram

sequenceDiagram
    participant Caller
    participant RuntimeContext
    participant sync.OnceValues
    participant fetchBotInfo
    participant CliConfig
    participant DoAPIAsBot

    Caller->>RuntimeContext: BotInfo()
    alt botInfoFunc == nil
        RuntimeContext-->>Caller: error(not fully initialized)
    else botInfoFunc set
        RuntimeContext->>sync.OnceValues: invoke botInfoFunc()
        alt already cached
            sync.OnceValues-->>RuntimeContext: cached (*BotInfo, error)
        else first call
            sync.OnceValues->>fetchBotInfo: call
            fetchBotInfo->>CliConfig: CanBot()
            alt SupportedIdentities has bot bit or == 0
                CliConfig-->>fetchBotInfo: true
                fetchBotInfo->>DoAPIAsBot: GET /open-apis/bot/v3/info
                DoAPIAsBot-->>fetchBotInfo: ApiResp
                alt HTTP >= 400
                    fetchBotInfo-->>sync.OnceValues: nil, error(HTTP N)
                else code != 0
                    fetchBotInfo-->>sync.OnceValues: nil, error(code msg)
                else open_id empty
                    fetchBotInfo-->>sync.OnceValues: nil, error(open_id is empty)
                else success
                    fetchBotInfo-->>sync.OnceValues: BotInfo, nil
                end
            else bot not supported
                CliConfig-->>fetchBotInfo: false
                fetchBotInfo-->>sync.OnceValues: nil, error(not available)
            end
            sync.OnceValues-->>RuntimeContext: (*BotInfo, error) cached
        end
        RuntimeContext-->>Caller: (*BotInfo, error)
    end
Loading

Reviews (1): Last reviewed commit: "feat: add RuntimeContext.BotInfo() for l..." | Re-trigger Greptile

@github-actions
Copy link
Copy Markdown

🚀 PR Preview Install Guide

🧰 CLI update

npm i -g https://pkg.pr.new/larksuite/cli/@larksuite/cli@2764540085c63759f365b39a004ff17311d10a06

🧩 Skill update

npx skills add larksuite/cli#feat/runtime-bot-info -y -g

@liangshuo-1 liangshuo-1 merged commit 9f81e7e into main Apr 11, 2026
16 checks passed
@liangshuo-1 liangshuo-1 deleted the feat/runtime-bot-info branch April 11, 2026 03:53
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

Development

Successfully merging this pull request may close these issues.

2 participants