fix(wiki): 依頁面類型解析 wiki repo 並統一 hash
This commit is contained in:
Executable
+64
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env sh
|
||||
# check-wiki-rules — 驗證 wiki repo 解析與 hash-id 規則。
|
||||
set -eu
|
||||
|
||||
dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
gitea="$dir/gitea.sh"
|
||||
hash_id="$dir/hash-id"
|
||||
|
||||
fail() {
|
||||
printf '%s\n' "$1" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
expect_eq() {
|
||||
got=$1
|
||||
want=$2
|
||||
label=$3
|
||||
[ "$got" = "$want" ] || fail "$label: want=$want got=$got"
|
||||
}
|
||||
|
||||
check_repo() {
|
||||
type=$1
|
||||
label=$2
|
||||
want=$3
|
||||
shift 3
|
||||
got=$(env -i PATH="${PATH:-/usr/bin:/bin}" "$@" "$gitea" wiki-repo "$type")
|
||||
expect_eq "$got" "$want" "$label"
|
||||
}
|
||||
|
||||
check_hash() {
|
||||
input=$1
|
||||
want=$2
|
||||
got=$("$hash_id" "$input")
|
||||
expect_eq "$got" "$want" "hash-id $input"
|
||||
}
|
||||
|
||||
check_repo REPO 'REPO specific wins' 'records/REPO' \
|
||||
JSC_WIKI_REPO_REPO='records/REPO' \
|
||||
JSC_WIKI_REPO_ANALYZE='knowledges/ANALYZE' \
|
||||
JSC_WIKI_REPO='shared/wiki'
|
||||
|
||||
check_repo REPO 'REPO falls back to shared only' 'shared/wiki' \
|
||||
JSC_WIKI_REPO_ANALYZE='knowledges/ANALYZE' \
|
||||
JSC_WIKI_REPO='shared/wiki'
|
||||
|
||||
check_repo ANALYZE 'ANALYZE specific wins' 'knowledges/ANALYZE' \
|
||||
JSC_WIKI_REPO_REPO='records/REPO' \
|
||||
JSC_WIKI_REPO_ANALYZE='knowledges/ANALYZE' \
|
||||
JSC_WIKI_REPO='shared/wiki'
|
||||
|
||||
check_repo ANALYZE 'ANALYZE falls back to shared only' 'shared/wiki' \
|
||||
JSC_WIKI_REPO_REPO='records/REPO' \
|
||||
JSC_WIKI_REPO='shared/wiki'
|
||||
|
||||
check_repo ERROR 'ERROR uses its own repo' 'errors/wiki' \
|
||||
JSC_WIKI_REPO_ERROR='errors/wiki' \
|
||||
JSC_WIKI_REPO='shared/wiki'
|
||||
|
||||
check_hash 'case-2' 'H5172CB7'
|
||||
check_hash 'case-11' 'HA9A6662'
|
||||
check_hash 'case-1' 'HB6EC7FD'
|
||||
check_hash 'case-12' 'HCCA8D42'
|
||||
|
||||
printf '%s\n' 'OK'
|
||||
+27
-10
@@ -5,9 +5,10 @@
|
||||
# gitea.sh repos <owner> # 列出 owner 底下可讀取的 repo(全名)
|
||||
# gitea.sh default-branch <owner>/<repo> # 印出預設分支
|
||||
# gitea.sh clone-url <owner>/<repo> # 印出 clone URL
|
||||
# gitea.sh hash-id <text> # 產生 8 碼大寫 SHA-1 hash;首碼為 0-9/A/B/C 時改成 Hxxxxxxx
|
||||
# gitea.sh wiki-repo <TYPE> # 解析頁面類型的 wiki 位置:
|
||||
# TYPE = QUESTION|PLAN|ANALYZE|MAINTAIN|REPO|LOG(即頁名前綴)
|
||||
# 依序取 JSC_WIKI_REPO_{TYPE} > JSC_WIKI_REPO;都未設定 exit 3(呼叫端須詢問使用者)
|
||||
# TYPE = QUESTION|PLAN|ANALYZE|MAINTAIN|REPO|LOG|ERROR(即頁名前綴)
|
||||
# 依序取 JSC_WIKI_REPO_{TYPE} > JSC_WIKI_REPO;不得跨類型代用;都未設定 exit 3
|
||||
# gitea.sh wiki-list <owner>/<repo> # 列出 wiki 頁名
|
||||
# gitea.sh wiki-get <owner>/<repo> <page> # 印出 wiki 頁 markdown;不存在時 exit 4
|
||||
# gitea.sh wiki-put <owner>/<repo> <page> <file> # 建立或更新 wiki 頁(內容取自檔案)
|
||||
@@ -21,6 +22,30 @@
|
||||
# (~/.config/tea/config.yml,優先取 default: true 的登入);兩者皆無才失敗。
|
||||
set -eu
|
||||
|
||||
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
|
||||
resolve_wiki_repo() { # TYPE -> JSC_WIKI_REPO_{TYPE} -> JSC_WIKI_REPO
|
||||
type=$(printf '%s' "${1:?TYPE required}" | tr a-z A-Z)
|
||||
case "$type" in
|
||||
QUESTION|PLAN|ANALYZE|MAINTAIN|REPO|LOG|ERROR) ;;
|
||||
*) echo "unknown wiki type: $type" >&2; exit 2 ;;
|
||||
esac
|
||||
eval "v=\${JSC_WIKI_REPO_${type}:-}"
|
||||
[ -n "$v" ] || v="${JSC_WIKI_REPO:-}"
|
||||
if [ -n "$v" ]; then printf '%s\n' "$v"; else
|
||||
echo "no wiki repo configured for $type (set JSC_WIKI_REPO_$type or JSC_WIKI_REPO)" >&2; exit 3
|
||||
fi
|
||||
}
|
||||
|
||||
cmd="${1:?usage: gitea.sh <command> ...}"; shift
|
||||
case "$cmd" in
|
||||
hash-id)
|
||||
exec "$script_dir/hash-id" "$@" ;;
|
||||
wiki-repo)
|
||||
resolve_wiki_repo "${1-}"
|
||||
exit 0 ;;
|
||||
esac
|
||||
|
||||
tea_token() { # 取 tea CLI 設定檔的 token(優先 default: true 的登入,否則第一個)
|
||||
cfg="${HOME}/.config/tea/config.yml"
|
||||
[ -f "$cfg" ] || return 1
|
||||
@@ -78,7 +103,6 @@ for i in items:
|
||||
' "$1"
|
||||
}
|
||||
|
||||
cmd="${1:?usage: gitea.sh <command> ...}"; shift
|
||||
case "$cmd" in
|
||||
owners)
|
||||
{ req GET "/user" | json_field login
|
||||
@@ -101,13 +125,6 @@ case "$cmd" in
|
||||
clone-url)
|
||||
or="${1:?owner/repo required}"
|
||||
req GET "/repos/$or" | json_field clone_url ;;
|
||||
wiki-repo)
|
||||
type=$(printf '%s' "${1:?TYPE required}" | tr a-z A-Z)
|
||||
eval "v=\${JSC_WIKI_REPO_$type:-}"
|
||||
[ -n "$v" ] || v="${JSC_WIKI_REPO:-}"
|
||||
if [ -n "$v" ]; then printf '%s\n' "$v"; else
|
||||
echo "no wiki repo configured for $type (set JSC_WIKI_REPO_$type or JSC_WIKI_REPO)" >&2; exit 3
|
||||
fi ;;
|
||||
wiki-list)
|
||||
or="${1:?owner/repo required}"
|
||||
req GET "/repos/$or/wiki/pages?limit=200" | json_field title ;;
|
||||
|
||||
Executable
+34
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env sh
|
||||
# hash-id — 產生 8 碼大寫 SHA-1 hash。
|
||||
# 用法:
|
||||
# hash-id <text>
|
||||
# printf '%s' <text> | hash-id
|
||||
# 規則:
|
||||
# 先取 SHA-1 前 8 碼大寫。
|
||||
# 若首碼為 0-9、A、B、C,改成 H 加上原 SHA-1 前 7 碼。
|
||||
set -eu
|
||||
|
||||
input=''
|
||||
if [ "$#" -gt 0 ]; then
|
||||
input=$*
|
||||
elif [ -t 0 ]; then
|
||||
input=''
|
||||
else
|
||||
input=$(cat)
|
||||
fi
|
||||
|
||||
if command -v sha1sum >/dev/null 2>&1; then
|
||||
raw=$(printf '%s' "$input" | sha1sum | awk '{print $1}')
|
||||
elif command -v shasum >/dev/null 2>&1; then
|
||||
raw=$(printf '%s' "$input" | shasum -a 1 | awk '{print $1}')
|
||||
else
|
||||
echo 'no SHA-1 helper found' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
hash=$(printf '%s' "$raw" | cut -c1-8 | tr a-f A-F)
|
||||
case "$hash" in
|
||||
[0-9ABC]*) hash="H$(printf '%s' "$hash" | cut -c1-7)" ;;
|
||||
esac
|
||||
|
||||
printf '%s\n' "$hash"
|
||||
Reference in New Issue
Block a user