Nicolas Cantu 61cec6f430 Sync ia_dev: token resolution via .secrets/<env>/ia_token, doc updates
**Motivations:**
- Align master with current codebase (token from projects/<id>/.secrets/<env>/ia_token)
- Id resolution by mail To or by API token; no slug

**Root causes:**
- Token moved from conf.json to .secrets/<env>/ia_token; env from directory name

**Correctifs:**
- Server and scripts resolve project+env by scanning all projects and envs

**Evolutions:**
- tickets-fetch-inbox routes by To address; notary-ai agents and API doc updated

**Pages affectées:**
- ai_working_help/server.js, docs, project_config.py, lib/project_config.sh
- projects/README.md, lecoffreio/docs/API.md, gitea-issues/tickets-fetch-inbox.py
2026-03-16 15:00:23 +01:00

123 lines
4.1 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Shared config and helpers for Gitea issues scripts.
# Source from gitea-issues/*.sh after cd to project root.
#
set -euo pipefail
GITEA_ISSUES_DIR="${GITEA_ISSUES_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}"
GITEA_API_URL="${GITEA_API_URL:-https://git.4nkweb.com/api/v1}"
GITEA_REPO_OWNER="${GITEA_REPO_OWNER:-4nk}"
GITEA_REPO_NAME="${GITEA_REPO_NAME:-lecoffre_ng}"
# Optional: load project config from ia_dev (projects/<id>/conf.json); logs and data per project under projects/<id>/
PROJECT_CONFIG_PATH=""
PROJECT_LOGS_DIR=""
DATA_ISSUES_DIR=""
if [[ -f "${GITEA_ISSUES_DIR}/../lib/project_config.sh" ]]; then
PROJECT_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)" || true
if [[ -z "${PROJECT_ROOT:-}" || "$(basename "$PROJECT_ROOT")" = "ia_dev" ]]; then
PROJECT_ROOT="$(cd "${GITEA_ISSUES_DIR}/../.." && pwd)"
fi
IA_DEV_ROOT="$(cd "$GITEA_ISSUES_DIR/.." && pwd)"
if [[ -n "${PROJECT_ROOT:-}" ]]; then
# shellcheck source=../lib/project_config.sh
source "${GITEA_ISSUES_DIR}/../lib/project_config.sh"
if [[ -n "${PROJECT_SLUG:-}" && -n "${IA_DEV_ROOT:-}" ]]; then
PROJECT_LOGS_DIR="${IA_DEV_ROOT}/projects/${PROJECT_SLUG}/logs"
DATA_ISSUES_DIR="${IA_DEV_ROOT}/projects/${PROJECT_SLUG}/data/issues"
mkdir -p "${PROJECT_LOGS_DIR}" "${DATA_ISSUES_DIR}"
fi
fi
fi
export PROJECT_LOGS_DIR
export DATA_ISSUES_DIR
# Load token: GITEA_TOKEN env, then project config git.token_file, then default .secrets path
load_gitea_token() {
if [[ -n "${GITEA_TOKEN:-}" ]]; then
return 0
fi
local token_file=""
if [[ -n "${PROJECT_CONFIG_PATH:-}" && -f "$PROJECT_CONFIG_PATH" ]] && command -v jq >/dev/null 2>&1; then
local rel_path
rel_path="$(jq -r '.git.token_file // empty' "$PROJECT_CONFIG_PATH" 2>/dev/null)"
if [[ -n "$rel_path" && -n "${PROJECT_ROOT:-}" && -f "${PROJECT_ROOT}/${rel_path}" ]]; then
token_file="${PROJECT_ROOT}/${rel_path}"
fi
fi
if [[ -z "$token_file" ]]; then
token_file="${GITEA_ISSUES_DIR}/../.secrets/gitea-issues/token"
fi
if [[ -f "$token_file" ]]; then
GITEA_TOKEN="$(cat "$token_file")"
return 0
fi
echo "[gitea-issues] ERROR: GITEA_TOKEN not set and ${token_file} not found" >&2
echo "[gitea-issues] Set GITEA_TOKEN or create the token file with a Gitea Personal Access Token." >&2
return 1
}
# curl wrapper for Gitea API (GET). Usage: gitea_api_get "/repos/owner/repo/issues"
gitea_api_get() {
local path="$1"
load_gitea_token || return 1
curl -sS -H "Accept: application/json" \
-H "Authorization: token ${GITEA_TOKEN}" \
"${GITEA_API_URL}${path}"
}
# curl wrapper for Gitea API (POST). Usage: gitea_api_post "/repos/owner/repo/issues/123/comments" '{"body":"..."}'
gitea_api_post() {
local path="$1"
local data="${2:-}"
load_gitea_token || return 1
curl -sS -X POST -H "Accept: application/json" -H "Content-Type: application/json" \
-H "Authorization: token ${GITEA_TOKEN}" \
-d "$data" \
"${GITEA_API_URL}${path}"
}
# curl wrapper for Gitea API (PATCH). Usage: gitea_api_patch "/repos/owner/repo/wiki/page/Foo" '{"content_base64":"..."}'
gitea_api_patch() {
local path="$1"
local data="${2:-}"
load_gitea_token || return 1
curl -sS -X PATCH -H "Accept: application/json" -H "Content-Type: application/json" \
-H "Authorization: token ${GITEA_TOKEN}" \
-d "$data" \
"${GITEA_API_URL}${path}"
}
# curl wrapper for Gitea API (DELETE). Usage: gitea_api_delete "/repos/owner/repo/wiki/page/Foo"
gitea_api_delete() {
local path="$1"
load_gitea_token || return 1
curl -sS -X DELETE -H "Accept: application/json" \
-H "Authorization: token ${GITEA_TOKEN}" \
"${GITEA_API_URL}${path}"
}
log_ts() { date -u '+%Y-%m-%dT%H:%M:%SZ'; }
log_info() { echo "[$(log_ts)] [gitea-issues] $*"; }
log_err() { echo "[$(log_ts)] [gitea-issues] $*" >&2; }
# Require jq for JSON output
require_jq() {
if ! command -v jq &>/dev/null; then
log_err "jq is required. Install with: apt install jq / brew install jq"
return 1
fi
}
# Ensure we are in the git repo root (for create-branch, etc.)
require_git_root() {
local root
root="$(git rev-parse --show-toplevel 2>/dev/null)" || true
if [[ -z "$root" ]]; then
log_err "Not inside a git repository."
return 1
fi
cd "$root"
}