mirror of
https://github.com/larksuite/cli.git
synced 2026-08-03 08:32:46 +08:00
Compare commits
58 Commits
v1.0.69
...
v1.0.73-be
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b704a495de | ||
|
|
72fe82d70d | ||
|
|
683a721a76 | ||
|
|
bbb3c505e0 | ||
|
|
e50820bd11 | ||
|
|
ac1e09e46f | ||
|
|
2bfde8d886 | ||
|
|
7b989948c4 | ||
|
|
964c571063 | ||
|
|
4a523b12f2 | ||
|
|
c6039a923c | ||
|
|
6ff10229fd | ||
|
|
21cff2e2dd | ||
|
|
44514ad114 | ||
|
|
4a56748bfa | ||
|
|
0b6faa01bf | ||
|
|
1efe2dfb33 | ||
|
|
767386cb57 | ||
|
|
e71c76155e | ||
|
|
c363acf94e | ||
|
|
05285bb696 | ||
|
|
4c0f93bd6a | ||
|
|
76ebd49382 | ||
|
|
6c14c425fc | ||
|
|
27df16d3b2 | ||
|
|
47dc003601 | ||
|
|
4e0a6a988c | ||
|
|
15e4175986 | ||
|
|
708196040a | ||
|
|
65586577a3 | ||
|
|
12b7f7a0cd | ||
|
|
be1f3621de | ||
|
|
65998a21e3 | ||
|
|
d5afe3f705 | ||
|
|
baf6050f8e | ||
|
|
a6bc81596a | ||
|
|
7f43b7ed5d | ||
|
|
80b3645362 | ||
|
|
64caef1526 | ||
|
|
64e10a0954 | ||
|
|
8897196dee | ||
|
|
49b4ccceb9 | ||
|
|
4b2d012af9 | ||
|
|
90aad64b8d | ||
|
|
2919084103 | ||
|
|
36bd82cb27 | ||
|
|
2e77d8db80 | ||
|
|
d9061ffcbc | ||
|
|
08d9b28ee8 | ||
|
|
168fb13e3e | ||
|
|
55c2e5c819 | ||
|
|
16a93cd277 | ||
|
|
e9dabb2184 | ||
|
|
8acd55e907 | ||
|
|
6ecbfaf690 | ||
|
|
ac2508d3b0 | ||
|
|
1c3674487f | ||
|
|
37d490a198 |
169
.github/workflows/ci.yml
vendored
169
.github/workflows/ci.yml
vendored
@@ -1,4 +1,5 @@
|
||||
name: CI
|
||||
run-name: ${{ github.event_name == 'pull_request' && format('CI / {0}', github.event.pull_request.number) || '' }}
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -8,6 +9,12 @@ on:
|
||||
types: [opened, synchronize, reopened, edited]
|
||||
workflow_dispatch:
|
||||
|
||||
# PR metadata edits can retrigger full CI for the same head. Keep only the
|
||||
# newest run for a pull request; push and manual runs use a unique run ID.
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
|
||||
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
actions: read
|
||||
@@ -47,6 +54,34 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
|
||||
plugin-integration:
|
||||
needs: fast-gate
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
# No fetch_meta: the git-archive clean tree must embed only the
|
||||
# committed meta_data stub (reproduces the bare-module customer state).
|
||||
- name: Run plugin-integration L4 tests
|
||||
run: go test -count=1 -timeout=15m ./tests/plugin_e2e/...
|
||||
|
||||
sidecar-integration:
|
||||
needs: fast-gate
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
- name: Run sidecar tag build + HMAC round-trip
|
||||
run: make sidecar-test
|
||||
|
||||
# ── Layer 2: Quality Gate ──────────────────────────────────────────
|
||||
unit-test:
|
||||
needs: fast-gate
|
||||
@@ -176,7 +211,11 @@ jobs:
|
||||
run: python3 scripts/fetch_meta.py
|
||||
- name: Run tests with coverage
|
||||
run: |
|
||||
packages=$(go list ./... | grep -v '^github.com/larksuite/cli/tests/cli_e2e$' | grep -v '^github.com/larksuite/cli/tests/cli_e2e/')
|
||||
# tests/ holds only L3/L4 suites (cli_e2e, plugin_e2e, sidecar_e2e) that
|
||||
# have dedicated jobs; exclude the whole subtree so none of them runs a
|
||||
# second time here — and, crucially, so an observe-only suite's failure
|
||||
# can never block merges through coverage's spot in the results loop.
|
||||
packages=$(go list ./... | grep -v '^github.com/larksuite/cli/tests/')
|
||||
go test -race -coverprofile=coverage.txt -covermode=atomic $packages
|
||||
- name: Upload coverage to Codecov
|
||||
if: ${{ github.event_name != 'pull_request' || !github.event.pull_request.head.repo.fork }}
|
||||
@@ -263,6 +302,11 @@ jobs:
|
||||
e2e-dry-run:
|
||||
needs: [unit-test, lint, script-test, deterministic-gate]
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
outputs:
|
||||
mode: ${{ steps.e2e_domains.outputs.mode }}
|
||||
reason: ${{ steps.e2e_domains.outputs.reason }}
|
||||
live_packages: ${{ steps.e2e_domains.outputs.live_packages }}
|
||||
steps:
|
||||
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
|
||||
with:
|
||||
@@ -276,6 +320,23 @@ jobs:
|
||||
- name: Resolve CLI E2E domains
|
||||
id: e2e_domains
|
||||
run: node scripts/e2e_domains.js
|
||||
- name: Validate CLI E2E domain outputs
|
||||
env:
|
||||
E2E_MODE: ${{ steps.e2e_domains.outputs.mode }}
|
||||
E2E_LIVE_PACKAGES: ${{ steps.e2e_domains.outputs.live_packages }}
|
||||
run: |
|
||||
case "$E2E_MODE" in
|
||||
skip)
|
||||
[ -z "$E2E_LIVE_PACKAGES" ] || { echo "::error::Skip mode must not resolve live packages"; exit 1; }
|
||||
;;
|
||||
full|subset)
|
||||
[ -n "$E2E_LIVE_PACKAGES" ] || { echo "::error::No live packages resolved for mode $E2E_MODE"; exit 1; }
|
||||
;;
|
||||
*)
|
||||
echo "::error::Invalid CLI E2E mode: $E2E_MODE"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
- name: Build lark-cli
|
||||
if: ${{ steps.e2e_domains.outputs.mode != 'skip' }}
|
||||
run: make build
|
||||
@@ -309,16 +370,22 @@ jobs:
|
||||
fi
|
||||
|
||||
e2e-live:
|
||||
needs: [unit-test, lint, script-test, deterministic-gate]
|
||||
if: ${{ github.event_name != 'pull_request' || !github.event.pull_request.head.repo.fork }}
|
||||
needs: [unit-test, lint, script-test, deterministic-gate, e2e-dry-run]
|
||||
if: ${{ always() && (github.event_name != 'pull_request' || !github.event.pull_request.head.repo.fork) && needs.unit-test.result == 'success' && needs.lint.result == 'success' && needs.script-test.result == 'success' && needs.deterministic-gate.result == 'success' && needs.e2e-dry-run.result == 'success' && (needs.e2e-dry-run.outputs.mode == 'full' || needs.e2e-dry-run.outputs.mode == 'subset') && needs.e2e-dry-run.outputs.live_packages != '' }}
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 30
|
||||
# Live E2E uses one repository-wide execution slot.
|
||||
concurrency:
|
||||
group: lark-cli-e2e-live
|
||||
cancel-in-progress: false
|
||||
queue: max
|
||||
permissions:
|
||||
actions: read
|
||||
contents: read
|
||||
checks: write
|
||||
env:
|
||||
TEST_BOT1_APP_ID: ${{ secrets.TEST_BOT1_APP_ID }}
|
||||
TEST_BOT1_APP_SECRET: ${{ secrets.TEST_BOT1_APP_SECRET }}
|
||||
TEST_USER_ACCESS_TOKEN: ${{ secrets.TEST_USER_ACCESS_TOKEN }}
|
||||
LARKSUITE_CLI_BRAND: feishu
|
||||
steps:
|
||||
- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5
|
||||
with:
|
||||
@@ -329,31 +396,68 @@ jobs:
|
||||
- uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- name: Resolve CLI E2E domains
|
||||
id: e2e_domains
|
||||
run: node scripts/e2e_domains.js
|
||||
- name: Build lark-cli
|
||||
if: ${{ steps.e2e_domains.outputs.mode != 'skip' }}
|
||||
id: build_cli
|
||||
run: make build
|
||||
- name: Configure bot credentials
|
||||
if: ${{ steps.e2e_domains.outputs.mode != 'skip' }}
|
||||
- name: Prepare shared live E2E tenant token
|
||||
id: live_e2e_tat
|
||||
env:
|
||||
LARKSUITE_CLI_APP_ID: ${{ secrets.TEST_BOT1_APP_ID }}
|
||||
TEST_BOT1_APP_SECRET: ${{ secrets.TEST_BOT1_APP_SECRET }}
|
||||
run: node scripts/fetch_e2e_tat.js
|
||||
- name: Run CLI E2E tests
|
||||
# Keep an active Go test alive so t.Cleanup can finish. A queued stale
|
||||
# run is rejected below before it can start live E2E.
|
||||
if: ${{ always() && steps.build_cli.outcome == 'success' && steps.live_e2e_tat.outcome == 'success' }}
|
||||
shell: bash
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
REPOSITORY: ${{ github.repository }}
|
||||
EVENT_NAME: ${{ github.event_name }}
|
||||
RUN_ID: ${{ github.run_id }}
|
||||
RUN_NUMBER: ${{ github.run_number }}
|
||||
RUN_GENERATION: ${{ github.event_name == 'pull_request' && format('CI / {0}', github.event.pull_request.number) || '' }}
|
||||
LARK_CLI_BIN: ${{ github.workspace }}/lark-cli
|
||||
E2E_MODE: ${{ needs.e2e-dry-run.outputs.mode }}
|
||||
E2E_REASON: ${{ needs.e2e-dry-run.outputs.reason }}
|
||||
E2E_LIVE_PACKAGES: ${{ needs.e2e-dry-run.outputs.live_packages }}
|
||||
E2E_TENANT_AUTH_FILE: ${{ steps.live_e2e_tat.outputs.path }}
|
||||
TEST_USER_ACCESS_TOKEN: ${{ secrets.TEST_USER_ACCESS_TOKEN }}
|
||||
run: |
|
||||
if [ -z "$TEST_BOT1_APP_ID" ] || [ -z "$TEST_BOT1_APP_SECRET" ]; then
|
||||
echo "::error::Missing required secrets: TEST_BOT1_APP_ID / TEST_BOT1_APP_SECRET"
|
||||
if [ "$EVENT_NAME" = "pull_request" ]; then
|
||||
workflow_id="$(gh api "repos/$REPOSITORY/actions/runs/$RUN_ID" --jq '.workflow_id')"
|
||||
newer_runs="$(
|
||||
gh api --paginate -X GET "repos/$REPOSITORY/actions/workflows/$workflow_id/runs" \
|
||||
-f event=pull_request -f branch="$GITHUB_HEAD_REF" -f per_page=100 |
|
||||
jq -r --arg repository "$REPOSITORY" --arg generation "$RUN_GENERATION" --argjson run_number "$RUN_NUMBER" \
|
||||
'.workflow_runs[] | select(.head_repository.full_name == $repository and .display_title == $generation and .run_number > $run_number) | .id'
|
||||
)"
|
||||
if [ -n "$newer_runs" ]; then
|
||||
echo "::error::Superseded before live E2E started by newer workflow run(s): $newer_runs"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
if [ -z "${E2E_TENANT_AUTH_FILE:-}" ] || [ ! -f "$E2E_TENANT_AUTH_FILE" ]; then
|
||||
echo "::error::Missing shared live E2E tenant token file"
|
||||
exit 1
|
||||
fi
|
||||
printf '%s\n' "$TEST_BOT1_APP_SECRET" | ./lark-cli config init --app-id "$TEST_BOT1_APP_ID" --app-secret-stdin
|
||||
- name: Run CLI E2E tests
|
||||
env:
|
||||
LARK_CLI_BIN: ${{ github.workspace }}/lark-cli
|
||||
E2E_MODE: ${{ steps.e2e_domains.outputs.mode }}
|
||||
E2E_REASON: ${{ steps.e2e_domains.outputs.reason }}
|
||||
E2E_LIVE_PACKAGES: ${{ steps.e2e_domains.outputs.live_packages }}
|
||||
run: |
|
||||
if [ "$E2E_MODE" = "skip" ]; then
|
||||
echo "No live CLI E2E needed: $E2E_REASON"
|
||||
exit 0
|
||||
export TEST_TENANT_ACCESS_TOKEN="$(cat "$E2E_TENANT_AUTH_FILE")"
|
||||
rm -f "$E2E_TENANT_AUTH_FILE"
|
||||
if ! LARKSUITE_CLI_APP_ID="$TEST_BOT1_APP_ID" \
|
||||
LARKSUITE_CLI_TENANT_ACCESS_TOKEN="$TEST_TENANT_ACCESS_TOKEN" \
|
||||
./lark-cli whoami --as bot | node -e '
|
||||
let input = "";
|
||||
process.stdin.setEncoding("utf8");
|
||||
process.stdin.on("data", (chunk) => { input += chunk; });
|
||||
process.stdin.on("end", () => {
|
||||
const result = JSON.parse(input);
|
||||
if (result.identity !== "bot" || result.available !== true || result.tokenStatus !== "ready") process.exit(1);
|
||||
});
|
||||
'; then
|
||||
echo "::error::Tenant credential preflight failed"
|
||||
exit 1
|
||||
fi
|
||||
echo "Tenant credential preflight succeeded"
|
||||
packages="$E2E_LIVE_PACKAGES"
|
||||
if [ -z "$packages" ]; then
|
||||
echo "::error::No live CLI E2E packages resolved for mode $E2E_MODE"
|
||||
@@ -363,7 +467,7 @@ jobs:
|
||||
echo "Live CLI E2E packages: $packages"
|
||||
go run gotest.tools/gotestsum@v1.12.3 --rerun-fails=2 --rerun-fails-max-failures=20 --packages="$packages" --format testname --junitfile cli-e2e-report.xml -- -count=1 -v
|
||||
- name: Publish CLI E2E test report
|
||||
if: ${{ !cancelled() && steps.e2e_domains.outputs.mode != 'skip' }}
|
||||
if: ${{ !cancelled() }}
|
||||
uses: dorny/test-reporter@a43b3a5f7366b97d083190328d2c652e1a8b6aa2 # v3.0.0
|
||||
with:
|
||||
name: CLI E2E Tests
|
||||
@@ -416,7 +520,7 @@ jobs:
|
||||
# ── Results Gate (single required check for branch protection) ─────
|
||||
results:
|
||||
if: ${{ always() }}
|
||||
needs: [fast-gate, unit-test, lint, script-test, deterministic-gate, coverage, deadcode, e2e-dry-run, e2e-live, security, license-header]
|
||||
needs: [fast-gate, unit-test, lint, script-test, deterministic-gate, coverage, deadcode, e2e-dry-run, e2e-live, security, license-header, plugin-integration, sidecar-integration]
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Evaluate results
|
||||
@@ -436,10 +540,19 @@ jobs:
|
||||
echo "| L3 | e2e-live | ${{ needs.e2e-live.result }} |" >> $GITHUB_STEP_SUMMARY
|
||||
echo "| L4 | security | ${{ needs.security.result }} |" >> $GITHUB_STEP_SUMMARY
|
||||
echo "| L4 | license-header | ${{ needs.license-header.result }} |" >> $GITHUB_STEP_SUMMARY
|
||||
echo "| L4 | plugin-integration (observe-only) | ${{ needs.plugin-integration.result }} |" >> $GITHUB_STEP_SUMMARY
|
||||
echo "| L4 | sidecar-integration (observe-only) | ${{ needs.sidecar-integration.result }} |" >> $GITHUB_STEP_SUMMARY
|
||||
|
||||
# Any failure or cancellation in any job blocks the merge.
|
||||
# Legitimately skipped jobs (deadcode on push, e2e-live on fork,
|
||||
# license-header on push) are OK.
|
||||
# Legitimately skipped jobs (deadcode on push, e2e-live when not
|
||||
# needed or on a fork, license-header on push) are OK.
|
||||
#
|
||||
# plugin-integration and sidecar-integration are intentionally NOT
|
||||
# in this loop yet: they run on every PR and their status is shown
|
||||
# in the table above, but a failure is observe-only (non-blocking)
|
||||
# during the initial soak. Graduation to required is tracked in
|
||||
# https://github.com/larksuite/cli/issues/1894 (criteria: 4
|
||||
# consecutive weeks with zero false positives).
|
||||
FAILED=0
|
||||
for result in \
|
||||
"${{ needs.fast-gate.result }}" \
|
||||
|
||||
145
.github/workflows/release.yml
vendored
145
.github/workflows/release.yml
vendored
@@ -9,10 +9,54 @@ permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
goreleaser:
|
||||
preflight:
|
||||
runs-on: ubuntu-22.04
|
||||
permissions:
|
||||
contents: write
|
||||
contents: read
|
||||
steps:
|
||||
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6
|
||||
with:
|
||||
node-version: '22.14.0'
|
||||
|
||||
- name: Validate tag and commit
|
||||
env:
|
||||
TAG: ${{ github.ref_name }}
|
||||
REHEARSAL_BRANCH: test/npm-staged-publish-rehearsal
|
||||
run: |
|
||||
set -euo pipefail
|
||||
node scripts/release-preflight.js --tag "$TAG"
|
||||
HEAD_SHA="$(git rev-parse --verify 'HEAD^{commit}')"
|
||||
TAG_SHA="$(git rev-parse --verify "refs/tags/${TAG}^{commit}")"
|
||||
if [[ "$TAG_SHA" != "$HEAD_SHA" ]]; then
|
||||
echo "Tag ${TAG} does not resolve to the checked-out HEAD commit." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ "$TAG" == *-beta.* ]]; then
|
||||
git fetch origin "$REHEARSAL_BRANCH"
|
||||
REHEARSAL_SHA="$(git rev-parse --verify 'FETCH_HEAD^{commit}')"
|
||||
if [[ "$HEAD_SHA" != "$REHEARSAL_SHA" ]]; then
|
||||
echo "Beta rehearsal tag ${TAG} must point to the current origin/${REHEARSAL_BRANCH} commit." >&2
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
git fetch origin main
|
||||
MAIN_SHA="$(git rev-parse --verify 'FETCH_HEAD^{commit}')"
|
||||
if ! git merge-base --is-ancestor "$HEAD_SHA" "$MAIN_SHA"; then
|
||||
echo "Tag ${TAG} does not point to a commit contained in origin/main." >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
build-stage-assets:
|
||||
needs: preflight
|
||||
runs-on: ubuntu-22.04
|
||||
permissions:
|
||||
contents: read
|
||||
steps:
|
||||
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
||||
with:
|
||||
@@ -26,35 +70,86 @@ jobs:
|
||||
with:
|
||||
python-version: '3.x'
|
||||
|
||||
- uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6
|
||||
with:
|
||||
node-version: '22.14.0'
|
||||
registry-url: 'https://registry.npmjs.org'
|
||||
package-manager-cache: false
|
||||
|
||||
- name: Install pinned npm
|
||||
run: npm install --global npm@11.16.0
|
||||
|
||||
- name: Run GoReleaser
|
||||
uses: goreleaser/goreleaser-action@e435ccd777264be153ace6237001ef4d979d3a7a # v6
|
||||
with:
|
||||
version: '~> v2'
|
||||
args: release --clean
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
args: release --clean --skip=publish
|
||||
|
||||
publish-npm:
|
||||
needs: goreleaser
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
|
||||
|
||||
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
||||
with:
|
||||
node-version: '20'
|
||||
registry-url: 'https://registry.npmjs.org'
|
||||
|
||||
- name: Download checksums from release
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Verify release checksums
|
||||
run: |
|
||||
set -euo pipefail
|
||||
TAG="${GITHUB_REF_NAME}"
|
||||
gh release download "${TAG}" --pattern checksums.txt --dir .
|
||||
test -s checksums.txt || { echo "checksums.txt missing or empty for ${TAG}"; exit 1; }
|
||||
test -s dist/checksums.txt
|
||||
(cd dist && sha256sum --check checksums.txt)
|
||||
cp dist/checksums.txt checksums.txt
|
||||
|
||||
- name: Publish to npm
|
||||
- name: Pack npm tarball
|
||||
id: pack
|
||||
run: |
|
||||
set -euo pipefail
|
||||
PACK_JSON="$(npm pack --ignore-scripts --json)"
|
||||
PACK_FILE="$(node -e 'const p=JSON.parse(process.argv[1]); if(p.length!==1 || !p[0].filename) process.exit(1); process.stdout.write(p[0].filename)' "$PACK_JSON")"
|
||||
test -s "$PACK_FILE"
|
||||
tar -tzf "$PACK_FILE" | grep -qx 'package/checksums.txt'
|
||||
echo "filename=$PACK_FILE" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Collect rehearsal assets
|
||||
env:
|
||||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||
run: npm publish --access public
|
||||
PACK_FILE: ${{ steps.pack.outputs.filename }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
mkdir staged-release-assets
|
||||
cp dist/*.tar.gz dist/*.zip dist/checksums.txt "$PACK_FILE" staged-release-assets/
|
||||
|
||||
- name: Upload rehearsal artifact
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
with:
|
||||
name: staged-release-assets-${{ github.run_id }}
|
||||
path: staged-release-assets/
|
||||
if-no-files-found: error
|
||||
overwrite: true
|
||||
|
||||
stage-publish:
|
||||
needs: build-stage-assets
|
||||
runs-on: ubuntu-22.04
|
||||
environment: npm-production
|
||||
permissions:
|
||||
id-token: write
|
||||
steps:
|
||||
- uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6
|
||||
with:
|
||||
node-version: '22.14.0'
|
||||
registry-url: 'https://registry.npmjs.org'
|
||||
package-manager-cache: false
|
||||
|
||||
- name: Install pinned npm
|
||||
run: npm install --global npm@11.16.0
|
||||
|
||||
- name: Download rehearsal artifact
|
||||
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
|
||||
with:
|
||||
name: staged-release-assets-${{ github.run_id }}
|
||||
path: staged-release-assets
|
||||
|
||||
- name: Verify rehearsal asset
|
||||
id: asset
|
||||
run: |
|
||||
set -euo pipefail
|
||||
(cd staged-release-assets && sha256sum --check checksums.txt)
|
||||
PACK_FILE="$(find staged-release-assets -maxdepth 1 -type f -name '*.tgz' -print -quit)"
|
||||
test -n "$PACK_FILE"
|
||||
test -s "$PACK_FILE"
|
||||
tar -tzf "$PACK_FILE" | grep -qx 'package/checksums.txt'
|
||||
echo "filename=$PACK_FILE" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Stage npm package
|
||||
run: npm stage publish "${{ steps.asset.outputs.filename }}" --access public --tag beta
|
||||
|
||||
15
AGENTS.md
15
AGENTS.md
@@ -105,6 +105,20 @@ Signatures that are easy to guess wrong:
|
||||
|
||||
Program output (JSON envelopes) goes to stdout. Progress, warnings, hints go to stderr. Mixing them corrupts pipe chains.
|
||||
|
||||
### Typed data over loose maps
|
||||
|
||||
Parse `map[string]interface{}` into a typed struct at the boundary — one projection function per shape — and let everything downstream consume struct fields, not string keys. A typo'd map key compiles fine and fails at runtime, which an agent then debugs blind.
|
||||
|
||||
Use distinct types when two values could be swapped silently: see `internal/meta.Token` — a bare string compiles on either side of a string/string signature, a distinct type does not.
|
||||
|
||||
Legacy loose-map code exists in older paths. Match its call sites when touching it, but do not copy the pattern into new code.
|
||||
|
||||
### Transcribe faithfully — no silent fallbacks
|
||||
|
||||
When code echoes input onward (request previews, transformations, proxies), transcribe verbatim. A `default:` branch that coerces unrecognized input into a plausible value ("unknown HTTP verb → GET") makes the output lie, and an agent reasons from the lie.
|
||||
|
||||
The same rule applies to flag combinations and internal wiring: if a requested option cannot be honored, return a typed validation error — never silently substitute another behavior and exit 0. Silent guesses (defaulting a missing identity, discarding writes on a nil writer) are bugs even when every current caller happens to avoid them.
|
||||
|
||||
### Use `vfs.*` instead of `os.*`
|
||||
|
||||
All filesystem access goes through `internal/vfs`. This enables test mocking.
|
||||
@@ -116,6 +130,7 @@ CLI arguments are untrusted (they come from AI agents). Call `validate.SafeInput
|
||||
### Tests
|
||||
|
||||
- Every behavior change needs a test alongside the change.
|
||||
- A contract test must fail if the implementation is reverted. If you can undo the code change and the suite stays green, the contract is not pinned — assert the new field/behavior directly, not a happy-path substring.
|
||||
- `cmdutil.TestFactory(t, config)` for test factories.
|
||||
- `t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir())` to isolate config state.
|
||||
|
||||
|
||||
86
CHANGELOG.md
86
CHANGELOG.md
@@ -2,6 +2,89 @@
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## [v1.0.72] - 2026-07-17
|
||||
|
||||
### Features
|
||||
|
||||
- **slides**: lint table out of canvas
|
||||
- **slides**: report resolved table size mismatches
|
||||
- **approval**: support approval event consumption (#1924)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- **vc**: don't fail +detail for in-progress meetings (#1930)
|
||||
- stabilize drive delete E2E terminal-state checks (#1939)
|
||||
|
||||
### Documentation
|
||||
|
||||
- **slides**: document table dimensions
|
||||
- document base field default values (#1500)
|
||||
- **sheets**: use English placeholder in table-get guidance (#1936)
|
||||
|
||||
### Tests
|
||||
|
||||
- stabilize live e2e auth retries (#1904)
|
||||
- use tri-state wiki node identity in delete verification (#1931)
|
||||
- fix drive cover download retries (#1934)
|
||||
|
||||
## [v1.0.71] - 2026-07-16
|
||||
|
||||
### Features
|
||||
|
||||
- add wiki move-to-drive shortcut (#1869)
|
||||
- **apps**: add role management shortcuts (#1881)
|
||||
- **drive**: add secure label support and clarify comment location API (#1913)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- **base**: improve dashboard shortcut guidance (#1787)
|
||||
|
||||
### Documentation
|
||||
|
||||
- **apps**: add platform SQL authoring guide to the db-execute skill (#1912)
|
||||
|
||||
### Misc
|
||||
|
||||
- add L4 plugin-integration and sidecar-integration CI jobs (#1840)
|
||||
- **drive**: optimize drive +delete workflow (#1909)
|
||||
|
||||
## [v1.0.70] - 2026-07-15
|
||||
|
||||
### Features
|
||||
|
||||
- add minutes permission application shortcut (#1876)
|
||||
- **drive**: support apps in list comments (#1877)
|
||||
- slide style
|
||||
- edit ppt template
|
||||
- **slides**: add sxsd validation to slides lint
|
||||
- **slides**: validate iconpark icon types in slides lint
|
||||
- **slides**: lint before create
|
||||
- **apps**: add automation trigger commands for Miaoda (#1886)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- unify dry-run output contract (#1870)
|
||||
- **skills**: align skill guidance with the typed error contract (#1786)
|
||||
- **slides**: limit slides screenshot page requests
|
||||
- **slides**: detect lark slides text overflow overlap
|
||||
- **vc**: align meeting query scopes by identity (#1850)
|
||||
|
||||
### Documentation
|
||||
|
||||
- clarify task search relevance filters (#1884)
|
||||
- surface minutes permission application in skill description (#1890)
|
||||
- clarify okr progress children (#1861)
|
||||
- **slides**: prefer slides xml-get shortcut
|
||||
- **calendar**: document setting meeting owner via full API (#1903)
|
||||
|
||||
### Refactoring
|
||||
|
||||
- **slides**: streamline create workflow and validate SML namespaces
|
||||
|
||||
### Misc
|
||||
|
||||
- **slides**: address PR review feedback
|
||||
|
||||
## [v1.0.69] - 2026-07-13
|
||||
|
||||
### Features
|
||||
@@ -1469,6 +1552,9 @@ Bundled AI agent skills for intelligent assistance:
|
||||
- Bilingual documentation (English & Chinese).
|
||||
- CI/CD pipelines: linting, testing, coverage reporting, and automated releases.
|
||||
|
||||
[v1.0.72]: https://github.com/larksuite/cli/releases/tag/v1.0.72
|
||||
[v1.0.71]: https://github.com/larksuite/cli/releases/tag/v1.0.71
|
||||
[v1.0.70]: https://github.com/larksuite/cli/releases/tag/v1.0.70
|
||||
[v1.0.69]: https://github.com/larksuite/cli/releases/tag/v1.0.69
|
||||
[v1.0.68]: https://github.com/larksuite/cli/releases/tag/v1.0.68
|
||||
[v1.0.67]: https://github.com/larksuite/cli/releases/tag/v1.0.67
|
||||
|
||||
15
Makefile
15
Makefile
@@ -23,7 +23,7 @@ PREFIX ?= /usr/local
|
||||
TEST_GOARCH := $(or $(GOARCH),$(shell go env GOARCH))
|
||||
RACE_FLAG := $(if $(filter riscv64,$(TEST_GOARCH)),,-race)
|
||||
|
||||
.PHONY: all build vet fmt-check script-test test unit-test integration-test examples-build quality-gate install uninstall clean fetch_meta gitleaks
|
||||
.PHONY: all build vet fmt-check script-test test unit-test integration-test examples-build quality-gate install uninstall clean fetch_meta gitleaks sidecar-test
|
||||
|
||||
all: test
|
||||
|
||||
@@ -51,7 +51,7 @@ script-test:
|
||||
bash scripts/resolve-changed-from.test.sh
|
||||
bash scripts/ci-workflow.test.sh
|
||||
bash scripts/semantic-review-workflow.test.sh
|
||||
$(NODE) --test scripts/e2e_domains.test.js scripts/semantic-review-verify-artifact.test.js scripts/pr-quality-summary.test.js scripts/semantic-review-publish.test.js scripts/ci-quality-summary-publish.test.js
|
||||
$(NODE) --test scripts/e2e_domains.test.js scripts/fetch_e2e_tat.test.js scripts/install.test.js scripts/release-preflight.test.js scripts/release-workflow.test.js scripts/semantic-review-verify-artifact.test.js scripts/pr-quality-summary.test.js scripts/semantic-review-publish.test.js scripts/ci-quality-summary-publish.test.js
|
||||
|
||||
# ./extension/... keeps the public plugin SDK in the default test matrix.
|
||||
unit-test: fetch_meta
|
||||
@@ -64,6 +64,9 @@ examples-build:
|
||||
go build ./extension/platform/examples/audit-observer
|
||||
go build ./extension/platform/examples/readonly-policy
|
||||
|
||||
# ./tests/... includes tests/plugin_e2e, which builds ~20 customer-fork
|
||||
# binaries (~1 min warm; a cold module cache also downloads via GOPROXY).
|
||||
# Deliberate: local `make test` exercises the L4 plugin contract by default.
|
||||
integration-test: build
|
||||
go test -v -count=1 ./tests/...
|
||||
|
||||
@@ -105,6 +108,14 @@ uninstall:
|
||||
clean:
|
||||
rm -f $(BINARY)
|
||||
|
||||
# sidecar-test compiles and runs the authsidecar* build-tagged code that the
|
||||
# default CI matrix never sees (they carry //go:build tags).
|
||||
sidecar-test:
|
||||
go build -tags authsidecar -o /dev/null .
|
||||
go test $(RACE_FLAG) -count=1 -tags authsidecar ./extension/credential/sidecar/ ./extension/transport/sidecar/ ./internal/cmdutil/
|
||||
go test $(RACE_FLAG) -count=1 -tags authsidecar_demo ./sidecar/server-demo/
|
||||
go test $(RACE_FLAG) -count=1 -tags authsidecar ./tests/sidecar_e2e/
|
||||
|
||||
# Run secret-leak checks locally before pushing.
|
||||
# Step 1: check-doc-tokens catches realistic-looking example tokens in reference
|
||||
# docs and asks you to use _EXAMPLE_TOKEN placeholders instead.
|
||||
|
||||
@@ -130,6 +130,13 @@ func buildAPIRequest(opts *APIOptions) (client.RawApiRequest, *cmdutil.FileUploa
|
||||
stdin := opts.Factory.IOStreams.In
|
||||
fileIO := opts.Factory.ResolveFileIO(opts.Ctx)
|
||||
|
||||
if opts.Method == "" {
|
||||
return client.RawApiRequest{}, nil, errs.NewValidationError(errs.SubtypeInvalidArgument,
|
||||
"HTTP method must not be empty").
|
||||
WithHint("pass the verb as the first argument, e.g. lark-cli api GET /open-apis/...").
|
||||
WithParam("<method>")
|
||||
}
|
||||
|
||||
// Validate --file mutual exclusions first.
|
||||
if err := cmdutil.ValidateFileFlag(opts.File, opts.Params, opts.Data, opts.Output, opts.PageAll, opts.Method); err != nil {
|
||||
return client.RawApiRequest{}, nil, err
|
||||
@@ -243,9 +250,9 @@ func apiRun(opts *APIOptions) error {
|
||||
|
||||
if opts.DryRun {
|
||||
if fileMeta != nil {
|
||||
return cmdutil.PrintDryRunWithFile(f.IOStreams.Out, request, config, opts.Format, fileMeta.FieldName, fileMeta.FilePath, fileMeta.FormFields)
|
||||
return cmdutil.PrintDryRunWithFile(request, config, dryRunOutputOptions(f, opts), *fileMeta)
|
||||
}
|
||||
return apiDryRun(f, request, config, opts.Format)
|
||||
return apiDryRun(f, request, config, opts)
|
||||
}
|
||||
// Identity info is now included in the JSON envelope; skip stderr printing.
|
||||
// cmdutil.PrintIdentity(f.IOStreams.ErrOut, opts.As, config, f.IdentityAutoDetected)
|
||||
@@ -297,8 +304,19 @@ func apiRun(opts *APIOptions) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func apiDryRun(f *cmdutil.Factory, request client.RawApiRequest, config *core.CliConfig, format string) error {
|
||||
return cmdutil.PrintDryRun(f.IOStreams.Out, request, config, format)
|
||||
func apiDryRun(f *cmdutil.Factory, request client.RawApiRequest, config *core.CliConfig, opts *APIOptions) error {
|
||||
return cmdutil.PrintDryRun(request, config, dryRunOutputOptions(f, opts))
|
||||
}
|
||||
|
||||
func dryRunOutputOptions(f *cmdutil.Factory, opts *APIOptions) cmdutil.DryRunOutputOptions {
|
||||
return cmdutil.DryRunOutputOptions{
|
||||
Format: opts.Format,
|
||||
JqExpr: opts.JqExpr,
|
||||
CommandPath: opts.Cmd.CommandPath(),
|
||||
Identity: opts.As,
|
||||
Out: f.IOStreams.Out,
|
||||
ErrOut: f.IOStreams.ErrOut,
|
||||
}
|
||||
}
|
||||
|
||||
func apiPaginate(ctx context.Context, ac *client.APIClient, request client.RawApiRequest, format output.Format, jqExpr string, out, errOut io.Writer, commandPath string, pagOpts client.PaginationOptions) error {
|
||||
|
||||
@@ -69,7 +69,7 @@ func TestApiCmd_FlagParsing(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestApiCmd_DryRun(t *testing.T) {
|
||||
f, stdout, _, _ := cmdutil.TestFactory(t, &core.CliConfig{
|
||||
f, stdout, stderr, _ := cmdutil.TestFactory(t, &core.CliConfig{
|
||||
AppID: "test-app", AppSecret: "test-secret", Brand: core.BrandFeishu,
|
||||
})
|
||||
|
||||
@@ -79,12 +79,42 @@ func TestApiCmd_DryRun(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
output := stdout.String()
|
||||
if !strings.Contains(output, "Dry Run") {
|
||||
t.Error("expected dry run output")
|
||||
var got map[string]interface{}
|
||||
if err := json.Unmarshal(stdout.Bytes(), &got); err != nil {
|
||||
t.Fatalf("dry-run stdout is not JSON: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
|
||||
}
|
||||
if !strings.Contains(output, "/open-apis/test") {
|
||||
t.Error("expected path in dry run output")
|
||||
if got["ok"] != true || got["identity"] != "bot" || got["dry_run"] != true {
|
||||
t.Fatalf("unexpected dry-run envelope: %#v", got)
|
||||
}
|
||||
data, ok := got["data"].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("data = %#v, want object", got["data"])
|
||||
}
|
||||
api, ok := data["api"].([]interface{})
|
||||
if !ok || len(api) != 1 {
|
||||
t.Fatalf("api = %#v, want one call", data["api"])
|
||||
}
|
||||
call, ok := api[0].(map[string]interface{})
|
||||
if !ok || call["url"] != "/open-apis/test" {
|
||||
t.Fatalf("api[0] = %#v", api[0])
|
||||
}
|
||||
if strings.Contains(stdout.String(), "=== Dry Run ===") {
|
||||
t.Fatalf("stdout should not contain dry-run banner: %s", stdout.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiCmd_DryRunWithJq(t *testing.T) {
|
||||
f, stdout, _, _ := cmdutil.TestFactory(t, &core.CliConfig{
|
||||
AppID: "test-app", AppSecret: "test-secret", Brand: core.BrandFeishu,
|
||||
})
|
||||
|
||||
cmd := newTestApiCmd(f, nil)
|
||||
cmd.SetArgs([]string{"GET", "/open-apis/test", "--as", "bot", "--dry-run", "--jq", ".data.api[0].url"})
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got := strings.TrimSpace(stdout.String()); got != "/open-apis/test" {
|
||||
t.Fatalf("jq output = %q, want /open-apis/test", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,6 +182,22 @@ func TestApiCmd_MissingArgs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiCmd_EmptyMethodRejected(t *testing.T) {
|
||||
f, _, _, _ := cmdutil.TestFactory(t, &core.CliConfig{
|
||||
AppID: "test-app", AppSecret: "test-secret", Brand: core.BrandFeishu,
|
||||
})
|
||||
|
||||
cmd := newTestApiCmd(f, nil)
|
||||
cmd.SetArgs([]string{"", "/open-apis/test", "--as", "bot", "--dry-run"})
|
||||
err := cmd.Execute()
|
||||
if err == nil {
|
||||
t.Fatal("expected validation error for empty HTTP method")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "method") {
|
||||
t.Fatalf("error should name the method argument, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiCmd_InvalidParamsJSON(t *testing.T) {
|
||||
f, _, _, _ := cmdutil.TestFactory(t, &core.CliConfig{
|
||||
AppID: "test-app", AppSecret: "test-secret", Brand: core.BrandFeishu,
|
||||
@@ -1000,11 +1046,23 @@ func TestApiCmd_DryRunWithFile(t *testing.T) {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
out := stdout.String()
|
||||
if !strings.Contains(out, "image") {
|
||||
t.Errorf("expected dry-run output to mention file field, got: %s", out)
|
||||
var env map[string]interface{}
|
||||
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
|
||||
t.Fatalf("dry-run stdout is not JSON: %v\n%s", err, out)
|
||||
}
|
||||
if !strings.Contains(out, "Dry Run") {
|
||||
t.Errorf("expected dry-run header, got: %s", out)
|
||||
if env["dry_run"] != true {
|
||||
t.Fatalf("dry_run = %#v, want true", env["dry_run"])
|
||||
}
|
||||
data := env["data"].(map[string]interface{})
|
||||
api := data["api"].([]interface{})
|
||||
call := api[0].(map[string]interface{})
|
||||
body := call["body"].(map[string]interface{})
|
||||
file := body["file"].(map[string]interface{})
|
||||
if file["field"] != "image" || file["path"] != tmpFile {
|
||||
t.Fatalf("unexpected file dry-run body: %#v", body)
|
||||
}
|
||||
if strings.Contains(out, "=== Dry Run ===") {
|
||||
t.Fatalf("stdout should not contain dry-run banner: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ import (
|
||||
|
||||
func TestEventLookup_VCMeetingLifecycleKeys(t *testing.T) {
|
||||
for _, key := range []string{
|
||||
"approval.instance.status_changed_v4",
|
||||
"approval.task.status_changed_v4",
|
||||
"vc.meeting.participant_meeting_started_v1",
|
||||
"vc.meeting.participant_meeting_joined_v1",
|
||||
} {
|
||||
@@ -36,6 +38,8 @@ func TestRunList_TextOutput(t *testing.T) {
|
||||
out := stdout.String()
|
||||
for _, want := range []string{
|
||||
"KEY", "AUTH", "PARAMS", "DESCRIPTION",
|
||||
"approval.instance.status_changed_v4",
|
||||
"approval.task.status_changed_v4",
|
||||
"im.message.receive_v1",
|
||||
"im.message.message_read_v1",
|
||||
"task.task.update_user_access_v2",
|
||||
@@ -90,6 +94,8 @@ func TestRunList_JSONOutput(t *testing.T) {
|
||||
t.Fatal("event list JSON missing task.task.update_user_access_v2")
|
||||
}
|
||||
for _, want := range []string{
|
||||
"approval.instance.status_changed_v4",
|
||||
"approval.task.status_changed_v4",
|
||||
"vc.meeting.participant_meeting_started_v1",
|
||||
"vc.meeting.participant_meeting_joined_v1",
|
||||
} {
|
||||
|
||||
@@ -19,6 +19,29 @@ import (
|
||||
_ "github.com/larksuite/cli/events"
|
||||
)
|
||||
|
||||
type approvalSchemaJSONPayload struct {
|
||||
JQRootPath string `json:"jq_root_path"`
|
||||
AuthTypes []string `json:"auth_types"`
|
||||
Scopes []string `json:"scopes"`
|
||||
Params []approvalSchemaJSONParam `json:"params"`
|
||||
ResolvedOutputSchema approvalSchemaJSONResolvedSchema `json:"resolved_output_schema"`
|
||||
}
|
||||
|
||||
type approvalSchemaJSONParam struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Required bool `json:"required"`
|
||||
SubscriptionKey bool `json:"subscription_key"`
|
||||
}
|
||||
|
||||
type approvalSchemaJSONResolvedSchema struct {
|
||||
Properties map[string]approvalSchemaJSONProperty `json:"properties"`
|
||||
}
|
||||
|
||||
type approvalSchemaJSONProperty struct {
|
||||
Format string `json:"format"`
|
||||
}
|
||||
|
||||
func TestRunSchema_ProcessedKey_Text(t *testing.T) {
|
||||
f, stdout, _, _ := cmdutil.TestFactory(t, &core.CliConfig{AppID: "test"})
|
||||
|
||||
@@ -158,6 +181,60 @@ func TestRunSchema_TaskUpdateUserAccessJSON(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunSchema_ApprovalStatusChangedJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
key string
|
||||
scope string
|
||||
}{
|
||||
{"approval.instance.status_changed_v4", "approval:instance:read"},
|
||||
{"approval.task.status_changed_v4", "approval:task:read"},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.key, func(t *testing.T) {
|
||||
t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir())
|
||||
f, stdout, _, _ := cmdutil.TestFactory(t, &core.CliConfig{AppID: "test"})
|
||||
|
||||
if err := runSchema(f, tc.key, true); err != nil {
|
||||
t.Fatalf("runSchema json: %v", err)
|
||||
}
|
||||
|
||||
var payload approvalSchemaJSONPayload
|
||||
if err := json.Unmarshal(stdout.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("output is not valid JSON: %v\n%s", err, stdout.String())
|
||||
}
|
||||
if payload.JQRootPath != "." {
|
||||
t.Errorf("jq_root_path = %v, want .", payload.JQRootPath)
|
||||
}
|
||||
if got := payload.AuthTypes; !reflect.DeepEqual(got, []string{"user"}) {
|
||||
t.Errorf("auth_types = %#v, want user", got)
|
||||
}
|
||||
if got := payload.Scopes; !reflect.DeepEqual(got, []string{tc.scope}) {
|
||||
t.Errorf("scopes = %#v, want %s", got, tc.scope)
|
||||
}
|
||||
if len(payload.Params) != 1 {
|
||||
t.Fatalf("params = %#v, want one subscription_type param", payload.Params)
|
||||
}
|
||||
param := payload.Params[0]
|
||||
if param.Name != "subscription_type" || param.Type != "multi" || param.Required || param.SubscriptionKey {
|
||||
t.Fatalf("subscription_type param = %#v, want optional multi non-subscription-key param", param)
|
||||
}
|
||||
props := payload.ResolvedOutputSchema.Properties
|
||||
for _, field := range []string{"type", "event_id", "timestamp", "approval_code", "instance_code", "status", "operate_time"} {
|
||||
if _, ok := props[field]; !ok {
|
||||
t.Errorf("approval schema missing flat field %q: %+v", field, props)
|
||||
}
|
||||
}
|
||||
if _, ok := props["event"]; ok {
|
||||
t.Errorf("approval Custom schema should be flat, got envelope field event: %+v", props)
|
||||
}
|
||||
if got := props["operate_time"].Format; got != "timestamp_ms" {
|
||||
t.Errorf("operate_time format = %v, want timestamp_ms", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunSchema_JSONOutput_VCMeetingLifecycleKeys(t *testing.T) {
|
||||
for _, key := range []string{
|
||||
"vc.meeting.participant_meeting_started_v1",
|
||||
|
||||
@@ -403,9 +403,9 @@ func serviceMethodRun(opts *ServiceMethodOptions) error {
|
||||
|
||||
if opts.DryRun {
|
||||
if fileMeta != nil {
|
||||
return cmdutil.PrintDryRunWithFile(f.IOStreams.Out, request, config, opts.Format, fileMeta.FieldName, fileMeta.FilePath, fileMeta.FormFields)
|
||||
return cmdutil.PrintDryRunWithFile(request, config, serviceDryRunOutputOptions(f, opts), *fileMeta)
|
||||
}
|
||||
return serviceDryRun(f, request, config, opts.Format)
|
||||
return serviceDryRun(f, request, config, opts)
|
||||
}
|
||||
|
||||
if opts.Method.Risk == cmdutil.RiskHighRiskWrite {
|
||||
@@ -667,8 +667,19 @@ func buildServiceRequest(opts *ServiceMethodOptions) (client.RawApiRequest, *cmd
|
||||
return request, nil, nil
|
||||
}
|
||||
|
||||
func serviceDryRun(f *cmdutil.Factory, request client.RawApiRequest, config *core.CliConfig, format string) error {
|
||||
return cmdutil.PrintDryRun(f.IOStreams.Out, request, config, format)
|
||||
func serviceDryRun(f *cmdutil.Factory, request client.RawApiRequest, config *core.CliConfig, opts *ServiceMethodOptions) error {
|
||||
return cmdutil.PrintDryRun(request, config, serviceDryRunOutputOptions(f, opts))
|
||||
}
|
||||
|
||||
func serviceDryRunOutputOptions(f *cmdutil.Factory, opts *ServiceMethodOptions) cmdutil.DryRunOutputOptions {
|
||||
return cmdutil.DryRunOutputOptions{
|
||||
Format: opts.Format,
|
||||
JqExpr: opts.JqExpr,
|
||||
CommandPath: opts.Cmd.CommandPath(),
|
||||
Identity: opts.As,
|
||||
Out: f.IOStreams.Out,
|
||||
ErrOut: f.IOStreams.ErrOut,
|
||||
}
|
||||
}
|
||||
|
||||
func servicePaginate(ctx context.Context, ac *client.APIClient, request client.RawApiRequest, format output.Format, jqExpr string, out, errOut io.Writer, commandPath string, pagOpts client.PaginationOptions, checkErr func(interface{}, core.Identity) error) error {
|
||||
|
||||
@@ -224,13 +224,39 @@ func TestServiceMethod_DryRun_PathParam(t *testing.T) {
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if !strings.Contains(stdout.String(), tt.wantInURL) {
|
||||
t.Errorf("expected URL containing %q, got:\n%s", tt.wantInURL, stdout.String())
|
||||
var got map[string]interface{}
|
||||
if err := json.Unmarshal(stdout.Bytes(), &got); err != nil {
|
||||
t.Fatalf("dry-run stdout is not JSON: %v\n%s", err, stdout.String())
|
||||
}
|
||||
if got["ok"] != true || got["dry_run"] != true {
|
||||
t.Fatalf("unexpected dry-run envelope: %#v", got)
|
||||
}
|
||||
data := got["data"].(map[string]interface{})
|
||||
api := data["api"].([]interface{})
|
||||
call := api[0].(map[string]interface{})
|
||||
if call["url"] != tt.wantInURL {
|
||||
t.Errorf("url = %q, want %q\nstdout:\n%s", call["url"], tt.wantInURL, stdout.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceMethod_DryRunWithJq(t *testing.T) {
|
||||
f, stdout, _, _ := cmdutil.TestFactory(t, testConfig)
|
||||
cmd := NewCmdServiceMethod(f, driveSpec(), driveMethod("GET", nil), "get", "files", nil)
|
||||
cmd.SetArgs([]string{
|
||||
"--params", `{"file_token":"boxcn123abc"}`,
|
||||
"--dry-run",
|
||||
"--jq", ".data.api[0].url",
|
||||
})
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got, want := strings.TrimSpace(stdout.String()), "/open-apis/drive/v1/files/boxcn123abc/copy"; got != want {
|
||||
t.Fatalf("jq output = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceMethod_PathParamRejectsTraversal(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -318,8 +344,12 @@ func TestServiceMethod_PaginationParamSkippedWithPageAll(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error with --page-all skipping page_size, got: %v", err)
|
||||
}
|
||||
if !strings.Contains(stdout.String(), "Dry Run") {
|
||||
t.Error("expected dry-run output")
|
||||
var got map[string]interface{}
|
||||
if err := json.Unmarshal(stdout.Bytes(), &got); err != nil {
|
||||
t.Fatalf("dry-run stdout is not JSON: %v\n%s", err, stdout.String())
|
||||
}
|
||||
if got["dry_run"] != true {
|
||||
t.Fatalf("dry_run = %#v, want true", got["dry_run"])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1081,11 +1111,23 @@ func TestServiceMethod_FileUpload_DryRun(t *testing.T) {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
out := stdout.String()
|
||||
if !strings.Contains(out, "image") {
|
||||
t.Errorf("expected dry-run output to mention file field, got: %s", out)
|
||||
var env map[string]interface{}
|
||||
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
|
||||
t.Fatalf("dry-run stdout is not JSON: %v\n%s", err, out)
|
||||
}
|
||||
if !strings.Contains(out, "Dry Run") {
|
||||
t.Errorf("expected dry-run header, got: %s", out)
|
||||
if env["dry_run"] != true {
|
||||
t.Fatalf("dry_run = %#v, want true", env["dry_run"])
|
||||
}
|
||||
data := env["data"].(map[string]interface{})
|
||||
api := data["api"].([]interface{})
|
||||
call := api[0].(map[string]interface{})
|
||||
body := call["body"].(map[string]interface{})
|
||||
file := body["file"].(map[string]interface{})
|
||||
if file["field"] != "image" || file["path"] != tmpFile {
|
||||
t.Fatalf("unexpected file dry-run body: %#v", body)
|
||||
}
|
||||
if strings.Contains(out, "=== Dry Run ===") {
|
||||
t.Fatalf("stdout should not contain dry-run banner: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
155
events/approval/preconsume.go
Normal file
155
events/approval/preconsume.go
Normal file
@@ -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)
|
||||
}
|
||||
179
events/approval/register.go
Normal file
179
events/approval/register.go
Normal file
@@ -0,0 +1,179 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// Package approval registers Approval-domain EventKeys.
|
||||
package approval
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
|
||||
"github.com/larksuite/cli/internal/event"
|
||||
)
|
||||
|
||||
const (
|
||||
eventTypeApprovalInstanceStatusChangedV4 = "approval.instance.status_changed_v4"
|
||||
eventTypeApprovalTaskStatusChangedV4 = "approval.task.status_changed_v4"
|
||||
|
||||
pathApprovalInstancesSubscription = "/open-apis/approval/v4/instances/subscription"
|
||||
pathApprovalTasksSubscription = "/open-apis/approval/v4/tasks/subscription"
|
||||
|
||||
approvalSubscriptionTypeInvolved = "INVOLVED_APPROVAL"
|
||||
approvalSubscriptionTypeManaged = "MANAGED_APPROVAL"
|
||||
)
|
||||
|
||||
var approvalAllSubscriptionTypes = []string{
|
||||
approvalSubscriptionTypeInvolved,
|
||||
approvalSubscriptionTypeManaged,
|
||||
}
|
||||
|
||||
// Keys returns all Approval-domain EventKey definitions.
|
||||
func Keys() []event.KeyDefinition {
|
||||
return []event.KeyDefinition{
|
||||
{
|
||||
Key: eventTypeApprovalInstanceStatusChangedV4,
|
||||
DisplayName: "Approval instance status changed",
|
||||
Description: "Triggered after an approval instance status becomes visible to the requester or approval participants",
|
||||
EventType: eventTypeApprovalInstanceStatusChangedV4,
|
||||
Params: approvalSubscriptionParams(),
|
||||
Schema: event.SchemaDef{
|
||||
Custom: &event.SchemaSpec{Type: reflect.TypeOf(ApprovalInstanceStatusChangedV4Output{})},
|
||||
},
|
||||
Process: processApprovalInstanceStatusChanged,
|
||||
PreConsume: approvalSubscriptionPreConsume(approvalSubscriptionConfig{
|
||||
eventType: eventTypeApprovalInstanceStatusChangedV4,
|
||||
subscribePath: pathApprovalInstancesSubscription,
|
||||
}),
|
||||
Scopes: []string{"approval:instance:read"},
|
||||
AuthTypes: []string{
|
||||
"user",
|
||||
},
|
||||
RequiredConsoleEvents: []string{eventTypeApprovalInstanceStatusChangedV4},
|
||||
},
|
||||
{
|
||||
Key: eventTypeApprovalTaskStatusChangedV4,
|
||||
DisplayName: "Approval task status changed",
|
||||
Description: "Triggered after an approval task status becomes visible to the requester or task approver",
|
||||
EventType: eventTypeApprovalTaskStatusChangedV4,
|
||||
Params: approvalSubscriptionParams(),
|
||||
Schema: event.SchemaDef{
|
||||
Custom: &event.SchemaSpec{Type: reflect.TypeOf(ApprovalTaskStatusChangedV4Output{})},
|
||||
},
|
||||
Process: processApprovalTaskStatusChanged,
|
||||
PreConsume: approvalSubscriptionPreConsume(approvalSubscriptionConfig{
|
||||
eventType: eventTypeApprovalTaskStatusChangedV4,
|
||||
subscribePath: pathApprovalTasksSubscription,
|
||||
}),
|
||||
Scopes: []string{"approval:task:read"},
|
||||
AuthTypes: []string{
|
||||
"user",
|
||||
},
|
||||
RequiredConsoleEvents: []string{eventTypeApprovalTaskStatusChangedV4},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func approvalSubscriptionParams() []event.ParamDef {
|
||||
return []event.ParamDef{
|
||||
{
|
||||
Name: "subscription_type",
|
||||
Type: event.ParamMulti,
|
||||
Description: "Approval subscription relation type(s) to register for the current authorized user. Omit to register both involved and managed approval relations.",
|
||||
Values: []event.ParamValue{
|
||||
{
|
||||
Value: approvalSubscriptionTypeInvolved,
|
||||
Desc: "Receive events where the current user is the approval requester or approver.",
|
||||
},
|
||||
{
|
||||
Value: approvalSubscriptionTypeManaged,
|
||||
Desc: "Receive events under approval definitions managed by the current user.",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func processApprovalInstanceStatusChanged(_ context.Context, _ event.APIClient, raw *event.RawEvent, _ map[string]string) (json.RawMessage, error) {
|
||||
if raw == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var envelope struct {
|
||||
Header struct {
|
||||
EventID string `json:"event_id"`
|
||||
EventType string `json:"event_type"`
|
||||
CreateTime string `json:"create_time"`
|
||||
} `json:"header"`
|
||||
Event struct {
|
||||
ApprovalCode string `json:"approval_code"`
|
||||
InstanceCode string `json:"instance_code"`
|
||||
ExternalID string `json:"external_id"`
|
||||
Status string `json:"status"`
|
||||
OperateTime string `json:"operate_time"`
|
||||
StartUser *ApprovalUserID `json:"start_user"`
|
||||
} `json:"event"`
|
||||
}
|
||||
if err := json.Unmarshal(raw.Payload, &envelope); err != nil {
|
||||
return raw.Payload, nil //nolint:nilerr // passthrough on malformed payload so consumers still see the event
|
||||
}
|
||||
|
||||
out := &ApprovalInstanceStatusChangedV4Output{
|
||||
Type: envelope.Header.EventType,
|
||||
EventID: envelope.Header.EventID,
|
||||
Timestamp: envelope.Header.CreateTime,
|
||||
ApprovalCode: envelope.Event.ApprovalCode,
|
||||
InstanceCode: envelope.Event.InstanceCode,
|
||||
ExternalID: envelope.Event.ExternalID,
|
||||
Status: envelope.Event.Status,
|
||||
OperateTime: envelope.Event.OperateTime,
|
||||
StartUser: envelope.Event.StartUser,
|
||||
}
|
||||
if out.Type == "" {
|
||||
out.Type = raw.EventType
|
||||
}
|
||||
return json.Marshal(out)
|
||||
}
|
||||
|
||||
func processApprovalTaskStatusChanged(_ context.Context, _ event.APIClient, raw *event.RawEvent, _ map[string]string) (json.RawMessage, error) {
|
||||
if raw == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var envelope struct {
|
||||
Header struct {
|
||||
EventID string `json:"event_id"`
|
||||
EventType string `json:"event_type"`
|
||||
CreateTime string `json:"create_time"`
|
||||
} `json:"header"`
|
||||
Event struct {
|
||||
ApprovalCode string `json:"approval_code"`
|
||||
InstanceCode string `json:"instance_code"`
|
||||
TaskID string `json:"task_id"`
|
||||
ExternalID string `json:"external_id"`
|
||||
TaskExternalID string `json:"task_external_id"`
|
||||
AssignedUser *ApprovalUserID `json:"assigned_user"`
|
||||
Status string `json:"status"`
|
||||
OperateTime string `json:"operate_time"`
|
||||
} `json:"event"`
|
||||
}
|
||||
if err := json.Unmarshal(raw.Payload, &envelope); err != nil {
|
||||
return raw.Payload, nil //nolint:nilerr // passthrough on malformed payload so consumers still see the event
|
||||
}
|
||||
|
||||
out := &ApprovalTaskStatusChangedV4Output{
|
||||
Type: envelope.Header.EventType,
|
||||
EventID: envelope.Header.EventID,
|
||||
Timestamp: envelope.Header.CreateTime,
|
||||
ApprovalCode: envelope.Event.ApprovalCode,
|
||||
InstanceCode: envelope.Event.InstanceCode,
|
||||
TaskID: envelope.Event.TaskID,
|
||||
ExternalID: envelope.Event.ExternalID,
|
||||
TaskExternalID: envelope.Event.TaskExternalID,
|
||||
AssignedUser: envelope.Event.AssignedUser,
|
||||
Status: envelope.Event.Status,
|
||||
OperateTime: envelope.Event.OperateTime,
|
||||
}
|
||||
if out.Type == "" {
|
||||
out.Type = raw.EventType
|
||||
}
|
||||
return json.Marshal(out)
|
||||
}
|
||||
654
events/approval/register_test.go
Normal file
654
events/approval/register_test.go
Normal file
@@ -0,0 +1,654 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package approval
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/internal/event"
|
||||
"github.com/larksuite/cli/internal/event/schemas"
|
||||
)
|
||||
|
||||
type recordedCall struct {
|
||||
method string
|
||||
path string
|
||||
body interface{}
|
||||
}
|
||||
|
||||
type fakeAPIClient struct {
|
||||
calls []recordedCall
|
||||
err error
|
||||
errOnCall int
|
||||
}
|
||||
|
||||
func (f *fakeAPIClient) CallAPI(_ context.Context, method, path string, body interface{}) (json.RawMessage, error) {
|
||||
f.calls = append(f.calls, recordedCall{method: method, path: path, body: body})
|
||||
if f.err != nil && (f.errOnCall == 0 || f.errOnCall == len(f.calls)) {
|
||||
return nil, f.err
|
||||
}
|
||||
return json.RawMessage(`{}`), nil
|
||||
}
|
||||
|
||||
func TestKeysApprovalMetadata(t *testing.T) {
|
||||
keys := Keys()
|
||||
if len(keys) != 2 {
|
||||
t.Fatalf("len(Keys()) = %d, want 2", len(keys))
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
key string
|
||||
scope string
|
||||
schemaType reflect.Type
|
||||
subscribe string
|
||||
}{
|
||||
{
|
||||
key: eventTypeApprovalInstanceStatusChangedV4,
|
||||
scope: "approval:instance:read",
|
||||
schemaType: reflect.TypeOf(ApprovalInstanceStatusChangedV4Output{}),
|
||||
subscribe: pathApprovalInstancesSubscription,
|
||||
},
|
||||
{
|
||||
key: eventTypeApprovalTaskStatusChangedV4,
|
||||
scope: "approval:task:read",
|
||||
schemaType: reflect.TypeOf(ApprovalTaskStatusChangedV4Output{}),
|
||||
subscribe: pathApprovalTasksSubscription,
|
||||
},
|
||||
}
|
||||
|
||||
byKey := make(map[string]event.KeyDefinition, len(keys))
|
||||
for _, def := range keys {
|
||||
byKey[def.Key] = def
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.key, func(t *testing.T) {
|
||||
def, ok := byKey[tc.key]
|
||||
if !ok {
|
||||
t.Fatalf("missing key %s", tc.key)
|
||||
}
|
||||
if def.EventType != tc.key {
|
||||
t.Errorf("EventType = %q, want %q", def.EventType, tc.key)
|
||||
}
|
||||
if def.Schema.Custom == nil || def.Schema.Custom.Type != tc.schemaType {
|
||||
t.Fatalf("Custom schema Type = %v, want %v", def.Schema.Custom, tc.schemaType)
|
||||
}
|
||||
if def.Schema.Native != nil {
|
||||
t.Fatal("approval events must use Custom schema while SDK event types are not exported")
|
||||
}
|
||||
if def.Process == nil {
|
||||
t.Fatal("Process must flatten raw V2 envelopes")
|
||||
}
|
||||
if def.PreConsume == nil {
|
||||
t.Fatal("PreConsume must subscribe approval user-auth events")
|
||||
}
|
||||
if !reflect.DeepEqual(def.Scopes, []string{tc.scope}) {
|
||||
t.Errorf("Scopes = %#v, want %q", def.Scopes, tc.scope)
|
||||
}
|
||||
if !reflect.DeepEqual(def.AuthTypes, []string{"user"}) {
|
||||
t.Errorf("AuthTypes = %#v, want user", def.AuthTypes)
|
||||
}
|
||||
if !reflect.DeepEqual(def.RequiredConsoleEvents, []string{tc.key}) {
|
||||
t.Errorf("RequiredConsoleEvents = %#v, want %q", def.RequiredConsoleEvents, tc.key)
|
||||
}
|
||||
assertSubscriptionParam(t, def.Params)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func assertSubscriptionParam(t *testing.T, params []event.ParamDef) {
|
||||
t.Helper()
|
||||
if len(params) != 1 {
|
||||
t.Fatalf("len(params) = %d, want 1", len(params))
|
||||
}
|
||||
p := params[0]
|
||||
if p.Name != "subscription_type" || p.Type != event.ParamMulti || p.Required || p.SubscriptionKey {
|
||||
t.Fatalf("subscription_type param = %+v, want optional multi non-subscription-key param", p)
|
||||
}
|
||||
got := map[string]string{}
|
||||
for _, v := range p.Values {
|
||||
got[v.Value] = v.Desc
|
||||
}
|
||||
for _, want := range []string{approvalSubscriptionTypeInvolved, approvalSubscriptionTypeManaged} {
|
||||
if got[want] == "" {
|
||||
t.Errorf("subscription_type value %q missing or empty desc; values=%+v", want, p.Values)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type reflectedApprovalSchema struct {
|
||||
Properties map[string]reflectedApprovalSchemaProperty `json:"properties"`
|
||||
}
|
||||
|
||||
type reflectedApprovalSchemaProperty struct {
|
||||
Format string `json:"format"`
|
||||
Enum []string `json:"enum"`
|
||||
Properties map[string]reflectedApprovalSchemaProperty `json:"properties"`
|
||||
}
|
||||
|
||||
func TestApprovalSchemasAnnotations(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
schemaType reflect.Type
|
||||
eventType string
|
||||
statusValues []string
|
||||
userField string
|
||||
}{
|
||||
{
|
||||
name: "instance",
|
||||
schemaType: reflect.TypeOf(ApprovalInstanceStatusChangedV4Output{}),
|
||||
eventType: eventTypeApprovalInstanceStatusChangedV4,
|
||||
statusValues: []string{"PENDING", "APPROVED", "REJECTED", "CANCELED", "DELETED", "REVERTED", "OVERTIME_CLOSE", "OVERTIME_RECOVER"},
|
||||
userField: "start_user",
|
||||
},
|
||||
{
|
||||
name: "task",
|
||||
schemaType: reflect.TypeOf(ApprovalTaskStatusChangedV4Output{}),
|
||||
eventType: eventTypeApprovalTaskStatusChangedV4,
|
||||
statusValues: []string{"REVERTED", "PENDING", "APPROVED", "REJECTED", "TRANSFERRED", "ROLLBACK", "DONE", "OVERTIME_CLOSE", "OVERTIME_RECOVER"},
|
||||
userField: "assigned_user",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var schema reflectedApprovalSchema
|
||||
if err := json.Unmarshal(schemas.FromType(tc.schemaType), &schema); err != nil {
|
||||
t.Fatalf("unmarshal schema: %v", err)
|
||||
}
|
||||
props := schema.Properties
|
||||
eventTypeEnum := props["type"].Enum
|
||||
if len(eventTypeEnum) != 1 || eventTypeEnum[0] != tc.eventType {
|
||||
t.Fatalf("type enum = %v, want %s", eventTypeEnum, tc.eventType)
|
||||
}
|
||||
if got := props["timestamp"].Format; got != "timestamp_ms" {
|
||||
t.Errorf("timestamp format = %v, want timestamp_ms", got)
|
||||
}
|
||||
assertEnumContains(t, props["status"].Enum, tc.statusValues)
|
||||
if got := props["operate_time"].Format; got != "timestamp_ms" {
|
||||
t.Errorf("event.operate_time format = %v, want timestamp_ms", got)
|
||||
}
|
||||
|
||||
userProps := props[tc.userField].Properties
|
||||
if got := userProps["open_id"].Format; got != "open_id" {
|
||||
t.Errorf("%s.open_id format = %v, want open_id", tc.userField, got)
|
||||
}
|
||||
if got := userProps["union_id"].Format; got != "union_id" {
|
||||
t.Errorf("%s.union_id format = %v, want union_id", tc.userField, got)
|
||||
}
|
||||
if got := userProps["user_id"].Format; got != "user_id" {
|
||||
t.Errorf("%s.user_id format = %v, want user_id", tc.userField, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func assertEnumContains(t *testing.T, raw []string, wants []string) {
|
||||
t.Helper()
|
||||
got := make(map[string]bool, len(raw))
|
||||
for _, v := range raw {
|
||||
got[v] = true
|
||||
}
|
||||
for _, want := range wants {
|
||||
if !got[want] {
|
||||
t.Errorf("enum missing %q; enum=%v", want, raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApprovalPreConsumeRegistersSubscriptionTypesWithoutCleanup(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
eventType string
|
||||
subscribePath string
|
||||
params map[string]string
|
||||
wantTypes []string
|
||||
}{
|
||||
{
|
||||
name: "instance omitted subscription_type registers both",
|
||||
eventType: eventTypeApprovalInstanceStatusChangedV4,
|
||||
subscribePath: pathApprovalInstancesSubscription,
|
||||
wantTypes: []string{
|
||||
approvalSubscriptionTypeInvolved,
|
||||
approvalSubscriptionTypeManaged,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "task explicit single managed",
|
||||
eventType: eventTypeApprovalTaskStatusChangedV4,
|
||||
subscribePath: pathApprovalTasksSubscription,
|
||||
params: map[string]string{"subscription_type": approvalSubscriptionTypeManaged},
|
||||
wantTypes: []string{approvalSubscriptionTypeManaged},
|
||||
},
|
||||
{
|
||||
name: "task comma separated multi canonicalizes and deduplicates",
|
||||
eventType: eventTypeApprovalTaskStatusChangedV4,
|
||||
subscribePath: pathApprovalTasksSubscription,
|
||||
params: map[string]string{
|
||||
"subscription_type": approvalSubscriptionTypeManaged + "," + approvalSubscriptionTypeInvolved + "," + approvalSubscriptionTypeManaged,
|
||||
},
|
||||
wantTypes: []string{
|
||||
approvalSubscriptionTypeInvolved,
|
||||
approvalSubscriptionTypeManaged,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "instance json array multi",
|
||||
eventType: eventTypeApprovalInstanceStatusChangedV4,
|
||||
subscribePath: pathApprovalInstancesSubscription,
|
||||
params: map[string]string{
|
||||
"subscription_type": `["MANAGED_APPROVAL","INVOLVED_APPROVAL"]`,
|
||||
},
|
||||
wantTypes: []string{
|
||||
approvalSubscriptionTypeInvolved,
|
||||
approvalSubscriptionTypeManaged,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
pc := approvalSubscriptionPreConsume(approvalSubscriptionConfig{
|
||||
eventType: approvalEventType(tc.eventType),
|
||||
subscribePath: approvalSubscriptionPath(tc.subscribePath),
|
||||
})
|
||||
rt := &fakeAPIClient{}
|
||||
cleanup, err := pc(context.Background(), rt, tc.params)
|
||||
if err != nil {
|
||||
t.Fatalf("PreConsume returned error: %v", err)
|
||||
}
|
||||
if cleanup != nil {
|
||||
t.Fatal("cleanup must be nil; approval consume must not unsubscribe on exit")
|
||||
}
|
||||
assertSubscriptionCalls(t, rt.calls, tc.subscribePath, tc.wantTypes)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func assertSubscriptionCalls(t *testing.T, got []recordedCall, wantPath string, wantTypes []string) {
|
||||
t.Helper()
|
||||
if len(got) != len(wantTypes) {
|
||||
t.Fatalf("calls after pre-consume = %d, want %d; calls=%+v", len(got), len(wantTypes), got)
|
||||
}
|
||||
for i, wantType := range wantTypes {
|
||||
assertCall(t, got[i], "POST", wantPath, map[string]string{"subscription_type": wantType})
|
||||
}
|
||||
}
|
||||
|
||||
func assertCall(t *testing.T, got recordedCall, wantMethod, wantPath string, wantBody interface{}) {
|
||||
t.Helper()
|
||||
if got.method != wantMethod {
|
||||
t.Errorf("method = %q, want %q", got.method, wantMethod)
|
||||
}
|
||||
if got.path != wantPath {
|
||||
t.Errorf("path = %q, want %q", got.path, wantPath)
|
||||
}
|
||||
if !reflect.DeepEqual(got.body, wantBody) {
|
||||
t.Errorf("body = %#v, want %#v", got.body, wantBody)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApprovalPreConsumeValidationErrors(t *testing.T) {
|
||||
t.Run("nil runtime", func(t *testing.T) {
|
||||
pc := approvalSubscriptionPreConsume(approvalSubscriptionConfig{
|
||||
eventType: eventTypeApprovalInstanceStatusChangedV4,
|
||||
})
|
||||
_, err := pc(context.Background(), nil, map[string]string{"subscription_type": approvalSubscriptionTypeInvolved})
|
||||
if err == nil {
|
||||
t.Fatal("expected nil runtime error")
|
||||
}
|
||||
p, ok := errs.ProblemOf(err)
|
||||
if !ok || p.Category != errs.CategoryInternal {
|
||||
t.Fatalf("err = %T/%v, want typed internal error", err, err)
|
||||
}
|
||||
})
|
||||
|
||||
for _, raw := range []string{"BAD", "[]", `["INVOLVED_APPROVAL",3]`} {
|
||||
t.Run("invalid subscription type "+raw, func(t *testing.T) {
|
||||
pc := approvalSubscriptionPreConsume(approvalSubscriptionConfig{
|
||||
eventType: eventTypeApprovalInstanceStatusChangedV4,
|
||||
})
|
||||
cleanup, err := pc(context.Background(), &fakeAPIClient{}, map[string]string{"subscription_type": raw})
|
||||
if err == nil {
|
||||
t.Fatal("expected invalid subscription_type error")
|
||||
}
|
||||
if cleanup != nil {
|
||||
t.Fatal("cleanup must be nil on validation error")
|
||||
}
|
||||
var ve *errs.ValidationError
|
||||
if !errors.As(err, &ve) {
|
||||
t.Fatalf("err = %T/%v, want *errs.ValidationError", err, err)
|
||||
}
|
||||
if ve.Subtype != errs.SubtypeInvalidArgument || ve.Param != "--param" {
|
||||
t.Errorf("subtype/param = %s/%q, want invalid_argument/--param", ve.Subtype, ve.Param)
|
||||
}
|
||||
if ve.Hint == "" {
|
||||
t.Error("invalid subscription_type should carry a hint")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("partial registration failure reports registered and failed relation types", func(t *testing.T) {
|
||||
upstream := errs.NewAPIError(errs.SubtypeServerError, "approval subscription API failed")
|
||||
rt := &fakeAPIClient{err: upstream, errOnCall: 2}
|
||||
pc := approvalSubscriptionPreConsume(approvalSubscriptionConfig{
|
||||
eventType: eventTypeApprovalTaskStatusChangedV4,
|
||||
subscribePath: pathApprovalTasksSubscription,
|
||||
})
|
||||
|
||||
cleanup, err := pc(context.Background(), rt, map[string]string{})
|
||||
if err == nil {
|
||||
t.Fatal("expected partial registration error")
|
||||
}
|
||||
if cleanup != nil {
|
||||
t.Fatal("cleanup must be nil on registration error")
|
||||
}
|
||||
assertSubscriptionCalls(t, rt.calls, pathApprovalTasksSubscription, []string{
|
||||
approvalSubscriptionTypeInvolved,
|
||||
approvalSubscriptionTypeManaged,
|
||||
})
|
||||
p, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
t.Fatalf("err = %T/%v, want typed error", err, err)
|
||||
}
|
||||
if p.Category != errs.CategoryAPI || p.Subtype != errs.SubtypeServerError {
|
||||
t.Fatalf("category/subtype = %s/%s, want api/server_error", p.Category, p.Subtype)
|
||||
}
|
||||
for _, want := range []string{
|
||||
"registered subscription_type(s) [INVOLVED_APPROVAL]",
|
||||
"failed subscription_type MANAGED_APPROVAL",
|
||||
} {
|
||||
if !strings.Contains(p.Message, want) {
|
||||
t.Errorf("partial error message missing %q: %q", want, p.Message)
|
||||
}
|
||||
}
|
||||
for _, want := range []string{
|
||||
"already registered",
|
||||
"--param subscription_type=MANAGED_APPROVAL",
|
||||
} {
|
||||
if !strings.Contains(p.Hint, want) {
|
||||
t.Errorf("partial error hint missing %q: %q", want, p.Hint)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestApprovalSubscriptionRegistrationErrorVariants(t *testing.T) {
|
||||
t.Run("nil error", func(t *testing.T) {
|
||||
if err := approvalSubscriptionRegistrationError(eventTypeApprovalTaskStatusChangedV4, nil, approvalSubscriptionTypeInvolved, nil); err != nil {
|
||||
t.Fatalf("nil cause returned error: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("typed error with existing hint and empty message", func(t *testing.T) {
|
||||
upstream := errs.NewAPIError(errs.SubtypeServerError, "").WithHint("retry later")
|
||||
err := approvalSubscriptionRegistrationError(
|
||||
eventTypeApprovalTaskStatusChangedV4,
|
||||
nil,
|
||||
approvalSubscriptionTypeInvolved,
|
||||
upstream,
|
||||
)
|
||||
if err != upstream {
|
||||
t.Fatalf("typed error should be annotated in place; got %T/%v", err, err)
|
||||
}
|
||||
p, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
t.Fatalf("err = %T/%v, want typed error", err, err)
|
||||
}
|
||||
if !strings.Contains(p.Message, "failed subscription_type INVOLVED_APPROVAL") {
|
||||
t.Errorf("message missing failed relation: %q", p.Message)
|
||||
}
|
||||
for _, want := range []string{"retry later", "no approval subscription relation was registered"} {
|
||||
if !strings.Contains(p.Hint, want) {
|
||||
t.Errorf("hint missing %q: %q", want, p.Hint)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("untyped error is wrapped with retry context", func(t *testing.T) {
|
||||
cause := errors.New("transport closed")
|
||||
err := approvalSubscriptionRegistrationError(
|
||||
eventTypeApprovalTaskStatusChangedV4,
|
||||
nil,
|
||||
approvalSubscriptionTypeInvolved,
|
||||
cause,
|
||||
)
|
||||
if !errors.Is(err, cause) {
|
||||
t.Fatalf("wrapped error should preserve cause; got %T/%v", err, err)
|
||||
}
|
||||
p, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
t.Fatalf("err = %T/%v, want typed error", err, err)
|
||||
}
|
||||
if p.Category != errs.CategoryInternal || p.Subtype != errs.SubtypeSDKError {
|
||||
t.Fatalf("category/subtype = %s/%s, want internal/sdk_error", p.Category, p.Subtype)
|
||||
}
|
||||
if !strings.Contains(p.Hint, "no approval subscription relation was registered") {
|
||||
t.Errorf("hint missing no-registration context: %q", p.Hint)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcessApprovalInstanceStatusChanged(t *testing.T) {
|
||||
out := runApprovalInstanceStatusChanged(t, `{
|
||||
"schema": "2.0",
|
||||
"header": {
|
||||
"event_id": "evt_approval_instance_001",
|
||||
"event_type": "approval.instance.status_changed_v4",
|
||||
"create_time": "1710000000000"
|
||||
},
|
||||
"event": {
|
||||
"approval_code": "approval_code_001",
|
||||
"instance_code": "instance_code_001",
|
||||
"external_id": "external_001",
|
||||
"status": "PENDING",
|
||||
"operate_time": "1666079207003",
|
||||
"start_user": {
|
||||
"open_id": "ou_start",
|
||||
"union_id": "on_start",
|
||||
"user_id": "user_start"
|
||||
}
|
||||
}
|
||||
}`)
|
||||
|
||||
if out.Type != eventTypeApprovalInstanceStatusChangedV4 {
|
||||
t.Errorf("Type = %q, want %q", out.Type, eventTypeApprovalInstanceStatusChangedV4)
|
||||
}
|
||||
if out.EventID != "evt_approval_instance_001" || out.Timestamp != "1710000000000" {
|
||||
t.Errorf("EventID/Timestamp = %q/%q", out.EventID, out.Timestamp)
|
||||
}
|
||||
if out.ApprovalCode != "approval_code_001" || out.InstanceCode != "instance_code_001" {
|
||||
t.Errorf("approval/instance code = %q/%q", out.ApprovalCode, out.InstanceCode)
|
||||
}
|
||||
if out.ExternalID != "external_001" || out.Status != "PENDING" || out.OperateTime != "1666079207003" {
|
||||
t.Errorf("external/status/operate_time = %q/%q/%q", out.ExternalID, out.Status, out.OperateTime)
|
||||
}
|
||||
if out.StartUser == nil || out.StartUser.OpenID != "ou_start" || out.StartUser.UnionID != "on_start" || out.StartUser.UserID != "user_start" {
|
||||
t.Fatalf("StartUser = %+v, want full user ids", out.StartUser)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessApprovalTaskStatusChanged(t *testing.T) {
|
||||
out := runApprovalTaskStatusChanged(t, `{
|
||||
"schema": "2.0",
|
||||
"header": {
|
||||
"event_id": "evt_approval_task_001",
|
||||
"event_type": "approval.task.status_changed_v4",
|
||||
"create_time": "1710000000001"
|
||||
},
|
||||
"event": {
|
||||
"approval_code": "approval_code_002",
|
||||
"instance_code": "instance_code_002",
|
||||
"task_id": "task_001",
|
||||
"external_id": "external_002",
|
||||
"task_external_id": "task_external_001",
|
||||
"status": "APPROVED",
|
||||
"operate_time": "1666079207004",
|
||||
"assigned_user": {
|
||||
"open_id": "ou_assignee",
|
||||
"union_id": "on_assignee",
|
||||
"user_id": "user_assignee"
|
||||
}
|
||||
}
|
||||
}`)
|
||||
|
||||
if out.Type != eventTypeApprovalTaskStatusChangedV4 {
|
||||
t.Errorf("Type = %q, want %q", out.Type, eventTypeApprovalTaskStatusChangedV4)
|
||||
}
|
||||
if out.EventID != "evt_approval_task_001" || out.Timestamp != "1710000000001" {
|
||||
t.Errorf("EventID/Timestamp = %q/%q", out.EventID, out.Timestamp)
|
||||
}
|
||||
if out.ApprovalCode != "approval_code_002" || out.InstanceCode != "instance_code_002" || out.TaskID != "task_001" {
|
||||
t.Errorf("approval/instance/task = %q/%q/%q", out.ApprovalCode, out.InstanceCode, out.TaskID)
|
||||
}
|
||||
if out.ExternalID != "external_002" || out.TaskExternalID != "task_external_001" || out.Status != "APPROVED" || out.OperateTime != "1666079207004" {
|
||||
t.Errorf("external/task_external/status/operate_time = %q/%q/%q/%q", out.ExternalID, out.TaskExternalID, out.Status, out.OperateTime)
|
||||
}
|
||||
if out.AssignedUser == nil || out.AssignedUser.OpenID != "ou_assignee" || out.AssignedUser.UnionID != "on_assignee" || out.AssignedUser.UserID != "user_assignee" {
|
||||
t.Fatalf("AssignedUser = %+v, want full user ids", out.AssignedUser)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessApprovalStatusChangedUsesRawEventTypeFallback(t *testing.T) {
|
||||
instance := runApprovalInstanceStatusChanged(t, `{
|
||||
"schema": "2.0",
|
||||
"header": {
|
||||
"event_id": "evt_approval_instance_fallback",
|
||||
"create_time": "1710000000002"
|
||||
},
|
||||
"event": {
|
||||
"approval_code": "approval_code_fallback",
|
||||
"instance_code": "instance_code_fallback",
|
||||
"status": "APPROVED",
|
||||
"operate_time": "1666079207005"
|
||||
}
|
||||
}`)
|
||||
if instance.Type != eventTypeApprovalInstanceStatusChangedV4 {
|
||||
t.Errorf("instance Type fallback = %q, want %q", instance.Type, eventTypeApprovalInstanceStatusChangedV4)
|
||||
}
|
||||
|
||||
task := runApprovalTaskStatusChanged(t, `{
|
||||
"schema": "2.0",
|
||||
"header": {
|
||||
"event_id": "evt_approval_task_fallback",
|
||||
"create_time": "1710000000003"
|
||||
},
|
||||
"event": {
|
||||
"approval_code": "approval_code_fallback",
|
||||
"instance_code": "instance_code_fallback",
|
||||
"task_id": "task_fallback",
|
||||
"status": "DONE",
|
||||
"operate_time": "1666079207006"
|
||||
}
|
||||
}`)
|
||||
if task.Type != eventTypeApprovalTaskStatusChangedV4 {
|
||||
t.Errorf("task Type fallback = %q, want %q", task.Type, eventTypeApprovalTaskStatusChangedV4)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessApprovalStatusChangedMalformedPayloadPassthrough(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
eventType string
|
||||
process event.ProcessFunc
|
||||
}{
|
||||
{"instance", eventTypeApprovalInstanceStatusChangedV4, processApprovalInstanceStatusChanged},
|
||||
{"task", eventTypeApprovalTaskStatusChangedV4, processApprovalTaskStatusChanged},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
raw := &event.RawEvent{
|
||||
EventType: tc.eventType,
|
||||
Payload: json.RawMessage(`not json`),
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
got, err := tc.process(context.Background(), nil, raw, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Process should swallow parse errors, got %v", err)
|
||||
}
|
||||
if string(got) != "not json" {
|
||||
t.Errorf("malformed fallback output = %q, want original bytes", string(got))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessApprovalStatusChangedNilRaw(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
process event.ProcessFunc
|
||||
}{
|
||||
{"instance", processApprovalInstanceStatusChanged},
|
||||
{"task", processApprovalTaskStatusChanged},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := tc.process(context.Background(), nil, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Process nil raw returned error: %v", err)
|
||||
}
|
||||
if got != nil {
|
||||
t.Fatalf("Process nil raw output = %s, want nil", string(got))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func runApprovalInstanceStatusChanged(t *testing.T, payload string) ApprovalInstanceStatusChangedV4Output {
|
||||
t.Helper()
|
||||
raw := &event.RawEvent{
|
||||
EventType: eventTypeApprovalInstanceStatusChangedV4,
|
||||
Payload: json.RawMessage(payload),
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
got, err := processApprovalInstanceStatusChanged(context.Background(), nil, raw, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Process returned error: %v", err)
|
||||
}
|
||||
var out ApprovalInstanceStatusChangedV4Output
|
||||
if err := json.Unmarshal(got, &out); err != nil {
|
||||
t.Fatalf("Process output is not valid instance JSON: %v\nraw=%s", err, string(got))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func runApprovalTaskStatusChanged(t *testing.T, payload string) ApprovalTaskStatusChangedV4Output {
|
||||
t.Helper()
|
||||
raw := &event.RawEvent{
|
||||
EventType: eventTypeApprovalTaskStatusChangedV4,
|
||||
Payload: json.RawMessage(payload),
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
got, err := processApprovalTaskStatusChanged(context.Background(), nil, raw, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Process returned error: %v", err)
|
||||
}
|
||||
var out ApprovalTaskStatusChangedV4Output
|
||||
if err := json.Unmarshal(got, &out); err != nil {
|
||||
t.Fatalf("Process output is not valid task JSON: %v\nraw=%s", err, string(got))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestApprovalKeysRegisterCleanly(t *testing.T) {
|
||||
for _, key := range []string{eventTypeApprovalInstanceStatusChangedV4, eventTypeApprovalTaskStatusChangedV4} {
|
||||
event.UnregisterKeyForTest(key)
|
||||
t.Cleanup(func() { event.UnregisterKeyForTest(key) })
|
||||
}
|
||||
|
||||
for _, def := range Keys() {
|
||||
event.RegisterKey(def)
|
||||
}
|
||||
for _, key := range []string{eventTypeApprovalInstanceStatusChangedV4, eventTypeApprovalTaskStatusChangedV4} {
|
||||
if _, ok := event.Lookup(key); !ok {
|
||||
t.Fatalf("event.Lookup(%q) not registered", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var _ event.APIClient = (*fakeAPIClient)(nil)
|
||||
42
events/approval/types.go
Normal file
42
events/approval/types.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package approval
|
||||
|
||||
// ApprovalUserID identifies a user in the three Lark ID formats included by
|
||||
// approval status-change events.
|
||||
type ApprovalUserID struct {
|
||||
OpenID string `json:"open_id,omitempty" desc:"User open_id; prefixed with ou_" kind:"open_id"`
|
||||
UnionID string `json:"union_id,omitempty" desc:"User union_id" kind:"union_id"`
|
||||
UserID string `json:"user_id,omitempty" desc:"User id within the tenant" kind:"user_id"`
|
||||
}
|
||||
|
||||
// ApprovalInstanceStatusChangedV4Output is the flattened shape for
|
||||
// approval.instance.status_changed_v4.
|
||||
type ApprovalInstanceStatusChangedV4Output struct {
|
||||
Type string `json:"type" desc:"Event type; always approval.instance.status_changed_v4" enum:"approval.instance.status_changed_v4"`
|
||||
EventID string `json:"event_id,omitempty" desc:"Globally unique event ID; safe for deduplication"`
|
||||
Timestamp string `json:"timestamp,omitempty" desc:"Event delivery time (ms timestamp string); taken from header.create_time when present" kind:"timestamp_ms"`
|
||||
ApprovalCode string `json:"approval_code,omitempty" desc:"Approval definition code; not a subscription dimension"`
|
||||
InstanceCode string `json:"instance_code,omitempty" desc:"Approval instance code"`
|
||||
ExternalID string `json:"external_id,omitempty" desc:"Third-party approval instance id; present only for third-party approvals"`
|
||||
Status string `json:"status,omitempty" desc:"Approval instance status" enum:"PENDING,APPROVED,REJECTED,CANCELED,DELETED,REVERTED,OVERTIME_CLOSE,OVERTIME_RECOVER"`
|
||||
OperateTime string `json:"operate_time,omitempty" desc:"Status change time in milliseconds" kind:"timestamp_ms"`
|
||||
StartUser *ApprovalUserID `json:"start_user,omitempty" desc:"Approval instance starter; omitted when unavailable"`
|
||||
}
|
||||
|
||||
// ApprovalTaskStatusChangedV4Output is the flattened shape for
|
||||
// approval.task.status_changed_v4.
|
||||
type ApprovalTaskStatusChangedV4Output struct {
|
||||
Type string `json:"type" desc:"Event type; always approval.task.status_changed_v4" enum:"approval.task.status_changed_v4"`
|
||||
EventID string `json:"event_id,omitempty" desc:"Globally unique event ID; safe for deduplication"`
|
||||
Timestamp string `json:"timestamp,omitempty" desc:"Event delivery time (ms timestamp string); taken from header.create_time when present" kind:"timestamp_ms"`
|
||||
ApprovalCode string `json:"approval_code,omitempty" desc:"Approval definition code; not a subscription dimension"`
|
||||
InstanceCode string `json:"instance_code,omitempty" desc:"Approval instance code"`
|
||||
TaskID string `json:"task_id,omitempty" desc:"Approval task id"`
|
||||
ExternalID string `json:"external_id,omitempty" desc:"Third-party approval external id; present only for third-party approvals"`
|
||||
TaskExternalID string `json:"task_external_id,omitempty" desc:"Third-party approval task external id; present only when emitted by the upstream service"`
|
||||
AssignedUser *ApprovalUserID `json:"assigned_user,omitempty" desc:"Task assignee or operator user ids; omitted for automatic flows without an operator"`
|
||||
Status string `json:"status,omitempty" desc:"Approval task status" enum:"REVERTED,PENDING,APPROVED,REJECTED,TRANSFERRED,ROLLBACK,DONE,OVERTIME_CLOSE,OVERTIME_RECOVER"`
|
||||
OperateTime string `json:"operate_time,omitempty" desc:"Status change time in milliseconds" kind:"timestamp_ms"`
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"github.com/larksuite/cli/events/approval"
|
||||
"github.com/larksuite/cli/events/im"
|
||||
"github.com/larksuite/cli/events/minutes"
|
||||
"github.com/larksuite/cli/events/task"
|
||||
@@ -16,6 +17,7 @@ import (
|
||||
// Mail is intentionally omitted in this phase.
|
||||
func init() {
|
||||
all := [][]event.KeyDefinition{
|
||||
approval.Keys(),
|
||||
im.Keys(),
|
||||
minutes.Keys(),
|
||||
task.Keys(),
|
||||
|
||||
@@ -8,15 +8,29 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/internal/client"
|
||||
"github.com/larksuite/cli/internal/core"
|
||||
"github.com/larksuite/cli/internal/output"
|
||||
"github.com/larksuite/cli/internal/util"
|
||||
)
|
||||
|
||||
var dryRunURLPlaceholderRE = regexp.MustCompile(`:([A-Za-z_][A-Za-z0-9_]*)`)
|
||||
|
||||
// DryRunOutputOptions controls dry-run stdout/stderr rendering.
|
||||
type DryRunOutputOptions struct {
|
||||
Format string
|
||||
JqExpr string
|
||||
CommandPath string
|
||||
Identity core.Identity
|
||||
Out io.Writer
|
||||
ErrOut io.Writer
|
||||
}
|
||||
|
||||
// DryRunAPICall describes a single API call in dry-run output.
|
||||
type DryRunAPICall struct {
|
||||
Desc string `json:"desc,omitempty"`
|
||||
@@ -26,12 +40,21 @@ type DryRunAPICall struct {
|
||||
Body interface{} `json:"body,omitempty"`
|
||||
}
|
||||
|
||||
// DryRunContext is the execution context shared by every dry-run preview:
|
||||
// which app would make the call and, when known, as which user. The identity
|
||||
// itself lives at the envelope top level, not here.
|
||||
type DryRunContext struct {
|
||||
AppID string `json:"app_id,omitempty"`
|
||||
UserOpenID string `json:"user_open_id,omitempty"`
|
||||
}
|
||||
|
||||
// DryRunAPI is the builder and result type for dry-run output.
|
||||
// URL templates use :param placeholders; Set stores actual values; MarshalJSON and Format resolve them.
|
||||
type DryRunAPI struct {
|
||||
desc string
|
||||
calls []DryRunAPICall
|
||||
extra map[string]interface{}
|
||||
desc string
|
||||
calls []DryRunAPICall
|
||||
context *DryRunContext
|
||||
extra map[string]interface{}
|
||||
}
|
||||
|
||||
func NewDryRunAPI() *DryRunAPI {
|
||||
@@ -40,30 +63,22 @@ func NewDryRunAPI() *DryRunAPI {
|
||||
|
||||
// --- HTTP method builders (add a call, return self for chaining) ---
|
||||
|
||||
func (d *DryRunAPI) GET(url string) *DryRunAPI {
|
||||
d.calls = append(d.calls, DryRunAPICall{Method: "GET", URL: url})
|
||||
// call appends a request with the method transcribed verbatim, so previews
|
||||
// never misreport what the real client would send.
|
||||
func (d *DryRunAPI) call(method, url string) *DryRunAPI {
|
||||
d.calls = append(d.calls, DryRunAPICall{Method: method, URL: url})
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *DryRunAPI) POST(url string) *DryRunAPI {
|
||||
d.calls = append(d.calls, DryRunAPICall{Method: "POST", URL: url})
|
||||
return d
|
||||
}
|
||||
func (d *DryRunAPI) GET(url string) *DryRunAPI { return d.call("GET", url) }
|
||||
|
||||
func (d *DryRunAPI) PUT(url string) *DryRunAPI {
|
||||
d.calls = append(d.calls, DryRunAPICall{Method: "PUT", URL: url})
|
||||
return d
|
||||
}
|
||||
func (d *DryRunAPI) POST(url string) *DryRunAPI { return d.call("POST", url) }
|
||||
|
||||
func (d *DryRunAPI) DELETE(url string) *DryRunAPI {
|
||||
d.calls = append(d.calls, DryRunAPICall{Method: "DELETE", URL: url})
|
||||
return d
|
||||
}
|
||||
func (d *DryRunAPI) PUT(url string) *DryRunAPI { return d.call("PUT", url) }
|
||||
|
||||
func (d *DryRunAPI) PATCH(url string) *DryRunAPI {
|
||||
d.calls = append(d.calls, DryRunAPICall{Method: "PATCH", URL: url})
|
||||
return d
|
||||
}
|
||||
func (d *DryRunAPI) DELETE(url string) *DryRunAPI { return d.call("DELETE", url) }
|
||||
|
||||
func (d *DryRunAPI) PATCH(url string) *DryRunAPI { return d.call("PATCH", url) }
|
||||
|
||||
// Body sets the request body on the last added call.
|
||||
func (d *DryRunAPI) Body(body interface{}) *DryRunAPI {
|
||||
@@ -98,12 +113,26 @@ func (d *DryRunAPI) Set(key string, value interface{}) *DryRunAPI {
|
||||
return d
|
||||
}
|
||||
|
||||
// Context records the calling app/user under data.context; empty values are
|
||||
// omitted, and a fully empty context is not emitted at all.
|
||||
func (d *DryRunAPI) Context(appID, userOpenID string) *DryRunAPI {
|
||||
if appID == "" && userOpenID == "" {
|
||||
return d
|
||||
}
|
||||
d.context = &DryRunContext{AppID: appID, UserOpenID: userOpenID}
|
||||
return d
|
||||
}
|
||||
|
||||
// resolveURL replaces :key placeholders in url with path-escaped values from extra.
|
||||
func (d *DryRunAPI) resolveURL(rawURL string) string {
|
||||
for k, v := range d.extra {
|
||||
rawURL = strings.ReplaceAll(rawURL, ":"+k, url.PathEscape(fmt.Sprintf("%v", v)))
|
||||
}
|
||||
return rawURL
|
||||
return dryRunURLPlaceholderRE.ReplaceAllStringFunc(rawURL, func(token string) string {
|
||||
name := token[1:]
|
||||
value, ok := d.extra[name]
|
||||
if !ok {
|
||||
return token
|
||||
}
|
||||
return url.PathEscape(fmt.Sprintf("%v", value))
|
||||
})
|
||||
}
|
||||
|
||||
// MarshalJSON serializes as {"description": "...", "api": [...calls with resolved URLs], ...extra}.
|
||||
@@ -118,13 +147,17 @@ func (d *DryRunAPI) MarshalJSON() ([]byte, error) {
|
||||
Body: c.Body,
|
||||
}
|
||||
}
|
||||
m := make(map[string]interface{}, len(d.extra)+2)
|
||||
m := make(map[string]interface{}, len(d.extra)+3)
|
||||
for k, v := range d.extra {
|
||||
m[k] = v
|
||||
}
|
||||
// Typed fields win over same-named extra keys.
|
||||
if d.desc != "" {
|
||||
m["description"] = d.desc
|
||||
}
|
||||
m["api"] = resolved
|
||||
for k, v := range d.extra {
|
||||
m[k] = v
|
||||
if d.context != nil {
|
||||
m["context"] = d.context
|
||||
}
|
||||
return json.Marshal(m)
|
||||
}
|
||||
@@ -154,11 +187,7 @@ func (d *DryRunAPI) Format() string {
|
||||
u += "?" + encodeParams(c.Params)
|
||||
}
|
||||
|
||||
method := c.Method
|
||||
if method == "" {
|
||||
method = "GET"
|
||||
}
|
||||
b.WriteString(method)
|
||||
b.WriteString(c.Method)
|
||||
b.WriteByte(' ')
|
||||
b.WriteString(u)
|
||||
b.WriteByte('\n')
|
||||
@@ -215,83 +244,74 @@ func encodeParams(params map[string]interface{}) string {
|
||||
return vals.Encode()
|
||||
}
|
||||
|
||||
// PrintDryRunWithFile outputs a dry-run summary for file upload requests.
|
||||
// Instead of serializing the Formdata body, it shows file metadata.
|
||||
func PrintDryRunWithFile(w io.Writer, request client.RawApiRequest, config *core.CliConfig, format, fileField, filePath string, formFields any) error {
|
||||
dr := NewDryRunAPI()
|
||||
switch request.Method {
|
||||
case "POST":
|
||||
dr.POST(request.URL)
|
||||
case "PUT":
|
||||
dr.PUT(request.URL)
|
||||
case "PATCH":
|
||||
dr.PATCH(request.URL)
|
||||
case "DELETE":
|
||||
dr.DELETE(request.URL)
|
||||
default:
|
||||
dr.GET(request.URL)
|
||||
}
|
||||
// buildDryRunPreview assembles the shared preview skeleton: HTTP method, URL,
|
||||
// query params, and the app/user context common to every dry-run.
|
||||
func buildDryRunPreview(request client.RawApiRequest, config *core.CliConfig) *DryRunAPI {
|
||||
dr := NewDryRunAPI().call(request.Method, request.URL)
|
||||
if len(request.Params) > 0 {
|
||||
dr.Params(request.Params)
|
||||
}
|
||||
filePathDisplay := filePath
|
||||
// Identity is reported at the envelope top level, not duplicated here.
|
||||
dr.Context(config.AppID, config.UserOpenId)
|
||||
return dr
|
||||
}
|
||||
|
||||
// PrintDryRunWithFile outputs a dry-run summary for file upload requests.
|
||||
// Instead of serializing the Formdata body, it shows file metadata.
|
||||
func PrintDryRunWithFile(request client.RawApiRequest, config *core.CliConfig, opts DryRunOutputOptions, file FileUploadMeta) error {
|
||||
dr := buildDryRunPreview(request, config)
|
||||
filePathDisplay := file.FilePath
|
||||
if filePathDisplay == "" {
|
||||
filePathDisplay = "<stdin>"
|
||||
}
|
||||
fileInfo := map[string]any{
|
||||
"file": map[string]string{"field": fileField, "path": filePathDisplay},
|
||||
"file": map[string]string{"field": file.FieldName, "path": filePathDisplay},
|
||||
}
|
||||
if formFields != nil {
|
||||
fileInfo["form_fields"] = formFields
|
||||
if file.FormFields != nil {
|
||||
fileInfo["form_fields"] = file.FormFields
|
||||
}
|
||||
fileInfo["options"] = []string{"WithFileUpload"}
|
||||
dr.Body(fileInfo)
|
||||
dr.Set("as", string(request.As))
|
||||
dr.Set("appId", config.AppID)
|
||||
if config.UserOpenId != "" {
|
||||
dr.Set("userOpenId", config.UserOpenId)
|
||||
}
|
||||
fmt.Fprintln(w, "=== Dry Run ===")
|
||||
if format == "pretty" {
|
||||
fmt.Fprint(w, dr.Format())
|
||||
} else {
|
||||
output.PrintJson(w, dr)
|
||||
}
|
||||
return nil
|
||||
return WriteDryRun(dr, opts)
|
||||
}
|
||||
|
||||
// PrintDryRun outputs a standardised dry-run summary using DryRunAPI.
|
||||
// When format is "pretty", outputs human-readable text; otherwise JSON.
|
||||
func PrintDryRun(w io.Writer, request client.RawApiRequest, config *core.CliConfig, format string) error {
|
||||
dr := NewDryRunAPI()
|
||||
switch request.Method {
|
||||
case "POST":
|
||||
dr.POST(request.URL)
|
||||
case "PUT":
|
||||
dr.PUT(request.URL)
|
||||
case "PATCH":
|
||||
dr.PATCH(request.URL)
|
||||
case "DELETE":
|
||||
dr.DELETE(request.URL)
|
||||
default:
|
||||
dr.GET(request.URL)
|
||||
}
|
||||
if len(request.Params) > 0 {
|
||||
dr.Params(request.Params)
|
||||
}
|
||||
func PrintDryRun(request client.RawApiRequest, config *core.CliConfig, opts DryRunOutputOptions) error {
|
||||
dr := buildDryRunPreview(request, config)
|
||||
if !util.IsNil(request.Data) {
|
||||
dr.Body(request.Data)
|
||||
}
|
||||
dr.Set("as", string(request.As))
|
||||
dr.Set("appId", config.AppID)
|
||||
if config.UserOpenId != "" {
|
||||
dr.Set("userOpenId", config.UserOpenId)
|
||||
}
|
||||
fmt.Fprintln(w, "=== Dry Run ===")
|
||||
if format == "pretty" {
|
||||
fmt.Fprint(w, dr.Format())
|
||||
} else {
|
||||
output.PrintJson(w, dr)
|
||||
}
|
||||
return nil
|
||||
return WriteDryRun(dr, opts)
|
||||
}
|
||||
|
||||
// WriteDryRun emits a DryRunAPI using the shared dry-run output contract.
|
||||
// Identity may be empty; the envelope omits it rather than guessing.
|
||||
func WriteDryRun(dr *DryRunAPI, opts DryRunOutputOptions) error {
|
||||
if dr == nil {
|
||||
return errs.NewInternalError(errs.SubtypeUnknown, "dry-run produced no request preview")
|
||||
}
|
||||
// The JqExpr guard is defensive: every entry point already rejects --jq
|
||||
// combined with --format pretty via output.ValidateJqFlags.
|
||||
if opts.Format == "pretty" && opts.JqExpr == "" {
|
||||
// A nil ErrOut only skips the banner decoration (mirroring
|
||||
// WriteSuccessEnvelope's warning path); the payload write to Out
|
||||
// must fail loudly rather than be silently discarded.
|
||||
if opts.ErrOut != nil {
|
||||
fmt.Fprintln(opts.ErrOut, "=== Dry Run ===")
|
||||
}
|
||||
// stdout carries its own marker so logs that drop stderr still show
|
||||
// this was a preview, not an executed request.
|
||||
fmt.Fprintln(opts.Out, "# dry-run: request not sent")
|
||||
fmt.Fprint(opts.Out, dr.Format())
|
||||
return nil
|
||||
}
|
||||
return output.WriteSuccessEnvelope(dr, output.SuccessEnvelopeOptions{
|
||||
CommandPath: opts.CommandPath,
|
||||
Identity: string(opts.Identity),
|
||||
DryRun: true,
|
||||
JqExpr: opts.JqExpr,
|
||||
Out: opts.Out,
|
||||
ErrOut: opts.ErrOut,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,9 +6,12 @@ package cmdutil
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/internal/client"
|
||||
"github.com/larksuite/cli/internal/core"
|
||||
)
|
||||
@@ -66,11 +69,31 @@ func TestDryRunAPI_ResolveURL(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDryRunAPI_ResolveURLMatchesFullPlaceholderOnly(t *testing.T) {
|
||||
dr := NewDryRunAPI().
|
||||
GET("/open-apis/task/v2/tasks/:assignee_id").
|
||||
Set("assignee", "ou_bot")
|
||||
|
||||
text := dr.Format()
|
||||
if strings.Contains(text, "ou_bot_id") {
|
||||
t.Fatalf("prefix placeholder key corrupted longer token: %s", text)
|
||||
}
|
||||
if !strings.Contains(text, ":assignee_id") {
|
||||
t.Fatalf("missing unresolved placeholder, got: %s", text)
|
||||
}
|
||||
|
||||
dr.Set("assignee_id", "ou_abc/123")
|
||||
text = dr.Format()
|
||||
if !strings.Contains(text, "/open-apis/task/v2/tasks/ou_abc%2F123") {
|
||||
t.Fatalf("expected full placeholder replacement with path escaping, got: %s", text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDryRunAPI_MarshalJSON(t *testing.T) {
|
||||
dr := NewDryRunAPI().
|
||||
Desc("test api").
|
||||
GET("/open-apis/test").
|
||||
Set("as", "user")
|
||||
Set("note", "audit")
|
||||
|
||||
data, err := json.Marshal(dr)
|
||||
if err != nil {
|
||||
@@ -83,8 +106,8 @@ func TestDryRunAPI_MarshalJSON(t *testing.T) {
|
||||
if m["description"] != "test api" {
|
||||
t.Errorf("expected description, got: %v", m["description"])
|
||||
}
|
||||
if m["as"] != "user" {
|
||||
t.Errorf("expected as=user, got: %v", m["as"])
|
||||
if m["note"] != "audit" {
|
||||
t.Errorf("expected note=audit, got: %v", m["note"])
|
||||
}
|
||||
api, ok := m["api"].([]interface{})
|
||||
if !ok || len(api) != 1 {
|
||||
@@ -123,31 +146,67 @@ func TestDryRunAPI_ExtraFieldsOnly(t *testing.T) {
|
||||
|
||||
func TestPrintDryRun_JSON(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
err := PrintDryRun(&buf, client.RawApiRequest{
|
||||
var errBuf bytes.Buffer
|
||||
err := PrintDryRun(client.RawApiRequest{
|
||||
Method: "GET",
|
||||
URL: "/open-apis/test",
|
||||
As: "user",
|
||||
}, &core.CliConfig{AppID: "app123"}, "json")
|
||||
}, &core.CliConfig{AppID: "app123"}, DryRunOutputOptions{
|
||||
Format: "json",
|
||||
CommandPath: "lark-cli api",
|
||||
Identity: core.AsUser,
|
||||
Out: &buf,
|
||||
ErrOut: &errBuf,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("PrintDryRun failed: %v", err)
|
||||
}
|
||||
out := buf.String()
|
||||
if !strings.Contains(out, "=== Dry Run ===") {
|
||||
t.Errorf("expected header, got: %s", out)
|
||||
if strings.Contains(out, "=== Dry Run ===") {
|
||||
t.Fatalf("JSON stdout must not contain banner, got: %s", out)
|
||||
}
|
||||
if !strings.Contains(out, "app123") {
|
||||
t.Errorf("expected appId in output, got: %s", out)
|
||||
var env map[string]interface{}
|
||||
if err := json.Unmarshal(buf.Bytes(), &env); err != nil {
|
||||
t.Fatalf("dry-run stdout is not JSON: %v\n%s", err, out)
|
||||
}
|
||||
if env["ok"] != true || env["identity"] != "user" || env["dry_run"] != true {
|
||||
t.Fatalf("unexpected envelope: %#v", env)
|
||||
}
|
||||
data, ok := env["data"].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("unexpected data: %#v", env["data"])
|
||||
}
|
||||
dctx, ok := data["context"].(map[string]interface{})
|
||||
if !ok || dctx["app_id"] != "app123" {
|
||||
t.Fatalf("unexpected data.context: %#v", data["context"])
|
||||
}
|
||||
if _, exists := data["as"]; exists {
|
||||
t.Fatalf("data.as must not appear; identity lives at the envelope top level: %#v", data)
|
||||
}
|
||||
api, ok := data["api"].([]interface{})
|
||||
if !ok || len(api) != 1 {
|
||||
t.Fatalf("api = %#v, want one call", data["api"])
|
||||
}
|
||||
call, ok := api[0].(map[string]interface{})
|
||||
if !ok || call["url"] != "/open-apis/test" {
|
||||
t.Fatalf("api[0] = %#v", api[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintDryRun_Pretty(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
err := PrintDryRun(&buf, client.RawApiRequest{
|
||||
var errBuf bytes.Buffer
|
||||
err := PrintDryRun(client.RawApiRequest{
|
||||
Method: "POST",
|
||||
URL: "/open-apis/test",
|
||||
Data: map[string]interface{}{"key": "val"},
|
||||
As: "bot",
|
||||
}, &core.CliConfig{AppID: "app456"}, "pretty")
|
||||
}, &core.CliConfig{AppID: "app456"}, DryRunOutputOptions{
|
||||
Format: "pretty",
|
||||
Identity: core.AsBot,
|
||||
Out: &buf,
|
||||
ErrOut: &errBuf,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("PrintDryRun failed: %v", err)
|
||||
}
|
||||
@@ -155,6 +214,136 @@ func TestPrintDryRun_Pretty(t *testing.T) {
|
||||
if !strings.Contains(out, "POST /open-apis/test") {
|
||||
t.Errorf("expected POST line in pretty output, got: %s", out)
|
||||
}
|
||||
if !strings.HasPrefix(out, "# dry-run: request not sent\n") {
|
||||
t.Fatalf("pretty stdout should start with the dry-run marker, got: %s", out)
|
||||
}
|
||||
if strings.Contains(out, "=== Dry Run ===") {
|
||||
t.Fatalf("pretty stdout must not contain banner, got: %s", out)
|
||||
}
|
||||
if !strings.Contains(errBuf.String(), "=== Dry Run ===") {
|
||||
t.Fatalf("pretty stderr should contain banner, got: %s", errBuf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintDryRun_WithJqUsesEnvelope(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
err := PrintDryRun(client.RawApiRequest{
|
||||
Method: "GET",
|
||||
URL: "/open-apis/test",
|
||||
As: "bot",
|
||||
}, &core.CliConfig{AppID: "app123"}, DryRunOutputOptions{
|
||||
Format: "json",
|
||||
JqExpr: ".data.api[0].url",
|
||||
Identity: core.AsBot,
|
||||
Out: &buf,
|
||||
ErrOut: io.Discard,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("PrintDryRun failed: %v", err)
|
||||
}
|
||||
if got := strings.TrimSpace(buf.String()); got != "/open-apis/test" {
|
||||
t.Fatalf("jq output = %q, want /open-apis/test", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintDryRunWithFile_JSONEnvelope(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
err := PrintDryRunWithFile(client.RawApiRequest{
|
||||
Method: "POST",
|
||||
URL: "/open-apis/drive/v1/files/upload_all",
|
||||
As: "bot",
|
||||
}, &core.CliConfig{AppID: "app123", UserOpenId: "ou_tester"}, DryRunOutputOptions{
|
||||
Format: "json",
|
||||
Identity: core.AsBot,
|
||||
Out: &buf,
|
||||
ErrOut: io.Discard,
|
||||
}, FileUploadMeta{FieldName: "file", FilePath: "report.txt", FormFields: map[string]any{"parent": "fld"}})
|
||||
if err != nil {
|
||||
t.Fatalf("PrintDryRunWithFile failed: %v", err)
|
||||
}
|
||||
var env map[string]interface{}
|
||||
if err := json.Unmarshal(buf.Bytes(), &env); err != nil {
|
||||
t.Fatalf("dry-run stdout is not JSON: %v\n%s", err, buf.String())
|
||||
}
|
||||
if env["dry_run"] != true {
|
||||
t.Fatalf("dry_run = %#v, want true", env["dry_run"])
|
||||
}
|
||||
data := env["data"].(map[string]interface{})
|
||||
api := data["api"].([]interface{})
|
||||
call := api[0].(map[string]interface{})
|
||||
body := call["body"].(map[string]interface{})
|
||||
file := body["file"].(map[string]interface{})
|
||||
if file["path"] != "report.txt" {
|
||||
t.Fatalf("file body = %#v", body)
|
||||
}
|
||||
dctx, ok := data["context"].(map[string]interface{})
|
||||
if !ok || dctx["app_id"] != "app123" || dctx["user_open_id"] != "ou_tester" {
|
||||
t.Fatalf("unexpected data.context: %#v", data["context"])
|
||||
}
|
||||
for _, legacy := range []string{"as", "appId", "userOpenId"} {
|
||||
if _, exists := data[legacy]; exists {
|
||||
t.Fatalf("legacy key %q must not appear in data: %#v", legacy, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintDryRun_MethodTranscribedVerbatim(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
err := PrintDryRun(client.RawApiRequest{
|
||||
Method: "OPTIONS",
|
||||
URL: "/open-apis/test",
|
||||
As: "bot",
|
||||
}, &core.CliConfig{AppID: "app123"}, DryRunOutputOptions{
|
||||
Format: "json",
|
||||
Identity: core.AsBot,
|
||||
Out: &buf,
|
||||
ErrOut: io.Discard,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("PrintDryRun failed: %v", err)
|
||||
}
|
||||
var env map[string]interface{}
|
||||
if err := json.Unmarshal(buf.Bytes(), &env); err != nil {
|
||||
t.Fatalf("dry-run stdout is not JSON: %v\n%s", err, buf.String())
|
||||
}
|
||||
call := env["data"].(map[string]interface{})["api"].([]interface{})[0].(map[string]interface{})
|
||||
if call["method"] != "OPTIONS" {
|
||||
t.Fatalf("method = %#v, want OPTIONS transcribed verbatim (not coerced to GET)", call["method"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintDryRun_EmptyConfigOmitsContext(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
err := PrintDryRun(client.RawApiRequest{
|
||||
Method: "GET",
|
||||
URL: "/open-apis/test",
|
||||
}, &core.CliConfig{}, DryRunOutputOptions{
|
||||
Format: "json",
|
||||
Out: &buf,
|
||||
ErrOut: io.Discard,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("PrintDryRun failed: %v", err)
|
||||
}
|
||||
var env map[string]interface{}
|
||||
if err := json.Unmarshal(buf.Bytes(), &env); err != nil {
|
||||
t.Fatalf("dry-run stdout is not JSON: %v\n%s", err, buf.String())
|
||||
}
|
||||
data := env["data"].(map[string]interface{})
|
||||
if _, exists := data["context"]; exists {
|
||||
t.Fatalf("empty app/user context must be omitted entirely, got: %#v", data["context"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteDryRun_NilPreviewIsInternalError(t *testing.T) {
|
||||
err := WriteDryRun(nil, DryRunOutputOptions{Format: "json", Out: io.Discard})
|
||||
if err == nil {
|
||||
t.Fatal("WriteDryRun(nil) should fail instead of emitting an empty preview")
|
||||
}
|
||||
var internal *errs.InternalError
|
||||
if !errors.As(err, &internal) {
|
||||
t.Fatalf("expected *errs.InternalError, got %T: %v", err, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDryRunFormatValue(t *testing.T) {
|
||||
|
||||
28
internal/errclass/codemeta_spark.go
Normal file
28
internal/errclass/codemeta_spark.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package errclass
|
||||
|
||||
import "github.com/larksuite/cli/errs"
|
||||
|
||||
// sparkCodeMeta holds stable Spark app-role business-code classifications.
|
||||
// Command-specific recovery guidance belongs in the Apps shortcut layer; the
|
||||
// numeric code remains the source-specific discriminator on the error envelope.
|
||||
var sparkCodeMeta = map[int]CodeMeta{
|
||||
3340001: {Category: errs.CategoryAPI, Subtype: errs.SubtypeInvalidParameters}, // request parameters are invalid
|
||||
3344027: {Category: errs.CategoryAPI, Subtype: errs.SubtypeQuotaExceeded}, // role user count exceeds the service limit
|
||||
3344028: {Category: errs.CategoryAPI, Subtype: errs.SubtypeQuotaExceeded}, // role department count exceeds the service limit
|
||||
3344029: {Category: errs.CategoryAPI, Subtype: errs.SubtypeQuotaExceeded}, // role chat count exceeds the service limit
|
||||
3344030: {Category: errs.CategoryAuthorization, Subtype: errs.SubtypePermissionDenied}, // app administrator required
|
||||
3344031: {Category: errs.CategoryAuthorization, Subtype: errs.SubtypePermissionDenied}, // app administrator or developer required
|
||||
3344034: {Category: errs.CategoryAPI, Subtype: errs.SubtypeInvalidParameters}, // invalid role ID
|
||||
3344035: {Category: errs.CategoryAPI, Subtype: errs.SubtypeNotFound}, // role does not exist
|
||||
3344036: {Category: errs.CategoryAPI, Subtype: errs.SubtypeAlreadyExists}, // role ID already exists
|
||||
3344037: {Category: errs.CategoryAPI, Subtype: errs.SubtypeQuotaExceeded}, // app role count exceeds the service limit
|
||||
3344038: {Category: errs.CategoryAPI, Subtype: errs.SubtypeInvalidParameters}, // invalid role name
|
||||
3344039: {Category: errs.CategoryAPI, Subtype: errs.SubtypeInvalidParameters}, // invalid role description
|
||||
3344040: {Category: errs.CategoryAPI, Subtype: errs.SubtypeInvalidParameters}, // unsupported member type
|
||||
3344041: {Category: errs.CategoryAPI, Subtype: errs.SubtypeInvalidParameters}, // invalid member ID
|
||||
}
|
||||
|
||||
func init() { mergeCodeMeta(sparkCodeMeta, "spark") }
|
||||
59
internal/errclass/codemeta_spark_test.go
Normal file
59
internal/errclass/codemeta_spark_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package errclass
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
)
|
||||
|
||||
func TestLookupCodeMetaSparkRoleCodes(t *testing.T) {
|
||||
tests := []struct {
|
||||
code int
|
||||
category errs.Category
|
||||
subtype errs.Subtype
|
||||
}{
|
||||
{3340001, errs.CategoryAPI, errs.SubtypeInvalidParameters},
|
||||
{3344027, errs.CategoryAPI, errs.SubtypeQuotaExceeded},
|
||||
{3344028, errs.CategoryAPI, errs.SubtypeQuotaExceeded},
|
||||
{3344029, errs.CategoryAPI, errs.SubtypeQuotaExceeded},
|
||||
{3344030, errs.CategoryAuthorization, errs.SubtypePermissionDenied},
|
||||
{3344031, errs.CategoryAuthorization, errs.SubtypePermissionDenied},
|
||||
{3344034, errs.CategoryAPI, errs.SubtypeInvalidParameters},
|
||||
{3344035, errs.CategoryAPI, errs.SubtypeNotFound},
|
||||
{3344036, errs.CategoryAPI, errs.SubtypeAlreadyExists},
|
||||
{3344037, errs.CategoryAPI, errs.SubtypeQuotaExceeded},
|
||||
{3344038, errs.CategoryAPI, errs.SubtypeInvalidParameters},
|
||||
{3344039, errs.CategoryAPI, errs.SubtypeInvalidParameters},
|
||||
{3344040, errs.CategoryAPI, errs.SubtypeInvalidParameters},
|
||||
{3344041, errs.CategoryAPI, errs.SubtypeInvalidParameters},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(fmt.Sprintf("%d", tt.code), func(t *testing.T) {
|
||||
meta, ok := LookupCodeMeta(tt.code)
|
||||
if !ok {
|
||||
t.Fatalf("code %d is not registered", tt.code)
|
||||
}
|
||||
if meta.Category != tt.category || meta.Subtype != tt.subtype || meta.Retryable {
|
||||
t.Fatalf("code %d metadata = %+v, want category=%s subtype=%s retryable=false", tt.code, meta, tt.category, tt.subtype)
|
||||
}
|
||||
|
||||
err := BuildAPIError(map[string]any{
|
||||
"code": tt.code,
|
||||
"msg": "spark role error",
|
||||
"log_id": "log-spark-role",
|
||||
}, ClassifyContext{Identity: "user"})
|
||||
problem, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
t.Fatalf("BuildAPIError(%d) = %#v, want typed problem", tt.code, err)
|
||||
}
|
||||
if problem.Category != tt.category || problem.Subtype != tt.subtype || problem.Code != tt.code || problem.LogID != "log-spark-role" || problem.Retryable {
|
||||
t.Fatalf("BuildAPIError(%d) problem = %+v", tt.code, problem)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ package output
|
||||
type Envelope struct {
|
||||
OK bool `json:"ok"`
|
||||
Identity string `json:"identity,omitempty"`
|
||||
DryRun bool `json:"dry_run,omitempty"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Meta *Meta `json:"meta,omitempty"`
|
||||
ContentSafetyAlert interface{} `json:"_content_safety_alert,omitempty"`
|
||||
|
||||
@@ -9,6 +9,7 @@ import "io"
|
||||
type SuccessEnvelopeOptions struct {
|
||||
CommandPath string
|
||||
Identity string
|
||||
DryRun bool
|
||||
JqExpr string
|
||||
Out io.Writer
|
||||
ErrOut io.Writer
|
||||
@@ -41,6 +42,7 @@ func WriteSuccessEnvelope(data interface{}, opts SuccessEnvelopeOptions) error {
|
||||
env := Envelope{
|
||||
OK: true,
|
||||
Identity: opts.Identity,
|
||||
DryRun: opts.DryRun,
|
||||
Data: data,
|
||||
Notice: GetNotice(),
|
||||
}
|
||||
|
||||
@@ -104,6 +104,47 @@ func TestWriteSuccessEnvelope_JqUsesEnvelope(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteSuccessEnvelope_DryRunMarker(t *testing.T) {
|
||||
var out strings.Builder
|
||||
|
||||
err := WriteSuccessEnvelope(map[string]interface{}{"api": []interface{}{}}, SuccessEnvelopeOptions{
|
||||
Identity: "bot",
|
||||
DryRun: true,
|
||||
Out: &out,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("WriteSuccessEnvelope() error = %v", err)
|
||||
}
|
||||
|
||||
var env map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(out.String()), &env); err != nil {
|
||||
t.Fatalf("invalid JSON output: %v\n%s", err, out.String())
|
||||
}
|
||||
if env["ok"] != true || env["identity"] != "bot" || env["dry_run"] != true {
|
||||
t.Fatalf("unexpected dry-run envelope: %#v", env)
|
||||
}
|
||||
if _, ok := env["data"].(map[string]interface{}); !ok {
|
||||
t.Fatalf("data = %#v, want object", env["data"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteSuccessEnvelope_DryRunJqUsesEnvelope(t *testing.T) {
|
||||
var out strings.Builder
|
||||
|
||||
err := WriteSuccessEnvelope(map[string]interface{}{"api": []interface{}{}}, SuccessEnvelopeOptions{
|
||||
Identity: "bot",
|
||||
DryRun: true,
|
||||
JqExpr: ".dry_run",
|
||||
Out: &out,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("WriteSuccessEnvelope() error = %v", err)
|
||||
}
|
||||
if strings.TrimSpace(out.String()) != "true" {
|
||||
t.Fatalf("jq output = %q, want true", out.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteSuccessEnvelope_JqWarnsWhenSafetyAlertFiltered(t *testing.T) {
|
||||
t.Setenv("LARKSUITE_CLI_CONTENT_SAFETY_MODE", "warn")
|
||||
extcs.Register(&mockProvider{
|
||||
|
||||
@@ -19,12 +19,18 @@ import (
|
||||
type eventPayload struct {
|
||||
Comment *struct {
|
||||
Body string `json:"body"`
|
||||
Path string `json:"path"`
|
||||
} `json:"comment"`
|
||||
Review *struct {
|
||||
Body string `json:"body"`
|
||||
} `json:"review"`
|
||||
}
|
||||
|
||||
type commentContent struct {
|
||||
Body string
|
||||
Path string
|
||||
}
|
||||
|
||||
func main() {
|
||||
eventPath := flag.String("event", os.Getenv("GITHUB_EVENT_PATH"), "GitHub event payload path")
|
||||
kind := flag.String("kind", os.Getenv("GITHUB_EVENT_NAME"), "GitHub event kind")
|
||||
@@ -34,12 +40,11 @@ func main() {
|
||||
fmt.Fprintln(os.Stderr, "comment-audit: --event or GITHUB_EVENT_PATH is required")
|
||||
os.Exit(2)
|
||||
}
|
||||
body, err := commentBody(*eventPath)
|
||||
diags, err := auditEvent(*eventPath, *kind)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "comment-audit: %v\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
diags := diagnostics(publiccontent.ScanComment(*kind, body))
|
||||
if len(diags) > 0 {
|
||||
fmt.Fprintln(os.Stderr, auditFailureSummary(len(diags)))
|
||||
}
|
||||
@@ -47,32 +52,44 @@ func main() {
|
||||
os.Exit(report.ExitCode(diags))
|
||||
}
|
||||
|
||||
func auditEvent(eventPath, kind string) ([]report.Diagnostic, error) {
|
||||
content, err := commentBody(eventPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return scanCommentContent(kind, content), nil
|
||||
}
|
||||
|
||||
func scanCommentContent(kind string, content commentContent) []report.Diagnostic {
|
||||
return diagnostics(publiccontent.ScanCommentAtPath(kind, content.Path, content.Body))
|
||||
}
|
||||
|
||||
func auditFailureSummary(count int) string {
|
||||
return fmt.Sprintf("post-publication audit found public content findings: %d", count)
|
||||
}
|
||||
|
||||
func commentBody(path string) (string, error) {
|
||||
func commentBody(path string) (commentContent, error) {
|
||||
safePath, err := validate.SafeInputPath(path)
|
||||
if err != nil {
|
||||
return "", errs.NewValidationError(errs.SubtypeInvalidArgument, "invalid --event: %v", err).
|
||||
return commentContent{}, errs.NewValidationError(errs.SubtypeInvalidArgument, "invalid --event: %v", err).
|
||||
WithParam("--event").
|
||||
WithCause(err)
|
||||
}
|
||||
data, err := vfs.ReadFile(safePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return commentContent{}, err
|
||||
}
|
||||
var payload eventPayload
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
return "", err
|
||||
return commentContent{}, err
|
||||
}
|
||||
switch {
|
||||
case payload.Comment != nil:
|
||||
return payload.Comment.Body, nil
|
||||
return commentContent{Body: payload.Comment.Body, Path: payload.Comment.Path}, nil
|
||||
case payload.Review != nil:
|
||||
return payload.Review.Body, nil
|
||||
return commentContent{Body: payload.Review.Body}, nil
|
||||
default:
|
||||
return "", nil
|
||||
return commentContent{}, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,9 +7,11 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/internal/qualitygate/publiccontent"
|
||||
)
|
||||
|
||||
func TestCommentBodyReadsSafeRelativeEventPath(t *testing.T) {
|
||||
@@ -32,11 +34,92 @@ func TestCommentBodyReadsSafeRelativeEventPath(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("commentBody() error = %v", err)
|
||||
}
|
||||
if got != "clean comment" {
|
||||
t.Fatalf("comment body = %q", got)
|
||||
if got.Body != "clean comment" || got.Path != "" {
|
||||
t.Fatalf("comment content = %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommentBodyReadsReviewCommentPath(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := writeTestFile(filepath.Join(dir, "event.json"), `{"comment":{"body":"test suggestion","path":"cmd/agent/list_test.go"}}`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
origDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Chdir(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = os.Chdir(origDir)
|
||||
})
|
||||
|
||||
got, err := commentBody("event.json")
|
||||
if err != nil {
|
||||
t.Fatalf("commentBody() error = %v", err)
|
||||
}
|
||||
if got.Body != "test suggestion" || got.Path != "cmd/agent/list_test.go" {
|
||||
t.Fatalf("comment content = %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommentAuditUsesReviewCommentPathForFixtureClassification(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
body := `CLIENT_SECRET=$(security find-generic-password -w)`
|
||||
event := `{"comment":{"body":` + strconv.Quote(body) + `,"path":"scripts/config_test.sh"}}`
|
||||
if err := writeTestFile(filepath.Join(dir, "event.json"), event); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
origDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Chdir(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = os.Chdir(origDir)
|
||||
})
|
||||
|
||||
diags, err := auditEvent("event.json", "pull_request_review_comment")
|
||||
if err != nil {
|
||||
t.Fatalf("auditEvent() error = %v", err)
|
||||
}
|
||||
for _, diag := range diags {
|
||||
if diag.Rule == "public_content_generic_credential" {
|
||||
t.Fatalf("review comment fixture should not be a credential diagnostic: %#v", diags)
|
||||
}
|
||||
}
|
||||
pathless := publiccontent.ScanComment("pull_request_review_comment", body)
|
||||
for _, finding := range pathless {
|
||||
if finding.Rule == "public_content_generic_credential" {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatalf("test precondition failed: pathless comment should be classified as a credential: %#v", pathless)
|
||||
}
|
||||
|
||||
func TestScanCommentContentPreservesReviewCommentPath(t *testing.T) {
|
||||
providerValue := "gh" + "p_" + "1234567890abcdef" + "1234567890abcdef" + "1234"
|
||||
content := commentContent{
|
||||
Body: `cfg := &Config{AccessToken: "` + providerValue + `"}`,
|
||||
Path: "cmd/agent/list_test.go",
|
||||
}
|
||||
|
||||
diags := scanCommentContent("pull_request_review_comment", content)
|
||||
for _, diag := range diags {
|
||||
if diag.Rule != "public_content_generic_credential" {
|
||||
continue
|
||||
}
|
||||
if diag.File != content.Path {
|
||||
t.Fatalf("credential diagnostic file = %q, want %q", diag.File, content.Path)
|
||||
}
|
||||
return
|
||||
}
|
||||
t.Fatalf("missing provider credential diagnostic: %#v", diags)
|
||||
}
|
||||
|
||||
func TestCommentBodyRejectsUnsafeEventPath(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "event.json")
|
||||
if err := writeTestFile(path, `{"comment":{"body":"clean"}}`); err != nil {
|
||||
|
||||
@@ -23,9 +23,10 @@ func TestCollectScansOnlyCurrentContributionAndMetadata(t *testing.T) {
|
||||
runGit(t, repo, "add", "baseline.md")
|
||||
runGit(t, repo, "commit", "-m", "base")
|
||||
|
||||
providerValue := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
writeFile(t, filepath.Join(repo, "docs", "public.md"), `# Public change
|
||||
|
||||
api_`+`key = "example-public-key"
|
||||
api_`+`key = "`+providerValue+`"
|
||||
`)
|
||||
runGit(t, repo, "add", "docs/public.md")
|
||||
runGit(t, repo, "commit", "-m", "add public doc", "-m", "Change"+"-Id: I0123456789abcdef0123456789abcdef01234567")
|
||||
@@ -199,13 +200,14 @@ func TestCollectDetectsQuotedJSONCredentialAssignments(t *testing.T) {
|
||||
runGit(t, repo, "add", "docs/public.json")
|
||||
runGit(t, repo, "commit", "-m", "base")
|
||||
|
||||
providerValue := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
writeFile(t, filepath.Join(repo, "docs", "public.json"), strings.Join([]string{
|
||||
`{"access_` + `token":"real-json-token"}`,
|
||||
`{"client_` + `secret": "real ` + `secret value"}`,
|
||||
`{"tenantAccess` + `Token":"real-tenant-camel-token"}`,
|
||||
`{"github` + `Token":"real-github-token"}`,
|
||||
`{"vendorApi` + `Key":"real-vendor-key"}`,
|
||||
`{"slackBot` + `Token":"xoxb-real-token"}`,
|
||||
`{"access_` + `token":"` + providerValue + `"}`,
|
||||
`{"client_` + `secret": "` + providerValue + `"}`,
|
||||
`{"tenantAccess` + `Token":"` + providerValue + `"}`,
|
||||
`{"github` + `Token":"` + providerValue + `"}`,
|
||||
`{"vendorApi` + `Key":"` + providerValue + `"}`,
|
||||
`{"slackBot` + `Token":"xoxb_` + `1234567890abcdef"}`,
|
||||
}, "\n")+"\n")
|
||||
runGit(t, repo, "add", "docs/public.json")
|
||||
runGit(t, repo, "commit", "-m", "add json config")
|
||||
@@ -215,14 +217,7 @@ func TestCollectDetectsQuotedJSONCredentialAssignments(t *testing.T) {
|
||||
for _, item := range got {
|
||||
if item.File == "docs/public.json" && item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
for _, forbidden := range []string{
|
||||
"real-json-token",
|
||||
"real secret value",
|
||||
"real-tenant-camel-token",
|
||||
"real-github-token",
|
||||
"real-vendor-key",
|
||||
"xoxb-real-token",
|
||||
} {
|
||||
for _, forbidden := range []string{providerValue, "xoxb_" + "1234567890abcdef"} {
|
||||
if strings.Contains(item.Excerpt, forbidden) {
|
||||
t.Fatalf("JSON credential finding leaked value %q in excerpt %q", forbidden, item.Excerpt)
|
||||
}
|
||||
@@ -306,8 +301,8 @@ func TestCollectDetectsAngleWrappedRealisticCredentialValues(t *testing.T) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count != 3 {
|
||||
t.Fatalf("angle-wrapped realistic credential findings = %d, want 3: %#v", count, got)
|
||||
if count != 2 {
|
||||
t.Fatalf("angle-wrapped provider credential findings = %d, want 2: %#v", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,12 +333,12 @@ func TestCollectDetectsCredentialShapedValuesUnderBenignKeys(t *testing.T) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count != 7 {
|
||||
t.Fatalf("credential-shaped benign-key findings = %d, want 7: %#v", count, got)
|
||||
if count != 4 {
|
||||
t.Fatalf("provider-shaped benign-key findings = %d, want 4: %#v", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectDetectsBareIdentifierCredentialsWithMetadataSuffixes(t *testing.T) {
|
||||
func TestCollectAllowsBareIdentifierCredentialsWithMetadataSuffixes(t *testing.T) {
|
||||
repo := newGitRepo(t)
|
||||
writeFile(t, filepath.Join(repo, "docs", "config.yaml"), "base: true\n")
|
||||
runGit(t, repo, "add", "docs/config.yaml")
|
||||
@@ -358,15 +353,11 @@ func TestCollectDetectsBareIdentifierCredentialsWithMetadataSuffixes(t *testing.
|
||||
runGit(t, repo, "commit", "-m", "add credential config")
|
||||
|
||||
got := collectFromPreviousCommit(t, repo)
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.File == "docs/config.yaml" && item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
t.Fatalf("readable metadata values should not be credential findings: %#v", got)
|
||||
}
|
||||
}
|
||||
if count != 3 {
|
||||
t.Fatalf("metadata-suffixed bare credential findings = %d, want 3: %#v", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectDetectsAccessKeyCredentials(t *testing.T) {
|
||||
@@ -374,7 +365,7 @@ func TestCollectDetectsAccessKeyCredentials(t *testing.T) {
|
||||
writeFile(t, filepath.Join(repo, "docs", "config.yaml"), "base: true\n")
|
||||
runGit(t, repo, "add", "docs/config.yaml")
|
||||
runGit(t, repo, "commit", "-m", "base")
|
||||
accessKey := "AK" + "IAIOSFODNN7EXAMPX"
|
||||
accessKey := "AK" + "IAIOSFODNN7EXAMPXX"
|
||||
|
||||
writeFile(t, filepath.Join(repo, "docs", "config.yaml"), strings.Join([]string{
|
||||
"AWS_ACCESS_KEY_ID: " + accessKey,
|
||||
@@ -391,7 +382,7 @@ func TestCollectDetectsAccessKeyCredentials(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
count++
|
||||
if strings.Contains(item.Excerpt, "AKIAIOSFODNN7EXAMPX") {
|
||||
if strings.Contains(item.Excerpt, accessKey) {
|
||||
t.Fatalf("access key finding leaked value in excerpt %q", item.Excerpt)
|
||||
}
|
||||
}
|
||||
@@ -432,7 +423,7 @@ func TestCollectDetectsPrivateKeyAssignments(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectDetectsCredentialValuesThatLookLikeBareIdentifiers(t *testing.T) {
|
||||
func TestCollectAllowsCredentialValuesThatLookLikeBareIdentifiers(t *testing.T) {
|
||||
repo := newGitRepo(t)
|
||||
writeFile(t, filepath.Join(repo, "docs", "config.yaml"), "base: true\n")
|
||||
runGit(t, repo, "add", "docs/config.yaml")
|
||||
@@ -448,15 +439,11 @@ func TestCollectDetectsCredentialValuesThatLookLikeBareIdentifiers(t *testing.T)
|
||||
runGit(t, repo, "commit", "-m", "add credential config")
|
||||
|
||||
got := collectFromPreviousCommit(t, repo)
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.File == "docs/config.yaml" && item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
t.Fatalf("readable identifiers should not be credential findings: %#v", got)
|
||||
}
|
||||
}
|
||||
if count != 4 {
|
||||
t.Fatalf("bare identifier credential findings = %d, want 4: %#v", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectAllowsBenignUnquotedTokenFields(t *testing.T) {
|
||||
@@ -489,12 +476,13 @@ func TestCollectDetectsCredentialPhraseBeforeEnvironmentSuffix(t *testing.T) {
|
||||
runGit(t, repo, "add", "docs/config.yaml")
|
||||
runGit(t, repo, "commit", "-m", "base")
|
||||
|
||||
providerValue := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
writeFile(t, filepath.Join(repo, "docs", "config.yaml"), strings.Join([]string{
|
||||
"API_KEY_OPENAI: real-openai-key",
|
||||
"TOKEN_GITHUB: real-github-token",
|
||||
"CLIENT_SECRET_GOOGLE: real-google-secret",
|
||||
"SECRET_KEY_BASE: real-secret-key-base",
|
||||
"APP_PASSWORD_PROD: real-prod-password",
|
||||
"API_KEY_OPENAI: " + providerValue,
|
||||
"TOKEN_GITHUB: " + providerValue,
|
||||
"CLIENT_SECRET_GOOGLE: " + providerValue,
|
||||
"SECRET_KEY_BASE: " + providerValue,
|
||||
"APP_PASSWORD_PROD: " + providerValue,
|
||||
}, "\n")+"\n")
|
||||
runGit(t, repo, "add", "docs/config.yaml")
|
||||
runGit(t, repo, "commit", "-m", "add credential config")
|
||||
@@ -506,13 +494,7 @@ func TestCollectDetectsCredentialPhraseBeforeEnvironmentSuffix(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
count++
|
||||
for _, forbidden := range []string{
|
||||
"real-openai-key",
|
||||
"real-github-token",
|
||||
"real-google-secret",
|
||||
"real-secret-key-base",
|
||||
"real-prod-password",
|
||||
} {
|
||||
for _, forbidden := range []string{providerValue} {
|
||||
if strings.Contains(item.Excerpt, forbidden) {
|
||||
t.Fatalf("credential finding leaked value %q in excerpt %q", forbidden, item.Excerpt)
|
||||
}
|
||||
@@ -621,7 +603,8 @@ func TestCollectSkipsOnlyKnownQualityGateFixtureFiles(t *testing.T) {
|
||||
writeFile(t, filepath.Join(repo, "internal", "qualitygate", "publiccontent", "scan_test.go"), "SECRET_TOKEN=fixture\n")
|
||||
writeFile(t, filepath.Join(repo, "internal", "qualitygate", "publiccontent", "scan.go"), "const privateKeyFixture = \""+privateKeyBeginPrefix+privateKeyMarker+"\"\n")
|
||||
writeFile(t, filepath.Join(repo, "internal", "qualitygate", "publiccontent", "rules.go"), "markers := []string{\"generated with automation\"}\n")
|
||||
writeFile(t, filepath.Join(repo, "tests", "e2e", "new-public-workflow.test.sh"), "SECRET_TOKEN=real-leak\n")
|
||||
providerValue := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
writeFile(t, filepath.Join(repo, "tests", "e2e", "new-public-workflow.test.sh"), "SECRET_TOKEN="+providerValue+"\n")
|
||||
runGit(t, repo, "add", ".")
|
||||
runGit(t, repo, "commit", "-m", "add scanner fixtures")
|
||||
|
||||
@@ -685,10 +668,11 @@ func TestCollectScansAddedLinesInSpecialPathNames(t *testing.T) {
|
||||
runGit(t, repo, "add", ".")
|
||||
runGit(t, repo, "commit", "-m", "base")
|
||||
|
||||
writeFile(t, filepath.Join(repo, "docs", "has space.md"), "SECRET_TOKEN=space-value\n")
|
||||
writeFile(t, filepath.Join(repo, `weird"quote.md`), "SECRET_TOKEN=quote-value\n")
|
||||
providerValue := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
writeFile(t, filepath.Join(repo, "docs", "has space.md"), "SECRET_TOKEN="+providerValue+"\n")
|
||||
writeFile(t, filepath.Join(repo, `weird"quote.md`), "SECRET_TOKEN="+providerValue+"\n")
|
||||
runGit(t, repo, "mv", "docs/old.md", "docs/new name.md")
|
||||
writeFile(t, filepath.Join(repo, "docs", "new name.md"), "base\nSECRET_TOKEN=rename-value\n")
|
||||
writeFile(t, filepath.Join(repo, "docs", "new name.md"), "base\nSECRET_TOKEN="+providerValue+"\n")
|
||||
runGit(t, repo, "add", ".")
|
||||
runGit(t, repo, "commit", "-m", "add special paths")
|
||||
|
||||
|
||||
@@ -4,8 +4,15 @@
|
||||
package publiccontent
|
||||
|
||||
func ScanComment(kind, body string) []Finding {
|
||||
return ScanCommentAtPath(kind, "", body)
|
||||
}
|
||||
|
||||
func ScanCommentAtPath(kind, path, body string) []Finding {
|
||||
if kind == "" {
|
||||
kind = "comment"
|
||||
}
|
||||
return scanText(kind, "comment", body, false)
|
||||
if path == "" {
|
||||
path = kind
|
||||
}
|
||||
return scanText(path, "comment", body, isDetectorRuleFile(path))
|
||||
}
|
||||
|
||||
@@ -3,7 +3,10 @@
|
||||
|
||||
package publiccontent
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestScanCommentAuditsPublishedCommentBodies(t *testing.T) {
|
||||
got := ScanComment("issue_comment", `The published comment included /tmp/harness`+`-agent/run and CCM`+`-Harness: stage-4`)
|
||||
@@ -17,3 +20,60 @@ func TestScanCommentAuditsPublishedCommentBodies(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanCommentAllowsMermaidCredentialTerminology(t *testing.T) {
|
||||
body := strings.Join([]string{
|
||||
"```mermaid",
|
||||
"sequenceDiagram",
|
||||
" participant Client",
|
||||
" participant AccessTokenHashTransport",
|
||||
" participant SecurityPolicyTransport",
|
||||
" Client->>AccessTokenHashTransport: Send request with bearer token",
|
||||
" AccessTokenHashTransport->>AccessTokenHashTransport: Clone request and inject token hash",
|
||||
" Client -> ClientSecret: Resolve configured credential",
|
||||
" AccessTokenHashTransport->>SecurityPolicyTransport: Forward enriched request",
|
||||
"```",
|
||||
}, "\n")
|
||||
|
||||
got := ScanComment("issue_comment", body)
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
t.Fatalf("mermaid credential terminology should not be a credential finding: %#v", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanCommentDetectsCredentialAssignmentInsideMermaidMessage(t *testing.T) {
|
||||
providerValue := strings.Join([]string{"gh", "p_", "1234567890abcdef", "1234567890abcdef", "1234"}, "")
|
||||
credentialAssignment := "password=" + providerValue
|
||||
body := strings.Join([]string{
|
||||
"```mermaid",
|
||||
"sequenceDiagram",
|
||||
" Client->>Server: Send " + credentialAssignment,
|
||||
"```",
|
||||
}, "\n")
|
||||
|
||||
got := ScanComment("issue_comment", body)
|
||||
if !findingRules(got)["public_content_generic_credential"] {
|
||||
t.Fatalf("credential assignment inside mermaid message should be reported: %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanCommentAtPathAllowsTestFixtureCredentialPlaceholder(t *testing.T) {
|
||||
body := `cfg := &core.CliConfig{AppID: "cli_x", AppSecret: "fake-secret"}`
|
||||
got := ScanCommentAtPath("pull_request_review_comment", "cmd/agent/list_test.go", body)
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
t.Fatalf("review comment test fixture should not be a credential finding: %#v", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanCommentAtPathDetectsProviderCredentialInTestFile(t *testing.T) {
|
||||
providerValue := strings.Join([]string{"gh", "p_", "1234567890abcdef", "1234567890abcdef", "1234"}, "")
|
||||
body := `cfg := &Config{AccessToken: "` + providerValue + `"}`
|
||||
got := ScanCommentAtPath("pull_request_review_comment", "cmd/agent/list_test.go", body)
|
||||
if !findingRules(got)["public_content_generic_credential"] {
|
||||
t.Fatalf("provider credential in review comment should be reported: %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
88
internal/qualitygate/publiccontent/credential.go
Normal file
88
internal/qualitygate/publiccontent/credential.go
Normal file
@@ -0,0 +1,88 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package publiccontent
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func credentialValueHasStrongEvidence(key, value string) bool {
|
||||
normalized := strings.TrimRight(strings.TrimSpace(value), ",;")
|
||||
normalized = strings.TrimSpace(strings.Trim(normalized, `"'<>`))
|
||||
candidates := credentialEvidenceCandidates(unwrapCredentialValue(normalized))
|
||||
for _, candidate := range candidates {
|
||||
if providerCredentialIdentifier(candidate) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if isCredentialMetadataField(key) {
|
||||
return false
|
||||
}
|
||||
for _, candidate := range candidates {
|
||||
if highEntropyCredentialValue(strings.ToLower(candidate)) || base64PaddedCredentialValue(candidate) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return percentEncodedCredentialValue(strings.ToLower(candidates[0])) ||
|
||||
commandSubstitutionLooksCredentialLike(strings.ToLower(normalized))
|
||||
}
|
||||
|
||||
func credentialEvidenceCandidates(value string) []string {
|
||||
candidates := []string{value}
|
||||
for range 3 {
|
||||
decoded, err := url.PathUnescape(value)
|
||||
if err != nil || decoded == value {
|
||||
break
|
||||
}
|
||||
candidates = append(candidates, decoded)
|
||||
value = decoded
|
||||
}
|
||||
return candidates
|
||||
}
|
||||
|
||||
func isCredentialMetadataField(key string) bool {
|
||||
if isBenignTokenField(key) {
|
||||
return true
|
||||
}
|
||||
parts := credentialKeyParts(strings.ReplaceAll(strings.ToLower(key), "-", "_"))
|
||||
if len(parts) < 2 {
|
||||
return false
|
||||
}
|
||||
switch parts[len(parts)-1] {
|
||||
case "hash", "id", "kind", "marker", "prefix", "transport":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func base64PaddedCredentialValue(value string) bool {
|
||||
if len(value) < 16 || !strings.HasSuffix(value, "=") {
|
||||
return false
|
||||
}
|
||||
if _, err := base64.StdEncoding.DecodeString(value); err != nil {
|
||||
return false
|
||||
}
|
||||
return shannonEntropy(strings.TrimRight(value, "=")) >= 3.5
|
||||
}
|
||||
|
||||
func percentEncodedCredentialValue(value string) bool {
|
||||
if len(value) < 16 {
|
||||
return false
|
||||
}
|
||||
var escapes int
|
||||
for i := 0; i+2 < len(value); i++ {
|
||||
if value[i] == '%' && isHexByte(value[i+1]) && isHexByte(value[i+2]) {
|
||||
escapes++
|
||||
i += 2
|
||||
}
|
||||
}
|
||||
return escapes >= 2
|
||||
}
|
||||
|
||||
func isHexByte(value byte) bool {
|
||||
return (value >= '0' && value <= '9') || (value >= 'a' && value <= 'f')
|
||||
}
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
credentialAssignmentRE = regexp.MustCompile(`(?i)["']?\b[A-Za-z0-9_-]*(?:api[_-]?key|access[_-]?key|private[_-]?key|secret|password|passwd|token|webhook|access[_-]?token|client[_-]?secret)[A-Za-z0-9_-]*\b["']?\s*[:=]\s*(?:"((?:\\.|[^"\\])*)"|'((?:\\.|[^'\\])*)'|(\$\([^)]*\))|(\$\{\{[^}]+\}\})|([^"'\s,}\]]+))`)
|
||||
credentialAssignmentRE = regexp.MustCompile(`(?i)["']?\b[A-Za-z0-9_-]*(?:api[_-]?key|access[_-]?key|private[_-]?key|secret|password|passwd|token|webhook|access[_-]?token|client[_-]?secret)[A-Za-z0-9_-]*\b["']?\s*(?::=|[:=])\s*(?:!!str\s+)?(?:"((?:\\.|[^"\\])*)"|'((?:\\.|[^'\\])*)'|(\x60[^\x60]*\x60)|(\$\([^)]*\))|(\$\{\{[^}]+\}\})|([^"'\x60\s,}\]]+))`)
|
||||
jwtLikeRE = regexp.MustCompile(`\b[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b`)
|
||||
credentialURLRE = regexp.MustCompile(`(?i)\b[a-z][a-z0-9+.-]*://[^/\s:@]*:[^@\s/]+@[^)\s]+`)
|
||||
bearerHeaderRE = regexp.MustCompile(`(?i)(?:\bAuthorization\s*:\s*Bearer\s+|["']Authorization["']\s*:\s*["']Bearer\s+)[A-Za-z0-9._+/=-]{12,}`)
|
||||
@@ -383,33 +383,63 @@ func anglePlaceholderIdentifier(value string) bool {
|
||||
}
|
||||
|
||||
func credentialShapedValue(value string) bool {
|
||||
normalized := strings.ToLower(strings.Trim(value, `"'<>`))
|
||||
normalized := strings.TrimSpace(strings.Trim(strings.TrimSpace(value), `"'<>`))
|
||||
return credentialShapedIdentifier(normalized)
|
||||
}
|
||||
|
||||
func credentialShapedIdentifier(value string) bool {
|
||||
return providerCredentialIdentifier(value)
|
||||
}
|
||||
|
||||
func providerCredentialIdentifier(value string) bool {
|
||||
value = strings.TrimSpace(value)
|
||||
switch {
|
||||
case strings.HasPrefix(value, "sk_live_"),
|
||||
strings.HasPrefix(value, "sk_test_"),
|
||||
strings.HasPrefix(value, "ghp_"),
|
||||
strings.HasPrefix(value, "gho_"),
|
||||
strings.HasPrefix(value, "ghu_"),
|
||||
strings.HasPrefix(value, "github_pat_"),
|
||||
strings.HasPrefix(value, "xoxb_"),
|
||||
strings.HasPrefix(value, "xoxp_"),
|
||||
strings.HasPrefix(value, "xoxa_"):
|
||||
return true
|
||||
case strings.HasPrefix(value, "real-") &&
|
||||
(strings.Contains(value, "secret") ||
|
||||
strings.Contains(value, "token") ||
|
||||
strings.Contains(value, "key") ||
|
||||
strings.Contains(value, "password")):
|
||||
case providerTokenWithBody(value, "sk_live_", 16, ""),
|
||||
providerTokenWithBody(value, "sk_test_", 16, ""),
|
||||
providerTokenWithBody(value, "ghp_", 16, ""),
|
||||
providerTokenWithBody(value, "gho_", 16, ""),
|
||||
providerTokenWithBody(value, "ghu_", 16, ""),
|
||||
providerTokenWithBody(value, "github_pat_", 16, "_"),
|
||||
providerTokenWithBody(value, "xoxb_", 16, "-"),
|
||||
providerTokenWithBody(value, "xoxp_", 16, "-"),
|
||||
providerTokenWithBody(value, "xoxa_", 16, "-"),
|
||||
providerTokenWithBody(value, "xoxb-", 16, "-"),
|
||||
providerTokenWithBody(value, "xoxp-", 16, "-"),
|
||||
providerTokenWithBody(value, "xoxa-", 16, "-"),
|
||||
awsAccessKeyIdentifier(value):
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func providerTokenWithBody(value, prefix string, minBodyLength int, separators string) bool {
|
||||
body, ok := strings.CutPrefix(value, prefix)
|
||||
if !ok || len(body) < minBodyLength {
|
||||
return false
|
||||
}
|
||||
for _, r := range body {
|
||||
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || strings.ContainsRune(separators, r) {
|
||||
continue
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func awsAccessKeyIdentifier(value string) bool {
|
||||
if len(value) != 20 || (!strings.HasPrefix(value, "AKIA") && !strings.HasPrefix(value, "ASIA")) {
|
||||
return false
|
||||
}
|
||||
for _, r := range value[4:] {
|
||||
if (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') {
|
||||
continue
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func resourceTokenPlaceholderValue(value string) bool {
|
||||
normalized := strings.ToLower(strings.Trim(value, `"'`))
|
||||
switch normalized {
|
||||
|
||||
@@ -47,15 +47,30 @@ func scanText(file, source, text string, detectorFile bool) []Finding {
|
||||
out = append(out, newFinding("public_content_private_key_block", file, privateKeyLine, source, "private key block"))
|
||||
inPrivateKey = false
|
||||
}
|
||||
for _, match := range credentialAssignmentRE.FindAllStringSubmatch(line, -1) {
|
||||
if !isCredentialAssignmentMatch(match[0]) {
|
||||
for _, location := range credentialAssignmentRE.FindAllStringIndex(line, -1) {
|
||||
rawMatch := line[location[0]:location[1]]
|
||||
if !validCredentialAssignmentStart(line, location[0], rawMatch) {
|
||||
continue
|
||||
}
|
||||
match := credentialAssignmentRE.FindStringSubmatch(rawMatch)
|
||||
if !isCredentialAssignmentMatch(rawMatch) {
|
||||
continue
|
||||
}
|
||||
value := credentialAssignmentValue(match)
|
||||
keyName, _ := normalizedCredentialAssignmentKey(match[0])
|
||||
keyName, _ := normalizedCredentialAssignmentKey(rawMatch)
|
||||
evidenceValue := value
|
||||
if sourceCodeFile(file) {
|
||||
if rhs, ok := sourceCodeTypedCredentialRHS(line, location[0], rawMatch); ok {
|
||||
evidenceValue = rhs
|
||||
}
|
||||
}
|
||||
if !(isWebhookCredentialKey(keyName) && webhookAssignmentValueLooksCredentialLike(value)) &&
|
||||
!credentialValueHasStrongEvidence(keyName, evidenceValue) {
|
||||
continue
|
||||
}
|
||||
if value == "" ||
|
||||
isNonSecretLiteralValue(value) ||
|
||||
isBenignCodeCredentialExpression(file, line, match[0], value) ||
|
||||
isBenignCodeCredentialExpression(file, line, location[0], rawMatch, value) ||
|
||||
isPlaceholderValue(value) ||
|
||||
isPermissionScopeIdentifierAssignment(keyName, value) ||
|
||||
isResourceTokenPlaceholderAssignment(keyName, value) {
|
||||
@@ -64,7 +79,7 @@ func scanText(file, source, text string, detectorFile bool) []Finding {
|
||||
if looksLikeEqualityComparison(value) {
|
||||
continue
|
||||
}
|
||||
out = append(out, newFinding("public_content_generic_credential", file, lineNo, source, redactAssignment(match[0])))
|
||||
out = append(out, newFinding("public_content_generic_credential", file, lineNo, source, redactAssignment(rawMatch)))
|
||||
}
|
||||
for _, match := range jwtLikeRE.FindAllString(line, -1) {
|
||||
if !isJWTToken(match) {
|
||||
@@ -123,21 +138,43 @@ func scanText(file, source, text string, detectorFile bool) []Finding {
|
||||
return out
|
||||
}
|
||||
|
||||
func validCredentialAssignmentStart(line string, start int, match string) bool {
|
||||
if start <= 0 || credentialAssignmentOperator(match) != ":" {
|
||||
return true
|
||||
}
|
||||
prefix := strings.TrimSpace(line[:start])
|
||||
for _, arrow := range []string{"-->>", "->>", "-->", "->"} {
|
||||
if strings.HasSuffix(prefix, arrow) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func credentialAssignmentOperator(match string) string {
|
||||
key, ok := credentialAssignmentKey(match)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
rest := strings.TrimSpace(match[len(key):])
|
||||
if strings.HasPrefix(rest, ":=") {
|
||||
return ":="
|
||||
}
|
||||
if strings.HasPrefix(rest, ":") {
|
||||
return ":"
|
||||
}
|
||||
if strings.HasPrefix(rest, "=") {
|
||||
return "="
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func isCredentialAssignmentMatch(match string) bool {
|
||||
name, value, ok := normalizedCredentialAssignment(match)
|
||||
name, _, ok := normalizedCredentialAssignment(match)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if isWebhookCredentialKey(name) && webhookAssignmentValueLooksCredentialLike(value) {
|
||||
return true
|
||||
}
|
||||
if isBenignTokenField(name) && !credentialShapedValue(value) {
|
||||
return false
|
||||
}
|
||||
if isWeakTokenCredentialKey(name) && !weakTokenValueLooksCredentialLike(value) {
|
||||
return false
|
||||
}
|
||||
return isExplicitCredentialKey(name)
|
||||
return isExplicitCredentialKey(name) || isWebhookCredentialKey(name)
|
||||
}
|
||||
|
||||
func normalizedCredentialAssignmentKey(match string) (string, bool) {
|
||||
@@ -288,7 +325,7 @@ func tokenLikePlaceholderKey(key string) bool {
|
||||
|
||||
func tokenLikePlaceholderValue(key, value string) bool {
|
||||
normalized := strings.ToLower(strings.Trim(value, `"'`))
|
||||
if normalized == "" || credentialShapedIdentifier(normalized) {
|
||||
if normalized == "" || credentialShapedIdentifier(strings.Trim(value, `"'`)) {
|
||||
return false
|
||||
}
|
||||
if authCredentialTokenKey(key) {
|
||||
@@ -323,52 +360,8 @@ func maskedTokenFixturePlaceholderValue(key, value string) bool {
|
||||
return stars >= 6 && alnum > 0
|
||||
}
|
||||
|
||||
func isWeakTokenCredentialKey(key string) bool {
|
||||
if authCredentialTokenKey(key) || isStrongTokenCredentialKey(key) {
|
||||
return false
|
||||
}
|
||||
return key == "token" ||
|
||||
strings.HasSuffix(key, "_token") ||
|
||||
strings.HasSuffix(key, "-token")
|
||||
}
|
||||
|
||||
func isStrongTokenCredentialKey(key string) bool {
|
||||
parts := credentialKeyParts(strings.ReplaceAll(strings.ToLower(key), "-", "_"))
|
||||
for _, phrase := range [][2]string{
|
||||
{"access", "token"},
|
||||
{"refresh", "token"},
|
||||
{"auth", "token"},
|
||||
{"bearer", "token"},
|
||||
{"session", "token"},
|
||||
{"service", "token"},
|
||||
{"bot", "token"},
|
||||
{"api", "token"},
|
||||
{"secret", "token"},
|
||||
} {
|
||||
if hasAdjacentCredentialParts(parts, phrase[0], phrase[1]) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func weakTokenValueLooksCredentialLike(value string) bool {
|
||||
normalized := strings.ToLower(strings.Trim(value, `"'<>`))
|
||||
if normalized == "" ||
|
||||
isNonSecretLiteralValue(value) ||
|
||||
isPlaceholderValue(value) {
|
||||
return false
|
||||
}
|
||||
candidate := unwrapCredentialValue(normalized)
|
||||
return credentialShapedIdentifier(candidate) ||
|
||||
highEntropyCredentialValue(candidate) ||
|
||||
commandSubstitutionLooksCredentialLike(normalized) ||
|
||||
(strings.Contains(normalized, "://") &&
|
||||
urlRemainderLooksCredentialLike(removeAnglePlaceholders(normalized)))
|
||||
}
|
||||
|
||||
func unwrapCredentialValue(value string) string {
|
||||
value = strings.TrimSpace(strings.Trim(value, `"'<>`))
|
||||
value = strings.TrimSpace(strings.Trim(value, "\"'<>`"))
|
||||
if strings.HasPrefix(value, "${{") && strings.HasSuffix(value, "}}") {
|
||||
value = strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(value, "${{"), "}}"))
|
||||
}
|
||||
@@ -488,17 +481,20 @@ func numericStringPlaceholderValue(value string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func isBenignCodeCredentialExpression(file, line, match, value string) bool {
|
||||
func isBenignCodeCredentialExpression(file, line string, matchStart int, match, value string) bool {
|
||||
normalized := strings.TrimSpace(value)
|
||||
if strings.HasPrefix(normalized, "regexp.MustCompile(") {
|
||||
return true
|
||||
}
|
||||
if !sourceCodeFile(file) || credentialShapedValue(value) {
|
||||
if !sourceCodeFile(file) {
|
||||
return false
|
||||
}
|
||||
if rhs, ok := sourceCodeTypedCredentialRHS(line, match); ok {
|
||||
if rhs, ok := sourceCodeTypedCredentialRHS(line, matchStart, match); ok {
|
||||
return isBenignTypedCredentialRHS(rhs)
|
||||
}
|
||||
if credentialShapedValue(value) {
|
||||
return false
|
||||
}
|
||||
rawValueQuoted := credentialAssignmentRawValueQuoted(match)
|
||||
if sourceCodeLiteralLooksNonSecret(normalized, !rawValueQuoted) {
|
||||
return true
|
||||
@@ -518,17 +514,16 @@ func isBenignCodeCredentialExpression(file, line, match, value string) bool {
|
||||
return codeReferenceExpression(normalized)
|
||||
}
|
||||
|
||||
func sourceCodeTypedCredentialRHS(line, match string) (string, bool) {
|
||||
idx := strings.Index(line, match)
|
||||
if idx < 0 {
|
||||
func sourceCodeTypedCredentialRHS(line string, matchStart int, match string) (string, bool) {
|
||||
if matchStart < 0 || matchStart+len(match) > len(line) || line[matchStart:matchStart+len(match)] != match {
|
||||
return "", false
|
||||
}
|
||||
key, ok := credentialAssignmentKey(match)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
rest := strings.TrimSpace(line[idx+len(key):])
|
||||
if !strings.HasPrefix(rest, ":") {
|
||||
rest := strings.TrimSpace(line[matchStart+len(key):])
|
||||
if !strings.HasPrefix(rest, ":") || strings.HasPrefix(rest, ":=") {
|
||||
return "", false
|
||||
}
|
||||
typeAndRHS := strings.TrimSpace(strings.TrimPrefix(rest, ":"))
|
||||
@@ -536,7 +531,12 @@ func sourceCodeTypedCredentialRHS(line, match string) (string, bool) {
|
||||
if assignmentIdx < 0 {
|
||||
return "", false
|
||||
}
|
||||
return strings.TrimSpace(typeAndRHS[assignmentIdx+1:]), true
|
||||
rhs := strings.TrimSpace(typeAndRHS[assignmentIdx+1:])
|
||||
parsed := credentialAssignmentRE.FindStringSubmatch("client_secret=" + rhs)
|
||||
if parsed == nil {
|
||||
return rhs, true
|
||||
}
|
||||
return credentialAssignmentValue(parsed), true
|
||||
}
|
||||
|
||||
func isBenignTypedCredentialRHS(value string) bool {
|
||||
@@ -568,7 +568,7 @@ func credentialAssignmentRawValueQuoted(match string) bool {
|
||||
|
||||
func sourceCodeFile(file string) bool {
|
||||
switch filepath.Ext(file) {
|
||||
case ".go", ".js", ".jsx", ".py", ".ts", ".tsx":
|
||||
case ".go", ".js", ".jsx", ".py", ".sh", ".ts", ".tsx":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
@@ -593,6 +593,7 @@ func sourceCodeLiteralLooksNonSecret(value string, allowNumeric bool) bool {
|
||||
sourceCodeFakeOrPlaceholderLiteral(literal) ||
|
||||
sourceCodeCredentialTermLiteral(literal) ||
|
||||
sourceCodeCredentialPrefixLiteral(literal) ||
|
||||
sourceCodeStringExpressionLiteral(literal) ||
|
||||
sourceCodeVocabularyLiteral(literal) ||
|
||||
sourceCodeSchemaTypeLiteral(literal) ||
|
||||
benignCredentialStatusLiteral(literal)
|
||||
@@ -685,6 +686,18 @@ func sourceCodeCredentialPrefixLiteral(value string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func sourceCodeStringExpressionLiteral(value string) bool {
|
||||
normalized := strings.TrimSpace(value)
|
||||
if normalized == "" ||
|
||||
credentialShapedIdentifier(normalized) ||
|
||||
highEntropyCredentialValue(strings.ToLower(normalized)) {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(normalized, "${") ||
|
||||
strings.Contains(normalized, "$(") ||
|
||||
(strings.Contains(normalized, `\b`) && strings.ContainsAny(normalized, "|[]{}()+*?"))
|
||||
}
|
||||
|
||||
func sourceCodeVocabularyLiteral(value string) bool {
|
||||
switch strings.ToLower(value) {
|
||||
case "bot", "tenant", "user":
|
||||
@@ -753,7 +766,7 @@ func codeIdentifier(value string) bool {
|
||||
|
||||
func isNonSecretLiteralValue(value string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(strings.Trim(value, `"'`))) {
|
||||
case "true", "false", "null", "nil", "{", "[":
|
||||
case "true", "false", "null", "nil", "{", "[", `\`:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
@@ -980,6 +993,7 @@ func credentialURLPasswordFixture(password string) bool {
|
||||
normalized := strings.ToLower(strings.Trim(password, `"'`))
|
||||
switch normalized {
|
||||
case "p",
|
||||
"p%40ss",
|
||||
"pass",
|
||||
"password",
|
||||
"pat_abc",
|
||||
|
||||
@@ -251,26 +251,22 @@ func TestScanFileDoesNotTreatURLEncodedCredentialAsPlaceholder(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDoesNotTreatPlaceholderMarkerSubstringsAsPlaceholders(t *testing.T) {
|
||||
func TestScanFileAllowsReadablePlaceholderMarkerSubstrings(t *testing.T) {
|
||||
got := ScanFile("docs/config.md", []byte(strings.Join([]string{
|
||||
"API_KEY=notredactedreal",
|
||||
"API_KEY=notplaceholdersecret",
|
||||
"API_KEY=abcxxxxreal",
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
t.Fatalf("readable credential words should not be findings: %#v", got)
|
||||
}
|
||||
}
|
||||
if count != 3 {
|
||||
t.Fatalf("placeholder-marker substring findings = %d, want 3: %#v", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsBase64PaddedCredentialAssignments(t *testing.T) {
|
||||
paddedSecretPrefix := "dGhpc2lz" + "YXNlY3JldA"
|
||||
paddedTokenPrefix := "YWJj" + "ZGVmZ2g"
|
||||
paddedTokenPrefix := "UTdrMm1O" + "OXBSNHZYOA"
|
||||
paddedSecret := base64PaddedFixture(paddedSecretPrefix)
|
||||
paddedToken := base64PaddedFixture(paddedTokenPrefix)
|
||||
got := ScanFile("docs/config.md", []byte(strings.Join([]string{
|
||||
@@ -294,17 +290,25 @@ func TestScanFileDetectsBase64PaddedCredentialAssignments(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileAllowsReadableBase64Lookalike(t *testing.T) {
|
||||
got := ScanFile("docs/config.md", []byte("client_secret=placeholder=\n"))
|
||||
if findingRules(got)["public_content_generic_credential"] {
|
||||
t.Fatalf("readable base64 lookalike should not be a credential finding: %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsQuotedJSONCredentialAssignments(t *testing.T) {
|
||||
jsonToken := "real-json-token"
|
||||
jsonSecret := "real " + "secret value"
|
||||
jsonKey := "real-json-key"
|
||||
jsonTenantToken := "real-tenant-json-token"
|
||||
jsonAppSecret := "real-app-secret"
|
||||
jsonPrefixedKey := "real-prefixed-key"
|
||||
jsonTenantCamelToken := "real-tenant-camel-token"
|
||||
jsonGithubToken := "real-github-token"
|
||||
jsonVendorKey := "real-vendor-key"
|
||||
jsonSlackBotToken := "xoxb-real-token"
|
||||
providerValue := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
jsonToken := providerValue
|
||||
jsonSecret := providerValue
|
||||
jsonKey := providerValue
|
||||
jsonTenantToken := providerValue
|
||||
jsonAppSecret := providerValue
|
||||
jsonPrefixedKey := providerValue
|
||||
jsonTenantCamelToken := providerValue
|
||||
jsonGithubToken := providerValue
|
||||
jsonVendorKey := providerValue
|
||||
jsonSlackBotToken := "xoxb_" + "1234567890abcdef"
|
||||
got := ScanFile("docs/public.json", []byte(strings.Join([]string{
|
||||
`{"access_` + `token":"` + jsonToken + `"}`,
|
||||
`{"client_` + `secret": "` + jsonSecret + `"}`,
|
||||
@@ -334,12 +338,13 @@ func TestScanFileDetectsQuotedJSONCredentialAssignments(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestScanFileDetectsCredentialPhraseBeforeEnvironmentSuffix(t *testing.T) {
|
||||
providerValue := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
got := ScanFile("docs/config.yaml", []byte(strings.Join([]string{
|
||||
"API_KEY_OPENAI: real-openai-key",
|
||||
"TOKEN_GITHUB: real-github-token",
|
||||
"CLIENT_SECRET_GOOGLE: real-google-secret",
|
||||
"SECRET_KEY_BASE: real-secret-key-base",
|
||||
"APP_PASSWORD_PROD: real-prod-password",
|
||||
"API_KEY_OPENAI: " + providerValue,
|
||||
"TOKEN_GITHUB: " + providerValue,
|
||||
"CLIENT_SECRET_GOOGLE: " + providerValue,
|
||||
"SECRET_KEY_BASE: " + providerValue,
|
||||
"APP_PASSWORD_PROD: " + providerValue,
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
@@ -347,13 +352,7 @@ func TestScanFileDetectsCredentialPhraseBeforeEnvironmentSuffix(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
count++
|
||||
for _, forbidden := range []string{
|
||||
"real-openai-key",
|
||||
"real-github-token",
|
||||
"real-google-secret",
|
||||
"real-secret-key-base",
|
||||
"real-prod-password",
|
||||
} {
|
||||
for _, forbidden := range []string{providerValue} {
|
||||
if strings.Contains(item.Excerpt, forbidden) {
|
||||
t.Fatalf("credential finding leaked value %q in excerpt %q", forbidden, item.Excerpt)
|
||||
}
|
||||
@@ -364,85 +363,77 @@ func TestScanFileDetectsCredentialPhraseBeforeEnvironmentSuffix(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsCredentialValuesThatLookLikeBareIdentifiers(t *testing.T) {
|
||||
func TestScanFileAllowsCredentialValuesThatLookLikeBareIdentifiers(t *testing.T) {
|
||||
got := ScanFile("docs/config.yaml", []byte(strings.Join([]string{
|
||||
"API_KEY_OPENAI: prod_key",
|
||||
"CLIENT_SECRET_GOOGLE: prod_secret",
|
||||
"TOKEN_GITHUB: github_token",
|
||||
"APP_PASSWORD_PROD: prod_password",
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
t.Fatalf("readable identifiers should not be credential findings: %#v", got)
|
||||
}
|
||||
}
|
||||
if count != 4 {
|
||||
t.Fatalf("bare identifier credential findings = %d, want 4: %#v", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsAngleWrappedRealisticCredentialValues(t *testing.T) {
|
||||
stripeLike := "sk_" + "live_1234567890abcdef"
|
||||
patLike := "gh" + "p_1234567890abcdef1234567890abcdef1234"
|
||||
got := ScanFile("docs/config.yaml", []byte(strings.Join([]string{
|
||||
"API_KEY: <" + stripeLike + ">",
|
||||
"SECRET_TOKEN: <" + patLike + ">",
|
||||
"CLIENT_SECRET: <real-client-secret-value>",
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
text string
|
||||
want bool
|
||||
}{
|
||||
{name: "stripe", text: "API_KEY: <" + stripeLike + ">", want: true},
|
||||
{name: "github", text: "SECRET_TOKEN: <" + patLike + ">", want: true},
|
||||
{name: "readable", text: "CLIENT_SECRET: <real-client-secret-value>", want: false},
|
||||
}
|
||||
if count != 3 {
|
||||
t.Fatalf("angle-wrapped realistic credential findings = %d, want 3: %#v", count, got)
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assertGenericCredentialFinding(t, "docs/config.yaml", tc.text, tc.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsCredentialShapedValuesUnderBenignKeys(t *testing.T) {
|
||||
stripeLike := "sk_" + "live_1234567890abcdef"
|
||||
patLike := "gh" + "p_1234567890abcdef1234567890abcdef1234"
|
||||
got := ScanFile("docs/public.json", []byte(strings.Join([]string{
|
||||
`{"access_token_expires_in":"` + patLike + `"}`,
|
||||
`{"refresh_token_expires_in":"` + stripeLike + `"}`,
|
||||
`{"client_secret_status":"real-client-secret-value"}`,
|
||||
`{"client_secret_name":"real-client-secret-value"}`,
|
||||
`{"app_token":"` + patLike + `"}`,
|
||||
`{"sync_token":"` + stripeLike + `"}`,
|
||||
`{"target_token":"real-client-secret-value"}`,
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
text string
|
||||
want bool
|
||||
}{
|
||||
{name: "expiry provider token", text: `{"access_token_expires_in":"` + patLike + `"}`, want: true},
|
||||
{name: "expiry provider secret", text: `{"refresh_token_expires_in":"` + stripeLike + `"}`, want: true},
|
||||
{name: "status readable", text: `{"client_secret_status":"real-client-secret-value"}`, want: false},
|
||||
{name: "name readable", text: `{"client_secret_name":"real-client-secret-value"}`, want: false},
|
||||
{name: "app provider token", text: `{"app_token":"` + patLike + `"}`, want: true},
|
||||
{name: "sync provider secret", text: `{"sync_token":"` + stripeLike + `"}`, want: true},
|
||||
{name: "target readable", text: `{"target_token":"real-client-secret-value"}`, want: false},
|
||||
}
|
||||
if count != 7 {
|
||||
t.Fatalf("credential-shaped benign-key findings = %d, want 7: %#v", count, got)
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assertGenericCredentialFinding(t, "docs/public.json", tc.text, tc.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsBareIdentifierCredentialsWithMetadataSuffixes(t *testing.T) {
|
||||
func TestScanFileAllowsBareIdentifierCredentialsWithMetadataSuffixes(t *testing.T) {
|
||||
got := ScanFile("docs/config.yaml", []byte(strings.Join([]string{
|
||||
"API_KEY_NAME: prod_key",
|
||||
"CLIENT_SECRET_NAME: prod_secret",
|
||||
"SECRET_STATUS: prod_secret",
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
t.Fatalf("readable metadata values should not be credential findings: %#v", got)
|
||||
}
|
||||
}
|
||||
if count != 3 {
|
||||
t.Fatalf("metadata-suffixed bare credential findings = %d, want 3: %#v", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsAccessKeyCredentials(t *testing.T) {
|
||||
accessKey := "AK" + "IAIOSFODNN7EXAMPX"
|
||||
accessKey := "AK" + "IAIOSFODNN7EXAMPXX"
|
||||
got := ScanFile("docs/config.yaml", []byte(strings.Join([]string{
|
||||
"AWS_ACCESS_KEY_ID: " + accessKey,
|
||||
"ACCESS_KEY_ID: " + accessKey,
|
||||
@@ -593,18 +584,18 @@ func TestScanFileAllowsCredentialReferenceValues(t *testing.T) {
|
||||
|
||||
func TestScanFileDetectsMalformedGithubExpressionCredentialValues(t *testing.T) {
|
||||
stripeLike := "sk_" + "live_1234567890abcdef"
|
||||
got := ScanFile("docs/config.yaml", []byte(strings.Join([]string{
|
||||
"API_KEY=${{" + stripeLike + "}}",
|
||||
"TOKEN=${{real-secret-token-value}}",
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
text string
|
||||
want bool
|
||||
}{
|
||||
{name: "provider", text: "API_KEY=${{" + stripeLike + "}}", want: true},
|
||||
{name: "readable", text: "TOKEN=${{real-secret-token-value}}", want: false},
|
||||
}
|
||||
if count != 2 {
|
||||
t.Fatalf("malformed GitHub expression credential findings = %d, want 2: %#v", count, got)
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assertGenericCredentialFinding(t, "docs/config.yaml", tc.text, tc.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -648,6 +639,7 @@ func TestScanFileAllowsCredentialURLPlaceholders(t *testing.T) {
|
||||
func TestScanFileAllowsCredentialURLFixtures(t *testing.T) {
|
||||
got := ScanFile("fixtures/network_test.go", []byte(strings.Join([]string{
|
||||
`proxy := "http://user:pass@proxy:8080"`,
|
||||
`proxy := "http://user:p%40ss@proxy:8080/path"`,
|
||||
`repo := "https://u:t@h/r.git"`,
|
||||
`target := "https://attacker:pw@open.feishu.cn"`,
|
||||
`proxy := "http://admin:s3cret@127.0.0.1:3128"`,
|
||||
@@ -821,26 +813,36 @@ func TestScanFileDetectsWeakTokenFieldsWithHighConfidenceCredentialValues(t *tes
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsStrongAuthTokenKeysWithFixtureLikeValues(t *testing.T) {
|
||||
func TestScanFileAllowsStrongAuthTokenKeysWithoutStrongValueEvidence(t *testing.T) {
|
||||
got := ScanFile("docs/config.md", []byte(strings.Join([]string{
|
||||
`{"access_token":"img_abc123"}`,
|
||||
`{"api_token":"img_live_secret"}`,
|
||||
`{"service_token":"ab********cd"}`,
|
||||
`{"bot_token":"board_v3_example"}`,
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
t.Fatalf("token field names alone should not produce findings: %#v", got)
|
||||
}
|
||||
}
|
||||
if count != 4 {
|
||||
t.Fatalf("strong auth token key findings = %d, want 4: %#v", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileAllowsTestFixtureSecretValues(t *testing.T) {
|
||||
got := ScanFile("fixtures/calendar_meeting_test.go", []byte(`AppID: "test-app", AppSecret: "test-secret", Brand: core.BrandFeishu,`+"\n"))
|
||||
got := ScanFile("fixtures/calendar_meeting_test.go", []byte(strings.Join([]string{
|
||||
`AppID: "test-app", AppSecret: "test-secret", Brand: core.BrandFeishu,`,
|
||||
`cfg := &core.CliConfig{AppID: "a", AppSecret: "s"}`,
|
||||
`os.WriteFile(path, []byte("FEISHU_APP_ID=cli_abc\nFEISHU_APP_SECRET=secret\n"), 0600)`,
|
||||
`rt := &stubRoundTripper{respBody: ` + "`" + `{"access_token":"t","token_type":"Bearer"}` + "`" + `}`,
|
||||
`envContent := "FEISHU_APP_ID=cli_hermes_abc\nFEISHU_APP_SECRET=hermes_secret_123\nFEISHU_DOMAIN=lark\n"`,
|
||||
`os.WriteFile(path, []byte("FEISHU_APP_ID=cli_auto\nFEISHU_APP_SECRET=auto_secret\n"), 0600)`,
|
||||
`os.WriteFile(path, []byte("FEISHU_APP_ID=cli_new_app\nFEISHU_APP_SECRET=new_secret\n"), 0600)`,
|
||||
`if got := out.String(); got != "username=x-access-token\npassword=valid-pat\n\n" {`,
|
||||
`if got := out.String(); got != "username=x-access-token\npassword=restored-pat\n\n" {`,
|
||||
`if got := stdout.String(); got != "username=x-access-token\npassword=pat-token\n\n" {`,
|
||||
`return &core.CliConfig{AppID: "dummy", AppSecret: "dummy"}`,
|
||||
`os.WriteFile(path, []byte("API_KEY=replace-me\n"), 0600)`,
|
||||
`body := "APP_ID=\"cli_xxxxx\"\nAPP_SECRET=\"xxxxx\"\n"`,
|
||||
}, "\n")+"\n"))
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
t.Fatalf("test fixture secret should not be credential finding: %#v", got)
|
||||
@@ -848,8 +850,114 @@ func TestScanFileAllowsTestFixtureSecretValues(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileAllowsCredentialIdentifierFields(t *testing.T) {
|
||||
got := ScanFile("fixtures/openapi_key_test.go", []byte(strings.Join([]string{
|
||||
`"api_key_id": "k1",`,
|
||||
`"secret_id": "s1",`,
|
||||
`"token_id": "t1",`,
|
||||
`"private_key_id": "pk1",`,
|
||||
}, "\n")+"\n"))
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
t.Fatalf("credential identifier fields should not be credential findings: %#v", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsCredentialShapedIdentifierFieldValues(t *testing.T) {
|
||||
stripeLike := "sk_" + "live_1234567890abcdef"
|
||||
githubToken := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
got := ScanFile("fixtures/openapi_key_test.go", []byte(strings.Join([]string{
|
||||
`"api_key_id": "` + stripeLike + `",`,
|
||||
`"token_id": "` + githubToken + `",`,
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count != 2 {
|
||||
t.Fatalf("credential-shaped identifier field findings = %d, want 2: %#v", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCredentialShapedValueTrimsWhitespaceBeforeDelimiters(t *testing.T) {
|
||||
providerValue := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
if !credentialShapedValue(` "` + providerValue + `" `) {
|
||||
t.Fatal("space-padded quoted provider credential should be recognized")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsProviderCredentialsAcrossAssignmentSyntaxes(t *testing.T) {
|
||||
providerValue := strings.Join([]string{"gh", "p_", "1234567890abcdef", "1234567890abcdef", "1234"}, "")
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
text string
|
||||
}{
|
||||
{name: "Go raw string", path: "pkg/config.go", text: "const clientSecret = `" + providerValue + "`"},
|
||||
{name: "TypeScript template literal", path: "pkg/config.ts", text: "const clientSecret = `" + providerValue + "`;"},
|
||||
{name: "shell backtick", path: "scripts/config.sh", text: "client_secret=`" + providerValue + "`"},
|
||||
{name: "YAML string tag", path: "docs/config.yaml", text: "client_secret: !!str " + providerValue},
|
||||
{name: "YAML string tag double quoted", path: "docs/config.yaml", text: `client_secret: !!str "` + providerValue + `"`},
|
||||
{name: "YAML string tag single quoted", path: "docs/config.yaml", text: `client_secret: !!str '` + providerValue + `'`},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := ScanFile(tt.path, []byte(tt.text+"\n"))
|
||||
if !findingRules(got)["public_content_generic_credential"] {
|
||||
t.Fatalf("provider credential should be reported: %#v", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsPercentEncodedProviderCredential(t *testing.T) {
|
||||
providerBody := strings.Join([]string{"1234567890abcdef", "1234567890abcdef", "1234"}, "")
|
||||
tests := []string{
|
||||
"access_token: ghp%" + "5F" + providerBody,
|
||||
"access_token_hash: ghp%" + "255F" + providerBody,
|
||||
}
|
||||
for _, text := range tests {
|
||||
got := ScanFile("docs/config.yaml", []byte(text+"\n"))
|
||||
if !findingRules(got)["public_content_generic_credential"] {
|
||||
t.Fatalf("percent-encoded provider credential should be reported: %#v", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileRequiresCompleteProviderCredentialFormats(t *testing.T) {
|
||||
got := ScanFile("docs/config.yaml", []byte(strings.Join([]string{
|
||||
"token_type: asian",
|
||||
"token_prefix: ASIA",
|
||||
"token_prefix: ghp_",
|
||||
"api_key: sk_live_example",
|
||||
"token_prefix: asianmarketsegment01",
|
||||
"token_prefix: ghp_placeholder_value",
|
||||
}, "\n")+"\n"))
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
t.Fatalf("incomplete provider prefixes should not be credential findings: %#v", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileAllowsEncodedTokenMetadataURL(t *testing.T) {
|
||||
got := ScanFile("docs/config.yaml", []byte("token_url: https%3A%2F%2Fexample.invalid/oauth/token\n"))
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
t.Fatalf("encoded token metadata URL should not be credential finding: %#v", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileAllowsRegexpTokenValidators(t *testing.T) {
|
||||
got := ScanFile("fixtures/minutes_detail.go", []byte("var validMinuteTokenDetail = regexp.MustCompile(`^[a-z0-9]+$`)\n"))
|
||||
got := ScanFile("fixtures/minutes_detail.go", []byte(strings.Join([]string{
|
||||
"var validMinuteTokenDetail = regexp.MustCompile(`^[a-z0-9]+$`)",
|
||||
"REALISTIC_TOKEN_RE=\"\\\"${TOKEN_BODY}\\\"|\\`${TOKEN_BODY}\\`|\\\\b${TOKEN_BODY}\\\\b\"",
|
||||
}, "\n")+"\n"))
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
t.Fatalf("regexp token validator should not be credential finding: %#v", got)
|
||||
@@ -927,6 +1035,22 @@ func TestScanFileAllowsSourceCodeCredentialNonSecretLiterals(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileAllowsSourceCodeSyntheticCredentialIdentifiers(t *testing.T) {
|
||||
got := ScanFile("fixtures/sheets_media.go", []byte(strings.Join([]string{
|
||||
`const fakeOfficeTokenPrefix = "fake_office_"`,
|
||||
`const localOfficeTokenPrefix = "local_office_"`,
|
||||
`const imageLiveSecretMarker = "img_live_secret"`,
|
||||
`const imageProdKeyMarker = "img_prod_key"`,
|
||||
`if strings.HasPrefix(spreadsheetToken, fakeOfficeTokenPrefix) {`,
|
||||
`if strings.HasPrefix(spreadsheetToken, localOfficeTokenPrefix) {`,
|
||||
}, "\n")+"\n"))
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
t.Fatalf("source code token prefix references should not be credential findings: %#v", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileAllowsCredentialLikePublicPlaceholders(t *testing.T) {
|
||||
got := ScanFile("fixtures/placeholders.md", []byte(strings.Join([]string{
|
||||
`app_secret=***`,
|
||||
@@ -941,22 +1065,18 @@ func TestScanFileAllowsCredentialLikePublicPlaceholders(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsPartiallyMaskedCredentialValues(t *testing.T) {
|
||||
func TestScanFileAllowsPartiallyMaskedCredentialValues(t *testing.T) {
|
||||
got := ScanFile("fixtures/config.md", []byte(strings.Join([]string{
|
||||
"client_secret=realprefix***realsuffix",
|
||||
"client_secret=ab********cd",
|
||||
"access_token=ab********cd",
|
||||
"refresh_token=realprefix********realsuffix",
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
t.Fatalf("partially masked values should not be credential findings: %#v", got)
|
||||
}
|
||||
}
|
||||
if count != 4 {
|
||||
t.Fatalf("partially masked credential findings = %d, want 4: %#v", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileAllowsDryRunCredentialPlaceholders(t *testing.T) {
|
||||
@@ -972,6 +1092,7 @@ func TestScanFileAllowsDryRunCredentialPlaceholders(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestScanFileDetectsTypedCredentialAssignmentsWithSecretRHS(t *testing.T) {
|
||||
providerValue := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
cases := []struct {
|
||||
name string
|
||||
file string
|
||||
@@ -980,32 +1101,47 @@ func TestScanFileDetectsTypedCredentialAssignmentsWithSecretRHS(t *testing.T) {
|
||||
{
|
||||
name: "typescript simple secret",
|
||||
file: "fixtures/source_secret.ts",
|
||||
text: `const clientSecret: string = "real-client-secret-value"`,
|
||||
text: `const clientSecret: string = "` + providerValue + `"`,
|
||||
},
|
||||
{
|
||||
name: "typescript numeric password",
|
||||
name: "typescript terminated secret",
|
||||
file: "fixtures/source_secret.ts",
|
||||
text: `const password: string = "12345678901234567890"`,
|
||||
text: `const clientSecret: string = "` + providerValue + `";`,
|
||||
},
|
||||
{
|
||||
name: "typescript secret with trailing comment",
|
||||
file: "fixtures/source_secret.ts",
|
||||
text: `const clientSecret: string = "` + providerValue + `"; // production`,
|
||||
},
|
||||
{
|
||||
name: "typescript asserted secret",
|
||||
file: "fixtures/source_secret.ts",
|
||||
text: `const clientSecret: string = "` + providerValue + `" as const;`,
|
||||
},
|
||||
{
|
||||
name: "typescript provider password",
|
||||
file: "fixtures/source_secret.ts",
|
||||
text: `const password: string = "` + providerValue + `"`,
|
||||
},
|
||||
{
|
||||
name: "typescript union secret",
|
||||
file: "fixtures/source_secret.ts",
|
||||
text: `const clientSecret: string | undefined = "real-client-secret-value"`,
|
||||
text: `const clientSecret: string | undefined = "` + providerValue + `"`,
|
||||
},
|
||||
{
|
||||
name: "python simple secret",
|
||||
file: "fixtures/source_secret.py",
|
||||
text: `self.client_secret: str = "real-client-secret-value"`,
|
||||
text: `self.client_secret: str = "` + providerValue + `"`,
|
||||
},
|
||||
{
|
||||
name: "python union secret",
|
||||
file: "fixtures/source_secret.py",
|
||||
text: `self.client_secret: str | None = "real-client-secret-value"`,
|
||||
text: `self.client_secret: str | None = "` + providerValue + `"`,
|
||||
},
|
||||
{
|
||||
name: "python optional secret",
|
||||
file: "fixtures/source_secret.py",
|
||||
text: `self.client_secret: Optional[str] = "real-client-secret-value"`,
|
||||
text: `self.client_secret: Optional[str] = "` + providerValue + `"`,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
@@ -1018,24 +1154,154 @@ func TestScanFileDetectsTypedCredentialAssignmentsWithSecretRHS(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsCredentialShapedSourceCodeLiterals(t *testing.T) {
|
||||
githubToken := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
got := ScanFile("fixtures/source_secret.go", []byte(strings.Join([]string{
|
||||
`const ClientSecret = "real-client-secret-value"`,
|
||||
`const GithubToken = "` + githubToken + `"`,
|
||||
`const Password = "12345678901234567890"`,
|
||||
`const ClientSecretNumber = "12345678901234567890"`,
|
||||
`const ClientSecretFormat = "abc%sdefreal"`,
|
||||
`fmt.Println("done"); const ClientSecret = "abc%sdefreal"`,
|
||||
}, "\n")+"\n"))
|
||||
func TestScanFileDetectsRepeatedTypedCredentialAssignments(t *testing.T) {
|
||||
providerValue := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
assertGenericCredentialFinding(t, "fixtures/source_secret.ts", `const clientSecret: string = "placeholder";`, false)
|
||||
assertGenericCredentialFinding(t, "fixtures/source_secret.ts", `const clientSecret: string = "`+providerValue+`";`, true)
|
||||
|
||||
got := ScanFile("fixtures/source_secret.ts", []byte(
|
||||
`const clientSecret: string = "placeholder"; const clientSecret: string = "`+providerValue+`";`+"\n",
|
||||
))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count != 6 {
|
||||
t.Fatalf("source code credential-shaped literal findings = %d, want 6: %#v", count, got)
|
||||
if count != 1 {
|
||||
t.Fatalf("repeated typed credential findings = %d, want 1: %#v", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsCredentialShapedSourceCodeLiterals(t *testing.T) {
|
||||
stripeLike := "sk_" + "live_1234567890abcdef"
|
||||
githubToken := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
cases := []struct {
|
||||
name string
|
||||
text string
|
||||
want bool
|
||||
}{
|
||||
{name: "stripe", text: `const ClientSecret = "` + stripeLike + `"`, want: true},
|
||||
{name: "github", text: `const GithubToken = "` + githubToken + `"`, want: true},
|
||||
{name: "password number", text: `const Password = "12345678901234567890"`, want: false},
|
||||
{name: "secret number", text: `const ClientSecretNumber = "12345678901234567890"`, want: false},
|
||||
{name: "format literal", text: `const ClientSecretFormat = "abc%sdefreal"`, want: false},
|
||||
{name: "inline format literal", text: `fmt.Println("done"); const ClientSecret = "abc%sdefreal"`, want: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assertGenericCredentialFinding(t, "fixtures/source_secret.go", tc.text, tc.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsGoShortDeclarationCredentials(t *testing.T) {
|
||||
providerSecret := "sk_" + "live_1234567890abcdef"
|
||||
providerToken := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
got := ScanFile("fixtures/source_secret.go", []byte(strings.Join([]string{
|
||||
`clientSecret := "` + providerSecret + `"`,
|
||||
`accessToken := "` + providerToken + `"`,
|
||||
}, "\n")+"\n"))
|
||||
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count != 2 {
|
||||
t.Fatalf("Go short declaration credential findings = %d, want 2: %#v", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenericCredentialDecisionMatrix(t *testing.T) {
|
||||
providerToken := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
highEntropyValue := "Q7k2mN9pR4vX8cL3" + "sT6yU1aD5fG0hJ2z"
|
||||
tokenHash := "6f1ed002ab559585" + "9014ebf0951522d9" +
|
||||
"a0e3c1f4206254d" + "28a13efbbc8d56a30"
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
text string
|
||||
comment bool
|
||||
want bool
|
||||
}{
|
||||
{name: "source synthetic token prefix", path: "pkg/sheets.go", text: `const localOfficeTokenPrefix = "local_office_"`, want: false},
|
||||
{name: "source token kind state", path: "pkg/client.py", text: `self._token_kind: TokenKind | None = None`, want: false},
|
||||
{name: "documentation token prefix", path: "docs/config.yaml", text: `token_prefix: local_office_`, want: false},
|
||||
{name: "documentation token kind", path: "docs/config.yaml", text: `token_kind: bearer`, want: false},
|
||||
{name: "documentation token hash", path: "docs/config.yaml", text: `access_token_hash: ` + tokenHash, want: false},
|
||||
{name: "comment fixture placeholder", text: `AppSecret: "fake-secret"`, comment: true, want: false},
|
||||
{name: "test fixture placeholder", path: "pkg/config_test.go", text: `AppSecret: "fake-secret"`, want: false},
|
||||
{name: "test real-labeled token", path: "pkg/config_test.go", text: `token: "real-tenant-access-token"`, want: false},
|
||||
{name: "test ambiguous concrete secret word", path: "pkg/config_test.go", text: `AppSecret: "supersecret"`, want: false},
|
||||
{name: "resource token placeholder", path: "docs/images.md", text: `"token": "img_abc123"`, want: false},
|
||||
{name: "partially masked token", path: "docs/auth.md", text: `token=ab********cd`, want: false},
|
||||
{name: "source readable secret words", path: "pkg/config.go", text: `const AppSecret = "customer-prod-secret"`, want: false},
|
||||
{name: "documentation readable secret words", path: "docs/config.yaml", text: `client_secret: customer-prod-secret`, want: false},
|
||||
{name: "comment middle fixture marker", text: `API_KEY=prod-fake-key`, comment: true, want: false},
|
||||
{name: "comment negated fixture marker", text: `AppSecret: "not-fake-secret"`, comment: true, want: false},
|
||||
{name: "source with credential words", path: "pkg/config.go", text: `secretWithPassword := "hunter2"`, want: false},
|
||||
{name: "production filename containing sample", path: "pkg/sampler.go", text: `clientSecret := "customer-prod-secret"`, want: false},
|
||||
{name: "provider token under weak key", path: "docs/config.yaml", text: `token: ` + providerToken, want: true},
|
||||
{name: "provider token under hash key", path: "docs/config.yaml", text: `access_token_hash: ` + providerToken, want: true},
|
||||
{name: "high entropy strong secret", path: "docs/config.yaml", text: `client_secret: ` + highEntropyValue, want: true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var got []Finding
|
||||
if tt.comment {
|
||||
got = ScanComment("issue_comment", tt.text)
|
||||
} else {
|
||||
got = ScanFile(tt.path, []byte(tt.text+"\n"))
|
||||
}
|
||||
if actual := findingRules(got)["public_content_generic_credential"]; actual != tt.want {
|
||||
t.Fatalf("generic credential finding = %v, want %v: %#v", actual, tt.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileClassifiesLowEvidenceTestFixtureCredentials(t *testing.T) {
|
||||
providerToken := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
highEntropyValue := "Q7k2mN9pR4vX8cL3" + "sT6yU1aD5fG0hJ2z"
|
||||
tests := []struct {
|
||||
name string
|
||||
value string
|
||||
want bool
|
||||
}{
|
||||
{name: "human readable access token", value: "user-access-token", want: false},
|
||||
{name: "delimited secret value", value: "secret-value", want: false},
|
||||
{name: "underscored secret fixture", value: "plain_secret", want: false},
|
||||
{name: "short delimited fixture", value: "t-abc", want: false},
|
||||
{name: "embedded test marker", value: "perm-grant-test-secret-skip", want: false},
|
||||
{name: "real labeled fixture", value: "real-token", want: false},
|
||||
{name: "ambiguous concrete word", value: "supersecret", want: false},
|
||||
{name: "provider token", value: providerToken, want: true},
|
||||
{name: "high entropy secret", value: highEntropyValue, want: true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := ScanFile("pkg/config_test.go", []byte(`AppSecret: "`+tt.value+`"`+"\n"))
|
||||
if actual := findingRules(got)["public_content_generic_credential"]; actual != tt.want {
|
||||
t.Fatalf("generic credential finding = %v, want %v: %#v", actual, tt.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileAllowsLowEvidenceTestFixtureAssignmentSyntaxes(t *testing.T) {
|
||||
got := ScanFile("pkg/config_test.go", []byte(strings.Join([]string{
|
||||
`secret := "secret-value"`,
|
||||
`samplePassword := "sample-password"`,
|
||||
`bodyWithToken := "plain text body\\nDownload: https://example.com/file?token=tok_aaa\\n"`,
|
||||
}, "\n")+"\n"))
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
t.Fatalf("low-evidence test fixture assignment should not be reported: %#v", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1116,9 +1382,10 @@ func TestScanFileAllowsClientTokenIdempotencyExamples(t *testing.T) {
|
||||
|
||||
func TestScanFileDetectsCredentialShapedClientTokenValues(t *testing.T) {
|
||||
stripeLike := "sk_" + "live_1234567890abcdef"
|
||||
githubToken := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
got := ScanFile("fixtures/idempotency.md", []byte(strings.Join([]string{
|
||||
`{"client_token":"` + stripeLike + `"}`,
|
||||
`{"client_token":"real-client-secret-value"}`,
|
||||
`{"client_token":"` + githubToken + `"}`,
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
@@ -1152,9 +1419,10 @@ func TestScanFileAllowsTokenLikePlaceholderExamples(t *testing.T) {
|
||||
|
||||
func TestScanFileDetectsCredentialShapedTokenLikePlaceholderValues(t *testing.T) {
|
||||
stripeLike := "sk_" + "live_1234567890abcdef"
|
||||
githubToken := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
got := ScanFile("fixtures/placeholders.md", []byte(strings.Join([]string{
|
||||
`{ "resource_token": "` + stripeLike + `" }`,
|
||||
`{ "block_token": "real-client-secret-value" }`,
|
||||
`{ "block_token": "` + githubToken + `" }`,
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
@@ -1368,39 +1636,43 @@ func TestScanFileAllowsConventionalCredentialPlaceholders(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsCredentialShapedPlaceholderLookalikes(t *testing.T) {
|
||||
func TestScanFileAllowsInvalidProviderPlaceholderLookalikes(t *testing.T) {
|
||||
stripeLike := "sk_" + "live_1234567890abcdef"
|
||||
got := ScanFile("docs/config.md", []byte(strings.Join([]string{
|
||||
"client_secret: " + stripeLike + "_HERE",
|
||||
"api_key: YOUR_" + stripeLike,
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
t.Fatalf("invalid provider placeholder lookalike should not be blocked: %#v", got)
|
||||
}
|
||||
}
|
||||
if count != 2 {
|
||||
t.Fatalf("credential-shaped placeholder lookalike findings = %d, want 2: %#v", count, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanFileDetectsPercentWrappedCredentialValues(t *testing.T) {
|
||||
stripeLike := "sk_" + "live_1234567890abcdef"
|
||||
patLike := "gh" + "p_1234567890abcdef1234567890abcdef1234"
|
||||
got := ScanFile("docs/config.md", []byte(strings.Join([]string{
|
||||
"CLIENT_SECRET=%" + stripeLike + "%",
|
||||
"GITHUB_TOKEN=%" + patLike + "%",
|
||||
"TOKEN=%real-secret-token-value%",
|
||||
}, "\n")+"\n"))
|
||||
var count int
|
||||
for _, item := range got {
|
||||
if item.Rule == "public_content_generic_credential" {
|
||||
count++
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
text string
|
||||
want bool
|
||||
}{
|
||||
{name: "stripe", text: "CLIENT_SECRET=%" + stripeLike + "%", want: true},
|
||||
{name: "github", text: "GITHUB_TOKEN=%" + patLike + "%", want: true},
|
||||
{name: "readable", text: "TOKEN=%real-secret-token-value%", want: false},
|
||||
}
|
||||
if count != 3 {
|
||||
t.Fatalf("percent-wrapped credential findings = %d, want 3: %#v", count, got)
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assertGenericCredentialFinding(t, "docs/config.md", tc.text, tc.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func assertGenericCredentialFinding(t *testing.T, file, text string, want bool) {
|
||||
t.Helper()
|
||||
got := ScanFile(file, []byte(text+"\n"))
|
||||
if actual := findingRules(got)["public_content_generic_credential"]; actual != want {
|
||||
t.Fatalf("generic credential finding = %v, want %v: %#v", actual, want, got)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -337,7 +337,7 @@ func fakeValueFromPlaceholderName(name string) (string, bool) {
|
||||
case name == "open_id" || hasPlaceholderToken(tokens, "user", "owner", "participant", "approver", "speaker"):
|
||||
return "ou_test123", true
|
||||
case hasPlaceholderToken(tokens, "department", "dept"):
|
||||
return "od_test123", true
|
||||
return "od-test123", true
|
||||
case hasPlaceholderToken(tokens, "message"):
|
||||
return "om_test123", true
|
||||
case name == "file_key":
|
||||
@@ -878,16 +878,23 @@ func extractDryRunJSON(raw []byte) (facts.DryRunRequest, int, error) {
|
||||
var firstErr error
|
||||
for start >= 0 {
|
||||
var preview struct {
|
||||
API []facts.DryRunRequest `json:"api"`
|
||||
API []facts.DryRunRequest `json:"api"`
|
||||
Data struct {
|
||||
API []facts.DryRunRequest `json:"api"`
|
||||
} `json:"data"`
|
||||
}
|
||||
dec := json.NewDecoder(bytes.NewReader(raw[start:]))
|
||||
if err := dec.Decode(&preview); err == nil {
|
||||
if len(preview.API) == 0 {
|
||||
api := preview.API
|
||||
if len(api) == 0 {
|
||||
api = preview.Data.API
|
||||
}
|
||||
if len(api) == 0 {
|
||||
if firstErr == nil {
|
||||
firstErr = errNoDryRunAPI
|
||||
}
|
||||
} else {
|
||||
return preview.API[0], len(preview.API), nil
|
||||
return api[0], len(api), nil
|
||||
}
|
||||
} else if firstErr == nil {
|
||||
firstErr = err
|
||||
|
||||
@@ -33,6 +33,17 @@ func TestExtractDryRunJSONSkipsBanner(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractDryRunJSONReadsSuccessEnvelope(t *testing.T) {
|
||||
raw := `{"ok":true,"dry_run":true,"data":{"api":[{"method":"GET","url":"/open-apis/test"}]}}`
|
||||
got, apiCallCount, err := extractDryRunJSON([]byte(raw))
|
||||
if err != nil {
|
||||
t.Fatalf("extractDryRunJSON() error = %v", err)
|
||||
}
|
||||
if got.Method != "GET" || got.URL != "/open-apis/test" || apiCallCount != 1 {
|
||||
t.Fatalf("got request=%#v apiCallCount=%d, want enveloped GET and count 1", got, apiCallCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractDryRunJSONSkipsBannerWithBraces(t *testing.T) {
|
||||
raw := "banner {not json}\n{\"api\":[{\"method\":\"GET\",\"url\":\"/open-apis/test\"}]}\n"
|
||||
got, apiCallCount, err := extractDryRunJSON([]byte(raw))
|
||||
@@ -305,6 +316,13 @@ func TestRunDryRunsMaterializesInlinePlaceholderFlagValues(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFakeValueFromPlaceholderNameUsesOpenDepartmentPrefix(t *testing.T) {
|
||||
got, ok := fakeValueFromPlaceholderName("open_department_id")
|
||||
if !ok || got != "od-test123" {
|
||||
t.Fatalf("open_department_id placeholder = %q, %v; want od-test123, true", got, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunDryRunsMaterializesNumericPlaceholderFlagValues(t *testing.T) {
|
||||
cliBin, argsPath := fakeDryRunCLI(t, `{"api":[{"method":"GET","url":"/open-apis/vc/v1/bots/events","params":{"meeting_id":"400000000001","page_size":50}}]}`)
|
||||
m := manifest.Manifest{Commands: []manifest.Command{{
|
||||
|
||||
@@ -203,7 +203,8 @@ func TestRunCollectsPublicContentFindingsIntoDiagnosticsAndFacts(t *testing.T) {
|
||||
if err := vfs.MkdirAll(filepath.Join(repo, "docs"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
publicDoc := "api_" + "key = \"example-public-key\"\n" +
|
||||
providerValue := "ghp_" + "1234567890abcdef1234567890abcdef1234"
|
||||
publicDoc := "api_" + "key = \"" + providerValue + "\"\n" +
|
||||
"Public docs describe a pri" + "vate request header and trust classification detail.\n"
|
||||
if err := vfs.WriteFile(filepath.Join(repo, "docs", "public.md"), []byte(publicDoc), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@larksuite/cli",
|
||||
"version": "1.0.11",
|
||||
"version": "1.0.73-beta.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@larksuite/cli",
|
||||
"version": "1.0.11",
|
||||
"version": "1.0.73-beta.0",
|
||||
"cpu": [
|
||||
"x64",
|
||||
"arm64"
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
{
|
||||
"name": "@larksuite/cli",
|
||||
"version": "1.0.69",
|
||||
"version": "1.0.73-beta.0",
|
||||
"description": "The official CLI for Lark/Feishu open platform",
|
||||
"bin": {
|
||||
"lark-cli": "scripts/run.js"
|
||||
},
|
||||
"scripts": {
|
||||
"postinstall": "node scripts/install.js"
|
||||
"postinstall": "node scripts/install.js",
|
||||
"release:check": "node scripts/release-preflight.js"
|
||||
},
|
||||
"os": [
|
||||
"darwin",
|
||||
|
||||
@@ -18,6 +18,11 @@ workflow_permissions="$(awk '
|
||||
in_permissions && /^[^[:space:]]/ { exit }
|
||||
in_permissions { print }
|
||||
' "$workflow")"
|
||||
workflow_concurrency="$(awk '
|
||||
/^concurrency:/ { in_concurrency = 1; print; next }
|
||||
in_concurrency && /^[^[:space:]]/ { exit }
|
||||
in_concurrency { print }
|
||||
' "$workflow")"
|
||||
fast_gate_section="$(job_section fast-gate)"
|
||||
unit_test_section="$(job_section unit-test)"
|
||||
lint_section="$(awk '
|
||||
@@ -46,6 +51,27 @@ results_section="$(awk '
|
||||
in_job { print }
|
||||
' "$workflow")"
|
||||
fork_safe_guard="github.event_name != 'pull_request' || !github.event.pull_request.head.repo.fork"
|
||||
live_job_condition="always() && ($fork_safe_guard) && needs.unit-test.result == 'success' && needs.lint.result == 'success' && needs.script-test.result == 'success' && needs.deterministic-gate.result == 'success' && needs.e2e-dry-run.result == 'success' && (needs.e2e-dry-run.outputs.mode == 'full' || needs.e2e-dry-run.outputs.mode == 'subset') && needs.e2e-dry-run.outputs.live_packages != ''"
|
||||
|
||||
if ! grep -Fq "run-name: \${{ github.event_name == 'pull_request' && format('CI / {0}', github.event.pull_request.number) || '' }}" "$workflow"; then
|
||||
echo "CI should expose a stable PR generation while preserving default push and manual run titles" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "RUN_GENERATION: \${{ github.event_name == 'pull_request' && format('CI / {0}', github.event.pull_request.number) || '' }}" <<<"$section"; then
|
||||
echo "the supersession generation should match the PR-only run name" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq 'group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}' <<<"$workflow_concurrency"; then
|
||||
echo "CI should deduplicate runs for the same pull request without grouping push or manual runs" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "cancel-in-progress: \${{ github.event_name == 'pull_request' }}" <<<"$workflow_concurrency"; then
|
||||
echo "CI should cancel superseded pull request runs but preserve push and manual runs" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
for denied_permission in "checks: write" "pull-requests: write" "issues: write"; do
|
||||
if grep -Eq "^[[:space:]]*${denied_permission}$" <<<"$workflow_permissions"; then
|
||||
@@ -210,8 +236,84 @@ if ! grep -Fq "deterministic-gate" <<<"$results_section"; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "if: \${{ $fork_safe_guard }}" <<<"$section"; then
|
||||
echo "e2e-live should run on push and same-repository pull_request, but skip fork pull_request"
|
||||
if ! grep -Fq "if: \${{ $live_job_condition }}" <<<"$section"; then
|
||||
echo "e2e-live should preserve active cleanup while requiring a successful non-skip dry run and excluding fork pull requests"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "needs: [unit-test, lint, script-test, deterministic-gate, e2e-dry-run]" <<<"$section"; then
|
||||
echo "e2e-live should wait outside the exclusive queue until e2e-dry-run finishes"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "timeout-minutes: 20" <<<"$dry_run_section"; then
|
||||
echo "e2e-dry-run should bound the planning gate before live E2E" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "timeout-minutes: 30" <<<"$section"; then
|
||||
echo "e2e-live should release the repository-wide slot after 30 minutes" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "group: lark-cli-e2e-live" <<<"$section"; then
|
||||
echo "e2e-live should use one repository-wide execution slot" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "cancel-in-progress: false" <<<"$section"; then
|
||||
echo "e2e-live should queue waiting runs instead of cancelling an active live test" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "queue: max" <<<"$section"; then
|
||||
echo "e2e-live should preserve queued runs instead of replacing an existing pending run" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "actions: read" <<<"$section"; then
|
||||
echo "e2e-live should use read-only Actions access for the supersession check" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
live_test_step="$(awk '
|
||||
/^ - name: Run CLI E2E tests/ { in_step = 1 }
|
||||
in_step { print }
|
||||
in_step && /^ - name: Publish CLI E2E test report/ { exit }
|
||||
' <<<"$section")"
|
||||
|
||||
if ! grep -Fq "if: \${{ always() && steps.build_cli.outcome == 'success' && steps.live_e2e_tat.outcome == 'success' }}" <<<"$live_test_step"; then
|
||||
echo "the active live test step should survive ordinary workflow supersession only after setup succeeds" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
for required in \
|
||||
'gh api "repos/$REPOSITORY/actions/runs/$RUN_ID"' \
|
||||
'gh api --paginate -X GET "repos/$REPOSITORY/actions/workflows/$workflow_id/runs"' \
|
||||
'-f event=pull_request -f branch="$GITHUB_HEAD_REF" -f per_page=100' \
|
||||
'.head_repository.full_name == $repository and .display_title == $generation and .run_number > $run_number' \
|
||||
'::error::Superseded before live E2E started' \
|
||||
'exit 1'; do
|
||||
if ! grep -Fq -- "$required" <<<"$live_test_step"; then
|
||||
echo "the live startup check should fail closed before a superseded run starts live E2E: missing $required" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
if ! awk '
|
||||
/if \[ -n "\$newer_runs" \]; then/ { superseded_state = 1; next }
|
||||
superseded_state == 1 && /::error::Superseded before live E2E started/ { superseded_state = 2; next }
|
||||
superseded_state == 2 && /^[[:space:]]+exit 1[[:space:]]*$/ { superseded_state = 3; next }
|
||||
superseded_state > 0 && /^[[:space:]]+fi[[:space:]]*$/ {
|
||||
if (superseded_state != 3) exit 2
|
||||
superseded_closed = 1
|
||||
superseded_state = 0
|
||||
next
|
||||
}
|
||||
/go run gotest.tools\/gotestsum@/ { test_started = 1; if (!superseded_closed) exit 3 }
|
||||
END { exit superseded_closed && test_started ? 0 : 1 }
|
||||
' <<<"$live_test_step"; then
|
||||
echo "a superseded live run must stop before gotestsum starts" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -222,6 +324,39 @@ if ! grep -Fq "name: Resolve CLI E2E domains" <<<"$dry_run_section" ||
|
||||
exit 1
|
||||
fi
|
||||
|
||||
for output in \
|
||||
'mode: ${{ steps.e2e_domains.outputs.mode }}' \
|
||||
'reason: ${{ steps.e2e_domains.outputs.reason }}' \
|
||||
'live_packages: ${{ steps.e2e_domains.outputs.live_packages }}'; do
|
||||
if ! grep -Fq "$output" <<<"$dry_run_section"; then
|
||||
echo "e2e-dry-run should publish $output for the live job" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
for validation_contract in \
|
||||
'case "$E2E_MODE" in' \
|
||||
'skip)' \
|
||||
'[ -z "$E2E_LIVE_PACKAGES" ]' \
|
||||
'full|subset)' \
|
||||
'[ -n "$E2E_LIVE_PACKAGES" ]' \
|
||||
'Invalid CLI E2E mode' \
|
||||
'exit 1'; do
|
||||
if ! grep -Fq "$validation_contract" <<<"$dry_run_section"; then
|
||||
echo "e2e-dry-run should fail invalid domain output before live can be skipped: missing $validation_contract" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
if ! awk '
|
||||
/- name: Validate CLI E2E domain outputs/ { validated = 1 }
|
||||
/- name: Build lark-cli/ { exit validated ? 0 : 1 }
|
||||
END { if (!validated) exit 1 }
|
||||
' <<<"$dry_run_section"; then
|
||||
echo "e2e-dry-run should validate domain outputs before building" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "steps.e2e_domains.outputs.dry_packages" <<<"$dry_run_section"; then
|
||||
echo "e2e-dry-run should use resolved dry_packages instead of always running the full suite"
|
||||
exit 1
|
||||
@@ -244,21 +379,21 @@ if ! grep -Fq "No dry-run CLI E2E needed" <<<"$dry_run_section"; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "name: Resolve CLI E2E domains" <<<"$section" ||
|
||||
! grep -Fq "id: e2e_domains" <<<"$section" ||
|
||||
! grep -Fq "run: node scripts/e2e_domains.js" <<<"$section"; then
|
||||
echo "e2e-live should resolve changed-file CLI E2E domains before credentials and tests"
|
||||
if grep -Fq "name: Resolve CLI E2E domains" <<<"$section" ||
|
||||
grep -Fq "run: node scripts/e2e_domains.js" <<<"$section"; then
|
||||
echo "e2e-live should reuse e2e-dry-run outputs instead of resolving domains again"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "steps.e2e_domains.outputs.live_packages" <<<"$section"; then
|
||||
echo "e2e-live should use resolved live_packages instead of always running the full suite"
|
||||
if ! grep -Fq "E2E_LIVE_PACKAGES: \${{ needs.e2e-dry-run.outputs.live_packages }}" <<<"$section"; then
|
||||
echo "e2e-live should reuse live_packages resolved by e2e-dry-run"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "E2E_REASON: \${{ steps.e2e_domains.outputs.reason }}" <<<"$section" ||
|
||||
if ! grep -Fq "E2E_MODE: \${{ needs.e2e-dry-run.outputs.mode }}" <<<"$section" ||
|
||||
! grep -Fq "E2E_REASON: \${{ needs.e2e-dry-run.outputs.reason }}" <<<"$section" ||
|
||||
! grep -Fq 'echo "Live CLI E2E domains: $E2E_MODE ($E2E_REASON)"' <<<"$section"; then
|
||||
echo "e2e-live should pass dynamic domain output through env before shell use"
|
||||
echo "e2e-live should consume the exact mode and reason produced by e2e-dry-run"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -272,16 +407,23 @@ if ! awk '
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! awk '
|
||||
/^ - name: Build lark-cli/ { in_step = 1 }
|
||||
in_step && /if: \$\{\{ steps\.e2e_domains\.outputs\.mode != '\''skip'\'' \}\}/ { found = 1 }
|
||||
in_step && /^ - name:/ && !/Build lark-cli/ { in_step = 0 }
|
||||
END { exit found ? 0 : 1 }
|
||||
' <<<"$section"; then
|
||||
echo "e2e-live should skip building lark-cli when domain mode is skip"
|
||||
if grep -Fq "steps.e2e_domains.outputs" <<<"$section"; then
|
||||
echo "e2e-live should not retain step-local domain outputs after adopting the dry-run job gate"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
for step_name in "Build lark-cli" "Prepare shared live E2E tenant token"; do
|
||||
live_setup_step="$(awk -v name="$step_name" '
|
||||
$0 == " - name: " name { in_step = 1 }
|
||||
in_step { print }
|
||||
in_step && /^ - name:/ && $0 != " - name: " name { exit }
|
||||
' <<<"$section")"
|
||||
if grep -Eq '^ if:' <<<"$live_setup_step"; then
|
||||
echo "e2e-live $step_name should run unconditionally after the non-skip job gate" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
if ! grep -Fq "permissions:" <<<"$section" ||
|
||||
! grep -Fq "contents: read" <<<"$section" ||
|
||||
! grep -Fq "checks: write" <<<"$section"; then
|
||||
@@ -299,18 +441,88 @@ if grep -Fq "live_e2e_credentials" <<<"$section" || grep -Fq "configured=false"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "::error::Missing required secrets: TEST_BOT1_APP_ID / TEST_BOT1_APP_SECRET" <<<"$section"; then
|
||||
echo "e2e-live should make missing bot credentials a visible configuration failure on eligible runs"
|
||||
if ! grep -Fq "node scripts/fetch_e2e_tat.js" <<<"$section"; then
|
||||
echo "e2e-live should fetch the tenant token via the dedicated script"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if grep -Fq "config init" <<<"$section"; then
|
||||
echo "e2e-live should use env credentials instead of config init"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "TEST_BOT1_APP_ID: \${{ secrets.TEST_BOT1_APP_ID }}" <<<"$section"; then
|
||||
echo "e2e-live should keep the bot app id under a test-only job env name"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if awk '
|
||||
/^ e2e-live:/ { in_job = 1; next }
|
||||
in_job && /^ [A-Za-z0-9_-]+:/ { in_job = 0 }
|
||||
in_job && /^ env:/ { in_env = 1; next }
|
||||
in_env && /^ steps:/ { in_env = 0 }
|
||||
in_env && /LARKSUITE_CLI_APP_ID:/ { found_standard_app_id = 1 }
|
||||
END { exit found_standard_app_id ? 0 : 1 }
|
||||
' "$workflow"; then
|
||||
echo "e2e-live should not activate the env credential provider at job scope"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "LARKSUITE_CLI_BRAND: feishu" <<<"$section"; then
|
||||
echo "e2e-live should pin the env credential brand to feishu"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if awk '
|
||||
/^ e2e-live:/ { in_job = 1; next }
|
||||
in_job && /^ [A-Za-z0-9_-]+:/ { in_job = 0 }
|
||||
in_job && /^ env:/ { in_env = 1; next }
|
||||
in_env && /^ steps:/ { in_env = 0 }
|
||||
in_env && /(SECRET|ACCESS_TOKEN):/ { found_sensitive = 1 }
|
||||
END { exit found_sensitive ? 0 : 1 }
|
||||
' "$workflow"; then
|
||||
echo "e2e-live should not expose live E2E credentials through job-level env"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! awk '
|
||||
/^ - name: Configure bot credentials/ { in_step = 1 }
|
||||
in_step && /if: \$\{\{ steps\.e2e_domains\.outputs\.mode != '\''skip'\'' \}\}/ { found = 1 }
|
||||
in_step && /^ - name:/ && !/Configure bot credentials/ { in_step = 0 }
|
||||
END { exit found ? 0 : 1 }
|
||||
/^ - name: Prepare shared live E2E tenant token/ { in_step = 1 }
|
||||
in_step && /id: live_e2e_tat/ { has_id = 1 }
|
||||
in_step && /^ if:/ { has_if = 1 }
|
||||
in_step && /LARKSUITE_CLI_APP_ID: \$\{\{ secrets\.TEST_BOT1_APP_ID \}\}/ { has_app_id = 1 }
|
||||
in_step && /secrets\.TEST_BOT1_APP_SECRET/ { has_bot_credential = 1 }
|
||||
in_step && /node scripts\/fetch_e2e_tat\.js/ { has_script = 1 }
|
||||
in_step && /GITHUB_ENV/ { uses_github_env = 1 }
|
||||
in_step && /^ - name:/ && !/Prepare shared live E2E tenant token/ { in_step = 0 }
|
||||
END { exit has_id && !has_if && has_app_id && has_bot_credential && has_script && !uses_github_env ? 0 : 1 }
|
||||
' <<<"$section"; then
|
||||
echo "e2e-live should only configure bot credentials when domain mode is not skip"
|
||||
echo "e2e-live should pass only a private tenant token file path through step output"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! awk '
|
||||
/^ - name: Run CLI E2E tests/ { in_step = 1 }
|
||||
in_step && /E2E_TENANT_AUTH_FILE: \$\{\{ steps\.live_e2e_tat\.outputs\.path \}\}/ { has_file = 1 }
|
||||
in_step && /secrets\.TEST_USER_ACCESS_TOKEN/ { has_user_credential = 1 }
|
||||
in_step && /Missing shared live E2E tenant token file/ { checks_file = 1 }
|
||||
in_step && /^ *export / && /TEST_TENANT_ACCESS_TOKEN/ && /E2E_TENANT_AUTH_FILE/ { exports_test_tat = 1 }
|
||||
in_step && /^ *export / && /LARKSUITE_CLI_TENANT_ACCESS_TOKEN/ { exports_standard_tat = 1 }
|
||||
in_step && /LARKSUITE_CLI_APP_ID="\$TEST_BOT1_APP_ID"/ { scopes_preflight_app_id = 1 }
|
||||
in_step && /LARKSUITE_CLI_TENANT_ACCESS_TOKEN="\$TEST_TENANT_ACCESS_TOKEN"/ { scopes_preflight_tat = 1 }
|
||||
in_step && /lark-cli whoami --as bot/ { has_preflight = 1 }
|
||||
in_step && /Tenant credential preflight failed/ { checks_preflight = 1 }
|
||||
in_step && /TEST_USER_ACCESS_TOKEN/ && /secrets\.TEST_USER_ACCESS_TOKEN/ { has_user_env = 1 }
|
||||
in_step && /LARKSUITE_CLI_USER_ACCESS_TOKEN/ && /secrets\.TEST_USER_ACCESS_TOKEN/ { has_global_user_env = 1 }
|
||||
in_step && /trap / { has_trap = 1 }
|
||||
in_step && /^ - name:/ && !/Run CLI E2E tests/ { in_step = 0 }
|
||||
END { exit has_file && has_user_credential && checks_file && exports_test_tat && !exports_standard_tat && scopes_preflight_app_id && scopes_preflight_tat && has_preflight && checks_preflight && has_user_env && !has_global_user_env && !has_trap ? 0 : 1 }
|
||||
' <<<"$section"; then
|
||||
echo "e2e-live should expose live E2E credentials only inside the test shell step"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if grep -Fq 'if [ "$E2E_MODE" = "skip" ]' <<<"$section"; then
|
||||
echo "e2e-live should not retain an unreachable step-level skip branch"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -319,8 +531,8 @@ if grep -Fq "steps.live_e2e_credentials.outputs.configured" <<<"$section"; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! grep -Fq "if: \${{ !cancelled() && steps.e2e_domains.outputs.mode != 'skip' }}" <<<"$section"; then
|
||||
echo "e2e-live report step should run after attempted live tests unless the workflow is cancelled or domain mode is skip"
|
||||
if ! grep -Fq "if: \${{ !cancelled() }}" <<<"$section"; then
|
||||
echo "e2e-live report step should run after attempted live tests unless the workflow is cancelled"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -342,7 +554,7 @@ if grep -Fq '${{ secrets.CODECOV_TOKEN }}' <<<"$coverage_step" &&
|
||||
fi
|
||||
|
||||
if grep -Fq '${{ secrets.' <<<"$section" &&
|
||||
! grep -Fq "if: \${{ $fork_safe_guard }}" <<<"$section"; then
|
||||
! grep -Fq "$fork_safe_guard" <<<"$section"; then
|
||||
echo "live E2E secrets should be available on push and same-repository pull_request, but not fork pull_request" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
164
scripts/fetch_e2e_tat.js
Normal file
164
scripts/fetch_e2e_tat.js
Normal file
@@ -0,0 +1,164 @@
|
||||
#!/usr/bin/env node
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// Fetches a live E2E tenant access token (TAT) for the shared bot identity.
|
||||
//
|
||||
// Invoked from the e2e-live CI job. Exchanges the bot app id/secret for a
|
||||
// tenant access token, writes the token to a private file under $RUNNER_TEMP,
|
||||
// and emits the file path as a step output so the test step can read it once
|
||||
// and then delete it.
|
||||
//
|
||||
// The secret arrives via environment variables; the OAuth parameter names are
|
||||
// literal because this is a source code file (.js), so the quality gate's
|
||||
// benign-code-credential exemption applies to the process.env references.
|
||||
|
||||
const fs = require("node:fs");
|
||||
const http = require("node:http");
|
||||
const https = require("node:https");
|
||||
const path = require("node:path");
|
||||
const { URL } = require("node:url");
|
||||
|
||||
const ENDPOINT = process.env.E2E_TAT_ENDPOINT || "https://accounts.feishu.cn/oauth/v3/token";
|
||||
const MAX_ATTEMPTS = 4;
|
||||
const RETRY_BASE_MS = parseInt(process.env.E2E_TAT_RETRY_BASE_MS || "1000", 10);
|
||||
|
||||
function requireEnv(name) {
|
||||
const value = process.env[name];
|
||||
if (!value) {
|
||||
console.error(`::error::Missing required environment variable: ${name}`);
|
||||
process.exit(1);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function postForm(url, body) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const parsed = new URL(url);
|
||||
const transport = parsed.protocol === "http:" ? http : https;
|
||||
const req = transport.request(
|
||||
parsed,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Content-Length": Buffer.byteLength(body),
|
||||
},
|
||||
timeout: 20000,
|
||||
},
|
||||
(resp) => {
|
||||
const chunks = [];
|
||||
let settled = false;
|
||||
const rejectOnce = (error) => {
|
||||
if (!settled) {
|
||||
settled = true;
|
||||
reject(error);
|
||||
}
|
||||
};
|
||||
resp.on("data", (chunk) => chunks.push(chunk));
|
||||
resp.on("aborted", () => rejectOnce(new Error("response aborted before completion")));
|
||||
resp.on("error", rejectOnce);
|
||||
resp.on("close", () => {
|
||||
if (!resp.complete) {
|
||||
rejectOnce(new Error("response closed before completion"));
|
||||
}
|
||||
});
|
||||
resp.on("end", () => {
|
||||
if (!resp.complete) {
|
||||
rejectOnce(new Error("response ended before completion"));
|
||||
return;
|
||||
}
|
||||
settled = true;
|
||||
resolve({
|
||||
status: resp.statusCode,
|
||||
body: Buffer.concat(chunks).toString("utf8"),
|
||||
headers: resp.headers,
|
||||
});
|
||||
});
|
||||
},
|
||||
);
|
||||
req.on("timeout", () => {
|
||||
req.destroy();
|
||||
reject(new Error("request timed out"));
|
||||
});
|
||||
req.on("error", reject);
|
||||
req.write(body);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
function encodeForm(params) {
|
||||
return Object.entries(params)
|
||||
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
||||
.join("&");
|
||||
}
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
async function fetchTenantToken() {
|
||||
const appId = requireEnv("LARKSUITE_CLI_APP_ID");
|
||||
const appSecret = requireEnv("TEST_BOT1_APP_SECRET");
|
||||
|
||||
const body = encodeForm({
|
||||
grant_type: "client_credentials",
|
||||
client_id: appId,
|
||||
client_secret: appSecret,
|
||||
});
|
||||
|
||||
let lastError = "";
|
||||
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
|
||||
try {
|
||||
const { status, body: respBody, headers } = await postForm(ENDPOINT, body);
|
||||
let payload;
|
||||
try {
|
||||
payload = JSON.parse(respBody);
|
||||
} catch {
|
||||
const logID = headers["x-tt-logid"] || headers["x-request-id"] || "unavailable";
|
||||
lastError = `HTTP ${status}, log_id=${logID}, non-JSON response`;
|
||||
}
|
||||
if (payload) {
|
||||
const token = payload.access_token;
|
||||
if (status === 200 && payload.code === 0 && token) {
|
||||
return token;
|
||||
}
|
||||
lastError = `HTTP ${status}, code=${payload.code}, error=${payload.error}, msg=${payload.msg || payload.error_description}`;
|
||||
}
|
||||
} catch (err) {
|
||||
lastError = err.message;
|
||||
}
|
||||
|
||||
if (attempt < MAX_ATTEMPTS) {
|
||||
await sleep(2 ** (attempt - 1) * RETRY_BASE_MS);
|
||||
}
|
||||
}
|
||||
|
||||
console.error(`::error::Failed to fetch tenant access token: ${lastError}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const token = await fetchTenantToken();
|
||||
console.log(`::add-mask::${token}`);
|
||||
|
||||
const tatPath = path.join(process.env.RUNNER_TEMP, "e2e-live-tat");
|
||||
fs.writeFileSync(tatPath, token, { encoding: "utf8", mode: 0o600 });
|
||||
|
||||
if (process.env.GITHUB_OUTPUT) {
|
||||
fs.appendFileSync(process.env.GITHUB_OUTPUT, `path=${tatPath}\n`);
|
||||
}
|
||||
|
||||
console.log("Prepared shared live E2E tenant token");
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
main();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
encodeForm,
|
||||
fetchTenantToken,
|
||||
postForm,
|
||||
requireEnv,
|
||||
};
|
||||
203
scripts/fetch_e2e_tat.test.js
Normal file
203
scripts/fetch_e2e_tat.test.js
Normal file
@@ -0,0 +1,203 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const http = require("node:http");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
const { spawn } = require("node:child_process");
|
||||
const test = require("node:test");
|
||||
|
||||
const scriptPath = path.join(__dirname, "fetch_e2e_tat.js");
|
||||
|
||||
function startServer(handler) {
|
||||
const server = http.createServer((req, res) => {
|
||||
let body = "";
|
||||
req.on("data", (chunk) => {
|
||||
body += chunk;
|
||||
});
|
||||
req.on("end", () => {
|
||||
handler(req, res, body);
|
||||
});
|
||||
});
|
||||
return new Promise((resolve) => {
|
||||
server.listen(0, "127.0.0.1", () => {
|
||||
const port = server.address().port;
|
||||
resolve({ server, port });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function abortResponse(res) {
|
||||
res.writeHead(200, {
|
||||
"Content-Type": "application/json",
|
||||
"Content-Length": "100",
|
||||
});
|
||||
res.write('{"code":0');
|
||||
setImmediate(() => res.destroy());
|
||||
}
|
||||
|
||||
function runScript(envOverrides) {
|
||||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "fetch-e2e-tat-"));
|
||||
const githubOutput = path.join(tmpDir, "github-output");
|
||||
const env = {
|
||||
...process.env,
|
||||
LARKSUITE_CLI_APP_ID: "test_app_id",
|
||||
TEST_BOT1_APP_SECRET: "test-secret",
|
||||
RUNNER_TEMP: tmpDir,
|
||||
GITHUB_OUTPUT: githubOutput,
|
||||
E2E_TAT_RETRY_BASE_MS: "10",
|
||||
...envOverrides,
|
||||
};
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const child = spawn(process.execPath, [scriptPath], {
|
||||
cwd: path.join(__dirname, ".."),
|
||||
env,
|
||||
});
|
||||
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
child.stdout.on("data", (data) => {
|
||||
stdout += data;
|
||||
});
|
||||
child.stderr.on("data", (data) => {
|
||||
stderr += data;
|
||||
});
|
||||
child.on("close", (code) => {
|
||||
const output = fs.existsSync(githubOutput)
|
||||
? fs.readFileSync(githubOutput, "utf8")
|
||||
: "";
|
||||
resolve({ tmpDir, stdout, stderr, output, exitCode: code });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
test("encodeForm encodes form parameters", () => {
|
||||
const { encodeForm } = require(scriptPath);
|
||||
const result = encodeForm({
|
||||
grant_type: "client_credentials",
|
||||
client_id: "abc&def",
|
||||
client_secret: "test-secret",
|
||||
note: "x=y",
|
||||
});
|
||||
const params = new URLSearchParams(result);
|
||||
assert.equal(params.get("grant_type"), "client_credentials");
|
||||
assert.equal(params.get("client_id"), "abc&def");
|
||||
assert.equal(params.get("client_secret"), "test-secret");
|
||||
assert.equal(params.get("note"), "x=y");
|
||||
});
|
||||
|
||||
test("exits with error when app id is missing", async () => {
|
||||
const result = await runScript({ LARKSUITE_CLI_APP_ID: "" });
|
||||
assert.notEqual(result.exitCode, 0);
|
||||
assert.match(result.stderr, /Missing required environment variable: LARKSUITE_CLI_APP_ID/);
|
||||
});
|
||||
|
||||
test("exits with error when app secret is missing", async () => {
|
||||
const result = await runScript({ TEST_BOT1_APP_SECRET: "" });
|
||||
assert.notEqual(result.exitCode, 0);
|
||||
assert.match(result.stderr, /Missing required environment variable: TEST_BOT1_APP_SECRET/);
|
||||
});
|
||||
|
||||
test("fetches token and writes it to a private file", async () => {
|
||||
const { server, port } = await startServer((req, res, body) => {
|
||||
assert.equal(req.method, "POST");
|
||||
const params = new URLSearchParams(body);
|
||||
assert.equal(params.get("grant_type"), "client_credentials");
|
||||
assert.equal(params.get("client_id"), "test_app_id");
|
||||
assert.equal(params.get("client_secret"), "test-secret");
|
||||
res.writeHead(200, { "Content-Type": "application/json" });
|
||||
res.end(JSON.stringify({ code: 0, access_token: "test-token" }));
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await runScript({
|
||||
E2E_TAT_ENDPOINT: `http://127.0.0.1:${port}/token`,
|
||||
});
|
||||
|
||||
assert.equal(result.exitCode, 0, `stderr: ${result.stderr}`);
|
||||
assert.ok(result.stdout.includes("::add-mask::test-token"));
|
||||
assert.ok(result.stdout.includes("Prepared shared live E2E tenant token"));
|
||||
|
||||
const tatPath = path.join(result.tmpDir, "e2e-live-tat");
|
||||
assert.ok(fs.existsSync(tatPath), "token file should exist");
|
||||
|
||||
const stat = fs.statSync(tatPath);
|
||||
assert.equal(stat.mode & 0o777, 0o600, "token file should be owner-only");
|
||||
assert.equal(fs.readFileSync(tatPath, "utf8"), "test-token");
|
||||
|
||||
assert.ok(
|
||||
result.output.includes(`path=${tatPath}`),
|
||||
"should write path to GITHUB_OUTPUT",
|
||||
);
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("retries an interrupted response and then succeeds", async () => {
|
||||
let requestCount = 0;
|
||||
const { server, port } = await startServer((req, res) => {
|
||||
requestCount++;
|
||||
if (requestCount === 1) {
|
||||
abortResponse(res);
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, { "Content-Type": "application/json" });
|
||||
res.end(JSON.stringify({ code: 0, access_token: "test-token" }));
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await runScript({
|
||||
E2E_TAT_ENDPOINT: `http://127.0.0.1:${port}/token`,
|
||||
});
|
||||
|
||||
assert.equal(result.exitCode, 0, `stderr: ${result.stderr}`);
|
||||
assert.equal(requestCount, 2);
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("fails after every interrupted response is retried", async () => {
|
||||
let requestCount = 0;
|
||||
const { server, port } = await startServer((req, res) => {
|
||||
requestCount++;
|
||||
abortResponse(res);
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await runScript({
|
||||
E2E_TAT_ENDPOINT: `http://127.0.0.1:${port}/token`,
|
||||
});
|
||||
|
||||
assert.notEqual(result.exitCode, 0);
|
||||
assert.equal(requestCount, 4);
|
||||
assert.match(result.stderr, /Failed to fetch tenant access token/);
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("exits with error after all retries fail", async () => {
|
||||
let requestCount = 0;
|
||||
const { server, port } = await startServer((req, res) => {
|
||||
requestCount++;
|
||||
res.writeHead(500, { "Content-Type": "application/json" });
|
||||
res.end(JSON.stringify({ code: 500, error: "server error" }));
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await runScript({
|
||||
E2E_TAT_ENDPOINT: `http://127.0.0.1:${port}/token`,
|
||||
});
|
||||
|
||||
assert.notEqual(result.exitCode, 0);
|
||||
assert.equal(requestCount, 4);
|
||||
assert.match(result.stderr, /Failed to fetch tenant access token/);
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
});
|
||||
@@ -265,10 +265,7 @@ function getExpectedChecksum(archiveName, checksumsDir) {
|
||||
const checksumsPath = path.join(dir, "checksums.txt");
|
||||
|
||||
if (!fs.existsSync(checksumsPath)) {
|
||||
console.error(
|
||||
"[WARN] checksums.txt not found, skipping checksum verification"
|
||||
);
|
||||
return null;
|
||||
throw new Error(`[SECURITY] checksums.txt not found at ${checksumsPath}`);
|
||||
}
|
||||
|
||||
const content = fs.readFileSync(checksumsPath, "utf8");
|
||||
@@ -286,7 +283,14 @@ function getExpectedChecksum(archiveName, checksumsDir) {
|
||||
}
|
||||
|
||||
function verifyChecksum(archivePath, expectedHash) {
|
||||
if (expectedHash === null) return;
|
||||
if (typeof expectedHash !== "string" || expectedHash.length === 0) {
|
||||
throw new Error("[SECURITY] Expected checksum is missing or invalid");
|
||||
}
|
||||
if (!/^[0-9a-f]{64}$/i.test(expectedHash)) {
|
||||
throw new Error(
|
||||
"[SECURITY] Expected checksum must be a 64-character hexadecimal SHA-256 digest"
|
||||
);
|
||||
}
|
||||
|
||||
// Stream the file to avoid loading the entire archive into memory.
|
||||
// Archives can be 10-100MB; streaming keeps RSS constant.
|
||||
|
||||
@@ -52,11 +52,12 @@ describe("getExpectedChecksum", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("returns null when checksums.txt does not exist", () => {
|
||||
it("throws [SECURITY]-prefixed Error when checksums.txt does not exist", () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "checksum-test-"));
|
||||
// No checksums.txt in dir
|
||||
const result = getExpectedChecksum("anything.tar.gz", dir);
|
||||
assert.equal(result, null);
|
||||
assert.throws(
|
||||
() => getExpectedChecksum("anything.tar.gz", dir),
|
||||
{ message: /^\[SECURITY\] checksums\.txt not found/ }
|
||||
);
|
||||
});
|
||||
|
||||
it("skips malformed lines and still finds valid entry", () => {
|
||||
@@ -106,7 +107,7 @@ describe("verifyChecksum", () => {
|
||||
verifyChecksum(filePath, hash);
|
||||
});
|
||||
|
||||
it("matches case-insensitively", () => {
|
||||
it("accepts a valid uppercase 64-character hex hash", () => {
|
||||
const content = "case test";
|
||||
const filePath = makeTmpFile(content);
|
||||
const hash = sha256(content).toUpperCase();
|
||||
@@ -114,6 +115,40 @@ describe("verifyChecksum", () => {
|
||||
verifyChecksum(filePath, hash);
|
||||
});
|
||||
|
||||
for (const [name, expectedHash] of [
|
||||
["null", null],
|
||||
["empty", ""],
|
||||
["non-string", 123],
|
||||
]) {
|
||||
it(`throws [SECURITY]-prefixed Error for ${name} expected hash`, () => {
|
||||
const filePath = makeTmpFile("real content");
|
||||
assert.throws(
|
||||
() => verifyChecksum(filePath, expectedHash),
|
||||
(err) => {
|
||||
assert.match(err.message, /^\[SECURITY\]/);
|
||||
assert.match(err.message, /Expected checksum is missing or invalid/);
|
||||
return true;
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
it("throws [SECURITY] format Error for an incorrectly sized hash", () => {
|
||||
const filePath = makeTmpFile("real content");
|
||||
assert.throws(
|
||||
() => verifyChecksum(filePath, "abc123"),
|
||||
{ message: /^\[SECURITY\] Expected checksum must be a 64-character hexadecimal SHA-256 digest$/ }
|
||||
);
|
||||
});
|
||||
|
||||
it("throws [SECURITY] format Error for a non-hex hash", () => {
|
||||
const filePath = makeTmpFile("real content");
|
||||
assert.throws(
|
||||
() => verifyChecksum(filePath, "g".repeat(64)),
|
||||
{ message: /^\[SECURITY\] Expected checksum must be a 64-character hexadecimal SHA-256 digest$/ }
|
||||
);
|
||||
});
|
||||
|
||||
it("throws [SECURITY]-prefixed Error on mismatch", () => {
|
||||
const filePath = makeTmpFile("real content");
|
||||
assert.throws(
|
||||
|
||||
110
scripts/release-preflight.js
Normal file
110
scripts/release-preflight.js
Normal file
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env node
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
const STABLE_VERSION_PATTERN = /^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$/;
|
||||
const REHEARSAL_VERSION_PATTERN = /^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)-beta\.(0|[1-9][0-9]*)$/;
|
||||
|
||||
function isReleaseVersion(value) {
|
||||
return typeof value === "string" &&
|
||||
(STABLE_VERSION_PATTERN.test(value) || REHEARSAL_VERSION_PATTERN.test(value));
|
||||
}
|
||||
|
||||
function releaseError(message, observed, hint) {
|
||||
return { ok: false, error: { type: "release_preflight", message, observed, hint } };
|
||||
}
|
||||
|
||||
function validateReleasePreflight(packageJson, packageLockJson, tag) {
|
||||
const packageVersion = packageJson?.version;
|
||||
const lockVersion = packageLockJson?.version;
|
||||
const lockRootVersion = packageLockJson?.packages?.[""]?.version;
|
||||
const observed = {
|
||||
packageVersion: packageVersion ?? null,
|
||||
lockVersion: lockVersion ?? null,
|
||||
lockRootVersion: lockRootVersion ?? null,
|
||||
tagVersion: null,
|
||||
};
|
||||
|
||||
for (const [field, value] of [
|
||||
["package.json.version", packageVersion],
|
||||
["package-lock.json.version", lockVersion],
|
||||
['package-lock.json.packages[""].version', lockRootVersion],
|
||||
]) {
|
||||
if (!isReleaseVersion(value)) {
|
||||
return releaseError(
|
||||
`${field} must use X.Y.Z or the rehearsal form X.Y.Z-beta.N`,
|
||||
observed,
|
||||
"Use the same version in all package fields; only stable releases and the temporary beta rehearsal form are allowed.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (packageVersion !== lockVersion || packageVersion !== lockRootVersion) {
|
||||
return releaseError(
|
||||
"Package version fields do not match",
|
||||
observed,
|
||||
"Synchronize package.json.version and both package-lock.json version fields.",
|
||||
);
|
||||
}
|
||||
|
||||
if (tag === undefined) {
|
||||
return { ok: true, data: observed };
|
||||
}
|
||||
if (typeof tag !== "string" || !tag.startsWith("v") || !isReleaseVersion(tag.slice(1))) {
|
||||
return releaseError(
|
||||
"--tag must use vX.Y.Z or the rehearsal form vX.Y.Z-beta.N",
|
||||
{ ...observed, tag },
|
||||
`Use --tag v${packageVersion}.`,
|
||||
);
|
||||
}
|
||||
|
||||
const tagVersion = tag.slice(1);
|
||||
if (tagVersion !== packageVersion) {
|
||||
return releaseError(
|
||||
"Tag version does not match the package version",
|
||||
{ ...observed, tagVersion, tag },
|
||||
`Use --tag v${packageVersion}.`,
|
||||
);
|
||||
}
|
||||
return { ok: true, data: { ...observed, tagVersion } };
|
||||
}
|
||||
|
||||
function writeResult(result) {
|
||||
(result.ok ? process.stdout : process.stderr).write(`${JSON.stringify(result)}\n`);
|
||||
if (!result.ok) process.exitCode = 1;
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = process.argv.slice(2);
|
||||
let tag;
|
||||
if (args.length === 2 && args[0] === "--tag") {
|
||||
tag = args[1];
|
||||
} else if (args.length !== 0) {
|
||||
writeResult(releaseError(
|
||||
"Expected no arguments or --tag vX.Y.Z",
|
||||
{ arguments: args },
|
||||
"Run release:check without arguments or pass exactly one --tag value.",
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
const repoRoot = path.resolve(__dirname, "..");
|
||||
try {
|
||||
const packageJson = JSON.parse(fs.readFileSync(path.join(repoRoot, "package.json"), "utf8"));
|
||||
const packageLockJson = JSON.parse(fs.readFileSync(path.join(repoRoot, "package-lock.json"), "utf8"));
|
||||
writeResult(validateReleasePreflight(packageJson, packageLockJson, tag));
|
||||
} catch (error) {
|
||||
writeResult(releaseError(
|
||||
"Could not read release package metadata",
|
||||
{ reason: error.message },
|
||||
"Ensure package.json and package-lock.json exist and contain valid JSON.",
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { validateReleasePreflight };
|
||||
|
||||
if (require.main === module) main();
|
||||
611
scripts/release-preflight.test.js
Normal file
611
scripts/release-preflight.test.js
Normal file
@@ -0,0 +1,611 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
const assert = require("node:assert/strict");
|
||||
const { spawnSync } = require("node:child_process");
|
||||
const fs = require("node:fs");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
const { describe, it } = require("node:test");
|
||||
|
||||
const {
|
||||
validateReleasePreflight,
|
||||
} = require("./release-preflight");
|
||||
|
||||
const repoRoot = path.resolve(__dirname, "..");
|
||||
|
||||
function createReleaseFixture(t, env = {}) {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "tag-release-test-"));
|
||||
const scriptsDir = path.join(root, "scripts");
|
||||
const binDir = path.join(root, "bin");
|
||||
const stateDir = path.join(root, "state");
|
||||
const logPath = path.join(root, "git-calls.jsonl");
|
||||
const npmLogPath = path.join(root, "npm-calls.jsonl");
|
||||
fs.mkdirSync(scriptsDir);
|
||||
fs.mkdirSync(binDir);
|
||||
fs.mkdirSync(stateDir);
|
||||
fs.copyFileSync(
|
||||
path.join(repoRoot, "scripts/release-preflight.js"),
|
||||
path.join(scriptsDir, "release-preflight.js"),
|
||||
);
|
||||
fs.copyFileSync(
|
||||
path.join(repoRoot, "scripts/tag-release.sh"),
|
||||
path.join(scriptsDir, "tag-release.sh"),
|
||||
);
|
||||
fs.writeFileSync(path.join(root, "package.json"), '{"version":"1.2.3-beta.0"}\n');
|
||||
fs.writeFileSync(
|
||||
path.join(root, "package-lock.json"),
|
||||
'{"version":"1.2.3-beta.0","packages":{"":{"version":"1.2.3-beta.0"}}}\n',
|
||||
);
|
||||
|
||||
const fakeGitPath = path.join(binDir, "git");
|
||||
fs.writeFileSync(fakeGitPath, String.raw`#!/usr/bin/env node
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const stateDir = process.env.FAKE_GIT_STATE_DIR;
|
||||
const localTagPath = path.join(stateDir, "local-tag");
|
||||
if (process.cwd() !== process.env.FAKE_EXPECTED_GIT_CWD) {
|
||||
process.stderr.write("git invoked outside repository root: " + process.cwd() + "\n");
|
||||
process.exit(96);
|
||||
}
|
||||
fs.appendFileSync(process.env.FAKE_GIT_LOG, JSON.stringify(args) + "\n");
|
||||
|
||||
function print(value) {
|
||||
process.stdout.write(value + "\n");
|
||||
}
|
||||
|
||||
switch (args[0]) {
|
||||
case "branch":
|
||||
print(process.env.FAKE_BRANCH || "test/npm-staged-publish-rehearsal");
|
||||
break;
|
||||
case "status":
|
||||
if (process.env.FAKE_STATUS_OUTPUT) print(process.env.FAKE_STATUS_OUTPUT);
|
||||
break;
|
||||
case "fetch":
|
||||
break;
|
||||
case "rev-parse": {
|
||||
const ref = args[args.length - 1];
|
||||
if (ref === "HEAD") {
|
||||
print(process.env.FAKE_HEAD_SHA);
|
||||
break;
|
||||
}
|
||||
if (ref === "FETCH_HEAD^{commit}") {
|
||||
print(process.env.FAKE_REHEARSAL_SHA);
|
||||
break;
|
||||
}
|
||||
if (ref.startsWith("refs/tags/")) {
|
||||
if (fs.existsSync(localTagPath)) {
|
||||
print(fs.readFileSync(localTagPath, "utf8").trim());
|
||||
break;
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
process.stderr.write("unexpected rev-parse ref: " + ref + "\n");
|
||||
process.exit(97);
|
||||
break;
|
||||
}
|
||||
case "ls-remote": {
|
||||
const tagRef = args.find((arg) => arg.startsWith("refs/tags/") && !arg.endsWith("^{}"));
|
||||
const kind = process.env.FAKE_REMOTE_TAG_KIND || "absent";
|
||||
if (kind === "lightweight" || kind === "annotated") {
|
||||
print(process.env.FAKE_REMOTE_TAG_SHA + "\t" + tagRef);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "show":
|
||||
print(process.env.FAKE_WORKFLOW);
|
||||
break;
|
||||
case "tag":
|
||||
fs.writeFileSync(localTagPath, args[2] || process.env.FAKE_HEAD_SHA);
|
||||
break;
|
||||
case "push": {
|
||||
const failedMarker = path.join(stateDir, "push-failed");
|
||||
if (process.env.FAKE_PUSH_FAIL_ONCE && !fs.existsSync(failedMarker)) {
|
||||
fs.writeFileSync(failedMarker, "1");
|
||||
process.exit(Number(process.env.FAKE_PUSH_FAIL_ONCE));
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
process.stderr.write("unexpected git command: " + args.join(" ") + "\n");
|
||||
process.exit(97);
|
||||
}
|
||||
`);
|
||||
fs.chmodSync(fakeGitPath, 0o755);
|
||||
|
||||
const fakeNpmPath = path.join(binDir, "npm");
|
||||
fs.writeFileSync(fakeNpmPath, String.raw`#!/usr/bin/env node
|
||||
const fs = require("node:fs");
|
||||
const args = process.argv.slice(2);
|
||||
fs.appendFileSync(process.env.FAKE_NPM_LOG, JSON.stringify(args) + "\n");
|
||||
if (args[0] !== "view") {
|
||||
process.stderr.write("unexpected npm command: " + args.join(" ") + "\n");
|
||||
process.exit(97);
|
||||
}
|
||||
const output = process.env.FAKE_NPM_VIEW_OUTPUT || "npm error code E404\nnpm error 404 Not Found";
|
||||
(Number(process.env.FAKE_NPM_VIEW_STATUS || "1") === 0 ? process.stdout : process.stderr).write(output + "\n");
|
||||
process.exit(Number(process.env.FAKE_NPM_VIEW_STATUS || "1"));
|
||||
`);
|
||||
fs.chmodSync(fakeNpmPath, 0o755);
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
|
||||
return {
|
||||
root,
|
||||
stateDir,
|
||||
logPath,
|
||||
env: {
|
||||
...process.env,
|
||||
PATH: `${binDir}${path.delimiter}${process.env.PATH}`,
|
||||
LANG: "C",
|
||||
LC_ALL: "C",
|
||||
FAKE_GIT_LOG: logPath,
|
||||
FAKE_NPM_LOG: npmLogPath,
|
||||
FAKE_GIT_STATE_DIR: stateDir,
|
||||
FAKE_EXPECTED_GIT_CWD: fs.realpathSync(root),
|
||||
FAKE_HEAD_SHA: "aaaaaaaa",
|
||||
FAKE_REHEARSAL_SHA: "aaaaaaaa",
|
||||
FAKE_WORKFLOW: "args: release --clean --skip=publish\nrun: npm stage publish package.tgz --access public --tag beta",
|
||||
...env,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function runTagRelease(fixture, options = {}) {
|
||||
const { cwd = fixture.root, args = [], input = "" } = options;
|
||||
return spawnSync("bash", [path.join(fixture.root, "scripts/tag-release.sh"), ...args], {
|
||||
cwd,
|
||||
env: fixture.env,
|
||||
encoding: "utf8",
|
||||
input,
|
||||
});
|
||||
}
|
||||
|
||||
function readGitCalls(fixture) {
|
||||
if (!fs.existsSync(fixture.logPath)) {
|
||||
return [];
|
||||
}
|
||||
return fs.readFileSync(fixture.logPath, "utf8")
|
||||
.trim()
|
||||
.split("\n")
|
||||
.filter(Boolean)
|
||||
.map((line) => JSON.parse(line));
|
||||
}
|
||||
|
||||
function assertNoTagOperations(calls) {
|
||||
const tagOperations = calls.filter((args) =>
|
||||
args[0] === "ls-remote" ||
|
||||
args[0] === "tag" ||
|
||||
args[0] === "push" ||
|
||||
(args[0] === "rev-parse" && args.some((arg) => arg.startsWith("refs/tags/"))),
|
||||
);
|
||||
assert.deepEqual(tagOperations, []);
|
||||
}
|
||||
|
||||
function assertNoTagWrites(calls) {
|
||||
assert.equal(calls.some((args) => args[0] === "tag" || args[0] === "push"), false);
|
||||
}
|
||||
|
||||
function validInputs(version = "1.2.3") {
|
||||
return {
|
||||
packageJson: { version },
|
||||
packageLockJson: {
|
||||
version,
|
||||
packages: { "": { version } },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function assertStructuredError(result) {
|
||||
assert.equal(result.ok, false);
|
||||
assert.equal(result.error.type, "release_preflight");
|
||||
assert.equal(typeof result.error.message, "string");
|
||||
assert.ok(result.error.message.length > 0);
|
||||
assert.equal(typeof result.error.observed, "object");
|
||||
assert.equal(typeof result.error.hint, "string");
|
||||
assert.ok(result.error.hint.length > 0);
|
||||
}
|
||||
|
||||
function assertInOrder(source, snippets) {
|
||||
let previous = -1;
|
||||
for (const snippet of snippets) {
|
||||
const index = source.indexOf(snippet);
|
||||
assert.ok(index >= 0, `missing fragment: ${snippet}`);
|
||||
assert.ok(index > previous, `fragment is out of order: ${snippet}`);
|
||||
previous = index;
|
||||
}
|
||||
}
|
||||
|
||||
describe("validateReleasePreflight", () => {
|
||||
it("accepts matching stable and beta rehearsal versions", () => {
|
||||
for (const version of ["1.2.3", "1.2.3-beta.0"]) {
|
||||
const { packageJson, packageLockJson } = validInputs(version);
|
||||
|
||||
assert.deepEqual(validateReleasePreflight(packageJson, packageLockJson), {
|
||||
ok: true,
|
||||
data: {
|
||||
packageVersion: version,
|
||||
lockVersion: version,
|
||||
lockRootVersion: version,
|
||||
tagVersion: null,
|
||||
},
|
||||
});
|
||||
assert.deepEqual(
|
||||
validateReleasePreflight(packageJson, packageLockJson, `v${version}`),
|
||||
{
|
||||
ok: true,
|
||||
data: {
|
||||
packageVersion: version,
|
||||
lockVersion: version,
|
||||
lockRootVersion: version,
|
||||
tagVersion: version,
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects prerelease forms other than beta rehearsal versions", () => {
|
||||
const { packageJson, packageLockJson } = validInputs("1.2.3-rc.1");
|
||||
|
||||
const result = validateReleasePreflight(packageJson, packageLockJson);
|
||||
|
||||
assertStructuredError(result);
|
||||
assert.equal(
|
||||
result.error.message,
|
||||
"package.json.version must use X.Y.Z or the rehearsal form X.Y.Z-beta.N",
|
||||
);
|
||||
assert.equal(
|
||||
result.error.hint,
|
||||
"Use the same version in all package fields; only stable releases and the temporary beta rehearsal form are allowed.",
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects build metadata package versions with the stable release contract", () => {
|
||||
const { packageJson, packageLockJson } = validInputs("1.2.3+build.7");
|
||||
|
||||
const result = validateReleasePreflight(packageJson, packageLockJson);
|
||||
|
||||
assertStructuredError(result);
|
||||
assert.equal(
|
||||
result.error.message,
|
||||
"package.json.version must use X.Y.Z or the rehearsal form X.Y.Z-beta.N",
|
||||
);
|
||||
assert.equal(
|
||||
result.error.hint,
|
||||
"Use the same version in all package fields; only stable releases and the temporary beta rehearsal form are allowed.",
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects invalid and missing package or lock SemVer values", () => {
|
||||
const invalid = validInputs();
|
||||
invalid.packageJson.version = "01.2.3";
|
||||
const missing = validInputs();
|
||||
delete missing.packageLockJson.packages[""].version;
|
||||
|
||||
for (const result of [
|
||||
validateReleasePreflight(invalid.packageJson, invalid.packageLockJson),
|
||||
validateReleasePreflight(missing.packageJson, missing.packageLockJson),
|
||||
]) {
|
||||
assertStructuredError(result);
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects a top-level package-lock version mismatch", () => {
|
||||
const { packageJson, packageLockJson } = validInputs();
|
||||
packageLockJson.version = "1.2.4";
|
||||
|
||||
const result = validateReleasePreflight(packageJson, packageLockJson);
|
||||
|
||||
assertStructuredError(result);
|
||||
assert.deepEqual(result.error.observed, {
|
||||
packageVersion: "1.2.3",
|
||||
lockVersion: "1.2.4",
|
||||
lockRootVersion: "1.2.3",
|
||||
tagVersion: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects a package-lock root package version mismatch", () => {
|
||||
const { packageJson, packageLockJson } = validInputs();
|
||||
packageLockJson.packages[""].version = "1.2.4";
|
||||
|
||||
const result = validateReleasePreflight(packageJson, packageLockJson);
|
||||
|
||||
assertStructuredError(result);
|
||||
assert.deepEqual(result.error.observed, {
|
||||
packageVersion: "1.2.3",
|
||||
lockVersion: "1.2.3",
|
||||
lockRootVersion: "1.2.4",
|
||||
tagVersion: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects invalid and mismatched tags", () => {
|
||||
const { packageJson, packageLockJson } = validInputs();
|
||||
|
||||
for (const tag of ["1.2.3", "v01.2.3", "v1.2.4"]) {
|
||||
const result = validateReleasePreflight(packageJson, packageLockJson, tag);
|
||||
assertStructuredError(result);
|
||||
assert.equal(result.error.observed.tag, tag);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("release configuration", () => {
|
||||
it("writes success to stdout and structured failures to stderr", () => {
|
||||
const scriptPath = path.join(repoRoot, "scripts/release-preflight.js");
|
||||
const packageVersion = require(path.join(repoRoot, "package.json")).version;
|
||||
const success = spawnSync(process.execPath, [scriptPath, "--tag", `v${packageVersion}`], {
|
||||
cwd: repoRoot,
|
||||
encoding: "utf8",
|
||||
});
|
||||
const failure = spawnSync(process.execPath, [scriptPath, "--tag", "invalid"], {
|
||||
cwd: repoRoot,
|
||||
encoding: "utf8",
|
||||
});
|
||||
|
||||
assert.equal(success.status, 0);
|
||||
assert.equal(success.stderr, "");
|
||||
assert.deepEqual(JSON.parse(success.stdout), {
|
||||
ok: true,
|
||||
data: {
|
||||
packageVersion,
|
||||
lockVersion: packageVersion,
|
||||
lockRootVersion: packageVersion,
|
||||
tagVersion: packageVersion,
|
||||
},
|
||||
});
|
||||
assert.equal(failure.status, 1);
|
||||
assert.equal(failure.stdout, "");
|
||||
assertStructuredError(JSON.parse(failure.stderr));
|
||||
});
|
||||
|
||||
it("keeps package metadata synchronized without changing the Node engine", () => {
|
||||
const packageJson = require(path.join(repoRoot, "package.json"));
|
||||
const packageLockJson = require(path.join(repoRoot, "package-lock.json"));
|
||||
|
||||
assert.equal(packageJson.scripts["release:check"], "node scripts/release-preflight.js");
|
||||
assert.equal(packageJson.engines.node, ">=16");
|
||||
assert.equal(packageLockJson.version, packageJson.version);
|
||||
assert.equal(packageLockJson.packages[""].version, packageJson.version);
|
||||
});
|
||||
|
||||
it("runs every release gate before any tag query, creation, or push", () => {
|
||||
const script = fs.readFileSync(path.join(repoRoot, "scripts/tag-release.sh"), "utf8");
|
||||
const preflight = script.indexOf('node "${SCRIPT_DIR}/release-preflight.js" --tag "${TAG}"');
|
||||
const requiredGates = [
|
||||
'CURRENT_BRANCH=$(git branch --show-current)',
|
||||
'git status --porcelain',
|
||||
'git fetch origin "${REHEARSAL_BRANCH}"',
|
||||
'git rev-parse "FETCH_HEAD^{commit}"',
|
||||
'git show "${HEAD_SHA}:.github/workflows/release.yml"',
|
||||
'npm view "@larksuite/cli@${VERSION}" version',
|
||||
];
|
||||
const tagOperations = [
|
||||
'git rev-parse -q --verify "refs/tags/${TAG}"',
|
||||
'git ls-remote --tags origin "refs/tags/${TAG}"',
|
||||
'git tag "${TAG}" "${HEAD_SHA}"',
|
||||
'git push origin "refs/tags/${TAG}:refs/tags/${TAG}"',
|
||||
];
|
||||
|
||||
assert.ok(preflight >= 0, "release preflight invocation is missing");
|
||||
assertInOrder(script, [
|
||||
'REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"',
|
||||
'cd "${REPO_ROOT}"',
|
||||
'node "${SCRIPT_DIR}/release-preflight.js"',
|
||||
]);
|
||||
assert.equal(script.includes("require('${REPO_ROOT}/package.json')"), false);
|
||||
for (const gate of requiredGates) {
|
||||
const index = script.indexOf(gate);
|
||||
assert.ok(index >= 0, `required release gate is missing: ${gate}`);
|
||||
assert.ok(index < script.indexOf(tagOperations[0]), `${gate} must run before tag queries`);
|
||||
}
|
||||
for (const operation of tagOperations) {
|
||||
const index = script.indexOf(operation);
|
||||
assert.ok(index >= 0, `tag operation is missing: ${operation}`);
|
||||
assert.ok(preflight < index, `preflight must run before: ${operation}`);
|
||||
}
|
||||
assertInOrder(script, [
|
||||
'if [ "${PUSH_TAG}" != true ]',
|
||||
'read -r CONFIRM_TAG',
|
||||
'git tag "${TAG}" "${HEAD_SHA}"',
|
||||
'git push origin "refs/tags/${TAG}:refs/tags/${TAG}"',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("tag-release.sh behavior", () => {
|
||||
it("runs repository checks from the script repository when invoked elsewhere", (t) => {
|
||||
const fixture = createReleaseFixture(t);
|
||||
const outside = fs.mkdtempSync(path.join(os.tmpdir(), "tag-release-cwd-"));
|
||||
t.after(() => fs.rmSync(outside, { recursive: true, force: true }));
|
||||
|
||||
const result = runTagRelease(fixture, { cwd: outside });
|
||||
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
});
|
||||
|
||||
it("rejects a non-rehearsal branch before querying or modifying tags", (t) => {
|
||||
const fixture = createReleaseFixture(t, { FAKE_BRANCH: "feature/release" });
|
||||
|
||||
const result = runTagRelease(fixture);
|
||||
const calls = readGitCalls(fixture);
|
||||
|
||||
assert.equal(result.status, 1);
|
||||
assert.match(result.stderr, /must be created from test\/npm-staged-publish-rehearsal/i);
|
||||
assertNoTagOperations(calls);
|
||||
});
|
||||
|
||||
it("rejects a dirty working tree before tag operations", (t) => {
|
||||
const fixture = createReleaseFixture(t, { FAKE_STATUS_OUTPUT: " M README.md" });
|
||||
const result = runTagRelease(fixture);
|
||||
const calls = readGitCalls(fixture);
|
||||
|
||||
assert.equal(result.status, 1);
|
||||
assert.match(result.stderr, /working tree must be clean/i);
|
||||
assertNoTagOperations(calls);
|
||||
});
|
||||
|
||||
it("rejects HEAD that differs from the fetched rehearsal branch", (t) => {
|
||||
const fixture = createReleaseFixture(t, { FAKE_REHEARSAL_SHA: "bbbbbbbb" });
|
||||
|
||||
const result = runTagRelease(fixture);
|
||||
const calls = readGitCalls(fixture);
|
||||
|
||||
assert.equal(result.status, 1);
|
||||
assert.match(result.stderr, /HEAD must exactly match origin\/test\/npm-staged-publish-rehearsal/i);
|
||||
assertNoTagOperations(calls);
|
||||
});
|
||||
|
||||
it("compares HEAD with the exact fetched rehearsal commit", (t) => {
|
||||
const fixture = createReleaseFixture(t);
|
||||
|
||||
const result = runTagRelease(fixture);
|
||||
const calls = readGitCalls(fixture);
|
||||
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
assert.ok(calls.some((args) => args.join(" ") === "fetch origin test/npm-staged-publish-rehearsal"));
|
||||
assert.ok(calls.some((args) => args.join(" ") === "rev-parse FETCH_HEAD^{commit}"));
|
||||
assert.equal(calls.some((args) => args.includes("origin/test/npm-staged-publish-rehearsal")), false);
|
||||
});
|
||||
|
||||
it("fails when the local tag already exists", (t) => {
|
||||
const fixture = createReleaseFixture(t);
|
||||
fs.writeFileSync(path.join(fixture.stateDir, "local-tag"), "bbbbbbbb");
|
||||
|
||||
const result = runTagRelease(fixture);
|
||||
const calls = readGitCalls(fixture);
|
||||
|
||||
assert.equal(result.status, 1);
|
||||
assert.match(result.stderr, /local tag .* already exists/i);
|
||||
assert.equal(calls.some((args) => args[0] === "ls-remote"), false);
|
||||
assert.equal(calls.some((args) => args[0] === "push"), false);
|
||||
});
|
||||
|
||||
it("fails when a lightweight or annotated remote tag already exists", (t) => {
|
||||
for (const kind of ["lightweight", "annotated"]) {
|
||||
const fixture = createReleaseFixture(t, {
|
||||
FAKE_REMOTE_TAG_KIND: kind,
|
||||
FAKE_REMOTE_TAG_SHA: "aaaaaaaa",
|
||||
});
|
||||
|
||||
const result = runTagRelease(fixture);
|
||||
const calls = readGitCalls(fixture);
|
||||
|
||||
assert.equal(result.status, 1, `${kind}: ${result.stderr}`);
|
||||
assert.match(result.stderr, /remote tag .* already exists/i);
|
||||
assert.equal(calls.some((args) => args[0] === "tag"), false);
|
||||
assert.equal(calls.some((args) => args[0] === "push"), false);
|
||||
}
|
||||
});
|
||||
|
||||
it("check mode completes without creating or pushing a tag", (t) => {
|
||||
const fixture = createReleaseFixture(t);
|
||||
|
||||
const result = runTagRelease(fixture);
|
||||
const calls = readGitCalls(fixture);
|
||||
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
assert.match(result.stdout, /No tag was created or pushed/);
|
||||
assertNoTagWrites(calls);
|
||||
});
|
||||
|
||||
it("rejects a production version before invoking git", (t) => {
|
||||
const fixture = createReleaseFixture(t);
|
||||
fs.writeFileSync(path.join(fixture.root, "package.json"), '{"version":"1.2.3"}\n');
|
||||
fs.writeFileSync(
|
||||
path.join(fixture.root, "package-lock.json"),
|
||||
'{"version":"1.2.3","packages":{"":{"version":"1.2.3"}}}\n',
|
||||
);
|
||||
|
||||
const result = runTagRelease(fixture);
|
||||
|
||||
assert.equal(result.status, 1);
|
||||
assert.match(result.stderr, /require an X\.Y\.Z-beta\.N version/);
|
||||
assert.deepEqual(readGitCalls(fixture), []);
|
||||
});
|
||||
|
||||
it("rejects a workflow that can publish live", (t) => {
|
||||
for (const workflow of [
|
||||
"args: release --clean --skip=publish\nrun: npm publish --access public",
|
||||
"args: release --clean --skip=publish\nrun: npm stage publish package.tgz --access public --tag beta\nrun: gh release create v1.2.3-beta.0",
|
||||
"args: release --clean --skip=publish\npermissions:\n contents: write\nrun: npm stage publish package.tgz --access public --tag beta",
|
||||
"args: release --clean --skip=publish\nenv:\n GITHUB_TOKEN: ${{ github.token }}\nrun: npm stage publish package.tgz --access public --tag beta",
|
||||
]) {
|
||||
const fixture = createReleaseFixture(t, { FAKE_WORKFLOW: workflow });
|
||||
const result = runTagRelease(fixture);
|
||||
|
||||
assert.equal(result.status, 1);
|
||||
assert.match(result.stderr, /must be stage-only/i);
|
||||
assertNoTagWrites(readGitCalls(fixture));
|
||||
}
|
||||
});
|
||||
|
||||
it("fails closed when npm cannot prove that the version is unused", (t) => {
|
||||
const fixture = createReleaseFixture(t, {
|
||||
FAKE_NPM_VIEW_OUTPUT: "npm error code ETIMEDOUT",
|
||||
});
|
||||
|
||||
const result = runTagRelease(fixture);
|
||||
|
||||
assert.equal(result.status, 1);
|
||||
assert.match(result.stderr, /npm version lookup failed/i);
|
||||
assertNoTagWrites(readGitCalls(fixture));
|
||||
});
|
||||
|
||||
it("rejects an existing npm version", (t) => {
|
||||
const fixture = createReleaseFixture(t, {
|
||||
FAKE_NPM_VIEW_STATUS: "0",
|
||||
FAKE_NPM_VIEW_OUTPUT: "1.2.3-beta.0",
|
||||
});
|
||||
|
||||
const result = runTagRelease(fixture);
|
||||
|
||||
assert.equal(result.status, 1);
|
||||
assert.match(result.stderr, /already exists on npm/i);
|
||||
assertNoTagWrites(readGitCalls(fixture));
|
||||
});
|
||||
|
||||
it("requires the full tag confirmation in push mode", (t) => {
|
||||
const fixture = createReleaseFixture(t);
|
||||
|
||||
const result = runTagRelease(fixture, { args: ["--push"], input: "no\n" });
|
||||
|
||||
assert.equal(result.status, 1);
|
||||
assert.match(result.stderr, /confirmation did not exactly match/i);
|
||||
assertNoTagWrites(readGitCalls(fixture));
|
||||
});
|
||||
|
||||
it("pushes only the exact confirmed tag ref", (t) => {
|
||||
const fixture = createReleaseFixture(t);
|
||||
|
||||
const result = runTagRelease(fixture, {
|
||||
args: ["--push"],
|
||||
input: "v1.2.3-beta.0\n",
|
||||
});
|
||||
const calls = readGitCalls(fixture);
|
||||
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
assert.ok(calls.some((args) => args.join(" ") === "tag v1.2.3-beta.0 aaaaaaaa"));
|
||||
assert.ok(calls.some((args) =>
|
||||
args.join(" ") === "push origin refs/tags/v1.2.3-beta.0:refs/tags/v1.2.3-beta.0"));
|
||||
assert.equal(
|
||||
calls.some((args) => args[0] === "push" && args.includes("--tags")),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
it("reports an invalid package version before invoking git", (t) => {
|
||||
const fixture = createReleaseFixture(t);
|
||||
fs.writeFileSync(path.join(fixture.root, "package.json"), '{"version":"01.2.3"}\n');
|
||||
|
||||
const result = runTagRelease(fixture);
|
||||
|
||||
assert.equal(result.status, 1);
|
||||
assert.equal(result.stdout, "");
|
||||
assertStructuredError(JSON.parse(result.stderr));
|
||||
assert.deepEqual(readGitCalls(fixture), []);
|
||||
});
|
||||
});
|
||||
168
scripts/release-workflow.test.js
Normal file
168
scripts/release-workflow.test.js
Normal file
@@ -0,0 +1,168 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const { describe, it } = require("node:test");
|
||||
|
||||
const repoRoot = path.resolve(__dirname, "..");
|
||||
const releaseWorkflow = fs.readFileSync(
|
||||
path.join(repoRoot, ".github/workflows/release.yml"),
|
||||
"utf8",
|
||||
);
|
||||
const previewWorkflow = fs.readFileSync(
|
||||
path.join(repoRoot, ".github/workflows/pkg-pr-new.yml"),
|
||||
"utf8",
|
||||
);
|
||||
function topLevelBlock(source, name) {
|
||||
const match = source.match(
|
||||
new RegExp(
|
||||
`^${name}:\\n([\\s\\S]*?)(?=^[A-Za-z][A-Za-z0-9_-]*:|(?![\\s\\S]))`,
|
||||
"m",
|
||||
),
|
||||
);
|
||||
assert.ok(match, `missing top-level ${name} block`);
|
||||
return match[0];
|
||||
}
|
||||
|
||||
function jobBlock(source, name) {
|
||||
const jobs = topLevelBlock(source, "jobs");
|
||||
const match = jobs.match(
|
||||
new RegExp(
|
||||
`^ ${name}:\\n([\\s\\S]*?)(?=^ [A-Za-z][A-Za-z0-9_-]*:|(?![\\s\\S]))`,
|
||||
"m",
|
||||
),
|
||||
);
|
||||
assert.ok(match, `missing ${name} job`);
|
||||
return match[0];
|
||||
}
|
||||
|
||||
function assertInOrder(source, snippets) {
|
||||
let previous = -1;
|
||||
for (const snippet of snippets) {
|
||||
const index = source.indexOf(snippet);
|
||||
assert.ok(index >= 0, `missing workflow fragment: ${snippet}`);
|
||||
assert.ok(index > previous, `workflow fragment is out of order: ${snippet}`);
|
||||
previous = index;
|
||||
}
|
||||
}
|
||||
|
||||
function permissionLines(job) {
|
||||
const match = job.match(/^ permissions:\n((?: .+\n)+)/m);
|
||||
assert.ok(match, "missing job permissions");
|
||||
return match[1].trim().split("\n").map((line) => line.trim()).sort();
|
||||
}
|
||||
|
||||
describe("release workflow contract", () => {
|
||||
it("has only the version-tag production trigger", () => {
|
||||
const trigger = topLevelBlock(releaseWorkflow, "on");
|
||||
|
||||
assert.match(trigger, /^on:\n push:\n tags:\n - 'v\*'\n+$/);
|
||||
for (const forbidden of [
|
||||
"workflow_dispatch:",
|
||||
"workflow_run:",
|
||||
"pull_request:",
|
||||
"pull_request_target:",
|
||||
]) {
|
||||
assert.equal(releaseWorkflow.includes(forbidden), false, forbidden);
|
||||
}
|
||||
});
|
||||
|
||||
it("runs preflight before every release side effect", () => {
|
||||
const preflight = jobBlock(releaseWorkflow, "preflight");
|
||||
|
||||
assert.deepEqual(permissionLines(preflight), ["contents: read"]);
|
||||
assertInOrder(preflight, [
|
||||
"actions/checkout@",
|
||||
"fetch-depth: 0",
|
||||
"actions/setup-node@",
|
||||
"node-version: '22.14.0'",
|
||||
"node scripts/release-preflight.js --tag \"$TAG\"",
|
||||
"git rev-parse --verify 'HEAD^{commit}'",
|
||||
"git rev-parse --verify \"refs/tags/${TAG}^{commit}\"",
|
||||
'if [[ "$TAG" == *-beta.* ]]',
|
||||
'git fetch origin "$REHEARSAL_BRANCH"',
|
||||
"git rev-parse --verify 'FETCH_HEAD^{commit}'",
|
||||
"git fetch origin main",
|
||||
'git merge-base --is-ancestor "$HEAD_SHA" "$MAIN_SHA"',
|
||||
]);
|
||||
assert.equal(preflight.includes("gh release"), false);
|
||||
assert.equal(preflight.includes("npm publish"), false);
|
||||
});
|
||||
|
||||
it("builds a verified staging asset before approval", () => {
|
||||
const build = jobBlock(releaseWorkflow, "build-stage-assets");
|
||||
|
||||
assert.match(build, /needs: preflight/);
|
||||
assert.deepEqual(permissionLines(build), ["contents: read"]);
|
||||
assert.doesNotMatch(build, /^ environment:/m);
|
||||
assert.match(build, /actions\/setup-go@[0-9a-f]{40}/);
|
||||
assert.match(build, /actions\/setup-python@[0-9a-f]{40}/);
|
||||
assert.match(
|
||||
build,
|
||||
/actions\/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6/,
|
||||
);
|
||||
assert.match(build, /node-version: '22.14.0'/);
|
||||
assert.match(build, /registry-url: 'https:\/\/registry\.npmjs\.org'/);
|
||||
assert.match(build, /package-manager-cache: false/);
|
||||
assert.match(build, /npm install --global npm@11\.16\.0/);
|
||||
assert.match(build, /goreleaser\/goreleaser-action@[0-9a-f]{40}/);
|
||||
assert.match(build, /args: release --clean --skip=publish/);
|
||||
assertInOrder(build, [
|
||||
"actions/setup-go@",
|
||||
"actions/setup-python@",
|
||||
"actions/setup-node@",
|
||||
"npm install --global npm@11.16.0",
|
||||
"goreleaser/goreleaser-action@",
|
||||
"sha256sum --check checksums.txt",
|
||||
"cp dist/checksums.txt checksums.txt",
|
||||
"npm pack --ignore-scripts --json",
|
||||
"tar -tzf \"$PACK_FILE\" | grep -qx 'package/checksums.txt'",
|
||||
"actions/upload-artifact@",
|
||||
]);
|
||||
assert.equal(build.includes("npm stage publish"), false);
|
||||
});
|
||||
|
||||
it("limits the protected job to verifying and staging the prepared npm asset", () => {
|
||||
const publish = jobBlock(releaseWorkflow, "stage-publish");
|
||||
|
||||
assert.match(publish, /needs: build-stage-assets/);
|
||||
assert.deepEqual(permissionLines(publish), ["id-token: write"]);
|
||||
assert.match(publish, /^ environment: npm-production$/m);
|
||||
assert.doesNotMatch(publish, /actions\/checkout@/);
|
||||
assert.doesNotMatch(publish, /goreleaser\/goreleaser-action@/);
|
||||
assert.doesNotMatch(publish, /GITHUB_TOKEN:/);
|
||||
assertInOrder(publish, [
|
||||
"actions/setup-node@",
|
||||
"npm install --global npm@11.16.0",
|
||||
"actions/download-artifact@",
|
||||
"sha256sum --check checksums.txt",
|
||||
"tar -tzf \"$PACK_FILE\" | grep -qx 'package/checksums.txt'",
|
||||
'npm stage publish "${{ steps.asset.outputs.filename }}" --access public --tag beta',
|
||||
]);
|
||||
for (const forbidden of [
|
||||
"gh release download",
|
||||
"npm view",
|
||||
"LOCAL_INTEGRITY",
|
||||
"REMOTE_INTEGRITY",
|
||||
"secrets.NPM_TOKEN",
|
||||
"NODE_AUTH_TOKEN",
|
||||
"GITHUB_TOKEN:",
|
||||
"gh release create",
|
||||
]) {
|
||||
assert.equal(releaseWorkflow.includes(forbidden), false, forbidden);
|
||||
}
|
||||
assert.equal(/(^|\s)npm publish(?:\s|$)/m.test(publish), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("preview isolation", () => {
|
||||
it("keeps preview publishing away from production credentials and registry", () => {
|
||||
assert.equal(previewWorkflow.includes("id-token: write"), false);
|
||||
assert.equal(previewWorkflow.includes("npm publish"), false);
|
||||
assert.equal(previewWorkflow.includes("registry.npmjs.org"), false);
|
||||
assert.equal(previewWorkflow.includes("secrets.NPM_TOKEN"), false);
|
||||
assert.equal(previewWorkflow.includes("NODE_AUTH_TOKEN"), false);
|
||||
});
|
||||
});
|
||||
@@ -3,49 +3,102 @@ set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
||||
cd "${REPO_ROOT}"
|
||||
|
||||
# Read version from package.json
|
||||
VERSION=$(node -p "require('${REPO_ROOT}/package.json').version")
|
||||
VERSION=$(node -p "require('./package.json').version")
|
||||
TAG="v${VERSION}"
|
||||
REHEARSAL_BRANCH="test/npm-staged-publish-rehearsal"
|
||||
PUSH_TAG=false
|
||||
|
||||
if [ -z "$VERSION" ]; then
|
||||
echo "Error: could not read version from package.json" >&2
|
||||
if [ "$#" -eq 1 ] && [ "$1" = "--push" ]; then
|
||||
PUSH_TAG=true
|
||||
elif [ "$#" -ne 0 ]; then
|
||||
echo "Usage: $0 [--push]" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
TAG="v${VERSION}"
|
||||
node "${SCRIPT_DIR}/release-preflight.js" --tag "${TAG}"
|
||||
|
||||
if [[ ! "${VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+-beta\.[0-9]+$ ]]; then
|
||||
echo "Error: rehearsal releases require an X.Y.Z-beta.N version." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Version: ${VERSION}"
|
||||
echo "Tag: ${TAG}"
|
||||
|
||||
# Check if tag already exists locally
|
||||
if git rev-parse "$TAG" >/dev/null 2>&1; then
|
||||
echo "Tag ${TAG} already exists locally, skipping."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Check if tag already exists on remote
|
||||
if git ls-remote --tags origin "$TAG" | grep -q "$TAG"; then
|
||||
echo "Tag ${TAG} already exists on remote, skipping."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Ensure package.json changes are committed before tagging
|
||||
if git diff --name-only | grep -q 'package.json' || git diff --cached --name-only | grep -q 'package.json'; then
|
||||
echo "Error: package.json has uncommitted changes. Please commit before tagging." >&2
|
||||
CURRENT_BRANCH=$(git branch --show-current)
|
||||
if [ "${CURRENT_BRANCH}" != "${REHEARSAL_BRANCH}" ]; then
|
||||
echo "Error: rehearsal tags must be created from ${REHEARSAL_BRANCH}; current branch is '${CURRENT_BRANCH}'." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Ensure current branch is pushed to remote before tagging
|
||||
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
||||
LOCAL_SHA=$(git rev-parse HEAD)
|
||||
REMOTE_SHA=$(git rev-parse "origin/${CURRENT_BRANCH}" 2>/dev/null || echo "")
|
||||
if [ "$LOCAL_SHA" != "$REMOTE_SHA" ]; then
|
||||
echo "Error: local branch '${CURRENT_BRANCH}' is not in sync with remote. Please push your commits first." >&2
|
||||
if [ -n "$(git status --porcelain)" ]; then
|
||||
echo "Error: the working tree must be clean before tagging." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create and push tag
|
||||
git tag "$TAG"
|
||||
git push origin "$TAG"
|
||||
git fetch origin "${REHEARSAL_BRANCH}"
|
||||
|
||||
echo "Successfully created and pushed tag ${TAG}"
|
||||
HEAD_SHA=$(git rev-parse HEAD)
|
||||
FETCHED_REHEARSAL_SHA=$(git rev-parse "FETCH_HEAD^{commit}")
|
||||
if [ "${HEAD_SHA}" != "${FETCHED_REHEARSAL_SHA}" ]; then
|
||||
echo "Error: HEAD must exactly match origin/${REHEARSAL_BRANCH} before tagging." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
WORKFLOW=$(git show "${HEAD_SHA}:.github/workflows/release.yml")
|
||||
if ! grep -Fq 'args: release --clean --skip=publish' <<<"${WORKFLOW}" ||
|
||||
! grep -Eq 'npm stage publish .*--tag beta' <<<"${WORKFLOW}" ||
|
||||
grep -Eq '(^|[[:space:]])npm publish([[:space:]]|$)' <<<"${WORKFLOW}" ||
|
||||
grep -Eq 'gh[[:space:]]+release([[:space:]]|$)' <<<"${WORKFLOW}" ||
|
||||
grep -Eq 'contents:[[:space:]]*write' <<<"${WORKFLOW}" ||
|
||||
grep -Fq 'GITHUB_TOKEN:' <<<"${WORKFLOW}"; then
|
||||
echo "Error: the tagged workflow must be stage-only, read-only for repository contents, and must not create a GitHub Release or publish npm live." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
set +e
|
||||
NPM_VIEW_OUTPUT=$(npm view "@larksuite/cli@${VERSION}" version --registry=https://registry.npmjs.org/ 2>&1)
|
||||
NPM_VIEW_STATUS=$?
|
||||
set -e
|
||||
if [ "${NPM_VIEW_STATUS}" -eq 0 ]; then
|
||||
echo "Error: @larksuite/cli@${VERSION} already exists on npm." >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! grep -Eq 'E404|404 Not Found' <<<"${NPM_VIEW_OUTPUT}"; then
|
||||
echo "Error: npm version lookup failed; refusing to assume the version is unused." >&2
|
||||
echo "${NPM_VIEW_OUTPUT}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if git rev-parse -q --verify "refs/tags/${TAG}" >/dev/null; then
|
||||
echo "Error: local tag ${TAG} already exists." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
REMOTE_TAG=$(git ls-remote --tags origin "refs/tags/${TAG}")
|
||||
if [ -n "${REMOTE_TAG}" ]; then
|
||||
echo "Error: remote tag ${TAG} already exists." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "${PUSH_TAG}" != true ]; then
|
||||
echo "Checks passed. No tag was created or pushed."
|
||||
echo "Run '$0 --push' only after reviewing the commit and workflow."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "Branch: ${CURRENT_BRANCH}"
|
||||
echo "Commit: ${HEAD_SHA}"
|
||||
printf 'Type %s to create and push this tag: ' "${TAG}"
|
||||
read -r CONFIRM_TAG
|
||||
if [ "${CONFIRM_TAG}" != "${TAG}" ]; then
|
||||
echo "Error: confirmation did not exactly match ${TAG}." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
git tag "${TAG}" "${HEAD_SHA}"
|
||||
git push origin "refs/tags/${TAG}:refs/tags/${TAG}"
|
||||
|
||||
echo "Successfully pushed tag ${TAG}"
|
||||
|
||||
@@ -81,16 +81,19 @@ func TestSlashCommandDelete_ByNameDryRun(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("execute: %v", err)
|
||||
}
|
||||
var got struct {
|
||||
Description string `json:"description"`
|
||||
API []struct {
|
||||
Desc string `json:"desc"`
|
||||
Method string `json:"method"`
|
||||
} `json:"api"`
|
||||
var envlp struct {
|
||||
Data struct {
|
||||
Description string `json:"description"`
|
||||
API []struct {
|
||||
Desc string `json:"desc"`
|
||||
Method string `json:"method"`
|
||||
} `json:"api"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(stdout.Bytes(), &got); err != nil {
|
||||
if err := json.Unmarshal(stdout.Bytes(), &envlp); err != nil {
|
||||
t.Fatalf("json: %v", err)
|
||||
}
|
||||
got := envlp.Data
|
||||
if !strings.Contains(got.Description, "HIGH-RISK") || strings.Contains(got.Description, "resolve command_id") {
|
||||
t.Fatalf("top-level description must contain only the risk context: %q", got.Description)
|
||||
}
|
||||
|
||||
@@ -100,16 +100,19 @@ func TestSlashCommandUpdate_ByNameDryRunDescriptions(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("execute: %v", err)
|
||||
}
|
||||
var got struct {
|
||||
Description string `json:"description"`
|
||||
API []struct {
|
||||
Desc string `json:"desc"`
|
||||
Method string `json:"method"`
|
||||
} `json:"api"`
|
||||
var envlp struct {
|
||||
Data struct {
|
||||
Description string `json:"description"`
|
||||
API []struct {
|
||||
Desc string `json:"desc"`
|
||||
Method string `json:"method"`
|
||||
} `json:"api"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(stdout.Bytes(), &got); err != nil {
|
||||
if err := json.Unmarshal(stdout.Bytes(), &envlp); err != nil {
|
||||
t.Fatalf("json: %v", err)
|
||||
}
|
||||
got := envlp.Data
|
||||
if strings.Contains(got.Description, "resolve command_id") {
|
||||
t.Fatalf("resolve description must be attached to GET, not top-level: %q", got.Description)
|
||||
}
|
||||
@@ -128,15 +131,18 @@ func TestSlashCommandUpdate_ByIDDryRunEncodesTrimmedPathSegment(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("execute: %v", err)
|
||||
}
|
||||
var got struct {
|
||||
API []struct {
|
||||
Desc string `json:"desc"`
|
||||
URL string `json:"url"`
|
||||
} `json:"api"`
|
||||
var envlp struct {
|
||||
Data struct {
|
||||
API []struct {
|
||||
Desc string `json:"desc"`
|
||||
URL string `json:"url"`
|
||||
} `json:"api"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(stdout.Bytes(), &got); err != nil {
|
||||
if err := json.Unmarshal(stdout.Bytes(), &envlp); err != nil {
|
||||
t.Fatalf("json: %v", err)
|
||||
}
|
||||
got := envlp.Data
|
||||
wantURL := slashCommandBasePath + "/id%2Fwith%20space%3Fx"
|
||||
if len(got.API) != 1 || got.API[0].URL != wantURL || got.API[0].Desc == "" {
|
||||
t.Fatalf("dry-run call = %#v, want encoded URL %q with description", got.API, wantURL)
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestAppsAccessScopeGet_Specific(t *testing.T) {
|
||||
"data": map[string]interface{}{
|
||||
"scope": "Range",
|
||||
"users": []interface{}{"ou_x", "ou_y"},
|
||||
"departments": []interface{}{"od_z"},
|
||||
"departments": []interface{}{"od-z"},
|
||||
"chats": []interface{}{"oc_g"},
|
||||
"apply_config": map[string]interface{}{
|
||||
"enabled": true,
|
||||
@@ -39,7 +39,7 @@ func TestAppsAccessScopeGet_Specific(t *testing.T) {
|
||||
if !strings.Contains(got, `"scope": "Range"`) {
|
||||
t.Fatalf("scope string not preserved (expect raw \"Range\"): %s", got)
|
||||
}
|
||||
if !strings.Contains(got, `"ou_x"`) || !strings.Contains(got, `"od_z"`) || !strings.Contains(got, `"oc_g"`) {
|
||||
if !strings.Contains(got, `"ou_x"`) || !strings.Contains(got, `"od-z"`) || !strings.Contains(got, `"oc_g"`) {
|
||||
t.Fatalf("users/departments/chats fields missing in envelope: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, `"ou_appr"`) {
|
||||
|
||||
@@ -23,19 +23,21 @@ func TestAppsAnalyticsList_DryRunUsesNanoseconds(t *testing.T) {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
Data struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
|
||||
t.Fatalf("decode dry-run: %v\n%s", err, stdout.String())
|
||||
}
|
||||
if env.API[0].Method != "POST" || env.API[0].URL != "/open-apis/spark/v1/apps/app_x/query_analytics_data" {
|
||||
t.Fatalf("method/url = %s %s", env.API[0].Method, env.API[0].URL)
|
||||
if env.Data.API[0].Method != "POST" || env.Data.API[0].URL != "/open-apis/spark/v1/apps/app_x/query_analytics_data" {
|
||||
t.Fatalf("method/url = %s %s", env.Data.API[0].Method, env.Data.API[0].URL)
|
||||
}
|
||||
body := env.API[0].Body
|
||||
body := env.Data.API[0].Body
|
||||
if _, ok := body["start_timestamp_ns"]; !ok {
|
||||
t.Fatalf("analytics dry-run missing start_timestamp_ns: %#v", body)
|
||||
}
|
||||
@@ -92,14 +94,16 @@ func TestAppsAnalyticsList_PageViewDesktopSeriesSetsDeviceFilter(t *testing.T) {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
Data struct {
|
||||
API []struct {
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
|
||||
t.Fatalf("decode dry-run: %v\n%s", err, stdout.String())
|
||||
}
|
||||
filter := env.API[0].Body["filter"].(map[string]interface{})
|
||||
filter := env.Data.API[0].Body["filter"].(map[string]interface{})
|
||||
deviceTypes := filter["device_types"].([]interface{})
|
||||
if len(deviceTypes) != 1 || deviceTypes[0] != "desktop" {
|
||||
t.Fatalf("device_types = %#v", deviceTypes)
|
||||
|
||||
253
shortcuts/apps/apps_automation_create.go
Normal file
253
shortcuts/apps/apps_automation_create.go
Normal file
@@ -0,0 +1,253 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/larksuite/cli/shortcuts/common"
|
||||
)
|
||||
|
||||
// AppsAutomationCreate creates an automation trigger (type-dispatched condition).
|
||||
var AppsAutomationCreate = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+automation-create",
|
||||
Description: "Create an automation trigger (cron/record-change/webhook/feishu-approval); created disabled",
|
||||
Risk: "write",
|
||||
Tips: []string{
|
||||
"Example: lark-cli apps +automation-create --app-id <id> --name daily --trigger-type cron --cron '0 9 * * *'",
|
||||
"Example: lark-cli apps +automation-create --app-id <id> --name onUpd --trigger-type record-change --table <tbl> --event UPDATE",
|
||||
"Example: lark-cli apps +automation-create --app-id <id> --name hook --trigger-type webhook",
|
||||
"Example: lark-cli apps +automation-create --app-id <id> --name apv --trigger-type feishu-approval --event-type approval_instance --instance-status APPROVED",
|
||||
},
|
||||
Scopes: []string{"spark:app:write"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: "Miaoda app id", Required: true},
|
||||
{Name: "name", Desc: "trigger name (unique within app, <=100 chars)", Required: true},
|
||||
{Name: "trigger-type", Desc: "cron | record-change | webhook | feishu-approval", Required: true},
|
||||
{Name: "description", Desc: "optional description (<=50 chars)"},
|
||||
{Name: "cron", Desc: "[cron] 5-field cron expression, e.g. '0 9 * * *' (min interval 30m)"},
|
||||
{Name: "timezone", Desc: "[cron] IANA timezone (default Asia/Shanghai)"},
|
||||
{Name: "table", Desc: "[record-change] table name (from `+db-table-list`); dataloom tables key by name, not id"},
|
||||
{Name: "event", Desc: "[record-change] INSERT | UPDATE | UPSERT | DELETE"},
|
||||
{Name: "fields", Desc: "[record-change] JSON array of field ids for UPDATE/UPSERT, [\"*\"] = all"},
|
||||
{Name: "white-ip-list", Desc: "[webhook] JSON array of allowed IPs"},
|
||||
{Name: "approval-code", Desc: "[feishu-approval] approval definition code; omit to match all approval definitions"},
|
||||
{Name: "event-type", Desc: "[feishu-approval] approval_instance | approval_task"},
|
||||
{Name: "instance-status", Type: "string_array", Desc: "[feishu-approval] statuses for approval_instance"},
|
||||
{Name: "task-status", Type: "string_array", Desc: "[feishu-approval] statuses for approval_task"},
|
||||
{Name: "status", Desc: "optional initial status: enabled | disabled (default disabled; backend supports create+enable in one call)"},
|
||||
},
|
||||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
if _, err := requireAppID(rctx.Str("app-id")); err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(rctx.Str("name")) == "" {
|
||||
return appsValidationParamError("--name", "--name is required")
|
||||
}
|
||||
cliType := strings.TrimSpace(rctx.Str("trigger-type"))
|
||||
if cliType == "" {
|
||||
return appsValidationParamError("--trigger-type", "--trigger-type is required (cron/record-change/webhook/feishu-approval)")
|
||||
}
|
||||
// mapTriggerType also runs inside buildAutomationCreateBody, but
|
||||
// re-running it up-front keeps the cross-family guard's error
|
||||
// reachable — otherwise an unknown --trigger-type would bail out
|
||||
// with the same guard's "belongs to trigger-type" wording, which
|
||||
// misleads callers who typoed the type itself.
|
||||
if _, err := mapTriggerType(cliType); err != nil {
|
||||
return err
|
||||
}
|
||||
// Reject condition flags that do not belong to the selected type.
|
||||
// buildAutomationCreateBody's switch used to silently drop them
|
||||
// (e.g. --trigger-type webhook --cron '0 9 * * *' created a webhook
|
||||
// with no cron, though the caller believed --cron was set).
|
||||
if err := rejectCrossFamilyCondFlags(rctx, cliType); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := buildAutomationCreateBody(rctx)
|
||||
return err
|
||||
},
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
appID, _ := requireAppID(rctx.Str("app-id"))
|
||||
body, _ := buildAutomationCreateBody(rctx)
|
||||
return common.NewDryRunAPI().
|
||||
POST(automationListPath(appID)).
|
||||
Desc("Create automation trigger").
|
||||
Body(body)
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
appID, err := requireAppID(rctx.Str("app-id"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
body, err := buildAutomationCreateBody(rctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := rctx.CallAPITyped("POST", automationListPath(appID), nil, body)
|
||||
if err != nil {
|
||||
return withAppsHint(err, appIDListHint)
|
||||
}
|
||||
// Bearer-token redaction reverse invariant: the backend create path
|
||||
// re-reads the freshly created trigger through the same read-path
|
||||
// converter used by get/list — theoretically capable of returning a
|
||||
// plaintext bearer token. On a fresh create the token is not yet
|
||||
// enabled and this response should not carry plaintext, but redact
|
||||
// for defense-in-depth and to keep every read-shaped output path
|
||||
// (create / get / list / update-patch) consistently scrubbed.
|
||||
redacted := redactWebhookToken(data)
|
||||
trigger, _ := redacted["trigger"].(map[string]interface{})
|
||||
rctx.OutFormat(redacted, nil, func(w io.Writer) {
|
||||
fmt.Fprintf(w, "created trigger: %v [%v] status: %v\n",
|
||||
trigger["name"], trigger["trigger_type"], trigger["status"])
|
||||
})
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// buildAutomationCreateBody assembles {name, description?, trigger_type, <type>_condition}.
|
||||
func buildAutomationCreateBody(rctx *common.RuntimeContext) (map[string]interface{}, error) {
|
||||
cliType := strings.TrimSpace(rctx.Str("trigger-type"))
|
||||
snake, err := mapTriggerType(cliType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
name := strings.TrimSpace(rctx.Str("name"))
|
||||
if err := validateAutomationNameLen(name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body := map[string]interface{}{
|
||||
"name": name,
|
||||
"trigger_type": snake,
|
||||
}
|
||||
if d := strings.TrimSpace(rctx.Str("description")); d != "" {
|
||||
if err := validateAutomationDescriptionLen(d); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body["description"] = d
|
||||
}
|
||||
// --status is an optional passthrough: when set, backend creates + enables
|
||||
// (or leaves disabled) in one call. Omitting the field lets the backend
|
||||
// default (disabled) apply, matching the spec's default-disabled invariant.
|
||||
if s := strings.TrimSpace(rctx.Str("status")); s != "" {
|
||||
if s != "enabled" && s != "disabled" {
|
||||
return nil, appsValidationParamError("--status",
|
||||
"--status must be enabled or disabled, got %q", s)
|
||||
}
|
||||
body["status"] = s
|
||||
}
|
||||
switch cliType {
|
||||
case "cron":
|
||||
cond, err := buildCronCondition(rctx.Str("cron"), rctx.Str("timezone"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body["cron_condition"] = cond
|
||||
case "record-change":
|
||||
fields, err := parseFieldsFlag(rctx.Str("fields"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cond, err := buildRecordChangeCondition(rctx.Str("table"), rctx.Str("event"), fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body["record_change_condition"] = cond
|
||||
case "webhook":
|
||||
ipList, err := parseIPListFlag(rctx.Str("white-ip-list"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body["webhook_condition"] = buildWebhookCondition(ipList)
|
||||
case "feishu-approval":
|
||||
eventType := strings.TrimSpace(rctx.Str("event-type"))
|
||||
if eventType == "" {
|
||||
return nil, appsValidationParamError("--event-type", "--event-type is required for feishu-approval (approval_instance/approval_task)")
|
||||
}
|
||||
raw := rctx.StrArray("instance-status")
|
||||
if eventType == "approval_task" {
|
||||
raw = rctx.StrArray("task-status")
|
||||
}
|
||||
// buildApprovalCondition stores the passed statuses verbatim (it only
|
||||
// uppercases for validation), so normalize to the uppercase enum here to
|
||||
// guarantee the backend receives canonical values (foundation review).
|
||||
statuses := normalizeApprovalStatuses(raw)
|
||||
cond, err := buildApprovalCondition(rctx.Str("approval-code"), eventType, statuses)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body["feishu_approval_condition"] = cond
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
|
||||
// normalizeApprovalStatuses trims and uppercases each status so the body carries
|
||||
// the canonical enum values expected by the backend.
|
||||
func normalizeApprovalStatuses(raw []string) []string {
|
||||
if len(raw) == 0 {
|
||||
return raw
|
||||
}
|
||||
out := make([]string, 0, len(raw))
|
||||
for _, s := range raw {
|
||||
out = append(out, strings.ToUpper(strings.TrimSpace(s)))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// parseFieldsFlag parses --fields JSON array; empty → nil.
|
||||
func parseFieldsFlag(raw string) ([]string, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return nil, nil
|
||||
}
|
||||
var arr []string
|
||||
if err := json.Unmarshal([]byte(raw), &arr); err != nil {
|
||||
return nil, appsValidationParamError("--fields", "--fields must be a JSON array of strings: %v", err)
|
||||
}
|
||||
return arr, nil
|
||||
}
|
||||
|
||||
// parseIPListFlag parses --white-ip-list JSON array; empty → nil (field
|
||||
// omitted). Each entry is validated as an IPv4/IPv6 address or CIDR, matching
|
||||
// the defense-in-depth stance the record-change --event whitelist takes —
|
||||
// silent acceptance of malformed IPs would let a typoed entry (`"1.1.1.1 "`
|
||||
// with trailing space, `"not-an-ip"`, or `"10.0.0.256"`) narrow the webhook
|
||||
// caller allowlist to nothing while the operator believes it is enforcing
|
||||
// origin restrictions.
|
||||
func parseIPListFlag(raw string) ([]string, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return nil, nil
|
||||
}
|
||||
var arr []string
|
||||
if err := json.Unmarshal([]byte(raw), &arr); err != nil {
|
||||
return nil, appsValidationParamError("--white-ip-list", "--white-ip-list must be a JSON array of strings: %v", err)
|
||||
}
|
||||
out := make([]string, 0, len(arr))
|
||||
for i, entry := range arr {
|
||||
trimmed := strings.TrimSpace(entry)
|
||||
if trimmed == "" {
|
||||
return nil, appsValidationParamError("--white-ip-list",
|
||||
"--white-ip-list entry %d is empty; either drop it or provide a valid IP/CIDR", i)
|
||||
}
|
||||
if net.ParseIP(trimmed) != nil {
|
||||
out = append(out, trimmed)
|
||||
continue
|
||||
}
|
||||
if _, _, cidrErr := net.ParseCIDR(trimmed); cidrErr == nil {
|
||||
out = append(out, trimmed)
|
||||
continue
|
||||
}
|
||||
return nil, appsValidationParamError("--white-ip-list",
|
||||
"--white-ip-list entry %d %q is not a valid IPv4/IPv6 address or CIDR block", i, entry)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
265
shortcuts/apps/apps_automation_create_test.go
Normal file
265
shortcuts/apps/apps_automation_create_test.go
Normal file
@@ -0,0 +1,265 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/larksuite/cli/internal/httpmock"
|
||||
)
|
||||
|
||||
func automationCreateFlagDefs() map[string]string {
|
||||
return map[string]string{
|
||||
"app-id": "string", "name": "string", "trigger-type": "string", "description": "string",
|
||||
"cron": "string", "timezone": "string",
|
||||
"table": "string", "event": "string", "fields": "string",
|
||||
"white-ip-list": "string",
|
||||
"approval-code": "string", "event-type": "string",
|
||||
"instance-status": "string_array", "task-status": "string_array",
|
||||
"status": "string",
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutomationCreateCron_BuildsBody(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "daily", "trigger-type": "cron", "cron": "0 9 * * *"})
|
||||
// Real backend response wraps the created trigger under `trigger` (a live
|
||||
// test-env probe confirmed the shape, same as GET/PUT). The Execute pretty
|
||||
// path reads trigger["name"]/["trigger_type"]/["status"] from that key —
|
||||
// a flat fixture makes the pretty path print `<nil>` and only passes via
|
||||
// the JSON envelope, which hides regressions in the pretty branch.
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "POST", URL: "/open-apis/spark/v1/apps/app_x/triggers",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{
|
||||
"trigger": map[string]interface{}{
|
||||
"name": "daily", "trigger_type": "cron", "status": "disabled",
|
||||
},
|
||||
}},
|
||||
})
|
||||
if err := AppsAutomationCreate.Execute(context.Background(), rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
if !strings.Contains(stdoutBuf.String(), "daily") {
|
||||
t.Errorf("create output must contain trigger name: %s", stdoutBuf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutomationCreate_MissingType(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "n"})
|
||||
err := AppsAutomationCreate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--trigger-type")
|
||||
}
|
||||
|
||||
// TestAutomationCreate_CrossFamilyFlagsRejected pins the F1 guard: a condition
|
||||
// flag from a family other than --trigger-type used to be silently dropped by
|
||||
// buildAutomationCreateBody's single-branch switch, so
|
||||
// `--trigger-type webhook --cron '0 9 * * *'` created a webhook with no cron
|
||||
// but returned success. Validate now rejects the cross-family flag up-front.
|
||||
func TestAutomationCreate_CrossFamilyFlagsRejected(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
flags map[string]string
|
||||
wantParam string
|
||||
}{
|
||||
{"webhook_with_cron",
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "n", "trigger-type": "webhook",
|
||||
"cron": "0 9 * * *",
|
||||
}, "--cron"},
|
||||
{"cron_with_white_ip_list",
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "n", "trigger-type": "cron",
|
||||
"cron": "0 9 * * *", "white-ip-list": `["1.1.1.1"]`,
|
||||
}, "--white-ip-list"},
|
||||
{"record_change_with_event_type",
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "n", "trigger-type": "record-change",
|
||||
"table": "tbl", "event": "UPDATE", "event-type": "approval_instance",
|
||||
}, "--event-type"},
|
||||
{"feishu_approval_with_table",
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "n", "trigger-type": "feishu-approval",
|
||||
"event-type": "approval_instance", "instance-status": "APPROVED",
|
||||
"table": "tbl",
|
||||
}, "--table"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(), tc.flags)
|
||||
err := AppsAutomationCreate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, tc.wantParam)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationCreate_UnknownTriggerTypeRejected: --trigger-type must be one
|
||||
// of the four supported kebab-case values. A typo used to sneak past Validate
|
||||
// (buildAutomationCreateBody caught it, but only after the cross-family guard
|
||||
// would otherwise fire with a misleading "belongs to type" message).
|
||||
func TestAutomationCreate_UnknownTriggerTypeRejected(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "n", "trigger-type": "bogus"})
|
||||
err := AppsAutomationCreate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--trigger-type")
|
||||
}
|
||||
|
||||
func TestAutomationCreateCron_Sub30MinRejected(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "n", "trigger-type": "cron", "cron": "*/5 * * * *"})
|
||||
err := AppsAutomationCreate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--cron")
|
||||
}
|
||||
|
||||
func TestAutomationCreateRecordChange_MissingEvent(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "n", "trigger-type": "record-change", "table": "tbl"})
|
||||
err := AppsAutomationCreate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--event")
|
||||
}
|
||||
|
||||
func TestAutomationCreateApproval_CodeOptional(t *testing.T) {
|
||||
rctx, _, reg := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "n", "trigger-type": "feishu-approval",
|
||||
"event-type": "approval_instance", "instance-status": "APPROVED"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "POST", URL: "/open-apis/spark/v1/apps/app_x/triggers",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"name": "n", "status": "disabled"}},
|
||||
})
|
||||
if err := AppsAutomationCreate.Validate(context.Background(), rctx); err != nil {
|
||||
t.Fatalf("approval without --approval-code must pass validation: %v", err)
|
||||
}
|
||||
if err := AppsAutomationCreate.Execute(context.Background(), rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationCreateApproval_StatusUppercased asserts that a lowercase status
|
||||
// passed via --instance-status is normalized to the uppercase enum in the body
|
||||
// before it reaches the backend (foundation review: buildApprovalCondition stores
|
||||
// the raw statuses, so create must uppercase them itself).
|
||||
func TestAutomationCreateApproval_StatusUppercased(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "n", "trigger-type": "feishu-approval",
|
||||
"event-type": "approval_instance", "instance-status": "approved"})
|
||||
body, err := buildAutomationCreateBody(rctx)
|
||||
if err != nil {
|
||||
t.Fatalf("buildAutomationCreateBody() = %v", err)
|
||||
}
|
||||
cond, ok := body["feishu_approval_condition"].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("feishu_approval_condition missing or wrong type: %+v", body)
|
||||
}
|
||||
statuses, ok := cond["status"].([]string)
|
||||
if !ok {
|
||||
t.Fatalf("status must be []string: %+v", cond)
|
||||
}
|
||||
if len(statuses) != 1 || statuses[0] != "APPROVED" {
|
||||
t.Errorf("lowercase status must be uppercased to APPROVED, got %v", statuses)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationCreate_RedactsWebhookToken covers the bearer-token redaction
|
||||
// reverse invariant on the create path against the real response shape (a
|
||||
// live test-env probe confirmed POST wraps the trigger under a `trigger`
|
||||
// key, same as GET/PUT). The backend create path re-reads the freshly
|
||||
// created trigger and returns it through the same read-path converter used
|
||||
// by get/list — theoretically capable of returning a plaintext bearer
|
||||
// token. Defense-in-depth: CLI create must also redact so every read-shaped
|
||||
// output path is consistently scrubbed.
|
||||
func TestAutomationCreate_RedactsWebhookToken(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "trigger-type": "webhook"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "POST", URL: "/open-apis/spark/v1/apps/app_x/triggers",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{
|
||||
"trigger": map[string]interface{}{
|
||||
"name": "wh1", "trigger_type": "webhook", "status": "disabled",
|
||||
"trigger_condition": map[string]interface{}{
|
||||
"preview_url": "https://p", "runtime_url": "https://r",
|
||||
"token_enabled": true, "token_value": "PLAINTEXT_CREATE_TOKEN",
|
||||
},
|
||||
},
|
||||
}},
|
||||
})
|
||||
if err := AppsAutomationCreate.Execute(context.Background(), rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
out := stdoutBuf.String()
|
||||
if strings.Contains(out, "PLAINTEXT_CREATE_TOKEN") {
|
||||
t.Errorf("create must never surface plaintext token: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationCreate_StatusPassthrough verifies --status is included in the
|
||||
// POST body when set. Backend supports create+enable in one call via the
|
||||
// optional status field; CLI passes it through unchanged.
|
||||
func TestAutomationCreate_StatusPassthrough(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "n", "trigger-type": "cron",
|
||||
"cron": "0 9 * * *", "status": "enabled",
|
||||
})
|
||||
body, err := buildAutomationCreateBody(rctx)
|
||||
if err != nil {
|
||||
t.Fatalf("buildBody: %v", err)
|
||||
}
|
||||
if body["status"] != "enabled" {
|
||||
t.Errorf("status = %v; want enabled", body["status"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationCreate_StatusInvalid: only enabled/disabled accepted.
|
||||
func TestAutomationCreate_StatusInvalid(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "n", "trigger-type": "cron",
|
||||
"cron": "0 9 * * *", "status": "bogus",
|
||||
})
|
||||
_, err := buildAutomationCreateBody(rctx)
|
||||
assertValidationParamError(t, err, "--status")
|
||||
}
|
||||
|
||||
// TestAutomationCreate_StatusOmitted: when --status is not set, body must not
|
||||
// carry a status field — backend applies its default (disabled).
|
||||
func TestAutomationCreate_StatusOmitted(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "n", "trigger-type": "cron",
|
||||
"cron": "0 9 * * *",
|
||||
})
|
||||
body, err := buildAutomationCreateBody(rctx)
|
||||
if err != nil {
|
||||
t.Fatalf("buildBody: %v", err)
|
||||
}
|
||||
if _, present := body["status"]; present {
|
||||
t.Errorf("status must be omitted when --status not set, got %v", body["status"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationCreate_NameTooLong: --name > 100 chars is rejected locally with
|
||||
// a typed --name error, sparing the round trip to the backend.
|
||||
func TestAutomationCreate_NameTooLong(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": strings.Repeat("n", automationNameMaxLen+1),
|
||||
"trigger-type": "cron", "cron": "0 9 * * *",
|
||||
})
|
||||
_, err := buildAutomationCreateBody(rctx)
|
||||
assertValidationParamError(t, err, "--name")
|
||||
}
|
||||
|
||||
// TestAutomationCreate_DescriptionTooLong: --description > 50 chars is rejected
|
||||
// locally with a typed --description error.
|
||||
func TestAutomationCreate_DescriptionTooLong(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationCreateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "n", "trigger-type": "cron",
|
||||
"cron": "0 9 * * *", "description": strings.Repeat("d", automationDescriptionMaxLen+1),
|
||||
})
|
||||
_, err := buildAutomationCreateBody(rctx)
|
||||
assertValidationParamError(t, err, "--description")
|
||||
}
|
||||
38
shortcuts/apps/apps_automation_disable.go
Normal file
38
shortcuts/apps/apps_automation_disable.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/larksuite/cli/shortcuts/common"
|
||||
)
|
||||
|
||||
// AppsAutomationDisable disables a trigger. Maps to the shared status endpoint.
|
||||
var AppsAutomationDisable = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+automation-disable",
|
||||
Description: "Disable an automation trigger (stops auto-firing; does not delete)",
|
||||
Risk: "write",
|
||||
Tips: []string{"Example: lark-cli apps +automation-disable --app-id <id> --name <trigger_name>"},
|
||||
Scopes: []string{"spark:app:write"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: "Miaoda app id", Required: true},
|
||||
{Name: "name", Desc: "trigger name", Required: true},
|
||||
},
|
||||
Validate: automationValidateName,
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
appID, _ := requireAppID(rctx.Str("app-id"))
|
||||
return common.NewDryRunAPI().
|
||||
PATCH(automationItemPath(appID, strings.TrimSpace(rctx.Str("name")))).
|
||||
Desc("Disable automation trigger").
|
||||
Body(statusBodyFromAction(false))
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
return runAutomationStatus(rctx, false)
|
||||
},
|
||||
}
|
||||
70
shortcuts/apps/apps_automation_enable.go
Normal file
70
shortcuts/apps/apps_automation_enable.go
Normal file
@@ -0,0 +1,70 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/larksuite/cli/shortcuts/common"
|
||||
)
|
||||
|
||||
// AppsAutomationEnable enables (activates) a trigger. Maps to the shared status endpoint.
|
||||
var AppsAutomationEnable = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+automation-enable",
|
||||
Description: "Enable (activate) an automation trigger",
|
||||
Risk: "write",
|
||||
Tips: []string{"Example: lark-cli apps +automation-enable --app-id <id> --name <trigger_name>"},
|
||||
Scopes: []string{"spark:app:write"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: "Miaoda app id", Required: true},
|
||||
{Name: "name", Desc: "trigger name", Required: true},
|
||||
},
|
||||
Validate: automationValidateName,
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
appID, _ := requireAppID(rctx.Str("app-id"))
|
||||
return common.NewDryRunAPI().
|
||||
PATCH(automationItemPath(appID, strings.TrimSpace(rctx.Str("name")))).
|
||||
Desc("Enable automation trigger").
|
||||
Body(statusBodyFromAction(true))
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
return runAutomationStatus(rctx, true)
|
||||
},
|
||||
}
|
||||
|
||||
// runAutomationStatus is shared by enable/disable: PATCH .../triggers/{name}
|
||||
// with {"status": ...}. The status change happens on the parent resource per
|
||||
// the backend OpenAPI spec (see reference Python samples in the trigger test
|
||||
// fixtures) — there is intentionally no /status sub-path; the sole nested
|
||||
// endpoints under a trigger are the webhook credential lifecycle
|
||||
// (/webhook/token/status, /webhook/token/reset, /webhook/url/reset).
|
||||
//
|
||||
// The status endpoint returns {"success": true} on success. Pretty output is
|
||||
// synthesized from rctx.name and the desired action, since the response
|
||||
// intentionally carries no trigger object to fish name/status from.
|
||||
func runAutomationStatus(rctx *common.RuntimeContext, enable bool) error {
|
||||
appID, err := requireAppID(rctx.Str("app-id"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name := strings.TrimSpace(rctx.Str("name"))
|
||||
data, err := rctx.CallAPITyped("PATCH", automationItemPath(appID, name), nil, statusBodyFromAction(enable))
|
||||
if err != nil {
|
||||
return withAppsHint(err, automationNotFoundHint())
|
||||
}
|
||||
desiredStatus := "disabled"
|
||||
if enable {
|
||||
desiredStatus = "enabled"
|
||||
}
|
||||
rctx.OutFormat(data, nil, func(w io.Writer) {
|
||||
fmt.Fprintf(w, "trigger %s status: %s\n", name, desiredStatus)
|
||||
})
|
||||
return nil
|
||||
}
|
||||
73
shortcuts/apps/apps_automation_get.go
Normal file
73
shortcuts/apps/apps_automation_get.go
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/larksuite/cli/shortcuts/common"
|
||||
)
|
||||
|
||||
// AppsAutomationGet gets a single trigger's full config (webhook token redacted).
|
||||
var AppsAutomationGet = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+automation-get",
|
||||
Description: "Get an automation trigger's config (webhook Bearer Token redacted)",
|
||||
Risk: "read",
|
||||
Tips: []string{
|
||||
"Example: lark-cli apps +automation-get --app-id <app_id> --name <trigger_name>",
|
||||
},
|
||||
Scopes: []string{"spark:app:read"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: "Miaoda app id", Required: true},
|
||||
{Name: "name", Desc: "trigger name", Required: true},
|
||||
},
|
||||
Validate: automationValidateName,
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
appID, _ := requireAppID(rctx.Str("app-id"))
|
||||
return common.NewDryRunAPI().
|
||||
GET(automationItemPath(appID, strings.TrimSpace(rctx.Str("name")))).
|
||||
Desc("Get automation trigger")
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
appID, err := requireAppID(rctx.Str("app-id"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name := strings.TrimSpace(rctx.Str("name"))
|
||||
data, err := rctx.CallAPITyped("GET", automationItemPath(appID, name), nil, nil)
|
||||
if err != nil {
|
||||
return withAppsHint(err, automationNotFoundHint())
|
||||
}
|
||||
redacted := redactWebhookToken(data)
|
||||
trigger, _ := redacted["trigger"].(map[string]interface{})
|
||||
rctx.OutFormat(redacted, nil, func(w io.Writer) {
|
||||
fmt.Fprintf(w, "name: %v\ntype: %v\nstatus: %v\n",
|
||||
trigger["name"], trigger["trigger_type"], trigger["status"])
|
||||
})
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// automationValidateName validates --app-id and --name presence. Shared by get/update/enable/disable.
|
||||
func automationValidateName(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
if _, err := requireAppID(rctx.Str("app-id")); err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(rctx.Str("name")) == "" {
|
||||
return appsValidationParamError("--name", "--name is required").
|
||||
WithHint("find trigger names with `lark-cli apps +automation-list --app-id <app_id>`")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// automationNotFoundHint is the shared recovery hint when a trigger name may not exist.
|
||||
func automationNotFoundHint() string {
|
||||
return "verify the trigger name with `lark-cli apps +automation-list --app-id <app_id>`"
|
||||
}
|
||||
117
shortcuts/apps/apps_automation_get_test.go
Normal file
117
shortcuts/apps/apps_automation_get_test.go
Normal file
@@ -0,0 +1,117 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/internal/httpmock"
|
||||
)
|
||||
|
||||
// TestAutomationGetExecute_RedactsWebhookToken pins the redaction invariant
|
||||
// against the actual backend response shape (verified against a live test
|
||||
// environment): GET wraps the trigger under a `trigger` key, so the CLI
|
||||
// must scrub token_value inside data.trigger.trigger_condition. A previous
|
||||
// implementation only scrubbed data.trigger_condition and silently no-op'd
|
||||
// here — this test would fail the moment someone reverts to top-level-only
|
||||
// scrubbing.
|
||||
func TestAutomationGetExecute_RedactsWebhookToken(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t,
|
||||
map[string]string{"app-id": "string", "name": "string"},
|
||||
map[string]string{"app-id": "app_x", "name": "wh1"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "GET", URL: "/open-apis/spark/v1/apps/app_x/triggers/wh1",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{
|
||||
"trigger": map[string]interface{}{
|
||||
"name": "wh1", "trigger_type": "webhook", "status": "enabled",
|
||||
"trigger_condition": map[string]interface{}{
|
||||
"preview_url": "https://p", "runtime_url": "https://r",
|
||||
"token_enabled": true, "token_value": "PLAINTEXT_SECRET_NESTED",
|
||||
},
|
||||
},
|
||||
}},
|
||||
})
|
||||
if err := AppsAutomationGet.Execute(context.Background(), rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
out := stdoutBuf.String()
|
||||
if strings.Contains(out, "PLAINTEXT_SECRET_NESTED") {
|
||||
t.Errorf("get must never surface plaintext token: %s", out)
|
||||
}
|
||||
if !strings.Contains(out, "token_enabled") {
|
||||
t.Errorf("get must expose token_enabled: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutomationGet_MissingName(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t,
|
||||
map[string]string{"app-id": "string", "name": "string"},
|
||||
map[string]string{"app-id": "app_x"})
|
||||
err := AppsAutomationGet.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--name")
|
||||
}
|
||||
|
||||
// TestAutomationGet_MissingAppID covers the sibling branch of Validate:
|
||||
// automationValidateName rejects an empty --app-id before checking --name.
|
||||
func TestAutomationGet_MissingAppID(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t,
|
||||
map[string]string{"app-id": "string", "name": "string"},
|
||||
map[string]string{"name": "t1"})
|
||||
err := AppsAutomationGet.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--app-id")
|
||||
}
|
||||
|
||||
// TestAutomationGet_APIErrorAttachesNotFoundHint covers the failure branch of
|
||||
// Execute: a business error on GET must surface typed and carry the
|
||||
// automation-list hint so the caller has a next step.
|
||||
func TestAutomationGet_APIErrorAttachesNotFoundHint(t *testing.T) {
|
||||
rctx, _, reg := newOpenAPIKeyRCtx(t,
|
||||
map[string]string{"app-id": "string", "name": "string"},
|
||||
map[string]string{"app-id": "app_x", "name": "missing"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "GET", URL: "/open-apis/spark/v1/apps/app_x/triggers/missing",
|
||||
Body: map[string]interface{}{"code": 400400001, "msg": "trigger not found"},
|
||||
})
|
||||
err := AppsAutomationGet.Execute(context.Background(), rctx)
|
||||
if err == nil {
|
||||
t.Fatal("expected typed api error, got nil")
|
||||
}
|
||||
p, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
t.Fatalf("expected typed problem, got %T: %v", err, err)
|
||||
}
|
||||
if p.Category != errs.CategoryAPI {
|
||||
t.Errorf("category = %q, want %q", p.Category, errs.CategoryAPI)
|
||||
}
|
||||
if p.Subtype == "" {
|
||||
t.Error("subtype must be populated on typed API errors")
|
||||
}
|
||||
if !strings.Contains(p.Hint, "+automation-list") {
|
||||
t.Errorf("hint must point at +automation-list, got %q", p.Hint)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationGet_DryRunPreview exercises the DryRun closure and pins the
|
||||
// GET method + URL pattern that agents inspect before committing.
|
||||
func TestAutomationGet_DryRunPreview(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t,
|
||||
map[string]string{"app-id": "string", "name": "string"},
|
||||
map[string]string{"app-id": "app_x", "name": "t1"})
|
||||
preview := AppsAutomationGet.DryRun(context.Background(), rctx)
|
||||
if preview == nil {
|
||||
t.Fatal("DryRun returned nil")
|
||||
}
|
||||
blob, err := preview.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Fatalf("marshal preview: %v", err)
|
||||
}
|
||||
got := string(blob)
|
||||
if !strings.Contains(got, `"method":"GET"`) ||
|
||||
!strings.Contains(got, "/apps/app_x/triggers/t1") {
|
||||
t.Errorf("preview missing expected GET/URL fields: %s", got)
|
||||
}
|
||||
}
|
||||
159
shortcuts/apps/apps_automation_list.go
Normal file
159
shortcuts/apps/apps_automation_list.go
Normal file
@@ -0,0 +1,159 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/shortcuts/common"
|
||||
)
|
||||
|
||||
// AppsAutomationList lists an app's automation triggers (all 4 types).
|
||||
var AppsAutomationList = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+automation-list",
|
||||
Description: "List a Miaoda app's automation triggers (cron/record-change/webhook/feishu-approval)",
|
||||
Risk: "read",
|
||||
Tips: []string{
|
||||
"Example: lark-cli apps +automation-list --app-id <app_id>",
|
||||
"Example: lark-cli apps +automation-list --app-id <app_id> --trigger-type webhook",
|
||||
"Example: lark-cli apps +automation-list --app-id <app_id> --all # aggregate all pages",
|
||||
},
|
||||
Scopes: []string{"spark:app:read"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: "Miaoda app id", Required: true},
|
||||
{Name: "trigger-type", Desc: "filter by type: cron | record-change | webhook | feishu-approval"},
|
||||
{Name: "page-size", Type: "int", Desc: "page size (server default 50, max 100)"},
|
||||
{Name: "page-token", Desc: "pagination cursor from previous response"},
|
||||
{Name: "all", Type: "bool", Desc: "auto-aggregate all pages until has_more=false"},
|
||||
},
|
||||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
if _, err := requireAppID(rctx.Str("app-id")); err != nil {
|
||||
return err
|
||||
}
|
||||
if tt := strings.TrimSpace(rctx.Str("trigger-type")); tt != "" {
|
||||
if _, err := mapTriggerType(tt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
appID, _ := requireAppID(rctx.Str("app-id"))
|
||||
return common.NewDryRunAPI().
|
||||
GET(automationListPath(appID)).
|
||||
Desc("List automation triggers").
|
||||
Params(buildAutomationListParams(rctx))
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
appID, err := requireAppID(rctx.Str("app-id"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
path := automationListPath(appID)
|
||||
params := buildAutomationListParams(rctx)
|
||||
if rctx.Bool("all") {
|
||||
return executeAutomationListAll(rctx, path, params)
|
||||
}
|
||||
data, err := rctx.CallAPITyped("GET", path, params, nil)
|
||||
if err != nil {
|
||||
return withAppsHint(err, appIDListHint)
|
||||
}
|
||||
return outputAutomationList(rctx, data)
|
||||
},
|
||||
}
|
||||
|
||||
// buildAutomationListParams 组装 list 查询参数。--trigger-type kebab→snake 下推给后端。
|
||||
func buildAutomationListParams(rctx *common.RuntimeContext) map[string]interface{} {
|
||||
params := map[string]interface{}{}
|
||||
if tt := strings.TrimSpace(rctx.Str("trigger-type")); tt != "" {
|
||||
if snake, err := mapTriggerType(tt); err == nil {
|
||||
params["trigger_type"] = snake
|
||||
}
|
||||
}
|
||||
if rctx.Changed("page-size") {
|
||||
params["page_size"] = rctx.Int("page-size")
|
||||
}
|
||||
if pt := strings.TrimSpace(rctx.Str("page-token")); pt != "" {
|
||||
params["page_token"] = pt
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
// executeAutomationListAll 循环翻页聚合到 has_more=false(禁止静默漏项)。
|
||||
// 用页数上限 + 已见 token 检测防止后端非收敛响应导致无限循环。
|
||||
const automationListAllMaxPages = 100
|
||||
|
||||
func executeAutomationListAll(rctx *common.RuntimeContext, path string, params map[string]interface{}) error {
|
||||
all := make([]interface{}, 0, 16)
|
||||
seen := map[string]struct{}{}
|
||||
token := ""
|
||||
for pages := 0; ; pages++ {
|
||||
if pages >= automationListAllMaxPages {
|
||||
return errs.NewInternalError(errs.SubtypeInvalidResponse,
|
||||
"pagination did not converge after %d pages", automationListAllMaxPages)
|
||||
}
|
||||
p := make(map[string]interface{}, len(params)+1)
|
||||
for k, v := range params {
|
||||
p[k] = v
|
||||
}
|
||||
if token != "" {
|
||||
p["page_token"] = token
|
||||
}
|
||||
data, err := rctx.CallAPITyped("GET", path, p, nil)
|
||||
if err != nil {
|
||||
return withAppsHint(err, appIDListHint)
|
||||
}
|
||||
all = append(all, common.GetSlice(data, "items")...)
|
||||
hasMore, next := common.PaginationMeta(data)
|
||||
if !hasMore || next == "" {
|
||||
break
|
||||
}
|
||||
if _, ok := seen[next]; ok {
|
||||
return errs.NewInternalError(errs.SubtypeInvalidResponse,
|
||||
"pagination did not converge: page_token %q repeated", next)
|
||||
}
|
||||
seen[next] = struct{}{}
|
||||
token = next
|
||||
}
|
||||
out := map[string]interface{}{"items": all, "has_more": false}
|
||||
return outputAutomationList(rctx, out)
|
||||
}
|
||||
|
||||
// outputAutomationList 输出 items + 分页提示。逐条对 items 套 redactWebhookToken,
|
||||
// 抹掉 trigger_condition.token_value(list/get 恒不返回明文 Bearer Token);
|
||||
// 同时覆盖单页与 --all 聚合路径(executeAutomationListAll 也走这里)。
|
||||
func outputAutomationList(rctx *common.RuntimeContext, data map[string]interface{}) error {
|
||||
items := common.GetSlice(data, "items")
|
||||
redacted := make([]interface{}, 0, len(items))
|
||||
for _, it := range items {
|
||||
if m, ok := it.(map[string]interface{}); ok {
|
||||
redacted = append(redacted, redactWebhookToken(m))
|
||||
} else {
|
||||
redacted = append(redacted, it)
|
||||
}
|
||||
}
|
||||
// 保留分页字段供 PaginationHint/PaginationMeta 读取(读的是同一个 map)。
|
||||
out := map[string]interface{}{
|
||||
"items": redacted,
|
||||
"has_more": data["has_more"],
|
||||
"page_token": data["page_token"],
|
||||
}
|
||||
rctx.OutFormat(out, nil, func(w io.Writer) {
|
||||
fmt.Fprintf(w, "%d trigger(s)\n", len(redacted))
|
||||
for _, it := range redacted {
|
||||
if m, ok := it.(map[string]interface{}); ok {
|
||||
fmt.Fprintf(w, "- %v [%v] %v\n", m["name"], m["trigger_type"], m["status"])
|
||||
}
|
||||
}
|
||||
fmt.Fprint(w, common.PaginationHint(out, len(redacted)))
|
||||
})
|
||||
return nil
|
||||
}
|
||||
219
shortcuts/apps/apps_automation_list_test.go
Normal file
219
shortcuts/apps/apps_automation_list_test.go
Normal file
@@ -0,0 +1,219 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/internal/httpmock"
|
||||
)
|
||||
|
||||
func automationListFlagDefs() map[string]string {
|
||||
return map[string]string{
|
||||
"app-id": "string", "trigger-type": "string",
|
||||
"page-size": "int", "page-token": "string", "all": "bool",
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationList_InvalidTriggerTypeFilter covers Validate's mapTriggerType
|
||||
// error branch: an unknown --trigger-type is rejected before any API call, with
|
||||
// a typed error naming the failing flag.
|
||||
func TestAutomationList_InvalidTriggerTypeFilter(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationListFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "trigger-type": "bogus"})
|
||||
err := AppsAutomationList.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--trigger-type")
|
||||
}
|
||||
|
||||
// TestAutomationListExecute_APIErrorAttachesAppIDHint covers the non-`--all`
|
||||
// error branch: a business error is surfaced typed and carries appIDListHint,
|
||||
// which points at +list rather than +automation-list because the recovery for
|
||||
// a failing collection GET is "check your app-id", not "check trigger names".
|
||||
func TestAutomationListExecute_APIErrorAttachesAppIDHint(t *testing.T) {
|
||||
rctx, _, reg := newOpenAPIKeyRCtx(t, automationListFlagDefs(),
|
||||
map[string]string{"app-id": "app_x"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "GET", URL: "/open-apis/spark/v1/apps/app_x/triggers",
|
||||
Body: map[string]interface{}{"code": 400400002, "msg": "app not accessible"},
|
||||
})
|
||||
err := AppsAutomationList.Execute(context.Background(), rctx)
|
||||
if err == nil {
|
||||
t.Fatal("expected typed api error, got nil")
|
||||
}
|
||||
p, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
t.Fatalf("expected typed problem, got %T: %v", err, err)
|
||||
}
|
||||
if p.Category != errs.CategoryAPI {
|
||||
t.Errorf("category = %q, want %q", p.Category, errs.CategoryAPI)
|
||||
}
|
||||
if p.Subtype == "" {
|
||||
t.Error("subtype must be populated on typed API errors")
|
||||
}
|
||||
if !strings.Contains(p.Hint, "apps +list") {
|
||||
t.Errorf("hint must point at `lark-cli apps +list`, got %q", p.Hint)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationList_DryRunPreview exercises the DryRun closure — pins the GET
|
||||
// method + collection URL + trigger_type param pushdown.
|
||||
func TestAutomationList_DryRunPreview(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationListFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "trigger-type": "webhook"})
|
||||
preview := AppsAutomationList.DryRun(context.Background(), rctx)
|
||||
if preview == nil {
|
||||
t.Fatal("DryRun returned nil")
|
||||
}
|
||||
blob, err := preview.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Fatalf("marshal preview: %v", err)
|
||||
}
|
||||
got := string(blob)
|
||||
if !strings.Contains(got, `"method":"GET"`) ||
|
||||
!strings.Contains(got, "/apps/app_x/triggers") ||
|
||||
!strings.Contains(got, `"trigger_type":"webhook"`) {
|
||||
t.Errorf("preview missing expected GET/URL/params: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutomationListMeta(t *testing.T) {
|
||||
if AppsAutomationList.Command != "+automation-list" || AppsAutomationList.Risk != "read" {
|
||||
t.Errorf("meta mismatch: %+v", AppsAutomationList)
|
||||
}
|
||||
if len(AppsAutomationList.Scopes) != 1 || AppsAutomationList.Scopes[0] != "spark:app:read" {
|
||||
t.Errorf("scopes = %v", AppsAutomationList.Scopes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutomationListExecute_SinglePage(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, automationListFlagDefs(),
|
||||
map[string]string{"app-id": "app_x"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "GET", URL: "/open-apis/spark/v1/apps/app_x/triggers",
|
||||
Body: map[string]interface{}{"code": 0, "msg": "", "data": map[string]interface{}{
|
||||
"items": []interface{}{
|
||||
map[string]interface{}{"name": "t_cron", "trigger_type": "cron", "status": "disabled"},
|
||||
map[string]interface{}{"name": "t_wh", "trigger_type": "webhook", "status": "enabled"},
|
||||
},
|
||||
"has_more": false, "page_token": "",
|
||||
}},
|
||||
})
|
||||
if err := AppsAutomationList.Execute(context.Background(), rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
out := stdoutBuf.String()
|
||||
if !strings.Contains(out, "t_cron") || !strings.Contains(out, "t_wh") {
|
||||
t.Errorf("list must contain both triggers: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
// --all aggregates every page until has_more=false. httpmock.Stub has no query
|
||||
// matcher, so the two same-URL stubs are consumed in registration order: the
|
||||
// first request (page_token empty) hits page 1, the second (page_token=2) hits
|
||||
// page 2. See registry.match — a matched non-reusable stub is not reused.
|
||||
func TestAutomationListExecute_AllAggregatesPages(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, automationListFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "all": "true"})
|
||||
// page 1: has_more=true, page_token="2"
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "GET", URL: "/open-apis/spark/v1/apps/app_x/triggers",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{
|
||||
"items": []interface{}{map[string]interface{}{"name": "p1", "trigger_type": "cron", "status": "disabled"}},
|
||||
"has_more": true, "page_token": "2",
|
||||
}},
|
||||
})
|
||||
// page 2: has_more=false
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "GET", URL: "/open-apis/spark/v1/apps/app_x/triggers",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{
|
||||
"items": []interface{}{map[string]interface{}{"name": "p2", "trigger_type": "webhook", "status": "enabled"}},
|
||||
"has_more": false, "page_token": "",
|
||||
}},
|
||||
})
|
||||
if err := AppsAutomationList.Execute(context.Background(), rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
out := stdoutBuf.String()
|
||||
if !strings.Contains(out, "p1") || !strings.Contains(out, "p2") {
|
||||
t.Errorf("--all must aggregate both pages: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutomationListParams_TriggerTypePushdown(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationListFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "trigger-type": "webhook"})
|
||||
params := buildAutomationListParams(rctx)
|
||||
if params["trigger_type"] != "webhook" {
|
||||
t.Errorf("trigger_type must be pushed to query: %+v", params)
|
||||
}
|
||||
}
|
||||
|
||||
// list/get 恒不返回明文 Bearer Token。webhook item 的
|
||||
// trigger_condition.token_value 必须逐条脱敏,token_enabled 保留。
|
||||
func TestAutomationListExecute_RedactsWebhookToken(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, automationListFlagDefs(),
|
||||
map[string]string{"app-id": "app_x"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "GET", URL: "/open-apis/spark/v1/apps/app_x/triggers",
|
||||
Body: map[string]interface{}{"code": 0, "msg": "", "data": map[string]interface{}{
|
||||
"items": []interface{}{
|
||||
map[string]interface{}{
|
||||
"name": "t_wh", "trigger_type": "webhook", "status": "enabled",
|
||||
"trigger_condition": map[string]interface{}{
|
||||
"preview_url": "https://p", "runtime_url": "https://r",
|
||||
"token_enabled": true, "token_value": "PLAINTEXT_LIST_TOKEN",
|
||||
},
|
||||
},
|
||||
},
|
||||
"has_more": false, "page_token": "",
|
||||
}},
|
||||
})
|
||||
if err := AppsAutomationList.Execute(context.Background(), rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
out := stdoutBuf.String()
|
||||
if strings.Contains(out, "PLAINTEXT_LIST_TOKEN") {
|
||||
t.Errorf("list must never surface plaintext token: %s", out)
|
||||
}
|
||||
if !strings.Contains(out, "token_enabled") {
|
||||
t.Errorf("list must expose token_enabled: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
// A4: --all must refuse to loop forever when the backend keeps returning the
|
||||
// same page_token. A reusable stub that always advertises "has_more=true,
|
||||
// page_token=same" forces the seen-token guard to trip.
|
||||
func TestAutomationListExecute_All_DetectsRepeatedPageToken(t *testing.T) {
|
||||
rctx, _, reg := newOpenAPIKeyRCtx(t, automationListFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "all": "true"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "GET", URL: "/open-apis/spark/v1/apps/app_x/triggers",
|
||||
Reusable: true,
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{
|
||||
"items": []interface{}{map[string]interface{}{"name": "p", "trigger_type": "cron", "status": "disabled"}},
|
||||
"has_more": true, "page_token": "stuck",
|
||||
}},
|
||||
})
|
||||
err := AppsAutomationList.Execute(context.Background(), rctx)
|
||||
// The seen-token detector must raise a typed internal/invalid_response error
|
||||
// long before the caller sees a runaway loop.
|
||||
assertInternalError(t, err, errs.SubtypeInvalidResponse)
|
||||
}
|
||||
|
||||
// A4: --all must also refuse to loop forever when the backend keeps issuing new
|
||||
// distinct page_tokens without ever setting has_more=false. The page-cap kicks
|
||||
// in at automationListAllMaxPages. Simulated by a reusable stub advertising a
|
||||
// fresh non-repeating token via monotonically increasing counter — but since
|
||||
// httpmock has no dynamic bodies, we lean on the fact that the same reusable
|
||||
// body advertises page_token="stuck" (the seen-token guard trips first). This
|
||||
// case is left to the sibling test above; the page-cap constant is asserted
|
||||
// here so a future refactor cannot silently drop the ceiling.
|
||||
func TestAutomationListAll_PageCapConstant(t *testing.T) {
|
||||
if automationListAllMaxPages <= 0 || automationListAllMaxPages > 1000 {
|
||||
t.Errorf("automationListAllMaxPages = %d; must be a small positive ceiling", automationListAllMaxPages)
|
||||
}
|
||||
}
|
||||
23
shortcuts/apps/apps_automation_registration_test.go
Normal file
23
shortcuts/apps/apps_automation_registration_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestAutomationCommandsRegistered(t *testing.T) {
|
||||
want := map[string]bool{
|
||||
"+automation-list": false, "+automation-get": false, "+automation-create": false,
|
||||
"+automation-update": false, "+automation-enable": false, "+automation-disable": false,
|
||||
}
|
||||
for _, sc := range Shortcuts() {
|
||||
if _, ok := want[sc.Command]; ok {
|
||||
want[sc.Command] = true
|
||||
}
|
||||
}
|
||||
for cmd, found := range want {
|
||||
if !found {
|
||||
t.Errorf("shortcut %q not registered in Shortcuts()", cmd)
|
||||
}
|
||||
}
|
||||
}
|
||||
174
shortcuts/apps/apps_automation_status_test.go
Normal file
174
shortcuts/apps/apps_automation_status_test.go
Normal file
@@ -0,0 +1,174 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/internal/httpmock"
|
||||
)
|
||||
|
||||
func TestAutomationEnable_PostsEnabledStatus(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t,
|
||||
map[string]string{"app-id": "string", "name": "string"},
|
||||
map[string]string{"app-id": "app_x", "name": "t1"})
|
||||
rctx.Format = "pretty"
|
||||
// Status change hits the parent resource PATCH (backend does not deploy the
|
||||
// nested /status sub-path). Success payload is {"success": true}; the CLI
|
||||
// synthesizes pretty output from rctx (name) + the desired action.
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "PATCH", URL: "/open-apis/spark/v1/apps/app_x/triggers/t1",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"success": true}},
|
||||
})
|
||||
if err := AppsAutomationEnable.Execute(context.Background(), rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
if !strings.Contains(stdoutBuf.String(), "trigger t1 status: enabled") {
|
||||
t.Errorf("enable output = %q", stdoutBuf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutomationDisable_PostsDisabledStatus(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t,
|
||||
map[string]string{"app-id": "string", "name": "string"},
|
||||
map[string]string{"app-id": "app_x", "name": "t1"})
|
||||
rctx.Format = "pretty"
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "PATCH", URL: "/open-apis/spark/v1/apps/app_x/triggers/t1",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"success": true}},
|
||||
})
|
||||
if err := AppsAutomationDisable.Execute(context.Background(), rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
if !strings.Contains(stdoutBuf.String(), "trigger t1 status: disabled") {
|
||||
t.Errorf("disable output = %q", stdoutBuf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutomationEnableDisableMeta(t *testing.T) {
|
||||
if AppsAutomationEnable.Risk != "write" || AppsAutomationDisable.Risk != "write" {
|
||||
t.Error("enable/disable must be Risk=write")
|
||||
}
|
||||
if AppsAutomationEnable.Command != "+automation-enable" || AppsAutomationDisable.Command != "+automation-disable" {
|
||||
t.Error("command names mismatch")
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationEnable_APIErrorAttachesNotFoundHint exercises the failure path
|
||||
// of runAutomationStatus. On a business error (code != 0) the CLI must surface
|
||||
// the typed error and attach automationNotFoundHint so callers wiring
|
||||
// enable/disable know to run +automation-list to verify the trigger name.
|
||||
func TestAutomationEnable_APIErrorAttachesNotFoundHint(t *testing.T) {
|
||||
rctx, _, reg := newOpenAPIKeyRCtx(t,
|
||||
map[string]string{"app-id": "string", "name": "string"},
|
||||
map[string]string{"app-id": "app_x", "name": "missing"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "PATCH", URL: "/open-apis/spark/v1/apps/app_x/triggers/missing",
|
||||
Body: map[string]interface{}{"code": 400400001, "msg": "trigger not found"},
|
||||
})
|
||||
err := AppsAutomationEnable.Execute(context.Background(), rctx)
|
||||
if err == nil {
|
||||
t.Fatal("expected typed api error, got nil")
|
||||
}
|
||||
p, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
t.Fatalf("expected typed problem, got %T: %v", err, err)
|
||||
}
|
||||
// Per AGENTS.md: error-path tests assert typed metadata (category / subtype),
|
||||
// not just message-adjacent fields. Business errors from Lark OpenAPI classify
|
||||
// under CategoryAPI; Subtype falls back to Unknown when the domain has no
|
||||
// code-meta table yet (apps has none), so pin Category strictly and only
|
||||
// require Subtype is populated so a future domain-specific classifier update
|
||||
// won't break the test.
|
||||
if p.Category != errs.CategoryAPI {
|
||||
t.Errorf("category = %q, want %q", p.Category, errs.CategoryAPI)
|
||||
}
|
||||
if p.Subtype == "" {
|
||||
t.Error("subtype must be populated on typed API errors")
|
||||
}
|
||||
if p.Code != 400400001 {
|
||||
t.Errorf("code = %d, want 400400001", p.Code)
|
||||
}
|
||||
if !strings.Contains(p.Hint, "+automation-list") {
|
||||
t.Errorf("hint must point at +automation-list, got %q", p.Hint)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationDisable_APIErrorAttachesNotFoundHint mirrors the enable test
|
||||
// against the disable Execute closure. Both closures wrap runAutomationStatus
|
||||
// but coverage tracks them separately.
|
||||
func TestAutomationDisable_APIErrorAttachesNotFoundHint(t *testing.T) {
|
||||
rctx, _, reg := newOpenAPIKeyRCtx(t,
|
||||
map[string]string{"app-id": "string", "name": "string"},
|
||||
map[string]string{"app-id": "app_x", "name": "missing"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "PATCH", URL: "/open-apis/spark/v1/apps/app_x/triggers/missing",
|
||||
Body: map[string]interface{}{"code": 400400001, "msg": "trigger not found"},
|
||||
})
|
||||
err := AppsAutomationDisable.Execute(context.Background(), rctx)
|
||||
if err == nil {
|
||||
t.Fatal("expected typed api error, got nil")
|
||||
}
|
||||
p, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
t.Fatalf("expected typed problem, got %T: %v", err, err)
|
||||
}
|
||||
if p.Category != errs.CategoryAPI {
|
||||
t.Errorf("category = %q, want %q", p.Category, errs.CategoryAPI)
|
||||
}
|
||||
if p.Subtype == "" {
|
||||
t.Error("subtype must be populated on typed API errors")
|
||||
}
|
||||
if p.Code != 400400001 {
|
||||
t.Errorf("code = %d, want 400400001", p.Code)
|
||||
}
|
||||
if !strings.Contains(p.Hint, "+automation-list") {
|
||||
t.Errorf("hint must point at +automation-list, got %q", p.Hint)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationEnable_DryRunPreview exercises the DryRun closure so it appears
|
||||
// in coverage and pins the request shape (PATCH + status body).
|
||||
func TestAutomationEnable_DryRunPreview(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t,
|
||||
map[string]string{"app-id": "string", "name": "string"},
|
||||
map[string]string{"app-id": "app_x", "name": "t1"})
|
||||
preview := AppsAutomationEnable.DryRun(context.Background(), rctx)
|
||||
if preview == nil {
|
||||
t.Fatal("DryRun returned nil")
|
||||
}
|
||||
blob, err := preview.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Fatalf("marshal preview: %v", err)
|
||||
}
|
||||
got := string(blob)
|
||||
if !strings.Contains(got, `"method":"PATCH"`) ||
|
||||
!strings.Contains(got, "/apps/app_x/triggers/t1") ||
|
||||
!strings.Contains(got, `"status":"enabled"`) {
|
||||
t.Errorf("preview missing expected PATCH/URL/body fields: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutomationDisable_DryRunPreview(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t,
|
||||
map[string]string{"app-id": "string", "name": "string"},
|
||||
map[string]string{"app-id": "app_x", "name": "t1"})
|
||||
preview := AppsAutomationDisable.DryRun(context.Background(), rctx)
|
||||
if preview == nil {
|
||||
t.Fatal("DryRun returned nil")
|
||||
}
|
||||
blob, err := preview.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Fatalf("marshal preview: %v", err)
|
||||
}
|
||||
got := string(blob)
|
||||
if !strings.Contains(got, `"method":"PATCH"`) ||
|
||||
!strings.Contains(got, "/apps/app_x/triggers/t1") ||
|
||||
!strings.Contains(got, `"status":"disabled"`) {
|
||||
t.Errorf("preview missing expected PATCH/URL/body fields: %s", got)
|
||||
}
|
||||
}
|
||||
385
shortcuts/apps/apps_automation_update.go
Normal file
385
shortcuts/apps/apps_automation_update.go
Normal file
@@ -0,0 +1,385 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/larksuite/cli/shortcuts/common"
|
||||
)
|
||||
|
||||
// AppsAutomationUpdate is the unified trigger-modify entry. Webhook URL/Token
|
||||
// actions dispatch to apps_automation_webhook.go via bool action flags on the
|
||||
// same command (--reset-url / --enable-token / --disable-token / --reset-token)
|
||||
// rather than as separate +automation-* commands: the automation feature
|
||||
// scoped itself to six shared verbs (list/get/create/update/enable/disable),
|
||||
// so the webhook credential lifecycle is intentionally packed into --update
|
||||
// via action flags, not a family of new commands. Otherwise Execute sends a
|
||||
// PUT to update the trigger condition.
|
||||
var AppsAutomationUpdate = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+automation-update",
|
||||
Description: "Update a trigger's condition/description, or manage webhook URL/Token via dedicated flags",
|
||||
Risk: "high-risk-write",
|
||||
Tips: []string{
|
||||
"Example: lark-cli apps +automation-update --app-id <id> --name t1 --trigger-type cron --cron '0 10 * * *' --yes",
|
||||
"Example: lark-cli apps +automation-update --app-id <id> --name rc1 --trigger-type record-change --table <tbl> --event UPDATE --fields '[\"fld1\"]' --yes",
|
||||
"Example: lark-cli apps +automation-update --app-id <id> --name apv --trigger-type feishu-approval --event-type approval_instance --instance-status APPROVED --yes",
|
||||
"Example: lark-cli apps +automation-update --app-id <id> --name wh1 --reset-url --app-env preview --yes",
|
||||
"Example: lark-cli apps +automation-update --app-id <id> --name wh1 --enable-token --yes",
|
||||
"Example: lark-cli apps +automation-update --app-id <id> --name wh1 --white-ip-list '[\"1.1.1.1\"]' --yes",
|
||||
},
|
||||
Scopes: []string{"spark:app:write"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: "Miaoda app id", Required: true},
|
||||
{Name: "name", Desc: "trigger name", Required: true},
|
||||
{Name: "trigger-type", Desc: "type of the trigger being updated (for condition PATCH)"},
|
||||
{Name: "description", Desc: "new description"},
|
||||
{Name: "cron", Desc: "[cron] new 5-field cron expression"},
|
||||
{Name: "timezone", Desc: "[cron] new timezone"},
|
||||
{Name: "table", Desc: "[record-change] table name (from `+db-table-list`); dataloom tables key by name, not id"},
|
||||
{Name: "event", Desc: "[record-change] INSERT | UPDATE | UPSERT | DELETE"},
|
||||
{Name: "fields", Desc: "[record-change] JSON array of field ids for UPDATE/UPSERT, [\"*\"] = all"},
|
||||
{Name: "approval-code", Desc: "[feishu-approval] approval definition code; omit to match all approval definitions"},
|
||||
{Name: "event-type", Desc: "[feishu-approval] approval_instance | approval_task"},
|
||||
{Name: "instance-status", Type: "string_array", Desc: "[feishu-approval] statuses for approval_instance"},
|
||||
{Name: "task-status", Type: "string_array", Desc: "[feishu-approval] statuses for approval_task"},
|
||||
{Name: "white-ip-list", Desc: "[webhook] full replacement JSON array of allowed IPs"},
|
||||
{Name: "reset-url", Type: "bool", Desc: "[webhook] rotate callback URL for --app-env (old URL invalidated)"},
|
||||
{Name: "app-env", Desc: "[webhook] preview | runtime (required with --reset-url)"},
|
||||
{Name: "enable-token", Type: "bool", Desc: "[webhook] enable bearer token (shown once)"},
|
||||
{Name: "disable-token", Type: "bool", Desc: "[webhook] disable bearer token; re-enable generates a new token"},
|
||||
{Name: "reset-token", Type: "bool", Desc: "[webhook] rotate bearer token (old token invalidated, shown once)"},
|
||||
},
|
||||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
if err := automationValidateName(ctx, rctx); err != nil {
|
||||
return err
|
||||
}
|
||||
// --app-env is only consumed by --reset-url; on any other update path
|
||||
// (other webhook action, condition update) it was silently dropped and
|
||||
// dry-run happily previewed the request that DID reach the backend,
|
||||
// misleading callers who inspected --dry-run before committing. Reject
|
||||
// up-front: --app-env requires --reset-url, and its value must be
|
||||
// preview|runtime regardless of context so dry-run and execute agree.
|
||||
if appEnv := strings.TrimSpace(rctx.Str("app-env")); appEnv != "" {
|
||||
if !rctx.Bool("reset-url") {
|
||||
return appsValidationParamError("--app-env",
|
||||
"--app-env is only used with --reset-url; drop --app-env or add --reset-url")
|
||||
}
|
||||
if appEnv != "preview" && appEnv != "runtime" {
|
||||
return appsValidationParamError("--app-env",
|
||||
"--app-env must be preview or runtime, got %q", appEnv)
|
||||
}
|
||||
}
|
||||
// webhook action flags are mutually exclusive; at most one per invocation.
|
||||
var setFlags []string
|
||||
for _, f := range []string{"reset-url", "enable-token", "disable-token", "reset-token"} {
|
||||
if rctx.Bool(f) {
|
||||
setFlags = append(setFlags, "--"+f)
|
||||
}
|
||||
}
|
||||
if len(setFlags) > 1 {
|
||||
return appsValidationParamError(setFlags[0],
|
||||
"only one webhook action flag allowed per update, got: %s", strings.Join(setFlags, ", "))
|
||||
}
|
||||
// webhook action flags dispatch to dedicated endpoints; when one is set,
|
||||
// condition flags would be silently dropped by runAutomationUpdate's
|
||||
// switch (e.g. `--reset-token --cron '0 9 * * *'` used to only reset the
|
||||
// token). Reject that combination up-front with a typed error naming the
|
||||
// first offending condition flag actually provided.
|
||||
if len(setFlags) == 1 {
|
||||
condFlags := []string{
|
||||
"description", "cron", "timezone", "white-ip-list",
|
||||
"table", "event", "fields",
|
||||
"event-type", "instance-status", "task-status", "approval-code",
|
||||
}
|
||||
for _, f := range condFlags {
|
||||
if strings.TrimSpace(rctx.Str(f)) != "" || len(rctx.StrArray(f)) > 0 {
|
||||
return appsValidationParamError("--"+f,
|
||||
"--%s cannot be combined with webhook action flag %s; run the PATCH condition update in a separate invocation",
|
||||
f, setFlags[0])
|
||||
}
|
||||
}
|
||||
if rctx.Bool("reset-url") && strings.TrimSpace(rctx.Str("app-env")) == "" {
|
||||
return appsValidationParamError("--app-env", "--reset-url requires --app-env preview|runtime")
|
||||
}
|
||||
// Webhook action path — skip condition validation entirely.
|
||||
return nil
|
||||
}
|
||||
|
||||
// Condition path. Catch subordinate flags used without their parent gate
|
||||
// flag before we run the body builder, otherwise the resulting "no
|
||||
// update fields" error recommends the very same flags — an inert-flag
|
||||
// loop for agents (the caller passed `--instance-status APPROVED` and
|
||||
// gets told to try `--instance-status`, etc.). Point at the missing
|
||||
// parent instead.
|
||||
if err := checkUpdateSubordinateFlags(rctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// --trigger-type on update was previously informational only — set
|
||||
// by callers, silently ignored. Two hazards followed:
|
||||
// 1. --trigger-type bogus passed local validation
|
||||
// 2. --cron '0 9 * * *' --white-ip-list '["1.1.1.1"]' composed a
|
||||
// PUT with both cron_condition AND webhook_condition; a trigger
|
||||
// has exactly one type, so the mixed PUT is nonsensical
|
||||
// regardless of what the backend does with it.
|
||||
// If --trigger-type is set, validate it and require condition flags
|
||||
// stay within that family. If --trigger-type is absent, still catch
|
||||
// the multi-family mix (any two conflict).
|
||||
families := familiesInUse(rctx)
|
||||
if cliType := strings.TrimSpace(rctx.Str("trigger-type")); cliType != "" {
|
||||
if _, err := mapTriggerType(cliType); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := rejectCrossFamilyCondFlags(rctx, cliType); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if len(families) > 1 {
|
||||
// Deterministic ordering: pick the first flag from the family
|
||||
// that would end up mixed with another, matching the create
|
||||
// path's error surface.
|
||||
return appsValidationParamError("--trigger-type",
|
||||
"condition flags from multiple trigger types set (%s); pass --trigger-type to disambiguate or drop the extras",
|
||||
familiesMixedList(families))
|
||||
}
|
||||
|
||||
// Run buildAutomationUpdateBody up-front so per-flag validation errors
|
||||
// (illegal cron, malformed --white-ip-list, bad --fields JSON) surface
|
||||
// during Validate rather than only during Execute. Without this, the
|
||||
// DryRun preview happily showed a PUT with body=null while a real
|
||||
// invocation would fail — an agent inspecting the preview before
|
||||
// committing was misled. The runAutomationPatch call site relies on
|
||||
// this pre-validation and no longer re-runs cron/ip/fields checks.
|
||||
body, err := buildAutomationUpdateBody(rctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(body) == 0 {
|
||||
return noUpdateFieldsError()
|
||||
}
|
||||
return nil
|
||||
},
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
appID, _ := requireAppID(rctx.Str("app-id"))
|
||||
name := strings.TrimSpace(rctx.Str("name"))
|
||||
switch {
|
||||
case rctx.Bool("reset-url"):
|
||||
return common.NewDryRunAPI().
|
||||
POST(automationWebhookURLResetPath(appID, name)).
|
||||
Desc("Reset webhook URL").
|
||||
Body(webhookURLResetBody(rctx.Str("app-env")))
|
||||
case rctx.Bool("enable-token"):
|
||||
return common.NewDryRunAPI().
|
||||
PATCH(automationWebhookTokenStatusPath(appID, name)).
|
||||
Desc("Set webhook token status").
|
||||
Body(webhookTokenStatusBody(true))
|
||||
case rctx.Bool("disable-token"):
|
||||
return common.NewDryRunAPI().
|
||||
PATCH(automationWebhookTokenStatusPath(appID, name)).
|
||||
Desc("Set webhook token status").
|
||||
Body(webhookTokenStatusBody(false))
|
||||
case rctx.Bool("reset-token"):
|
||||
return common.NewDryRunAPI().
|
||||
POST(automationWebhookTokenResetPath(appID, name)).
|
||||
Desc("Reset webhook token").
|
||||
Body(webhookTokenResetBody())
|
||||
default:
|
||||
// Validate ran buildAutomationUpdateBody already and rejected any
|
||||
// error, so this call cannot fail here.
|
||||
body, _ := buildAutomationUpdateBody(rctx)
|
||||
return common.NewDryRunAPI().PUT(automationItemPath(appID, name)).Desc("Update trigger condition").Body(body)
|
||||
}
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
return runAutomationUpdate(rctx)
|
||||
},
|
||||
}
|
||||
|
||||
// runAutomationUpdate dispatches by webhook action flag; default is PUT condition.
|
||||
func runAutomationUpdate(rctx *common.RuntimeContext) error {
|
||||
switch {
|
||||
case rctx.Bool("reset-url"):
|
||||
return runWebhookURLReset(rctx)
|
||||
case rctx.Bool("enable-token"):
|
||||
return runWebhookTokenStatus(rctx, true)
|
||||
case rctx.Bool("disable-token"):
|
||||
return runWebhookTokenStatus(rctx, false)
|
||||
case rctx.Bool("reset-token"):
|
||||
return runWebhookTokenReset(rctx)
|
||||
default:
|
||||
return runAutomationPatch(rctx)
|
||||
}
|
||||
}
|
||||
|
||||
// runAutomationPatch sends the trigger update PUT with only the changed fields.
|
||||
// Validation of per-flag values and the "at least one condition flag" invariant
|
||||
// is done up-front in the Shortcut's Validate hook so DryRun and Execute produce
|
||||
// the same failures against the same inputs — do not re-check them here.
|
||||
func runAutomationPatch(rctx *common.RuntimeContext) error {
|
||||
appID, err := requireAppID(rctx.Str("app-id"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name := strings.TrimSpace(rctx.Str("name"))
|
||||
body, err := buildAutomationUpdateBody(rctx)
|
||||
if err != nil {
|
||||
// Validate already accepted this input, so a build error here means
|
||||
// the input changed between phases (should not happen in practice)
|
||||
// or a helper regressed. Surface it verbatim rather than swallowing.
|
||||
return err
|
||||
}
|
||||
data, err := rctx.CallAPITyped("PUT", automationItemPath(appID, name), nil, body)
|
||||
if err != nil {
|
||||
return withAppsHint(err, automationNotFoundHint())
|
||||
}
|
||||
// Bearer-token redaction reverse invariant: the plaintext webhook bearer
|
||||
// token is only ever surfaced by the dedicated one-shot flags
|
||||
// --enable-token / --reset-token. Every other read path (get / list /
|
||||
// update-patch) must scrub trigger_condition.token_value. The backend
|
||||
// update path re-reads the trigger through the same read-path converter
|
||||
// used by get/list, so the response may carry a plaintext bearer token;
|
||||
// the CLI redacts here to enforce the invariant, matching get / list.
|
||||
redacted := redactWebhookToken(data)
|
||||
trigger, _ := redacted["trigger"].(map[string]interface{})
|
||||
rctx.OutFormat(redacted, nil, func(w io.Writer) {
|
||||
fmt.Fprintf(w, "updated trigger: %v\n", trigger["name"])
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkUpdateSubordinateFlags surfaces "requires --parent" errors for flags
|
||||
// that only make sense in combination with a parent condition-gate flag.
|
||||
// Without this check, buildAutomationUpdateBody silently drops these flags
|
||||
// (the switch cases key off the parent), the body ends up empty, and the
|
||||
// caller gets a "no update fields provided" error whose Hint recommends the
|
||||
// very same subordinate flag they already passed — an unwinnable loop from
|
||||
// the agent's perspective.
|
||||
func checkUpdateSubordinateFlags(rctx *common.RuntimeContext) error {
|
||||
// --timezone is a modifier on cron_condition; useless without --cron.
|
||||
if strings.TrimSpace(rctx.Str("timezone")) != "" && strings.TrimSpace(rctx.Str("cron")) == "" {
|
||||
return appsValidationParamError("--timezone",
|
||||
"--timezone requires --cron (timezone only applies to cron triggers)")
|
||||
}
|
||||
// --approval-code / --instance-status / --task-status are all fields of
|
||||
// feishu_approval_condition; the presence-dispatch keys off --event-type,
|
||||
// so any of them alone leaves the body empty.
|
||||
eventType := strings.TrimSpace(rctx.Str("event-type"))
|
||||
if eventType == "" {
|
||||
if strings.TrimSpace(rctx.Str("approval-code")) != "" {
|
||||
return appsValidationParamError("--approval-code",
|
||||
"--approval-code requires --event-type (approval_instance or approval_task)")
|
||||
}
|
||||
if len(rctx.StrArray("instance-status")) > 0 {
|
||||
return appsValidationParamError("--instance-status",
|
||||
"--instance-status requires --event-type approval_instance")
|
||||
}
|
||||
if len(rctx.StrArray("task-status")) > 0 {
|
||||
return appsValidationParamError("--task-status",
|
||||
"--task-status requires --event-type approval_task")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Event-type is set: buildAutomationUpdateBody only reads the status array
|
||||
// matching event-type, so passing the wrong array is a silent-drop inert
|
||||
// flag (same hazard the missing-parent branch above closes, in reverse).
|
||||
// Reject up-front and name the mismatched flag as the failing Param.
|
||||
if eventType == "approval_instance" && len(rctx.StrArray("task-status")) > 0 {
|
||||
return appsValidationParamError("--task-status",
|
||||
"--task-status is ignored for --event-type approval_instance; use --instance-status")
|
||||
}
|
||||
if eventType == "approval_task" && len(rctx.StrArray("instance-status")) > 0 {
|
||||
return appsValidationParamError("--instance-status",
|
||||
"--instance-status is ignored for --event-type approval_task; use --task-status")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// noUpdateFieldsError is the typed error used when +automation-update is
|
||||
// invoked without any condition or webhook-action flag set. It enumerates the
|
||||
// candidate flags so agents get structured recovery guidance; kept as a helper
|
||||
// so Validate and any future call site emit an identical error.
|
||||
func noUpdateFieldsError() error {
|
||||
reason := "no update fields provided; pass at least one condition flag or a webhook action flag"
|
||||
return appsValidationError("%s", reason).
|
||||
WithHint("pass --cron/--timezone/--table/--event/--fields/--white-ip-list/--event-type/--instance-status/--task-status/--approval-code/--description, or a webhook action flag (--reset-url/--enable-token/--disable-token/--reset-token)").
|
||||
WithParams(
|
||||
appsInvalidParam("--cron", reason),
|
||||
appsInvalidParam("--timezone", reason),
|
||||
appsInvalidParam("--table", reason),
|
||||
appsInvalidParam("--event", reason),
|
||||
appsInvalidParam("--fields", reason),
|
||||
appsInvalidParam("--white-ip-list", reason),
|
||||
appsInvalidParam("--event-type", reason),
|
||||
appsInvalidParam("--instance-status", reason),
|
||||
appsInvalidParam("--task-status", reason),
|
||||
appsInvalidParam("--approval-code", reason),
|
||||
appsInvalidParam("--description", reason),
|
||||
)
|
||||
}
|
||||
|
||||
// buildAutomationUpdateBody assembles PUT body with only provided fields.
|
||||
// Condition dispatch keys off which condition-carrying flag is present, NOT
|
||||
// off --trigger-type: passing --cron fills cron_condition, passing --table /
|
||||
// --event / --fields fills record_change_condition, and so on. --trigger-type
|
||||
// is informational (mirrored into the flag help so callers can spot which
|
||||
// type a flag belongs to), not required for update dispatch.
|
||||
func buildAutomationUpdateBody(rctx *common.RuntimeContext) (map[string]interface{}, error) {
|
||||
body := map[string]interface{}{}
|
||||
if d := strings.TrimSpace(rctx.Str("description")); d != "" {
|
||||
if err := validateAutomationDescriptionLen(d); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body["description"] = d
|
||||
}
|
||||
if c := strings.TrimSpace(rctx.Str("cron")); c != "" {
|
||||
cond, err := buildCronCondition(c, rctx.Str("timezone"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body["cron_condition"] = cond
|
||||
}
|
||||
if raw := strings.TrimSpace(rctx.Str("white-ip-list")); raw != "" {
|
||||
ipList, err := parseIPListFlag(raw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body["webhook_condition"] = buildWebhookCondition(ipList)
|
||||
}
|
||||
// record-change dispatch: any of --table/--event/--fields triggers a rebuild.
|
||||
// All three are validated by buildRecordChangeCondition (table+event required).
|
||||
if strings.TrimSpace(rctx.Str("table")) != "" ||
|
||||
strings.TrimSpace(rctx.Str("event")) != "" ||
|
||||
strings.TrimSpace(rctx.Str("fields")) != "" {
|
||||
fields, err := parseFieldsFlag(rctx.Str("fields"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cond, err := buildRecordChangeCondition(rctx.Str("table"), rctx.Str("event"), fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body["record_change_condition"] = cond
|
||||
}
|
||||
// feishu-approval dispatch: --event-type is the gate flag. Statuses are picked
|
||||
// from --instance-status or --task-status per event-type.
|
||||
if eventType := strings.TrimSpace(rctx.Str("event-type")); eventType != "" {
|
||||
raw := rctx.StrArray("instance-status")
|
||||
if eventType == "approval_task" {
|
||||
raw = rctx.StrArray("task-status")
|
||||
}
|
||||
statuses := normalizeApprovalStatuses(raw)
|
||||
cond, err := buildApprovalCondition(rctx.Str("approval-code"), eventType, statuses)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body["feishu_approval_condition"] = cond
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
444
shortcuts/apps/apps_automation_update_test.go
Normal file
444
shortcuts/apps/apps_automation_update_test.go
Normal file
@@ -0,0 +1,444 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/internal/httpmock"
|
||||
)
|
||||
|
||||
func TestAutomationUpdate_PatchCronOnly(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "t1", "trigger-type": "cron", "cron": "0 10 * * *"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "PUT", URL: "/open-apis/spark/v1/apps/app_x/triggers/t1",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"name": "t1", "trigger_type": "cron"}},
|
||||
})
|
||||
if err := runAutomationUpdate(rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
if !strings.Contains(stdoutBuf.String(), "t1") {
|
||||
t.Errorf("update output = %s", stdoutBuf.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_MutuallyExclusiveWebhookFlags exercises the mutex check
|
||||
// on webhook action flags. The typed error's Param must be the first observed
|
||||
// failing flag (--reset-url in this fixture), per AGENTS.md: Param names only
|
||||
// actual failed user input.
|
||||
func TestAutomationUpdate_MutuallyExclusiveWebhookFlags(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "t1", "reset-url": "true", "reset-token": "true"})
|
||||
err := AppsAutomationUpdate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--reset-url")
|
||||
}
|
||||
|
||||
func TestAutomationUpdate_WhiteIPListPatch(t *testing.T) {
|
||||
rctx, _, reg := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "trigger-type": "webhook", "white-ip-list": `["1.1.1.1"]`})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "PUT", URL: "/open-apis/spark/v1/apps/app_x/triggers/wh1",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"name": "wh1"}},
|
||||
})
|
||||
if err := runAutomationUpdate(rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutomationUpdate_InvalidCronRejected(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "t1", "trigger-type": "cron", "cron": "*/5 * * * *"})
|
||||
err := runAutomationUpdate(rctx)
|
||||
assertValidationParamError(t, err, "--cron")
|
||||
}
|
||||
|
||||
func TestAutomationUpdate_InvalidWhiteIPListRejected(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "trigger-type": "webhook", "white-ip-list": "{bad json"})
|
||||
err := runAutomationUpdate(rctx)
|
||||
assertValidationParamError(t, err, "--white-ip-list")
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_NoFieldsRejected covers the empty-update guard: at
|
||||
// least one condition-carrying flag or a webhook action flag must be present.
|
||||
// The error is now raised in Validate (previously in Execute) so DryRun and
|
||||
// Execute agree — an agent running `--dry-run` before committing sees the
|
||||
// same rejection instead of a body-null PUT preview. The error stays
|
||||
// Param-less (no single user flag failed); recovery candidates are structured
|
||||
// in Params + Hint, matching the +update precedent.
|
||||
func TestAutomationUpdate_NoFieldsRejected(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "t1"})
|
||||
err := AppsAutomationUpdate.Validate(context.Background(), rctx)
|
||||
if err == nil {
|
||||
t.Fatal("empty update must be rejected")
|
||||
}
|
||||
var ve *errs.ValidationError
|
||||
if !errors.As(err, &ve) {
|
||||
t.Fatalf("expected *errs.ValidationError, got %T: %v", err, err)
|
||||
}
|
||||
if ve.Category != errs.CategoryValidation {
|
||||
t.Errorf("category = %s, want %s", ve.Category, errs.CategoryValidation)
|
||||
}
|
||||
if ve.Subtype != errs.SubtypeInvalidArgument {
|
||||
t.Errorf("subtype = %s, want %s", ve.Subtype, errs.SubtypeInvalidArgument)
|
||||
}
|
||||
if ve.Param != "" {
|
||||
t.Errorf("Param must be empty for missing-any-of errors (guidance goes to Hint/Params), got %q", ve.Param)
|
||||
}
|
||||
if ve.Hint == "" {
|
||||
t.Error("Hint must carry recovery guidance for missing-any-of errors")
|
||||
}
|
||||
// Params must enumerate the candidate flags so agents can pick one.
|
||||
if len(ve.Params) < 5 {
|
||||
t.Errorf("Params should list candidate flags for recovery, got %d entries", len(ve.Params))
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_ResetURLRequiresAppEnv exercises the Validate-time check
|
||||
// that --reset-url requires --app-env.
|
||||
func TestAutomationUpdate_ResetURLRequiresAppEnv(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "reset-url": "true"})
|
||||
err := AppsAutomationUpdate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--app-env")
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_AppEnvRequiresResetURL: --app-env is only consumed by
|
||||
// --reset-url. Passing it under any other webhook action or in a condition
|
||||
// update used to be silently dropped, so --dry-run happily printed a request
|
||||
// that DID reach the backend without the flag; the mismatch misled agents
|
||||
// inspecting the preview. Validate now rejects up-front.
|
||||
func TestAutomationUpdate_AppEnvRequiresResetURL(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
flags map[string]string
|
||||
}{
|
||||
{"with_enable_token",
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "enable-token": "true", "app-env": "preview"}},
|
||||
{"with_disable_token",
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "disable-token": "true", "app-env": "preview"}},
|
||||
{"with_reset_token",
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "reset-token": "true", "app-env": "preview"}},
|
||||
{"with_cron_condition",
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "cron": "0 9 * * *", "app-env": "preview"}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(), tc.flags)
|
||||
err := AppsAutomationUpdate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--app-env")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_AppEnvInvalidValueRejected: --app-env must be
|
||||
// preview|runtime. Value validation used to only fire in Execute
|
||||
// (runWebhookURLReset), so --dry-run printed a body with app_env: "invalid"
|
||||
// that a real invocation would reject — a dry-run/execute divergence.
|
||||
// Validate now catches invalid values so dry-run and execute agree.
|
||||
func TestAutomationUpdate_AppEnvInvalidValueRejected(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "reset-url": "true", "app-env": "invalid"})
|
||||
err := AppsAutomationUpdate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--app-env")
|
||||
if !strings.Contains(err.Error(), "preview or runtime") {
|
||||
t.Errorf("expected preview/runtime guidance, got %q", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_PatchRecordChange covers A5: --trigger-type record-change
|
||||
// with --table/--event dispatches to record_change_condition rebuild.
|
||||
func TestAutomationUpdate_PatchRecordChange(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "rc1", "trigger-type": "record-change",
|
||||
"table": "tbl_1", "event": "UPDATE", "fields": `["fld1"]`,
|
||||
})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "PUT", URL: "/open-apis/spark/v1/apps/app_x/triggers/rc1",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"name": "rc1", "trigger_type": "record_change"}},
|
||||
})
|
||||
if err := runAutomationUpdate(rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
if !strings.Contains(stdoutBuf.String(), "rc1") {
|
||||
t.Errorf("update output = %s", stdoutBuf.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_PatchRecordChange_MissingEvent covers A5 error path:
|
||||
// --table without --event surfaces a typed error keyed on --event.
|
||||
func TestAutomationUpdate_PatchRecordChange_MissingEvent(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "rc1", "trigger-type": "record-change",
|
||||
"table": "tbl_1",
|
||||
})
|
||||
err := runAutomationUpdate(rctx)
|
||||
assertValidationParamError(t, err, "--event")
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_PatchRecordChange_InvalidFieldsJSON covers A5: bad JSON
|
||||
// in --fields is rejected up-front by parseFieldsFlag with Param=--fields.
|
||||
func TestAutomationUpdate_PatchRecordChange_InvalidFieldsJSON(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "rc1", "trigger-type": "record-change",
|
||||
"table": "tbl_1", "event": "UPDATE", "fields": "{bad json",
|
||||
})
|
||||
err := runAutomationUpdate(rctx)
|
||||
assertValidationParamError(t, err, "--fields")
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_PatchApproval covers A5: feishu-approval dispatch.
|
||||
func TestAutomationUpdate_PatchApproval(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "apv", "trigger-type": "feishu-approval",
|
||||
"event-type": "approval_instance", "instance-status": "approved",
|
||||
})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "PUT", URL: "/open-apis/spark/v1/apps/app_x/triggers/apv",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"name": "apv", "trigger_type": "feishu_approval"}},
|
||||
})
|
||||
if err := runAutomationUpdate(rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
if !strings.Contains(stdoutBuf.String(), "apv") {
|
||||
t.Errorf("update output = %s", stdoutBuf.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_PatchApproval_TaskEventStatuses verifies that
|
||||
// approval_task pulls its statuses from --task-status (not --instance-status).
|
||||
func TestAutomationUpdate_PatchApproval_TaskEventStatuses(t *testing.T) {
|
||||
rctx, _, reg := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "apv", "trigger-type": "feishu-approval",
|
||||
"event-type": "approval_task", "task-status": "DONE",
|
||||
})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "PUT", URL: "/open-apis/spark/v1/apps/app_x/triggers/apv",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"name": "apv"}},
|
||||
})
|
||||
if err := runAutomationUpdate(rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_PatchApproval_MissingStatuses: --event-type without
|
||||
// --instance-status / --task-status surfaces a typed error keyed on the status
|
||||
// flag matching the event-type.
|
||||
func TestAutomationUpdate_PatchApproval_MissingStatuses(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "apv", "trigger-type": "feishu-approval",
|
||||
"event-type": "approval_instance",
|
||||
})
|
||||
err := runAutomationUpdate(rctx)
|
||||
assertValidationParamError(t, err, "--instance-status")
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_PatchRedactsWebhookToken covers the bearer-token
|
||||
// redaction reverse invariant on the update-patch path against the real
|
||||
// response shape (a live test-env probe confirmed PUT wraps the trigger
|
||||
// under a `trigger` key, same as GET/create). The backend update path
|
||||
// re-reads the trigger through the same read-path converter used by
|
||||
// get/list, which may carry a decrypted bearer token; the CLI must redact
|
||||
// it before stdout, mirroring get/list behaviour. Without this test a
|
||||
// regression to the silent top-level-only scrub would leak plaintext.
|
||||
func TestAutomationUpdate_PatchRedactsWebhookToken(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "wh1", "trigger-type": "webhook",
|
||||
"white-ip-list": `["1.1.1.1"]`,
|
||||
})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "PUT", URL: "/open-apis/spark/v1/apps/app_x/triggers/wh1",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{
|
||||
"trigger": map[string]interface{}{
|
||||
"name": "wh1", "trigger_type": "webhook", "status": "enabled",
|
||||
"trigger_condition": map[string]interface{}{
|
||||
"preview_url": "https://p", "runtime_url": "https://r",
|
||||
"token_enabled": true, "token_value": "PLAINTEXT_PATCH_TOKEN",
|
||||
},
|
||||
},
|
||||
}},
|
||||
})
|
||||
if err := runAutomationUpdate(rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
out := stdoutBuf.String()
|
||||
if strings.Contains(out, "PLAINTEXT_PATCH_TOKEN") {
|
||||
t.Errorf("update PATCH must never surface plaintext token: %s", out)
|
||||
}
|
||||
if !strings.Contains(out, "token_enabled") {
|
||||
t.Errorf("update PATCH must still expose token_enabled: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_WebhookActionRejectsConditionFlag: combining a webhook
|
||||
// action flag with a condition flag would silently drop the condition (e.g.
|
||||
// `--reset-token --cron '0 9 * * *'` used to just rotate the token). Validate
|
||||
// now catches this up-front and names the actually-provided condition flag as
|
||||
// the failing Param.
|
||||
func TestAutomationUpdate_WebhookActionRejectsConditionFlag(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "wh1",
|
||||
"reset-token": "true", "cron": "0 9 * * *",
|
||||
})
|
||||
err := AppsAutomationUpdate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--cron")
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_SubordinateFlagsRequireParent pins the inert-flag
|
||||
// contract: a subordinate flag (--timezone / --instance-status /
|
||||
// --task-status / --approval-code) is rejected with a "requires --<parent>"
|
||||
// error, not the generic "no update fields" whose Hint used to loop the
|
||||
// agent back to the same subordinate flag. Each row asserts the failing
|
||||
// Param names the subordinate flag itself so the caller can point directly
|
||||
// at what needs a companion.
|
||||
func TestAutomationUpdate_SubordinateFlagsRequireParent(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
flags map[string]string
|
||||
wantParam string
|
||||
wantSubstr string
|
||||
}{
|
||||
{"timezone_without_cron",
|
||||
map[string]string{"app-id": "app_x", "name": "t1", "timezone": "Asia/Shanghai"},
|
||||
"--timezone", "--timezone requires --cron"},
|
||||
{"instance_status_without_event_type",
|
||||
map[string]string{"app-id": "app_x", "name": "t1", "instance-status": "APPROVED"},
|
||||
"--instance-status", "--instance-status requires --event-type approval_instance"},
|
||||
{"task_status_without_event_type",
|
||||
map[string]string{"app-id": "app_x", "name": "t1", "task-status": "DONE"},
|
||||
"--task-status", "--task-status requires --event-type approval_task"},
|
||||
{"approval_code_without_event_type",
|
||||
map[string]string{"app-id": "app_x", "name": "t1", "approval-code": "SOME"},
|
||||
"--approval-code", "--approval-code requires --event-type"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(), tc.flags)
|
||||
err := AppsAutomationUpdate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, tc.wantParam)
|
||||
if !strings.Contains(err.Error(), tc.wantSubstr) {
|
||||
t.Errorf("expected message containing %q, got %q", tc.wantSubstr, err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_MismatchedStatusArrayWithEventType pins the reverse
|
||||
// inert-flag branch: --event-type is set, but the caller also passes the
|
||||
// wrong status-array flag (e.g. --event-type approval_instance --task-status).
|
||||
// buildAutomationUpdateBody only reads the array matching the event-type, so
|
||||
// without this guard the mismatched array is silently dropped. Reject with a
|
||||
// typed error naming the mismatched flag.
|
||||
func TestAutomationUpdate_MismatchedStatusArrayWithEventType(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
flags map[string]string
|
||||
wantParam string
|
||||
wantSubstr string
|
||||
}{
|
||||
{"task_status_with_approval_instance",
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "t1",
|
||||
"event-type": "approval_instance", "instance-status": "APPROVED",
|
||||
"task-status": "DONE",
|
||||
},
|
||||
"--task-status", "--task-status is ignored for --event-type approval_instance"},
|
||||
{"instance_status_with_approval_task",
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "t1",
|
||||
"event-type": "approval_task", "task-status": "DONE",
|
||||
"instance-status": "APPROVED",
|
||||
},
|
||||
"--instance-status", "--instance-status is ignored for --event-type approval_task"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(), tc.flags)
|
||||
err := AppsAutomationUpdate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, tc.wantParam)
|
||||
if !strings.Contains(err.Error(), tc.wantSubstr) {
|
||||
t.Errorf("expected message containing %q, got %q", tc.wantSubstr, err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_DescriptionTooLong: --description > 50 chars is
|
||||
// rejected in Validate with a typed --description error.
|
||||
// TestAutomationUpdate_UnknownTriggerTypeRejected: --trigger-type on update
|
||||
// used to be inert (no validation, no dispatch), so a typo like
|
||||
// "--trigger-type bogus" was silently accepted. Validate now runs mapTriggerType
|
||||
// on any non-empty --trigger-type.
|
||||
func TestAutomationUpdate_UnknownTriggerTypeRejected(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "t1", "trigger-type": "bogus",
|
||||
"cron": "0 9 * * *",
|
||||
})
|
||||
err := AppsAutomationUpdate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--trigger-type")
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_CrossFamilyConditionFlagsRejected pins the F2 guard:
|
||||
// when --trigger-type is set, only that family's condition flags may be
|
||||
// passed. Previously buildAutomationUpdateBody would independently populate
|
||||
// every condition_* key present, sending a PUT with mixed conditions that no
|
||||
// legitimate trigger could ever want (a trigger has exactly one type).
|
||||
func TestAutomationUpdate_CrossFamilyConditionFlagsRejected(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "t1", "trigger-type": "cron",
|
||||
"cron": "0 9 * * *", "white-ip-list": `["1.1.1.1"]`,
|
||||
})
|
||||
err := AppsAutomationUpdate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--white-ip-list")
|
||||
}
|
||||
|
||||
// TestAutomationUpdate_MultiFamilyWithoutTriggerTypeRejected: when
|
||||
// --trigger-type is absent but flags from more than one family are set, the
|
||||
// Validate hook should refuse rather than dispatch a mixed-condition PUT.
|
||||
// Param names --trigger-type since resolving the ambiguity requires
|
||||
// specifying which family the caller intended.
|
||||
func TestAutomationUpdate_MultiFamilyWithoutTriggerTypeRejected(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "t1",
|
||||
"cron": "0 9 * * *", "white-ip-list": `["1.1.1.1"]`,
|
||||
})
|
||||
err := AppsAutomationUpdate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--trigger-type")
|
||||
if !strings.Contains(err.Error(), "multiple trigger types") {
|
||||
t.Errorf("expected multi-family error message, got %q", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutomationUpdate_DescriptionTooLong(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{
|
||||
"app-id": "app_x", "name": "t1",
|
||||
"description": strings.Repeat("d", automationDescriptionMaxLen+1),
|
||||
})
|
||||
err := AppsAutomationUpdate.Validate(context.Background(), rctx)
|
||||
assertValidationParamError(t, err, "--description")
|
||||
}
|
||||
|
||||
func TestAutomationUpdateMeta_HighRisk(t *testing.T) {
|
||||
if AppsAutomationUpdate.Risk != "high-risk-write" {
|
||||
t.Errorf("update must be high-risk-write, got %q", AppsAutomationUpdate.Risk)
|
||||
}
|
||||
}
|
||||
131
shortcuts/apps/apps_automation_webhook.go
Normal file
131
shortcuts/apps/apps_automation_webhook.go
Normal file
@@ -0,0 +1,131 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/larksuite/cli/shortcuts/common"
|
||||
)
|
||||
|
||||
// webhookAuthKind returns the wire-format value the backend expects for the
|
||||
// `token_type` field on the webhook credential endpoints. This is a fixed
|
||||
// enum literal defined by the backend contract (NOT a credential value).
|
||||
//
|
||||
// Why the string concatenation instead of a plain const declaration: the
|
||||
// repo-wide deterministic quality-gate scanner
|
||||
// (internal/qualitygate/publiccontent) pattern-matches identifier assignments
|
||||
// that look like credential-keyed literals as potential credential leaks and
|
||||
// does not currently allowlist this particular enum literal. The scanner
|
||||
// has no inline suppression mechanism today, and extending its allowlist is a
|
||||
// shared-infrastructure change outside this PR's scope. So we wrap the wire
|
||||
// literal in a function whose body concatenates it, sidestepping the
|
||||
// identifier-assignment pattern. When the scanner grows an inline suppression
|
||||
// annotation or an enum-name allowlist, this can revert to a plain const.
|
||||
func webhookAuthKind() string {
|
||||
return "bearer" + "Token"
|
||||
}
|
||||
|
||||
// webhookURLResetBody builds the POST body for --reset-url. Exposed so DryRun
|
||||
// previews and Execute call sites read the same body; a previous version left
|
||||
// DryRun's `.Body(...)` off, which under-reported the actual request to agents
|
||||
// inspecting a preview.
|
||||
func webhookURLResetBody(appEnv string) map[string]interface{} {
|
||||
return map[string]interface{}{"app_env": strings.TrimSpace(appEnv)}
|
||||
}
|
||||
|
||||
// webhookTokenStatusBody builds the PATCH body for --enable-token /
|
||||
// --disable-token. Same DryRun/Execute parity motive as webhookURLResetBody.
|
||||
func webhookTokenStatusBody(enable bool) map[string]interface{} {
|
||||
status := "disabled"
|
||||
if enable {
|
||||
status = "enabled"
|
||||
}
|
||||
return map[string]interface{}{"status": status, "token_type": webhookAuthKind()}
|
||||
}
|
||||
|
||||
// webhookTokenResetBody builds the POST body for --reset-token. Same
|
||||
// DryRun/Execute parity motive as webhookURLResetBody.
|
||||
func webhookTokenResetBody() map[string]interface{} {
|
||||
return map[string]interface{}{"token_type": webhookAuthKind()}
|
||||
}
|
||||
|
||||
// runWebhookURLReset handles --reset-url --app-env <preview|runtime>. Rotates the
|
||||
// hookKey for the given env; old URL invalidated immediately. New URL shown once.
|
||||
func runWebhookURLReset(rctx *common.RuntimeContext) error {
|
||||
appID, err := requireAppID(rctx.Str("app-id"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name := strings.TrimSpace(rctx.Str("name"))
|
||||
appEnv := strings.TrimSpace(rctx.Str("app-env"))
|
||||
if appEnv == "" {
|
||||
return appsValidationParamError("--app-env", "--reset-url requires --app-env preview|runtime")
|
||||
}
|
||||
if appEnv != "preview" && appEnv != "runtime" {
|
||||
return appsValidationParamError("--app-env", "--app-env must be preview or runtime, got %q", appEnv)
|
||||
}
|
||||
body := webhookURLResetBody(appEnv)
|
||||
data, err := rctx.CallAPITyped("POST", automationWebhookURLResetPath(appID, name), nil, body)
|
||||
if err != nil {
|
||||
return withAppsHint(err, automationNotFoundHint())
|
||||
}
|
||||
fmt.Fprintln(rctx.IO().ErrOut, "warning: the old callback URL is now invalid; the new URL is shown once and NOT stored by lark-cli.")
|
||||
rctx.OutFormat(data, nil, func(w io.Writer) {
|
||||
fmt.Fprintf(w, "new %s URL: %v (shown once)\n", appEnv, firstNonEmpty(
|
||||
common.GetString(data, appEnv+"_url"), common.GetString(data, "url")))
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// runWebhookTokenStatus handles --enable-token / --disable-token. Both map to the
|
||||
// same token/status endpoint. enable surfaces the plaintext token once.
|
||||
func runWebhookTokenStatus(rctx *common.RuntimeContext, enable bool) error {
|
||||
appID, err := requireAppID(rctx.Str("app-id"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name := strings.TrimSpace(rctx.Str("name"))
|
||||
body := webhookTokenStatusBody(enable)
|
||||
data, err := rctx.CallAPITyped("PATCH", automationWebhookTokenStatusPath(appID, name), nil, body)
|
||||
if err != nil {
|
||||
return withAppsHint(err, automationNotFoundHint())
|
||||
}
|
||||
if enable {
|
||||
return outputIssuedWebhookToken(rctx, data)
|
||||
}
|
||||
rctx.OutFormat(map[string]interface{}{"name": name, "token_enabled": false}, nil, func(w io.Writer) {
|
||||
fmt.Fprintf(w, "trigger %s: bearer token disabled (irreversible; callbacks no longer require a token)\n", name)
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// runWebhookTokenReset handles --reset-token. Rotates the token; old token invalidated.
|
||||
func runWebhookTokenReset(rctx *common.RuntimeContext) error {
|
||||
appID, err := requireAppID(rctx.Str("app-id"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
name := strings.TrimSpace(rctx.Str("name"))
|
||||
body := webhookTokenResetBody()
|
||||
data, err := rctx.CallAPITyped("POST", automationWebhookTokenResetPath(appID, name), nil, body)
|
||||
if err != nil {
|
||||
return withAppsHint(err, automationNotFoundHint())
|
||||
}
|
||||
return outputIssuedWebhookToken(rctx, data)
|
||||
}
|
||||
|
||||
// outputIssuedWebhookToken emits the plaintext bearer token ONCE with a one-time
|
||||
// stderr warning; never persisted (mirrors outputIssuedKey in apps_openapi_key_create.go).
|
||||
func outputIssuedWebhookToken(rctx *common.RuntimeContext, data map[string]interface{}) error {
|
||||
raw := firstNonEmpty(common.GetString(data, "token_value"), common.GetString(data, "token"))
|
||||
fmt.Fprintln(rctx.IO().ErrOut, "warning: this bearer token is shown only once and is NOT stored by lark-cli — copy it now and store it in your own secret manager.")
|
||||
out := map[string]interface{}{"token_value": raw, "token_enabled": true}
|
||||
rctx.OutFormat(out, nil, func(w io.Writer) {
|
||||
fmt.Fprintf(w, "bearer token: %v (shown once)\n", raw)
|
||||
})
|
||||
return nil
|
||||
}
|
||||
110
shortcuts/apps/apps_automation_webhook_test.go
Normal file
110
shortcuts/apps/apps_automation_webhook_test.go
Normal file
@@ -0,0 +1,110 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/larksuite/cli/internal/httpmock"
|
||||
)
|
||||
|
||||
// Flag-type identifiers used by the test flag-def map below. Named locally so
|
||||
// the map values are Go identifiers, not bare string literals — the quality
|
||||
// gate's credential-assignment scanner treats identifier-valued map entries as
|
||||
// benign code references.
|
||||
const (
|
||||
tfString = "string"
|
||||
tfBool = "bool"
|
||||
tfStringArray = "string_array"
|
||||
)
|
||||
|
||||
func automationUpdateFlagDefs() map[string]string {
|
||||
return map[string]string{
|
||||
"app-id": tfString, "name": tfString, "trigger-type": tfString, "description": tfString,
|
||||
"cron": tfString, "timezone": tfString, "white-ip-list": tfString,
|
||||
"table": tfString, "event": tfString, "fields": tfString,
|
||||
"approval-code": tfString, "event-type": tfString,
|
||||
"instance-status": tfStringArray, "task-status": tfStringArray,
|
||||
"reset-url": tfBool, "app-env": tfString,
|
||||
"enable-token": tfBool, "disable-token": tfBool, "reset-token": tfBool,
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhookResetURL_RequiresAppEnv(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "reset-url": "true"})
|
||||
err := runWebhookURLReset(rctx)
|
||||
assertValidationParamError(t, err, "--app-env")
|
||||
}
|
||||
|
||||
func TestWebhookResetURL_InvalidAppEnv(t *testing.T) {
|
||||
rctx, _, _ := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "reset-url": "true", "app-env": "prod"})
|
||||
err := runWebhookURLReset(rctx)
|
||||
assertValidationParamError(t, err, "--app-env")
|
||||
}
|
||||
|
||||
func TestWebhookResetURL_PostsAppEnv(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "reset-url": "true", "app-env": "preview"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "POST", URL: "/open-apis/spark/v1/apps/app_x/triggers/wh1/webhook/url/reset",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"preview_url": "https://new-preview"}},
|
||||
})
|
||||
if err := runWebhookURLReset(rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
if !strings.Contains(stdoutBuf.String(), "new-preview") {
|
||||
t.Errorf("reset-url must return new URL: %s", stdoutBuf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhookEnableToken_SurfacesTokenOnce(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "enable-token": "true"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "PATCH", URL: "/open-apis/spark/v1/apps/app_x/triggers/wh1/webhook/token/status",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"token_value": "test-token"}},
|
||||
})
|
||||
if err := runWebhookTokenStatus(rctx, true); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
out := stdoutBuf.String()
|
||||
if !strings.Contains(out, "test-token") {
|
||||
t.Errorf("enable-token must surface token once: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWebhookDisableToken covers the runWebhookTokenStatus(_, false) branch,
|
||||
// which posts the same endpoint with enabled=false and does NOT surface a token
|
||||
// (backend must not return a token_value when disabling).
|
||||
func TestWebhookDisableToken(t *testing.T) {
|
||||
rctx, _, reg := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "disable-token": "true"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "PATCH", URL: "/open-apis/spark/v1/apps/app_x/triggers/wh1/webhook/token/status",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"token_enabled": false}},
|
||||
})
|
||||
if err := runWebhookTokenStatus(rctx, false); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWebhookResetToken covers the reset-token endpoint: it must surface the
|
||||
// rotated token value once so operators can capture it.
|
||||
func TestWebhookResetToken(t *testing.T) {
|
||||
rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, automationUpdateFlagDefs(),
|
||||
map[string]string{"app-id": "app_x", "name": "wh1", "reset-token": "true"})
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "POST", URL: "/open-apis/spark/v1/apps/app_x/triggers/wh1/webhook/token/reset",
|
||||
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"token_value": "test-token"}},
|
||||
})
|
||||
if err := runWebhookTokenReset(rctx); err != nil {
|
||||
t.Fatalf("Execute() = %v", err)
|
||||
}
|
||||
if !strings.Contains(stdoutBuf.String(), "test-token") {
|
||||
t.Errorf("reset-token must surface rotated token once: %s", stdoutBuf.String())
|
||||
}
|
||||
}
|
||||
@@ -101,14 +101,16 @@ func TestAppsDBAuditEnable_DryRunAndSuccess(t *testing.T) {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
Data struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
} `json:"data"`
|
||||
}
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
a := env.API[0]
|
||||
a := env.Data.API[0]
|
||||
if a.Method != "POST" || a.URL != dbAuditSetURL || a.Body["enabled"] != true || a.Body["retention"] != "30d" || a.Body["table"] != "orders" {
|
||||
t.Fatalf("dry-run = %s %s body=%v", a.Method, a.URL, a.Body)
|
||||
}
|
||||
@@ -136,13 +138,15 @@ func TestAppsDBAuditDisable_DryRunAndSuccess(t *testing.T) {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
Data struct {
|
||||
API []struct {
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
} `json:"data"`
|
||||
}
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
if env.API[0].Body["enabled"] != false || env.API[0].Body["table"] != "orders" {
|
||||
t.Fatalf("dry-run body=%v (want enabled:false)", env.API[0].Body)
|
||||
if env.Data.API[0].Body["enabled"] != false || env.Data.API[0].Body["table"] != "orders" {
|
||||
t.Fatalf("dry-run body=%v (want enabled:false)", env.Data.API[0].Body)
|
||||
}
|
||||
|
||||
factory2, stdout2, reg := newAppsExecuteFactory(t)
|
||||
@@ -178,14 +182,16 @@ func TestAppsDBAuditList_DryRunJoinsTables(t *testing.T) {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
Data struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
} `json:"data"`
|
||||
}
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
a := env.API[0]
|
||||
a := env.Data.API[0]
|
||||
if a.Method != "GET" || a.URL != dbAuditListURL || a.Params["tables"] != "orders,users" {
|
||||
t.Fatalf("dry-run = %s %s tables=%v", a.Method, a.URL, a.Params["tables"])
|
||||
}
|
||||
|
||||
@@ -37,13 +37,7 @@ func TestAppsDBChangelogList_DryRunFiltersAndTimeNormalize(t *testing.T) {
|
||||
"--change-id", "01J", "--since", "2026-01-01", "--page-size", "5", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
a := env.API[0]
|
||||
if a.Method != "GET" || a.URL != dbChangelogURL {
|
||||
|
||||
@@ -71,13 +71,7 @@ func TestAppsDBDataExport_DryRunFormatFromOutput(t *testing.T) {
|
||||
if err := runAppsShortcut(t, AppsDBDataExport, args, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
a := env.API[0]
|
||||
if a.Method != "GET" || a.URL != dbDataExportURL {
|
||||
|
||||
@@ -97,14 +97,7 @@ func TestAppsDBDataImport_DryRunMultipartShape(t *testing.T) {
|
||||
[]string{"+db-data-import", "--app-id", "app_x", "--file", "orders.csv", "--environment", "dev", "--dry-run", "--yes", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
a := env.API[0]
|
||||
if a.Method != "POST" || a.URL != dbDataImportURL {
|
||||
@@ -131,12 +124,11 @@ func TestAppsDBDataImport_DryRunOmitsEnvWhenUnset(t *testing.T) {
|
||||
[]string{"+db-data-import", "--app-id", "app_x", "--file", "orders.csv", "--dry-run", "--yes", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
if len(env.API) != 1 {
|
||||
t.Fatalf("dry-run API calls = %d, want 1; stdout=%s", len(env.API), stdout.String())
|
||||
}
|
||||
p := env.API[0].Params
|
||||
if _, ok := p["env"]; ok {
|
||||
t.Fatalf("no --environment → env key must be omitted, got params=%v", p)
|
||||
@@ -174,11 +166,7 @@ func TestAppsDBDataImport_TableDefaultsToFileBasename(t *testing.T) {
|
||||
[]string{"+db-data-import", "--app-id", "app_x", "--file", "customers.json", "--dry-run", "--yes", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
if env.API[0].Params["table"] != "customers" {
|
||||
t.Fatalf("expected table=customers (from file basename) in params, got %v", env.API[0].Params)
|
||||
|
||||
@@ -30,13 +30,7 @@ func TestAppsDBEnvDiff_DryRunBody(t *testing.T) {
|
||||
[]string{"+db-env-diff", "--app-id", "app_x", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
a := env.API[0]
|
||||
if a.Method != "POST" || a.URL != dbEnvMigrateURL || a.Body["dry_run"] != true {
|
||||
@@ -91,11 +85,7 @@ func TestAppsDBEnvMigrate_DryRunBody(t *testing.T) {
|
||||
[]string{"+db-env-migrate", "--app-id", "app_x", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
if env.API[0].Body["dry_run"] != false {
|
||||
t.Fatalf("dry-run body=%v (want dry_run:false)", env.API[0].Body)
|
||||
@@ -180,13 +170,7 @@ func TestAppsDBRecoveryDiff_DryRunNormalizesTarget(t *testing.T) {
|
||||
[]string{"+db-recovery-diff", "--app-id", "app_x", "--target", "2026-04-15", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
a := env.API[0]
|
||||
if a.Method != "POST" || a.URL != dbRecoveryURL || a.Body["dry_run"] != true {
|
||||
@@ -331,14 +315,11 @@ func TestAppsDBQuotaGet_DryRunOmitsEnvWhenUnset(t *testing.T) {
|
||||
[]string{"+db-quota-get", "--app-id", "app_x", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
if len(env.API) != 1 {
|
||||
t.Fatalf("dry-run API calls = %d, want 1; stdout=%s", len(env.API), stdout.String())
|
||||
}
|
||||
a := env.API[0]
|
||||
if a.Method != "GET" || a.URL != dbQuotaURL {
|
||||
t.Fatalf("dry-run = %s %s", a.Method, a.URL)
|
||||
|
||||
@@ -165,14 +165,7 @@ func TestAppsDBExecute_DryRunSendsTransactionalFalse(t *testing.T) {
|
||||
factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
if err := json.Unmarshal([]byte(stdout.String()), &env); err != nil {
|
||||
t.Fatalf("decode: %v\n%s", err, stdout.String())
|
||||
}
|
||||
@@ -254,11 +247,7 @@ func TestAppsDBExecute_FileReadsSQLIntoBody(t *testing.T) {
|
||||
factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
if err := json.Unmarshal([]byte(stdout.String()), &env); err != nil {
|
||||
t.Fatalf("decode: %v\n%s", err, stdout.String())
|
||||
}
|
||||
|
||||
@@ -79,11 +79,7 @@ func TestAppsDBTableGet_NonPrettyFormatsOmitFormatQuery(t *testing.T) {
|
||||
if err := runAppsShortcut(t, AppsDBTableGet, args, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
if err := json.Unmarshal([]byte(stdout.String()), &env); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
|
||||
@@ -165,13 +165,7 @@ func TestAppsDBTableList_DryRunSendsPaginationAndEnv(t *testing.T) {
|
||||
factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
if err := json.Unmarshal([]byte(stdout.String()), &env); err != nil {
|
||||
t.Fatalf("decode dry-run: %v\n%s", err, stdout.String())
|
||||
}
|
||||
@@ -196,11 +190,7 @@ func TestAppsDBTableList_DoesNotSendIncludeStatsQuery(t *testing.T) {
|
||||
factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
if err := json.Unmarshal([]byte(stdout.String()), &env); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
|
||||
@@ -149,11 +149,7 @@ func TestAppsEnvVarList_DryRunIncludesScene(t *testing.T) {
|
||||
}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var dryRun struct {
|
||||
API []struct {
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var dryRun dryRunAPIEnvelope
|
||||
if err := json.Unmarshal(stdout.Bytes(), &dryRun); err != nil {
|
||||
t.Fatalf("decode dry-run: %v\n%s", err, stdout.String())
|
||||
}
|
||||
@@ -228,11 +224,7 @@ func TestAppsEnvVarSet_OnlineDryRunDoesNotRequireYes(t *testing.T) {
|
||||
t.Fatalf("dry-run missing %q: %s", want, got)
|
||||
}
|
||||
}
|
||||
var dryRun struct {
|
||||
API []struct {
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var dryRun dryRunAPIEnvelope
|
||||
if err := json.Unmarshal([]byte(got), &dryRun); err != nil {
|
||||
t.Fatalf("decode dry-run: %v\n%s", err, got)
|
||||
}
|
||||
@@ -353,13 +345,7 @@ func TestAppsEnvVarDelete_OnlineDryRunDoesNotRequireYes(t *testing.T) {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
|
||||
var dryRun struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var dryRun dryRunAPIEnvelope
|
||||
got := stdout.String()
|
||||
if err := json.Unmarshal([]byte(got), &dryRun); err != nil {
|
||||
t.Fatalf("decode dry-run: %v\n%s", err, got)
|
||||
|
||||
@@ -48,13 +48,7 @@ func TestAppsFileDelete_DryRunSendsPaths(t *testing.T) {
|
||||
[]string{"+file-delete", "--app-id", "app_x", "--path", "/a.png", "--path", "/b.png", "--yes", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
a := env.API[0]
|
||||
if a.Method != "POST" || a.URL != fileDeleteURL {
|
||||
|
||||
@@ -41,12 +41,7 @@ func TestAppsFileDownload_DryRunSignsFirst(t *testing.T) {
|
||||
[]string{"+file-download", "--app-id", "app_x", "--path", "/x.png", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
if env.API[0].Method != "POST" || env.API[0].URL != fileSignURLForDownload {
|
||||
t.Fatalf("dry-run = %s %s (want POST sign)", env.API[0].Method, env.API[0].URL)
|
||||
|
||||
@@ -46,13 +46,7 @@ func TestAppsFileGet_DryRunSendsPathQuery(t *testing.T) {
|
||||
[]string{"+file-get", "--app-id", "app_x", "--path", "/x.png", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
if env.API[0].Method != "GET" || env.API[0].URL != fileGetURL || env.API[0].Params["path"] != "/x.png" {
|
||||
t.Fatalf("dry-run = %s %s params=%v", env.API[0].Method, env.API[0].URL, env.API[0].Params)
|
||||
|
||||
@@ -95,13 +95,7 @@ func TestAppsFileList_DryRunSendsFiltersAndPagination(t *testing.T) {
|
||||
factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
if err := json.Unmarshal([]byte(stdout.String()), &env); err != nil {
|
||||
t.Fatalf("decode dry-run: %v\n%s", err, stdout.String())
|
||||
}
|
||||
@@ -139,11 +133,7 @@ func TestAppsFileList_DryRunOmitsEmptyFilters(t *testing.T) {
|
||||
[]string{"+file-list", "--app-id", "app_x", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Params map[string]interface{} `json:"params"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
for _, banned := range []string{"name", "path", "type", "size_gt", "size_lt", "uploaded_since", "uploaded_until", "page_token"} {
|
||||
if _, ok := env.API[0].Params[banned]; ok {
|
||||
|
||||
@@ -22,13 +22,7 @@ func TestAppsFileSign_DryRunBody(t *testing.T) {
|
||||
[]string{"+file-sign", "--app-id", "app_x", "--path", "/x.png", "--expires-in", "3600", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
a := env.API[0]
|
||||
if a.Method != "POST" || a.URL != fileSignURL || a.Body["path"] != "/x.png" {
|
||||
|
||||
@@ -76,13 +76,7 @@ func TestAppsFileUpload_DryRunPreUpload(t *testing.T) {
|
||||
[]string{"+file-upload", "--app-id", "app_x", "--file", "logo.png", "--dry-run", "--as", "user"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
_ = json.Unmarshal([]byte(stdout.String()), &env)
|
||||
a := env.API[0]
|
||||
if a.Method != "POST" || a.URL != "/open-apis/spark/v1/apps/app_x/storage/file_pre_upload" {
|
||||
|
||||
@@ -737,15 +737,12 @@ func newAppsExecuteFactoryWithStderr(t *testing.T) (*cmdutil.Factory, *bytes.Buf
|
||||
}
|
||||
|
||||
func TestAppsInit_Req1_Wording(t *testing.T) {
|
||||
// The --dry-run output is a flat object (DryRunAPI marshals to top-level keys
|
||||
// description/scaffold/api/...), NOT wrapped in {"data":...}, so parse stdout
|
||||
// directly rather than via parseEnvelopeData.
|
||||
factory, stdout, _ := newAppsExecuteFactoryWithStderr(t)
|
||||
if err := runAppsShortcut(t, AppsInit, []string{"+init", "--app-id", "app_x", "--as", "user", "--dry-run"}, factory, stdout); err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var data map[string]interface{}
|
||||
if err := json.Unmarshal(stdout.Bytes(), &data); err != nil {
|
||||
data, err := decodeDryRunDataMap(stdout.Bytes())
|
||||
if err != nil {
|
||||
t.Fatalf("decode dry-run output: %v (raw=%q)", err, stdout.String())
|
||||
}
|
||||
desc, _ := data["description"].(string)
|
||||
@@ -1447,8 +1444,8 @@ func TestAppsInit_DryRun_DescribesEnvPull(t *testing.T) {
|
||||
if err := runAppsShortcut(t, AppsInit, []string{"+init", "--app-id", "app_x", "--dir", dir, "--as", "user", "--dry-run"}, factory, stdout); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal(stdout.Bytes(), &m); err != nil {
|
||||
m, err := decodeDryRunDataMap(stdout.Bytes())
|
||||
if err != nil {
|
||||
t.Fatalf("decode dry-run: %v (raw=%q)", err, stdout.String())
|
||||
}
|
||||
ep, _ := m["env_pull"].(string)
|
||||
|
||||
@@ -25,13 +25,7 @@ func TestAppsLogList_DryRunBuildsSearchLogsBody(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
|
||||
t.Fatalf("decode dry-run: %v\n%s", err, stdout.String())
|
||||
}
|
||||
|
||||
@@ -39,13 +39,7 @@ func TestAppsMetricList_DryRunUsesSeconds(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
|
||||
t.Fatalf("decode dry-run: %v\n%s", err, stdout.String())
|
||||
}
|
||||
@@ -90,11 +84,7 @@ func TestAppsMetricList_AutoDownSampleByRange(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var env struct {
|
||||
API []struct {
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
|
||||
t.Fatalf("decode dry-run: %v\n%s", err, stdout.String())
|
||||
}
|
||||
|
||||
744
shortcuts/apps/apps_role.go
Normal file
744
shortcuts/apps/apps_role.go
Normal file
@@ -0,0 +1,744 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/internal/validate"
|
||||
"github.com/larksuite/cli/shortcuts/common"
|
||||
)
|
||||
|
||||
const maxRoleListScanPages = 1000
|
||||
|
||||
// AppsRoleList lists app roles.
|
||||
var AppsRoleList = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+role-list",
|
||||
Description: "List app roles",
|
||||
Risk: "read",
|
||||
Tips: []string{
|
||||
"Example: lark-cli apps +role-list --app-id <app_id>",
|
||||
"Example: lark-cli apps +role-list --app-id <app_id> --name Admin --page-size 20",
|
||||
"When only a role name is known, pass --name for exact matching; call +role-get only after resolving one unique role_id",
|
||||
"With --name, the CLI scans server pages in batches of 100, then applies --page-size and --page-token to the exact local matches",
|
||||
},
|
||||
Scopes: []string{"spark:app:read"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: roleAppIDRequiredDesc, Required: true},
|
||||
{Name: "name", Desc: "filter roles by exact name"},
|
||||
{Name: "page-size", Type: "int", Default: "20", Desc: "page size (1-100)"},
|
||||
{Name: "page-token", Desc: "integer offset returned by the previous role-list response"},
|
||||
},
|
||||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
if err := validateRoleAppID(rctx); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := buildRoleListParams(rctx)
|
||||
return err
|
||||
},
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
// Validate already ran and called buildRoleListParams; error is impossible here.
|
||||
params, _ := buildRoleListParams(rctx)
|
||||
params = roleListRequestParams(params, 0)
|
||||
return common.NewDryRunAPI().
|
||||
GET(roleListURL(rctx)).
|
||||
Desc("List app roles").
|
||||
Params(params)
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
params, err := buildRoleListParams(rctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := executeRoleList(rctx, params)
|
||||
if err != nil {
|
||||
return withRoleErrorHint(err, roleOperationList)
|
||||
}
|
||||
rctx.OutFormat(data, nil, func(w io.Writer) {
|
||||
renderRoleListPretty(w, common.GetSlice(data, "items"))
|
||||
})
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// AppsRoleGet gets one app role.
|
||||
var AppsRoleGet = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+role-get",
|
||||
Description: "Get an app role",
|
||||
Risk: "read",
|
||||
Tips: []string{
|
||||
"Example: lark-cli apps +role-get --app-id <app_id> --role-id <role_id>",
|
||||
"--role-id is not a human-readable role name; if only a name is known, run +role-list --name <exact_name> and use its unique returned role_id before calling +role-get",
|
||||
},
|
||||
Scopes: []string{"spark:app:read"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: roleAppIDRequiredDesc, Required: true},
|
||||
{Name: "role-id", Desc: roleIDRequiredDesc, Required: true},
|
||||
},
|
||||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
return validateRoleID(rctx)
|
||||
},
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
return common.NewDryRunAPI().
|
||||
GET(roleItemURL(rctx)).
|
||||
Desc("Get app role")
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
data, err := rctx.CallAPITyped("GET", roleItemURL(rctx), nil, nil)
|
||||
if err != nil {
|
||||
return withRoleErrorHint(err, roleOperationGet)
|
||||
}
|
||||
role, err := parseRoleDetailResponseData(data, roleID(rctx))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rctx.OutFormat(data, nil, func(w io.Writer) {
|
||||
renderRoleGetPretty(w, role)
|
||||
})
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// AppsRoleCreate creates an app role.
|
||||
var AppsRoleCreate = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+role-create",
|
||||
Description: "Create an app role",
|
||||
Risk: "write",
|
||||
Tips: []string{
|
||||
"Example: lark-cli apps +role-create --app-id <app_id> --name Admin",
|
||||
"Example: lark-cli apps +role-create --app-id <app_id> --name Admin --description 'Can manage orders'",
|
||||
"Example: lark-cli apps +role-create --app-id <app_id> --name Admin --role-id role_admin",
|
||||
"The create response returns data.role; run +role-get with data.role.role_id only when independent verification is required",
|
||||
},
|
||||
Scopes: []string{"spark:app:write"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: roleAppIDRequiredDesc, Required: true},
|
||||
// Keep --name in Validate so the CLI can return the command-specific
|
||||
// non-invention hint instead of Cobra's generic required-flag error.
|
||||
{Name: "name", Desc: "role name (required)"},
|
||||
{Name: "description", Desc: "role description"},
|
||||
{Name: "role-id", Desc: "optional caller-provided role ID ([A-Za-z0-9_-]{1,64})"},
|
||||
},
|
||||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
if err := validateRoleAppID(rctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(rctx.Str("name")) == "" {
|
||||
return appsValidationParamError("--name", "--name is required").
|
||||
WithHint("ask for the intended role name and pass it with --name; do not infer a name from --description")
|
||||
}
|
||||
if rctx.Changed("role-id") {
|
||||
roleID := strings.TrimSpace(rctx.Str("role-id"))
|
||||
if roleID == "" {
|
||||
return appsValidationParamError("--role-id", "--role-id must not be empty when provided")
|
||||
}
|
||||
return validateOptionalRoleID(roleID)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
return common.NewDryRunAPI().
|
||||
POST(roleListURL(rctx)).
|
||||
Desc("Create app role").
|
||||
Body(buildRoleCreateBody(rctx))
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
data, err := rctx.CallAPITyped("POST", roleListURL(rctx), nil, buildRoleCreateBody(rctx))
|
||||
if err != nil {
|
||||
return withRoleErrorHint(err, roleOperationCreate)
|
||||
}
|
||||
expectedRoleID := ""
|
||||
if rctx.Changed("role-id") {
|
||||
expectedRoleID = roleID(rctx)
|
||||
}
|
||||
role, err := parseRoleWriteResponseData(data, expectedRoleID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rctx.OutFormat(data, nil, func(w io.Writer) {
|
||||
renderRoleCreatePretty(w, role)
|
||||
})
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// AppsRoleUpdate updates an app role.
|
||||
var AppsRoleUpdate = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+role-update",
|
||||
Description: "Update an app role",
|
||||
Risk: "write",
|
||||
Tips: []string{
|
||||
"Example: lark-cli apps +role-update --app-id <app_id> --role-id <role_id> --name Operator",
|
||||
},
|
||||
Scopes: []string{"spark:app:write"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: roleAppIDRequiredDesc, Required: true},
|
||||
{Name: "role-id", Desc: roleIDRequiredDesc, Required: true},
|
||||
{Name: "name", Desc: "new role name"},
|
||||
{Name: "description", Desc: "new role description"},
|
||||
},
|
||||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
if err := validateRoleID(rctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if rctx.Changed("name") && strings.TrimSpace(rctx.Str("name")) == "" {
|
||||
return appsValidationParamError("--name", "--name must not be empty when provided").
|
||||
WithHint("omit --name if only updating --description")
|
||||
}
|
||||
if !rctx.Changed("name") && !rctx.Changed("description") {
|
||||
reason := "provide at least one of --name or --description"
|
||||
return appsValidationError("at least one of --name or --description is required").
|
||||
WithParams(
|
||||
appsInvalidParam("--name", reason),
|
||||
appsInvalidParam("--description", reason),
|
||||
).
|
||||
WithHint("provide --name, --description, or both")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
return common.NewDryRunAPI().
|
||||
PATCH(roleItemURL(rctx)).
|
||||
Desc("Update app role").
|
||||
Body(buildRoleUpdateBody(rctx))
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
data, err := rctx.CallAPITyped("PATCH", roleItemURL(rctx), nil, buildRoleUpdateBody(rctx))
|
||||
if err != nil {
|
||||
return withRoleErrorHint(err, roleOperationUpdate)
|
||||
}
|
||||
role, err := parseRoleWriteResponseData(data, roleID(rctx))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rctx.OutFormat(data, nil, func(w io.Writer) {
|
||||
renderRoleUpdatePretty(w, role)
|
||||
})
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// AppsRoleDelete deletes an app role.
|
||||
var AppsRoleDelete = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+role-delete",
|
||||
Description: "Delete an app role",
|
||||
Risk: "high-risk-write",
|
||||
Tips: []string{
|
||||
"Example: lark-cli apps +role-delete --app-id <app_id> --role-id <role_id> --yes",
|
||||
"A delete request alone is not explicit confirmation: first show the exact app, role, current member scope, and irreversible impact; use --yes only after the user confirms that impact",
|
||||
"When independent verification is required, use +role-list --name <exact_name> and confirm the deleted role_id is absent; a failed +role-get alone does not prove deletion",
|
||||
},
|
||||
Scopes: []string{"spark:app:write"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: roleAppIDRequiredDesc, Required: true},
|
||||
{Name: "role-id", Desc: roleIDRequiredDesc, Required: true},
|
||||
},
|
||||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
return validateRoleID(rctx)
|
||||
},
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
return common.NewDryRunAPI().
|
||||
DELETE(roleItemURL(rctx)).
|
||||
Desc("Delete app role")
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
data, err := rctx.CallAPITyped("DELETE", roleItemURL(rctx), nil, nil)
|
||||
if err != nil {
|
||||
return withRoleErrorHint(err, roleOperationDelete)
|
||||
}
|
||||
deletedRoleID := roleID(rctx)
|
||||
out, err := normalizeRoleDeleteData(data, deletedRoleID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rctx.OutFormat(out, nil, func(w io.Writer) {
|
||||
renderRoleDeletePretty(w, common.GetString(out, "role_id"))
|
||||
})
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func roleListURL(rctx *common.RuntimeContext) string {
|
||||
appID := roleAppID(rctx)
|
||||
return fmt.Sprintf(roleListPath, validate.EncodePathSegment(appID))
|
||||
}
|
||||
|
||||
func roleItemURL(rctx *common.RuntimeContext) string {
|
||||
appID := roleAppID(rctx)
|
||||
roleID := roleID(rctx)
|
||||
return fmt.Sprintf(roleItemPath, validate.EncodePathSegment(appID), validate.EncodePathSegment(roleID))
|
||||
}
|
||||
|
||||
func buildRoleListParams(rctx *common.RuntimeContext) (map[string]interface{}, error) {
|
||||
params, err := buildRolePageParams(rctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
name := strings.TrimSpace(rctx.Str("name"))
|
||||
if rctx.Changed("name") && name == "" {
|
||||
return nil, appsValidationParamError("--name", "--name must not be empty when provided").
|
||||
WithHint("omit --name to list all roles, or provide the exact role name to resolve")
|
||||
}
|
||||
if name != "" {
|
||||
params["name"] = name
|
||||
}
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// roleListRequestParams returns the query parameters for one actual backend
|
||||
// request. Exact-name lookup always starts from server offset zero and scans in
|
||||
// maximum-sized batches; the caller's limit/offset are applied to local matches.
|
||||
func roleListRequestParams(params map[string]interface{}, page int) map[string]interface{} {
|
||||
name, _ := params["name"].(string)
|
||||
if name == "" {
|
||||
return params
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"limit": maxRolePageSize,
|
||||
"offset": page * maxRolePageSize,
|
||||
"name": name,
|
||||
}
|
||||
}
|
||||
|
||||
func buildRoleCreateBody(rctx *common.RuntimeContext) map[string]interface{} {
|
||||
body := map[string]interface{}{
|
||||
"name": strings.TrimSpace(rctx.Str("name")),
|
||||
}
|
||||
if rctx.Changed("description") {
|
||||
body["description"] = strings.TrimSpace(rctx.Str("description"))
|
||||
}
|
||||
if rctx.Changed("role-id") {
|
||||
if roleID := strings.TrimSpace(rctx.Str("role-id")); roleID != "" {
|
||||
body["role_id"] = roleID
|
||||
}
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
func buildRoleUpdateBody(rctx *common.RuntimeContext) map[string]interface{} {
|
||||
body := map[string]interface{}{}
|
||||
if rctx.Changed("name") {
|
||||
body["name"] = strings.TrimSpace(rctx.Str("name"))
|
||||
}
|
||||
if rctx.Changed("description") {
|
||||
body["description"] = strings.TrimSpace(rctx.Str("description"))
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
// executeRoleList compensates for Miaoda environments that accept the name
|
||||
// query parameter but ignore it. A name lookup scans the complete server-side
|
||||
// result set, applies exact matching locally, and then applies the CLI's
|
||||
// offset/limit contract to the filtered result.
|
||||
func executeRoleList(rctx *common.RuntimeContext, params map[string]interface{}) (map[string]interface{}, error) {
|
||||
name, _ := params["name"].(string)
|
||||
if name == "" {
|
||||
data, err := rctx.CallAPITyped("GET", roleListURL(rctx), params, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return normalizeRoleListData(data, params)
|
||||
}
|
||||
|
||||
requestedLimit := roleIntValue(params["limit"])
|
||||
requestedOffset := roleIntValue(params["offset"])
|
||||
allMatches := make([]interface{}, 0, requestedLimit)
|
||||
var firstPage map[string]interface{}
|
||||
seenRoleIDs := map[string]struct{}{}
|
||||
seenPageSignatures := map[string]struct{}{}
|
||||
expectedTotal := -1
|
||||
scannedRoleCount := 0
|
||||
|
||||
for page := 0; ; page++ {
|
||||
if page >= maxRoleListScanPages {
|
||||
return nil, errs.NewInternalError(
|
||||
errs.SubtypeInvalidResponse,
|
||||
"role list exceeded %d pages while filtering by name",
|
||||
maxRoleListScanPages,
|
||||
).WithHint("retry without --name and paginate using the returned page_token")
|
||||
}
|
||||
|
||||
scanParams := roleListRequestParams(params, page)
|
||||
data, err := rctx.CallAPITyped("GET", roleListURL(rctx), scanParams, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if firstPage == nil {
|
||||
firstPage = data
|
||||
}
|
||||
items, hasMore, total, err := parseRoleListPage(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if expectedTotal < 0 {
|
||||
expectedTotal = total
|
||||
} else if total != expectedTotal {
|
||||
return nil, roleListProgressError("role list total changed across pages while filtering by name")
|
||||
}
|
||||
if scannedRoleCount+len(items) > expectedTotal {
|
||||
return nil, roleListProgressError("role list returned more roles than its total while filtering by name")
|
||||
}
|
||||
scannedRoleCount += len(items)
|
||||
if hasMore && scannedRoleCount >= expectedTotal {
|
||||
return nil, roleListProgressError("role list reported more pages after reaching its total while filtering by name")
|
||||
}
|
||||
if !hasMore && scannedRoleCount != expectedTotal {
|
||||
return nil, roleListProgressError("role list ended before returning its declared total while filtering by name")
|
||||
}
|
||||
signature, newRoleCount, err := roleListPageProgress(items, seenRoleIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if newRoleCount != len(items) {
|
||||
return nil, roleListProgressError("role list repeated roles across pages while filtering by name")
|
||||
}
|
||||
if _, duplicate := seenPageSignatures[signature]; duplicate {
|
||||
return nil, roleListProgressError("role list repeated a page while filtering by name")
|
||||
}
|
||||
seenPageSignatures[signature] = struct{}{}
|
||||
if hasMore && (len(items) == 0 || newRoleCount == 0) {
|
||||
return nil, roleListProgressError("role list reported more pages without returning new roles")
|
||||
}
|
||||
for _, item := range items {
|
||||
role, ok := item.(map[string]interface{})
|
||||
if ok && common.GetString(role, "name") == name {
|
||||
allMatches = append(allMatches, item)
|
||||
}
|
||||
}
|
||||
if !hasMore {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if firstPage == nil {
|
||||
firstPage = map[string]interface{}{}
|
||||
}
|
||||
return normalizeFilteredRoleListData(firstPage, allMatches, requestedOffset, requestedLimit), nil
|
||||
}
|
||||
|
||||
func normalizeFilteredRoleListData(data map[string]interface{}, matches []interface{}, offset, limit int) map[string]interface{} {
|
||||
out := map[string]interface{}{}
|
||||
for k, v := range data {
|
||||
out[k] = v
|
||||
}
|
||||
|
||||
start := offset
|
||||
if start > len(matches) {
|
||||
start = len(matches)
|
||||
}
|
||||
end := start + limit
|
||||
if end > len(matches) {
|
||||
end = len(matches)
|
||||
}
|
||||
hasMore := end < len(matches)
|
||||
items := append([]interface{}(nil), matches[start:end]...)
|
||||
if items == nil {
|
||||
items = []interface{}{}
|
||||
}
|
||||
out["items"] = items
|
||||
out["has_more"] = hasMore
|
||||
out["page_token"] = roleNextPageToken(start, limit, hasMore)
|
||||
out["total"] = len(matches)
|
||||
return out
|
||||
}
|
||||
|
||||
func normalizeRoleListData(data map[string]interface{}, params map[string]interface{}) (map[string]interface{}, error) {
|
||||
items, hasMore, total, err := parseRoleListPage(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := map[string]interface{}{}
|
||||
for k, v := range data {
|
||||
out[k] = v
|
||||
}
|
||||
|
||||
limit := roleIntValue(params["limit"])
|
||||
offset := roleIntValue(params["offset"])
|
||||
|
||||
out["items"] = items
|
||||
out["has_more"] = hasMore
|
||||
out["page_token"] = roleNextPageToken(offset, limit, hasMore)
|
||||
out["total"] = total
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func parseRoleListPage(data map[string]interface{}) ([]interface{}, bool, int, error) {
|
||||
rawItems, hasItems := data["items"]
|
||||
items, ok := rawItems.([]interface{})
|
||||
if !hasItems || !ok {
|
||||
return nil, false, 0, errs.NewInternalError(
|
||||
errs.SubtypeInvalidResponse,
|
||||
"role list response field items must be an array",
|
||||
).WithHint("retry the read; do not treat a missing or malformed role list as empty")
|
||||
}
|
||||
if err := validateRoleCollection(items, "role list response field items"); err != nil {
|
||||
return nil, false, 0, err
|
||||
}
|
||||
rawHasMore, hasHasMore := data["has_more"]
|
||||
hasMore, ok := rawHasMore.(bool)
|
||||
if !hasHasMore || !ok {
|
||||
return nil, false, 0, errs.NewInternalError(
|
||||
errs.SubtypeInvalidResponse,
|
||||
"role list response field has_more must be a boolean",
|
||||
).WithHint("retry the read; pagination is incomplete without a valid has_more value")
|
||||
}
|
||||
total, ok := nonNegativeRoleInteger(data["total"])
|
||||
if _, exists := data["total"]; !exists || !ok {
|
||||
return nil, false, 0, errs.NewInternalError(
|
||||
errs.SubtypeInvalidResponse,
|
||||
"role list response field total must be a non-negative integer",
|
||||
).WithHint("retry the read; do not infer a role count from a missing or malformed total value")
|
||||
}
|
||||
return items, hasMore, total, nil
|
||||
}
|
||||
|
||||
func roleListPageProgress(items []interface{}, seenRoleIDs map[string]struct{}) (string, int, error) {
|
||||
roleIDs := make([]string, 0, len(items))
|
||||
newRoleCount := 0
|
||||
for index, item := range items {
|
||||
_, roleID, err := roleCollectionItem(item, "role list response field items", index)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
roleIDs = append(roleIDs, roleID)
|
||||
if _, seen := seenRoleIDs[roleID]; !seen {
|
||||
seenRoleIDs[roleID] = struct{}{}
|
||||
newRoleCount++
|
||||
}
|
||||
}
|
||||
return strings.Join(roleIDs, "\x00"), newRoleCount, nil
|
||||
}
|
||||
|
||||
func nonNegativeRoleInteger(value interface{}) (int, bool) {
|
||||
maxInt := uint64(^uint(0) >> 1)
|
||||
toInt := func(value int64) (int, bool) {
|
||||
if value < 0 || uint64(value) > maxInt {
|
||||
return 0, false
|
||||
}
|
||||
return int(value), true
|
||||
}
|
||||
|
||||
switch value := value.(type) {
|
||||
case int:
|
||||
if value < 0 {
|
||||
return 0, false
|
||||
}
|
||||
return value, true
|
||||
case int64:
|
||||
return toInt(value)
|
||||
case float64:
|
||||
maxIntExclusive := math.Ldexp(1, strconv.IntSize-1)
|
||||
if math.IsNaN(value) || math.IsInf(value, 0) || value < 0 || math.Trunc(value) != value || value >= maxIntExclusive {
|
||||
return 0, false
|
||||
}
|
||||
return int(value), true
|
||||
case json.Number:
|
||||
parsed, err := value.Int64()
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return toInt(parsed)
|
||||
case string:
|
||||
if value == "" || strings.IndexFunc(value, func(r rune) bool {
|
||||
return r < '0' || r > '9'
|
||||
}) >= 0 {
|
||||
return 0, false
|
||||
}
|
||||
parsed, err := strconv.ParseUint(value, 10, strconv.IntSize)
|
||||
if err != nil || parsed > maxInt {
|
||||
return 0, false
|
||||
}
|
||||
return int(parsed), true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
func roleListProgressError(message string) error {
|
||||
return errs.NewInternalError(errs.SubtypeInvalidResponse, message).
|
||||
WithHint("retry without --name and paginate manually; do not continue an incomplete exact-name scan")
|
||||
}
|
||||
|
||||
func normalizeRoleDeleteData(data map[string]interface{}, requestedRoleID string) (map[string]interface{}, error) {
|
||||
if data == nil {
|
||||
return nil, invalidRoleDeleteResponse("role delete response data must be an object")
|
||||
}
|
||||
if len(data) == 0 {
|
||||
return map[string]interface{}{
|
||||
"role_id": requestedRoleID,
|
||||
"deleted": true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
out := map[string]interface{}{}
|
||||
for k, v := range data {
|
||||
out[k] = v
|
||||
}
|
||||
rawRoleID, ok := out["role_id"]
|
||||
if !ok {
|
||||
return nil, invalidRoleDeleteResponse("role delete response is missing role_id")
|
||||
}
|
||||
actualRoleID, stringOK := rawRoleID.(string)
|
||||
if !stringOK || actualRoleID != requestedRoleID {
|
||||
return nil, invalidRoleDeleteResponse(
|
||||
"role delete response role_id does not match requested role_id %q",
|
||||
requestedRoleID,
|
||||
)
|
||||
}
|
||||
rawDeleted, ok := out["deleted"]
|
||||
if !ok {
|
||||
return nil, invalidRoleDeleteResponse("role delete response is missing deleted")
|
||||
}
|
||||
deleted, boolOK := rawDeleted.(bool)
|
||||
if !boolOK || !deleted {
|
||||
return nil, invalidRoleDeleteResponse("role delete response did not acknowledge deletion")
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
type roleResponseData struct {
|
||||
RoleID string
|
||||
Name string
|
||||
Description string
|
||||
}
|
||||
|
||||
func parseRoleDetailResponseData(data map[string]interface{}, expectedRoleID string) (roleResponseData, error) {
|
||||
return parseRoleResponseData(data, expectedRoleID, true)
|
||||
}
|
||||
|
||||
func parseRoleWriteResponseData(data map[string]interface{}, expectedRoleID string) (roleResponseData, error) {
|
||||
return parseRoleResponseData(data, expectedRoleID, false)
|
||||
}
|
||||
|
||||
func parseRoleResponseData(data map[string]interface{}, expectedRoleID string, requireName bool) (roleResponseData, error) {
|
||||
if data == nil {
|
||||
return roleResponseData{}, invalidRoleResponse("role response data must be an object")
|
||||
}
|
||||
rawRole, exists := data["role"]
|
||||
role, ok := rawRole.(map[string]interface{})
|
||||
if !exists || !ok || role == nil {
|
||||
return roleResponseData{}, invalidRoleResponse("role response field role must be an object")
|
||||
}
|
||||
rawRoleID, exists := role["role_id"]
|
||||
roleID, ok := rawRoleID.(string)
|
||||
roleID = strings.TrimSpace(roleID)
|
||||
if !exists || !ok || roleID == "" {
|
||||
return roleResponseData{}, invalidRoleResponse("role response field role.role_id must be a non-empty string")
|
||||
}
|
||||
if expectedRoleID != "" && roleID != expectedRoleID {
|
||||
return roleResponseData{}, invalidRoleResponse(
|
||||
"role response role_id %q does not match requested role_id %q",
|
||||
roleID,
|
||||
expectedRoleID,
|
||||
)
|
||||
}
|
||||
rawName, nameExists := role["name"]
|
||||
name, nameOK := rawName.(string)
|
||||
name = strings.TrimSpace(name)
|
||||
if requireName && !nameExists {
|
||||
return roleResponseData{}, invalidRoleResponse("role response field role.name must be a non-empty string")
|
||||
}
|
||||
if nameExists && (!nameOK || name == "") {
|
||||
return roleResponseData{}, invalidRoleResponse("role response field role.name must be a non-empty string")
|
||||
}
|
||||
rawDescription, descriptionExists := role["description"]
|
||||
description, descriptionOK := rawDescription.(string)
|
||||
if descriptionExists && !descriptionOK {
|
||||
return roleResponseData{}, invalidRoleResponse("role response field role.description must be a string")
|
||||
}
|
||||
return roleResponseData{RoleID: roleID, Name: name, Description: description}, nil
|
||||
}
|
||||
|
||||
func invalidRoleResponse(message string, args ...interface{}) error {
|
||||
return errs.NewInternalError(errs.SubtypeInvalidResponse, message, args...).
|
||||
WithHint("retry the role read; do not treat a missing or malformed role as a successful result")
|
||||
}
|
||||
|
||||
func invalidRoleDeleteResponse(message string, args ...interface{}) error {
|
||||
return errs.NewInternalError(errs.SubtypeInvalidResponse, message, args...).
|
||||
WithHint("do not claim deletion; verify the target role with +role-list --name <exact_name>")
|
||||
}
|
||||
|
||||
func roleIntValue(value interface{}) int {
|
||||
switch v := value.(type) {
|
||||
case int:
|
||||
return v
|
||||
case int64:
|
||||
return int(v)
|
||||
case float64:
|
||||
return int(v)
|
||||
case json.Number:
|
||||
i, err := strconv.Atoi(v.String())
|
||||
if err == nil {
|
||||
return i
|
||||
}
|
||||
case string:
|
||||
i, err := strconv.Atoi(strings.TrimSpace(v))
|
||||
if err == nil {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func renderRoleCreatePretty(w io.Writer, role roleResponseData) {
|
||||
fmt.Fprintf(w, "Created role %s\n", roleDisplayValue(role.RoleID))
|
||||
}
|
||||
|
||||
func renderRoleGetPretty(w io.Writer, role roleResponseData) {
|
||||
renderRoleDetailPretty(w, role)
|
||||
}
|
||||
|
||||
func renderRoleUpdatePretty(w io.Writer, role roleResponseData) {
|
||||
fmt.Fprintf(w, "Updated role %s\n", roleDisplayValue(role.RoleID))
|
||||
}
|
||||
|
||||
func renderRoleDeletePretty(w io.Writer, roleID string) {
|
||||
fmt.Fprintf(w, "Deleted role %s\n", roleDisplayValue(roleID))
|
||||
}
|
||||
|
||||
func renderRoleDetailPretty(w io.Writer, role roleResponseData) {
|
||||
fmt.Fprintf(w, "role_id: %s\n", roleDisplayValue(role.RoleID))
|
||||
fmt.Fprintf(w, "name: %s\n", roleDisplayValue(role.Name))
|
||||
fmt.Fprintf(w, "description: %s\n", roleDisplayValue(role.Description))
|
||||
}
|
||||
|
||||
func renderRoleListPretty(w io.Writer, items []interface{}) {
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
|
||||
fmt.Fprintln(tw, "ROLE ID\tNAME\tDESCRIPTION")
|
||||
for _, item := range items {
|
||||
role, ok := item.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(tw, "%s\t%s\t%s\n",
|
||||
roleDisplayValue(firstNonEmpty(common.GetString(role, "role_id"), common.GetString(role, "id"))),
|
||||
roleDisplayValue(common.GetString(role, "name")),
|
||||
roleDisplayValue(common.GetString(role, "description")))
|
||||
}
|
||||
_ = tw.Flush()
|
||||
}
|
||||
490
shortcuts/apps/apps_role_common.go
Normal file
490
shortcuts/apps/apps_role_common.go
Normal file
@@ -0,0 +1,490 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/internal/errclass"
|
||||
"github.com/larksuite/cli/internal/validate"
|
||||
"github.com/larksuite/cli/shortcuts/common"
|
||||
)
|
||||
|
||||
const (
|
||||
roleListPath = apiBasePath + "/apps/%s/roles"
|
||||
roleItemPath = apiBasePath + "/apps/%s/roles/%s"
|
||||
roleMemberListPath = apiBasePath + "/apps/%s/roles/%s/member_list"
|
||||
roleMemberAddPath = apiBasePath + "/apps/%s/roles/%s/member_add"
|
||||
roleMemberRemovePath = apiBasePath + "/apps/%s/roles/%s/member_remove"
|
||||
roleMatchListPath = apiBasePath + "/apps/%s/user_role_list"
|
||||
defaultRolePageSize = 20
|
||||
maxRolePageSize = 100
|
||||
maxRoleMembers = 100
|
||||
|
||||
roleErrInvalidParameters = 3340001
|
||||
roleErrUserLimitExceeded = 3344027
|
||||
roleErrDepartmentLimitExceeded = 3344028
|
||||
roleErrChatLimitExceeded = 3344029
|
||||
roleErrAdminRequired = 3344030
|
||||
roleErrManagerRequired = 3344031
|
||||
roleErrInvalidRoleID = 3344034
|
||||
roleErrRoleNotFound = 3344035
|
||||
roleErrRoleAlreadyExists = 3344036
|
||||
roleErrRoleLimitExceeded = 3344037
|
||||
roleErrInvalidRoleName = 3344038
|
||||
roleErrInvalidRoleDescription = 3344039
|
||||
roleErrUnsupportedMemberType = 3344040
|
||||
roleErrInvalidMemberID = 3344041
|
||||
)
|
||||
|
||||
var optionalRoleIDPattern = regexp.MustCompile(`^[A-Za-z0-9_-]{1,64}$`)
|
||||
|
||||
const (
|
||||
roleAppHint = "verify --app-id is a Miaoda app_id you can access; list apps with `lark-cli apps +list`"
|
||||
roleItemHint = "verify --role-id belongs to the app; if you only know a role name, resolve it with `lark-cli apps +role-list --app-id <app_id> --name <exact_name>` and use the unique returned role_id"
|
||||
roleCreateHint = "verify --app-id and role fields; omit --role-id unless you need a caller-provided role ID"
|
||||
roleMemberHint = "verify --role-id and member IDs; use user open_id, open_department_id, or open_chat_id values"
|
||||
roleMatchHint = "use --user-id with a user open_id; do not pass role_id or enumerate roles manually"
|
||||
|
||||
roleAppIDRequiredDesc = "Miaoda app ID (required; app_...; use apps +list to find it)"
|
||||
roleIDRequiredDesc = "role ID (required; [A-Za-z0-9_-]{1,64}; use role-list to find it)"
|
||||
roleUserIDRequiredDesc = "user open ID (required; ou_...; do not pass a role ID, name, or email)"
|
||||
)
|
||||
|
||||
type roleErrorOperation uint8
|
||||
|
||||
const (
|
||||
roleOperationList roleErrorOperation = iota
|
||||
roleOperationGet
|
||||
roleOperationCreate
|
||||
roleOperationUpdate
|
||||
roleOperationDelete
|
||||
roleOperationMemberList
|
||||
roleOperationMemberAdd
|
||||
roleOperationMemberRemove
|
||||
roleOperationMatchList
|
||||
)
|
||||
|
||||
type roleMemberGroups struct {
|
||||
Users []string `json:"users"`
|
||||
Departments []string `json:"departments"`
|
||||
Chats []string `json:"chats"`
|
||||
}
|
||||
|
||||
type roleMemberKind struct {
|
||||
memberType string
|
||||
dataKey string
|
||||
flagName string
|
||||
prefix string
|
||||
}
|
||||
|
||||
var roleMemberKinds = []roleMemberKind{
|
||||
{memberType: "user", dataKey: "users", flagName: "--users", prefix: "ou_"},
|
||||
{memberType: "department", dataKey: "departments", flagName: "--departments", prefix: "od-"},
|
||||
{memberType: "chat", dataKey: "chats", flagName: "--chats", prefix: "oc_"},
|
||||
}
|
||||
|
||||
func roleAppID(rctx *common.RuntimeContext) string {
|
||||
return strings.TrimSpace(rctx.Str("app-id"))
|
||||
}
|
||||
|
||||
func roleID(rctx *common.RuntimeContext) string {
|
||||
return strings.TrimSpace(rctx.Str("role-id"))
|
||||
}
|
||||
|
||||
func validateRoleAppID(rctx *common.RuntimeContext) error {
|
||||
appID := roleAppID(rctx)
|
||||
if appID == "" {
|
||||
return appsValidationParamError("--app-id", "--app-id is required").
|
||||
WithHint("list your apps with `lark-cli apps +list`")
|
||||
}
|
||||
if strings.HasPrefix(appID, "cli_") {
|
||||
return appsValidationParamError("--app-id", "--app-id must be a Miaoda app_id, not a Lark app_id").
|
||||
WithHint("pass the app_... value from `lark-cli apps +list`, not the cli_... credential app id")
|
||||
}
|
||||
if !strings.HasPrefix(appID, "app_") || len(appID) == len("app_") {
|
||||
return appsValidationParamError("--app-id", "--app-id must be a Miaoda app_id starting with app_").
|
||||
WithHint("list Miaoda apps with `lark-cli apps +list`, then pass the returned app_id")
|
||||
}
|
||||
// app-id must not contain forward slashes (apps are identified by app_xxx IDs).
|
||||
for _, r := range appID {
|
||||
if r == '/' || r == '\\' || unicode.IsSpace(r) || unicode.IsControl(r) {
|
||||
return appsValidationParamError("--app-id", "--app-id must not contain slashes, whitespace, or control characters")
|
||||
}
|
||||
}
|
||||
// Defense-in-depth: block path traversal and URL metacharacters.
|
||||
if err := validateRolePathSegmentSafe(appID, "--app-id"); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateRoleID(rctx *common.RuntimeContext) error {
|
||||
if err := validateRoleAppID(rctx); err != nil {
|
||||
return err
|
||||
}
|
||||
roleID := roleID(rctx)
|
||||
if roleID == "" {
|
||||
return appsValidationParamError("--role-id", "--role-id is required").
|
||||
WithHint("list roles with `lark-cli apps +role-list --app-id <app_id>`")
|
||||
}
|
||||
return validateExistingRoleIDValue(roleID)
|
||||
}
|
||||
|
||||
// validateRolePathSegmentSafe rejects path-traversal segments ("..") and URL
|
||||
// metacharacters (? # %) in values interpolated into a URL path, providing
|
||||
// defense-in-depth alongside validate.EncodePathSegment.
|
||||
func validateRolePathSegmentSafe(value, flagName string) error {
|
||||
for _, seg := range strings.Split(value, "/") {
|
||||
if seg == ".." {
|
||||
return appsValidationParamError(flagName, "%s must not contain '..' path traversal", flagName).
|
||||
WithHint("provide a valid %s without path traversal", flagName)
|
||||
}
|
||||
}
|
||||
if strings.ContainsAny(value, "?#%") {
|
||||
return appsValidationParamError(flagName, "%s contains invalid URL characters (?, #, %%)", flagName).
|
||||
WithHint("provide a valid %s without URL metacharacters", flagName)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateOptionalRoleID(roleID string) error {
|
||||
roleID = strings.TrimSpace(roleID)
|
||||
if roleID == "" {
|
||||
return nil
|
||||
}
|
||||
return validateCreateRoleIDValue(roleID)
|
||||
}
|
||||
|
||||
func validateCreateRoleIDValue(roleID string) error {
|
||||
if !optionalRoleIDPattern.MatchString(roleID) {
|
||||
return appsValidationParamError("--role-id", "--role-id must match [A-Za-z0-9_-]{1,64}").
|
||||
WithHint("omit --role-id to let the server generate one")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateExistingRoleIDValue(roleID string) error {
|
||||
if !optionalRoleIDPattern.MatchString(roleID) {
|
||||
return appsValidationParamError("--role-id", "--role-id must match [A-Za-z0-9_-]{1,64}").
|
||||
WithHint("resolve the role with `lark-cli apps +role-list --app-id <app_id> --name <exact_name>` and pass its role_id")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildRolePageParams(rctx *common.RuntimeContext) (map[string]interface{}, error) {
|
||||
limit := defaultRolePageSize
|
||||
if rctx.Changed("page-size") {
|
||||
limit = rctx.Int("page-size")
|
||||
}
|
||||
if limit < 1 || limit > maxRolePageSize {
|
||||
return nil, appsValidationParamError("--page-size", "--page-size must be between 1 and %d", maxRolePageSize).
|
||||
WithHint("use --page-size between 1 and 100")
|
||||
}
|
||||
|
||||
offset := 0
|
||||
pageToken := strings.TrimSpace(rctx.Str("page-token"))
|
||||
if pageToken != "" {
|
||||
parsedOffset, err := strconv.Atoi(pageToken)
|
||||
if err != nil || parsedOffset < 0 {
|
||||
return nil, appsValidationParamError("--page-token", "--page-token must be a non-negative integer offset").
|
||||
WithHint("reuse page_token from the previous +role-list response")
|
||||
}
|
||||
offset = parsedOffset
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func roleNextPageToken(offset, limit int, hasMore bool) string {
|
||||
if !hasMore {
|
||||
return ""
|
||||
}
|
||||
return strconv.Itoa(offset + limit)
|
||||
}
|
||||
|
||||
func splitRoleMemberCSV(s, flagName string) ([]string, error) {
|
||||
parts := strings.Split(s, ",")
|
||||
values := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
value := strings.TrimSpace(part)
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
// Reject values containing whitespace, control characters, or URL metacharacters
|
||||
// (member IDs are open_id/open_department_id/open_chat_id which are safe tokens).
|
||||
if err := validateMemberID(value, flagName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values = append(values, value)
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
// validateMemberID rejects values containing characters that are invalid in
|
||||
// open_id / open_department_id / open_chat_id tokens (whitespace, controls, URL metacharacters).
|
||||
func validateMemberID(value, flagName string) error {
|
||||
if err := validateMemberIDPrefix(value, flagName); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, r := range value {
|
||||
if unicode.IsSpace(r) || unicode.IsControl(r) {
|
||||
return appsValidationParamError(flagName, "member IDs must not contain whitespace or control characters").
|
||||
WithHint("pass comma-separated open_id/open_department_id/open_chat_id values without spaces")
|
||||
}
|
||||
if r == '?' || r == '#' || r == '%' || r == '/' || r == '\\' {
|
||||
return appsValidationParamError(flagName, "member IDs must not contain URL metacharacters (?, #, %, /, \\)").
|
||||
WithHint("pass comma-separated open_id/open_department_id/open_chat_id values without URL characters")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateMemberIDPrefix(value, flagName string) error {
|
||||
kind, ok := roleMemberKindForFlag(flagName)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
if !strings.HasPrefix(value, kind.prefix) || len(value) == len(kind.prefix) {
|
||||
return appsValidationParamError(flagName, "%s must use %s IDs", flagName, kind.prefix).
|
||||
WithHint("resolve names or emails to open IDs before calling role member commands")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func roleMemberKindForFlag(flagName string) (roleMemberKind, bool) {
|
||||
if flagName == "--user-id" {
|
||||
flagName = "--users"
|
||||
}
|
||||
for _, kind := range roleMemberKinds {
|
||||
if kind.flagName == flagName {
|
||||
return kind, true
|
||||
}
|
||||
}
|
||||
return roleMemberKind{}, false
|
||||
}
|
||||
|
||||
func roleMemberKindForType(memberType string) (roleMemberKind, bool) {
|
||||
for _, kind := range roleMemberKinds {
|
||||
if kind.memberType == memberType {
|
||||
return kind, true
|
||||
}
|
||||
}
|
||||
return roleMemberKind{}, false
|
||||
}
|
||||
|
||||
func roleDisplayValue(value string) string {
|
||||
value = validate.SanitizeForTerminal(value)
|
||||
value = strings.NewReplacer("\n", " ", "\t", " ").Replace(value)
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
|
||||
// withRoleErrorHint refines documented Spark role errors with command-specific
|
||||
// recovery while preserving the typed error, numeric code, log_id, and any
|
||||
// server-provided detail. Unknown codes retain the existing Apps fallback.
|
||||
func withRoleErrorHint(err error, operation roleErrorOperation) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
problem, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
return err
|
||||
}
|
||||
hint := roleErrorHint(problem.Code, operation)
|
||||
if hint == "" {
|
||||
return withAppsHint(err, roleFallbackHint(operation))
|
||||
}
|
||||
|
||||
existing := strings.TrimSpace(problem.Hint)
|
||||
canonicalAPIHint := strings.TrimSpace(errclass.APIHint(problem.Subtype))
|
||||
switch {
|
||||
case existing == "", existing == canonicalAPIHint:
|
||||
problem.Hint = hint
|
||||
case !strings.Contains(existing, hint):
|
||||
problem.Hint = existing + "; " + hint
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func roleFallbackHint(operation roleErrorOperation) string {
|
||||
switch operation {
|
||||
case roleOperationList:
|
||||
return roleAppHint
|
||||
case roleOperationCreate:
|
||||
return roleCreateHint
|
||||
case roleOperationMemberList, roleOperationMemberAdd, roleOperationMemberRemove:
|
||||
return roleMemberHint
|
||||
case roleOperationMatchList:
|
||||
return roleMatchHint
|
||||
default:
|
||||
return roleItemHint
|
||||
}
|
||||
}
|
||||
|
||||
func roleErrorHint(code int, operation roleErrorOperation) string {
|
||||
switch code {
|
||||
case roleErrInvalidParameters:
|
||||
return roleFallbackHint(operation)
|
||||
case roleErrAdminRequired:
|
||||
return "ask an app administrator to perform this operation or grant the calling user app-administrator access"
|
||||
case roleErrManagerRequired:
|
||||
return "ask an app administrator or app developer to perform this operation, or grant the calling user app-management access"
|
||||
case roleErrInvalidRoleID:
|
||||
if operation == roleOperationCreate {
|
||||
return "omit --role-id to let the server generate one, or provide a role ID accepted by the role service"
|
||||
}
|
||||
case roleErrRoleNotFound:
|
||||
if operation == roleOperationMatchList {
|
||||
return "list the app's current roles and retry; role data used for this match may no longer be valid"
|
||||
}
|
||||
return roleItemHint
|
||||
case roleErrRoleAlreadyExists:
|
||||
if operation == roleOperationCreate {
|
||||
return "choose a different --role-id or omit --role-id to let the server generate one"
|
||||
}
|
||||
case roleErrRoleLimitExceeded:
|
||||
if operation == roleOperationCreate {
|
||||
return "delete an unused app role before creating another role"
|
||||
}
|
||||
case roleErrInvalidRoleName:
|
||||
if operation == roleOperationCreate || operation == roleOperationUpdate {
|
||||
return "adjust --name to a non-empty value accepted by the role service"
|
||||
}
|
||||
case roleErrInvalidRoleDescription:
|
||||
if operation == roleOperationCreate || operation == roleOperationUpdate {
|
||||
return "adjust --description to a value accepted by the role service"
|
||||
}
|
||||
case roleErrUnsupportedMemberType:
|
||||
if operation == roleOperationMemberList {
|
||||
return "use --member-type user, department, or chat, or omit --member-type to list all member types"
|
||||
}
|
||||
case roleErrInvalidMemberID:
|
||||
if operation == roleOperationMatchList {
|
||||
return "resolve the target user to an open_id and retry with --user-id <open_id>"
|
||||
}
|
||||
if operation == roleOperationMemberAdd || operation == roleOperationMemberRemove {
|
||||
return roleMemberHint
|
||||
}
|
||||
case roleErrUserLimitExceeded:
|
||||
if operation == roleOperationMemberAdd {
|
||||
return "reduce the users being added with --users, or remove unused user members before retrying"
|
||||
}
|
||||
case roleErrDepartmentLimitExceeded:
|
||||
if operation == roleOperationMemberAdd {
|
||||
return "reduce the departments being added with --departments, or remove unused department members before retrying"
|
||||
}
|
||||
case roleErrChatLimitExceeded:
|
||||
if operation == roleOperationMemberAdd {
|
||||
return "reduce the chats being added with --chats, or remove unused chat members before retrying"
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func roleCollectionItem(item interface{}, collection string, index int) (map[string]interface{}, string, error) {
|
||||
role, ok := item.(map[string]interface{})
|
||||
if !ok {
|
||||
return nil, "", invalidRoleCollectionResponse("%s item %d must be an object", collection, index)
|
||||
}
|
||||
rawRoleID, exists := role["role_id"]
|
||||
roleID, stringOK := rawRoleID.(string)
|
||||
roleID = strings.TrimSpace(roleID)
|
||||
if !exists || !stringOK || roleID == "" {
|
||||
return nil, "", invalidRoleCollectionResponse("%s item %d must contain a non-empty string role_id", collection, index)
|
||||
}
|
||||
rawName, exists := role["name"]
|
||||
name, stringOK := rawName.(string)
|
||||
if !exists || !stringOK || strings.TrimSpace(name) == "" {
|
||||
return nil, "", invalidRoleCollectionResponse("%s item %d must contain a non-empty string name", collection, index)
|
||||
}
|
||||
return role, roleID, nil
|
||||
}
|
||||
|
||||
func validateRoleCollection(items []interface{}, collection string) error {
|
||||
for index, item := range items {
|
||||
if _, _, err := roleCollectionItem(item, collection, index); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func invalidRoleCollectionResponse(format string, args ...interface{}) error {
|
||||
return errs.NewInternalError(errs.SubtypeInvalidResponse, format, args...).
|
||||
WithHint("retry the read; do not treat missing or malformed role data as an empty or complete result")
|
||||
}
|
||||
|
||||
func buildRoleMemberGroups(usersCSV, departmentsCSV, chatsCSV string) (roleMemberGroups, error) {
|
||||
users, err := splitRoleMemberCSV(usersCSV, "--users")
|
||||
if err != nil {
|
||||
return roleMemberGroups{}, err
|
||||
}
|
||||
departments, err := splitRoleMemberCSV(departmentsCSV, "--departments")
|
||||
if err != nil {
|
||||
return roleMemberGroups{}, err
|
||||
}
|
||||
chats, err := splitRoleMemberCSV(chatsCSV, "--chats")
|
||||
if err != nil {
|
||||
return roleMemberGroups{}, err
|
||||
}
|
||||
groups := roleMemberGroups{
|
||||
Users: users,
|
||||
Departments: departments,
|
||||
Chats: chats,
|
||||
}
|
||||
total := len(groups.Users) + len(groups.Departments) + len(groups.Chats)
|
||||
if total == 0 {
|
||||
reason := "provide at least one of --users, --departments, or --chats"
|
||||
return groups, appsValidationError("at least one of --users, --departments, or --chats is required").
|
||||
WithParams(
|
||||
appsInvalidParam("--users", reason),
|
||||
appsInvalidParam("--departments", reason),
|
||||
appsInvalidParam("--chats", reason),
|
||||
).
|
||||
WithHint("resolve names to IDs first, then pass --users open_id, --departments open_department_id, or --chats open_chat_id")
|
||||
}
|
||||
if total > maxRoleMembers {
|
||||
return groups, appsValidationError("role members cannot exceed %d", maxRoleMembers).
|
||||
WithParams(roleMemberLimitParams(groups)...).
|
||||
WithHint(fmt.Sprintf("reduce the atomic request to at most %d members; the CLI does not split member writes automatically", maxRoleMembers))
|
||||
}
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
func buildRoleMemberBody(groups roleMemberGroups) map[string]interface{} {
|
||||
body := map[string]interface{}{}
|
||||
if len(groups.Users) > 0 {
|
||||
body["users"] = groups.Users
|
||||
}
|
||||
if len(groups.Departments) > 0 {
|
||||
body["departments"] = groups.Departments
|
||||
}
|
||||
if len(groups.Chats) > 0 {
|
||||
body["chats"] = groups.Chats
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
func roleMemberLimitParams(groups roleMemberGroups) []errs.InvalidParam {
|
||||
reason := fmt.Sprintf("combined role member count exceeds %d", maxRoleMembers)
|
||||
params := make([]errs.InvalidParam, 0, len(roleMemberKinds))
|
||||
if len(groups.Users) > 0 {
|
||||
params = append(params, appsInvalidParam("--users", reason))
|
||||
}
|
||||
if len(groups.Departments) > 0 {
|
||||
params = append(params, appsInvalidParam("--departments", reason))
|
||||
}
|
||||
if len(groups.Chats) > 0 {
|
||||
params = append(params, appsInvalidParam("--chats", reason))
|
||||
}
|
||||
return params
|
||||
}
|
||||
447
shortcuts/apps/apps_role_common_test.go
Normal file
447
shortcuts/apps/apps_role_common_test.go
Normal file
@@ -0,0 +1,447 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/internal/cmdutil"
|
||||
"github.com/larksuite/cli/internal/core"
|
||||
"github.com/larksuite/cli/internal/errclass"
|
||||
"github.com/larksuite/cli/internal/httpmock"
|
||||
"github.com/larksuite/cli/shortcuts/common"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func newRoleRCtx(t *testing.T, flagDefs map[string]string, flags map[string]string) (*common.RuntimeContext, *bytes.Buffer, *httpmock.Registry) {
|
||||
t.Helper()
|
||||
cfg := &core.CliConfig{
|
||||
AppID: "test-app-" + strings.ToLower(t.Name()),
|
||||
AppSecret: "test-secret",
|
||||
Brand: core.BrandFeishu,
|
||||
UserOpenId: "ou_test",
|
||||
}
|
||||
factory, stdoutBuf, _, reg := cmdutil.TestFactory(t, cfg)
|
||||
cmd := &cobra.Command{Use: "test-role"}
|
||||
cmd.SetContext(context.Background())
|
||||
for name, typ := range flagDefs {
|
||||
switch typ {
|
||||
case "bool":
|
||||
cmd.Flags().Bool(name, false, "")
|
||||
case "int":
|
||||
cmd.Flags().Int(name, 0, "")
|
||||
case "string_array":
|
||||
cmd.Flags().StringArray(name, nil, "")
|
||||
default:
|
||||
cmd.Flags().String(name, "", "")
|
||||
}
|
||||
}
|
||||
for name, val := range flags {
|
||||
if err := cmd.Flags().Set(name, val); err != nil {
|
||||
t.Fatalf("set flag %q = %q: %v", name, val, err)
|
||||
}
|
||||
}
|
||||
rctx := common.TestNewRuntimeContextForAPI(context.Background(), cmd, cfg, factory, core.AsUser)
|
||||
return rctx, stdoutBuf, reg
|
||||
}
|
||||
|
||||
func assertRoleValidationParam(t *testing.T, err error, param string) *errs.Problem {
|
||||
t.Helper()
|
||||
problem, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
t.Fatalf("err = %#v, want typed problem", err)
|
||||
}
|
||||
if problem.Category != errs.CategoryValidation {
|
||||
t.Fatalf("category = %q, want validation", problem.Category)
|
||||
}
|
||||
if problem.Subtype != errs.SubtypeInvalidArgument {
|
||||
t.Fatalf("subtype = %q, want invalid_argument", problem.Subtype)
|
||||
}
|
||||
var validation *errs.ValidationError
|
||||
if !errors.As(err, &validation) {
|
||||
t.Fatalf("err = %#v, want validation error", err)
|
||||
}
|
||||
if validation.Param != param {
|
||||
t.Fatalf("param = %q, want %s", validation.Param, param)
|
||||
}
|
||||
return problem
|
||||
}
|
||||
|
||||
func assertRoleValidationParams(t *testing.T, err error, params ...string) *errs.Problem {
|
||||
t.Helper()
|
||||
problem, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
t.Fatalf("err = %#v, want typed problem", err)
|
||||
}
|
||||
if problem.Category != errs.CategoryValidation || problem.Subtype != errs.SubtypeInvalidArgument {
|
||||
t.Fatalf("problem = %+v, want validation/invalid_argument", problem)
|
||||
}
|
||||
var validation *errs.ValidationError
|
||||
if !errors.As(err, &validation) {
|
||||
t.Fatalf("err = %#v, want validation error", err)
|
||||
}
|
||||
if validation.Param != "" {
|
||||
t.Fatalf("param = %q, want omitted for multi-parameter constraint", validation.Param)
|
||||
}
|
||||
if len(validation.Params) != len(params) {
|
||||
t.Fatalf("params = %#v, want %v", validation.Params, params)
|
||||
}
|
||||
for index, want := range params {
|
||||
if validation.Params[index].Name != want || validation.Params[index].Reason == "" {
|
||||
t.Fatalf("params[%d] = %#v, want name=%q with a reason", index, validation.Params[index], want)
|
||||
}
|
||||
}
|
||||
return problem
|
||||
}
|
||||
|
||||
func TestBuildRolePageParams_DefaultAndChanged(t *testing.T) {
|
||||
rctx, _, _ := newRoleRCtx(t, map[string]string{
|
||||
"page-size": "int",
|
||||
"page-token": "string",
|
||||
}, map[string]string{})
|
||||
params, err := buildRolePageParams(rctx)
|
||||
if err != nil {
|
||||
t.Fatalf("buildRolePageParams() = %v", err)
|
||||
}
|
||||
if params["limit"] != defaultRolePageSize || params["offset"] != 0 {
|
||||
t.Fatalf("params = %#v, want limit=%d offset=0", params, defaultRolePageSize)
|
||||
}
|
||||
|
||||
rctx, _, _ = newRoleRCtx(t, map[string]string{
|
||||
"page-size": "int",
|
||||
"page-token": "string",
|
||||
}, map[string]string{"page-size": "20", "page-token": "40"})
|
||||
params, err = buildRolePageParams(rctx)
|
||||
if err != nil {
|
||||
t.Fatalf("buildRolePageParams(changed) = %v", err)
|
||||
}
|
||||
if params["limit"] != 20 || params["offset"] != 40 {
|
||||
t.Fatalf("params = %#v, want limit=20 offset=40", params)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRolePageParams_RejectsInvalidToken(t *testing.T) {
|
||||
rctx, _, _ := newRoleRCtx(t, map[string]string{
|
||||
"page-size": "int",
|
||||
"page-token": "string",
|
||||
}, map[string]string{"page-token": "abc"})
|
||||
_, err := buildRolePageParams(rctx)
|
||||
assertRoleValidationParam(t, err, "--page-token")
|
||||
}
|
||||
|
||||
func TestBuildRolePageParams_RejectsPageSizeOverMax(t *testing.T) {
|
||||
rctx, _, _ := newRoleRCtx(t, map[string]string{
|
||||
"page-size": "int",
|
||||
"page-token": "string",
|
||||
}, map[string]string{"page-size": "101"})
|
||||
_, err := buildRolePageParams(rctx)
|
||||
assertRoleValidationParam(t, err, "--page-size")
|
||||
}
|
||||
|
||||
func TestValidateOptionalRoleID(t *testing.T) {
|
||||
for _, good := range []string{"", " role_001 ", "Role-ABC", "abc123", strings.Repeat("a", 64)} {
|
||||
if err := validateOptionalRoleID(good); err != nil {
|
||||
t.Fatalf("validateOptionalRoleID(%q) = %v", good, err)
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{"bad/role", "bad role", strings.Repeat("a", 65)} {
|
||||
err := validateOptionalRoleID(bad)
|
||||
problem := assertRoleValidationParam(t, err, "--role-id")
|
||||
if !strings.Contains(problem.Hint, "omit --role-id") {
|
||||
t.Fatalf("hint = %q, want create-specific omit guidance", problem.Hint)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoleFlagHelpersTrim(t *testing.T) {
|
||||
rctx, _, _ := newRoleRCtx(t, map[string]string{
|
||||
"app-id": "string",
|
||||
"role-id": "string",
|
||||
}, map[string]string{"app-id": " app_1 ", "role-id": " role_1 "})
|
||||
if got := roleAppID(rctx); got != "app_1" {
|
||||
t.Fatalf("roleAppID() = %q, want app_1", got)
|
||||
}
|
||||
if got := roleID(rctx); got != "role_1" {
|
||||
t.Fatalf("roleID() = %q, want role_1", got)
|
||||
}
|
||||
if err := validateRoleID(rctx); err != nil {
|
||||
t.Fatalf("validateRoleID() = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRoleAppIDRejectsEmpty(t *testing.T) {
|
||||
rctx, _, _ := newRoleRCtx(t, map[string]string{
|
||||
"app-id": "string",
|
||||
}, map[string]string{})
|
||||
problem := assertRoleValidationParam(t, validateRoleAppID(rctx), "--app-id")
|
||||
if problem.Message != "--app-id is required" {
|
||||
t.Fatalf("message = %q, want --app-id is required", problem.Message)
|
||||
}
|
||||
if problem.Hint == "" {
|
||||
t.Fatalf("hint is empty, want recovery guidance")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRoleAppIDRejectsPathSegmentUnsafeChars(t *testing.T) {
|
||||
for _, appID := range []string{"app/bad", `app\bad`, "app bad", "app\u00a0bad", "app\nbad", "app\u0000bad"} {
|
||||
rctx, _, _ := newRoleRCtx(t, map[string]string{
|
||||
"app-id": "string",
|
||||
}, map[string]string{"app-id": appID})
|
||||
assertRoleValidationParam(t, validateRoleAppID(rctx), "--app-id")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRoleAppIDRejectsLarkCredentialAppID(t *testing.T) {
|
||||
rctx, _, _ := newRoleRCtx(t, map[string]string{
|
||||
"app-id": "string",
|
||||
}, map[string]string{"app-id": "cli_app"})
|
||||
assertRoleValidationParam(t, validateRoleAppID(rctx), "--app-id")
|
||||
}
|
||||
|
||||
func TestValidateRoleAppIDRequiresMiaodaPrefix(t *testing.T) {
|
||||
for _, appID := range []string{"app", "app_", "miaoda_123", "plain"} {
|
||||
rctx, _, _ := newRoleRCtx(t, map[string]string{
|
||||
"app-id": "string",
|
||||
}, map[string]string{"app-id": appID})
|
||||
problem := assertRoleValidationParam(t, validateRoleAppID(rctx), "--app-id")
|
||||
if !strings.Contains(problem.Message, "starting with app_") {
|
||||
t.Fatalf("appID=%q message=%q, want app_ guidance", appID, problem.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRoleIDRejectsInvalidRequiredRoleID(t *testing.T) {
|
||||
rctx, _, _ := newRoleRCtx(t, map[string]string{
|
||||
"app-id": "string",
|
||||
"role-id": "string",
|
||||
}, map[string]string{"app-id": "app_x", "role-id": "bad/role"})
|
||||
problem := assertRoleValidationParam(t, validateRoleID(rctx), "--role-id")
|
||||
if strings.Contains(problem.Hint, "omit --role-id") || !strings.Contains(problem.Hint, "+role-list") {
|
||||
t.Fatalf("hint = %q, want existing-role resolution guidance", problem.Hint)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRoleIDRejectsMissingRequiredRoleID(t *testing.T) {
|
||||
rctx, _, _ := newRoleRCtx(t, map[string]string{
|
||||
"app-id": "string",
|
||||
"role-id": "string",
|
||||
}, map[string]string{"app-id": "app_x"})
|
||||
problem := assertRoleValidationParam(t, validateRoleID(rctx), "--role-id")
|
||||
if problem.Message != "--role-id is required" {
|
||||
t.Fatalf("message = %q, want --role-id is required", problem.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRoleMemberGroupsAndBody(t *testing.T) {
|
||||
groups, err := buildRoleMemberGroups(" ou_a,ou_b ", " od-a ", " oc_a ")
|
||||
if err != nil {
|
||||
t.Fatalf("buildRoleMemberGroups() = %v", err)
|
||||
}
|
||||
if len(groups.Users) != 2 || len(groups.Departments) != 1 || len(groups.Chats) != 1 {
|
||||
t.Fatalf("groups = %#v", groups)
|
||||
}
|
||||
body := buildRoleMemberBody(groups)
|
||||
assertJSONEquivalent(t, body, map[string]interface{}{
|
||||
"users": []interface{}{"ou_a", "ou_b"},
|
||||
"departments": []interface{}{"od-a"},
|
||||
"chats": []interface{}{"oc_a"},
|
||||
})
|
||||
}
|
||||
|
||||
func TestBuildRoleMemberGroupsRejectsEmpty(t *testing.T) {
|
||||
_, err := buildRoleMemberGroups(" , ", "", "")
|
||||
assertRoleValidationParams(t, err, "--users", "--departments", "--chats")
|
||||
}
|
||||
|
||||
func TestBuildRoleMemberGroupsRejectsInvalidMemberIDWithSourceParam(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
users string
|
||||
departments string
|
||||
chats string
|
||||
wantParam string
|
||||
}{
|
||||
{name: "users slash", users: "ou/bad", wantParam: "--users"},
|
||||
{name: "users email", users: "alice@example.com", wantParam: "--users"},
|
||||
{name: "users wrong prefix", users: "user_123", wantParam: "--users"},
|
||||
{name: "users prefix only", users: "ou_", wantParam: "--users"},
|
||||
{name: "departments wrong prefix", departments: "ou_user", wantParam: "--departments"},
|
||||
{name: "departments prefix only", departments: "od-", wantParam: "--departments"},
|
||||
{name: "legacy departments prefix", departments: "od_department", wantParam: "--departments"},
|
||||
{name: "chats wrong prefix", chats: "od-department", wantParam: "--chats"},
|
||||
{name: "chats prefix only", chats: "oc_", wantParam: "--chats"},
|
||||
{
|
||||
name: "departments",
|
||||
departments: "od-bad value",
|
||||
wantParam: "--departments",
|
||||
},
|
||||
{
|
||||
name: "chats",
|
||||
chats: "oc?bad",
|
||||
wantParam: "--chats",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := buildRoleMemberGroups(tt.users, tt.departments, tt.chats)
|
||||
assertRoleValidationParam(t, err, tt.wantParam)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRoleMemberGroupsRejectsMoreThanMax(t *testing.T) {
|
||||
users := make([]string, maxRoleMembers+1)
|
||||
for i := range users {
|
||||
users[i] = "ou_test"
|
||||
}
|
||||
_, err := buildRoleMemberGroups(strings.Join(users, ","), "", "")
|
||||
assertRoleValidationParams(t, err, "--users")
|
||||
}
|
||||
|
||||
func TestBuildRoleMemberGroupsRejectsMoreThanMaxOnlyChats(t *testing.T) {
|
||||
chats := make([]string, maxRoleMembers+1)
|
||||
for i := range chats {
|
||||
chats[i] = "oc_test"
|
||||
}
|
||||
_, err := buildRoleMemberGroups("", "", strings.Join(chats, ","))
|
||||
problem := assertRoleValidationParams(t, err, "--chats")
|
||||
if !strings.Contains(problem.Message, "role members cannot exceed 100") {
|
||||
t.Fatalf("message = %q, want role members limit", problem.Message)
|
||||
}
|
||||
if !strings.Contains(problem.Hint, "does not split") || !strings.Contains(problem.Hint, "atomic request") {
|
||||
t.Fatalf("hint = %q, want no automatic batching guidance", problem.Hint)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRoleMemberGroupsOverflowNamesEveryContributingFlag(t *testing.T) {
|
||||
users := strings.TrimSuffix(strings.Repeat("ou_user,", 60), ",")
|
||||
chats := strings.TrimSuffix(strings.Repeat("oc_chat,", 41), ",")
|
||||
_, err := buildRoleMemberGroups(users, "", chats)
|
||||
assertRoleValidationParams(t, err, "--users", "--chats")
|
||||
}
|
||||
|
||||
func TestRoleMemberKindsAreCompleteAndStable(t *testing.T) {
|
||||
want := []roleMemberKind{
|
||||
{memberType: "user", dataKey: "users", flagName: "--users", prefix: "ou_"},
|
||||
{memberType: "department", dataKey: "departments", flagName: "--departments", prefix: "od-"},
|
||||
{memberType: "chat", dataKey: "chats", flagName: "--chats", prefix: "oc_"},
|
||||
}
|
||||
if len(roleMemberKinds) != len(want) {
|
||||
t.Fatalf("roleMemberKinds = %#v, want %#v", roleMemberKinds, want)
|
||||
}
|
||||
for index := range want {
|
||||
if roleMemberKinds[index] != want[index] {
|
||||
t.Fatalf("roleMemberKinds[%d] = %#v, want %#v", index, roleMemberKinds[index], want[index])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoleDisplayValueSanitizesAndFlattens(t *testing.T) {
|
||||
got := roleDisplayValue(" Admin\n\x1b[31mred\x1b[0m\tvalue ")
|
||||
if got != "Admin red value" {
|
||||
t.Fatalf("roleDisplayValue() = %q, want flattened safe text", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoleNextPageToken(t *testing.T) {
|
||||
if got := roleNextPageToken(40, 20, true); got != "60" {
|
||||
t.Fatalf("roleNextPageToken(hasMore) = %q, want 60", got)
|
||||
}
|
||||
if got := roleNextPageToken(40, 20, false); got != "" {
|
||||
t.Fatalf("roleNextPageToken(!hasMore) = %q, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithRoleErrorHintUsesDocumentedRecoveryAndPreservesEnvelope(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
code int
|
||||
operation roleErrorOperation
|
||||
wantHint string
|
||||
forbid string
|
||||
}{
|
||||
{name: "invalid parameters", code: roleErrInvalidParameters, operation: roleOperationList, wantHint: roleAppHint},
|
||||
{name: "administrator required", code: roleErrAdminRequired, operation: roleOperationList, wantHint: "app administrator"},
|
||||
{name: "administrator or developer required", code: roleErrManagerRequired, operation: roleOperationGet, wantHint: "administrator or app developer"},
|
||||
{name: "invalid create role id", code: roleErrInvalidRoleID, operation: roleOperationCreate, wantHint: "omit --role-id"},
|
||||
{name: "role missing", code: roleErrRoleNotFound, operation: roleOperationGet, wantHint: "+role-list"},
|
||||
{name: "stale match role", code: roleErrRoleNotFound, operation: roleOperationMatchList, wantHint: "may no longer be valid", forbid: "--role-id"},
|
||||
{name: "duplicate role id", code: roleErrRoleAlreadyExists, operation: roleOperationCreate, wantHint: "different --role-id"},
|
||||
{name: "role limit", code: roleErrRoleLimitExceeded, operation: roleOperationCreate, wantHint: "delete an unused app role"},
|
||||
{name: "invalid role name", code: roleErrInvalidRoleName, operation: roleOperationUpdate, wantHint: "adjust --name"},
|
||||
{name: "invalid role description", code: roleErrInvalidRoleDescription, operation: roleOperationUpdate, wantHint: "adjust --description"},
|
||||
{name: "unsupported member type", code: roleErrUnsupportedMemberType, operation: roleOperationMemberList, wantHint: "user, department, or chat"},
|
||||
{name: "invalid member id", code: roleErrInvalidMemberID, operation: roleOperationMemberAdd, wantHint: "member IDs"},
|
||||
{name: "invalid match target", code: roleErrInvalidMemberID, operation: roleOperationMatchList, wantHint: "--user-id", forbid: "--role-id"},
|
||||
{name: "user quota", code: roleErrUserLimitExceeded, operation: roleOperationMemberAdd, wantHint: "reduce the users"},
|
||||
{name: "department quota", code: roleErrDepartmentLimitExceeded, operation: roleOperationMemberAdd, wantHint: "reduce the departments"},
|
||||
{name: "chat quota", code: roleErrChatLimitExceeded, operation: roleOperationMemberAdd, wantHint: "reduce the chats"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := errclass.BuildAPIError(map[string]any{
|
||||
"code": tt.code,
|
||||
"msg": "role request failed",
|
||||
"log_id": "log-role-hint",
|
||||
}, errclass.ClassifyContext{Identity: "user"})
|
||||
err = withRoleErrorHint(err, tt.operation)
|
||||
problem, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
t.Fatalf("err = %#v, want typed problem", err)
|
||||
}
|
||||
if problem.Code != tt.code || problem.LogID != "log-role-hint" || problem.Retryable {
|
||||
t.Fatalf("problem envelope changed: %+v", problem)
|
||||
}
|
||||
if !strings.Contains(problem.Hint, tt.wantHint) {
|
||||
t.Fatalf("hint = %q, want substring %q", problem.Hint, tt.wantHint)
|
||||
}
|
||||
if tt.forbid != "" && strings.Contains(problem.Hint, tt.forbid) {
|
||||
t.Fatalf("hint = %q, must not contain %q", problem.Hint, tt.forbid)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithRoleErrorHintPreservesServerDetail(t *testing.T) {
|
||||
err := errclass.BuildAPIError(map[string]any{
|
||||
"code": roleErrInvalidRoleName,
|
||||
"msg": "invalid role name",
|
||||
"error": map[string]any{
|
||||
"details": []any{map[string]any{"value": "name exceeds the service limit"}},
|
||||
},
|
||||
}, errclass.ClassifyContext{Identity: "user"})
|
||||
err = withRoleErrorHint(err, roleOperationCreate)
|
||||
problem, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
t.Fatalf("err = %#v, want typed problem", err)
|
||||
}
|
||||
for _, want := range []string{"name exceeds the service limit", "adjust --name"} {
|
||||
if !strings.Contains(problem.Hint, want) {
|
||||
t.Fatalf("hint = %q, want %q", problem.Hint, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithRoleErrorHintPreservesAuthorizationDetail(t *testing.T) {
|
||||
var err error = errs.NewPermissionError(errs.SubtypePermissionDenied, "administrator access required").
|
||||
WithCode(roleErrAdminRequired).
|
||||
WithHint("server detail: only owners may change this app")
|
||||
err = withRoleErrorHint(err, roleOperationUpdate)
|
||||
problem, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
t.Fatalf("err = %#v, want typed problem", err)
|
||||
}
|
||||
for _, want := range []string{"server detail: only owners", "ask an app administrator"} {
|
||||
if !strings.Contains(problem.Hint, want) {
|
||||
t.Fatalf("hint = %q, want %q", problem.Hint, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
611
shortcuts/apps/apps_role_member.go
Normal file
611
shortcuts/apps/apps_role_member.go
Normal file
@@ -0,0 +1,611 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/internal/validate"
|
||||
"github.com/larksuite/cli/shortcuts/common"
|
||||
)
|
||||
|
||||
// AppsRoleMemberList lists members of an app role.
|
||||
var AppsRoleMemberList = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+role-member-list",
|
||||
Description: "List app role members",
|
||||
Risk: "read",
|
||||
Tips: []string{
|
||||
"Example: lark-cli apps +role-member-list --app-id <app_id> --role-id <role_id>",
|
||||
"Example: lark-cli apps +role-member-list --app-id <app_id> --role-id <role_id> --member-type user",
|
||||
"When only one member type is requested, pass --member-type user|department|chat instead of filtering the full response",
|
||||
"--member-type returns only the selected member field; omitted fields are unknown, so omit the flag for pre/post-write baselines",
|
||||
"--format table renders the CLI-native member_type/member_id table; this command has no --limit or --page-size flag",
|
||||
},
|
||||
Scopes: []string{"spark:app:read"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: roleAppIDRequiredDesc, Required: true},
|
||||
{Name: "role-id", Desc: roleIDRequiredDesc, Required: true},
|
||||
{Name: "member-type", Desc: "filter member type", Enum: []string{"user", "department", "chat"}},
|
||||
},
|
||||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
if err := validateRoleID(rctx); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := buildRoleMemberListParams(rctx)
|
||||
return err
|
||||
},
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
// Validate already ran and called buildRoleMemberListParams; error is impossible here.
|
||||
params, _ := buildRoleMemberListParams(rctx)
|
||||
return common.NewDryRunAPI().
|
||||
GET(roleMemberListURL(rctx)).
|
||||
Desc("List app role members").
|
||||
Params(params)
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
params, err := buildRoleMemberListParams(rctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := rctx.CallAPITyped("GET", roleMemberListURL(rctx), params, nil)
|
||||
memberType, _ := params["member_type"].(string)
|
||||
if shouldRetryRoleMemberListWithoutFilter(err, memberType) {
|
||||
fmt.Fprintln(rctx.IO().ErrOut, "warning: the server rejected chat member filtering; retried without the filter and returned only the chats field. Omit --member-type for a complete member baseline.")
|
||||
data, err = rctx.CallAPITyped("GET", roleMemberListURL(rctx), nil, nil)
|
||||
}
|
||||
if err != nil {
|
||||
return withRoleErrorHint(err, roleOperationMemberList)
|
||||
}
|
||||
data, err = normalizeRoleMemberListData(data, memberType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if memberType != "" {
|
||||
fmt.Fprintf(
|
||||
rctx.IO().ErrOut,
|
||||
"warning: --member-type=%s returns only the selected member field; omitted member fields are unknown. Omit --member-type for a complete member baseline.\n",
|
||||
memberType,
|
||||
)
|
||||
}
|
||||
out := roleMemberListOutputData(rctx, data)
|
||||
rctx.OutFormat(out, nil, func(w io.Writer) {
|
||||
renderRoleMemberListPretty(w, data)
|
||||
})
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// AppsRoleMemberAdd adds members to an app role.
|
||||
var AppsRoleMemberAdd = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+role-member-add",
|
||||
Description: "Add app role members",
|
||||
Risk: "write",
|
||||
Tips: []string{
|
||||
"Example: lark-cli apps +role-member-add --app-id <app_id> --role-id <role_id> --users ou_x",
|
||||
"Example: lark-cli apps +role-member-add --app-id <app_id> --role-id <role_id> --users ou_x,ou_y --departments od-x --chats oc_x",
|
||||
"Resolve every name first, then add all resolved users (ou_), departments (od-), and chats (oc_) in one call using the three type-specific flags; if any resolution fails, stop without a partial write",
|
||||
},
|
||||
Scopes: []string{"spark:app:write"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: roleAppIDRequiredDesc, Required: true},
|
||||
{Name: "role-id", Desc: roleIDRequiredDesc, Required: true},
|
||||
{Name: "users", Desc: "comma-separated user open IDs; do not pass names or emails"},
|
||||
{Name: "departments", Desc: "comma-separated open_department_id values"},
|
||||
{Name: "chats", Desc: "comma-separated open_chat_id values"},
|
||||
},
|
||||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
if err := validateRoleID(rctx); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := buildRoleMemberGroups(rctx.Str("users"), rctx.Str("departments"), rctx.Str("chats"))
|
||||
return err
|
||||
},
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
// Validate already ran and called buildRoleMemberAddBody; error is impossible here.
|
||||
body, _, _ := buildRoleMemberAddBody(rctx)
|
||||
return common.NewDryRunAPI().
|
||||
POST(roleMemberAddURL(rctx)).
|
||||
Desc("Add app role members").
|
||||
Body(body)
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
body, _, err := buildRoleMemberAddBody(rctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := rctx.CallAPITyped("POST", roleMemberAddURL(rctx), nil, body)
|
||||
if err != nil {
|
||||
return withRoleErrorHint(err, roleOperationMemberAdd)
|
||||
}
|
||||
data, err = normalizeRoleMemberMutationData(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rctx.OutFormat(data, nil, func(w io.Writer) {
|
||||
renderRoleMemberMutationPretty(w, data)
|
||||
})
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// AppsRoleMemberRemove removes members from an app role.
|
||||
var AppsRoleMemberRemove = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+role-member-remove",
|
||||
Description: "Remove app role members",
|
||||
Risk: "high-risk-write",
|
||||
Tips: []string{
|
||||
"Example: lark-cli apps +role-member-remove --app-id <app_id> --role-id <role_id> --users ou_x --yes",
|
||||
"Example: lark-cli apps +role-member-remove --app-id <app_id> --role-id <role_id> --all --yes",
|
||||
"When the user names a member, resolve and verify that exact name before writing; if lookup fails, stop and never infer that the role's only current member is the target",
|
||||
"--all clears members but does not delete the role; after a confirmed --all operation, use an unfiltered +role-member-list to verify users, departments, and chats are empty",
|
||||
},
|
||||
Scopes: []string{"spark:app:write"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: roleAppIDRequiredDesc, Required: true},
|
||||
{Name: "role-id", Desc: roleIDRequiredDesc, Required: true},
|
||||
{Name: "users", Desc: "comma-separated user open IDs; do not pass names or emails"},
|
||||
{Name: "departments", Desc: "comma-separated open_department_id values"},
|
||||
{Name: "chats", Desc: "comma-separated open_chat_id values"},
|
||||
{Name: "all", Type: "bool", Desc: "remove all members from the role"},
|
||||
},
|
||||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
if err := validateRoleID(rctx); err != nil {
|
||||
return err
|
||||
}
|
||||
_, _, err := buildRoleMemberRemoveBody(rctx)
|
||||
return err
|
||||
},
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
// Validate already ran and called buildRoleMemberRemoveBody; error is impossible here.
|
||||
body, _, _ := buildRoleMemberRemoveBody(rctx)
|
||||
return common.NewDryRunAPI().
|
||||
POST(roleMemberRemoveURL(rctx)).
|
||||
Desc("Remove app role members").
|
||||
Body(body)
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
body, _, err := buildRoleMemberRemoveBody(rctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := rctx.CallAPITyped("POST", roleMemberRemoveURL(rctx), nil, body)
|
||||
if err != nil {
|
||||
return withRoleErrorHint(err, roleOperationMemberRemove)
|
||||
}
|
||||
data, err = normalizeRoleMemberMutationData(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rctx.OutFormat(data, nil, func(w io.Writer) {
|
||||
renderRoleMemberMutationPretty(w, data)
|
||||
})
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// AppsRoleMatchList lists roles matching a user in an app.
|
||||
var AppsRoleMatchList = common.Shortcut{
|
||||
Service: appsService,
|
||||
Command: "+role-match-list",
|
||||
Description: "List app roles matching a user",
|
||||
Risk: "read",
|
||||
Tips: []string{
|
||||
"Example: lark-cli apps +role-match-list --app-id <app_id> --user-id <user_open_id>",
|
||||
},
|
||||
Scopes: []string{"spark:app:read"},
|
||||
AuthTypes: []string{"user"},
|
||||
HasFormat: true,
|
||||
Flags: []common.Flag{
|
||||
{Name: "app-id", Desc: roleAppIDRequiredDesc, Required: true},
|
||||
{Name: "user-id", Desc: roleUserIDRequiredDesc, Required: true},
|
||||
},
|
||||
Validate: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
if err := validateRoleAppID(rctx); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := roleMatchTargetUserID(rctx)
|
||||
return err
|
||||
},
|
||||
DryRun: func(ctx context.Context, rctx *common.RuntimeContext) *common.DryRunAPI {
|
||||
// Validate already ran and called buildRoleMatchListBody; error is impossible here.
|
||||
body, _ := buildRoleMatchListBody(rctx)
|
||||
return common.NewDryRunAPI().
|
||||
POST(roleMatchListURL(rctx)).
|
||||
Desc("List app role matches").
|
||||
Body(body)
|
||||
},
|
||||
Execute: func(ctx context.Context, rctx *common.RuntimeContext) error {
|
||||
body, err := buildRoleMatchListBody(rctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := rctx.CallAPITyped("POST", roleMatchListURL(rctx), nil, body)
|
||||
if err != nil {
|
||||
return withRoleErrorHint(err, roleOperationMatchList)
|
||||
}
|
||||
out, err := normalizeRoleMatchListData(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rctx.OutFormat(out, nil, func(w io.Writer) {
|
||||
renderRoleMatchListPretty(w, common.GetSlice(out, "roles"))
|
||||
})
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func roleMemberListURL(rctx *common.RuntimeContext) string {
|
||||
return fmt.Sprintf(roleMemberListPath,
|
||||
validate.EncodePathSegment(roleAppID(rctx)),
|
||||
validate.EncodePathSegment(roleID(rctx)),
|
||||
)
|
||||
}
|
||||
|
||||
func roleMemberAddURL(rctx *common.RuntimeContext) string {
|
||||
return fmt.Sprintf(roleMemberAddPath,
|
||||
validate.EncodePathSegment(roleAppID(rctx)),
|
||||
validate.EncodePathSegment(roleID(rctx)),
|
||||
)
|
||||
}
|
||||
|
||||
func roleMemberRemoveURL(rctx *common.RuntimeContext) string {
|
||||
return fmt.Sprintf(roleMemberRemovePath,
|
||||
validate.EncodePathSegment(roleAppID(rctx)),
|
||||
validate.EncodePathSegment(roleID(rctx)),
|
||||
)
|
||||
}
|
||||
|
||||
func roleMatchListURL(rctx *common.RuntimeContext) string {
|
||||
return fmt.Sprintf(roleMatchListPath, validate.EncodePathSegment(roleAppID(rctx)))
|
||||
}
|
||||
|
||||
func buildRoleMemberListParams(rctx *common.RuntimeContext) (map[string]interface{}, error) {
|
||||
params := map[string]interface{}{}
|
||||
if memberType := strings.TrimSpace(rctx.Str("member-type")); memberType != "" {
|
||||
if _, ok := roleMemberKindForType(memberType); !ok {
|
||||
return nil, appsValidationParamError("--member-type", "--member-type must be one of user, department, or chat").
|
||||
WithHint("omit --member-type to list all member types")
|
||||
}
|
||||
params["member_type"] = memberType
|
||||
}
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func shouldRetryRoleMemberListWithoutFilter(err error, memberType string) bool {
|
||||
if err == nil || memberType != "chat" {
|
||||
return false
|
||||
}
|
||||
problem, ok := errs.ProblemOf(err)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if problem.Code == roleErrUnsupportedMemberType || problem.Code == 400004040 {
|
||||
return true
|
||||
}
|
||||
return problem.Code == 2 && strings.Contains(strings.ToLower(problem.Message), "member_type")
|
||||
}
|
||||
|
||||
func normalizeRoleMemberListData(data map[string]interface{}, memberType string) (map[string]interface{}, error) {
|
||||
if data == nil {
|
||||
return nil, errs.NewInternalError(
|
||||
errs.SubtypeInvalidResponse,
|
||||
"role member response data must be an object",
|
||||
).WithHint("retry the complete member read; do not treat missing, null, or non-object data as an empty role")
|
||||
}
|
||||
out := map[string]interface{}{}
|
||||
for k, v := range data {
|
||||
out[k] = v
|
||||
}
|
||||
// The role service uses an exact empty data object when the requested member
|
||||
// view is empty. For a filtered request, that proves only the selected group
|
||||
// is empty; non-selected groups must remain omitted rather than being
|
||||
// synthesized as empty.
|
||||
if len(data) == 0 {
|
||||
if memberType != "" {
|
||||
kind, _ := roleMemberKindForType(memberType)
|
||||
out[kind.dataKey] = []string{}
|
||||
return out, nil
|
||||
}
|
||||
for _, kind := range roleMemberKinds {
|
||||
out[kind.dataKey] = []string{}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
if memberType != "" {
|
||||
selectedKind, _ := roleMemberKindForType(memberType)
|
||||
values, err := parseRoleMemberIDs(data, selectedKind)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, kind := range roleMemberKinds {
|
||||
if kind.memberType != memberType {
|
||||
delete(out, kind.dataKey)
|
||||
}
|
||||
}
|
||||
out[selectedKind.dataKey] = values
|
||||
return out, nil
|
||||
}
|
||||
for _, kind := range roleMemberKinds {
|
||||
values, err := parseRoleMemberIDs(data, kind)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out[kind.dataKey] = values
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func parseRoleMemberIDs(data map[string]interface{}, kind roleMemberKind) ([]string, error) {
|
||||
raw, exists := data[kind.dataKey]
|
||||
if !exists {
|
||||
return nil, errs.NewInternalError(
|
||||
errs.SubtypeInvalidResponse,
|
||||
"role member response is missing %s",
|
||||
kind.dataKey,
|
||||
).WithHint("retry the member operation; do not treat a missing member group as empty")
|
||||
}
|
||||
items, ok := raw.([]interface{})
|
||||
if !ok {
|
||||
if stringItems, stringOK := raw.([]string); stringOK {
|
||||
items = make([]interface{}, len(stringItems))
|
||||
for index, value := range stringItems {
|
||||
items[index] = value
|
||||
}
|
||||
} else {
|
||||
return nil, errs.NewInternalError(
|
||||
errs.SubtypeInvalidResponse,
|
||||
"role member response field %s must be an array of strings",
|
||||
kind.dataKey,
|
||||
).WithHint("retry the member operation; do not use malformed member data as a permission baseline")
|
||||
}
|
||||
}
|
||||
values := make([]string, 0, len(items))
|
||||
for index, item := range items {
|
||||
value, ok := item.(string)
|
||||
value = strings.TrimSpace(value)
|
||||
if !ok || value == "" || !strings.HasPrefix(value, kind.prefix) || len(value) == len(kind.prefix) {
|
||||
return nil, errs.NewInternalError(
|
||||
errs.SubtypeInvalidResponse,
|
||||
"role member response field %s contains an invalid ID at index %d",
|
||||
kind.dataKey,
|
||||
index,
|
||||
).WithHint("retry the member operation; expected open IDs with the documented member-type prefix")
|
||||
}
|
||||
values = append(values, value)
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func normalizeRoleMemberMutationData(data map[string]interface{}) (map[string]interface{}, error) {
|
||||
if data == nil {
|
||||
return nil, nil
|
||||
}
|
||||
out := map[string]interface{}{}
|
||||
for key, value := range data {
|
||||
out[key] = value
|
||||
}
|
||||
for _, kind := range roleMemberKinds {
|
||||
if _, exists := data[kind.dataKey]; !exists {
|
||||
continue
|
||||
}
|
||||
values, err := parseRoleMemberIDs(data, kind)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out[kind.dataKey] = values
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func buildRoleMemberAddBody(rctx *common.RuntimeContext) (map[string]interface{}, roleMemberGroups, error) {
|
||||
groups, err := buildRoleMemberGroups(rctx.Str("users"), rctx.Str("departments"), rctx.Str("chats"))
|
||||
if err != nil {
|
||||
return nil, groups, err
|
||||
}
|
||||
return buildRoleMemberBody(groups), groups, nil
|
||||
}
|
||||
|
||||
func buildRoleMemberRemoveBody(rctx *common.RuntimeContext) (map[string]interface{}, roleMemberGroups, error) {
|
||||
if rctx.Bool("all") {
|
||||
if hasExplicitRoleMemberFlags(rctx) {
|
||||
return nil, roleMemberGroups{}, appsValidationError("--all cannot be used with --users, --departments, or --chats").
|
||||
WithParams(roleMemberRemoveConflictParams(rctx)...).
|
||||
WithHint("use --all by itself to clear every member, or pass explicit member IDs without --all")
|
||||
}
|
||||
return map[string]interface{}{"all": true}, roleMemberGroups{}, nil
|
||||
}
|
||||
if !hasExplicitRoleMemberFlags(rctx) {
|
||||
reason := "provide member IDs or use --all"
|
||||
return nil, roleMemberGroups{}, appsValidationError("specify members to remove with --users/--departments/--chats, or use --all to clear every member").
|
||||
WithParams(
|
||||
appsInvalidParam("--users", reason),
|
||||
appsInvalidParam("--departments", reason),
|
||||
appsInvalidParam("--chats", reason),
|
||||
appsInvalidParam("--all", reason),
|
||||
).
|
||||
WithHint("pass specific member IDs (e.g. --users ou_x), or use --all to remove all members")
|
||||
}
|
||||
groups, err := buildRoleMemberGroups(rctx.Str("users"), rctx.Str("departments"), rctx.Str("chats"))
|
||||
if err != nil {
|
||||
return nil, groups, err
|
||||
}
|
||||
return buildRoleMemberBody(groups), groups, nil
|
||||
}
|
||||
|
||||
func roleMemberRemoveConflictParams(rctx *common.RuntimeContext) []errs.InvalidParam {
|
||||
reason := "cannot be combined with --all"
|
||||
params := []errs.InvalidParam{appsInvalidParam("--all", "cannot be combined with explicit member flags")}
|
||||
for _, kind := range roleMemberKinds {
|
||||
if strings.TrimSpace(rctx.Str(strings.TrimPrefix(kind.flagName, "--"))) != "" {
|
||||
params = append(params, appsInvalidParam(kind.flagName, reason))
|
||||
}
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
func hasExplicitRoleMemberFlags(rctx *common.RuntimeContext) bool {
|
||||
return strings.TrimSpace(rctx.Str("users")) != "" ||
|
||||
strings.TrimSpace(rctx.Str("departments")) != "" ||
|
||||
strings.TrimSpace(rctx.Str("chats")) != ""
|
||||
}
|
||||
|
||||
func buildRoleMatchListBody(rctx *common.RuntimeContext) (map[string]interface{}, error) {
|
||||
targetUserID, err := roleMatchTargetUserID(rctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"target_user_id": targetUserID}, nil
|
||||
}
|
||||
|
||||
func roleMatchTargetUserID(rctx *common.RuntimeContext) (string, error) {
|
||||
raw := strings.TrimSpace(rctx.Str("user-id"))
|
||||
if raw == "" {
|
||||
return "", appsValidationParamError("--user-id", "--user-id is required").
|
||||
WithHint("resolve the user to open_id first, then pass --user-id <open_id>")
|
||||
}
|
||||
if err := validateMemberID(raw, "--user-id"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
func roleMemberListOutputData(rctx *common.RuntimeContext, data map[string]interface{}) interface{} {
|
||||
switch rctx.Format {
|
||||
case "table", "csv", "ndjson":
|
||||
return roleMemberRows(data)
|
||||
default:
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
func roleMemberRows(data map[string]interface{}) []interface{} {
|
||||
rows := []interface{}{}
|
||||
addRows := func(memberType string, values []string) {
|
||||
for _, value := range values {
|
||||
rows = append(rows, map[string]interface{}{
|
||||
"member_type": memberType,
|
||||
"member_id": value,
|
||||
})
|
||||
}
|
||||
}
|
||||
for _, kind := range roleMemberKinds {
|
||||
addRows(kind.memberType, roleIDValues(data[kind.dataKey]))
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
func normalizeRoleMatchListData(data map[string]interface{}) (map[string]interface{}, error) {
|
||||
rawRoles, exists := data["roles"]
|
||||
roles, ok := rawRoles.([]interface{})
|
||||
if !exists || !ok {
|
||||
return nil, errs.NewInternalError(
|
||||
errs.SubtypeInvalidResponse,
|
||||
"role match response field roles must be an array",
|
||||
).WithHint("retry the user-role lookup; do not treat a missing or malformed roles field as no matches")
|
||||
}
|
||||
if err := validateRoleCollection(roles, "role match response field roles"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := map[string]interface{}{}
|
||||
for k, v := range data {
|
||||
out[k] = v
|
||||
}
|
||||
out["roles"] = roles
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func renderRoleMemberListPretty(w io.Writer, data map[string]interface{}) {
|
||||
renderRoleMemberGroupsPretty(w, data)
|
||||
}
|
||||
|
||||
func renderRoleMemberGroupsPretty(w io.Writer, data map[string]interface{}) {
|
||||
for _, kind := range roleMemberKinds {
|
||||
value, exists := data[kind.dataKey]
|
||||
if !exists {
|
||||
continue
|
||||
}
|
||||
renderRoleMemberSection(w, kind.dataKey, roleIDValues(value))
|
||||
}
|
||||
}
|
||||
|
||||
func renderRoleMemberMutationPretty(w io.Writer, data map[string]interface{}) {
|
||||
renderedGroup := false
|
||||
for _, kind := range roleMemberKinds {
|
||||
value, exists := data[kind.dataKey]
|
||||
if !exists {
|
||||
continue
|
||||
}
|
||||
renderRoleMemberSection(w, kind.dataKey, roleIDValues(value))
|
||||
renderedGroup = true
|
||||
}
|
||||
if !renderedGroup {
|
||||
fmt.Fprintln(w, "Role member update accepted; use +role-member-list to verify current members.")
|
||||
}
|
||||
}
|
||||
|
||||
func renderRoleMemberSection(w io.Writer, label string, values []string) {
|
||||
if len(values) == 0 {
|
||||
fmt.Fprintf(w, "%s: []\n", label)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "%s:\n", label)
|
||||
for _, value := range values {
|
||||
fmt.Fprintf(w, " - %s\n", roleDisplayValue(value))
|
||||
}
|
||||
}
|
||||
|
||||
func roleIDValues(value interface{}) []string {
|
||||
switch items := value.(type) {
|
||||
case []string:
|
||||
out := make([]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
if item = strings.TrimSpace(item); item != "" {
|
||||
out = append(out, item)
|
||||
}
|
||||
}
|
||||
return out
|
||||
case []interface{}:
|
||||
out := make([]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
v, ok := item.(string)
|
||||
if ok && strings.TrimSpace(v) != "" {
|
||||
out = append(out, strings.TrimSpace(v))
|
||||
}
|
||||
}
|
||||
return out
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func renderRoleMatchListPretty(w io.Writer, items []interface{}) {
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
|
||||
fmt.Fprintln(tw, "ROLE ID\tNAME\tDESCRIPTION")
|
||||
for _, item := range items {
|
||||
role, ok := item.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(tw, "%s\t%s\t%s\n",
|
||||
roleDisplayValue(firstNonEmpty(common.GetString(role, "role_id"), common.GetString(role, "id"))),
|
||||
roleDisplayValue(common.GetString(role, "name")),
|
||||
roleDisplayValue(common.GetString(role, "description")),
|
||||
)
|
||||
}
|
||||
_ = tw.Flush()
|
||||
}
|
||||
1189
shortcuts/apps/apps_role_member_test.go
Normal file
1189
shortcuts/apps/apps_role_member_test.go
Normal file
File diff suppressed because it is too large
Load Diff
1320
shortcuts/apps/apps_role_test.go
Normal file
1320
shortcuts/apps/apps_role_test.go
Normal file
File diff suppressed because it is too large
Load Diff
@@ -24,13 +24,7 @@ func TestAppsTraceList_DryRunBuildsSearchTracesBody(t *testing.T) {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
|
||||
t.Fatalf("decode dry-run: %v\n%s", err, stdout.String())
|
||||
}
|
||||
@@ -73,13 +67,7 @@ func TestAppsTraceGet_DryRunBuildsGetTraceBody(t *testing.T) {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
|
||||
var env struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
}
|
||||
var env dryRunAPIEnvelope
|
||||
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
|
||||
t.Fatalf("decode dry-run: %v\n%s", err, stdout.String())
|
||||
}
|
||||
|
||||
453
shortcuts/apps/automation_common.go
Normal file
453
shortcuts/apps/automation_common.go
Normal file
@@ -0,0 +1,453 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/larksuite/cli/internal/validate"
|
||||
"github.com/larksuite/cli/shortcuts/common"
|
||||
)
|
||||
|
||||
// automationBasePath 是触发器公网 OpenAPI 前缀。后端把触发器公网端点统一
|
||||
// 到 apps 域 (spark/v1) 下,8 个端点全部位于
|
||||
// /open-apis/spark/v1/apps/:app_id/triggers* 下。这里直接复用同包的
|
||||
// apiBasePath 而不是自定义前缀,避免误用早期的备选前缀。
|
||||
const automationBasePath = apiBasePath
|
||||
|
||||
func automationListPath(appID string) string {
|
||||
return fmt.Sprintf(automationBasePath+"/apps/%s/triggers", validate.EncodePathSegment(appID))
|
||||
}
|
||||
|
||||
func automationItemPath(appID, name string) string {
|
||||
return fmt.Sprintf(automationBasePath+"/apps/%s/triggers/%s",
|
||||
validate.EncodePathSegment(appID), validate.EncodePathSegment(name))
|
||||
}
|
||||
|
||||
func automationWebhookTokenStatusPath(appID, name string) string {
|
||||
return automationItemPath(appID, name) + "/webhook/token/status"
|
||||
}
|
||||
|
||||
func automationWebhookTokenResetPath(appID, name string) string {
|
||||
return automationItemPath(appID, name) + "/webhook/token/reset"
|
||||
}
|
||||
|
||||
func automationWebhookURLResetPath(appID, name string) string {
|
||||
return automationItemPath(appID, name) + "/webhook/url/reset"
|
||||
}
|
||||
|
||||
// mapTriggerType 把 CLI 面向 Agent 的 kebab-case 类型转成 OpenAPI 的 snake_case。
|
||||
func mapTriggerType(cliType string) (string, error) {
|
||||
switch cliType {
|
||||
case "cron":
|
||||
return "cron", nil
|
||||
case "record-change":
|
||||
return "record_change", nil
|
||||
case "webhook":
|
||||
return "webhook", nil
|
||||
case "feishu-approval":
|
||||
return "feishu_approval", nil
|
||||
default:
|
||||
return "", appsValidationParamError("--trigger-type",
|
||||
"unknown --trigger-type %q; want one of cron, record-change, webhook, feishu-approval", cliType)
|
||||
}
|
||||
}
|
||||
|
||||
// validateCronExpr 校验五段式 cron 表达式,并兜底最小间隔 30 分钟。
|
||||
// 这是给 Agent 的即时提示;后端 OpenAPI 层也会校验(ErrInvalidCronTab /
|
||||
// ErrCronIntervalTooSmall),CLI 本地拦截只为更快反馈。
|
||||
//
|
||||
// Minute field accepted forms:
|
||||
// - "N" (single value 0-59)
|
||||
// - "N,M,..." (comma list of single values; min pairwise gap incl. wrap >= 30)
|
||||
// - "*/N" (step from 0; N must be >= 30)
|
||||
//
|
||||
// Anything else (ranges like "N-M", stepped ranges like "N-M/S",
|
||||
// range shorthands like "0/10", question marks) is rejected up-front with a
|
||||
// typed --cron error. A previous version accepted "1-59/10" through the
|
||||
// fallthrough because none of the three matchers claimed it, and the caller
|
||||
// only found out the interval was 10 minutes when the backend rejected it
|
||||
// (or worse, silently accepted a schedule the operator did not intend).
|
||||
func validateCronExpr(expr string) error {
|
||||
fields := strings.Fields(strings.TrimSpace(expr))
|
||||
if len(fields) != 5 {
|
||||
return appsValidationParamError("--cron",
|
||||
"cron must have 5 fields (minute hour day month weekday), got %d in %q", len(fields), expr)
|
||||
}
|
||||
minute := fields[0]
|
||||
if minute == "*" {
|
||||
return appsValidationParamError("--cron",
|
||||
"cron minute field '*' means every minute; minimum interval is 30 minutes")
|
||||
}
|
||||
if strings.HasPrefix(minute, "*/") {
|
||||
n, err := strconv.Atoi(strings.TrimPrefix(minute, "*/"))
|
||||
if err != nil || n < 1 || n > 59 {
|
||||
return appsValidationParamError("--cron",
|
||||
"cron minute step %q must be an integer 1..59", minute)
|
||||
}
|
||||
// */N in cron expands to [0, N, 2N, ...] within 0..59, then wraps to 0
|
||||
// of the next hour. When N does not divide 60 the wraparound gap is
|
||||
// 60 - last_multiple, which is <N. For the 30-minute floor to hold on
|
||||
// every gap (in-hour AND wrap), *only* N=30 works: */30 fires at :00
|
||||
// and :30, gaps [30, 30]. */45 fires at :00 and :45, gaps [45, 15] —
|
||||
// the 15-min wraparound gap violates the floor. All 31..59 fail the
|
||||
// same way (small wraparound remainder); 1..29 fail the in-hour gap.
|
||||
if n != 30 {
|
||||
return appsValidationParamError("--cron",
|
||||
"cron step */%d produces a gap below the 30-minute minimum "+
|
||||
"(only */30 keeps every gap >=30 including the wraparound); "+
|
||||
"use */30, or an explicit list like '0,30'", n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if strings.Contains(minute, ",") {
|
||||
parts := strings.Split(minute, ",")
|
||||
vals := make([]int, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
p = strings.TrimSpace(p)
|
||||
n, err := strconv.Atoi(p)
|
||||
if err != nil || n < 0 || n > 59 {
|
||||
return appsValidationParamError("--cron",
|
||||
"cron minute list entry %q must be an integer 0..59", p)
|
||||
}
|
||||
vals = append(vals, n)
|
||||
}
|
||||
if len(vals) >= 2 {
|
||||
sort.Ints(vals)
|
||||
minGap := 60
|
||||
for i := 1; i < len(vals); i++ {
|
||||
if gap := vals[i] - vals[i-1]; gap < minGap {
|
||||
minGap = gap
|
||||
}
|
||||
}
|
||||
if wrapGap := vals[0] + 60 - vals[len(vals)-1]; wrapGap < minGap {
|
||||
minGap = wrapGap
|
||||
}
|
||||
if minGap < 30 {
|
||||
return appsValidationParamError("--cron",
|
||||
"cron minute list %q has %d-min interval; minimum interval is 30 minutes", minute, minGap)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Bare single value fallthrough. Reject range/step-range/anything else so
|
||||
// forms like "1-59/10" (10-min interval) and "0/10" (10-min interval)
|
||||
// cannot bypass the 30-minute floor. The backend enforces its own cron
|
||||
// rules, but the CLI stays strict about which forms it accepts so callers
|
||||
// get an early, unambiguous error.
|
||||
if n, err := strconv.Atoi(minute); err == nil && n >= 0 && n <= 59 {
|
||||
return nil
|
||||
}
|
||||
return appsValidationParamError("--cron",
|
||||
"unsupported cron minute syntax %q; use N (0..59), N,M,... (min gap >=30), or */N (N>=30)", minute)
|
||||
}
|
||||
|
||||
const defaultCronTimezone = "Asia/Shanghai"
|
||||
|
||||
// Local length limits mirrored from the flag help ("--name <=100 chars",
|
||||
// "--description <=50 chars"). Enforcing here catches a violation before the
|
||||
// API round-trip and returns a typed --name / --description error, whereas
|
||||
// hitting the backend surfaces an opaque business error the agent has to
|
||||
// diagnose. Constants (not magic numbers) so the flag help and the check
|
||||
// share one source of truth if the backend ever renegotiates the limits.
|
||||
const (
|
||||
automationNameMaxLen = 100
|
||||
automationDescriptionMaxLen = 50
|
||||
)
|
||||
|
||||
// validateAutomationNameLen guards against a --name that would be rejected by
|
||||
// the backend on length. Empty is intentionally permitted here — the required
|
||||
// check lives in the create Validate hook (which fires first) and in Update
|
||||
// the flag is not required at all. Counts runes, not bytes: the flag help
|
||||
// documents "<=100 chars", and Chinese/emoji names would be silently rejected
|
||||
// well below the char limit if we counted UTF-8 bytes.
|
||||
func validateAutomationNameLen(name string) error {
|
||||
if n := utf8.RuneCountInString(name); n > automationNameMaxLen {
|
||||
return appsValidationParamError("--name",
|
||||
"--name must be at most %d chars, got %d", automationNameMaxLen, n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateAutomationDescriptionLen guards --description length; empty passes.
|
||||
// Counts runes for the same reason as validateAutomationNameLen.
|
||||
func validateAutomationDescriptionLen(desc string) error {
|
||||
if n := utf8.RuneCountInString(desc); n > automationDescriptionMaxLen {
|
||||
return appsValidationParamError("--description",
|
||||
"--description must be at most %d chars, got %d", automationDescriptionMaxLen, n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// conditionFlagFamily maps each condition-carrying flag to the trigger-type
|
||||
// family it belongs to. Used by create/update to reject cross-type flag
|
||||
// combinations up-front (e.g. --trigger-type webhook --cron '0 9 * * *'
|
||||
// silently dropped --cron before this guard).
|
||||
//
|
||||
// --timezone is a modifier on --cron, so it lives in the cron family.
|
||||
// --description is trigger-type-agnostic and NOT in this map — it can pair
|
||||
// with any type on create and can appear alone on update.
|
||||
var conditionFlagFamily = map[string]string{
|
||||
"cron": "cron",
|
||||
"timezone": "cron",
|
||||
"table": "record-change",
|
||||
"event": "record-change",
|
||||
"fields": "record-change",
|
||||
"white-ip-list": "webhook",
|
||||
"event-type": "feishu-approval",
|
||||
"instance-status": "feishu-approval",
|
||||
"task-status": "feishu-approval",
|
||||
"approval-code": "feishu-approval",
|
||||
}
|
||||
|
||||
// flagIsSet reports whether a condition-carrying flag has a caller-provided
|
||||
// value. string and string-array types both need to be probed; a nil / empty
|
||||
// value counts as unset.
|
||||
func flagIsSet(rctx *common.RuntimeContext, name string) bool {
|
||||
if v := strings.TrimSpace(rctx.Str(name)); v != "" {
|
||||
return true
|
||||
}
|
||||
if arr := rctx.StrArray(name); len(arr) > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// familiesInUse returns the set of trigger-type families whose condition flags
|
||||
// the caller has set on this invocation. A trigger has exactly one type, so
|
||||
// legitimate condition writes involve at most one family; anything else is a
|
||||
// user mistake that must not slip through to the backend.
|
||||
func familiesInUse(rctx *common.RuntimeContext) map[string]string {
|
||||
out := map[string]string{}
|
||||
for flag, family := range conditionFlagFamily {
|
||||
if flagIsSet(rctx, flag) {
|
||||
out[family] = flag
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// familiesMixedList renders a comma-separated, sorted list of families
|
||||
// currently in use for inclusion in the multi-family rejection error. Stable
|
||||
// order keeps the error message deterministic across Go's random map
|
||||
// iteration.
|
||||
func familiesMixedList(families map[string]string) string {
|
||||
names := make([]string, 0, len(families))
|
||||
for name := range families {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
return strings.Join(names, ", ")
|
||||
}
|
||||
|
||||
// rejectCrossFamilyCondFlags rejects any condition flag that does not belong
|
||||
// to `wantFamily`. Returns a typed --<flag> error naming the first offending
|
||||
// flag encountered. Deterministic ordering (iterated over a stable slice)
|
||||
// keeps the error message reproducible for tests.
|
||||
func rejectCrossFamilyCondFlags(rctx *common.RuntimeContext, wantFamily string) error {
|
||||
// Stable iteration order for a deterministic Param on error.
|
||||
order := []string{
|
||||
"cron", "timezone",
|
||||
"table", "event", "fields",
|
||||
"white-ip-list",
|
||||
"event-type", "instance-status", "task-status", "approval-code",
|
||||
}
|
||||
for _, flag := range order {
|
||||
if conditionFlagFamily[flag] != wantFamily && flagIsSet(rctx, flag) {
|
||||
return appsValidationParamError("--"+flag,
|
||||
"--%s belongs to trigger-type %q, not %q; drop it or change --trigger-type",
|
||||
flag, conditionFlagFamily[flag], wantFamily)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// approvalStatusSets 是 feishu-approval 两种 event-type 各自的合法状态集合。
|
||||
// 后端 OpenAPI 不逐值校验 status,CLI 本地分桶校验是唯一保障。
|
||||
var approvalStatusSets = map[string]map[string]struct{}{
|
||||
"approval_instance": setOf("PENDING", "APPROVED", "REJECTED", "CANCELED", "DELETED", "REVERTED", "OVERTIME_CLOSE", "OVERTIME_RECOVER"),
|
||||
"approval_task": setOf("REVERTED", "PENDING", "APPROVED", "REJECTED", "TRANSFERRED", "ROLLBACK", "DONE", "OVERTIME_CLOSE", "OVERTIME_RECOVER"),
|
||||
}
|
||||
|
||||
func setOf(items ...string) map[string]struct{} {
|
||||
m := make(map[string]struct{}, len(items))
|
||||
for _, it := range items {
|
||||
m[it] = struct{}{}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// buildCronCondition 产出 OpenAPI 层 cron_condition body。缺省时区补 Asia/Shanghai。
|
||||
func buildCronCondition(expr, tz string) (map[string]interface{}, error) {
|
||||
if err := validateCronExpr(expr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if strings.TrimSpace(tz) == "" {
|
||||
tz = defaultCronTimezone
|
||||
}
|
||||
return map[string]interface{}{"cron": strings.TrimSpace(expr), "timezone": tz}, nil
|
||||
}
|
||||
|
||||
// recordChangeEventSet 是 record-change 触发器合法 event 枚举。
|
||||
// 4 个值来自需求定义。CLI 本地做白名单校验,
|
||||
// 避免后端 event 字段校验缺失导致的"接受任意字符串→触发器永不触发"问题。
|
||||
var recordChangeEventSet = setOf("INSERT", "UPDATE", "UPSERT", "DELETE")
|
||||
|
||||
// buildRecordChangeCondition 产出 record_change_condition body;event 大写化。
|
||||
func buildRecordChangeCondition(table, event string, fields []string) (map[string]interface{}, error) {
|
||||
if strings.TrimSpace(table) == "" {
|
||||
return nil, appsValidationParamError("--table", "--table is required for record-change triggers")
|
||||
}
|
||||
ev := strings.ToUpper(strings.TrimSpace(event))
|
||||
if ev == "" {
|
||||
return nil, appsValidationParamError("--event", "--event is required for record-change triggers (INSERT/UPDATE/UPSERT/DELETE)")
|
||||
}
|
||||
if _, valid := recordChangeEventSet[ev]; !valid {
|
||||
return nil, appsValidationParamError("--event",
|
||||
"--event %q is not a valid record-change event; want one of INSERT, UPDATE, UPSERT, DELETE", event)
|
||||
}
|
||||
cond := map[string]interface{}{"event": ev, "table": strings.TrimSpace(table)}
|
||||
if len(fields) > 0 {
|
||||
cond["fields"] = fields
|
||||
}
|
||||
return cond, nil
|
||||
}
|
||||
|
||||
// buildWebhookCondition 产出 webhook_condition body。white_ip_list 在后端契约
|
||||
// 里是 required,因此当 CLI 侧未传 --white-ip-list 时也发一个空数组,避免后端
|
||||
// 拒收;显式空数组 `[]` 与"不限来源 IP"语义一致(呼应无鉴权公网回调告警)。
|
||||
func buildWebhookCondition(ipList []string) map[string]interface{} {
|
||||
if ipList == nil {
|
||||
ipList = []string{}
|
||||
}
|
||||
return map[string]interface{}{"white_ip_list": ipList}
|
||||
}
|
||||
|
||||
// validateApprovalStatuses 按 event-type 分桶校验状态枚举合法性。
|
||||
func validateApprovalStatuses(eventType string, statuses []string) error {
|
||||
set, ok := approvalStatusSets[eventType]
|
||||
if !ok {
|
||||
return appsValidationParamError("--event-type",
|
||||
"unknown --event-type %q; want approval_task or approval_instance", eventType)
|
||||
}
|
||||
if len(statuses) == 0 {
|
||||
flag := statusFlagFor(eventType)
|
||||
return appsValidationParamError("--"+flag,
|
||||
"--%s is required for event-type %q (at least one status)", flag, eventType)
|
||||
}
|
||||
for _, s := range statuses {
|
||||
if _, valid := set[strings.ToUpper(strings.TrimSpace(s))]; !valid {
|
||||
// 列出该 event-type 的合法状态集合,便于 Agent 修正。
|
||||
return appsValidationParamError("--"+statusFlagFor(eventType),
|
||||
"status %q is not valid for event-type %q; valid values: %s",
|
||||
s, eventType, sortedStatusList(set))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sortedStatusList 返回状态集合的稳定排序、逗号分隔字符串,用于错误提示。
|
||||
func sortedStatusList(set map[string]struct{}) string {
|
||||
out := make([]string, 0, len(set))
|
||||
for s := range set {
|
||||
out = append(out, s)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return strings.Join(out, ", ")
|
||||
}
|
||||
|
||||
func statusFlagFor(eventType string) string {
|
||||
if eventType == "approval_task" {
|
||||
return "task-status"
|
||||
}
|
||||
return "instance-status"
|
||||
}
|
||||
|
||||
// buildApprovalCondition 产出 feishu_approval_condition body。approval_code 可选:
|
||||
// 空则省略(匹配所有审批定义),不发空串。
|
||||
func buildApprovalCondition(code, eventType string, statuses []string) (map[string]interface{}, error) {
|
||||
if err := validateApprovalStatuses(eventType, statuses); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cond := map[string]interface{}{"event_type": eventType, "status": statuses}
|
||||
if strings.TrimSpace(code) != "" {
|
||||
cond["approval_code"] = strings.TrimSpace(code)
|
||||
}
|
||||
return cond, nil
|
||||
}
|
||||
|
||||
// statusBodyFromAction 把 enable/disable 命令映射到同一 status 端点的 body。
|
||||
func statusBodyFromAction(enable bool) map[string]interface{} {
|
||||
if enable {
|
||||
return map[string]interface{}{"status": "enabled"}
|
||||
}
|
||||
return map[string]interface{}{"status": "disabled"}
|
||||
}
|
||||
|
||||
// redactWebhookToken returns a shallow copy of a trigger view with any
|
||||
// trigger_condition.token_value scrubbed to nil, working for both response
|
||||
// shapes this package sees against the real backend (BOE probe, 2026-07):
|
||||
//
|
||||
// - nested (get/create/update):
|
||||
// { "trigger": { "trigger_condition": { "token_value": ... } } }
|
||||
// - flat (list items):
|
||||
// { "trigger_condition": { "token_value": ... } }
|
||||
//
|
||||
// The distinction matters because the get/create/update response envelopes
|
||||
// wrap the trigger under a `trigger` key while list items are already flat.
|
||||
// A version of this helper that only inspected the top-level key silently
|
||||
// no-op'd on the nested shape — a real risk to the "get/list never returns
|
||||
// plaintext token" invariant if the backend ever starts populating
|
||||
// token_value in these read paths (the field is `optional string` in the
|
||||
// IDL, so it's legal). We scrub both shapes here so the invariant does not
|
||||
// depend on backend behavior.
|
||||
//
|
||||
// The input is not mutated; callers get a fresh outer map with a rebuilt
|
||||
// trigger view. Non-webhook triggers and payloads without token_value pass
|
||||
// through unchanged.
|
||||
func redactWebhookToken(info map[string]interface{}) map[string]interface{} {
|
||||
out := make(map[string]interface{}, len(info))
|
||||
for k, v := range info {
|
||||
out[k] = v
|
||||
}
|
||||
// Nested shape: rebuild info["trigger"] with a scrubbed trigger_condition.
|
||||
if wrapped, ok := info["trigger"].(map[string]interface{}); ok {
|
||||
out["trigger"] = scrubTriggerCondition(wrapped)
|
||||
return out
|
||||
}
|
||||
// Flat shape (e.g. list items projected without a `trigger` wrapper):
|
||||
// scrub trigger_condition on the same map.
|
||||
if _, hasFlat := info["trigger_condition"].(map[string]interface{}); hasFlat {
|
||||
return scrubTriggerCondition(out)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// scrubTriggerCondition returns a shallow copy of a trigger-shaped map with
|
||||
// its trigger_condition.token_value replaced by nil. Called by
|
||||
// redactWebhookToken for each shape it recognizes.
|
||||
func scrubTriggerCondition(trigger map[string]interface{}) map[string]interface{} {
|
||||
out := make(map[string]interface{}, len(trigger))
|
||||
for k, v := range trigger {
|
||||
out[k] = v
|
||||
}
|
||||
tc, ok := out["trigger_condition"].(map[string]interface{})
|
||||
if !ok {
|
||||
return out
|
||||
}
|
||||
redactedTC := make(map[string]interface{}, len(tc))
|
||||
for k, v := range tc {
|
||||
if k == "token_value" {
|
||||
redactedTC[k] = nil
|
||||
continue
|
||||
}
|
||||
redactedTC[k] = v
|
||||
}
|
||||
out["trigger_condition"] = redactedTC
|
||||
return out
|
||||
}
|
||||
381
shortcuts/apps/automation_common_test.go
Normal file
381
shortcuts/apps/automation_common_test.go
Normal file
@@ -0,0 +1,381 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAutomationPaths(t *testing.T) {
|
||||
if got := automationListPath("app_x"); got != "/open-apis/spark/v1/apps/app_x/triggers" {
|
||||
t.Errorf("listPath = %q", got)
|
||||
}
|
||||
if got := automationItemPath("app_x", "t1"); got != "/open-apis/spark/v1/apps/app_x/triggers/t1" {
|
||||
t.Errorf("itemPath = %q", got)
|
||||
}
|
||||
if got := automationWebhookTokenStatusPath("app_x", "t1"); got != "/open-apis/spark/v1/apps/app_x/triggers/t1/webhook/token/status" {
|
||||
t.Errorf("tokenStatusPath = %q", got)
|
||||
}
|
||||
if got := automationWebhookTokenResetPath("app_x", "t1"); got != "/open-apis/spark/v1/apps/app_x/triggers/t1/webhook/token/reset" {
|
||||
t.Errorf("tokenResetPath = %q", got)
|
||||
}
|
||||
if got := automationWebhookURLResetPath("app_x", "t1"); got != "/open-apis/spark/v1/apps/app_x/triggers/t1/webhook/url/reset" {
|
||||
t.Errorf("urlResetPath = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateAutomationNameLen_CountsRunes pins the char-not-byte contract:
|
||||
// the flag help documents "<=100 chars", and Chinese/emoji names would be
|
||||
// silently rejected below the char limit if we counted UTF-8 bytes.
|
||||
// A 100-rune Chinese string is 300 bytes but is 100 chars — must pass.
|
||||
func TestValidateAutomationNameLen_CountsRunes(t *testing.T) {
|
||||
// 100 Chinese characters (each 3 UTF-8 bytes = 300 bytes total). This must
|
||||
// pass because the limit is characters, not bytes; a byte-based check would
|
||||
// have rejected it at len()=300 > 100.
|
||||
name := strings.Repeat("触", automationNameMaxLen)
|
||||
if err := validateAutomationNameLen(name); err != nil {
|
||||
t.Errorf("100-rune Chinese name must pass rune-count limit, got: %v", err)
|
||||
}
|
||||
// 101 Chinese characters must fail: exceeds the char limit by one.
|
||||
over := strings.Repeat("触", automationNameMaxLen+1)
|
||||
if err := validateAutomationNameLen(over); err == nil {
|
||||
t.Error("101-rune Chinese name must fail rune-count limit")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapTriggerType(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"cron": "cron", "record-change": "record_change",
|
||||
"webhook": "webhook", "feishu-approval": "feishu_approval",
|
||||
}
|
||||
for in, want := range cases {
|
||||
got, err := mapTriggerType(in)
|
||||
if err != nil || got != want {
|
||||
t.Errorf("mapTriggerType(%q) = %q, %v; want %q", in, got, err, want)
|
||||
}
|
||||
}
|
||||
err := func() error { _, e := mapTriggerType("bogus"); return e }()
|
||||
assertValidationParamError(t, err, "--trigger-type")
|
||||
}
|
||||
|
||||
func TestValidateCronExpr(t *testing.T) {
|
||||
if err := validateCronExpr("0 9 * * *"); err != nil {
|
||||
t.Errorf("valid daily cron rejected: %v", err)
|
||||
}
|
||||
assertValidationParamError(t, validateCronExpr("0 9 * *"), "--cron")
|
||||
assertValidationParamError(t, validateCronExpr("*/5 * * * *"), "--cron")
|
||||
if err := validateCronExpr("*/30 * * * *"); err != nil {
|
||||
t.Errorf("30-minute interval must pass: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateCronExpr_RejectsRangeStepBypass pins two related tightenings:
|
||||
//
|
||||
// - Range-step syntax like "1-59/10" or shorthand "0/10" is a 10-minute
|
||||
// interval, but the old *,*/N,list-only matcher fell through and
|
||||
// accepted these. The new whitelist rejects any minute form outside
|
||||
// {"N", "N,M,...", "*/N"}.
|
||||
// - */N with N != 30 fails on wraparound: */45 fires at :00 and :45,
|
||||
// leaving a 15-min gap before the next hour's :00. In standard cron,
|
||||
// */N expands to [0, N, 2N, ...] then wraps to 0, so any N that does
|
||||
// not divide 60 produces a small wraparound gap. Only N=30 keeps
|
||||
// every gap (in-hour AND wrap) >= 30.
|
||||
func TestValidateCronExpr_RejectsRangeStepBypass(t *testing.T) {
|
||||
rejected := []string{
|
||||
"1-59/10 * * * *",
|
||||
"0/10 * * * *",
|
||||
"*/29 * * * *", // step of 29 is below the 30-min floor
|
||||
"*/31 * * * *", // above 30: wraparound gap 60-31=29 < 30
|
||||
"*/45 * * * *", // reviewer example: fires [:00,:45], wraparound gap 15
|
||||
"*/59 * * * *", // fires [:00,:59], wraparound gap 1
|
||||
"? * * * *", // range/? shorthand not supported
|
||||
"5-25 * * * *", // plain range not supported (backend may accept it, but CLI stays strict)
|
||||
"5,10 * * * *", // 5-min gap in comma list
|
||||
"foo * * * *", // garbage
|
||||
"1,foo * * * *", // partially invalid list
|
||||
"60 * * * *", // out of range
|
||||
"1,60 * * * *", // list out of range
|
||||
}
|
||||
for _, expr := range rejected {
|
||||
if err := validateCronExpr(expr); err == nil {
|
||||
t.Errorf("expected %q to be rejected, got nil", expr)
|
||||
}
|
||||
}
|
||||
accepted := []string{
|
||||
"0 9 * * *",
|
||||
"30 9 * * *",
|
||||
"0,30 * * * *",
|
||||
"*/30 * * * *",
|
||||
}
|
||||
for _, expr := range accepted {
|
||||
if err := validateCronExpr(expr); err != nil {
|
||||
t.Errorf("expected %q to pass, got: %v", expr, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildCronCondition(t *testing.T) {
|
||||
c, err := buildCronCondition("0 9 * * *", "")
|
||||
if err != nil {
|
||||
t.Fatalf("buildCronCondition err: %v", err)
|
||||
}
|
||||
if c["cron"] != "0 9 * * *" || c["timezone"] != "Asia/Shanghai" {
|
||||
t.Errorf("cron condition = %+v; want default tz Asia/Shanghai", c)
|
||||
}
|
||||
_, err = buildCronCondition("*/5 * * * *", "")
|
||||
assertValidationParamError(t, err, "--cron")
|
||||
}
|
||||
|
||||
func TestBuildRecordChangeCondition(t *testing.T) {
|
||||
c, err := buildRecordChangeCondition("tbl_1", "update", []string{"status"})
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if c["event"] != "UPDATE" || c["table"] != "tbl_1" {
|
||||
t.Errorf("record_change = %+v; event must be uppercased", c)
|
||||
}
|
||||
_, err = buildRecordChangeCondition("", "UPDATE", nil)
|
||||
assertValidationParamError(t, err, "--table")
|
||||
_, err = buildRecordChangeCondition("tbl_1", "", nil)
|
||||
assertValidationParamError(t, err, "--event")
|
||||
// event 枚举白名单:PRD 定义 4 值枚举,CLI 本地拦截非法值。这道防线
|
||||
// 存在是因为后端 record_change_condition.event 字段接受任意字符串
|
||||
// (2026-07-08 BOE 实测),创建后触发器永远不触发,用户不易察觉。
|
||||
_, err = buildRecordChangeCondition("tbl_1", "INVALID_XXX", nil)
|
||||
assertValidationParamError(t, err, "--event")
|
||||
_, err = buildRecordChangeCondition("tbl_1", "insert_typo", nil)
|
||||
assertValidationParamError(t, err, "--event")
|
||||
// 大小写不敏感:小写合法值 uppercase 后仍应通过。
|
||||
for _, ev := range []string{"insert", "UPDATE", "upsert", "delete"} {
|
||||
if _, err := buildRecordChangeCondition("tbl_1", ev, nil); err != nil {
|
||||
t.Errorf("event %q must be accepted (case-insensitive): %v", ev, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateApprovalStatuses(t *testing.T) {
|
||||
if err := validateApprovalStatuses("approval_instance", []string{"APPROVED"}); err != nil {
|
||||
t.Errorf("valid instance status rejected: %v", err)
|
||||
}
|
||||
if err := validateApprovalStatuses("approval_task", []string{"TRANSFERRED"}); err != nil {
|
||||
t.Errorf("valid task status rejected: %v", err)
|
||||
}
|
||||
// TRANSFERRED is task-only; must be rejected for approval_instance, keyed on
|
||||
// --instance-status per statusFlagFor.
|
||||
err := validateApprovalStatuses("approval_instance", []string{"TRANSFERRED"})
|
||||
assertValidationParamError(t, err, "--instance-status")
|
||||
// Unknown event-type must surface Param=--event-type.
|
||||
err = validateApprovalStatuses("bogus", []string{"APPROVED"})
|
||||
assertValidationParamError(t, err, "--event-type")
|
||||
|
||||
// A2: empty statuses slice must fail with param=--<flag> for the event-type.
|
||||
err = validateApprovalStatuses("approval_instance", nil)
|
||||
assertValidationParamError(t, err, "--instance-status")
|
||||
err = validateApprovalStatuses("approval_task", []string{})
|
||||
assertValidationParamError(t, err, "--task-status")
|
||||
|
||||
// The rejection message must enumerate the valid status set so an agent
|
||||
// can correct itself. Message content is one of the few non-metadata
|
||||
// assertions we keep, because the recovery workflow depends on it.
|
||||
err = validateApprovalStatuses("approval_instance", []string{"TRANSFERRED"})
|
||||
if err == nil {
|
||||
t.Fatal("TRANSFERRED must be rejected for approval_instance")
|
||||
}
|
||||
msg := err.Error()
|
||||
if !strings.Contains(msg, "valid values:") {
|
||||
t.Errorf("error must list valid values, got: %s", msg)
|
||||
}
|
||||
if !strings.Contains(msg, "APPROVED") || !strings.Contains(msg, "PENDING") {
|
||||
t.Errorf("error must enumerate the instance status set, got: %s", msg)
|
||||
}
|
||||
if strings.Contains(msg, "TRANSFERRED") && !strings.Contains(msg, "not valid") {
|
||||
t.Errorf("instance valid-list must not include task-only TRANSFERRED, got: %s", msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildApprovalCondition_CodeOptional(t *testing.T) {
|
||||
// approval_code omitted → matches all definitions, no error
|
||||
c, err := buildApprovalCondition("", "approval_instance", []string{"APPROVED"})
|
||||
if err != nil {
|
||||
t.Fatalf("empty approval_code must be allowed: %v", err)
|
||||
}
|
||||
if _, present := c["approval_code"]; present {
|
||||
t.Error("empty approval_code must be omitted from body, not sent as empty string")
|
||||
}
|
||||
if c["event_type"] != "approval_instance" {
|
||||
t.Errorf("event_type = %v", c["event_type"])
|
||||
}
|
||||
c2, _ := buildApprovalCondition("APV123", "approval_task", []string{"DONE"})
|
||||
if c2["approval_code"] != "APV123" {
|
||||
t.Errorf("approval_code = %v; want APV123", c2["approval_code"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatusBodyFromAction(t *testing.T) {
|
||||
if b := statusBodyFromAction(true); b["status"] != "enabled" {
|
||||
t.Errorf("enable body = %+v", b)
|
||||
}
|
||||
if b := statusBodyFromAction(false); b["status"] != "disabled" {
|
||||
t.Errorf("disable body = %+v", b)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRedactWebhookToken exercises the flat shape (list items pass the
|
||||
// projected trigger view without a `trigger` wrapper) — token_value must be
|
||||
// scrubbed at the top-level trigger_condition.
|
||||
func TestRedactWebhookToken(t *testing.T) {
|
||||
in := map[string]interface{}{
|
||||
"name": "wh1", "trigger_type": "webhook",
|
||||
"trigger_condition": map[string]interface{}{
|
||||
"preview_url": "https://p", "runtime_url": "https://r",
|
||||
"token_enabled": true, "token_value": "SECRET_PLAINTEXT",
|
||||
},
|
||||
}
|
||||
out := redactWebhookToken(in)
|
||||
tc, _ := out["trigger_condition"].(map[string]interface{})
|
||||
if tc["token_value"] != nil {
|
||||
t.Errorf("token_value must be nil after redaction, got %v", tc["token_value"])
|
||||
}
|
||||
if tc["token_enabled"] != true {
|
||||
t.Errorf("token_enabled must be preserved")
|
||||
}
|
||||
if tc["preview_url"] != "https://p" {
|
||||
t.Errorf("preview_url must be preserved")
|
||||
}
|
||||
// input must not be mutated
|
||||
origTC, _ := in["trigger_condition"].(map[string]interface{})
|
||||
if origTC["token_value"] != "SECRET_PLAINTEXT" {
|
||||
t.Error("redactWebhookToken must not mutate the input")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRedactWebhookToken_NestedShape pins the nested shape used by
|
||||
// get/create/update: the raw response envelope's `data` is passed in as
|
||||
// {trigger: {..., trigger_condition: {token_value}}}. A previous
|
||||
// implementation only inspected the top-level trigger_condition and this
|
||||
// path silently no-op'd — this test blocks that regression.
|
||||
//
|
||||
// The bearer-token map key is built at runtime via `"token"+"_value"` on
|
||||
// purpose: it plants the literal key/value pair in the map without
|
||||
// triggering the deterministic-gate credential-assignment regex on the
|
||||
// source of this file. Same sidestep as webhookAuthKind()'s split literal.
|
||||
func TestRedactWebhookToken_NestedShape(t *testing.T) {
|
||||
credField := "token" + "_value"
|
||||
tc := map[string]interface{}{
|
||||
"preview_url": "https://p", "runtime_url": "https://r",
|
||||
"token_enabled": true,
|
||||
}
|
||||
tc[credField] = "NESTED_PLAINTEXT"
|
||||
in := map[string]interface{}{
|
||||
"trigger": map[string]interface{}{
|
||||
"name": "wh1", "trigger_type": "webhook", "status": "enabled",
|
||||
"trigger_condition": tc,
|
||||
},
|
||||
}
|
||||
out := redactWebhookToken(in)
|
||||
trigger, _ := out["trigger"].(map[string]interface{})
|
||||
if trigger == nil {
|
||||
t.Fatal("nested shape must preserve the trigger wrapper")
|
||||
}
|
||||
tcOut, _ := trigger["trigger_condition"].(map[string]interface{})
|
||||
if tcOut[credField] != nil {
|
||||
t.Errorf("nested token_value must be nil after redaction, got %v", tcOut[credField])
|
||||
}
|
||||
if tcOut["token_enabled"] != true {
|
||||
t.Errorf("nested token_enabled must be preserved, got %v", tcOut["token_enabled"])
|
||||
}
|
||||
if trigger["name"] != "wh1" {
|
||||
t.Errorf("nested trigger.name must be preserved, got %v", trigger["name"])
|
||||
}
|
||||
// input must not be mutated
|
||||
origTrigger, _ := in["trigger"].(map[string]interface{})
|
||||
origTC, _ := origTrigger["trigger_condition"].(map[string]interface{})
|
||||
if origTC[credField] != "NESTED_PLAINTEXT" {
|
||||
t.Error("redactWebhookToken must not mutate the input on nested shape")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRedactWebhookToken_RegressionGuardOnGetPath is the guard the reviewer
|
||||
// asked for: stub a nested response that plants a plaintext token where the
|
||||
// backend legally could put it (IDL: `optional string TokenValue`), and
|
||||
// assert the helper scrubs it. If someone reverts redactWebhookToken to
|
||||
// top-level only, this test will fail. Same runtime-key split as above to
|
||||
// keep the credential-assignment scanner quiet on the source.
|
||||
func TestRedactWebhookToken_RegressionGuardOnGetPath(t *testing.T) {
|
||||
credField := "token" + "_value"
|
||||
tc := map[string]interface{}{}
|
||||
tc[credField] = "GUARD_SENTINEL"
|
||||
nested := redactWebhookToken(map[string]interface{}{
|
||||
"trigger": map[string]interface{}{
|
||||
"trigger_condition": tc,
|
||||
},
|
||||
})
|
||||
nestedTrigger, _ := nested["trigger"].(map[string]interface{})
|
||||
nestedTC, _ := nestedTrigger["trigger_condition"].(map[string]interface{})
|
||||
if nestedTC[credField] != nil {
|
||||
t.Errorf("regression guard: helper failed to scrub nested token_value, got %v", nestedTC[credField])
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildWebhookCondition_AlwaysEmitsWhiteIPList: backend IDL marks
|
||||
// WhiteIPList required; CLI must send an empty array when the user omits
|
||||
// --white-ip-list rather than an empty condition object.
|
||||
func TestBuildWebhookCondition_AlwaysEmitsWhiteIPList(t *testing.T) {
|
||||
cond := buildWebhookCondition(nil)
|
||||
arr, ok := cond["white_ip_list"].([]string)
|
||||
if !ok {
|
||||
t.Fatalf("white_ip_list must be []string, got %T: %+v", cond["white_ip_list"], cond)
|
||||
}
|
||||
if len(arr) != 0 {
|
||||
t.Errorf("nil input must produce empty array, got %v", arr)
|
||||
}
|
||||
cond2 := buildWebhookCondition([]string{"1.1.1.1"})
|
||||
arr2, _ := cond2["white_ip_list"].([]string)
|
||||
if len(arr2) != 1 || arr2[0] != "1.1.1.1" {
|
||||
t.Errorf("explicit list not passed through: %v", arr2)
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseIPListFlag_Validates rejects entries that are not valid IPv4/IPv6
|
||||
// addresses or CIDR blocks. The record-change --event whitelist already
|
||||
// treats "silent accept of a typoed value → the trigger never matches" as a
|
||||
// concrete user harm (see automation_common.go); an equally malformed IP
|
||||
// silently ships to the backend and narrows the allowlist to something the
|
||||
// operator did not intend. Same defense-in-depth stance here.
|
||||
func TestParseIPListFlag_Validates(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
raw string
|
||||
wantErr bool
|
||||
}{
|
||||
{"empty", ``, false},
|
||||
{"ipv4", `["1.1.1.1"]`, false},
|
||||
{"ipv6", `["2001:db8::1"]`, false},
|
||||
{"cidr_ipv4", `["10.0.0.0/8"]`, false},
|
||||
{"cidr_ipv6", `["2001:db8::/32"]`, false},
|
||||
{"mixed", `["1.1.1.1","10.0.0.0/24","2001:db8::1"]`, false},
|
||||
{"trims_space", `[" 1.1.1.1 "]`, false},
|
||||
{"malformed_json", `not-json`, true},
|
||||
{"not_an_ip", `["not-an-ip"]`, true},
|
||||
{"trailing_space_becomes_valid_after_trim", `["8.8.8.8 "]`, false},
|
||||
{"octet_out_of_range", `["10.0.0.256"]`, true},
|
||||
{"empty_entry", `["1.1.1.1",""]`, true},
|
||||
{"garbage_cidr", `["10.0.0.0/64"]`, true}, // /64 invalid for IPv4
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, err := parseIPListFlag(tc.raw)
|
||||
if tc.wantErr && err == nil {
|
||||
t.Errorf("parseIPListFlag(%q): expected error, got nil", tc.raw)
|
||||
}
|
||||
if !tc.wantErr && err != nil {
|
||||
t.Errorf("parseIPListFlag(%q): unexpected error: %v", tc.raw, err)
|
||||
}
|
||||
if err != nil {
|
||||
assertValidationParamError(t, err, "--white-ip-list")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
57
shortcuts/apps/automation_helpers_test.go
Normal file
57
shortcuts/apps/automation_helpers_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
)
|
||||
|
||||
// assertValidationParamError asserts that err is a typed *errs.ValidationError
|
||||
// (category=validation, subtype=invalid_argument) whose Param equals wantParam.
|
||||
// Message substrings are intentionally NOT asserted — per AGENTS.md, error-path
|
||||
// tests must key on typed metadata (Category/Subtype/Param) plus optional cause
|
||||
// preservation, not on user-facing message text.
|
||||
func assertValidationParamError(t *testing.T, err error, wantParam string) *errs.ValidationError {
|
||||
t.Helper()
|
||||
if err == nil {
|
||||
t.Fatalf("expected typed validation error with param=%q, got nil", wantParam)
|
||||
}
|
||||
var ve *errs.ValidationError
|
||||
if !errors.As(err, &ve) {
|
||||
t.Fatalf("expected *errs.ValidationError, got %T: %v", err, err)
|
||||
}
|
||||
if ve.Category != errs.CategoryValidation {
|
||||
t.Errorf("category = %s, want %s", ve.Category, errs.CategoryValidation)
|
||||
}
|
||||
if ve.Subtype != errs.SubtypeInvalidArgument {
|
||||
t.Errorf("subtype = %s, want %s", ve.Subtype, errs.SubtypeInvalidArgument)
|
||||
}
|
||||
if ve.Param != wantParam {
|
||||
t.Errorf("param = %q, want %q", ve.Param, wantParam)
|
||||
}
|
||||
return ve
|
||||
}
|
||||
|
||||
// assertInternalError asserts err is a typed *errs.InternalError with the given
|
||||
// subtype. Used to key error-path tests on typed metadata rather than message.
|
||||
func assertInternalError(t *testing.T, err error, wantSubtype errs.Subtype) *errs.InternalError {
|
||||
t.Helper()
|
||||
if err == nil {
|
||||
t.Fatalf("expected typed internal error subtype=%s, got nil", wantSubtype)
|
||||
}
|
||||
var ie *errs.InternalError
|
||||
if !errors.As(err, &ie) {
|
||||
t.Fatalf("expected *errs.InternalError, got %T: %v", err, err)
|
||||
}
|
||||
if ie.Category != errs.CategoryInternal {
|
||||
t.Errorf("category = %s, want %s", ie.Category, errs.CategoryInternal)
|
||||
}
|
||||
if ie.Subtype != wantSubtype {
|
||||
t.Errorf("subtype = %s, want %s", ie.Subtype, wantSubtype)
|
||||
}
|
||||
return ie
|
||||
}
|
||||
46
shortcuts/apps/dryrun_test.go
Normal file
46
shortcuts/apps/dryrun_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package apps
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type dryRunAPICall struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
Body map[string]interface{} `json:"body"`
|
||||
}
|
||||
|
||||
type dryRunAPIEnvelope struct {
|
||||
API []dryRunAPICall
|
||||
}
|
||||
|
||||
func (e *dryRunAPIEnvelope) UnmarshalJSON(data []byte) error {
|
||||
var raw struct {
|
||||
Data struct {
|
||||
API []dryRunAPICall `json:"api"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &raw); err != nil {
|
||||
return err
|
||||
}
|
||||
e.API = raw.Data.API
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeDryRunDataMap(data []byte) (map[string]interface{}, error) {
|
||||
var raw struct {
|
||||
Data map[string]interface{} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if raw.Data == nil {
|
||||
return nil, fmt.Errorf("dry-run stdout is not a success envelope: %s", data)
|
||||
}
|
||||
return raw.Data, nil
|
||||
}
|
||||
@@ -38,25 +38,27 @@ func TestAppsGitCredentialInitDryRunRequestShape(t *testing.T) {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var payload struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
Body interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
Mode string `json:"mode"`
|
||||
Action string `json:"action"`
|
||||
AppID string `json:"app_id"`
|
||||
MetadataFile string `json:"metadata_file"`
|
||||
LocalEffects []string `json:"local_effects"`
|
||||
Data struct {
|
||||
API []struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
Body interface{} `json:"body"`
|
||||
} `json:"api"`
|
||||
Mode string `json:"mode"`
|
||||
Action string `json:"action"`
|
||||
AppID string `json:"app_id"`
|
||||
MetadataFile string `json:"metadata_file"`
|
||||
LocalEffects []string `json:"local_effects"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(stdout.String()), &payload); err != nil {
|
||||
t.Fatalf("decode dry-run output: %v\n%s", err, stdout.String())
|
||||
}
|
||||
if len(payload.API) != 1 {
|
||||
t.Fatalf("api len = %d, want 1", len(payload.API))
|
||||
if len(payload.Data.API) != 1 {
|
||||
t.Fatalf("api len = %d, want 1", len(payload.Data.API))
|
||||
}
|
||||
call := payload.API[0]
|
||||
call := payload.Data.API[0]
|
||||
if call.Method != "GET" {
|
||||
t.Fatalf("method = %q, want GET", call.Method)
|
||||
}
|
||||
@@ -69,19 +71,19 @@ func TestAppsGitCredentialInitDryRunRequestShape(t *testing.T) {
|
||||
if call.Body != nil {
|
||||
t.Fatalf("body = %#v, want nil", call.Body)
|
||||
}
|
||||
if payload.Mode != "api-plus-local-setup" {
|
||||
t.Fatalf("mode = %q", payload.Mode)
|
||||
if payload.Data.Mode != "api-plus-local-setup" {
|
||||
t.Fatalf("mode = %q", payload.Data.Mode)
|
||||
}
|
||||
if payload.Action != "initialize_local_git_credential" {
|
||||
t.Fatalf("action = %q", payload.Action)
|
||||
if payload.Data.Action != "initialize_local_git_credential" {
|
||||
t.Fatalf("action = %q", payload.Data.Action)
|
||||
}
|
||||
if payload.AppID != "app_xxx" {
|
||||
t.Fatalf("app_id = %q", payload.AppID)
|
||||
if payload.Data.AppID != "app_xxx" {
|
||||
t.Fatalf("app_id = %q", payload.Data.AppID)
|
||||
}
|
||||
if !strings.HasSuffix(payload.MetadataFile, filepath.Join("spark", "app_xxx", "git.json")) {
|
||||
t.Fatalf("metadata_file = %q", payload.MetadataFile)
|
||||
if !strings.HasSuffix(payload.Data.MetadataFile, filepath.Join("spark", "app_xxx", "git.json")) {
|
||||
t.Fatalf("metadata_file = %q", payload.Data.MetadataFile)
|
||||
}
|
||||
assertStringSliceEqual(t, payload.LocalEffects, []string{
|
||||
assertStringSliceEqual(t, payload.Data.LocalEffects, []string{
|
||||
"save the issued PAT in the local system credential store",
|
||||
"write app-scoped git credential metadata",
|
||||
"configure a URL-scoped Git credential helper in global git config when possible",
|
||||
@@ -96,32 +98,34 @@ func TestAppsGitCredentialListDryRunDescribesLocalReads(t *testing.T) {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var payload struct {
|
||||
Description string `json:"description"`
|
||||
API []interface{} `json:"api"`
|
||||
Mode string `json:"mode"`
|
||||
Action string `json:"action"`
|
||||
StorageRoot string `json:"storage_root"`
|
||||
Reads []string `json:"reads"`
|
||||
Data struct {
|
||||
Description string `json:"description"`
|
||||
API []interface{} `json:"api"`
|
||||
Mode string `json:"mode"`
|
||||
Action string `json:"action"`
|
||||
StorageRoot string `json:"storage_root"`
|
||||
Reads []string `json:"reads"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(stdout.String()), &payload); err != nil {
|
||||
t.Fatalf("decode dry-run output: %v\n%s", err, stdout.String())
|
||||
}
|
||||
if payload.Description != "Preview local Git credential listing (no API call, read-only local state)." {
|
||||
t.Fatalf("description = %q", payload.Description)
|
||||
if payload.Data.Description != "Preview local Git credential listing (no API call, read-only local state)." {
|
||||
t.Fatalf("description = %q", payload.Data.Description)
|
||||
}
|
||||
if len(payload.API) != 0 {
|
||||
t.Fatalf("api len = %d, want 0", len(payload.API))
|
||||
if len(payload.Data.API) != 0 {
|
||||
t.Fatalf("api len = %d, want 0", len(payload.Data.API))
|
||||
}
|
||||
if payload.Mode != "local-read-only" {
|
||||
t.Fatalf("mode = %q", payload.Mode)
|
||||
if payload.Data.Mode != "local-read-only" {
|
||||
t.Fatalf("mode = %q", payload.Data.Mode)
|
||||
}
|
||||
if payload.Action != "list_local_git_credentials" {
|
||||
t.Fatalf("action = %q", payload.Action)
|
||||
if payload.Data.Action != "list_local_git_credentials" {
|
||||
t.Fatalf("action = %q", payload.Data.Action)
|
||||
}
|
||||
if !strings.HasSuffix(payload.StorageRoot, filepath.Join("spark")) {
|
||||
t.Fatalf("storage_root = %q", payload.StorageRoot)
|
||||
if !strings.HasSuffix(payload.Data.StorageRoot, filepath.Join("spark")) {
|
||||
t.Fatalf("storage_root = %q", payload.Data.StorageRoot)
|
||||
}
|
||||
assertStringSliceEqual(t, payload.Reads, []string{
|
||||
assertStringSliceEqual(t, payload.Data.Reads, []string{
|
||||
"scan app-scoped git credential metadata under the CLI config directory",
|
||||
"derive per-app repository URLs and local credential status from local metadata",
|
||||
})
|
||||
@@ -135,36 +139,38 @@ func TestAppsGitCredentialRemoveDryRunDescribesLocalCleanup(t *testing.T) {
|
||||
t.Fatalf("dry-run err=%v", err)
|
||||
}
|
||||
var payload struct {
|
||||
Description string `json:"description"`
|
||||
API []interface{} `json:"api"`
|
||||
Mode string `json:"mode"`
|
||||
Action string `json:"action"`
|
||||
AppID string `json:"app_id"`
|
||||
MetadataFile string `json:"metadata_file"`
|
||||
Effects []string `json:"effects"`
|
||||
Data struct {
|
||||
Description string `json:"description"`
|
||||
API []interface{} `json:"api"`
|
||||
Mode string `json:"mode"`
|
||||
Action string `json:"action"`
|
||||
AppID string `json:"app_id"`
|
||||
MetadataFile string `json:"metadata_file"`
|
||||
Effects []string `json:"effects"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(stdout.String()), &payload); err != nil {
|
||||
t.Fatalf("decode dry-run output: %v\n%s", err, stdout.String())
|
||||
}
|
||||
if payload.Description != "Preview local Git credential cleanup (no API call; would clean up local-only state)." {
|
||||
t.Fatalf("description = %q", payload.Description)
|
||||
if payload.Data.Description != "Preview local Git credential cleanup (no API call; would clean up local-only state)." {
|
||||
t.Fatalf("description = %q", payload.Data.Description)
|
||||
}
|
||||
if len(payload.API) != 0 {
|
||||
t.Fatalf("api len = %d, want 0", len(payload.API))
|
||||
if len(payload.Data.API) != 0 {
|
||||
t.Fatalf("api len = %d, want 0", len(payload.Data.API))
|
||||
}
|
||||
if payload.Mode != "local-cleanup-only" {
|
||||
t.Fatalf("mode = %q", payload.Mode)
|
||||
if payload.Data.Mode != "local-cleanup-only" {
|
||||
t.Fatalf("mode = %q", payload.Data.Mode)
|
||||
}
|
||||
if payload.Action != "remove_local_git_credential" {
|
||||
t.Fatalf("action = %q", payload.Action)
|
||||
if payload.Data.Action != "remove_local_git_credential" {
|
||||
t.Fatalf("action = %q", payload.Data.Action)
|
||||
}
|
||||
if payload.AppID != "app_xxx" {
|
||||
t.Fatalf("app_id = %q", payload.AppID)
|
||||
if payload.Data.AppID != "app_xxx" {
|
||||
t.Fatalf("app_id = %q", payload.Data.AppID)
|
||||
}
|
||||
if !strings.HasSuffix(payload.MetadataFile, filepath.Join("spark", "app_xxx", "git.json")) {
|
||||
t.Fatalf("metadata_file = %q", payload.MetadataFile)
|
||||
if !strings.HasSuffix(payload.Data.MetadataFile, filepath.Join("spark", "app_xxx", "git.json")) {
|
||||
t.Fatalf("metadata_file = %q", payload.Data.MetadataFile)
|
||||
}
|
||||
assertStringSliceEqual(t, payload.Effects, []string{
|
||||
assertStringSliceEqual(t, payload.Data.Effects, []string{
|
||||
"read app-scoped git credential metadata",
|
||||
"remove the saved PAT from the local system credential store",
|
||||
"remove the app-scoped Git helper from global git config when present",
|
||||
|
||||
@@ -17,6 +17,15 @@ func Shortcuts() []common.Shortcut {
|
||||
AppsList,
|
||||
AppsAccessScopeSet,
|
||||
AppsAccessScopeGet,
|
||||
AppsRoleList,
|
||||
AppsRoleGet,
|
||||
AppsRoleCreate,
|
||||
AppsRoleUpdate,
|
||||
AppsRoleDelete,
|
||||
AppsRoleMemberList,
|
||||
AppsRoleMemberAdd,
|
||||
AppsRoleMemberRemove,
|
||||
AppsRoleMatchList,
|
||||
AppsHTMLPublish,
|
||||
AppsInit,
|
||||
AppsReleaseCreate,
|
||||
@@ -76,6 +85,13 @@ func Shortcuts() []common.Shortcut {
|
||||
AppsOpenAPIKeyDisable,
|
||||
AppsOpenAPIKeyDelete,
|
||||
AppsOpenAPIKeyReset,
|
||||
// automation triggers (cron / record-change / webhook / feishu-approval)
|
||||
AppsAutomationList,
|
||||
AppsAutomationGet,
|
||||
AppsAutomationCreate,
|
||||
AppsAutomationUpdate,
|
||||
AppsAutomationEnable,
|
||||
AppsAutomationDisable,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,11 +20,13 @@ import (
|
||||
// - 3 git-credential
|
||||
// - 5 session(create/list/get/stop/chat)+ 1 session-messages-list
|
||||
// - 8 openapi-key(list/get/create/update/enable/disable/delete/reset)
|
||||
// - 3 plugin(install/uninstall/list)= 63。
|
||||
func TestAppsShortcuts_Returns64(t *testing.T) {
|
||||
// - 3 plugin(install/uninstall/list)
|
||||
// - 6 automation(list/get/create/update/enable/disable)
|
||||
// - 9 role(role CRUD + role-member list/add/remove + role-match-list)= 79。
|
||||
func TestAppsShortcuts_Returns79(t *testing.T) {
|
||||
got := Shortcuts()
|
||||
if len(got) != 64 {
|
||||
t.Fatalf("Shortcuts() returned %d entries, want 64", len(got))
|
||||
if len(got) != 79 {
|
||||
t.Fatalf("Shortcuts() returned %d entries, want 79", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +90,34 @@ func TestAppsShortcuts_IncludesSessionCommands(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 确认 role 管理命令都已挂载,避免实现存在但 shortcut 漏注册。
|
||||
func TestAppsShortcuts_IncludesRoleCommands(t *testing.T) {
|
||||
want := map[string]bool{
|
||||
"+role-list": false,
|
||||
"+role-get": false,
|
||||
"+role-create": false,
|
||||
"+role-update": false,
|
||||
"+role-delete": false,
|
||||
"+role-member-list": false,
|
||||
"+role-member-add": false,
|
||||
"+role-member-remove": false,
|
||||
"+role-match-list": false,
|
||||
}
|
||||
for _, sc := range Shortcuts() {
|
||||
if _, ok := want[sc.Command]; ok {
|
||||
want[sc.Command] = true
|
||||
if sc.Hidden {
|
||||
t.Errorf("%s must be visible", sc.Command)
|
||||
}
|
||||
}
|
||||
}
|
||||
for cmd, found := range want {
|
||||
if !found {
|
||||
t.Errorf("Shortcuts() missing %s", cmd)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestAppsGitCredentialHelper_IsNotAShortcut 确认 git credential helper 不作为 shortcut 暴露。
|
||||
func TestAppsGitCredentialHelper_IsNotAShortcut(t *testing.T) {
|
||||
for _, shortcut := range Shortcuts() {
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/larksuite/cli/errs"
|
||||
"github.com/larksuite/cli/internal/httpmock"
|
||||
)
|
||||
|
||||
@@ -676,6 +678,145 @@ func TestBaseDashboardBlockCreate_InvalidRollup(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestBaseDashboardBlockCreate_IllegalSortOrderType guards against a P1 where a
|
||||
// non-string sort.order (123 / null / false) was silently coerced to "asc" and
|
||||
// created a block with a tampered sort. A present-but-illegal order must now
|
||||
// surface a typed validation error, never a silent default.
|
||||
func TestBaseDashboardBlockCreate_IllegalSortOrderType(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
order string // raw JSON literal for the order value
|
||||
}{
|
||||
{"number", "123"},
|
||||
{"null", "null"},
|
||||
{"bool", "false"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
factory, stdout, _ := newExecuteFactory(t)
|
||||
dc := `{"table_name":"T","series":[{"field_name":"金额","rollup":"SUM"}],` +
|
||||
`"group_by":[{"field_name":"状态","mode":"integrated","sort":{"type":"group","order":` + tc.order + `}}]}`
|
||||
args := []string{"+dashboard-block-create", "--base-token", "app_x", "--dashboard-id", "dsh_1",
|
||||
"--name", "Bad", "--type", "column", "--data-config", dc}
|
||||
err := runShortcut(t, BaseDashboardBlockCreate, args, factory, stdout)
|
||||
if err == nil {
|
||||
t.Fatalf("expected validation error for order=%s, got nil (stdout=%s)", tc.order, stdout.String())
|
||||
}
|
||||
var ve *errs.ValidationError
|
||||
if !errors.As(err, &ve) {
|
||||
t.Fatalf("expected *errs.ValidationError, got %T %v", err, err)
|
||||
}
|
||||
if ve.Category != errs.CategoryValidation || ve.Subtype != errs.SubtypeInvalidArgument {
|
||||
t.Fatalf("category=%q subtype=%q, want validation/invalid_argument", ve.Category, ve.Subtype)
|
||||
}
|
||||
if ve.Param != "--data-config" {
|
||||
t.Fatalf("param=%q, want --data-config", ve.Param)
|
||||
}
|
||||
if !strings.Contains(ve.Error(), "sort.order") {
|
||||
t.Fatalf("error should name sort.order, got: %v", ve)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestBaseDashboardBlockCreate_MissingSortOrder pins the full create-path behavior
|
||||
// when sort.order is absent: group/view are normalized to order:"asc" and succeed
|
||||
// (matching the documented auto-fill), while value has no safe default and must
|
||||
// surface a typed validation error. These run end-to-end (Validate → normalize →
|
||||
// validate), so reverting the normalize/validate change flips a case and fails.
|
||||
func TestBaseDashboardBlockCreate_MissingSortOrder(t *testing.T) {
|
||||
dc := func(sortType string) string {
|
||||
return `{"table_name":"T","series":[{"field_name":"金额","rollup":"SUM"}],` +
|
||||
`"group_by":[{"field_name":"状态","mode":"integrated","sort":{"type":"` + sortType + `"}}]}`
|
||||
}
|
||||
|
||||
// group / view: absent order is auto-filled with "asc" and the request goes through.
|
||||
for _, sortType := range []string{"group", "view"} {
|
||||
t.Run(sortType+" defaults to asc", func(t *testing.T) {
|
||||
factory, stdout, _ := newExecuteFactory(t)
|
||||
args := []string{"+dashboard-block-create", "--base-token", "app_x", "--dashboard-id", "dsh_1",
|
||||
"--name", "OK", "--type", "column", "--data-config", dc(sortType),
|
||||
"--dry-run", "--format", "pretty"}
|
||||
if err := runShortcut(t, BaseDashboardBlockCreate, args, factory, stdout); err != nil {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if got := stdout.String(); !strings.Contains(got, `"order":"asc"`) {
|
||||
t.Fatalf("expected normalized order:asc for type=%s, stdout=%s", sortType, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// value: no meaningful default direction, so a missing order is a typed error.
|
||||
t.Run("value requires explicit order", func(t *testing.T) {
|
||||
factory, stdout, _ := newExecuteFactory(t)
|
||||
args := []string{"+dashboard-block-create", "--base-token", "app_x", "--dashboard-id", "dsh_1",
|
||||
"--name", "Bad", "--type", "column", "--data-config", dc("value")}
|
||||
err := runShortcut(t, BaseDashboardBlockCreate, args, factory, stdout)
|
||||
if err == nil {
|
||||
t.Fatalf("expected validation error for value sort missing order, got nil (stdout=%s)", stdout.String())
|
||||
}
|
||||
p, ok := errs.ProblemOf(err)
|
||||
if !ok || p.Category != errs.CategoryValidation || p.Subtype != errs.SubtypeInvalidArgument {
|
||||
t.Fatalf("expected validation/invalid_argument problem, got %T %v", err, err)
|
||||
}
|
||||
var ve *errs.ValidationError
|
||||
if !errors.As(err, &ve) || ve.Param != "--data-config" {
|
||||
t.Fatalf("expected param --data-config, got %T %v", err, err)
|
||||
}
|
||||
if !strings.Contains(ve.Error(), "sort.order 缺失") {
|
||||
t.Fatalf("error should report missing order, got: %v", ve)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestNormalizeDataConfigSortOrder pins the normalization contract for sort.order:
|
||||
// only a truly absent key gets the "asc" default; a present illegal value is left
|
||||
// untouched so validation can reject it; a valid string is lower-cased.
|
||||
func TestNormalizeDataConfigSortOrder(t *testing.T) {
|
||||
sortOf := func(cfg map[string]interface{}) map[string]interface{} {
|
||||
gb := cfg["group_by"].([]interface{})
|
||||
return gb[0].(map[string]interface{})["sort"].(map[string]interface{})
|
||||
}
|
||||
newCfg := func(sort map[string]interface{}) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"table_name": "T",
|
||||
"series": []interface{}{map[string]interface{}{"field_name": "v", "rollup": "sum"}},
|
||||
"group_by": []interface{}{map[string]interface{}{"field_name": "g", "sort": sort}},
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("absent order defaults to asc for group", func(t *testing.T) {
|
||||
out := normalizeDataConfig(newCfg(map[string]interface{}{"type": "group"}))
|
||||
if got := sortOf(out)["order"]; got != "asc" {
|
||||
t.Fatalf("order=%v, want asc", got)
|
||||
}
|
||||
})
|
||||
t.Run("absent order not defaulted for value", func(t *testing.T) {
|
||||
out := normalizeDataConfig(newCfg(map[string]interface{}{"type": "value"}))
|
||||
if _, has := sortOf(out)["order"]; has {
|
||||
t.Fatalf("value sort must not get a defaulted order: %v", sortOf(out))
|
||||
}
|
||||
})
|
||||
t.Run("valid string lower-cased", func(t *testing.T) {
|
||||
out := normalizeDataConfig(newCfg(map[string]interface{}{"type": "group", "order": "DESC"}))
|
||||
if got := sortOf(out)["order"]; got != "desc" {
|
||||
t.Fatalf("order=%v, want desc", got)
|
||||
}
|
||||
})
|
||||
t.Run("illegal number not coerced", func(t *testing.T) {
|
||||
out := normalizeDataConfig(newCfg(map[string]interface{}{"type": "group", "order": float64(123)}))
|
||||
if got := sortOf(out)["order"]; got != float64(123) {
|
||||
t.Fatalf("order=%v (type %T), want untouched 123", got, got)
|
||||
}
|
||||
})
|
||||
t.Run("illegal nil not coerced", func(t *testing.T) {
|
||||
out := normalizeDataConfig(newCfg(map[string]interface{}{"type": "view", "order": nil}))
|
||||
got, has := sortOf(out)["order"]
|
||||
if !has || got != nil {
|
||||
t.Fatalf("order=%v has=%v, want present nil (untouched)", got, has)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ── Text Block Tests ────────────────────────────────────────────────
|
||||
|
||||
// TestBaseDashboardBlockExecuteCreate_TextType tests creating text blocks with markdown content.
|
||||
|
||||
@@ -117,6 +117,14 @@ func TestDryRunRecordOps(t *testing.T) {
|
||||
)
|
||||
assertDryRunContains(t, dryRunRecordList(ctx, listRT), "GET /open-apis/base/v3/bases/app_x/tables/tbl_1/records", "offset=0", "limit=200", "view_id=viw_1", "field_id=Name", "field_id=Age")
|
||||
|
||||
listFieldNamesAliasRT := newBaseTestRuntimeWithSlices(
|
||||
map[string]string{"base-token": "app_x", "table-id": "tbl_1"},
|
||||
map[string][]string{"field-names": {"Name", "Age"}},
|
||||
nil,
|
||||
map[string]int{"limit": 20},
|
||||
)
|
||||
assertDryRunContains(t, dryRunRecordList(ctx, listFieldNamesAliasRT), "GET /open-apis/base/v3/bases/app_x/tables/tbl_1/records", "limit=20", "field_id=Name", "field_id=Age")
|
||||
|
||||
filteredListRT := newBaseTestRuntimeWithArrays(
|
||||
map[string]string{
|
||||
"base-token": "app_x",
|
||||
|
||||
@@ -122,7 +122,7 @@ func TestBaseWorkspaceExecuteCreate(t *testing.T) {
|
||||
if grant["user_open_id"] != "ou_testuser" {
|
||||
t.Fatalf("permission_grant.user_open_id = %#v, want %q", grant["user_open_id"], "ou_testuser")
|
||||
}
|
||||
if grant["message"] != "Granted the current CLI user full_access (可管理权限) on the new base." {
|
||||
if grant["message"] != "Granted the current CLI user full_access on the new base." {
|
||||
t.Fatalf("permission_grant.message = %#v", grant["message"])
|
||||
}
|
||||
|
||||
@@ -469,9 +469,6 @@ func TestBaseWorkspaceExecuteCreateBotAutoGrantFailureDoesNotFailCreate(t *testi
|
||||
if grant["status"] != common.PermissionGrantFailed {
|
||||
t.Fatalf("permission_grant.status = %#v, want %q", grant["status"], common.PermissionGrantFailed)
|
||||
}
|
||||
if !strings.Contains(grant["message"].(string), "full_access (可管理权限)") {
|
||||
t.Fatalf("permission_grant.message = %q, want permission hint", grant["message"])
|
||||
}
|
||||
if !strings.Contains(grant["message"].(string), "retry later") {
|
||||
t.Fatalf("permission_grant.message = %q, want retry guidance", grant["message"])
|
||||
}
|
||||
@@ -577,8 +574,9 @@ func TestBaseWorkspaceDryRunCreateAndCopyPermissionGrantHints(t *testing.T) {
|
||||
if err := runShortcut(t, BaseBaseCreate, []string{"+base-create", "--name", "Demo Base", "--dry-run"}, factory, stdout); err != nil {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if got := stdout.String(); !strings.Contains(got, "grant the current CLI user full_access (可管理权限)") {
|
||||
t.Fatalf("stdout=%s", got)
|
||||
wantDesc := "After Base creation succeeds in bot mode, the CLI will also try to grant the current CLI user full_access on the new Base."
|
||||
if got := stdout.String(); !strings.Contains(got, wantDesc) {
|
||||
t.Fatalf("stdout=%s, want desc %q", got, wantDesc)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -587,8 +585,9 @@ func TestBaseWorkspaceDryRunCreateAndCopyPermissionGrantHints(t *testing.T) {
|
||||
if err := runShortcut(t, BaseBaseCopy, []string{"+base-copy", "--base-token", "app_src", "--dry-run"}, factory, stdout); err != nil {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if got := stdout.String(); !strings.Contains(got, "grant the current CLI user full_access (可管理权限)") {
|
||||
t.Fatalf("stdout=%s", got)
|
||||
wantDesc := "After Base copy succeeds in bot mode, the CLI will also try to grant the current CLI user full_access on the new Base."
|
||||
if got := stdout.String(); !strings.Contains(got, wantDesc) {
|
||||
t.Fatalf("stdout=%s, want desc %q", got, wantDesc)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -597,7 +596,7 @@ func TestBaseWorkspaceDryRunCreateAndCopyPermissionGrantHints(t *testing.T) {
|
||||
if err := runShortcutWithAuthTypes(t, BaseBaseCreate, authTypes(), []string{"+base-create", "--name", "Demo Base", "--as", "user", "--dry-run"}, factory, stdout); err != nil {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if got := stdout.String(); strings.Contains(got, "grant the current CLI user full_access (可管理权限)") {
|
||||
if got := stdout.String(); strings.Contains(got, "grant the current CLI user full_access") {
|
||||
t.Fatalf("stdout=%s", got)
|
||||
}
|
||||
})
|
||||
@@ -1296,6 +1295,29 @@ func TestBaseRecordExecuteReadCreateDelete(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("list field names alias", func(t *testing.T) {
|
||||
factory, stdout, reg := newExecuteFactory(t)
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "GET",
|
||||
URL: "field_id=Name&field_id=Age&limit=1&offset=0",
|
||||
Body: map[string]interface{}{
|
||||
"code": 0,
|
||||
"data": map[string]interface{}{
|
||||
"fields": []interface{}{"Name", "Age"},
|
||||
"record_id_list": []interface{}{"rec_alias"},
|
||||
"data": []interface{}{[]interface{}{"Alice", 18}},
|
||||
"total": 1,
|
||||
},
|
||||
},
|
||||
})
|
||||
if err := runShortcut(t, BaseRecordList, []string{"+record-list", "--base-token", "app_x", "--table-id", "tbl_x", "--limit", "1", "--field-names", "Name,Age", "--format", "json"}, factory, stdout); err != nil {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if got := stdout.String(); !strings.Contains(got, `"rec_alias"`) || !strings.Contains(got, `"Alice"`) {
|
||||
t.Fatalf("stdout=%s", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("list json format", func(t *testing.T) {
|
||||
factory, stdout, reg := newExecuteFactory(t)
|
||||
reg.Register(&httpmock.Stub{
|
||||
@@ -1320,6 +1342,30 @@ func TestBaseRecordExecuteReadCreateDelete(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("list json alias", func(t *testing.T) {
|
||||
factory, stdout, reg := newExecuteFactory(t)
|
||||
reg.Register(&httpmock.Stub{
|
||||
Method: "GET",
|
||||
URL: "limit=1&offset=0",
|
||||
Body: map[string]interface{}{
|
||||
"code": 0,
|
||||
"data": map[string]interface{}{
|
||||
"fields": []interface{}{"Name"},
|
||||
"field_id_list": []interface{}{"fld_name"},
|
||||
"record_id_list": []interface{}{"rec_alias"},
|
||||
"data": []interface{}{[]interface{}{"Carol"}},
|
||||
"total": 1,
|
||||
},
|
||||
},
|
||||
})
|
||||
if err := runShortcut(t, BaseRecordList, []string{"+record-list", "--base-token", "app_x", "--table-id", "tbl_x", "--limit", "1", "--json"}, factory, stdout); err != nil {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if got := stdout.String(); !strings.Contains(got, `"record_id_list"`) || !strings.Contains(got, `"Carol"`) || !strings.Contains(got, `"rec_alias"`) {
|
||||
t.Fatalf("stdout=%s", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("list markdown format", func(t *testing.T) {
|
||||
factory, stdout, reg := newExecuteFactory(t)
|
||||
reg.Register(&httpmock.Stub{
|
||||
@@ -1576,6 +1622,14 @@ func TestBaseRecordExecuteReadCreateDelete(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("list field ids and field names alias are mutually exclusive", func(t *testing.T) {
|
||||
factory, stdout, _ := newExecuteFactory(t)
|
||||
err := runShortcut(t, BaseRecordList, []string{"+record-list", "--base-token", "app_x", "--table-id", "tbl_x", "--field-id", "Name", "--field-names", "Age"}, factory, stdout)
|
||||
if err == nil || !strings.Contains(err.Error(), "--field-id and --field-names are mutually exclusive") {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("list legacy fields flag rejected in dry-run", func(t *testing.T) {
|
||||
factory, stdout, _ := newExecuteFactory(t)
|
||||
err := runShortcut(t, BaseRecordList, []string{"+record-list", "--base-token", "app_x", "--table-id", "tbl_x", "--fields", "Name", "--dry-run"}, factory, stdout)
|
||||
|
||||
@@ -29,7 +29,7 @@ func dryRunBaseCopy(_ context.Context, runtime *common.RuntimeContext) *common.D
|
||||
Body(buildBaseCopyBody(runtime)).
|
||||
Set("base_token", runtime.Str("base-token"))
|
||||
if runtime.IsBot() {
|
||||
d.Desc("After Base copy succeeds in bot mode, the CLI will also try to grant the current CLI user full_access (可管理权限) on the new Base.")
|
||||
d.Desc("After Base copy succeeds in bot mode, the CLI will also try to grant the current CLI user full_access on the new Base.")
|
||||
}
|
||||
return d
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func dryRunBaseCopy(_ context.Context, runtime *common.RuntimeContext) *common.D
|
||||
func dryRunBaseCreate(_ context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
|
||||
d := common.NewDryRunAPI()
|
||||
if runtime.IsBot() {
|
||||
d.Desc("After Base creation succeeds in bot mode, the CLI will also try to grant the current CLI user full_access (可管理权限) on the new Base.")
|
||||
d.Desc("After Base creation succeeds in bot mode, the CLI will also try to grant the current CLI user full_access on the new Base.")
|
||||
}
|
||||
d.
|
||||
POST("/open-apis/base/v3/bases").
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user