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
82 changes: 82 additions & 0 deletions .agents/hook/enforce_gh_english.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@
# - `gh repo` / `gh api` / other subcommands
# - git commit messages (project rule covers issue/PR only)
#
# Repository scoping (2026-09-19): the English-only rule is THIS repo's, not
# the host's. The same machine hosts sibling repos whose issues / PRs are
# written in another language, and `gh --repo <other>` calls for them run
# from this cwd (so this PreToolUse hook fires on them). The hook therefore
# allows early when the command targets another repository:
# - `--repo <owner/name>` / `-R <owner/name>` (also `--repo=<owner/name>`)
# names a repo other than this one, OR
# - no --repo is given and the cwd's git toplevel is not this checkout
# (`gh` then targets the cwd's own origin).
# "This repo" is derived from `git remote get-url origin` of the checkout the
# hook lives in (GH_ENGLISH_REPO overrides; falls back to the literal
# ycpss91255/initialization when origin is unreadable). Behaviour for this
# repo is unchanged.
#
# Refs: project rule "GitHub interaction English-only" (2026-05-16
# session), aligns with ycpss91255-docker/docker_harness convention.
#
Expand All @@ -48,6 +62,65 @@
source "${LIB_DIR:-$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../../lib" && pwd -P)}/hook_bootstrap.sh"
hook_bootstrap "enforce-gh-english"

# ── Repository scoping ───────────────────────────────────────────────────────

# Literal fallback for "this repo" when the checkout's origin cannot be read.
GH_ENGLISH_REPO_LITERAL="ycpss91255/initialization"

# _repo_slug <ref> — normalise a repository reference to lower-case
# "owner/name". Accepts what `gh --repo` and `git remote` produce: owner/name,
# host/owner/name, https:// or ssh:// URLs, git@host:owner/name, an optional
# trailing .git, and optional surrounding quotes. Prints nothing when no
# owner/name pair can be found.
_repo_slug() {
local _v="${1:-}" _name _rest _owner
_v="${_v#\"}"; _v="${_v%\"}"; _v="${_v#\'}"; _v="${_v%\'}"
_v="${_v%/}"
_v="${_v%.git}"
_v="${_v//://}"
_name="${_v##*/}"
_rest="${_v%/*}"
_owner="${_rest##*/}"
[[ -n "${_owner}" && -n "${_name}" && "${_rest}" != "${_v}" ]] || return 0
printf '%s/%s' "${_owner,,}" "${_name,,}"
}

# _this_repo — owner/name of the checkout this hook lives in (REPO_ROOT's
# origin remote; a linked worktree shares it), else the literal fallback.
_this_repo() {
local _slug
_slug="$(_repo_slug "$(git -C "${REPO_ROOT}" remote get-url origin 2>/dev/null)")"
printf '%s' "${_slug:-${GH_ENGLISH_REPO_LITERAL}}"
}

