Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 3 additions & 36 deletions scripts/aw.sh
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ cmd_run() {
fi

# Build context from environment or fixture
local issue_number issue_title issue_body issue_labels issue_url repo issue_action
local issue_number issue_title issue_body issue_labels issue_url repo
if [[ -n "$fixture" ]]; then
# Pass path via env var to avoid injecting user-controlled paths into Python source
issue_number="$(AW_FIXTURE="$fixture" python3 - <<'PYEOF'
Expand Down Expand Up @@ -201,13 +201,6 @@ with open(os.environ['AW_FIXTURE'], encoding='utf-8') as fh:
d = json.load(fh)
print(d['repository']['full_name'])
PYEOF
)"
issue_action="$(AW_FIXTURE="$fixture" python3 - <<'PYEOF'
import json, os
with open(os.environ['AW_FIXTURE'], encoding='utf-8') as fh:
d = json.load(fh)
print(d.get('action', 'opened'))
PYEOF
)"
else
issue_number="${ISSUE_NUMBER:-}"
Expand All @@ -216,7 +209,6 @@ PYEOF
issue_labels="${ISSUE_LABELS:-}"
issue_url="${ISSUE_URL:-https://github.com/${GITHUB_REPOSITORY:-unknown}/issues/${ISSUE_NUMBER:-0}}"
repo="${GITHUB_REPOSITORY:-unknown}"
issue_action="${ISSUE_ACTION:-opened}"
fi

# Skip guard: read threshold from frontmatter (default 2), skip if label count >= threshold
Expand All @@ -243,31 +235,6 @@ PYEOF
return 0
fi

# Skip if any existing label is in the allowed-labels set (prevents re-triage on reopen)
local allowed_json
allowed_json="$(python3 - "$wf_file" <<'PYEOF'
import sys, re, yaml, json
path = sys.argv[1]
with open(path, encoding='utf-8') as f:
text = f.read()
m = re.match(r'^---\n(.*?)\n---\n', text, re.DOTALL)
fm = yaml.safe_load(m.group(1)) if m else {}
print(json.dumps(fm.get('allowed-labels', [])))
PYEOF
)"
local has_triage_label
has_triage_label="$(AW_ALLOWED="$allowed_json" AW_LABELS="$issue_labels" python3 - <<'PYEOF'
import os, json
current = set(l for l in os.environ.get('AW_LABELS', '').split(',') if l)
allowed = set(json.loads(os.environ.get('AW_ALLOWED', '[]')))
print('true' if current & allowed else 'false')
PYEOF
)"
if [[ "$has_triage_label" == "true" && "$issue_action" == "reopened" ]]; then
echo '{"skip": true}'
return 0
fi

# Substitute variables into workflow body.
# Use a quoted heredoc and env vars so user-controlled issue data cannot
# escape into Python source code (code injection hotspot).
Expand Down Expand Up @@ -416,7 +383,7 @@ with open(path, encoding='utf-8') as f:
text = f.read()
m = re.match(r'^---\n(.*?)\n---\n', text, re.DOTALL)
fm = yaml.safe_load(m.group(1)) if m else {}
print(json.dumps(fm.get("allowed-labels", [])))
print(json.dumps(fm.get("safe-outputs", {}).get("add-labels", {}).get("allowed", [])))
PYEOF
)"
max_labels="$(python3 - "$wf_file" <<'PYEOF'
Expand All @@ -426,7 +393,7 @@ with open(path, encoding='utf-8') as f:
text = f.read()
m = re.match(r'^---\n(.*?)\n---\n', text, re.DOTALL)
fm = yaml.safe_load(m.group(1)) if m else {}
print(fm.get('max-labels', 3))
print(fm.get("safe-outputs", {}).get("add-labels", {}).get("max", 3))
PYEOF
)"

Expand Down
18 changes: 18 additions & 0 deletions tests/aw/issue-triage/test_aw_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ else
fi
rm -f "$tmp"

# ---------------------------------------------------------------------------
# Test 6: safe-output accepts valid labels + comment → validation passed
# ---------------------------------------------------------------------------
tmp=$(mktemp)
printf '{"labels":["bug","needs-triage"],"comment":"Thank you for the report!"}' > "$tmp"
mock_dir=$(mktemp -d)
printf '#!/bin/sh\nexit 0\n' > "$mock_dir/gh"
chmod +x "$mock_dir/gh"
output=$(PATH="$mock_dir:$PATH" ISSUE_NUMBER=1 GITHUB_REPOSITORY=example/repo \
bash "$REPO_ROOT/scripts/aw.sh" safe-output apply issue-triage "$tmp" 2>&1)
rm -rf "$mock_dir"
if echo "$output" | grep -q "validation passed"; then
ok "safe-output: valid allowed labels → validation passed"
else
fail "safe-output: valid allowed labels → validation passed" "got: $output"
fi
rm -f "$tmp"

# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
Expand Down
Loading