Files
ljwx-deploy/scripts/github/_gh_target.sh
T

79 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
gh_target_fail() {
echo "[错误] $1" >&2
return 1
}
gh_target_resolve_repo_from_remote() {
local remote_url=""
local owner=""
local repo=""
remote_url="$(git config --get remote.origin.url || true)"
if [[ -z "$remote_url" ]]; then
gh_target_fail "无法从 git remote.origin.url 解析仓库"
return 1
fi
if [[ "$remote_url" =~ ^git@github\.com:([^/]+)/(.+)\.git$ ]]; then
owner="${BASH_REMATCH[1]}"
repo="${BASH_REMATCH[2]}"
elif [[ "$remote_url" =~ ^https://github\.com/([^/]+)/(.+)\.git$ ]]; then
owner="${BASH_REMATCH[1]}"
repo="${BASH_REMATCH[2]}"
elif [[ "$remote_url" =~ ^https://github\.com/([^/]+)/(.+)$ ]]; then
owner="${BASH_REMATCH[1]}"
repo="${BASH_REMATCH[2]}"
else
gh_target_fail "不支持的 GitHub remote URL: ${remote_url}"
return 1
fi
printf '%s/%s\n' "$owner" "$repo"
}
gh_target_resolve_project_from_url() {
local url="$1"
local owner=""
local number=""
local owner_type=""
if [[ "$url" =~ ^https://github\.com/orgs/([^/]+)/projects/([0-9]+)(/.*)?$ ]]; then
owner="${BASH_REMATCH[1]}"
number="${BASH_REMATCH[2]}"
owner_type="organization"
elif [[ "$url" =~ ^https://github\.com/users/([^/]+)/projects/([0-9]+)(/.*)?$ ]]; then
owner="${BASH_REMATCH[1]}"
number="${BASH_REMATCH[2]}"
owner_type="user"
else
gh_target_fail "无法解析 GitHub Project URL: ${url}"
return 1
fi
printf '%s\t%s\t%s\n' "$owner" "$number" "$owner_type"
}
gh_target_resolve_content_from_url() {
local url="$1"
local repo=""
local kind=""
local number=""
if [[ "$url" =~ ^https://github\.com/([^/]+/[^/]+)/issues/([0-9]+)(/.*)?$ ]]; then
repo="${BASH_REMATCH[1]}"
number="${BASH_REMATCH[2]}"
kind="issue"
elif [[ "$url" =~ ^https://github\.com/([^/]+/[^/]+)/pull/([0-9]+)(/.*)?$ ]]; then
repo="${BASH_REMATCH[1]}"
number="${BASH_REMATCH[2]}"
kind="pull"
else
gh_target_fail "无法解析 GitHub 内容 URL: ${url}"
return 1
fi
printf '%s\t%s\t%s\n' "$repo" "$kind" "$number"
}