# _target_repo <cmd> — owner/name the gh command targets: an explicit
# `--repo <ref>` / `-R <ref>` / `--repo=<ref>`, else $GH_REPO, else the origin
# of the directory the command runs in (a leading `cd <dir> &&`, the payload's
# .cwd, or $PWD). Prints nothing when the target cannot be determined.
_target_repo() {
local _cmd="$1" _ref="" _dir _cd
if [[ "${_cmd}" =~ (^|[[:space:]])(--repo|-R)[[:space:]]+([^[:space:]]+) ]]; then
_ref="${BASH_REMATCH[3]}"
elif [[ "${_cmd}" =~ (^|[[:space:]])--repo=([^[:space:]]+) ]]; then
_ref="${BASH_REMATCH[2]}"
elif [[ -n "${GH_REPO:-}" ]]; then
_ref="${GH_REPO}"
else
_dir="$(hook_field '.cwd')"
[[ -z "${_dir}" ]] && _dir="${PWD}"
if [[ "${_cmd}" =~ (^|[[:space:]\;\&])cd[[:space:]]+([^[:space:]\&\;]+)[[:space:]]*\&\& ]]; then
_cd="${BASH_REMATCH[2]}"
if [[ "${_cd}" == /* ]]; then
_dir="${_cd}"
else
_dir="${_dir}/${_cd}"
fi
fi
_ref="$(git -C "${_dir}" remote get-url origin 2>/dev/null)"
fi
_repo_slug "${_ref}"
}

main() {
hook_read_input
local cmd
Expand All @@ -59,6 +132,15 @@ main() {
return 0
fi

# Repository scoping: the English-only rule guards THIS repo's artifacts.
# A command that demonstrably targets another repository passes through;
# an undeterminable target keeps the guard (conservative, unchanged).
local target
target="$(_target_repo "${cmd}")"
if [[ -n "${target}" && "${target}" != "$(_this_repo)" ]]; then
return 0
fi

# Extract body + title text to scan. Strategy: pull each flag's value.
local to_scan=""

Expand Down
17 changes: 17 additions & 0 deletions doc/changelog/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,23 @@ not deferred to release. `release-tag.sh` promotes `[Unreleased]` →

### Fixed

- **`enforce_gh_english.sh` is scoped to this repository**
(`.agents/hook/enforce_gh_english.sh`): the same machine now hosts sibling
repos whose issues / PRs are written in another language, and their
`gh --repo <other>` calls run from this repo's cwd, so this PreToolUse Bash
hook fired on them and denied their CJK bodies. The hook now allows early
when the `gh issue|pr create|comment` command demonstrably targets another
repository: an explicit `--repo <ref>` / `-R <ref>` / `--repo=<ref>`
naming a repo other than this one (owner/name, host/owner/name, URL and
`.git` forms are normalised, case-insensitively), else `$GH_REPO`, else the
origin of the directory the command runs in (a leading `cd <dir> &&`, the
payload's `.cwd`, or `$PWD`) resolving to another repo. "This repo" is
derived from the checkout's `git remote get-url origin` with a literal
`ycpss91255/initialization` fallback. An undeterminable target keeps the
guard, and behaviour for this repo is unchanged. Covered by six new cases in
`test/unit/hook/enforce_gh_english_spec.bats` (other repo via `--repo`,
`-R`, `--repo=`; this repo via `--repo`; no `--repo` inside this repo still
denied; no `--repo` from a cwd outside this repo allowed).
- **`just -f justfile.ci lint` no longer lints gitignored third-party files**
(`script/ci/ci.sh`): `_find_lintable_sh` collected `*.sh`/`*.bash`/`*.bats`
with a raw `find`, which also caught machine-local third-party skills
Expand Down
55 changes: 55 additions & 0 deletions test/unit/hook/enforce_gh_english_spec.bats
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,58 @@ _require_python3() {
[ "${status}" -eq 0 ]
[ -z "${output}" ]
}

# ── repository scoping: the rule is this repo's, not the host's ──────────────
# The same machine hosts sibling repos whose issues / PRs are written in
# another language, and `gh --repo <other>` for them runs from this cwd. The
# hook must only guard ycpss91255/initialization: an explicit --repo / -R
# naming another repo passes through, and so does a cwd outside this checkout.
# Without either signal (this repo, no --repo) the guard is unchanged.

@test "allows CJK when --repo names another repository" {
_require_python3
_run_hook 'gh issue create --repo other/repo --title "修复错误" --body "你好世界"'
[ "${status}" -eq 0 ]
[ -z "${output}" ]
}

@test "allows CJK when -R names another repository" {
_require_python3
_run_hook 'gh pr create -R other/repo --title "feat" --body "你好世界"'
[ "${status}" -eq 0 ]
[ -z "${output}" ]
}

@test "allows CJK when --repo=<other> (equals form) names another repository" {
_require_python3
_run_hook 'gh issue comment 7 --repo=other/repo --body "ありがとう"'
[ "${status}" -eq 0 ]
[ -z "${output}" ]
}

@test "still denies CJK when --repo names this repository" {
_require_python3
_run_hook 'gh issue create --repo ycpss91255/initialization --title "bug" --body "你好世界"'
[ "${status}" -eq 0 ]
[[ "${output}" == *"deny"* ]]
}

@test "still denies CJK without --repo inside this repository" {
_require_python3
_run_hook 'gh issue create --title "bug" --body "你好世界"'
[ "${status}" -eq 0 ]
[[ "${output}" == *"deny"* ]]
}

@test "allows CJK without --repo when cwd is outside this repository" {
_require_python3
local _elsewhere="${BATS_TEST_TMPDIR}/elsewhere"
mkdir -p "${_elsewhere}"
git -C "${_elsewhere}" init -q
git -C "${_elsewhere}" remote add origin https://github.com/other/repo.git
run bash -c 'cd "$3" && printf "%s" "$1" | "$2"' _ \
"$(_json 'gh issue create --title "bug" --body "你好世界"')" \
"${HOOK_SH}" "${_elsewhere}"
[ "${status}" -eq 0 ]
[ -z "${output}" ]
}
Loading