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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ script-test:
$(call run-timed,bash scripts/gitleaks-install-test.sh)
$(call run-timed,bash scripts/post-failure-report-test.sh)
$(call run-timed,bash scripts/pr-assignee-test.sh)
$(call run-timed,bash scripts/labels-test.sh)
$(call run-timed,bash scripts/post-triage-test.sh)
$(call run-timed,bash scripts/post-prioritize-test.sh)
$(call run-timed,bash scripts/pre-code-test.sh)
Expand Down
147 changes: 147 additions & 0 deletions scripts/labels-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#!/usr/bin/env bash
# labels-test.sh — Tests for scripts/lib/labels.lib.sh
#
# Run from the repo root:
# bash scripts/labels-test.sh

set -euo pipefail

if [[ "${SCRIPT_TEST_TARGET:-source}" == "bundled" ]]; then
echo "SKIP: labels-test (lib tests skipped in bundled mode)"
exit 0
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

FAILURES=0

# --- Test helpers ---

GH_CALLS_FILE=$(mktemp)
export GH_CALLS_FILE
trap 'rm -f "${GH_CALLS_FILE}"' EXIT

setup_test() {
true > "${GH_CALLS_FILE}"
export GH_EXIT_CODE=0
export GH_STDERR=""
export REPO_FULL_NAME="test-org/test-repo"
unset REPO 2>/dev/null || true
}

get_gh_call() {
local index="${1:-0}"
sed -n "$((index + 1))p" "${GH_CALLS_FILE}"
}

get_gh_call_count() {
wc -l < "${GH_CALLS_FILE}" | tr -d ' '
}

# Stub gh that records calls and returns configured exit/stderr.
gh() {
echo "$*" >> "${GH_CALLS_FILE}"
if [[ ${GH_EXIT_CODE} -ne 0 ]]; then
echo "${GH_STDERR}" >&2
return ${GH_EXIT_CODE}
fi
}
export -f gh 2>/dev/null || true

run_test() {
local test_name="$1"
local expected_pattern="$2"
local actual="$3"

if [[ "${actual}" == *"${expected_pattern}"* ]] || [[ "${expected_pattern}" == "${actual}" ]]; then
echo "PASS: ${test_name}"
else
echo "FAIL: ${test_name}"
echo " expected pattern: '${expected_pattern}'"
echo " actual: '${actual}'"
FAILURES=$((FAILURES + 1))
fi
}

# Source the lib under test (after defining gh stub).
# shellcheck source=lib/labels.lib.sh
source "${SCRIPT_DIR}/lib/labels.lib.sh"

# --- Tests ---

# Test 1: Mandatory label emits gh label create with correct args.
setup_test
forge_ensure_label "ready-for-review"
run_test "mandatory-label-creates" \
"label create ready-for-review --repo test-org/test-repo" \
"$(get_gh_call 0)"

# Test 2: Non-mandatory label is a no-op.
setup_test
forge_ensure_label "question"
run_test "non-mandatory-is-noop" \
"0" \
"$(get_gh_call_count)"

# Test 3: "already exists" error produces no warning.
setup_test
GH_EXIT_CODE=1
GH_STDERR='label with name "ready-for-review" already exists; use `--force` to update'
stderr_output=$(forge_ensure_label "ready-for-review" 2>&1 >/dev/null)
run_test "already-exists-silent" \
"" \
"${stderr_output}"

# Test 4: Other errors produce a warning.
setup_test
GH_EXIT_CODE=1
GH_STDERR="HTTP 403: Resource not accessible by integration"
stderr_output=$(forge_ensure_label "ready-for-review" 2>&1 >/dev/null)
run_test "other-error-warns" \
"Warning:" \
"${stderr_output}"

# Test 5: Defaults are applied when no description/color provided.
setup_test
forge_ensure_label "ready-to-code"
run_test "defaults-applied-description" \
"--description" \
"$(get_gh_call 0)"
run_test "defaults-applied-color" \
"--color" \
"$(get_gh_call 0)"

