Files
2026-11-16 17:26:44 -08:00

111 lines
5.7 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
#
# meego_check.sh — probe Meego (飞书项目) Open API plugin permissions.
#
# Default run is SAFE / non-mutating: it only reads, and probes the comment /
# state-change endpoints with bodies that get rejected before any write.
# Add flags to perform a REAL write test on a ticket you're OK mutating.
#
# Usage:
# ./scripts/meego_check.sh [WORK_ITEM_ID]
# ./scripts/meego_check.sh 6931196005 --comment "权限测试,可删除" # real comment
# ./scripts/meego_check.sh 6931196005 --resolve # real OPEN→…→RESOLVED transition
#
# Config via env (falls back to the known values / repo .env):
# MEEGO_PLUGIN_ID, MEEGO_PLUGIN_SECRET, MEEGO_HOST, MEEGO_PROJECT,
# MEEGO_TYPE, MEEGO_USER_KEY
set -uo pipefail
# Pull MEEGO_USER_KEY (and any overrides) from repo .env if present.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
[ -f "$SCRIPT_DIR/../.env" ] && { set -a; . "$SCRIPT_DIR/../.env"; set +a; }
PLUGIN_ID="${MEEGO_PLUGIN_ID:-MII_630C308A7F02410D}"
PLUGIN_SECRET="${MEEGO_PLUGIN_SECRET:-07CD349464C75BCEEA0612152E2786DB}"
HOST="${MEEGO_HOST_OVERRIDE:-meego.larkoffice.com}"
PROJECT="${MEEGO_PROJECT:-tiktok}"
TYPE_KEY="${MEEGO_TYPE:-issue}"
USER_KEY="${MEEGO_USER_KEY:-}"
WORK_ITEM_ID="6931196005"
DO_COMMENT=""; COMMENT_TEXT=""; DO_RESOLVE=""; RESOLUTION="1" # 1=本人已修复
while [ $# -gt 0 ]; do
case "$1" in
--comment) DO_COMMENT=1; COMMENT_TEXT="${2:-Meego 权限测试}"; shift 2;;
--resolve) DO_RESOLVE=1; shift;;
--resolution) RESOLUTION="$2"; shift 2;; # bug_resolution 选项值: 1本人已修复 2第三方 3无法复现 ...
*) WORK_ITEM_ID="$1"; shift;;
esac
done
jget() { python3 -c "import sys,json
try: d=json.load(sys.stdin)
except: print(''); sys.exit()
for k in '$1'.split('.'):
d=d.get(k) if isinstance(d,dict) else None
print('' if d is None else (json.dumps(d,ensure_ascii=False) if isinstance(d,(dict,list)) else d))"; }
echo "host=$HOST project=$PROJECT type=$TYPE_KEY work_item=$WORK_ITEM_ID user_key_set=$([ -n "$USER_KEY" ] && echo yes || echo NO)"
[ -z "$USER_KEY" ] && { echo "!! MEEGO_USER_KEY 未设置(读写都需要 X-USER-KEY)"; exit 1; }
# 1) plugin token --------------------------------------------------------------
TOK_RESP=$(curl -s -m 25 -X POST "https://$HOST/open_api/authen/plugin_token" \
-H "Content-Type: application/json" \
-d "{\"plugin_id\":\"$PLUGIN_ID\",\"plugin_secret\":\"$PLUGIN_SECRET\",\"type\":0}")
PT=$(echo "$TOK_RESP" | jget data.token)
if [ -z "$PT" ]; then echo "1) plugin_token ❌ $(echo "$TOK_RESP" | head -c 200)"; exit 1; fi
echo "1) plugin_token ✅ (${PT:0:3}…, expire=$(echo "$TOK_RESP" | jget data.expire_time)s)"
H=(-H "Content-Type: application/json" -H "X-PLUGIN-TOKEN: $PT" -H "X-USER-KEY: $USER_KEY")
BASE="https://$HOST/open_api/$PROJECT"
# helper: POST, echo "HTTP <code> err=<err_code> <err_msg>"
post() { # url body
local body resp code
resp=$(curl -s -m 25 -o /tmp/_mc.txt -w "%{http_code}" -X POST "${H[@]}" "$1" -d "$2")
code="$resp"; body=$(cat /tmp/_mc.txt)
echo "HTTP $code err=$(echo "$body" | jget err_code) $(echo "$body" | jget err_msg)"
}
# 2) read work item ------------------------------------------------------------
RD=$(curl -s -m 25 -X POST "${H[@]}" "$BASE/work_item/$TYPE_KEY/query" -d "{\"work_item_ids\":[$WORK_ITEM_ID]}")
RDE=$(echo "$RD" | jget err_code)
if [ "$RDE" = "0" ]; then
NAME=$(echo "$RD" | python3 -c "import sys,json;d=json.load(sys.stdin)['data'][0];print(d.get('name',''))")
STATE=$(echo "$RD" | python3 -c "import sys,json;d=json.load(sys.stdin)['data'][0];print((d.get('work_item_status') or {}).get('state_key',''))")
echo "2) read item ✅ state=$STATE name=${NAME:0:40}"
else
echo "2) read item ❌ err=$RDE $(echo "$RD" | jget err_msg)"; STATE=""
fi
# 3) read workflow + find current→RESOLVED transition --------------------------
WF=$(curl -s -m 25 -X POST "${H[@]}" "$BASE/work_item/$TYPE_KEY/$WORK_ITEM_ID/workflow/query" -d '{"flow_type":1}')
TRID=$(echo "$WF" | STATE="$STATE" python3 -c "
import sys,json,os
d=json.load(sys.stdin); st=os.environ.get('STATE','')
conns=(d.get('data') or {}).get('connections',[]) if isinstance(d.get('data'),dict) else []
cand=[c for c in conns if c.get('target_state_key')=='RESOLVED']
exact=[c for c in cand if c.get('source_state_key')==st]
print((exact or cand or [{}])[0].get('transition_id',''))" 2>/dev/null)
echo "3) read workflow $([ -n "$TRID" ] && echo "✅ current($STATE)→RESOLVED transition_id=$TRID" || echo "⚠️ no RESOLVED transition found (err=$(echo "$WF" | jget err_code))")"
# 4) comment permission (non-mutating: empty body) -----------------------------
# 10301 = 插件无评论权限 ; 20005/缺参 = 有权限(只是没给内容)
echo -n "4) comment perm "; post "$BASE/work_item/$TYPE_KEY/$WORK_ITEM_ID/comment/create" '{}'
# 5) state-change permission (non-mutating: empty body) ------------------------
# 20005 缺 transition_id = 接口可达 ; 10001/403 = 无权限
echo -n "5) state perm "; post "https://$HOST/open_api/$PROJECT/workflow/$TYPE_KEY/$WORK_ITEM_ID/node/state_change" '{}'
# --- optional REAL writes -----------------------------------------------------
if [ -n "$DO_COMMENT" ]; then
echo -n ">> REAL comment "; post "$BASE/work_item/$TYPE_KEY/$WORK_ITEM_ID/comment/create" "{\"content\":\"$COMMENT_TEXT\"}"
fi
if [ -n "$DO_RESOLVE" ]; then
if [ -z "$TRID" ]; then echo ">> REAL resolve ❌ 找不到 →RESOLVED 的 transition_id"; else
echo -n ">> REAL resolve (transition $TRID, bug_resolution=$RESOLUTION, 不可逆!) "
post "https://$HOST/open_api/$PROJECT/workflow/$TYPE_KEY/$WORK_ITEM_ID/node/state_change" \
"{\"transition_id\":$TRID,\"field_value_pairs\":[{\"field_key\":\"bug_resolution\",\"field_value\":\"$RESOLUTION\"}]}"
fi
fi