#!/bin/sh set -e echo "=== Proto-Gen Go Toolchain ===" echo "PROTO_REPO: ${PROTO_REPO}" echo "PROTO_BRANCH: ${PROTO_BRANCH}" echo "TARGET_REPO: ${TARGET_REPO}" REPO_DIR=$(mktemp -d) OUT_DIR=${OUT_DIR:-/out} mkdir -p "${OUT_DIR}" echo "--- Cloning proto repository ---" git clone --branch "${PROTO_BRANCH}" --depth 1 \ "https://${GIT_TOKEN}@${GIT_BASE_URL}/${PROTO_REPO}.git" "${REPO_DIR}/proto" PROTO_SHA=$(cd "${REPO_DIR}/proto" && git rev-parse HEAD) echo "Proto commit: ${PROTO_SHA}" echo "--- Running protoc ---" PROTO_FILES=$(find "${REPO_DIR}/proto" -name "*.proto" -type f) for proto_file in ${PROTO_FILES}; do protoc \ --proto_path="${REPO_DIR}/proto" \ --go_out="${OUT_DIR}" --go_opt=paths=source_relative \ --go-grpc_out="${OUT_DIR}" --go-grpc_opt=paths=source_relative \ "${proto_file#${REPO_DIR}/proto/}" done echo "--- Cloning target repository ---" TARGET_DIR=$(mktemp -d) if git clone "https://${GIT_TOKEN}@${GIT_BASE_URL}/${TARGET_REPO}.git" "${TARGET_DIR}/target" 2>/dev/null; then echo "Target repo exists" else echo "Creating target repository..." curl -s -X POST \ -H "Authorization: token ${GIT_TOKEN}" \ -H "Content-Type: application/json" \ -d "{\"name\": \"${TARGET_REPO}\", \"auto_init\": false}" \ "${GIT_BASE_URL}/api/v1/user/repos" git clone "https://${GIT_TOKEN}@${GIT_BASE_URL}/${TARGET_REPO}.git" "${TARGET_DIR}/target" fi echo "--- Checking for changes ---" cd "${TARGET_DIR}/target" cp -r "${OUT_DIR}"/* . 2>/dev/null || true git add -A if git diff --cached --quiet; then echo "No changes detected, idempotent exit" exit 0 fi echo "--- Committing and pushing ---" git config user.email "proto-gen@bot" git config user.name "Proto-Gen Bot" git commit -m "gen(go): from ${PROTO_REPO}@${PROTO_SHA}" git push origin "${PROTO_BRANCH}" echo "=== Done ==="