# Test 6: Explicit description/color overrides defaults.
setup_test
forge_ensure_label "ready-for-review" "Custom desc" "FF0000"
run_test "explicit-overrides-default" \
"--description Custom desc --color FF0000" \
"$(get_gh_call 0)"

# Test 7: Uses REPO when REPO_FULL_NAME is unset.
setup_test
unset REPO_FULL_NAME
export REPO="triage-org/triage-repo"
forge_ensure_label "ready-for-triage"
run_test "falls-back-to-REPO" \
"--repo triage-org/triage-repo" \
"$(get_gh_call 0)"

# Test 8: No --force flag in the gh call.
setup_test
forge_ensure_label "ready-for-review"
gh_call=$(get_gh_call 0)
if [[ "${gh_call}" == *"--force"* ]]; then
echo "FAIL: no-force-flag"
echo " gh call contains --force: '${gh_call}'"
FAILURES=$((FAILURES + 1))
else
echo "PASS: no-force-flag"
fi

echo ""
if [ ${FAILURES} -gt 0 ]; then
echo "${FAILURES} test(s) failed"
exit 1
fi
echo "All tests passed"
64 changes: 64 additions & 0 deletions scripts/lib/labels.lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env bash
# labels.lib.sh — Mandatory label management for fullsend agent scripts.
#
# Provides forge_ensure_label() which creates mandatory dispatch labels
# without --force, preserving admin customizations. Non-mandatory labels
# are silently skipped (no-op).

# shellcheck shell=bash

[[ -n "${LABELS_SH_LOADED:-}" ]] && return 0
LABELS_SH_LOADED=1

MANDATORY_LABELS=("ready-for-review" "ready-to-code" "ready-for-triage")
Comment thread
maruiz93 marked this conversation as resolved.

_labels_mandatory_defaults() {
printf '%s\t%s\t%s\n' \
"ready-for-review" "Triggers review agent dispatch" "0E8A16" \
"ready-to-code" "Triggers code agent dispatch" "0E8A16" \
"ready-for-triage" "Triggers triage agent dispatch" "0E8A16"
}

forge_ensure_label() {
local name="$1"
local description="${2:-}"
local color="${3:-}"

local is_mandatory=false
local m
for m in "${MANDATORY_LABELS[@]}"; do
[[ "${m}" == "${name}" ]] && is_mandatory=true && break
done
if [[ "${is_mandatory}" != "true" ]]; then
return 0
fi

if [[ -z "${description}" || -z "${color}" ]]; then
local line
line=$(_labels_mandatory_defaults | grep "^${name} " || true)
if [[ -n "${line}" ]]; then
[[ -z "${description}" ]] && description=$(printf '%s' "${line}" | cut -f2)
[[ -z "${color}" ]] && color=$(printf '%s' "${line}" | cut -f3)
fi
fi

local create_args=("${name}" --repo "${REPO_FULL_NAME:-${REPO}}")
[[ -n "${description}" ]] && create_args+=(--description "${description}")
[[ -n "${color}" ]] && create_args+=(--color "${color}")

local err
if ! err=$(gh label create "${create_args[@]}" 2>&1); then
case "${err}" in
*already\ exists*) ;;
Comment thread
maruiz93 marked this conversation as resolved.
Comment thread
maruiz93 marked this conversation as resolved.
*)
err="${err//$'\n'/ }"
err="${err//::/:}"
err="${err//%0A/}"
err="${err//%0a/}"
err="${err//%0D/}"
err="${err//%0d/}"
echo "Warning: gh label create ${name}: ${err}" >&2
;;
esac
fi
Comment thread
maruiz93 marked this conversation as resolved.
}
14 changes: 12 additions & 2 deletions scripts/post-code-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ else
echo "PASS: bundled-script-has-auto-merge"
fi

