#!/bin/bash # Gitea API 示例脚本 # 包含常用 Gitea API 操作的函数示例 set -e # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # 检查环境变量 check_env() { if [[ -z "$GITEA_URL" || -z "$GITEA_TOKEN" ]]; then echo -e "${RED}[ERROR]${NC} 请先设置 Gitea 环境变量" echo " 运行: source ~/.gitea-actor.env" echo " 或设置: export GITEA_URL=..., export GITEA_TOKEN=..." return 1 fi } # 通用 API 请求函数 gitea_api() { local method=$1 local endpoint=$2 local data=$3 local url="${GITEA_URL}/api/v1/${endpoint}" local curl_cmd="curl -s -H 'Authorization: token $GITEA_TOKEN' -H 'Accept: application/json'" if [[ -n "$data" ]]; then curl_cmd="$curl_cmd -H 'Content-Type: application/json' -d '$data'" fi curl_cmd="$curl_cmd -X $method '$url'" eval "$curl_cmd" } # 1. 用户相关操作 get_current_user() { check_env || return 1 echo -e "${BLUE}[INFO]${NC} 获取当前用户信息..." gitea_api "GET" "user" | jq . } list_user_repos() { check_env || return 1 local page=${1:-1} local limit=${2:-20} echo -e "${BLUE}[INFO]${NC} 列出用户仓库 (第 $page 页, 每页 $limit 个)..." gitea_api "GET" "user/repos?page=$page&limit=$limit" | jq '.[] | {id, full_name, description, private, html_url}' } # 2. 仓库操作 create_repository() { check_env || return 1 local name=$1 local description=${2:-""} local private=${3:-false} if [[ -z "$name" ]]; then echo -e "${YELLOW}[USAGE]${NC} create_repository [description] [private(true/false)]" return 1 fi local data=$(jq -n \ --arg name "$name" \ --arg desc "$description" \ --argjson private "$private" \ '{name: $name, description: $desc, private: $private, auto_init: true}') echo -e "${BLUE}[INFO]${NC} 创建仓库: $name" gitea_api "POST" "user/repos" "$data" | jq . } get_repository() { check_env || return 1 local owner=$1 local repo=$2 if [[ -z "$owner" || -z "$repo" ]]; then echo -e "${YELLOW}[USAGE]${NC} get_repository " return 1 fi echo -e "${BLUE}[INFO]${NC} 获取仓库信息: $owner/$repo" gitea_api "GET" "repos/$owner/$repo" | jq . } # 3. Pull Request 操作 create_pull_request() { check_env || return 1 local owner=$1 local repo=$2 local title=$3 local body=$4 local head=$5 local base=${6:-"main"} if [[ -z "$owner" || -z "$repo" || -z "$title" || -z "$head" ]]; then echo -e "${YELLOW}[USAGE]${NC} create_pull_request <body> <head> [base]" return 1 fi local data=$(jq -n \ --arg title "$title" \ --arg body "$body" \ --arg head "$head" \ --arg base "$base" \ '{title: $title, body: $body, head: $head, base: $base}') echo -e "${BLUE}[INFO]${NC} 创建 Pull Request: $title" gitea_api "POST" "repos/$owner/$repo/pulls" "$data" | jq . } list_pull_requests() { check_env || return 1 local owner=$1 local repo=$2 local state=${3:-"open"} if [[ -z "$owner" || -z "$repo" ]]; then echo -e "${YELLOW}[USAGE]${NC} list_pull_requests <owner> <repo> [state(open/closed/all)]" return 1 fi echo -e "${BLUE}[INFO]${NC} 列出 Pull Requests: $owner/$repo (状态: $state)" gitea_api "GET" "repos/$owner/$repo/pulls?state=$state" | jq '.[] | {number, title, state, user: .user.login, created_at}' } merge_pull_request() { check_env || return 1 local owner=$1 local repo=$2 local pr_number=$3 if [[ -z "$owner" || -z "$repo" || -z "$pr_number" ]]; then echo -e "${YELLOW}[USAGE]${NC} merge_pull_request <owner> <repo> <pr-number>" return 1 fi local data='{"Do":"merge"}' echo -e "${BLUE}[INFO]${NC} 合并 Pull Request: #$pr_number" gitea_api "POST" "repos/$owner/$repo/pulls/$pr_number/merge" "$data" | jq . } # 4. Issue 操作 create_issue() { check_env || return 1 local owner=$1 local repo=$2 local title=$3 local body=$4 local labels=$5 if [[ -z "$owner" || -z "$repo" || -z "$title" ]]; then echo -e "${YELLOW}[USAGE]${NC} create_issue <owner> <repo> <title> [body] [labels逗号分隔]" return 1 fi local data="{\"title\":\"$title\"" if [[ -n "$body" ]]; then data="$data, \"body\":\"$body\"" fi if [[ -n "$labels" ]]; then IFS=',' read -ra label_array <<< "$labels" local labels_json=$(printf '"%s",' "${label_array[@]}") labels_json="[${labels_json%,}]" data="$data, \"labels\":$labels_json" fi data="$data}" echo -e "${BLUE}[INFO]${NC} 创建 Issue: $title" gitea_api "POST" "repos/$owner/$repo/issues" "$data" | jq . } # 5. 分支和标签 create_branch() { check_env || return 1 local owner=$1 local repo=$2 local new_branch=$3 local from_branch=${4:-"main"} if [[ -z "$owner" || -z "$repo" || -z "$new_branch" ]]; then echo -e "${YELLOW}[USAGE]${NC} create_branch <owner> <repo> <new-branch> [from-branch]" return 1 fi local data=$(jq -n \ --arg new_branch "$new_branch" \ --arg old_branch "$from_branch" \ '{new_branch_name: $new_branch, old_branch_name: $old_branch}') echo -e "${BLUE}[INFO]${NC} 创建分支: $new_branch (从 $from_branch)" gitea_api "POST" "repos/$owner/$repo/branches" "$data" | jq . } create_tag() { check_env || return 1 local owner=$1 local repo=$2 local tag_name=$3 local message=${4:-"Release $tag_name"} local target=${5:-"main"} if [[ -z "$owner" || -z "$repo" || -z "$tag_name" ]]; then echo -e "${YELLOW}[USAGE]${NC} create_tag <owner> <repo> <tag-name> [message] [target-branch]" return 1 fi local data=$(jq -n \ --arg tag_name "$tag_name" \ --arg message "$message" \ --arg target "$target" \ '{tag_name: $tag_name, message: $message, target: $target}') echo -e "${BLUE}[INFO]${NC} 创建标签: $tag_name" gitea_api "POST" "repos/$owner/$repo/tags" "$data" | jq . } # 6. 搜索操作 search_repositories() { check_env || return 1 local query=$1 local limit=${2:-10} if [[ -z "$query" ]]; then echo -e "${YELLOW}[USAGE]${NC} search_repositories <query> [limit]" return 1 fi echo -e "${BLUE}[INFO]${NC} 搜索仓库: $query" gitea_api "GET" "repos/search?q=$query&limit=$limit" | jq '.data[] | {full_name, description, stars_count, forks_count}' } # 显示帮助信息 show_help() { echo -e "${GREEN}Gitea API 示例脚本${NC}" echo "" echo -e "${BLUE}使用方法:${NC}" echo " source api-examples.sh # 加载函数到当前shell" echo " 然后调用任意函数" echo "" echo -e "${BLUE}可用函数:${NC}" echo " 用户操作:" echo " get_current_user" echo " list_user_repos [page] [limit]" echo "" echo " 仓库操作:" echo " create_repository <name> [description] [private]" echo " get_repository <owner> <repo>" echo "" echo " Pull Request 操作:" echo " create_pull_request <owner> <repo> <title> <body> <head> [base]" echo " list_pull_requests <owner> <repo> [state]" echo " merge_pull_request <owner> <repo> <pr-number>" echo "" echo " Issue 操作:" echo " create_issue <owner> <repo> <title> [body] [labels]" echo "" echo " 分支和标签:" echo " create_branch <owner> <repo> <new-branch> [from-branch]" echo " create_tag <owner> <repo> <tag-name> [message] [target]" echo "" echo " 搜索操作:" echo " search_repositories <query> [limit]" echo "" echo -e "${YELLOW}示例:${NC}" echo " # 获取当前用户信息" echo " get_current_user" echo "" echo " # 创建新仓库" echo " create_repository \"my-new-repo\" \"My description\" false" echo "" echo " # 创建 Pull Request" echo " create_pull_request \"owner\" \"repo\" \"添加新功能\" \"详细描述\" \"feature-branch\" \"main\"" } # 如果直接运行脚本,显示帮助 if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then show_help fi