|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Test suite for block-glab-api-discussions.sh |
| 3 | +# Runs representative BLOCK/ALLOW cases; exits non-zero on any mismatch. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +HERE="$(cd "$(dirname "$0")" && pwd)" |
| 8 | +HOOK="$HERE/block-glab-api-discussions.sh" |
| 9 | + |
| 10 | +if [ ! -x "$HOOK" ]; then |
| 11 | + echo "hook not executable: $HOOK" >&2 |
| 12 | + exit 2 |
| 13 | +fi |
| 14 | + |
| 15 | +pass=0 |
| 16 | +fail=0 |
| 17 | + |
| 18 | +run_case() { |
| 19 | + local expect="$1" cmd="$2" out got |
| 20 | + out=$(jq -nc --arg c "$cmd" '{tool_input:{command:$c}}' | "$HOOK") |
| 21 | + if [ -n "$out" ]; then got=BLOCK; else got=ALLOW; fi |
| 22 | + if [ "$got" = "$expect" ]; then |
| 23 | + pass=$((pass + 1)) |
| 24 | + printf '[ok] expect=%-5s got=%-5s | %s\n' "$expect" "$got" "$cmd" |
| 25 | + else |
| 26 | + fail=$((fail + 1)) |
| 27 | + printf '[FAIL] expect=%-5s got=%-5s | %s\n' "$expect" "$got" "$cmd" |
| 28 | + fi |
| 29 | +} |
| 30 | + |
| 31 | +# ---------------- BLOCK: glab api to MR discussions/notes ---------------- |
| 32 | +run_case BLOCK 'glab api "projects/:id/merge_requests/42/discussions" --paginate' |
| 33 | +run_case BLOCK 'glab api projects/mygroup%2Fmyrepo/merge_requests/42/discussions' |
| 34 | +run_case BLOCK 'glab api projects/123/merge_requests/5/discussions/abc/notes' |
| 35 | +run_case BLOCK 'glab api projects/123/merge_requests/5/notes' |
| 36 | +run_case BLOCK 'glab api projects/123/merge_requests/5/notes/99 -X DELETE' |
| 37 | +run_case BLOCK 'glab api "projects/123/merge_requests/5/discussions/abc" -X PUT -F resolved=true' |
| 38 | +run_case BLOCK "glab api 'projects/foo/merge_requests/1/discussions/X/notes/42' -X PUT" |
| 39 | +run_case BLOCK 'cd /tmp && glab api "projects/:id/merge_requests/42/discussions" > /tmp/out.json 2>&1; echo EXIT=$?' |
| 40 | +run_case BLOCK 'glab api "projects/x/merge_requests/9/discussions"' |
| 41 | + |
| 42 | +# ---------------- BLOCK: glab mr view --comments ---------------- |
| 43 | +run_case BLOCK 'glab mr view 42 --comments' |
| 44 | +run_case BLOCK 'glab mr view 42 -R mygroup/myrepo --comments -F json' |
| 45 | +run_case BLOCK 'cd /repo && glab mr view 42 --comments' |
| 46 | + |
| 47 | +# ---------------- BLOCK: glab mr note (and sub-subcommands) ---------------- |
| 48 | +run_case BLOCK 'glab mr note' |
| 49 | +run_case BLOCK 'glab mr note 42' |
| 50 | +run_case BLOCK 'glab mr note 42 -m "hi"' |
| 51 | +run_case BLOCK 'glab mr note 42 --message "hi"' |
| 52 | +run_case BLOCK 'glab mr note 42 -R mygroup/myrepo -m "x"' |
| 53 | +run_case BLOCK 'glab mr note my-branch-name -m "text"' |
| 54 | +run_case BLOCK 'glab mr note list' |
| 55 | +run_case BLOCK 'glab mr note list 42' |
| 56 | +run_case BLOCK 'glab mr note list -R mygroup/myrepo' |
| 57 | +run_case BLOCK 'glab mr note resolve' |
| 58 | +run_case BLOCK 'glab mr note resolve DISC_ID' |
| 59 | +run_case BLOCK 'glab mr note reopen DISC_ID' |
| 60 | +run_case BLOCK 'glab mr note list' |
| 61 | + |
| 62 | +# ---------------- ALLOW: unrelated glab commands ---------------- |
| 63 | +run_case ALLOW 'glab mr view 42' |
| 64 | +run_case ALLOW 'glab mr view 42 -R mygroup/myrepo -F json' |
| 65 | +run_case ALLOW 'glab mr diff 42' |
| 66 | +run_case ALLOW 'glab mr list' |
| 67 | +run_case ALLOW 'glab mr create' |
| 68 | +run_case ALLOW 'glab mr approve 42' |
| 69 | +run_case ALLOW 'glab mr todo 42' |
| 70 | + |
| 71 | +# ---------------- ALLOW: unrelated glab api calls ---------------- |
| 72 | +run_case ALLOW 'glab api projects/mygroup%2Fmyrepo/merge_requests/42' |
| 73 | +run_case ALLOW 'glab api "projects/mygroup%2Fmyrepo"' |
| 74 | +run_case ALLOW 'glab api "projects?search=myrepo"' |
| 75 | +run_case ALLOW 'glab api "projects?search=x" --hostname gitlab.example.com' |
| 76 | + |
| 77 | +# ---------------- ALLOW: unrelated commands / pipeline stages ---------------- |
| 78 | +run_case ALLOW 'jq ".[] | {id}" /tmp/out.json' |
| 79 | +run_case ALLOW 'git status' |
| 80 | +run_case ALLOW 'glab-discussion read' |
| 81 | +run_case ALLOW 'glab-discussion write --body "x"' |
| 82 | +run_case ALLOW 'glab auth status' |
| 83 | + |
| 84 | +# ---------------- Empty / missing command ---------------- |
| 85 | +run_case ALLOW '' |
| 86 | + |
| 87 | +echo |
| 88 | +echo "passed: $pass" |
| 89 | +echo "failed: $fail" |
| 90 | +[ "$fail" -eq 0 ] |
0 commit comments