if ! grep -q 'forge_ensure_label' "${POST_SCRIPT}"; then
echo "FAIL: bundled-script-has-ensure-label"
echo " ${POST_SCRIPT} missing forge_ensure_label"
FAILURES=$((FAILURES + 1))
else
echo "PASS: bundled-script-has-ensure-label"
fi

# ---------------------------------------------------------------------------
# Test helper — reimplements the title-rewriting logic from post-code.sh
# so we can test it without a git repo or network access.
Expand Down Expand Up @@ -2180,10 +2188,12 @@ run_gitlab_sanitize_test "sanitize-glpat-replaced" \
"[REDACTED]" "yes"

# oauth2:TOKEN in push URLs should be redacted
# gitleaks:allow
run_gitlab_sanitize_test "sanitize-oauth2-push-url" \
"https://oauth2:glpat-secret-token@gitlab.com/group/project.git" \
"glpat-secret-token" "no"

# gitleaks:allow
run_gitlab_sanitize_test "sanitize-oauth2-replaced" \
"https://oauth2:glpat-secret-token@gitlab.com/group/project.git" \
"oauth2:[REDACTED]" "yes"
Expand Down Expand Up @@ -2248,11 +2258,11 @@ run_gitlab_push_url_test() {

run_gitlab_push_url_test "gitlab-push-url-format" \
"glpat-testtoken1234567890" "gitlab.com" "group/project" \
"https://oauth2:glpat-testtoken1234567890@gitlab.com/group/project.git"
"https://oauth2:glpat-testtoken1234567890@gitlab.com/group/project.git" # gitleaks:allow

run_gitlab_push_url_test "gitlab-push-url-nested-group" \
"token123" "gitlab.cee.redhat.com" "org/sub/project" \
"https://oauth2:token123@gitlab.cee.redhat.com/org/sub/project.git"
"https://oauth2:token123@gitlab.cee.redhat.com/org/sub/project.git" # gitleaks:allow

# ---------------------------------------------------------------------------
# Forge dispatch pattern — tests that the declare -F dispatch pattern used
Expand Down
67 changes: 67 additions & 0 deletions scripts/post-code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,72 @@ forge_append_path() {
;;
esac
# END bundled: lib/code-ops.lib.sh
# shellcheck source=lib/labels.lib.sh
# BEGIN bundled: lib/labels.lib.sh
# labels.lib.sh — Mandatory label management for fullsend agent scripts.
#
# Provides forge_ensure_label() which creates mandatory dispatch labels
# without --force, preserving admin customizations. Non-mandatory labels
# are silently skipped (no-op).

# shellcheck shell=bash

[[ -n "${LABELS_SH_LOADED:-}" ]] && return 0
LABELS_SH_LOADED=1

MANDATORY_LABELS=("ready-for-review" "ready-to-code" "ready-for-triage")

_labels_mandatory_defaults() {
printf '%s\t%s\t%s\n' \
"ready-for-review" "Triggers review agent dispatch" "0E8A16" \
"ready-to-code" "Triggers code agent dispatch" "0E8A16" \
"ready-for-triage" "Triggers triage agent dispatch" "0E8A16"
}

forge_ensure_label() {
local name="$1"
local description="${2:-}"
local color="${3:-}"

local is_mandatory=false
local m
for m in "${MANDATORY_LABELS[@]}"; do
[[ "${m}" == "${name}" ]] && is_mandatory=true && break
done
if [[ "${is_mandatory}" != "true" ]]; then
return 0
fi

if [[ -z "${description}" || -z "${color}" ]]; then
local line
line=$(_labels_mandatory_defaults | grep "^${name} " || true)
if [[ -n "${line}" ]]; then
[[ -z "${description}" ]] && description=$(printf '%s' "${line}" | cut -f2)
[[ -z "${color}" ]] && color=$(printf '%s' "${line}" | cut -f3)
fi
fi

local create_args=("${name}" --repo "${REPO_FULL_NAME:-${REPO}}")
[[ -n "${description}" ]] && create_args+=(--description "${description}")
[[ -n "${color}" ]] && create_args+=(--color "${color}")

local err
if ! err=$(gh label create "${create_args[@]}" 2>&1); then
case "${err}" in
*already\ exists*) ;;
*)
err="${err//$'\n'/ }"
err="${err//::/:}"
err="${err//%0A/}"
err="${err//%0a/}"
err="${err//%0D/}"
err="${err//%0d/}"
echo "Warning: gh label create ${name}: ${err}" >&2
;;
esac
fi
}
# END bundled: lib/labels.lib.sh

# ---------------------------------------------------------------------------
# enable_auto_merge — arm auto-merge on a PR/MR (best-effort).
Expand Down Expand Up @@ -2402,6 +2468,7 @@ forge_write_output "pr_url" "${PR_URL}"
# .github/scripts/check-e2e-authorization-test.sh for trusted-actor rules.
# Note: variable name is PR_NUMBER_FROM_URL (not PR_NUMBER) to avoid SC2153.
PR_NUMBER_FROM_URL="${PR_URL##*/}"
forge_ensure_label "ready-for-review"
forge_add_label "ready-for-review" "pr" "${PR_NUMBER_FROM_URL}"

# ---------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions scripts/post-code.src.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ source "${SCRIPT_DIR_POST}/lib/branch-guard.lib.sh"
SCRIPT_DIR="${SCRIPT_DIR_POST}"
# shellcheck source=lib/code-ops.lib.sh
source "${SCRIPT_DIR_POST}/lib/code-ops.lib.sh"
# shellcheck source=lib/labels.lib.sh
source "${SCRIPT_DIR_POST}/lib/labels.lib.sh"

# ---------------------------------------------------------------------------
# enable_auto_merge — arm auto-merge on a PR/MR (best-effort).
Expand Down Expand Up @@ -902,6 +904,7 @@ forge_write_output "pr_url" "${PR_URL}"
# .github/scripts/check-e2e-authorization-test.sh for trusted-actor rules.
Comment thread
maruiz93 marked this conversation as resolved.
# Note: variable name is PR_NUMBER_FROM_URL (not PR_NUMBER) to avoid SC2153.
PR_NUMBER_FROM_URL="${PR_URL##*/}"
forge_ensure_label "ready-for-review"
forge_add_label "ready-for-review" "pr" "${PR_NUMBER_FROM_URL}"

# ---------------------------------------------------------------------------
Expand Down
22 changes: 16 additions & 6 deletions scripts/post-retro.sh
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,23 @@ if [[ "${PROPOSAL_COUNT}" -gt 0 ]]; then
fi

# Ensure the label exists in the target repo before applying it.
# Follows the same pattern as post-review.sh for ready-for-merge.
# --force makes this idempotent (no error if the label already exists).
gh label create "ready-for-triage" \
if ! _lbl_err=$(gh label create "ready-for-triage" \
Comment thread
maruiz93 marked this conversation as resolved.
--repo "${TARGET_REPO}" \
--description "Retro-filed issue awaiting triage agent" \
--color "ededed" \
--force 2>/dev/null || true
--description "Triggers triage agent dispatch" \
--color "0E8A16" 2>&1); then
case "${_lbl_err}" in
*already\ exists*) ;;
*)
_lbl_err="${_lbl_err//$'\n'/ }"
_lbl_err="${_lbl_err//::/:}"
_lbl_err="${_lbl_err//%0A/}"
_lbl_err="${_lbl_err//%0a/}"
_lbl_err="${_lbl_err//%0D/}"
_lbl_err="${_lbl_err//%0d/}"
echo "Warning: gh label create ready-for-triage: ${_lbl_err}" >&2
;;
esac
fi

SAFE_TITLE="${TITLE//::/}"
SAFE_TITLE="${SAFE_TITLE//%0A/}"
Expand Down
Loading
Loading